KNX IoT
KNX IoT Point API stack implementation
oc_rep.h File Reference

request and response payload handling in cbor More...

#include "deps/tinycbor/src/cbor.h"
#include "oc_helpers.h"
#include "util/oc_memb.h"
#include <oc_config.h>
#include <stdbool.h>
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  oc_rep_s
 parsed entry of a cbor object This represents a link list of response values one can iterate over the responses to find values by More...
 

Macros

#define OC_PRETTY_PRINT_TAB_CHARACTER   " "
 Tab character(s) used for oc_rep_to_json function when doing pretty_print. More...
 
#define oc_rep_add_boolean(parent, value)    g_err |= cbor_encode_boolean(&parent##_array, value)
 Add an bool value to a parent array. More...
 
#define oc_rep_add_byte_string(parent, value, value_len)    g_err |= cbor_encode_byte_string(&parent##_array, value, value_len)
 Add a byte string value to a parent array. More...
 
#define oc_rep_add_double(parent, value)    g_err |= cbor_encode_double(&parent##_array, value)
 Add an double value to a parent array. More...
 
#define oc_rep_add_float(parent, value)    g_err |= cbor_encode_float(&parent##_array, value)
 Add an float value to a parent array. More...
 
#define oc_rep_add_int(parent, value)    g_err |= cbor_encode_int(&parent##_array, value)
 Add an int value to a parent array. More...
 
#define oc_rep_add_text_string(parent, value)
 Add a text string value to a parent array. More...
 
#define oc_rep_array(name)   &name##_array
 Get a pointer to the cbor array object with the given name More...
 
#define oc_rep_begin_array(parent, name)
 This macro is unlikely to be used by outside the library. More...
 
#define oc_rep_begin_links_array()
 
#define oc_rep_begin_object(parent, key)
 
#define oc_rep_begin_root()    g_err |= cbor_encoder_create_map(&g_encoder, &root_map, CborIndefiniteLength)
 Begin the root (without map). More...
 
#define oc_rep_begin_root_object()    g_err |= cbor_encoder_create_map(&g_encoder, &root_map, CborIndefiniteLength)
 Begin the root object. More...
 
#define oc_rep_close_array(parent, key)   oc_rep_end_array(&parent##_map, key)
 Close the array object. More...
 
#define oc_rep_close_object(parent, key)   oc_rep_end_object(&parent##_map, key)
 Close the object. More...
 
#define oc_rep_end_array(parent, name)
 End the array object. More...
 
#define oc_rep_end_links_array()    g_err |= cbor_encoder_close_container(&g_encoder, &links_array)
 
#define oc_rep_end_object(parent, key)
 This macro has been replaced with oc_rep_begin_object. More...
 
#define oc_rep_end_root_object()    g_err |= cbor_encoder_close_container(&g_encoder, &root_map)
 End the root object. More...
 
#define oc_rep_i_set_bool_array(object, key, values, length)
 Add a boolean array with values of length to the cbor object under the integer key name. More...
 
#define oc_rep_i_set_boolean(object, key, value)
 Add an boolean value to the cbor object under the integer key name Example: More...
 
#define oc_rep_i_set_byte_string(object, key, value, length)
 Add an byte array value to the cbor object under the integer key name Example: More...
 
#define oc_rep_i_set_double(object, key, value)
 Add a double value to the cbor object under the integer key name Example: More...
 
#define oc_rep_i_set_double_array(object, key, values, length)
 Add a double array with values of length to the cbor object under the integer key name. More...
 
#define oc_rep_i_set_float(object, key, value)
 Add a float value to the cbor object under the integer key name Example: More...
 
#define oc_rep_i_set_float_array(object, key, values, length)
 Add a float array with values of length to the cbor object under the integer key name. More...
 
#define oc_rep_i_set_float_array(object, key, values, length)
 Add a float array with values of length to the cbor object under the integer key name. More...
 
#define oc_rep_i_set_int(object, key, value)
 Add an integer value to the cbor object under the integer key name Example: More...
 
#define oc_rep_i_set_int_array(object, key, values, length)
 Add an integer array with values of length to the cbor object under the integer key name. More...
 
#define oc_rep_i_set_key(parent, key)
 End users are very unlikely to use this macro. More...
 
#define oc_rep_i_set_string_array(object, key, values)
 Add a string array using an oc_string_array_t as values to the cbor object under the integer key name. More...
 
#define oc_rep_i_set_text_string(object, key, value)
 Add an string value to the cbor object under the integer key name Example: More...
 
#define oc_rep_i_set_uint(object, key, value)
 Add an unsigned integer value to the cbor object under the integer key name Example: More...
 
#define oc_rep_object(name)   &name##_map
 Get a pointer to the cbor object with the given name More...
 
#define oc_rep_object_array_begin_item(key)    oc_rep_begin_object(&key##_array, key)
 Begin a cbor object for an array of cbor objects. More...
 
#define oc_rep_object_array_end_item(key)   oc_rep_end_object(&key##_array, key)
 End the cbor object for the key array of cbor objects. More...
 
#define oc_rep_object_array_start_item(key)   oc_rep_object_array_begin_item(key)
 This macro has been replaced with oc_rep_object_array_begin_item. More...
 
#define oc_rep_open_array(parent, key)
 Open a cbor array object belonging to parent object under the key name. More...
 
#define oc_rep_open_object(parent, key)
 Open a cbor object belonging to parent object under the key name. More...
 
#define oc_rep_set_array(object, key)   oc_rep_open_array(object, key)
 This macro has been replaced with oc_rep_open_array. More...
 
#define oc_rep_set_bool_array(object, key, values, length)
 Add a boolean array with values of length to the cbor object under the key name. More...
 
#define oc_rep_set_boolean(object, key, value)
 Add an boolean value to the cbor object under the key name Example: More...
 
#define oc_rep_set_byte_string(object, key, value, length)
 Add an byte array value to the cbor object under the key name Example: More...
 
#define oc_rep_set_double(object, key, value)
 Add a double value to the cbor object under the key name Example: More...
 
#define oc_rep_set_double_array(object, key, values, length)
 Add a double array with values of length to the cbor object under the key name. More...
 
#define oc_rep_set_float(object, key, value)
 Add a float value to the cbor object under the key name Example: More...
 
#define oc_rep_set_float_array(object, key, values, length)
 Add a float array with values of length to the cbor object under the key name. More...
 
#define oc_rep_set_float_array(object, key, values, length)
 Add a float array with values of length to the cbor object under the key name. More...
 
#define oc_rep_set_int(object, key, value)
 Add an integer value to the cbor object under the key name Example: More...
 
#define oc_rep_set_int_array(object, key, values, length)
 Add an integer array with values of length to the cbor object under the key name. More...
 
#define oc_rep_set_key(parent, key)
 End users are very unlikely to use this macro. More...
 
#define oc_rep_set_object(object, key)   oc_rep_open_object(object, key)
 This macro has been replaced with oc_rep_open_object. More...
 
#define oc_rep_set_string_array(object, key, values)
 Add a string array using an oc_string_array_t as values to the cbor object under the key name. More...
 
#define oc_rep_set_text_string(object, key, value)
 Add an string value to the cbor object under the key name Example: More...
 
#define oc_rep_set_text_string_no_tag(object, value)
 Add an string value to the cbor object under the key name Example: More...
 
#define oc_rep_set_uint(object, key, value)
 Add an unsigned integer value to the cbor object under the key name Example: More...
 
#define oc_rep_set_value_boolean(parent, value)    g_err |= cbor_encode_boolean(&parent##_map, value)
 
#define oc_rep_set_value_byte_string(parent, value, value_len)    g_err |= cbor_encode_byte_string(&parent##_map, value, value_len)
 
#define oc_rep_set_value_double(parent, value)    g_err |= cbor_encode_double(&parent##_map, value)
 
#define oc_rep_set_value_float(parent, value)    g_err |= cbor_encode_float(&parent##_map, value)
 
#define oc_rep_set_value_int(parent, value)    g_err |= cbor_encode_int(&parent##_map, value)
 
#define oc_rep_set_value_text_string(parent, value)
 
#define oc_rep_start_array(parent, name)   oc_rep_begin_array(parent, name)
 This macro has been replaced with oc_rep_begin_array. More...
 
#define oc_rep_start_links_array()   oc_rep_begin_links_array()
 
#define oc_rep_start_object(parent, key)   oc_rep_begin_object(parent, key)
 This macro has been replaced with oc_rep_begin_object. More...
 

Typedefs

typedef 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 responses to find values by More...
 

Enumerations

enum  oc_rep_value_type_t {
  OC_REP_NIL = 0 , OC_REP_INT = 0x01 , OC_REP_DOUBLE = 0x02 , OC_REP_BOOL = 0x03 ,
  OC_REP_BYTE_STRING = 0x04 , OC_REP_STRING = 0x05 , OC_REP_OBJECT = 0x06 , OC_REP_FLOAT = 0x07 ,
  OC_REP_ARRAY = 0x10 , OC_REP_INT_ARRAY = 0x11 , OC_REP_DOUBLE_ARRAY = 0x12 , OC_REP_BOOL_ARRAY = 0x13 ,
  OC_REP_BYTE_STRING_ARRAY = 0x14 , OC_REP_STRING_ARRAY = 0x15 , OC_REP_OBJECT_ARRAY = 0x16 , OC_REP_FLOAT_ARRAY = 0x17 ,
  OC_REP_MIXED_ARRAY = 0x1f
}
 

Functions

void oc_free_rep (oc_rep_t *rep)
 
int oc_parse_rep (const uint8_t *payload, int payload_size, oc_rep_t **value_list)
 
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 lines false: JSON formatted on a single line More...
 
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 More...
 
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 More...
 
