24 #include "deps/tinycbor/src/cbor.h"
26 #include "util/oc_memb.h"
27 #include <oc_config.h>
35 extern CborEncoder g_encoder, root_map, links_array;
125 #define oc_rep_object(name) &name##_map
132 #define oc_rep_array(name) &name##_array
151 #define oc_rep_set_double(object, key, value) \
153 g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
154 g_err |= cbor_encode_double(&object##_map, value); \
176 #define oc_rep_i_set_double(object, key, value) \
178 g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
179 g_err |= cbor_encode_double(&object##_map, value); \
199 #define oc_rep_set_float(object, key, value) \
201 g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
202 g_err |= cbor_encode_float(&object##_map, value); \
224 #define oc_rep_i_set_float(object, key, value) \
226 g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
227 g_err |= cbor_encode_float(&object##_map, value); \
249 #define oc_rep_set_int(object, key, value) \
251 g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
252 g_err |= cbor_encode_int(&object##_map, value); \
274 #define oc_rep_i_set_int(object, key, value) \
276 g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
277 g_err |= cbor_encode_int(&object##_map, value); \
301 #define oc_rep_set_uint(object, key, value) \
303 g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
304 g_err |= cbor_encode_uint(&object##_map, value); \
328 #define oc_rep_i_set_uint(object, key, value) \
330 g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
331 g_err |= cbor_encode_uint(&object##_map, value); \
353 #define oc_rep_set_boolean(object, key, value) \
355 g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
356 g_err |= cbor_encode_boolean(&object##_map, value); \
378 #define oc_rep_i_set_boolean(object, key, value) \
380 g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
381 g_err |= cbor_encode_boolean(&object##_map, value); \
401 #define oc_rep_set_text_string(object, key, value) \
403 g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
404 if ((const char *)(value) != NULL) { \
405 g_err |= cbor_encode_text_string(&object##_map, value, strlen(value)); \
407 g_err |= cbor_encode_text_string(&object##_map, "", 0); \
428 #define oc_rep_i_set_text_string(object, key, value) \
430 g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
431 if ((const char *)(value) != NULL) { \
432 g_err |= cbor_encode_text_string(&object##_map, value, strlen(value)); \
434 g_err |= cbor_encode_text_string(&object##_map, "", 0); \
455 #define oc_rep_set_text_string_no_tag(object, value) \
457 if ((const char *)(value) != NULL) { \
458 g_err |= cbor_encode_text_string(&object##_map, value, strlen(value)); \
460 g_err |= cbor_encode_text_string(&object##_map, "", 0); \
485 #define oc_rep_set_byte_string(object, key, value, length) \
487 g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
488 g_err |= cbor_encode_byte_string(&object##_map, value, length); \
512 #define oc_rep_i_set_byte_string(object, key, value, length) \
514 g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
515 g_err |= cbor_encode_byte_string(&object##_map, value, length); \
526 #define oc_rep_start_array(parent, name) oc_rep_begin_array(parent, name)
573 #define oc_rep_begin_array(parent, name) \
575 CborEncoder name##_array; \
577 cbor_encoder_create_array(parent, &name##_array, CborIndefiniteLength)
587 #define oc_rep_end_array(parent, name) \
588 g_err |= cbor_encoder_close_container(parent, &name##_array); \
592 #define oc_rep_start_links_array() oc_rep_begin_links_array()
594 #define oc_rep_begin_links_array() \
596 cbor_encoder_create_array(&g_encoder, &links_array, CborIndefiniteLength)
598 #define oc_rep_end_links_array() \
599 g_err |= cbor_encoder_close_container(&g_encoder, &links_array)
607 #define oc_rep_begin_root_object() \
608 g_err |= cbor_encoder_create_map(&g_encoder, &root_map, CborIndefiniteLength)
616 #define oc_rep_begin_root() \
617 g_err |= cbor_encoder_create_map(&g_encoder, &root_map, CborIndefiniteLength)
624 #define oc_rep_end_root_object() \
625 g_err |= cbor_encoder_close_container(&g_encoder, &root_map)
660 #define oc_rep_add_byte_string(parent, value, value_len) \
661 g_err |= cbor_encode_byte_string(&parent##_array, value, value_len)
663 #define oc_rep_set_value_byte_string(parent, value, value_len) \
664 g_err |= cbor_encode_byte_string(&parent##_map, value, value_len)
707 #define oc_rep_add_text_string(parent, value) \
709 if ((const char *)(value) != NULL) { \
710 g_err |= cbor_encode_text_string(&parent##_array, value, strlen(value)); \
712 g_err |= cbor_encode_text_string(&parent##_array, "", 0); \
717 #define oc_rep_set_value_text_string(parent, value) \
719 if ((const char *)(value) != NULL) { \
720 g_err |= cbor_encode_text_string(&parent##_map, value, strlen(value)); \
722 g_err |= cbor_encode_text_string(&parent##_map, "", 0); \
760 #define oc_rep_add_float(parent, value) \
761 g_err |= cbor_encode_float(&parent##_array, value)
763 #define oc_rep_set_value_float(parent, value) \
764 g_err |= cbor_encode_float(&parent##_map, value)
800 #define oc_rep_add_double(parent, value) \
801 g_err |= cbor_encode_double(&parent##_array, value)
803 #define oc_rep_set_value_double(parent, value) \
804 g_err |= cbor_encode_double(&parent##_map, value)
838 #define oc_rep_add_int(parent, value) \
839 g_err |= cbor_encode_int(&parent##_array, value)
841 #define oc_rep_set_value_int(parent, value) \
842 g_err |= cbor_encode_int(&parent##_map, value)
876 #define oc_rep_add_boolean(parent, value) \
877 g_err |= cbor_encode_boolean(&parent##_array, value)
878 #define oc_rep_set_value_boolean(parent, value) \
879 g_err |= cbor_encode_boolean(&parent##_map, value)
894 #define oc_rep_set_key(parent, key) \
895 if ((const char *)(key) != NULL) \
896 g_err |= cbor_encode_text_string(parent, key, strlen(key))
911 #define oc_rep_i_set_key(parent, key) \
912 if ((uint64_t)(key) != 0) \
913 g_err |= cbor_encode_int(parent, (int64_t)(key));
921 #define oc_rep_set_array(object, key) oc_rep_open_array(object, key)
936 #define oc_rep_open_array(parent, key) \
937 g_err |= cbor_encode_text_string(&parent##_map, #key, strlen(#key)); \
938 oc_rep_begin_array(&parent##_map, key)
946 #define oc_rep_close_array(parent, key) oc_rep_end_array(&parent##_map, key)
954 #define oc_rep_start_object(parent, key) oc_rep_begin_object(parent, key)
956 #define oc_rep_begin_object(parent, key) \
958 CborEncoder key##_map; \
959 g_err |= cbor_encoder_create_map(parent, &key##_map, CborIndefiniteLength)
967 #define oc_rep_end_object(parent, key) \
968 g_err |= cbor_encoder_close_container(parent, &key##_map); \
978 #define oc_rep_object_array_start_item(key) oc_rep_object_array_begin_item(key)
1022 #define oc_rep_object_array_begin_item(key) \
1023 oc_rep_begin_object(&key##_array, key)
1028 #define oc_rep_object_array_end_item(key) oc_rep_end_object(&key##_array, key)
1036 #define oc_rep_set_object(object, key) oc_rep_open_object(object, key)
1067 #define oc_rep_open_object(parent, key) \
1068 g_err |= cbor_encode_text_string(&parent##_map, #key, strlen(#key)); \
1069 oc_rep_begin_object(&parent##_map, key)
1077 #define oc_rep_close_object(parent, key) oc_rep_end_object(&parent##_map, key)
1103 #define oc_rep_set_int_array(object, key, values, length) \
1105 g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
1106 CborEncoder key##_value_array; \
1108 cbor_encoder_create_array(&object##_map, &key##_value_array, length); \
1110 for (i = 0; i < (length); i++) { \
1111 g_err |= cbor_encode_int(&key##_value_array, (values)[i]); \
1113 g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array); \
1140 #define oc_rep_i_set_int_array(object, key, values, length) \
1142 g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
1143 CborEncoder x_key##_value_array; \
1145 cbor_encoder_create_array(&object##_map, &x_key##_value_array, length); \
1147 for (i = 0; i < (length); i++) { \
1148 g_err |= cbor_encode_int(&x_key##_value_array, (values)[i]); \
1151 cbor_encoder_close_container(&object##_map, &x_key##_value_array); \
1178 #define oc_rep_set_bool_array(object, key, values, length) \
1180 g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
1181 CborEncoder key##_value_array; \
1183 cbor_encoder_create_array(&object##_map, &key##_value_array, length); \
1185 for (i = 0; i < (length); i++) { \
1186 g_err |= cbor_encode_boolean(&key##_value_array, (values)[i]); \
1188 g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array); \
1215 #define oc_rep_i_set_bool_array(object, key, values, length) \
1217 g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
1218 CborEncoder x_key##_value_array; \
1220 cbor_encoder_create_array(&object##_map, &x_key##_value_array, length); \
1222 for (i = 0; i < (length); i++) { \
1223 g_err |= cbor_encode_boolean(&x_key##_value_array, (values)[i]); \
1226 cbor_encoder_close_container(&object##_map, &x_key##_value_array); \
1254 #define oc_rep_set_float_array(object, key, values, length) \
1256 g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
1257 CborEncoder key##_value_array; \
1259 cbor_encoder_create_array(&object##_map, &key##_value_array, length); \
1261 for (i = 0; i < (length); i++) { \
1262 g_err |= cbor_encode_floating_point(&key##_value_array, CborFloatType, \
1265 g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array); \
1295 #define oc_rep_i_set_float_array(object, key, values, length) \
1297 g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
1298 CborEncoder x_key##_value_array; \
1300 cbor_encoder_create_array(&object##_map, &x_key##_value_array, length); \
1302 for (i = 0; i < (length); i++) { \
1303 g_err |= cbor_encode_floating_point(&x_key##_value_array, CborFloatType, \
1307 cbor_encoder_close_container(&object##_map, &x_key##_value_array); \
1335 #define oc_rep_set_double_array(object, key, values, length) \
1337 g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
1338 CborEncoder key##_value_array; \
1340 cbor_encoder_create_array(&object##_map, &key##_value_array, length); \
1342 for (i = 0; i < (length); i++) { \
1343 g_err |= cbor_encode_floating_point(&key##_value_array, CborDoubleType, \
1346 g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array); \
1376 #define oc_rep_i_set_double_array(object, key, values, length) \
1378 g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
1379 CborEncoder x_key##_value_array; \
1381 cbor_encoder_create_array(&object##_map, &x_key##_value_array, length); \
1383 for (i = 0; i < (length); i++) { \
1384 g_err |= cbor_encode_floating_point(&x_key##_value_array, \
1385 CborDoubleType, &(values)[i]); \
1388 cbor_encoder_close_container(&object##_map, &x_key##_value_array); \
1437 #define oc_rep_set_string_array(object, key, values) \
1439 g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
1440 CborEncoder key##_value_array; \
1441 g_err |= cbor_encoder_create_array(&object##_map, &key##_value_array, \
1442 CborIndefiniteLength); \
1444 for (i = 0; i < (int)oc_string_array_get_allocated_size(values); i++) { \
1445 if (oc_string_array_get_item_size(values, i) > 0) { \
1446 g_err |= cbor_encode_text_string( \
1447 &key##_value_array, oc_string_array_get_item(values, i), \
1448 oc_string_array_get_item_size(values, i)); \
1451 g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array); \
1479 #define oc_rep_set_float_array(object, key, values, length) \
1481 g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
1482 CborEncoder key##_value_array; \
1484 cbor_encoder_create_array(&object##_map, &key##_value_array, length); \
1486 for (i = 0; i < (length); i++) { \
1487 g_err |= cbor_encode_floating_point(&key##_value_array, CborFloatType, \
1490 g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array); \
1520 #define oc_rep_i_set_float_array(object, key, values, length) \
1522 g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
1523 CborEncoder x_key##_value_array; \
1525 cbor_encoder_create_array(&object##_map, &x_key##_value_array, length); \
1527 for (i = 0; i < (length); i++) { \
1528 g_err |= cbor_encode_floating_point(&x_key##_value_array, CborFloatType, \
1532 cbor_encoder_close_container(&object##_map, &x_key##_value_array); \
1581 #define oc_rep_i_set_string_array(object, key, values) \
1583 g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
1584 CborEncoder x_key##_value_array; \
1585 g_err |= cbor_encoder_create_array(&object##_map, &x_key##_value_array, \
1586 CborIndefiniteLength); \
1588 for (i = 0; i < (int)oc_string_array_get_allocated_size(values); i++) { \
1589 if (oc_string_array_get_item_size(values, i) > 0) { \
1590 g_err |= cbor_encode_text_string( \
1591 &x_key##_value_array, oc_string_array_get_item(values, i), \
1592 oc_string_array_get_item_size(values, i)); \
1596 cbor_encoder_close_container(&object##_map, &x_key##_value_array); \
1615 OC_REP_DOUBLE = 0x02,
1617 OC_REP_BYTE_STRING = 0x04,
1618 OC_REP_STRING = 0x05,
1619 OC_REP_OBJECT = 0x06,
1620 OC_REP_FLOAT = 0x07,
1622 OC_REP_ARRAY = 0x10,
1623 OC_REP_INT_ARRAY = 0x11,
1624 OC_REP_DOUBLE_ARRAY = 0x12,
1625 OC_REP_BOOL_ARRAY = 0x13,
1626 OC_REP_BYTE_STRING_ARRAY = 0x14,
1627 OC_REP_STRING_ARRAY = 0x15,
1628 OC_REP_OBJECT_ARRAY = 0x16,
1629 OC_REP_FLOAT_ARRAY = 0x17,
1630 OC_REP_MIXED_ARRAY = 0x1f,
1631 } oc_rep_value_type_t;
1647 union oc_rep_value {
1661 void oc_rep_set_pool(
struct oc_memb *rep_objects_pool);
1664 int oc_parse_rep(
const uint8_t *payload,
int payload_size,
2178 oc_string_array_t *
value,
size_t *size);
2212 oc_string_array_t *
value,
size_t *size);
2247 oc_string_array_t *
value,
size_t *size);
2490 #define OC_PRETTY_PRINT_TAB_CHARACTER " "
2548 size_t py_oc_rep_to_json(
oc_rep_t *rep,
char *buf,
size_t buf_size,
bool oc_rep_get_string(oc_rep_t *rep, const char *key, char **value, size_t *size)
Read a text string value from an oc_rep_t
bool oc_rep_i_get_float_array(oc_rep_t *rep, int key, float **value, size_t *size)
Read an float array value from an oc_rep_t with an integer key.
bool oc_rep_i_get_byte_string_array(oc_rep_t *rep, int key, oc_string_array_t *value, size_t *size)
Read an byte string array value from an oc_rep_t with integer key.
bool oc_rep_i_get_float(oc_rep_t *rep, int key, float *value)
Read a float value from an oc_rep_t
bool oc_rep_i_get_bool(oc_rep_t *rep, int key, bool *value)
Read a boolean value from an oc_rep_t with key as integer.
bool oc_rep_i_get_string(oc_rep_t *rep, int key, char **value, size_t *size)
Read a text string value from an oc_rep_t with integer key.
bool oc_rep_get_double_array(oc_rep_t *rep, const char *key, double **value, size_t *size)
Read an double array value from an oc_rep_t
bool oc_rep_get_object(oc_rep_t *rep, const char *key, oc_rep_t **value)
Read a object value from an oc_rep_t
int oc_rep_add_line_to_buffer(const char *line)
add a line to the response buffer note updates the occupied size in the response buffer
bool oc_rep_get_float_array(oc_rep_t *rep, const char *key, float **value, size_t *size)
Read an float array value from an oc_rep_t
bool oc_rep_i_get_double_array(oc_rep_t *rep, int key, double **value, size_t *size)
Read an double array value from an oc_rep_t with an integer key.
int oc_rep_add_line_size_to_buffer(const char *line, int len)
add a line to the response buffer note updates the occupied size in the response buffer
void oc_print_rep_as_json(oc_rep_t *rep, bool pretty_print)
prints the response tree as JSON to the output pretty_print: true: nicely formatted JSON on multiple ...
bool oc_rep_i_get_byte_string(oc_rep_t *rep, int key, char **value, size_t *size)
Read a byte string value from an oc_rep_t with an integer key.
const uint8_t * oc_rep_get_encoder_buf(void)
Get the buffer pointer at the start of the encoded cbor data.
bool oc_rep_i_get_mixed_array(oc_rep_t *rep, int key, oc_rep_t **value)
Read a mixed array value from an oc_rep_t with integer key.
bool oc_rep_i_get_object_array(oc_rep_t *rep, int key, oc_rep_t **value)
Read a object array value from an oc_rep_t with integer key.
void oc_rep_new(uint8_t *payload, int size)
Initialize the buffer used to hold the cbor encoded data.
bool oc_rep_get_string_array(oc_rep_t *rep, const char *key, oc_string_array_t *value, size_t *size)
Read a string array value from an oc_rep_t
bool oc_rep_get_int_array(oc_rep_t *rep, const char *key, int64_t **value, size_t *size)
Read an integer array value from an oc_rep_t
bool oc_rep_get_byte_string(oc_rep_t *rep, const char *key, char **value, size_t *size)
Read a byte string value from an oc_rep_t
bool oc_rep_get_mixed_array(oc_rep_t *rep, const char *key, oc_rep_t **value)
Read a mixed array value from an oc_rep_t
bool oc_rep_i_get_bool_array(oc_rep_t *rep, int key, bool **value, size_t *size)
Read an boolean array value from an oc_rep_t with integer key.
void oc_rep_encode_raw(const uint8_t *data, size_t len)
Encode raw data, as if it was already encoded.
bool oc_rep_i_get_int_array(oc_rep_t *rep, int key, int64_t **value, size_t *size)
Read an integer array value from an oc_rep_t with an integer key.
bool oc_rep_get_bool_array(oc_rep_t *rep, const char *key, bool **value, size_t *size)
Read an boolean array value from an oc_rep_t
bool oc_rep_get_object_array(oc_rep_t *rep, const char *key, oc_rep_t **value)
Read a object array value from an oc_rep_t
bool oc_rep_i_get_int(oc_rep_t *rep, int key, int64_t *value)
Read an integer from an oc_rep_t with an integer as key.
bool oc_rep_i_get_string_array(oc_rep_t *rep, int key, oc_string_array_t *value, size_t *size)
Read a string array value from an oc_rep_t with integer key.
bool oc_rep_get_byte_string_array(oc_rep_t *rep, const char *key, oc_string_array_t *value, size_t *size)
Read an byte string array value from an oc_rep_t
bool oc_rep_get_int(oc_rep_t *rep, const char *key, int64_t *value)
Read an integer from an oc_rep_t
void oc_rep_encode_raw_encoder(CborEncoder *encoder, const uint8_t *data, size_t len)
Encode raw data, as if it was already encoded.
size_t oc_rep_to_json(oc_rep_t *rep, char *buf, size_t buf_size, bool pretty_print)
Convert an oc_rep_t to JSON encoded string.
int oc_rep_get_encoded_payload_size(void)
Get the size of the cbor encoded data.
struct oc_rep_s oc_rep_t
parsed entry of a cbor object This represents a link list of response values one can iterate over the...
CborError oc_rep_get_cbor_errno(void)
Called after any oc_rep_set_*, oc_rep_start_*, oc_rep_begin_*, oc_rep_end_*, oc_rep_add_*,...
bool oc_rep_i_get_double(oc_rep_t *rep, int key, double *value)
Read a double value from an oc_rep_t
bool oc_rep_get_double(oc_rep_t *rep, const char *key, double *value)
Read a double value from an oc_rep_t
bool oc_rep_get_bool(oc_rep_t *rep, const char *key, bool *value)
Read a boolean value from an oc_rep_t
bool oc_rep_i_get_object(oc_rep_t *rep, int key, oc_rep_t **value)
Read a object value from an oc_rep_t with integer key.
bool oc_rep_get_float(oc_rep_t *rep, const char *key, float *value)
Read a float value from an oc_rep_t
parsed entry of a cbor object This represents a link list of response values one can iterate over the...
oc_rep_value_type_t type
type of the data
oc_string_t name
name of the tag
union oc_rep_s::oc_rep_value value
the value as union
struct oc_rep_s * next
next in list
int iname
integer (identifier) as tag name