KNX IoT
KNX IoT Point API stack implementation
oc_helpers.h
Go to the documentation of this file.
1 /*
2 // Copyright (c) 2016 Intel Corporation
3 // Copyright (c) 2021 Cascoda Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 */
21 #ifndef OC_HELPERS_H
22 #define OC_HELPERS_H
23 
24 #include "util/oc_list.h"
25 #include "util/oc_mmem.h"
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <string.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 typedef struct oc_mmem oc_handle_t, oc_string_t, oc_array_t, oc_string_array_t,
36  oc_byte_string_array_t;
37 
38 enum StringRepresentation {
39  DEC_REPRESENTATION = 0,
40  HEX_REPRESENTATION,
41 };
42 
43 #define oc_cast(block, type) ((type *)(OC_MMEM_PTR(&(block))))
44 
49 #define oc_string(ocstring) (oc_cast(ocstring, char))
50 
56 #define oc_string_checked(ocstring) \
57  (oc_cast(ocstring, char) ? oc_cast(ocstring, char) : "NULL")
58 
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)
64 
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))
73 
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))
82 
83 #define oc_new_string_array(ocstringarray, size) \
84  (_oc_alloc_string_array(__func__, ocstringarray, size))
85 
86 #define oc_free_string_array(ocstringarray) \
87  (_oc_free_string(__func__, ocstringarray))
88 
89 #define oc_new_byte_string_array(ocstringarray, size) \
90  (_oc_alloc_string_array(__func__, ocstringarray, size))
91 
92 #define oc_free_byte_string_array(ocstringarray) \
93  (__func__, _oc_free_string(ocstringarray))
94 
95 #else /* OC_MEMORY_TRACE */
96 
101 #define oc_alloc_string(ocstring, size) _oc_alloc_string((ocstring), (size))
102 
107 #define oc_new_string(ocstring, str, str_len) \
108  _oc_new_string(ocstring, str, str_len)
109 
114 #define oc_new_byte_string(ocstring, str, str_len) \
115  _oc_new_byte_string(ocstring, str, str_len)
116 
121 #define oc_free_string(ocstring) _oc_free_string(ocstring)
122 
127 #define oc_free_int_array(ocarray) (_oc_free_array(ocarray, INT_POOL))
128 
133 #define oc_free_bool_array(ocarray) (_oc_free_array(ocarray, BYTE_POOL))
134 
139 #define oc_free_float_array(ocarray) (_oc_free_array(ocarray, FLOAT_POOL))
140 
145 #define oc_free_double_array(ocarray) (_oc_free_array(ocarray, DOUBLE_POOL))
146 
151 #define oc_new_int_array(ocarray, size) (_oc_new_array(ocarray, size, INT_POOL))
152 
157 #define oc_new_bool_array(ocarray, size) \
158  (_oc_new_array(ocarray, size, BYTE_POOL))
159 
164 #define oc_new_float_array(ocarray, size) \
165  (_oc_new_array(ocarray, size, FLOAT_POOL))
166 
171 #define oc_new_double_array(ocarray, size) \
172  (_oc_new_array(ocarray, size, DOUBLE_POOL))
173 
178 #define oc_new_string_array(ocstringarray, size) \
179  (_oc_alloc_string_array(ocstringarray, size))
180 
185 #define oc_free_string_array(ocstringarray) (_oc_free_string(ocstringarray))
186 
187 #define oc_new_byte_string_array(ocstringarray, size) \
188  (_oc_alloc_string_array(ocstringarray, size))
189 
190 #define oc_free_byte_string_array(ocstringarray) \
191  (_oc_free_string(ocstringarray))
192 
193 #endif /* !OC_MEMORY_TRACE */
194 #define _MAKE_NULL(...) NULL
195 #define _ECHO
196 #define OC_SIZE_ZERO() _MAKE_NULL, 0
197 #define OC_SIZE_MANY(x) _ECHO, x
208 #define oc_mmem_create_const(count, ptr) \
209  { \
210  NULL, count, ptr \
211  }
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__ }))
225 
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)
229 
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))))
242 
243 #ifdef OC_DYNAMIC_ALLOCATION
244 #define STRING_ARRAY_ITEM_MAX_LEN 32
245 #else /* OC_DYNAMIC_ALLOCATION */
246 #define STRING_ARRAY_ITEM_MAX_LEN 32
247 #endif /* !OC_DYNAMIC_ALLOCATION */
248 
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,
252  const char str[]);
253 void oc_join_string_array(oc_string_array_t *ocstringarray,
254  oc_string_t *ocstring);
255 
256 bool _oc_copy_byte_string_to_array(oc_string_array_t *ocstringarray,
257  const char str[], size_t str_len,
258  size_t index);
259 bool _oc_byte_string_array_add_item(oc_string_array_t *ocstringarray,
260  const char str[], size_t str_len);
261 
262 /* Arrays of text strings */
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)
273 
274 /* Arrays of byte strings */
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)
285 
294 #ifdef OC_MEMORY_TRACE
295  const char *func,
296 #endif
297  oc_string_t *ocstring, const char *str, size_t str_len);
298 
307 #ifdef OC_MEMORY_TRACE
308  const char *func,
309 #endif
310  oc_string_t *ocstring, const char *str, size_t str_len);
311 
319 #ifdef OC_MEMORY_TRACE
320  const char *func,
321 #endif
322  oc_string_t *ocstring, size_t size);
323 
330 #ifdef OC_MEMORY_TRACE
331  const char *func,
332 #endif
333  oc_string_t *ocstring);
334 
342 #ifdef OC_MEMORY_TRACE
343  const char *func,
344 #endif
345  oc_array_t *ocarray, pool type);
346 
355 #ifdef OC_MEMORY_TRACE
356  const char *func,
357 #endif
358  oc_array_t *ocarray, size_t size, pool type);
359 
367 #ifdef OC_MEMORY_TRACE
368  const char *func,
369 #endif
370  oc_string_array_t *ocstringarray, size_t size);
371 
385 int oc_conv_byte_array_to_hex_string(const uint8_t *array, size_t array_len,
386  char *hex_str, size_t *hex_str_len);
387 
397 int oc_conv_hex_string_to_byte_array(const char *hex_str, size_t hex_str_len,
398  uint8_t *array, size_t *array_len);
407 int oc_conv_hex_string_to_oc_string(const char *hex_str, size_t hex_str_len,
408  oc_string_t *out);
409 
417 int oc_string_is_hex_array(oc_string_t hex_string);
418 
425 int oc_string_print_hex(oc_string_t hex_string);
426 
433 int oc_string_println_hex(oc_string_t hex_string);
434 
441 int oc_char_convert_to_lower(char *string);
442 
450 int oc_char_print_hex(const char *str, int str_len);
458 int oc_char_println_hex(const char *str, int str_len);
459 
467 bool oc_uri_contains_wildcard(const char *uri);
468 
487 int oc_uri_get_wildcard_value_as_int(const char *uri_resource, size_t uri_len,
488  const char *uri_invoked,
489  size_t invoked_len);
490 
502 bool oc_uri_contains_wildcard_value_underscore(const char *uri_resource,
503  size_t uri_len,
504  const char *uri_invoked,
505  size_t invoked_len);
506 
517  size_t uri_len,
518  const char *uri_invoked,
519  size_t invoked_len);
520 
538 int oc_uri_get_wildcard_value_as_string(const char *uri_resource,
539  size_t uri_len, const char *uri_invoked,
540  size_t invoked_len, const char **value);
541 
551 char *oc_strnchr(const char *string, char p, int size);
552 
567 int oc_get_sn_from_ep(const char *param, int param_len, char *sn, int sn_len,
568  uint32_t *ia);
569 
583 int oc_get_sn_ia_iid_from_ep(const char *param, int param_len, char *sn,
584  int sn_len, uint32_t *ia, uint64_t *iid);
585 
593 int oc_string_copy_from_char(oc_string_t *string1, const char *string2);
594 
604 int oc_string_copy_from_char_with_size(oc_string_t *string1,
605  const char *string2, size_t string2_len);
606 
617  const char *string2,
618  size_t string2_len);
619 
627 int oc_string_copy(oc_string_t *string1, oc_string_t string2);
628 
636 int oc_byte_string_copy(oc_string_t *string1, oc_string_t string2);
637 
645 int oc_string_cmp(oc_string_t string1, oc_string_t string2);
646 
654 int oc_byte_string_cmp(oc_string_t string1, oc_string_t string2);
655 
664 int oc_url_cmp(oc_string_t string1, oc_string_t string2);
665 
673 int oc_print_uint64_t(uint64_t number, enum StringRepresentation rep);
674 
683 int oc_conv_uint64_to_dec_string(char *str, uint64_t number);
684 
693 int oc_conv_uint64_to_hex_string(char *str, uint64_t number);
694 
695 #ifdef __cplusplus
696 }
697 #endif
698 
699 #endif /* OC_HELPERS_H */
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.