53 #ifndef __STDC_LIMIT_MACROS 54 #define __STDC_LIMIT_MACROS 63 #include <libtrap/trap.h> 66 #define UR_DEFAULT_LENGTH_OF_TEMPLATE 1024 67 #define UR_DEFAULT_LENGTH_OF_FIELD_NAME 128 68 #define UR_DEFAULT_LENGTH_OF_FIELD_TYPE 16 69 #define UR_INITIAL_SIZE_FIELDS_TABLE 5 70 #define UR_FIELD_ID_MAX INT16_MAX 71 #define UR_FIELDS(...) 72 #define UR_ARRAY_DELIMITER ' ' 73 #define UR_ARRAY_ALLOC 10 75 #define UR_ITER_BEGIN (-1) 76 #define UR_ITER_END INT16_MAX 78 #define UR_INVALID_OFFSET 0xffff 79 #define UR_NO_DYNAMIC_VALUES 0xffff 80 #define UR_UNINITIALIZED 0 81 #define UR_INITIALIZED 1 82 #define UR_INVALID_FIELD ((ur_field_id_t) 0xffff) 84 #define UR_E_INVALID_PARAMETER -6 85 #define UR_E_INVALID_FIELD_ID -5 86 #define UR_E_TYPE_MISMATCH -4 87 #define UR_E_INVALID_NAME -3 88 #define UR_E_INVALID_TYPE -2 89 #define UR_E_MEMORY -1 94 #define UR_COUNT_OF_TYPES 29 145 ur_field_id_linked_list_t *
next;
182 typedef struct field_spec_s {
219 #define TRAP_RECEIVE(ifc_num, data, data_size, tmplt) \ 220 TRAP_CTX_RECEIVE(trap_get_global_ctx(), ifc_num, data, data_size, tmplt); 233 #define TRAP_CTX_RECEIVE(ctx, ifc_num, data, data_size, tmplt) __extension__ \ 235 int ret = trap_ctx_recv(ctx, ifc_num, &data, &data_size);\ 236 if (ret == TRAP_E_FORMAT_CHANGED) {\ 237 const char *spec = NULL;\ 239 if (trap_ctx_get_data_fmt(ctx, TRAPIFC_INPUT, ifc_num, &data_fmt, &spec) != TRAP_E_OK) {\ 240 fprintf(stderr, "Data format was not loaded.\n");\ 242 tmplt = ur_define_fields_and_update_template(spec, tmplt);\ 243 if (tmplt == NULL) {\ 244 fprintf(stderr, "Template could not be edited.\n");\ 246 if (tmplt->direction == UR_TMPLT_DIRECTION_BI) {\ 247 char * spec_cpy = ur_cpy_string(spec);\ 248 if (spec_cpy == NULL) {\ 249 fprintf(stderr, "Memory allocation problem.\n");\ 251 trap_ctx_set_data_fmt(ctx, tmplt->ifc_out, TRAP_FMT_UNIREC, spec_cpy);\ 364 #define ur_template_string(tmplt) \ 365 ur_template_string_delimiter(tmplt, ',') 372 #define ur_size_of(type) \ 373 ur_field_type_size[type] 380 #define ur_get_name(field_id) \ 381 ur_field_specs.ur_field_names[field_id] 388 #define ur_get_type(field_id) \ 389 ur_field_specs.ur_field_types[field_id] 405 #define ur_get_size(field_id) \ 406 ur_field_specs.ur_field_sizes[field_id] 418 #define ur_get(tmplt, data, field_id) \ 419 (*(field_id ## _T*)((char *)(data) + (tmplt)->offset[field_id])) 430 #define ur_get_ptr(tmplt, data, field_id) \ 431 (field_id ## _T*)((char *) (ur_is_static(field_id) ? ((char *)(data) + (tmplt)->offset[field_id]) : \ 432 (char *)(data) + (tmplt)->static_size + (*((uint16_t *)((char *)(data) + (tmplt)->offset[field_id]))))) 442 #define ur_get_ptr_by_id(tmplt, data, field_id) \ 443 (void *)((char *) (ur_is_static(field_id) ? ((char *)(data) + (tmplt)->offset[field_id]) : \ 444 (char *)(data) + (tmplt)->static_size + (*((uint16_t *)((char *)(data) + (tmplt)->offset[field_id]))))) 454 #define ur_set(tmplt, data, field_id, value) \ 455 (*(field_id ## _T*)((char *)(data) + (tmplt)->offset[field_id]) = (value)) 464 #define ur_get_var_offset(tmplt, rec, field_id) \ 465 *((uint16_t *)((char *) rec + tmplt->offset[field_id])) 474 #define ur_get_var_len(tmplt, rec, field_id) \ 475 *((uint16_t *)((char *) rec + tmplt->offset[field_id] + 2)) 485 #define ur_set_var_len(tmplt, rec, field_id, len) \ 486 *((uint16_t *)((char *) rec + tmplt->offset[field_id] + 2)) = (uint16_t) len 496 #define ur_set_var_offset(tmplt, rec, field_id, offset_val) \ 497 *((uint16_t *)((char *) rec + tmplt->offset[field_id])) = (uint16_t) offset_val 506 #define ur_get_len(tmplt, rec, field) \ 507 ur_is_static(field) ? ur_get_size(field) : ur_get_var_len(tmplt, rec, field) 517 #define ur_set_string(tmplt, rec, field_id, str) \ 518 ur_set_var(tmplt, rec, field_id, str, strlen(str)) 526 #define ur_is_array(field_id) \ 527 (ur_field_type_size[ur_get_type(field_id)] < 0 ? 1 : 0) 535 #define ur_array_get_elem_size(field_id) \ 536 (ur_field_type_size[ur_get_type(field_id)] < 0 ? -ur_field_type_size[ur_get_type(field_id)] : ur_field_type_size[ur_get_type(field_id)]) 546 #define ur_array_get_elem_cnt(tmplt, rec, field_id) \ 547 (ur_get_var_len(tmplt, rec, field_id) / ur_array_get_elem_size(field_id)) 555 #define ur_array_get_elem_type(field_id) \ 556 ur_field_array_elem_type[ur_get_type(field_id)] 568 #define ur_array_set(tmplt, rec, field_id, index, element) \ 569 if ((index) * ur_array_get_elem_size(field_id) < ur_get_var_len(tmplt, rec, field_id) || ur_array_resize(tmplt, rec, field_id, (index + 1) * ur_array_get_elem_size(field_id)) == UR_OK) { \ 570 (((field_id ## _T)((char *)(ur_get_ptr_by_id(tmplt, rec, field_id))))[index] = (element)); \ 581 #define ur_array_append(tmplt, rec, field_id, element) \ 582 if (ur_array_resize(tmplt, rec, field_id, (ur_array_get_elem_cnt(tmplt, rec, field_id) + 1) * ur_array_get_elem_size(field_id)) == UR_OK) { \ 583 (((field_id ## _T)((char *)(ur_get_ptr_by_id(tmplt, rec, field_id))))[ur_array_get_elem_cnt(tmplt, rec, field_id) - 1] = (element)); \ 604 #define ur_array_clear(tmplt, rec, field_id) \ 605 ur_array_resize(tmplt, rec, field_id, 0) 617 #define ur_array_get(tmplt, rec, field_id, index) \ 618 ((field_id ## _T)((char *)(ur_get_ptr_by_id(tmplt, rec, field_id))))[index] 629 #define ur_array_allocate(tmplt, rec, field_id, elem_cnt) \ 630 ur_array_resize(tmplt, rec, field_id, (elem_cnt) * ur_array_get_elem_size(field_id)) 638 #define ur_is_present(tmplt, field_id) \ 639 ((tmplt)->offset_size > (field_id) && (tmplt)->offset[(field_id)] != UR_INVALID_OFFSET) 646 #define ur_is_varlen(field_id) \ 647 (ur_field_specs.ur_field_sizes[field_id] < 0) 650 #define ur_is_dynamic(field_id) \ 651 ur_is_varlen(field_id) 658 #define ur_is_fixlen(field_id) \ 659 (ur_field_specs.ur_field_sizes[field_id] >= 0) 662 #define ur_is_static(field_id) \ 663 ur_is_fixlen(field_id) 671 #define ur_rec_fixlen_size(tmplt) \ 672 ((tmplt)->static_size) 680 #define ur_rec_size(tmplt, rec) \ 681 (ur_rec_fixlen_size(tmplt) + ur_rec_varlen_size(tmplt, rec)) 793 #define ur_create_input_template(ifc, fields, errstr) \ 794 ur_ctx_create_input_template(trap_get_global_ctx(), ifc, fields, errstr); 806 #define ur_create_output_template(ifc, fields, errstr) \ 807 ur_ctx_create_output_template(trap_get_global_ctx(), ifc, fields, errstr); 847 #define ur_set_output_template(ifc, tmplt) \ 848 ur_ctx_set_output_template(trap_get_global_ctx(), ifc, tmplt) 863 #define ur_set_input_template(ifc, tmplt) \ 864 ur_ctx_set_input_template(trap_get_global_ctx(), ifc, tmplt) 890 #define ur_create_bidirectional_template(ifc_in, ifc_out, fields, errstr) \ 891 ur_ctx_create_bidirectional_template(trap_get_global_ctx(), ifc_in, ifc_out, fields, errstr); 1013 #define UR_MAX_SIZE 0xffff 1056 #define ur_cpy(tmplt, src, dst) \ 1057 (memcpy(dst,src,ur_rec_size(tmplt,src))) 1176 #define ur_values_get_name(field, value) \ 1177 ur_values_get_name_start_end(UR_TYPE_START_ ## field, UR_TYPE_END_ ## field, value) 1186 #define ur_values_get_description(field, value) \ 1187 ur_values_get_description_start_end(UR_TYPE_START_ ## field, UR_TYPE_END_ ## field, value) var-len fields (string where only printable characters are expected; '\0' at the end should NOT be in...
ur_iter_t ur_iter_fields(const ur_template_t *tmplt, ur_iter_t id)
Iterate over fields of a template in order of a record This function can be used to iterate over all ...
int ur_set_array_from_string(const ur_template_t *tmpl, void *data, ur_field_id_t f_id, const char *v)
Set value of a UniRec array field.
ur_tmplt_direction direction
Direction of data input, output, bidirection, no direction.
Sorting fields structure This structure is used to sort fields by their size and name. The structure is passed to the sorting algorithm.
int ur_define_set_of_fields(const char *ifc_data_fmt)
Define set of new UniRec fields Define new UniRec fields at run-time. It adds new fields into existin...
void * ur_clone_record(const ur_template_t *tmplt, const void *src)
Create new UniRec and copy the source UniRec into it. Function creates new UniRec record and fills it...
ur_field_id_t ur_allocated_fields
ur_field_id_t id
ID of a field.
int ur_get_empty_id()
Return first empty id for new UniRec field Return first empty id for new UniRec field. If there is no space in the UniRec structures, it will increase space in the existing structures.
ur_template_t * ur_create_template(const char *fields, char **errstr)
Create UniRec template Create new UniRec template specified by a string containing names of its field...
Structure to store both IPv4 and IPv6 addresses and associated functions.
void ur_clear_varlen(const ur_template_t *tmplt, void *rec)
Clear variable-length part of a record. For better performance of setting content to variable-length ...
int ur_define_field(const char *name, ur_field_type_t type)
Define new UniRec field Define new UniRec field at run-time. It adds new field into existing structur...
const char * ur_values_get_description_start_end(uint32_t start, uint32_t end, int32_t value)
Returns description of specified value (Helper function) Helper function for ur_values_get_descriptio...
ur_static_field_specs_t UR_FIELD_SPECS_STATIC
Structure that lists staticaly defined UniRec field specifications such as names, types...
Linked list for undefined field ids Linked list consisting of field ids, which are freed after operat...
short * ur_field_sizes
Array of sizes of fields.
int ur_ctx_set_output_template(trap_ctx_t *ctx, int ifc, ur_template_t *tmplt)
Set UniRec template to ouput interface on specified context.
char * ur_cpy_string(const char *str)
Duplicates given string. Helper function which returns pointer to duplicated string. New string has to be freed by user.
short * ur_field_sizes
Array of sizes of fields.
int ur_set_from_string(const ur_template_t *tmpl, void *data, ur_field_id_t f_id, const char *v)
Set value of a UniRec field.
ur_template_t * ur_ctx_create_output_template(trap_ctx_t *ctx, int ifc, const char *fields, char **errstr)
Create UniRec template and set it to output interface on specified context Creates UniRec template...
int ur_field_array_elem_type[]
UniRec array element types.
char ** ur_field_names
Array of names of fields.
char ** ur_field_names
Array of names of fields.
char * name
Name of a field.
Structure to store MAC address and associated functions.
UniRec default field list It contains all fields which are specified statically in source code of a m...
const int ur_field_type_size[]
Sizes of UniRec data types.
int ur_get_field_type_from_str(const char *type)
char * ur_array_append_get_ptr(const ur_template_t *tmplt, void *rec, int field_id)
Allocate new element at the end of array and return its pointer.
ur_template_t * ur_expand_template(const char *ifc_data_fmt, ur_template_t *tmplt)
Expand UniRec template Expand existing UniRec template by a string containing types and names of its ...
ur_template_t * ur_define_fields_and_update_template(const char *ifc_data_fmt, ur_template_t *tmplt)
Defined new fields and expand an UniRec template Define new fields (function ur_define_set_of_fields)...
void ur_finalize()
Deallocate UniRec structures Deallocate UniRec structures at the end of a program. This function SHOULD be called after all UniRec functions and macros invocations, typically during a cleanup phase before the program's end. This function has to be called if some fields are defined during run-time, otherwise this function is needless.
ur_template_t * ur_ctx_create_input_template(trap_ctx_t *ctx, int ifc, const char *fields, char **errstr)
Create UniRec template and set it to input interface on specified context Creates UniRec template...
int ur_set_var(const ur_template_t *tmplt, void *rec, int field_id, const void *val_ptr, int val_len)
Set content of variable-length UniRec field Copy given data into variable-length UniRec field...
char * ur_get_var_as_str(const ur_template_t *tmplt, const void *rec, ur_field_id_t field_id)
Get variable-length UniRec field as a C string Copy data of a variable-length field from UniRec recor...
template is not used for sending data
Types, macros and function for UniRec timestamp format.
ur_field_type_t * ur_field_types
Array of types of fields.
ur_field_specs_t ur_field_specs
Structure that lists UniRec field specifications such as names, types, id.
Definition of structures and functions for handling LINK_BIT_FIELD. Implementation is in unirec...
int ur_undefine_field_by_id(ur_field_id_t field_id)
Undefine UniRec field by its id Undefine UniRec field created at run-time. It erases given field from...
void * ur_create_record(const ur_template_t *tmplt, uint16_t max_var_size)
uint16_t count
Count of fields in template.
UniRec fields structure It contains all fields which are statically defined by UR_FIELDS(...) and run-time generated fields. This structure can be modified during run-time by generating new fields and erasing existing fields.
int ur_undefine_field(const char *name)
Undefine UniRec field by its name Undefine UniRec field created at run-time. It erases given field fr...
ur_template_t * ur_create_template_from_ifc_spec(const char *ifc_data_fmt)
Create UniRec template from data format string. Creates new UniRec template (function ur_create_templ...
int16_t * ids
Array of ids in template.
void ur_free_record(void *record)
int16_t ur_field_id_t
Type of UniRec field identifiers.
uint16_t ur_rec_varlen_size(const ur_template_t *tmplt, const void *rec)
Get size of variable sized part of UniRec record Get total size of all variable-length fields in an U...
void ur_free_template(ur_template_t *tmplt)
Destroy UniRec template Free all memory allocated for a template created previously by ur_create_temp...
ur_field_id_t ur_last_id
Last specified ID.
ur_template_t * ur_ctx_create_bidirectional_template(trap_ctx_t *ctx, int ifc_in, int ifc_out, const char *fields, char **errstr)
Create UniRec template and set it to input and output interface on specified context Creates UniRec t...
var-len fields (generic string of bytes)
uint16_t static_size
Size of static part.
uint32_t ifc_out
output interface number (stored only if the direction == UR_TMPLT_DIRECTION_BI)
int ur_array_resize(const ur_template_t *tmplt, void *rec, int field_id, int len)
Change length of a array field.
ur_field_id_t ur_last_statically_defined_id
Last statically defined field by UR_FIELDS(...)
ur_field_id_t ur_last_id
The highest ID of a field + 1.
int ur_init(ur_static_field_specs_t field_specs_static)
Initialize UniRec structures Initialize UniRec structures. Function is called during defining first o...
int ur_ctx_set_input_template(trap_ctx_t *ctx, int ifc, ur_template_t *tmplt)
Set UniRec template to input interface on specified context.
ur_field_id_t ur_iter_t
Type for identifying iteration id through all fields.
void ur_print_template(ur_template_t *tmplt)
Print UniRec template Print static_size, first_dynamic and table of offsets to stdout (for debugging)...
ur_field_id_linked_list_t * ur_undefine_fields
linked list of free (undefined) IDs
char * ur_ifc_data_fmt_to_field_names(const char *ifc_data_fmt)
Parses field names from data format Function parses field names from data format and returns pointer ...
int compare_fields(const void *field1, const void *field2)
Compare fields Compare two fields. This function is for sorting the fields in the right order...
char * ur_template_string_delimiter(const ur_template_t *tmplt, int delimiter)
Get UniRec specifier of the tmplt template with delimiter between fields.
uint16_t * offset
Table of offsets.
uint8_t intialized
If the UniRec is initialized by function ur_init variable is set to UR_INITIALIZED, otherwise 0.
uint16_t first_dynamic
First dynamic (variable-length) field. Index to the ids array.
ur_field_id_linked_list_t * next
Pointer to next item in the linked list.
ur_field_type_t * ur_field_types
Array of types of fields.
void ur_copy_fields(const ur_template_t *dst_tmplt, void *dst, const ur_template_t *src_tmplt, const void *src)
Copy data from one UniRec record to another. Copies all fields present in both templates from src to ...
UniRec template. It contains a table mapping a field to its position in an UniRec record...
uint16_t offset_size
size of offset table.
int ur_template_compare(const ur_template_t *tmpltA, const ur_template_t *tmpltB)
Compares fields of two UniRec templates Function compares only sets of UniRec fields (direction is no...
ur_iter_t ur_iter_fields_record_order(const ur_template_t *tmplt, int index)
Iterate over fields of a template This function can be used to iterate over all fields of a given tem...
int ur_get_id_by_name(const char *name)
Get ID of a field by its name Get ID of a field by its name.
const char * ur_field_type_str[]
UniRec data types.
const char * ur_values_get_name_start_end(uint32_t start, uint32_t end, int32_t value)
Returns name of specified value (Helper function) Helper function for ur_values_get_name. This function returns name of specified value and field, which is defined in values file. Function needs start and end index of a field.