void oc_rep_encode_raw (const uint8_t *data, size_t len)
 Encode raw data, as if it was already encoded. More...
 
void oc_rep_encode_raw_encoder (CborEncoder *encoder, const uint8_t *data, size_t len)
 Encode raw data, as if it was already encoded. More...
 
bool oc_rep_get_bool (oc_rep_t *rep, const char *key, bool *value)
 Read a boolean value from an oc_rep_t More...
 
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 More...
 
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 More...
 
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 More...
 
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_*, oc_rep_open_*, and oc_rep_close_* macros to check if an error occurred while executing the commands. More...
 
bool oc_rep_get_double (oc_rep_t *rep, const char *key, double *value)
 Read a double value from an oc_rep_t More...
 
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 More...
 
int oc_rep_get_encoded_payload_size (void)
 Get the size of the cbor encoded data. More...
 
const uint8_t * oc_rep_get_encoder_buf (void)
 Get the buffer pointer at the start of the encoded cbor data. More...
 
bool oc_rep_get_float (oc_rep_t *rep, const char *key, float *value)
 Read a float value from an oc_rep_t More...
 
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 More...
 
bool oc_rep_get_int (oc_rep_t *rep, const char *key, int64_t *value)
 Read an integer from an oc_rep_t More...
 
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 More...
 
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 More...
 
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 More...
 
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 More...
 
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 More...
 
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 More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
bool oc_rep_i_get_double (oc_rep_t *rep, int key, double *value)
 Read a double value from an oc_rep_t More...
 
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. More...
 
bool oc_rep_i_get_float (oc_rep_t *rep, int key, float *value)
 Read a float value from an oc_rep_t More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
void oc_rep_new (uint8_t *payload, int size)
 Initialize the buffer used to hold the cbor encoded data. More...
 
void oc_rep_set_pool (struct oc_memb *rep_objects_pool)
 
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. More...
 
size_t py_oc_rep_to_json (oc_rep_t *rep, char *buf, size_t buf_size, bool pretty_print)
 

Variables

CborEncoder g_encoder
 
int g_err
 
CborEncoder links_array
 
CborEncoder root_map
 

Detailed Description

request and response payload handling in cbor

Definition in file oc_rep.h.

Macro Definition Documentation

◆ OC_PRETTY_PRINT_TAB_CHARACTER

#define OC_PRETTY_PRINT_TAB_CHARACTER   " "

Tab character(s) used for oc_rep_to_json function when doing pretty_print.

Definition at line 2490 of file oc_rep.h.

◆ oc_rep_add_boolean

