DeviceAtlas Enterprise C API  2.0
Welcome DeviceAtlas Enterprise C API
dac.h
Go to the documentation of this file.
1 #ifndef MOBI_DA_DAC_H
2 #define MOBI_DA_DAC_H
3 
11 #include <sys/types.h>
12 #include <pcre.h>
13 #include <limits.h>
14 #include <inttypes.h>
15 #include <stdlib.h>
16 #include <stdarg.h>
17 
18 #ifndef __cplusplus
19 #ifndef true
20 #define bool _Bool
21 
22 #define true 1
23 #define false 0
24 #endif
25 #endif
26 
31 enum da_type {
40 };
41 
46 enum da_status {
47  DA_OK, /* Success. */
48  DA_INVALID_JSON, /* The JSON format is invalid, or the content is unexpected in a given context. */
49  DA_OVERFLOW, /* Overflow occurred. Note this is used to indicate an unfinished string parse in JSON */
50  DA_FORMAT_ERROR, /* The data supplied is formatted incorrectly. */
51  DA_NOMEM, /* There was not enough space to complete the operation */
52  DA_SYS, /* A system error occurred - consult the OS for more details (eg, check errno) */
53  DA_NOTIMPL, /* This method is not implemented */
54  DA_NOTFOUND, /* The requested item was not found. */
55  DA_REGEXBAD, /* An invalid regex was provided. */
56  DA_NOMORE, /* Used to indicate the end of an iterator. */
57  DA_INVALID_COOKIE, /* Cookie value supplied was invalid */
58  DA_INVALID_TYPE, /* A value of an unexpected type was found. */
60  DA_STATUS_LAST /* Placeholder to indicate highest possible error value. (value will change as API matures) */
61 };
62 
64  DA_SEV_FATAL, /* The operation will not continue, and the operation will return an error. */
65  DA_SEV_ERROR, /* An error occurred, but the API call will return at least some valid information */
66  DA_SEV_WARN, /* An unexpected event occured, but the system dealt with it */
67  DA_SEV_INFO /* An informational message. */
68 };
69 /* Forward references to tagged types */
70 struct atlas_image;
71 struct da_atlas;
72 struct da_deviceinfo;
73 struct da_jsonparser;
74 struct da_node;
75 struct da_propset;
76 union da_value;
77 struct da_evidence;
78 struct da_bitset;
79 struct da_allocator;
80 
87 typedef enum da_severity da_severity_t; /* A severity for the error callback. */
88 typedef enum da_status da_status_t; /* An error code - returned from most API calls. */
89 typedef da_status_t (*da_setpos_fn)(void *ctx, off_t off); /* callback provided to API to rewind input stream */
90 typedef enum da_type da_type_t; /* A value type (integer, string, etc) */
91 
99 typedef int da_evidence_id_t;
100 
111 typedef int da_propid_t;
112 typedef size_t (*da_read_fn)(void *ctx, size_t maxlen, char *ptr);
113 typedef struct da_atlas da_atlas_t;
115 typedef struct da_evidence da_evidence_t;
116 typedef struct da_jsonparser da_jsonparser_t;
117 typedef struct da_node da_node_t;
119 typedef struct da_propset da_propset_t;
120 typedef void *(*da_alloc_fn)(void *ctx, size_t);
121 typedef void (*da_free_fn)(void *ctx, void *);
122 typedef void *(*da_realloc_fn)(void *ctx, void *, size_t);
123 typedef void (*da_errorfunc_t)(da_severity_t severity, da_status_t status, const char *msg, va_list args);
124 
125 
126 /* Manifest constants. */
127 enum {
128  /*
129  * used as the initial guess for the compiled size of an atlas.
130  * If atlas sizes grow more beyond this, it can be expanded to avoid multiple scans of the data.
131  */
132  DA_INITIAL_MEMORY_ESTIMATE = 1024 * 1024 * 30
133 };
134 
135 
143 void da_init();
144 
145 
149 void da_fini();
150 
158 void da_seterrorfunc(da_errorfunc_t callback);
159 
167 da_evidence_id_t da_atlas_header_evidence_id(const da_atlas_t *atlas, const char *header_name);
174 
189 da_status_t da_atlas_compile(void *ctx, da_read_fn readfn, da_setpos_fn setposfn, void **ptr, size_t *len);
190 
204 da_status_t da_atlas_open(da_atlas_t *atlas, da_property_decl_t *extra_props, const void *ptr, size_t pos);
205 
211 void da_atlas_close(da_atlas_t *atlas);
212 
223 da_status_t da_searchv(const da_atlas_t *atlas, da_deviceinfo_t *info, da_evidence_t *ev, size_t count);
224 
237 da_status_t da_search(const da_atlas_t *atlas, da_deviceinfo_t *info, ...);
238 
243 void da_close(da_deviceinfo_t *info);
244 
253 da_status_t da_atlas_getpropid(const da_atlas_t *atlas, const char *propname, da_propid_t *propid);
254 
267 
279 da_status_t da_atlas_getpropname(const da_atlas_t *atlas, da_propid_t propid, const char **propname);
280 
296 da_status_t da_getpropstring(const da_deviceinfo_t *info, da_propid_t propid, const char **value);
304 da_status_t da_getpropinteger(const da_deviceinfo_t *info, da_propid_t propid, long *value);
312 da_status_t da_getpropboolean(const da_deviceinfo_t *info, da_propid_t propid, bool *value);
320 da_status_t da_getpropfloat(const da_deviceinfo_t *info, da_propid_t propid, double *value);
321 
349 da_status_t da_getpropname(const da_deviceinfo_t *info, da_propid_t propid, const char **propname);
350 
383 
391 da_status_t da_reporterror(da_status_t severity, const char *fmt, ...);
392 
398 const char *da_typename(da_type_t type);
399 
406 const char *da_atlas_get_property_name(const da_atlas_t *atlas, da_propid_t propid);
407 
413 size_t da_getpropcount(const da_deviceinfo_t *info);
414 
415 
416 /*
417  * Details below should not be required for usage of the API
418  */
419 
426 struct da_atlas {
427  const struct atlas_image *image;
428  struct header_evidence_entry *header_priorities;
430 
431  struct pcre_regex_info *uar_regexes;
433 
434  struct pcre_regex_info *replacement_regexes;
436 
440 
443 
445  size_t cpr_count;
446 };
447 
448 /* fixed constants. */
449 enum {
450  DA_BUFSIZE = 16000
451 };
452 
458 struct da_buf {
459  struct da_buf *next;
460  char *cur;
461  char *limit;
463 };
464 
469 struct da_allocator {
473  void *context;
474 };
475 
487  const da_atlas_t *atlas; /* reference to the atlas the search was carried out on. */
488  struct da_bitset *present; /* property received from tree */
489  struct da_bitset *localprop; /* property was received from UAR rule or CPR */
490  struct da_bitset *cprprop; /* property was received from CPR */
491  union da_value *properties; /* properties - indexed by property id. */
492  da_propid_t *proplist; /* list of properties present in this result. */
493  size_t propcount; /* size of proplist */
494  da_propset_t *local_types; /* property descriptors local to this search result. */
495 
502  struct da_buf *heap;
504 };
505 
509 struct da_evidence {
511  char *value;
512 };
513 
520  const char *name;
522 };
523 
524 
525 #endif /* DEVATLAS_DAC_H */
da_status_t da_reporterror(da_status_t severity, const char *fmt,...)
Report an error, as per a report from the API to the user-callback.
da_propid_t * proplist
Definition: dac.h:492
enum da_severity da_severity_t
Primary types of the interface.
Definition: dac.h:87
da_status_t da_searchv(const da_atlas_t *atlas, da_deviceinfo_t *info, da_evidence_t *ev, size_t count)
Find device properties given a set of evidence.
Definition: dac.h:37
da_status_t da_getpropname(const da_deviceinfo_t *info, da_propid_t propid, const char **propname)
returns the name of a given device property from the search functions
da_propid_t id_propid
Definition: dac.h:442
da_status_t da_atlas_getpropname(const da_atlas_t *atlas, da_propid_t propid, const char **propname)
Given a property ID, return the name of that property.
da_evidence_id_t da_atlas_header_evidence_id(const da_atlas_t *atlas, const char *header_name)
Given a specific HTTP header, return the associated ID for that header.
struct da_propset da_propset_t
Definition: dac.h:119
Definition: dac.h:52
da_status_t da_getpropfloat(const da_deviceinfo_t *info, da_propid_t propid, double *value)
returns a property value as a float from a given float typed property id
da_status_t da_getproptype(const da_deviceinfo_t *info, da_propid_t propid, da_type_t *type)
Some properties may not be not known to the atlas before the search commences.
void da_init()
Functional interface.
int da_evidence_id_t
An operation on an atlas involves converting a set of evidence strings into a set of property/value p...
Definition: dac.h:99
Used to pass evidence to da_searchv()
Definition: dac.h:509
Definition: dac.h:48
Definition: dac.h:53
da_status_t da_getpropstring(const da_deviceinfo_t *info, da_propid_t propid, const char **value)
Given a search result, find the value of a specific property.
size_t propcount
Definition: dac.h:493
Definition: dac.h:55
da_status
Any method that returns a da_status may potentially fail for one of these reasons.
Definition: dac.h:46
Definition: dac.h:39
void *(* da_realloc_fn)(void *ctx, void *, size_t)
Definition: dac.h:122
struct da_jsonparser da_jsonparser_t
Definition: dac.h:116
size_t header_evidence_count
Definition: dac.h:429
Definition: dac.h:36
da_status_t da_atlas_open(da_atlas_t *atlas, da_property_decl_t *extra_props, const void *ptr, size_t pos)
opens a previously compiled atlas for operations.
size_t replacement_regex_count
Definition: dac.h:435
Used to pass properties the API intends to query to the da_atlas_open function This can be used to im...
Definition: dac.h:519
Represents a search result Can be used to retrieve values of known properties discovered from the evi...
Definition: dac.h:485
da_status_t da_atlas_getpropid(const da_atlas_t *atlas, const char *propname, da_propid_t *propid)
Given a property name (Eg, "displayWidth"), return the property ID associated with it for the specifi...
da_deviceinfo_t ** cpr_props
Definition: dac.h:444
Definition: dac.h:47
struct header_evidence_entry * header_priorities
Definition: dac.h:428
Definition: dac.h:56
da_status_t da_atlas_getproptype(const da_atlas_t *atlas, da_propid_t propid, da_type_t *type)
Given a property ID, return the type of that property.
void da_seterrorfunc(da_errorfunc_t callback)
User-supplied callback to be invoked with information about an error.
struct da_bitset * cprprop
Definition: dac.h:490
char buf[DA_BUFSIZE]
Definition: dac.h:462
da_severity
Definition: dac.h:63
enum da_status da_status_t
Definition: dac.h:88
enum da_type da_type_t
Definition: dac.h:90
Definition: dac.h:34
Definition: dac.h:66
Definition: dac.h:38
Definition: dac.h:32
struct da_buf initial_heap
Definition: dac.h:503
const struct atlas_image * image
Definition: dac.h:427
Definition: dac.h:54
void(* da_free_fn)(void *ctx, void *)
Definition: dac.h:121
Definition: dac.h:65
void(* da_errorfunc_t)(da_severity_t severity, da_status_t status, const char *msg, va_list args)
Definition: dac.h:123
void * context
Definition: dac.h:473
size_t uar_regex_count
Definition: dac.h:432
Definition: dac.h:50
da_status_t da_search(const da_atlas_t *atlas, da_deviceinfo_t *info,...)
As da_search, but unrolls the evidence array into variable arguments for simpler calling convention w...
Definition: dac.h:58
char * cur
Definition: dac.h:460
da_realloc_fn realloc
Definition: dac.h:472
struct da_node da_node_t
Definition: dac.h:117
da_type
All values returned by the API have one of these types.
Definition: dac.h:31
char * value
Definition: dac.h:511
Definition: dac.h:60
da_evidence_id_t next_evidence
Definition: dac.h:439
void da_close(da_deviceinfo_t *info)
After finishing with a search result, release resources associated with it.
da_alloc_fn alloc
Definition: dac.h:470
da_evidence_id_t user_agent_evidence
Definition: dac.h:437
size_t cpr_count
Definition: dac.h:445
size_t(* da_read_fn)(void *ctx, size_t maxlen, char *ptr)
Definition: dac.h:112
da_evidence_id_t da_atlas_clientprop_evidence_id(const da_atlas_t *atlas)
Return the associated ID of the client side properties evidence.
struct pcre_regex_info * replacement_regexes
Definition: dac.h:434
Represents a usable device atlas interface.
Definition: dac.h:426
int da_propid_t
The search result encompasses a key/value set.
Definition: dac.h:111
da_status_t da_atlas_compile(void *ctx, da_read_fn readfn, da_setpos_fn setposfn, void **ptr, size_t *len)
readfn should present JSON content from ctx.
da_status_t da_getpropboolean(const da_deviceinfo_t *info, da_propid_t propid, bool *value)
returns a property value as a boolean from a given boolean typed property id
da_free_fn free
Definition: dac.h:471
struct da_bitset * present
Definition: dac.h:488
da_type_t type
Definition: dac.h:521
void da_atlas_close(da_atlas_t *atlas)
Release any resources associated with the atlas structure atlas, which was previously generated from ...
da_propset_t * properties
Definition: dac.h:441
Definition: dac.h:49
struct pcre_regex_info * uar_regexes
Definition: dac.h:431
const char * da_typename(da_type_t type)
returns a textual description of the type "type".
Definition: dac.h:132
Definition: dac.h:57
da_status_t(* da_setpos_fn)(void *ctx, off_t off)
Definition: dac.h:89
Definition: dac.h:67
da_propset_t * local_types
Definition: dac.h:494
Definition: dac.h:33
da_status_t da_getfirstprop(const da_deviceinfo_t *info, da_propid_t **propid)
da_getfirstprop/da_getnextprop provide iteration over all properties in a search result.
void da_fini()
Release all resources used by the API.
Definition: dac.h:51
struct da_allocator allocator
Definition: dac.h:486
union da_value * properties
Definition: dac.h:491
Definition: dac.h:450
da_evidence_id_t key
Definition: dac.h:510
struct da_buf * next
Definition: dac.h:459
Represents a chunk of memory.
Definition: dac.h:458
Definition: dac.h:64
struct da_bitset * localprop
Definition: dac.h:489
A callback interface for allocating memory from some source Not for public consumption.
Definition: dac.h:469
Definition: dac.h:59
da_evidence_id_t clientprops_evidence
Definition: dac.h:438
Definition: dac.h:35
char * limit
Definition: dac.h:461
struct da_buf * heap
The per-deviceinfo heap is stored here.
Definition: dac.h:502
da_status_t da_getpropinteger(const da_deviceinfo_t *info, da_propid_t propid, long *value)
returns a property value as a long from a given long typed property id
const da_atlas_t * atlas
Definition: dac.h:487
da_status_t da_getnextprop(const da_deviceinfo_t *info, da_propid_t **propid)
device info properties iterator
const char * da_atlas_get_property_name(const da_atlas_t *atlas, da_propid_t propid)
returns the name of a global property
void *(* da_alloc_fn)(void *ctx, size_t)
Definition: dac.h:120
const char * name
Definition: dac.h:520
size_t da_getpropcount(const da_deviceinfo_t *info)
returns the number of properties in a result.