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)
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_print_template(ur_template_t *tmplt)
Print UniRec template Print static_size, first_dynamic and table of offsets to stdout (for debugging)...
char * ur_cpy_string(const char *str)
Duplicates given string. Helper function which returns pointer to duplicated string....
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_init(ur_static_field_specs_t field_specs_static)
Initialize UniRec structures Initialize UniRec structures. Function is called during defining first o...
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...
const char * ur_field_type_str[]
UniRec data types.
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 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_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)...
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...
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...
int ur_array_resize(const ur_template_t *tmplt, void *rec, int field_id, int len)
Change length of a array field.
ur_static_field_specs_t UR_FIELD_SPECS_STATIC
Structure that lists staticaly defined UniRec field specifications such as names, types,...
void ur_free_template(ur_template_t *tmplt)
Destroy UniRec template Free all memory allocated for a template created previously by ur_create_temp...
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.
void ur_finalize()
Deallocate UniRec structures Deallocate UniRec structures at the end of a program....
void * ur_create_record(const ur_template_t *tmplt, uint16_t max_var_size)
const int ur_field_type_size[]
Sizes of UniRec data types.
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...
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...
ur_field_specs_t ur_field_specs
Structure that lists UniRec field specifications such as names, types, id.
int ur_get_empty_id()
Return first empty id for new UniRec field Return first empty id for new UniRec field....
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.
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.
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...
int ur_field_array_elem_type[]
UniRec array element types.
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_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,...
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,...
char * ur_template_string_delimiter(const ur_template_t *tmplt, int delimiter)
Get UniRec specifier of the tmplt template with delimiter between fields.
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 ...
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....
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,...
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_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 ...
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.
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...
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.
int ur_get_field_type_from_str(const char *type)
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.
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...
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...
void ur_free_record(void *record)
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....
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 ...
Structure to store both IPv4 and IPv6 addresses and associated functions.
Definition of structures and functions for handling LINK_BIT_FIELD. Implementation is in unirec....
Structure to store MAC address and associated functions.
@ UR_TYPE_A_UINT32
unsigned int (32b) array
@ UR_TYPE_UINT64
unsigned int (64b)
@ UR_TYPE_A_UINT16
unsigned int (16b) array
@ UR_TYPE_A_INT64
int (64b) array
@ UR_TYPE_A_DOUBLE
double (64b) array
@ UR_TYPE_A_IP
IP address (128b) array.
@ UR_TYPE_A_INT32
int (32b) array
@ UR_TYPE_A_INT8
int (8b) array
@ UR_TYPE_A_INT16
int (8b) array
@ UR_TYPE_IP
IP address (128b)
@ UR_TYPE_A_UINT8
unsigned int (8b) array
@ UR_TYPE_DOUBLE
double (64b)
@ UR_TYPE_A_FLOAT
float (32b) array
@ UR_TYPE_A_UINT64
unsigned int (64b) array
@ UR_TYPE_A_MAC
MAC address (48b) array.
@ UR_TYPE_UINT8
unsigned int (8b)
@ UR_TYPE_STRING
var-len fields (string where only printable characters are expected; '\0' at the end should NOT be in...
@ UR_TYPE_A_TIME
time (64b) array
@ UR_TYPE_UINT16
unsigned int (16b)
@ UR_TYPE_UINT32
unsigned int (32b)
@ UR_TYPE_MAC
MAC address (48b)
@ UR_TYPE_BYTES
var-len fields (generic string of bytes)
@ UR_TYPE_FLOAT
float (32b)
ur_field_id_t ur_last_id
The highest ID of a field + 1.
ur_tmplt_direction direction
Direction of data input, output, bidirection, no direction.
ur_field_id_t ur_iter_t
Type for identifying iteration id through all fields.
ur_field_id_linked_list_t * next
Pointer to next item in the linked list.
int16_t ur_field_id_t
Type of UniRec field identifiers.
uint8_t intialized
If the UniRec is initialized by function ur_init variable is set to UR_INITIALIZED,...
ur_field_id_linked_list_t * ur_undefine_fields
linked list of free (undefined) IDs
ur_field_id_t ur_last_statically_defined_id
Last statically defined field by UR_FIELDS(...)
uint16_t count
Count of fields in template.
ur_field_type_t * ur_field_types
Array of types of fields.
char * name
Name of a field.
uint16_t first_dynamic
First dynamic (variable-length) field. Index to the ids array.
uint16_t * offset
Table of offsets.
uint32_t ifc_out
output interface number (stored only if the direction == UR_TMPLT_DIRECTION_BI)
uint16_t static_size
Size of static part.
ur_field_id_t id
ID of a field.
short * ur_field_sizes
Array of sizes of fields.
ur_field_id_t ur_last_id
Last specified ID.
@ UR_TMPLT_DIRECTION_IN
input direction
@ UR_TMPLT_DIRECTION_NO
template is not used for sending data
@ UR_TMPLT_DIRECTION_OUT
ouput direction
@ UR_TMPLT_DIRECTION_BI
bidirection
short * ur_field_sizes
Array of sizes of fields.
char ** ur_field_names
Array of names of fields.
char ** ur_field_names
Array of names of fields.
ur_field_id_t ur_allocated_fields
int16_t * ids
Array of ids in template.
ur_field_type_t * ur_field_types
Array of types of fields.
uint16_t offset_size
size of offset table.
Sorting fields structure This structure is used to sort fields by their size and name....
Linked list for undefined field ids Linked list consisting of field ids, which are freed after operat...
UniRec fields structure It contains all fields which are statically defined by UR_FIELDS(....
UniRec default field list It contains all fields which are specified statically in source code of a m...
UniRec template. It contains a table mapping a field to its position in an UniRec record.
Types, macros and function for UniRec timestamp format.