#define oc_rep_add_boolean (   parent,
  value 
)     g_err |= cbor_encode_boolean(&parent##_array, value)

Add an bool value to a parent array.

Using oc_rep_add_boolean can be used when the number of items is calculated at run time or for some reason it is not know till after calling oc_rep_open_array.

If the size of the bool array is already known oc_rep_set_bool_array should be used.

Example: To build the an object with the following cbor value

{
  "flip": [ false, false, true, false, false ]
}

The following code could be used:

bool flip[] = {false, false, true, false, false };
oc_rep_open_array(root, flip);
for(size_t i = 0; i < (sizeof(flip)/ sizeof(flip[0])); i++) {
oc_rep_add_boolean(flip, flip[i]);
}
oc_rep_close_array(root, flip);
#define oc_rep_add_boolean(parent, value)
Add an bool value to a parent array.
Definition: oc_rep.h:876
#define oc_rep_close_array(parent, key)
Close the array object.
Definition: oc_rep.h:946
#define oc_rep_end_root_object()
End the root object.
Definition: oc_rep.h:624
#define oc_rep_begin_root_object()
Begin the root object.
Definition: oc_rep.h:607
#define oc_rep_open_array(parent, key)
Open a cbor array object belonging to parent object under the key name.
Definition: oc_rep.h:936
See also
oc_rep_open_array
oc_rep_close_array
oc_rep_set_bool_array

Definition at line 876 of file oc_rep.h.

◆ oc_rep_add_byte_string

#define oc_rep_add_byte_string (   parent,
  value,
  value_len 
)     g_err |= cbor_encode_byte_string(&parent##_array, value, value_len)

Add a byte string value to a parent array.

Currently the only way to make an array of byte strings is using this macro

Example:

To build the an object with the following cbor value note, base64 encoding used to represent binary array data

{
  "barray": [ "AAECAwQFBg==", "AQECAwUIEyE0VYk=", "AAD/AAA=" ]
}

The following code could be used:

uint8_t ba1[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
uint8_t ba2[] = {0x01, 0x01, 0x02, 0x03, 0x05, 0x08,
0x13, 0x21, 0x34, 0x55, 0x89};
uint8_t ba3[] = {0x00, 0x00, 0xff, 0x00, 0x00};
// add values to root object
oc_rep_open_array(root, barray);
oc_rep_add_byte_string(barray, ba1, sizeof(ba1));
oc_rep_add_byte_string(barray, ba2, sizeof(ba2));
oc_rep_add_byte_string(barray, ba3, sizeof(ba3));
oc_rep_close_array(root, barray);
#define oc_rep_add_byte_string(parent, value, value_len)
Add a byte string value to a parent array.
Definition: oc_rep.h:660
See also
oc_rep_open_array
oc_rep_close_array

Definition at line 660 of file oc_rep.h.

◆ oc_rep_add_double

#define oc_rep_add_double (   parent,
  value 
)     g_err |= cbor_encode_double(&parent##_array, value)

Add an double value to a parent array.

Using oc_rep_add_double can be useful when the number of items is calculated at run time or for some reason is not know till after calling oc_rep_open_array.

If the size of the double array is already known oc_rep_set_double_array should be used.

Example:

To build the an object with the following cbor value

{
  "math_constants": [ 3.14159, 2.71828, 1.414121, 1.61803 ]
}

The following code could be used:

double math_constants[] = { 3.14159, 2.71828, 1.414121, 1.61803 };
oc_rep_open_array(root, math_constants);
for(size_t i = 0; i < (sizeof(math_constants)/
sizeof(math_constants[0])); i++) {
oc_rep_add_double(math_constants, math_constants[i]);
}
oc_rep_close_array(root, math_constants);
#define oc_rep_add_double(parent, value)
Add an double value to a parent array.
Definition: oc_rep.h:800
See also
oc_rep_open_array
oc_rep_close_array
oc_rep_set_double_array

Definition at line 800 of file oc_rep.h.

◆ oc_rep_add_float

#define oc_rep_add_float (   parent,
  value 
)     g_err |= cbor_encode_float(&parent##_array, value)

Add an float value to a parent array.

Using oc_rep_add_float can be useful when the number of items is calculated at run time or for some reason is not know till after calling oc_rep_open_array.

If the size of the float array is already known oc_rep_set_float_array should be used.

Example:

To build the an object with the following cbor value

{
  "math_constants": [ 3.14159, 2.71828, 1.414121, 1.61803 ]
}

The following code could be used:

float math_constants[] = { 3.14159, 2.71828, 1.414121, 1.61803 };
oc_rep_open_array(root, math_constants);
for(size_t i = 0; i < (sizeof(math_constants)/
sizeof(math_constants[0])); i++) {
oc_rep_add_float(math_constants, math_constants[i]);
}
oc_rep_close_array(root, math_constants);
#define oc_rep_add_float(parent, value)
Add an float value to a parent array.
Definition: oc_rep.h:760
See also
oc_rep_open_array
oc_rep_close_array
oc_rep_set_float_array

Definition at line 760 of file oc_rep.h.

◆ oc_rep_add_int

#define oc_rep_add_int (   parent,
  value 
)     g_err |= cbor_encode_int(&parent##_array, value)

Add an int value to a parent array.

Using oc_rep_add_int can be useful when the number of items is calculated at run time or for some reason it not know till after calling oc_rep_open_array.

If the size of the int array is already known oc_rep_set_int_array should be used.

Example: To build the an object with the following cbor value

{
  "fibonacci": [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ]
}

The following code could be used:

int fib[] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
oc_rep_open_array(root, fibonacci);
for(size_t i = 0; i < (sizeof(fib)/ sizeof(fib[0])); i++) {
oc_rep_add_int(fibonacci, fib[i]);
}
oc_rep_close_array(root, fibonacci);
#define oc_rep_add_int(parent, value)
Add an int value to a parent array.
Definition: oc_rep.h:838
See also
oc_rep_open_array
oc_rep_close_array
oc_rep_set_int_array

Definition at line 838 of file oc_rep.h.

◆ oc_rep_add_text_string

#define oc_rep_add_text_string (   parent,
  value 
)
Value:
do { \
if ((const char *)(value) != NULL) { \
g_err |= cbor_encode_text_string(&parent##_array, value, strlen(value)); \
} else { \
g_err |= cbor_encode_text_string(&parent##_array, "", 0); \
} \
} while (0)

Add a text string value to a parent array.

Currently the only way to make an array of text strings is using this macro

Example:

To build the an object with the following cbor value

{
  "quotes": [
  "Do not take life too seriously. You will never get out of it alive.",
  "All generalizations are false, including this one.",
  "Those who believe in telekinetics, raise my hand.",
  "I refuse to join any club that would have me as a member."
  ]
}

The following code could be used:

const char* str0 = "Do not take life too seriously. You will never get
out of it alive.";
const char* str1 = "All generalizations are false, including this one.";
const char* str2 = "Those who believe in telekinetics, raise my hand.";
const char* str3 = "I refuse to join any club that would have me as a
member.";
// add values to root object
oc_rep_open_array(root, quotes);
oc_rep_add_text_string(quotes, str0);
oc_rep_add_text_string(quotes, str1);
oc_rep_add_text_string(quotes, str2);
oc_rep_add_text_string(quotes, str3);
oc_rep_close_array(root, quotes);
#define oc_rep_add_text_string(parent, value)
Add a text string value to a parent array.
Definition: oc_rep.h:707
See also
oc_rep_open_array
oc_rep_close_array

Definition at line 707 of file oc_rep.h.

◆ oc_rep_array

#define oc_rep_array (   name)    &name##_array

Get a pointer to the cbor array object with the given name

Returns
cbor array object pointer

Definition at line 132 of file oc_rep.h.

◆ oc_rep_begin_array

#define oc_rep_begin_array (   parent,
  name 
)
Value:
do { \
CborEncoder name##_array; \
g_err |= \
cbor_encoder_create_array(parent, &name##_array, CborIndefiniteLength)

This macro is unlikely to be used by outside the library.

Begin a cbor array object with name belonging to parent object. Items can then be added to the array till oc_rep_end_array is called.

Since no functions exist to retrieve an array object without a key it is unlikely this macro will be used without using oc_rep_set_key first. Most likely oc_rep_open_array will be used to create an array object with a key.

Example: To build the an object with the following cbor value

{
  "fibonacci": [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ]
}

The following code could be used:

int fib[] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
oc_rep_set_key(oc_rep_object(root), "fibonacci");
for(size_t i = 0; i < (sizeof(fib)/ sizeof(fib[0])); i++) {
oc_rep_add_int(fibonacci, fib[i]);
}
oc_rep_end_array(oc_rep_object(root), fibonacci);
#define oc_rep_object(name)
Get a pointer to the cbor object with the given name
Definition: oc_rep.h:125
#define oc_rep_end_array(parent, name)
End the array object.
Definition: oc_rep.h:587
#define oc_rep_set_key(parent, key)
End users are very unlikely to use this macro.
Definition: oc_rep.h:894
#define oc_rep_begin_array(parent, name)
This macro is unlikely to be used by outside the library.
Definition: oc_rep.h:573

See oc_rep_add_int to see an example using the recommended way to do the same thing using oc_rep_open_array and oc_rep_close_array instead.

See also
oc_rep_set_key
oc_rep_end_array
oc_rep_add_byte_string
oc_rep_add_text_string
oc_rep_add_double
oc_rep_add_float
oc_rep_add_int
oc_rep_add_boolean
oc_rep_open_array
oc_rep_close_array

Definition at line 573 of file oc_rep.h.

◆ oc_rep_begin_links_array

#define oc_rep_begin_links_array ( )
Value:
g_err |= \
cbor_encoder_create_array(&g_encoder, &links_array, CborIndefiniteLength)

Definition at line 594 of file oc_rep.h.

◆ oc_rep_begin_object

#define oc_rep_begin_object (   parent,
  key 
)
Value:
do { \
CborEncoder key##_map; \
g_err |= cbor_encoder_create_map(parent, &key##_map, CborIndefiniteLength)

Definition at line 956 of file oc_rep.h.

◆ oc_rep_begin_root

#define oc_rep_begin_root ( )     g_err |= cbor_encoder_create_map(&g_encoder, &root_map, CborIndefiniteLength)

Begin the root (without map).

Items can be added to the root object till oc_rep_end_root_object is called

See also
oc_rep_end_root_object

Definition at line 616 of file oc_rep.h.

◆ oc_rep_begin_root_object

#define oc_rep_begin_root_object ( )     g_err |= cbor_encoder_create_map(&g_encoder, &root_map, CborIndefiniteLength)

Begin the root object.

Items can be added to the root object till oc_rep_end_root_object is called

See also
oc_rep_end_root_object

Definition at line 607 of file oc_rep.h.

◆ oc_rep_close_array

#define oc_rep_close_array (   parent,
  key 
)    oc_rep_end_array(&parent##_map, key)

Close the array object.

No additional items can be added to the array after this is called.

See also
oc_rep_open_array

Definition at line 946 of file oc_rep.h.

◆ oc_rep_close_object

#define oc_rep_close_object (   parent,
  key 
)    oc_rep_end_object(&parent##_map, key)

Close the object.

No additional items can be added to the object after this is called.

See also
oc_rep_open_object

Definition at line 1077 of file oc_rep.h.

◆ oc_rep_end_array

#define oc_rep_end_array (   parent,
  name 
)
Value:
g_err |= cbor_encoder_close_container(parent, &name##_array); \
} \
while (0)

End the array object.

No additional items can be added to the array after this is called.

See also
oc_rep_begin_array
oc_rep_open_array
oc_rep_close_array

Definition at line 587 of file oc_rep.h.

◆ oc_rep_end_object

#define oc_rep_end_object (   parent,
  key 
)
Value:
g_err |= cbor_encoder_close_container(parent, &key##_map); \
} \
while (0)

This macro has been replaced with oc_rep_begin_object.

See also
oc_rep_begin_object
oc_rep_end_object

Definition at line 967 of file oc_rep.h.

◆ oc_rep_end_root_object

#define oc_rep_end_root_object ( )     g_err |= cbor_encoder_close_container(&g_encoder, &root_map)

End the root object.

Items can no longer be added to the root object.

See also
oc_rep_begin_root_object

Definition at line 624 of file oc_rep.h.

◆ oc_rep_i_set_bool_array

#define oc_rep_i_set_bool_array (   object,
  key,
  values,
  length 
)
Value:
do { \
g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
CborEncoder x_key##_value_array; \
g_err |= \
cbor_encoder_create_array(&object##_map, &x_key##_value_array, length); \
int i; \
for (i = 0; i < (length); i++) { \
g_err |= cbor_encode_boolean(&x_key##_value_array, (values)[i]); \
} \
g_err |= \
cbor_encoder_close_container(&object##_map, &x_key##_value_array); \
} while (0)

Add a boolean array with values of length to the cbor object under the integer key name.

Example:

To build the an object with the following cbor value

{
  4 : [ false, false, true, false, false ]
}

The following code could be used:

bool flip[] = {false, false, true, false, false };
4,
flip,
(int)(sizeof(flip)/ sizeof(flip[0]) ) );
#define oc_rep_i_set_bool_array(object, key, values, length)
Add a boolean array with values of length to the cbor object under the integer key name.
Definition: oc_rep.h:1215

Definition at line 1215 of file oc_rep.h.

◆ oc_rep_i_set_boolean

#define oc_rep_i_set_boolean (   object,
  key,
  value 
)
Value:
do { \
g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
g_err |= cbor_encode_boolean(&object##_map, value); \
} while (0)

Add an boolean value to the cbor object under the integer key name Example:

To build the an object with the following cbor value

{
  3: false
}

The following code could be used:

oc_rep_set_boolean(root, 3, false);
#define oc_rep_set_boolean(object, key, value)
Add an boolean value to the cbor object under the key name Example:
Definition: oc_rep.h:353
See also
oc_rep_i_get_bool

Definition at line 378 of file oc_rep.h.

◆ oc_rep_i_set_byte_string

#define oc_rep_i_set_byte_string (   object,
  key,
  value,
  length 
)
Value:
do { \
g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
g_err |= cbor_encode_byte_string(&object##_map, value, length); \
} while (0)

Add an byte array value to the cbor object under the integer key name Example:

To build the an object with the following cbor value Note using base64 encoding in the following example string.

{
  3 : "AAECAwQF"
}

The following code could be used:

// the following bytes equal "AAECAwQF" when base64 encoded
uint8_t byte_string[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
oc_rep_set_byte_string(root, 3, byte_string,
sizeof(byte_string));
#define oc_rep_set_byte_string(object, key, value, length)
Add an byte array value to the cbor object under the key name Example:
Definition: oc_rep.h:485

Definition at line 512 of file oc_rep.h.

◆ oc_rep_i_set_double

#define oc_rep_i_set_double (   object,
  key,
  value 
)
Value:
do { \
g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
g_err |= cbor_encode_double(&object##_map, value); \
} while (0)

Add a double value to the cbor object under the integer key name Example:

To build the an object with the following cbor value

{
  5 : 3.14159
}

The following code could be used:

oc_rep_i_set_double(root, 5, 3.14159);
#define oc_rep_i_set_double(object, key, value)
Add a double value to the cbor object under the integer key name Example:
Definition: oc_rep.h:176
See also
oc_rep_i_get_double

Definition at line 176 of file oc_rep.h.

◆ oc_rep_i_set_double_array

#define oc_rep_i_set_double_array (   object,
  key,
  values,
  length 
)
Value:
do { \
g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
CborEncoder x_key##_value_array; \
g_err |= \
cbor_encoder_create_array(&object##_map, &x_key##_value_array, length); \
int i; \
for (i = 0; i < (length); i++) { \
g_err |= cbor_encode_floating_point(&x_key##_value_array, \
CborDoubleType, &(values)[i]); \
} \
g_err |= \
cbor_encoder_close_container(&object##_map, &x_key##_value_array); \
} while (0)

Add a double array with values of length to the cbor object under the integer key name.

Example:

To build the an object with the following cbor value

{
  6 : [ 3.14159, 2.71828, 1.414121, 1.61803 ]
}

The following code could be used:

double math_constants[] = { 3.14159, 2.71828, 1.414121, 1.61803 };
6,
math_constants,
(int)(sizeof(math_constants)/
sizeof(math_constants[0]) ) );
#define oc_rep_i_set_double_array(object, key, values, length)
Add a double array with values of length to the cbor object under the integer key name.
Definition: oc_rep.h:1376
See also
oc_rep_i_get_double_array

Definition at line 1376 of file oc_rep.h.

◆ oc_rep_i_set_float

#define oc_rep_i_set_float (   object,
  key,
  value 
)
Value:
do { \
g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
g_err |= cbor_encode_float(&object##_map, value); \
} while (0)

Add a float value to the cbor object under the integer key name Example:

To build the an object with the following cbor value

{
  5 : 3.14159
}

The following code could be used:

oc_rep_i_set_float(root, 5, 3.14159);
#define oc_rep_i_set_float(object, key, value)
Add a float value to the cbor object under the integer key name Example:
Definition: oc_rep.h:224
See also
oc_rep_i_get_float

Definition at line 224 of file oc_rep.h.

◆ oc_rep_i_set_float_array [1/2]

#define oc_rep_i_set_float_array (   object,
  key,
  values,
  length 
)
Value:
do { \
g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
CborEncoder x_key##_value_array; \
g_err |= \
cbor_encoder_create_array(&object##_map, &x_key##_value_array, length); \
int i; \
for (i = 0; i < (length); i++) { \
g_err |= cbor_encode_floating_point(&x_key##_value_array, CborFloatType, \
&(values)[i]); \
} \
g_err |= \
cbor_encoder_close_container(&object##_map, &x_key##_value_array); \
} while (0)

Add a float array with values of length to the cbor object under the integer key name.

Example:

To build the an object with the following cbor value

{
  6 : [ 3.14159, 2.71828, 1.414121, 1.61803 ]
}

The following code could be used:

float math_constants[] = { 3.14159, 2.71828, 1.414121, 1.61803 };
6,
math_constants,
(int)(sizeof(math_constants)/
sizeof(math_constants[0]) ) );
#define oc_rep_i_set_float_array(object, key, values, length)
Add a float array with values of length to the cbor object under the integer key name.
Definition: oc_rep.h:1520
See also
oc_rep_i_get_float_array

Definition at line 1520 of file oc_rep.h.

◆ oc_rep_i_set_float_array [2/2]

#define oc_rep_i_set_float_array (   object,
  key,
  values,
  length 
)
Value:
do { \
g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
CborEncoder x_key##_value_array; \
g_err |= \
cbor_encoder_create_array(&object##_map, &x_key##_value_array, length); \
int i; \
for (i = 0; i < (length); i++) { \
g_err |= cbor_encode_floating_point(&x_key##_value_array, CborFloatType, \
&(values)[i]); \
} \
g_err |= \
cbor_encoder_close_container(&object##_map, &x_key##_value_array); \
} while (0)

Add a float array with values of length to the cbor object under the integer key name.

Example:

To build the an object with the following cbor value

{
  6 : [ 3.14159, 2.71828, 1.414121, 1.61803 ]
}

The following code could be used:

float math_constants[] = { 3.14159, 2.71828, 1.414121, 1.61803 };
6,
math_constants,
(int)(sizeof(math_constants)/
sizeof(math_constants[0]) ) );
See also
oc_rep_i_get_float_array

Definition at line 1520 of file oc_rep.h.

◆ oc_rep_i_set_int

#define oc_rep_i_set_int (   object,
  key,
  value 
)
Value:
do { \
g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
g_err |= cbor_encode_int(&object##_map, value); \
} while (0)

Add an integer value to the cbor object under the integer key name Example:

To build the an object with the following cbor value

{
  5 : 42
}

The following code could be used:

oc_rep_set_int(root, 5, 42);
#define oc_rep_set_int(object, key, value)
Add an integer value to the cbor object under the key name Example:
Definition: oc_rep.h:249
See also
oc_rep_i_get_int

Definition at line 274 of file oc_rep.h.

◆ oc_rep_i_set_int_array

#define oc_rep_i_set_int_array (   object,
  key,
  values,
  length 
)
Value:
do { \
g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
CborEncoder x_key##_value_array; \
g_err |= \
cbor_encoder_create_array(&object##_map, &x_key##_value_array, length); \
int i; \
for (i = 0; i < (length); i++) { \
g_err |= cbor_encode_int(&x_key##_value_array, (values)[i]); \
} \
g_err |= \
cbor_encoder_close_container(&object##_map, &x_key##_value_array); \
} while (0)

Add an integer array with values of length to the cbor object under the integer key name.

Example:

To build the an object with the following cbor value

{
  3 : [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ]
}

The following code could be used:

int fib[] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
3,
fib,
(int)(sizeof(fib)/ sizeof(fib[0]) ) );
#define oc_rep_i_set_int_array(object, key, values, length)
Add an integer array with values of length to the cbor object under the integer key name.
Definition: oc_rep.h:1140

Definition at line 1140 of file oc_rep.h.

◆ oc_rep_i_set_key

#define oc_rep_i_set_key (   parent,
  key 
)
Value:
if ((uint64_t)(key) != 0) \
g_err |= cbor_encode_int(parent, (int64_t)(key));

End users are very unlikely to use this macro.

This will add a key as integer to a parent object.

This is almost always followed by oc_rep_begin_array to build an array when the number of items being placed in the array are not known before the end of the array.

See oc_rep_begin_array for example code

See also
oc_rep_begin_array

Definition at line 911 of file oc_rep.h.

◆ oc_rep_i_set_string_array

#define oc_rep_i_set_string_array (   object,
  key,
  values 
)
Value:
do { \
g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
CborEncoder x_key##_value_array; \
g_err |= cbor_encoder_create_array(&object##_map, &x_key##_value_array, \
CborIndefiniteLength); \
int i; \
for (i = 0; i < (int)oc_string_array_get_allocated_size(values); i++) { \
if (oc_string_array_get_item_size(values, i) > 0) { \
g_err |= cbor_encode_text_string( \
&x_key##_value_array, oc_string_array_get_item(values, i), \
oc_string_array_get_item_size(values, i)); \
} \
} \
g_err |= \
cbor_encoder_close_container(&object##_map, &x_key##_value_array); \
} while (0)

Add a string array using an oc_string_array_t as values to the cbor object under the integer key name.

Example:

To build the an object with the following cbor value

{
  4 : [
  "Do not take life too seriously. You will never get out of it alive.",
  "All generalizations are false, including this one.",
  "Those who believe in telekinetics, raise my hand.",
  "I refuse to join any club that would have me as a member."
  ]
}

The following code could be used:

const char* str0 = "Do not take life too seriously. You will never get
out of it alive.";
const char* str1 = "All generalizations are false, including this one.";
const char* str2 = "Those who believe in telekinetics, raise my hand.";
const char* str3 = "I refuse to join any club that would have me as a
member.";
oc_string_array_t quotes;
oc_new_string_array(&quotes, (size_t)4);
oc_string_array_add_item(quotes, str0);
oc_string_array_add_item(quotes, str1);
oc_string_array_add_item(quotes, str2);
oc_string_array_add_item(quotes, str3);
//add values to root object
oc_rep_i_set_string_array(root, 4, quotes);
#define oc_free_string_array(ocstringarray)
free oc string array
Definition: oc_helpers.h:185
#define oc_new_string_array(ocstringarray, size)
new oc string array
Definition: oc_helpers.h:178
#define oc_rep_i_set_string_array(object, key, values)
Add a string array using an oc_string_array_t as values to the cbor object under the integer key name...
Definition: oc_rep.h:1581
See also
oc_string_array_t
oc_new_string_array
oc_free_string_array
oc_string_array_add_item

Definition at line 1581 of file oc_rep.h.

◆ oc_rep_i_set_text_string

#define oc_rep_i_set_text_string (   object,
  key,
  value 
)
Value:
do { \
g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
if ((const char *)(value) != NULL) { \
g_err |= cbor_encode_text_string(&object##_map, value, strlen(value)); \
} else { \
g_err |= cbor_encode_text_string(&object##_map, "", 0); \
} \
} while (0)

Add an string value to the cbor object under the integer key name Example:

To build the an object with the following cbor value

{
  4 : "Hello, world!"
}

The following code could be used:

oc_rep_set_text_string(root, 4, "Hello, world!");
#define oc_rep_set_text_string(object, key, value)
Add an string value to the cbor object under the key name Example:
Definition: oc_rep.h:401

Definition at line 428 of file oc_rep.h.

◆ oc_rep_i_set_uint

#define oc_rep_i_set_uint (   object,
  key,
  value 
)
Value:
do { \
g_err |= cbor_encode_int(&object##_map, (int64_t)(key)); \
g_err |= cbor_encode_uint(&object##_map, value); \
} while (0)

Add an unsigned integer value to the cbor object under the integer key name Example:

To build the an object with the following cbor value

{
  5: 42
}

The following code could be used:

oc_rep_set_uint(root, 5, 42);
#define oc_rep_set_uint(object, key, value)
Add an unsigned integer value to the cbor object under the key name Example:
Definition: oc_rep.h:301

Note: when the cbor object is converted to a oc_rep_the data type will be encoded as an OC_REP_INT. There is no way for a client to know that the server sent the INT as an unsigned value.

Definition at line 328 of file oc_rep.h.

◆ oc_rep_object

#define oc_rep_object (   name)    &name##_map

Get a pointer to the cbor object with the given name

Returns
cbor object pointer

Definition at line 125 of file oc_rep.h.

◆ oc_rep_object_array_begin_item

#define oc_rep_object_array_begin_item (   key)     oc_rep_begin_object(&key##_array, key)

Begin a cbor object for an array of cbor objects.

The key is the name of the array object.

Example:

To build the an object with the following cbor value

{
  "space2001": [
                {"name": "Dave Bowman", "job": "astronaut"},
                {"name": "Frank Poole", "job": "astronaut"},
                {"name": "Hal 9000", "job": "AI computer"}
                ]
}

The following code could be used:

oc_rep_set_array(root, space2001);
oc_rep_set_text_string(space2001, name, "Dave Bowman");
oc_rep_set_text_string(space2001, job, "astronaut");
oc_rep_set_text_string(space2001, name, "Frank Poole");
oc_rep_set_text_string(space2001, job, "astronaut");
oc_rep_set_text_string(space2001, name, "Hal 9000");
oc_rep_set_text_string(space2001, job, "AI computer");
oc_rep_close_array(root, space2001);
#define oc_rep_object_array_begin_item(key)
Begin a cbor object for an array of cbor objects.
Definition: oc_rep.h:1022
#define oc_rep_set_array(object, key)
This macro has been replaced with oc_rep_open_array.
Definition: oc_rep.h:921
#define oc_rep_object_array_end_item(key)
End the cbor object for the key array of cbor objects.
Definition: oc_rep.h:1028
See also
oc_rep_object_array_end_item

Definition at line 1022 of file oc_rep.h.

◆ oc_rep_object_array_end_item

#define oc_rep_object_array_end_item (   key)    oc_rep_end_object(&key##_array, key)

End the cbor object for the key array of cbor objects.

Definition at line 1028 of file oc_rep.h.

◆ oc_rep_object_array_start_item

#define oc_rep_object_array_start_item (   key)    oc_rep_object_array_begin_item(key)

This macro has been replaced with oc_rep_object_array_begin_item.

See also
oc_rep_object_array_begin_item
oc_rep_object_array_end_item

Definition at line 978 of file oc_rep.h.

◆ oc_rep_open_array

#define oc_rep_open_array (   parent,
  key 
)
Value:
g_err |= cbor_encode_text_string(&parent##_map, #key, strlen(#key)); \
oc_rep_begin_array(&parent##_map, key)

Open a cbor array object belonging to parent object under the key name.

Items can then be added to the array till oc_rep_close_array is called.

Most common array types such as int, bool, double, float and strings have specific macros for handling those array types. This macro will mostly be used to make arrays where the length is unknown ahead of time or to make an array of other objects.

For and example of this macro being used see oc_rep_object_array_begin_item.

See also
oc_rep_close_array

Definition at line 936 of file oc_rep.h.

◆ oc_rep_open_object

#define oc_rep_open_object (   parent,
  key 
)
Value:
g_err |= cbor_encode_text_string(&parent##_map, #key, strlen(#key)); \
oc_rep_begin_object(&parent##_map, key)

Open a cbor object belonging to parent object under the key name.

Items can then be added to the array till oc_rep_close_object is called.

Example:

To build the an object with the following cbor value

{
    "my_object": {
        "a": 1
        "b": false
        "c": "three"
    }
}

The following code could be used:

oc_rep_set_object(root, my_object);
oc_rep_set_int(my_object, a, 1);
oc_rep_set_boolean(my_object, b, false);
oc_rep_set_text_string(my_object, c, "three");
oc_rep_close_object(root, my_object);
#define oc_rep_close_object(parent, key)
Close the object.
Definition: oc_rep.h:1077
#define oc_rep_set_object(object, key)
This macro has been replaced with oc_rep_open_object.
Definition: oc_rep.h:1036
See also
oc_rep_close_object

Definition at line 1067 of file oc_rep.h.

◆ oc_rep_set_array

#define oc_rep_set_array (   object,
  key 
)    oc_rep_open_array(object, key)

This macro has been replaced with oc_rep_open_array.

See also
oc_rep_open_array
oc_rep_close_array

Definition at line 921 of file oc_rep.h.

◆ oc_rep_set_bool_array

#define oc_rep_set_bool_array (   object,
  key,
  values,
  length 
)
Value:
do { \
g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
CborEncoder key##_value_array; \
g_err |= \
cbor_encoder_create_array(&object##_map, &key##_value_array, length); \
int i; \
for (i = 0; i < (length); i++) { \
g_err |= cbor_encode_boolean(&key##_value_array, (values)[i]); \
} \
g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array); \
} while (0)

Add a boolean array with values of length to the cbor object under the key name.

Example:

To build the an object with the following cbor value

{
  "flip": [ false, false, true, false, false ]
}

The following code could be used:

bool flip[] = {false, false, true, false, false };
flip,
flip,
(int)(sizeof(flip)/ sizeof(flip[0]) ) );
#define oc_rep_set_bool_array(object, key, values, length)
Add a boolean array with values of length to the cbor object under the key name.
Definition: oc_rep.h:1178

Definition at line 1178 of file oc_rep.h.

◆ oc_rep_set_boolean

#define oc_rep_set_boolean (   object,
  key,
  value 
)
Value:
do { \
g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
g_err |= cbor_encode_boolean(&object##_map, value); \
} while (0)

Add an boolean value to the cbor object under the key name Example:

To build the an object with the following cbor value

{
  "door_open": false
}

The following code could be used:

See also
oc_rep_get_bool

Definition at line 353 of file oc_rep.h.

◆ oc_rep_set_byte_string

#define oc_rep_set_byte_string (   object,
  key,
  value,
  length 
)
Value:
do { \
g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
g_err |= cbor_encode_byte_string(&object##_map, value, length); \
} while (0)

Add an byte array value to the cbor object under the key name Example:

To build the an object with the following cbor value Note using base64 encoding in the following example string.

{
  "byte_string_key": "AAECAwQF"
}

The following code could be used:

// the following bytes equal "AAECAwQF" when base64 encoded
uint8_t byte_string[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
oc_rep_set_byte_string(root, byte_string_key, byte_string,
sizeof(byte_string));

Definition at line 485 of file oc_rep.h.

◆ oc_rep_set_double

#define oc_rep_set_double (   object,
  key,
  value 
)
Value:
do { \
g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
g_err |= cbor_encode_double(&object##_map, value); \
} while (0)

Add a double value to the cbor object under the key name Example:

To build the an object with the following cbor value

{
  "pi": 3.14159
}

The following code could be used:

oc_rep_set_double(root, pi, 3.14159);
#define oc_rep_set_double(object, key, value)
Add a double value to the cbor object under the key name Example:
Definition: oc_rep.h:151

Definition at line 151 of file oc_rep.h.

◆ oc_rep_set_double_array

#define oc_rep_set_double_array (   object,
  key,
  values,
  length 
)
Value:
do { \
g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
CborEncoder key##_value_array; \
g_err |= \
cbor_encoder_create_array(&object##_map, &key##_value_array, length); \
int i; \
for (i = 0; i < (length); i++) { \
g_err |= cbor_encode_floating_point(&key##_value_array, CborDoubleType, \
&(values)[i]); \
} \
g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array); \
} while (0)

Add a double array with values of length to the cbor object under the key name.

Example:

To build the an object with the following cbor value

{
  "math_constants": [ 3.14159, 2.71828, 1.414121, 1.61803 ]
}

The following code could be used:

double math_constants[] = { 3.14159, 2.71828, 1.414121, 1.61803 };
math_constants,
math_constants,
(int)(sizeof(math_constants)/
sizeof(math_constants[0]) ) );
#define oc_rep_set_double_array(object, key, values, length)
Add a double array with values of length to the cbor object under the key name.
Definition: oc_rep.h:1335

Definition at line 1335 of file oc_rep.h.

◆ oc_rep_set_float

#define oc_rep_set_float (   object,
  key,
  value 
)
Value:
do { \
g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
g_err |= cbor_encode_float(&object##_map, value); \
} while (0)

Add a float value to the cbor object under the key name Example:

To build the an object with the following cbor value

{
  "pi": 3.14159
}

The following code could be used:

oc_rep_set_float(root, pi, 3.14159);
#define oc_rep_set_float(object, key, value)
Add a float value to the cbor object under the key name Example:
Definition: oc_rep.h:199

Definition at line 199 of file oc_rep.h.

◆ oc_rep_set_float_array [1/2]

#define oc_rep_set_float_array (   object,
  key,
  values,
  length 
)
Value:
do { \
g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
CborEncoder key##_value_array; \
g_err |= \
cbor_encoder_create_array(&object##_map, &key##_value_array, length); \
int i; \
for (i = 0; i < (length); i++) { \
g_err |= cbor_encode_floating_point(&key##_value_array, CborFloatType, \
&(values)[i]); \
} \
g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array); \
} while (0)

Add a float array with values of length to the cbor object under the key name.

Example:

To build the an object with the following cbor value

{
  "math_constants": [ 3.14159, 2.71828, 1.414121, 1.61803 ]
}

The following code could be used:

float math_constants[] = { 3.14159, 2.71828, 1.414121, 1.61803 };
math_constants,
math_constants,
(int)(sizeof(math_constants)/
sizeof(math_constants[0]) ) );
#define oc_rep_set_float_array(object, key, values, length)
Add a float array with values of length to the cbor object under the key name.
Definition: oc_rep.h:1479

Definition at line 1479 of file oc_rep.h.

◆ oc_rep_set_float_array [2/2]

#define oc_rep_set_float_array (   object,
  key,
  values,
  length 
)
Value:
do { \
g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
CborEncoder key##_value_array; \
g_err |= \
cbor_encoder_create_array(&object##_map, &key##_value_array, length); \
int i; \
for (i = 0; i < (length); i++) { \
g_err |= cbor_encode_floating_point(&key##_value_array, CborFloatType, \
&(values)[i]); \
} \
g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array); \
} while (0)

Add a float array with values of length to the cbor object under the key name.

Example:

To build the an object with the following cbor value

{
  "math_constants": [ 3.14159, 2.71828, 1.414121, 1.61803 ]
}

The following code could be used:

float math_constants[] = { 3.14159, 2.71828, 1.414121, 1.61803 };
math_constants,
math_constants,
(int)(sizeof(math_constants)/
sizeof(math_constants[0]) ) );

Definition at line 1479 of file oc_rep.h.

◆ oc_rep_set_int

#define oc_rep_set_int (   object,
  key,
  value 
)
Value:
do { \
g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
g_err |= cbor_encode_int(&object##_map, value); \
} while (0)

Add an integer value to the cbor object under the key name Example:

To build the an object with the following cbor value

{
  "power": 42
}

The following code could be used:

See also
oc_rep_get_int

Definition at line 249 of file oc_rep.h.

◆ oc_rep_set_int_array

#define oc_rep_set_int_array (   object,
  key,
  values,
  length 
)
Value:
do { \
g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
CborEncoder key##_value_array; \
g_err |= \
cbor_encoder_create_array(&object##_map, &key##_value_array, length); \
int i; \
for (i = 0; i < (length); i++) { \
g_err |= cbor_encode_int(&key##_value_array, (values)[i]); \
} \
g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array); \
} while (0)

Add an integer array with values of length to the cbor object under the key name.

Example:

To build the an object with the following cbor value

{
  "fibonacci": [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ]
}

The following code could be used:

int fib[] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
fibonacci,
fib,
(int)(sizeof(fib)/ sizeof(fib[0]) ) );
#define oc_rep_set_int_array(object, key, values, length)
Add an integer array with values of length to the cbor object under the key name.
Definition: oc_rep.h:1103

Definition at line 1103 of file oc_rep.h.

◆ oc_rep_set_key

#define oc_rep_set_key (   parent,
  key 
)
Value:
if ((const char *)(key) != NULL) \
g_err |= cbor_encode_text_string(parent, key, strlen(key))

