24 #include "util/oc_list.h"
25 #include "util/oc_mmem.h"
35 typedef struct oc_mmem oc_handle_t, oc_string_t, oc_array_t, oc_string_array_t,
36 oc_byte_string_array_t;
38 enum StringRepresentation {
39 DEC_REPRESENTATION = 0,
43 #define oc_cast(block, type) ((type *)(OC_MMEM_PTR(&(block))))
49 #define oc_string(ocstring) (oc_cast(ocstring, char))
56 #define oc_string_checked(ocstring) \
57 (oc_cast(ocstring, char) ? oc_cast(ocstring, char) : "NULL")
59 #ifdef OC_MEMORY_TRACE
60 #define oc_alloc_string(ocstring, size) \
61 _oc_alloc_string(__func__, ocstring, size)
62 #define oc_new_string(ocstring, str, str_len) \
63 _oc_new_string(__func__, ocstring, str, str_len)
65 #define oc_free_string(ocstring) _oc_free_string(__func__, ocstring)
66 #define oc_free_int_array(ocarray) (_oc_free_array(__func__, ocarray, INT_POOL))
67 #define oc_free_bool_array(ocarray) \
68 (_oc_free_array(__func__, ocarray, BYTE_POOL))
69 #define oc_free_float_array(ocarray) \
70 (_oc_free_array(__func__, ocarray, FLOAT_POOL))
71 #define oc_free_double_array(ocarray) \
72 (_oc_free_array(__func__, ocarray, DOUBLE_POOL))
74 #define oc_new_int_array(ocarray, size) \
75 (_oc_new_array(__func__, ocarray, size, INT_POOL))
76 #define oc_new_bool_array(ocarray, size) \
77 (_oc_new_array(__func__, ocarray, size, BYTE_POOL))
78 #define oc_new_float_array(ocarray, size) \
79 (_oc_new_array(__func__, ocarray, size, FLOAT_POOL))
80 #define oc_new_double_array(ocarray, size) \
81 (_oc_new_array(__func__, ocarray, size, DOUBLE_POOL))
83 #define oc_new_string_array(ocstringarray, size) \
84 (_oc_alloc_string_array(__func__, ocstringarray, size))
86 #define oc_free_string_array(ocstringarray) \
87 (_oc_free_string(__func__, ocstringarray))
89 #define oc_new_byte_string_array(ocstringarray, size) \
90 (_oc_alloc_string_array(__func__, ocstringarray, size))
92 #define oc_free_byte_string_array(ocstringarray) \
93 (__func__, _oc_free_string(ocstringarray))
101 #define oc_alloc_string(ocstring, size) _oc_alloc_string((ocstring), (size))
107 #define oc_new_string(ocstring, str, str_len) \
108 _oc_new_string(ocstring, str, str_len)
114 #define oc_new_byte_string(ocstring, str, str_len) \
115 _oc_new_byte_string(ocstring, str, str_len)
121 #define oc_free_string(ocstring) _oc_free_string(ocstring)
127 #define oc_free_int_array(ocarray) (_oc_free_array(ocarray, INT_POOL))
133 #define oc_free_bool_array(ocarray) (_oc_free_array(ocarray, BYTE_POOL))
139 #define oc_free_float_array(ocarray) (_oc_free_array(ocarray, FLOAT_POOL))
145 #define oc_free_double_array(ocarray) (_oc_free_array(ocarray, DOUBLE_POOL))
151 #define oc_new_int_array(ocarray, size) (_oc_new_array(ocarray, size, INT_POOL))
157 #define oc_new_bool_array(ocarray, size) \
158 (_oc_new_array(ocarray, size, BYTE_POOL))
164 #define oc_new_float_array(ocarray, size) \
165 (_oc_new_array(ocarray, size, FLOAT_POOL))
171 #define oc_new_double_array(ocarray, size) \
172 (_oc_new_array(ocarray, size, DOUBLE_POOL))
178 #define oc_new_string_array(ocstringarray, size) \
179 (_oc_alloc_string_array(ocstringarray, size))
185 #define oc_free_string_array(ocstringarray) (_oc_free_string(ocstringarray))
187 #define oc_new_byte_string_array(ocstringarray, size) \
188 (_oc_alloc_string_array(ocstringarray, size))
190 #define oc_free_byte_string_array(ocstringarray) \
191 (_oc_free_string(ocstringarray))
194 #define _MAKE_NULL(...) NULL
196 #define OC_SIZE_ZERO() _MAKE_NULL, 0
197 #define OC_SIZE_MANY(x) _ECHO, x
208 #define oc_mmem_create_const(count, ptr) \
212 #define oc_string_create_const(s) oc_mmem_create_const(sizeof(s), s)
213 #define oc_string_array_create_const(f, n, ...) \
214 oc_mmem_create_const( \
215 (n * STRING_ARRAY_ITEM_MAX_LEN), \
216 f((const char[n][STRING_ARRAY_ITEM_MAX_LEN]){ __VA_ARGS__ }))
217 #define oc_int_array_create_const(f, n, ...) \
218 oc_mmem_create_const(n, f((const int64_t[n]){ __VA_ARGS__ }))
219 #define oc_bool_array_create_const(f, n, ...) \
220 oc_mmem_create_const(n, f((const bool[n]){ __VA_ARGS__ }))
221 #define oc_float_array_create_const(f, n, ...) \
222 oc_mmem_create_const(n, f((const float[n]){ __VA_ARGS__ }))
223 #define oc_double_array_create_const(f, n, ...) \
224 oc_mmem_create_const(n, f((const double[n]){ __VA_ARGS__ }))
226 void oc_concat_strings(oc_string_t *concat,
const char *str1,
const char *str2);
227 #define oc_string_len(ocstring) ((ocstring).size ? (ocstring).size - 1 : 0)
228 #define oc_byte_string_len(ocstring) ((ocstring).size)
230 #define oc_int_array_size(ocintarray) ((ocintarray).size)
231 #define oc_bool_array_size(ocboolarray) ((ocboolarray).size)
232 #define oc_float_array_size(ocfloatarray) ((ocfloatarray).size)
233 #define oc_double_array_size(ocdoublearray) ((ocdoublearray).size)
234 #define oc_string_array_size(ocstringarray) \
235 ((ocstringarray).size / STRING_ARRAY_ITEM_MAX_LEN)
236 #define oc_int_array(ocintarray) (oc_cast(ocintarray, int64_t))
237 #define oc_bool_array(ocboolarray) (oc_cast(ocboolarray, bool))
238 #define oc_float_array(ocfloatarray) (oc_cast(ocfloatarray, float))
239 #define oc_double_array(ocdoublearray) (oc_cast(ocdoublearray, double))
240 #define oc_string_array(ocstringarray) \
241 ((char(*)[STRING_ARRAY_ITEM_MAX_LEN])(OC_MMEM_PTR(&(ocstringarray))))
243 #ifdef OC_DYNAMIC_ALLOCATION
244 #define STRING_ARRAY_ITEM_MAX_LEN 32
246 #define STRING_ARRAY_ITEM_MAX_LEN 32
249 bool _oc_copy_string_to_array(oc_string_array_t *ocstringarray,
250 const char str[],
size_t index);
251 bool _oc_string_array_add_item(oc_string_array_t *ocstringarray,
253 void oc_join_string_array(oc_string_array_t *ocstringarray,
254 oc_string_t *ocstring);
256 bool _oc_copy_byte_string_to_array(oc_string_array_t *ocstringarray,
257 const char str[],
size_t str_len,
259 bool _oc_byte_string_array_add_item(oc_string_array_t *ocstringarray,
260 const char str[],
size_t str_len);
263 #define oc_string_array_add_item(ocstringarray, str) \
264 (_oc_string_array_add_item(&(ocstringarray), str))
265 #define oc_string_array_get_item(ocstringarray, index) \
266 (oc_string(ocstringarray) + (index)*STRING_ARRAY_ITEM_MAX_LEN)
267 #define oc_string_array_set_item(ocstringarray, str, index) \
268 (_oc_copy_string_to_array(&(ocstringarray), str, index))
269 #define oc_string_array_get_item_size(ocstringarray, index) \
270 (strlen((const char *)oc_string_array_get_item(ocstringarray, index)))
271 #define oc_string_array_get_allocated_size(ocstringarray) \
272 ((ocstringarray).size / STRING_ARRAY_ITEM_MAX_LEN)
275 #define oc_byte_string_array_add_item(ocstringarray, str, str_len) \
276 (_oc_byte_string_array_add_item(&(ocstringarray), str, str_len))
277 #define oc_byte_string_array_get_item(ocstringarray, index) \
278 (oc_string(ocstringarray) + (index)*STRING_ARRAY_ITEM_MAX_LEN + 1)
279 #define oc_byte_string_array_set_item(ocstringarray, str, str_len, index) \
280 (_oc_copy_byte_string_to_array(&(ocstringarray), str, str_len, index))
281 #define oc_byte_string_array_get_item_size(ocstringarray, index) \
282 (*(oc_string(ocstringarray) + (index)*STRING_ARRAY_ITEM_MAX_LEN))
283 #define oc_byte_string_array_get_allocated_size(ocstringarray) \
284 ((ocstringarray).size / STRING_ARRAY_ITEM_MAX_LEN)
294 #ifdef OC_MEMORY_TRACE
297 oc_string_t *ocstring,
const char *str,
size_t str_len);
307 #ifdef OC_MEMORY_TRACE
310 oc_string_t *ocstring,
const char *str,
size_t str_len);
319 #ifdef OC_MEMORY_TRACE
322 oc_string_t *ocstring,
size_t size);
330 #ifdef OC_MEMORY_TRACE
333 oc_string_t *ocstring);
342 #ifdef OC_MEMORY_TRACE
345 oc_array_t *ocarray, pool type);
355 #ifdef OC_MEMORY_TRACE
358 oc_array_t *ocarray,
size_t size, pool type);
367 #ifdef OC_MEMORY_TRACE
370 oc_string_array_t *ocstringarray,
size_t size);
386 char *hex_str,
size_t *hex_str_len);
398 uint8_t *array,
size_t *array_len);
488 const char *uri_invoked,
504 const char *uri_invoked,
518 const char *uri_invoked,
539 size_t uri_len,
const char *uri_invoked,
540 size_t invoked_len,
const char **value);
584 int sn_len, uint32_t *ia, uint64_t *iid);
605 const char *string2,
size_t string2_len);
int oc_string_cmp(oc_string_t string1, oc_string_t string2)
oc_string compare
void _oc_free_string(oc_string_t *ocstring)
free oc string
void _oc_new_array(oc_array_t *ocarray, size_t size, pool type)
new array
int oc_conv_hex_string_to_oc_string(const char *hex_str, size_t hex_str_len, oc_string_t *out)
convert hex string to oc_string
bool oc_uri_contains_wildcard_value_underscore(const char *uri_resource, size_t uri_len, const char *uri_invoked, size_t invoked_len)
function to check if in the wild card section is a "_" (underscore) underscores can be used in functi...
int oc_conv_hex_string_to_byte_array(const char *hex_str, size_t hex_str_len, uint8_t *array, size_t *array_len)
convert hex string to byte array
int oc_string_println_hex(oc_string_t hex_string)
prints the input as hex string with newline ( ) at the end.
int oc_print_uint64_t(uint64_t number, enum StringRepresentation rep)
print a uint64_t, in either decimal or hex representation
int oc_char_println_hex(const char *str, int str_len)
prints the input as hex string with newline ( ) at the end.
int oc_string_is_hex_array(oc_string_t hex_string)
checks if the input is an array containing hex values e.g.
void _oc_new_byte_string(oc_string_t *ocstring, const char *str, size_t str_len)
new oc_string byte from string
int oc_byte_string_cmp(oc_string_t string1, oc_string_t string2)
oc_string compare for byte strings (no null terminator)
int oc_byte_string_copy(oc_string_t *string1, oc_string_t string2)
copy oc_string used as a byte string
int oc_conv_uint64_to_dec_string(char *str, uint64_t number)
Converts a uint64_t to a decimal string representation.
int oc_url_cmp(oc_string_t string1, oc_string_t string2)
url compare same as string compare but ignores the leading / of the urls
int oc_uri_get_wildcard_value_as_string(const char *uri_resource, size_t uri_len, const char *uri_invoked, size_t invoked_len, const char **value)
retrieve the wild card value as string The invoked URI is checked against the URI of a resource that ...
int oc_string_copy_from_char(oc_string_t *string1, const char *string2)
copy string from char*
int oc_string_print_hex(oc_string_t hex_string)
prints the input as hex string
int oc_uri_get_wildcard_value_as_int(const char *uri_resource, size_t uri_len, const char *uri_invoked, size_t invoked_len)
retrieve the wild card value as integer The invoked URI is checked against the URI of a resource that...
bool oc_uri_contains_wildcard(const char *uri)
checks if the uri contains a wildcard (e.g.
void _oc_alloc_string_array(oc_string_array_t *ocstringarray, size_t size)
allocate string array
char * oc_strnchr(const char *string, char p, int size)
search a string (non null terminated) for a character
void _oc_alloc_string(oc_string_t *ocstring, size_t size)
allocate oc_string
int oc_char_convert_to_lower(char *string)
converts the input string to lower case
void _oc_free_array(oc_array_t *ocarray, pool type)
free array
int oc_get_sn_ia_iid_from_ep(const char *param, int param_len, char *sn, int sn_len, uint32_t *ia, uint64_t *iid)
retrieves the serial number and individual address from the ep parameter
int oc_get_sn_from_ep(const char *param, int param_len, char *sn, int sn_len, uint32_t *ia)
retrieves the serial number and individual address from the ep parameter
int oc_conv_uint64_to_hex_string(char *str, uint64_t number)
Converts a uint64_t to a hex string representation.
int oc_byte_string_copy_from_char_with_size(oc_string_t *string1, const char *string2, size_t string2_len)
copy byte string from char*
int oc_char_print_hex(const char *str, int str_len)
prints the string as hex
void _oc_new_string(oc_string_t *ocstring, const char *str, size_t str_len)
new oc_string from string
int oc_uri_get_wildcard_value_as_int_after_underscore(const char *uri_resource, size_t uri_len, const char *uri_invoked, size_t invoked_len)
retrieve the integer after the "_" (underscore) e.g.
int oc_string_copy_from_char_with_size(oc_string_t *string1, const char *string2, size_t string2_len)
copy string from char*
int oc_string_copy(oc_string_t *string1, oc_string_t string2)
copy oc_string
int oc_conv_byte_array_to_hex_string(const uint8_t *array, size_t array_len, char *hex_str, size_t *hex_str_len)
Conversions between hex encoded strings and byte arrays.