End users are very unlikely to use this macro.

This will add a key to a parent object.

This is almost always followed by oc_rep_begin_array to build an array when the number of items being placed in the array are not known before the end of the array.

See oc_rep_begin_array for example code

See also
oc_rep_begin_array

Definition at line 894 of file oc_rep.h.

◆ oc_rep_set_object

#define oc_rep_set_object (   object,
  key 
)    oc_rep_open_object(object, key)

This macro has been replaced with oc_rep_open_object.

See also
oc_rep_open_object
oc_rep_close_object

Definition at line 1036 of file oc_rep.h.

◆ oc_rep_set_string_array

#define oc_rep_set_string_array (   object,
  key,
  values 
)
Value:
do { \
g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
CborEncoder key##_value_array; \
g_err |= cbor_encoder_create_array(&object##_map, &key##_value_array, \
CborIndefiniteLength); \
int i; \
for (i = 0; i < (int)oc_string_array_get_allocated_size(values); i++) { \
if (oc_string_array_get_item_size(values, i) > 0) { \
g_err |= cbor_encode_text_string( \
&key##_value_array, oc_string_array_get_item(values, i), \
oc_string_array_get_item_size(values, i)); \
} \
} \
g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array); \
} while (0)

Add a string array using an oc_string_array_t as values to the cbor object under the key name.

Example:

To build the an object with the following cbor value

{
  "quotes": [
  "Do not take life too seriously. You will never get out of it alive.",
  "All generalizations are false, including this one.",
  "Those who believe in telekinetics, raise my hand.",
  "I refuse to join any club that would have me as a member."
  ]
}

The following code could be used:

const char* str0 = "Do not take life too seriously. You will never get
out of it alive.";
const char* str1 = "All generalizations are false, including this one.";
const char* str2 = "Those who believe in telekinetics, raise my hand.";
const char* str3 = "I refuse to join any club that would have me as a
member.";
oc_string_array_t quotes;
oc_new_string_array(&quotes, (size_t)4);
oc_string_array_add_item(quotes, str0);
oc_string_array_add_item(quotes, str1);
oc_string_array_add_item(quotes, str2);
oc_string_array_add_item(quotes, str3);
//add values to root object
oc_rep_set_string_array(root, quotes, quotes);
#define oc_rep_set_string_array(object, key, values)
Add a string array using an oc_string_array_t as values to the cbor object under the key name.
Definition: oc_rep.h:1437
See also
oc_string_array_t
oc_new_string_array
oc_free_string_array
oc_string_array_add_item

Definition at line 1437 of file oc_rep.h.

◆ oc_rep_set_text_string

#define oc_rep_set_text_string (   object,
  key,
  value 
)
Value:
do { \
g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
if ((const char *)(value) != NULL) { \
g_err |= cbor_encode_text_string(&object##_map, value, strlen(value)); \
} else { \
g_err |= cbor_encode_text_string(&object##_map, "", 0); \
} \
} while (0)

Add an string value to the cbor object under the key name Example:

To build the an object with the following cbor value

{
  "greeting": "Hello, world!"
}

The following code could be used:

Definition at line 401 of file oc_rep.h.

◆ oc_rep_set_text_string_no_tag

#define oc_rep_set_text_string_no_tag (   object,
  value 
)
Value:
do { \
if ((const char *)(value) != NULL) { \
g_err |= cbor_encode_text_string(&object##_map, value, strlen(value)); \
} else { \
g_err |= cbor_encode_text_string(&object##_map, "", 0); \
} \
} while (0)

Add an string value to the cbor object under the key name Example:

To build the an object with the following cbor value

{
  "Hello, world!"
}

The following code could be used:

Definition at line 455 of file oc_rep.h.

◆ oc_rep_set_uint

#define oc_rep_set_uint (   object,
  key,
  value 
)
Value:
do { \
g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \
g_err |= cbor_encode_uint(&object##_map, value); \
} while (0)

Add an unsigned integer value to the cbor object under the key name Example:

To build the an object with the following cbor value

{
  "power": 42
}

The following code could be used:

Note: when the cbor object is converted to a oc_rep_the data type will be encoded as an OC_REP_INT. There is no way for a client to know that the server sent the INT as an unsigned value.

Definition at line 301 of file oc_rep.h.

◆ oc_rep_set_value_text_string

#define oc_rep_set_value_text_string (   parent,
  value 
)
Value:
do { \
if ((const char *)(value) != NULL) { \
g_err |= cbor_encode_text_string(&parent##_map, value, strlen(value)); \
} else { \
g_err |= cbor_encode_text_string(&parent##_map, "", 0); \
} \
} while (0)

Definition at line 717 of file oc_rep.h.

◆ oc_rep_start_array

#define oc_rep_start_array (   parent,
  name 
)    oc_rep_begin_array(parent, name)

This macro has been replaced with oc_rep_begin_array.

See also
oc_rep_start_array
oc_rep_end_array
oc_rep_open_array
oc_rep_close_array

Definition at line 526 of file oc_rep.h.

◆ oc_rep_start_object

#define oc_rep_start_object (   parent,
  key 
)    oc_rep_begin_object(parent, key)

This macro has been replaced with oc_rep_begin_object.

See also
oc_rep_begin_object
oc_rep_end_object

Definition at line 954 of file oc_rep.h.

Typedef Documentation

◆ oc_rep_t

typedef 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 responses to find values by

  • tag name or tag identifier when the tag name is NULL then the tag identifier is being used.
  • type of the value

Function Documentation

◆ oc_print_rep_as_json()

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 lines false: JSON formatted on a single line

Parameters
repthe response tree
pretty_printtrue: nicely formatted json

◆ oc_rep_add_line_size_to_buffer()

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

Parameters
linedata to be added
lenthe length of the line data to be added
Returns
length of the line added to the response buffer

◆ oc_rep_add_line_to_buffer()

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

Parameters
linedata to be added
Returns
length of the line added to the response buffer

◆ oc_rep_encode_raw()

void oc_rep_encode_raw ( const uint8_t *  data,
size_t  len 
)

Encode raw data, as if it was already encoded.

using the global encoder.

Parameters
dataPointer to data to be encoded. Will be copied into the global buffer.
lenLength of data.

◆ oc_rep_encode_raw_encoder()

void oc_rep_encode_raw_encoder ( CborEncoder *  encoder,
const uint8_t *  data,
size_t  len 
)

Encode raw data, as if it was already encoded.

Parameters
encoderThe encoder to be used.
dataPointer to data to be encoded. Will be copied into the global buffer.
lenLength of data.

◆ oc_rep_get_bool()

bool oc_rep_get_bool ( oc_rep_t rep,
const char *  key,
bool *  value 
)

Read a boolean value from an oc_rep_t

Example:

bool door_open_flag = false;
if( true == oc_rep_get_bool(rep, "door_open", &door_open_flag)) {
printf("The door is open : %s\n", (door_open_flag) ? "true" :
"false");
}
bool oc_rep_get_bool(oc_rep_t *rep, const char *key, bool *value)
Read a boolean value from an oc_rep_t
Parameters
repoc_rep_t to read boolean value from
keythe key name for the boolean value
valuethe return boolean value
Returns
true if key and value are found and returned.
See also
oc_rep_set_boolean

◆ oc_rep_get_bool_array()

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

Example:

bool* flip_out = 0;
size_t flip_len;
if( true == oc_rep_get_bool_array(rep, "flip", &flip_out, &flip_len)) {
// flip_out can now be used
}
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
Parameters
repoc_rep_t to boolean array value from
keythe key name for the boolean array value
valuethe return boolean array value
sizethe size of the boolean array
Returns
true if key and value are found and returned.
See also
oc_rep_set_bool_array

◆ oc_rep_get_byte_string()

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

Example:

char* byte_string_out = NULL;
size_t str_len;
if( true == oc_rep_get_byte_string(rep, "byte_string_key",
&byte_string_out, &str_len)) {
// byte_string_out can be used
}
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
Parameters
repoc_rep_t to read byte string value from
keythe key name for the byte string value
valuethe return byte string value
sizethe size of the byte string
Returns
true if key and value are found and returned.
See also
oc_rep_set_byte_string

◆ oc_rep_get_byte_string_array()

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

Example:

oc_string_array_t barray_out;
size_t barray_len;
"barray",
&barray_out,
&barray_len)) {
for (size_t i = 0; i < barray_len); i++) {
char* value = oc_byte_string_array_get_item(barray_out, i);
size_t value_len =oc_byte_string_array_get_item_size(barray_out,
i);
// access the individual byte string
}
}
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
Parameters
repoc_rep_t to byte string array value from
keythe key name for the byte string array value
valuethe return byte string array value
sizethe size of the byte string array
Returns
true if key and value are found and returned.
See also
oc_rep_add_byte_string
oc_byte_string_array_get_item
oc_byte_string_array_get_item_size

◆ oc_rep_get_cbor_errno()

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_*, oc_rep_open_*, and oc_rep_close_* macros to check if an error occurred while executing the commands.

If the value returned is anything other than CborNoError then one of the oc_rep_* macros failed.

The error returned is not automatically cleared. To clear the error set g_err to CborNoError.

◆ oc_rep_get_double()

bool oc_rep_get_double ( oc_rep_t rep,
const char *  key,
double *  value 
)

Read a double value from an oc_rep_t

Example:

double pi_out = 0;
if( true == oc_rep_get_double(rep, "pi", &pi_out)) {
printf("The value for 'pi' is : %f\n", pi_out);
}
bool oc_rep_get_double(oc_rep_t *rep, const char *key, double *value)
Read a double value from an oc_rep_t
Parameters
repoc_rep_t to read double value from
keythe key name for the double value
valuethe return double value
Returns
true if key and value are found and returned.
See also
oc_rep_set_double

◆ oc_rep_get_double_array()

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

Example:

double* math_constants_out = 0;
size_t math_constants_len;
if( true == oc_rep_get_double_array(rep,
"math_constants",
&math_constants_out,
&math_constants_len)) {
// math_constants_out can now be used
}
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
Parameters
repoc_rep_t to double array value from
keythe key name for the double array value
valuethe return double array value
sizethe size of the double array
Returns
true if key and value are found and returned.
See also
oc_rep_set_double_array

◆ oc_rep_get_encoded_payload_size()

int oc_rep_get_encoded_payload_size ( void  )

Get the size of the cbor encoded data.

This can be used to check if the cbor encode data will fit inside the payload buffer. If the payload buffer is too small -1 is returned.

Returns
  • the size of the cbor encoded data.
  • returns -1 if the cbor encoded data will not fit in the oc_rep_t payload
See also
oc_rep_new

◆ oc_rep_get_encoder_buf()

const uint8_t* oc_rep_get_encoder_buf ( void  )

Get the buffer pointer at the start of the encoded cbor data.

This is used when parsing the encoded cbor data to an oc_rep_t. It is unlikely to be used outside the stack library.

Returns
pointer to the start of the cbor encoded buffer
See also
oc_parse_rep

◆ oc_rep_get_float()

bool oc_rep_get_float ( oc_rep_t rep,
const char *  key,
float *  value 
)

Read a float value from an oc_rep_t

Example:

float pi_out = 0;
if( true == oc_rep_get_float(rep, "pi", &pi_out)) {
printf("The value for 'pi' is : %f\n", pi_out);
}
bool oc_rep_get_float(oc_rep_t *rep, const char *key, float *value)
Read a float value from an oc_rep_t
Parameters
repoc_rep_t to read float value from
keythe key name for the float value
valuethe return float value
Returns
true if key and value are found and returned.
See also
oc_rep_set_float

◆ oc_rep_get_float_array()

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

Example:

float* math_constants_out = 0;
size_t math_constants_len;
if( true == oc_rep_get_float_array(rep,
"math_constants",
&math_constants_out,
&math_constants_len)) {
// math_constants_out can now be used
}
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
Parameters
repoc_rep_t to float array value from
keythe key name for the float array value
valuethe return float array value
sizethe size of the float array
Returns
true if key and value are found and returned.
See also
oc_rep_set_float_array

◆ oc_rep_get_int()

bool oc_rep_get_int ( oc_rep_t rep,
const char *  key,
int64_t *  value 
)

Read an integer from an oc_rep_t

Example:

int ultimate_answer_out = 0;
if( true == oc_rep_get_int(rep, "ultimate_answer",
&ultimate_answer_out)) {
printf("The ultimate answer is : %d\n", ultimate_answer_out);
}
bool oc_rep_get_int(oc_rep_t *rep, const char *key, int64_t *value)
Read an integer from an oc_rep_t
Parameters
repoc_rep_t to read int value from
keythe key name for the integer value
valuethe return integer value
Returns
true if key and value are found and returned.
See also
oc_rep_set_int

◆ oc_rep_get_int_array()

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

Example:

int* fib_out = 0;
size_t fib_len;
if( true == oc_rep_get_int_array(rep, "fibonacci", &fib_out, &fib_len)) {
// fib_out can now be used
}
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
Parameters
repoc_rep_t to integer array value from
keythe key name for the integer array value
valuethe return integer array value
sizethe size of the integer array
Returns
true if key and value are found and returned.
See also
oc_rep_set_int_array

◆ oc_rep_get_mixed_array()

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

Calling the returned value an array is a misnomer. The value actually returned is a linked list of oc_rep_t objects. The linked list must be walked to see each item in the mixed array.

Example:

oc_rep_t * space_2001_out = NULL;
if ( true == oc_rep_get_mixed_array(rep, "space_2001", &space_2001_out))
{
while (space_2001_out != NULL) {
char * str_out = NULL;
size_t str_out_size = 0;
if (space_2001_out->type == OC_REP_STRING)
printf("Character Name: %s",
oc_string(space_2001_out->value.string)); space_2001_out =
space_2001_out->next;
}
}
#define oc_string(ocstring)
cast oc_string to string
Definition: oc_helpers.h:49
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
parsed entry of a cbor object This represents a link list of response values one can iterate over the...
Definition: oc_rep.h:1642
oc_rep_value_type_t type
type of the data
Definition: oc_rep.h:1643
union oc_rep_s::oc_rep_value value
the value as union
struct oc_rep_s * next
next in list
Definition: oc_rep.h:1644
Parameters
repoc_rep_t to read mixed array value from
keythe key name for the mixed array value
valuethe return mixed array value
Returns
true if key and value are found and returned.
See also
oc_rep_set_{type}

◆ oc_rep_get_object()

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

Example:

oc_rep_t * my_object_out = NULL;
if ( true == oc_rep_get_object(rep, "my_object", &my_object_out)) {
int a_out;
if (oc_rep_get_int(my_object_out, "a", &a_out))
printf("a = %d\n", a_out);
bool b_out = true;
if (oc_rep_get_bool(my_object_out, "b", &b_out))
printf("b = %s\n", (b_out) ? "true" : "false");
char * c_out = NULL;
size_t c_out_size = 0;
if (oc_rep_get_string(my_object_out, "c", &c_out, &c_out_size))
printf("c = %s\n", c_cout);
}
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_get_object(oc_rep_t *rep, const char *key, oc_rep_t **value)
Read a object value from an oc_rep_t
Parameters
repoc_rep_t to read object value from
keythe key name for the object value
valuethe return object value
Returns
true if key and value are found and returned.
See also
oc_rep_set_object

◆ oc_rep_get_object_array()

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

Calling the returned value an array is a misnomer. The value actually returned is a linked list of oc_rep_t objects. The linked list must be walked to see each item in the object array.

Example:

oc_rep_t * space_2001_out = NULL;
if ( true == oc_rep_get_object_array(rep, "space_2001", &space_2001_out))
{
while (space_2001_out != NULL) {
char * str_out = NULL;
size_t str_out_size = 0;
if (oc_rep_get_string(space_2001_out->value.object,
"name",
&str_out,
&str_out_size))
printf("Character Name: %s", str_out);
if (oc_rep_get_string(space_2001_out->value.object,
"job",
&str_out, &str_out_size))
printf(" job %s\n", str_out);
space_2001_out = space_2001_out->next;
}
}
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
Parameters
repoc_rep_t to read object array value from
keythe key name for the object array value
valuethe return object array value
Returns
true if key and value are found and returned.
See also
oc_rep_set_object

◆ oc_rep_get_string()

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

Example:

char* greeting_out = NULL;
size_t str_len;
if( true == oc_rep_get_string(rep, "greeting", &greeting_out, &str_len))
{
printf("%s\n", greeting_out);
}
Parameters
repoc_rep_t to read string value from
keythe key name for the string value
valuethe return string value
sizethe size of the string
Returns
true if key and value are found and returned.
See also
oc_rep_set_text_string

◆ oc_rep_get_string_array()

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

Example:

oc_string_array_t quotes_out;
size_t quotes_len;
if( true == oc_rep_get_string_array(rep,
"quotes",
&quotes_out,
&quotes_len)) {
printf("Quotes :\n")
for (size_t i = 0; i < barray_len); i++) {
char* value = oc_string_array_get_item(quotes_out, i);
size_t value_len = oc_string_array_get_item_size(quotes_out, i);
printf("[%zd] %s\n", i + 1, value);
}
}
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
Parameters
repoc_rep_t to string array value from
keythe key name for the string array value
valuethe return string array value
sizethe size of the string array
Returns
true if key and value are found and returned.
See also
oc_rep_set_string_array
oc_rep_add_text_string
oc_string_array_get_item
oc_string_array_get_item_size

◆ oc_rep_i_get_bool()

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.

Example:

bool door_open_flag = false;
if( true == oc_rep_i_get_bool(rep, 5, &door_open_flag)) {
printf("The door is open : %s\n", (door_open_flag) ? "true" :
"false");
}
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.
Parameters
repoc_rep_t to read boolean value from
keythe integer key name for the boolean value
valuethe return boolean value
Returns
true if key and value are found and returned.
See also
oc_rep_i_set_boolean

◆ oc_rep_i_get_bool_array()

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.

Example:

bool* flip_out = 0;
size_t flip_len;
if( true == oc_rep_i_get_bool_array(rep, 3, &flip_out, &flip_len)) {
// flip_out can now be used
}
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.
Parameters
repoc_rep_t to boolean array value from
keythe key name for the boolean array value
valuethe return boolean array value
sizethe size of the boolean array
Returns
true if key and value are found and returned.
See also
oc_rep_i_set_bool_array

◆ oc_rep_i_get_byte_string()

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.

Example:

char* byte_string_out = NULL;
size_t str_len;
if( true == oc_rep_i_get_byte_string(rep, 5,
&byte_string_out, &str_len)) {
// byte_string_out can be used
}
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.
Parameters
repoc_rep_t to read byte string value from
keythe key name for the byte string value
valuethe return byte string value
sizethe size of the byte string
Returns
true if key and value are found and returned.
See also
oc_rep_i_set_byte_string

◆ oc_rep_i_get_byte_string_array()

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.

Example:

oc_string_array_t barray_out;
size_t barray_len;
5,
&barray_out,
&barray_len)) {
for (size_t i = 0; i < barray_len); i++) {
char* value = oc_byte_string_array_get_item(barray_out, i);
size_t value_len = oc_byte_string_array_get_item_size(
barray_out, i);
// access the individual byte string
}
}
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.
Parameters
repoc_rep_t to byte string array value from
keythe key name for the byte string array value
valuethe return byte string array value
sizethe size of the byte string array
Returns
true if key and value are found and returned.
See also
oc_rep_add_byte_string
oc_byte_string_array_get_item
oc_byte_string_array_get_item_size

◆ oc_rep_i_get_double()

bool oc_rep_i_get_double ( oc_rep_t rep,
int  key,
double *  value 
)

Read a double value from an oc_rep_t

Example:

double pi_out = 0;
if( true == oc_rep_i_get_double(rep, "5, &pi_out)) {
printf("The value for 'pi' is : %f\n", pi_out);
}
bool oc_rep_i_get_double(oc_rep_t *rep, int key, double *value)
Read a double value from an oc_rep_t
Parameters
repoc_rep_t to read double value from
keythe key name for the double value, as integer
valuethe return double value
Returns
true if key and value are found and returned.
See also
oc_rep_i_set_double

◆ oc_rep_i_get_double_array()

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.

Example:

double* math_constants_out = 0;
size_t math_constants_len;
if( true == oc_rep_i_get_double_array(rep,
3,
&math_constants_out,
&math_constants_len)) {
// math_constants_out can now be used
}
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.
Parameters
repoc_rep_t to double array value from
keythe key name for the double array value
valuethe return double array value
sizethe size of the double array
Returns
true if key and value are found and returned.
See also
oc_rep_i_set_double_array

◆ oc_rep_i_get_float()

bool oc_rep_i_get_float ( oc_rep_t rep,
int  key,
float *  value 
)

Read a float value from an oc_rep_t

Example:

float pi_out = 0;
if( true == oc_rep_i_get_float(rep, "5, &pi_out)) {
printf("The value for 'pi' is : %f\n", pi_out);
}
bool oc_rep_i_get_float(oc_rep_t *rep, int key, float *value)
Read a float value from an oc_rep_t
Parameters
repoc_rep_t to read float value from
keythe key name for the float value, as integer
valuethe return float value
Returns
true if key and value are found and returned.
See also
oc_rep_i_set_float

◆ oc_rep_i_get_float_array()

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.

Example:

float* math_constants_out = 0;
size_t math_constants_len;
if( true == oc_rep_i_get_float_array(rep,
3,
&math_constants_out,
&math_constants_len)) {
// math_constants_out can now be used
}
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.
Parameters
repoc_rep_t to float array value from
keythe key name for the float array value
valuethe return float array value
sizethe size of the float array
Returns
true if key and value are found and returned.
See also
oc_rep_i_set_float_array

◆ oc_rep_i_get_int()

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.

Example:

int ultimate_answer_out = 0;
if( true == oc_rep_i_get_int(rep, 5,
&ultimate_answer_out)) {
printf("The ultimate answer is : %d\n", ultimate_answer_out);
}
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.
Parameters
repoc_rep_t to read int value from
keythe key name for the integer value
valuethe return integer value
Returns
true if key and value are found and returned.
See also
oc_rep_i_set_int

◆ oc_rep_i_get_int_array()

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.

Example:

int* fib_out = 0;
size_t fib_len;
if( true == oc_rep_i_get_int_array(rep, 3, &fib_out, &fib_len)) {
// fib_out can now be used
}
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.
Parameters
repoc_rep_t to integer array value from
keythe key name for the integer array value
valuethe return integer array value
sizethe size of the integer array
Returns
true if key and value are found and returned.
See also
oc_rep_i_set_int_array

◆ oc_rep_i_get_mixed_array()

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.

Calling the returned value an array is a misnomer. The value actually returned is a linked list of oc_rep_t objects. The linked list must be walked to see each item in the mixed array.

Example:

oc_rep_t * space_2001_out = NULL;
if ( true == oc_rep_i_get_mixed_array(rep, 5, &space_2001_out))
{
while (space_2001_out != NULL) {
char * str_out = NULL;
size_t str_out_size = 0;
if (space_2001_out->type == OC_REP_STRING)
printf("Character Name: %s",
oc_string(space_2001_out->value.string)); space_2001_out =
space_2001_out->next;
}
}
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.
Parameters
repoc_rep_t to read mixed array value from
keythe key name for the mixed array value
valuethe return mixed array value
Returns
true if key and value are found and returned.
See also
oc_rep_set_{type}

◆ oc_rep_i_get_object()

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.

Example:

oc_rep_t * my_object_out = NULL;
if ( true == oc_rep_i_get_object(rep, 5, &my_object_out)) {
int a_out;
if (oc_rep_i_get_int(my_object_out, 2, &a_out))
printf("a = %d\n", a_out);
bool b_out = true;
if (oc_rep_i_get_bool(my_object_out, 3, &b_out))
printf("b = %s\n", (b_out) ? "true" : "false");
char * c_out = NULL;
size_t c_out_size = 0;
if (oc_rep_i_get_string(my_object_out,4, &c_out, &c_out_size))
printf("c = %s\n", c_cout);
}
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_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.
Parameters
repoc_rep_t to read object value from
keythe key name for the object value
valuethe return object value
Returns
true if key and value are found and returned.
See also
oc_rep_i_set_object

◆ oc_rep_i_get_object_array()

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.

Calling the returned value an array is a misnomer. The value actually returned is a linked list of oc_rep_t objects. The linked list must be walked to see each item in the object array.

Example:

oc_rep_t * space_2001_out = NULL;
if ( true == oc_rep_i_get_object_array(rep, 5, &space_2001_out))
{
while (space_2001_out != NULL) {
char * str_out = NULL;
size_t str_out_size = 0;
if (oc_rep_i_get_string(space_2001_out->value.object,
3,
&str_out,
&str_out_size))
printf("Character Name: %s", str_out);
if (oc_rep_i_get_string(space_2001_out->value.object,
4,
&str_out, &str_out_size))
printf(" job %s\n", str_out);
space_2001_out = space_2001_out->next;
}
}
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.
Parameters
repoc_rep_t to read object array value from
keythe key name for the object array value
valuethe return object array value
Returns
true if key and value are found and returned.
See also
oc_rep_set_object

◆ oc_rep_i_get_string()

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.

Example:

char* greeting_out = NULL;
size_t str_len;
if( true == oc_rep_i_get_string(rep, "greeting", &greeting_out,
&str_len))
{
printf("%s\n", greeting_out);
}
Parameters
repoc_rep_t to read string value from
keythe key name for the string value
valuethe return string value
sizethe size of the string
Returns
true if key and value are found and returned.
See also
oc_rep_i_set_text_string

◆ oc_rep_i_get_string_array()

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.

Example:

oc_string_array_t quotes_out;
size_t quotes_len;
if( true == oc_rep_i_get_string_array(rep,
5,
&quotes_out,
&quotes_len)) {
printf("Quotes :\n")
for (size_t i = 0; i < barray_len); i++) {
char* value = oc_string_array_get_item(quotes_out, i);
size_t value_len = oc_string_array_get_item_size(quotes_out, i);
printf("[%zd] %s\n", i + 1, value);
}
}
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.
Parameters
repoc_rep_t to string array value from
keythe key name for the string array value
valuethe return string array value
sizethe size of the string array
Returns
true if key and value are found and returned.
See also
oc_rep_i_set_string_array
oc_rep_add_text_string
oc_string_array_get_item
oc_string_array_get_item_size

◆ oc_rep_new()

void oc_rep_new ( uint8_t *  payload,
int  size 
)

Initialize the buffer used to hold the cbor encoded data.

Unlikely to be used by outside the stack library.

Parameters
[in]payloadpointer to payload buffer
[in]sizethe size of the payload buffer

◆ oc_rep_to_json()

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.

An oc_rep_t that is NULL or empty will return as an empty JSON object "{}".

All binary data will be encoded to a string using base64 encoding.

Converting binary data to a base64 encoded string is only done if the buf can hold the entire base64 string. If the resulting base64 string would overflow the buffer nothing is placed in the buffer.

The function will not write more than buf_size bytes (including the terminating null byte ('\0')). If the output was truncated due to this limit then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available. Thus, a return value of buf_size or more means that the output was truncated.

NOTE: if the key is an integer, the key will be printed as string e.g. "5". integers with value 0 will not be framed.

Parameters
[in]repthe oc_rep_t object to be converted to JSON
[out]bufa char array that will hold the JSON encoded string.
[in]buf_sizethe size of the passed in char array
[in]pretty_printif true extra white space and new lines will be added to the output making it more human readable. Note return value will differ if pretty_print value is changed.

Example:

char * json;
size_t json_size;
json_size = oc_rep_to_json(rep, NULL, 0, true);
json = (char *)malloc(json_size + 1);
oc_rep_to_json(rep, json, json_size + 1, true);
printf("%s", rep);
free(json);
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.
Returns
the number of characters printed (excluding the null byte used to end output to strings).