IoTivity-Lite
oc_rep.h File Reference
#include "oc_config.h"
#include "oc_export.h"
#include "oc_helpers.h"
#include "util/oc_compiler.h"
#include "util/oc_memb.h"
#include "util/oc_features.h"
#include "cbor.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

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 |= oc_rep_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 |= oc_rep_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 |= oc_rep_encode_double(&parent##_array, value)
 Add an double value to a parent array. More...
 
#define oc_rep_add_int(parent, value)    g_err |= oc_rep_encode_int(&parent##_array, value)
 Add an int value to a parent array. More...
 
#define oc_rep_add_text_string(parent, value)    oc_rep_add_text_string_v1(parent, value, (value) != NULL ? strlen(value) : 0)
 Add a text string value to a parent array. More...
 
#define oc_rep_add_text_string_v1(parent, value, value_len)
 Alternative to oc_rep_add_text_string in case we know the length of the value. 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 IoTivity-lite library. More...
 
#define oc_rep_begin_links_array()
 
#define oc_rep_begin_object(parent, key)
 
#define oc_rep_begin_root_object()
 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 |= oc_rep_encoder_close_container(oc_rep_get_encoder(), &links_array)
 
#define oc_rep_end_object(parent, key)
 
#define oc_rep_end_root_object()    g_err |= oc_rep_encoder_close_container(oc_rep_get_encoder(), &root_map)
 End the root object. 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_int(object, key, value)    g_err |= oc_rep_object_set_int(&object##_map, #key, sizeof(#key) - 1, 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)    oc_rep_set_key_v1(parent, key, (key) != NULL ? strlen(key) : 0)
 End users are very unlikely to use this macro. More...
 
#define oc_rep_set_key_v1(parent, key, key_len)
 Alternative to oc_rep_set_key in case we know the length of the key. More...
 
#define oc_rep_set_null(object, key)    g_err |= oc_rep_object_set_null(&object##_map, #key, sizeof(#key) - 1)
 Add an null value to the cbor object under the key name Example: 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_v1(object, key, value, value_len)
 Alternative to oc_rep_set_text_string in case we know the length of the value. More...
 
#define oc_rep_set_uint(object, key, value)    g_err |= oc_rep_object_set_uint(&object##_map, #key, sizeof(#key) - 1, 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 |= oc_rep_encode_boolean(&parent##_map, value)
 
#define oc_rep_set_value_byte_string(parent, value, value_len)    g_err |= oc_rep_encode_byte_string(&parent##_map, value, value_len)
 
#define oc_rep_set_value_double(parent, value)    g_err |= oc_rep_encode_double(&parent##_map, value)
 
#define oc_rep_set_value_int(parent, value)    g_err |= oc_rep_encode_int(&parent##_map, value)
 
#define oc_rep_set_value_text_string(parent, value)    oc_rep_set_value_text_string_v1(parent, (value), (value) != NULL ? strlen(value))
 
#define oc_rep_set_value_text_string_v1(parent, value, value_len)
 Alternative to oc_rep_set_value_text_string in case we know the length of the value. More...
 
#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...
 
#define oc_rep_start_root_object()   oc_rep_begin_root_object()
 This macro has been replaced with oc_rep_begin_root_object. More...
 

Typedefs

typedef enum oc_rep_encoder_type_t oc_rep_encoder_type_t
 
typedef struct oc_rep_s oc_rep_t
 
typedef union oc_rep_value oc_rep_value_t
 

Enumerations

enum  oc_rep_encoder_type_t { OC_REP_CBOR_ENCODER = 0 , OC_REP_CRC_ENCODER = 2 }
 
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_ARRAY = 0x08 ,
  OC_REP_INT_ARRAY = 0x09 , OC_REP_DOUBLE_ARRAY = 0x0A , OC_REP_BOOL_ARRAY = 0x0B , OC_REP_BYTE_STRING_ARRAY = 0x0C ,
  OC_REP_STRING_ARRAY = 0x0D , OC_REP_OBJECT_ARRAY = 0x0E
}
 

Functions

CborError oc_rep_encode_boolean (CborEncoder *encoder, bool value)
 Encode a boolean value. More...
 
CborError oc_rep_encode_byte_string (CborEncoder *encoder, const uint8_t *string, size_t length)
 Encode a byte string. More...
 
CborError oc_rep_encode_double (CborEncoder *encoder, double value)
 Encode a double value. More...
 
CborError oc_rep_encode_floating_point (CborEncoder *encoder, CborType fpType, const void *value)
 Encode floating point value. More...
 
CborError oc_rep_encode_int (CborEncoder *encoder, int64_t value)
 Encode a signed integer value. More...
 
CborError oc_rep_encode_null (CborEncoder *encoder)
 Encode a NULL value. More...
 
void oc_rep_encode_raw (const uint8_t *data, size_t len)
 Encode raw data to the global encoder, as if it was already encoded. More...
 
CborError oc_rep_encode_text_string (CborEncoder *encoder, const char *string, size_t length)
 Encode a C-string. More...
 
CborError oc_rep_encode_uint (CborEncoder *encoder, uint64_t value)
 Encode an unsigned integer value. More...
 
CborError oc_rep_encoder_close_container (CborEncoder *encoder, CborEncoder *containerEncoder)
 Encode the ending of a container (an array or a map). More...
 
CborError oc_rep_encoder_create_array (CborEncoder *encoder, CborEncoder *arrayEncoder, size_t length)
 Encode the beginning of an array. More...
 
CborError oc_rep_encoder_create_map (CborEncoder *encoder, CborEncoder *mapEncoder, size_t length)
 Encode the beginning of a map. More...
 
oc_rep_encoder_type_t oc_rep_encoder_get_type (void)
 Get the encoder type used to encode the response payloads. More...
 
void oc_rep_encoder_set_type (oc_rep_encoder_type_t type)
 Set the global encoder used to encode the response payloads. More...
 
bool oc_rep_get_bool (const 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 (const oc_rep_t *rep, const char *key, bool **value, size_t *size)
 Read an boolean array value from an oc_rep_t More...
 
const oc_rep_t * oc_rep_get_by_type_and_key (const oc_rep_t *rep, oc_rep_value_type_t type, const char *key, size_t key_len)
 Get the value of a property by type and key. More...
 
bool oc_rep_get_byte_string (const 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 (const oc_rep_t *rep, const char *key, oc_byte_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 (const 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 (const 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...
 
CborEncoder * oc_rep_get_encoder (void) 1
 Get the global cbor encoder. More...
 
const uint8_t * oc_rep_get_encoder_buf (void)
 Get the buffer pointer at the start of the encoded cbor data. More...
 
int oc_rep_get_encoder_buffer_size (void)
 Get the size of the encoder buffer. More...
 
bool oc_rep_get_int (const 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 (const 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_object (const 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 (const 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 (const 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 (const 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_is_null (const oc_rep_t *rep, const char *key, bool *is_null)
 Check for a null value from an oc_rep_t More...
 
void oc_rep_new (uint8_t *payload, int size)
 Initialize the buffer used to hold the cbor encoded data without reallocation. More...
 
void oc_rep_new_realloc (uint8_t **payload, int size, int max_size)
 Initialize the buffer used to hold the cbor encoded data with reallocation. More...
 
void oc_rep_new_realloc_v1 (uint8_t **payload, size_t size, size_t max_size)
 Initialize the buffer used to hold the cbor encoded data with reallocation. More...
 
void oc_rep_new_v1 (uint8_t *payload, size_t size)
 Initialize the buffer used to hold the cbor encoded data without reallocation. More...
 
CborError oc_rep_object_set_boolean (CborEncoder *object, const char *key, size_t key_len, bool value)
 Add an boolean value to the cbor object under the key name. More...
 
CborError oc_rep_object_set_byte_string (CborEncoder *object, const char *key, size_t key_len, const uint8_t *value, size_t length)
 Add an byte array value to the cbor object under the key name. More...
 
CborError oc_rep_object_set_double (CborEncoder *object, const char *key, size_t key_len, double value)
 Add a double value to the cbor object under the key name. More...
 
CborError oc_rep_object_set_int (CborEncoder *object, const char *key, size_t key_len, int64_t value)
 Add an integer value to the cbor object under the key name. More...
 
CborError oc_rep_object_set_null (CborEncoder *object, const char *key, size_t key_len)
 Add an integer value to the cbor object under the key name. More...
 
CborError oc_rep_object_set_string_array (CborEncoder *object, const char *key, size_t key_len, const oc_string_array_t *array)
 Add a string array using an oc_string_array_t as values to the cbor object under the key name. More...
 
CborError oc_rep_object_set_text_string (CborEncoder *object, const char *key, size_t key_len, const char *value, size_t length)
 Add an string value to the cbor object under the key name. More...
 
CborError oc_rep_object_set_uint (CborEncoder *object, const char *key, size_t key_len, uint64_t value)
 Add an unsigned integer value to the cbor object under the key name. More...
 
uint8_t * oc_rep_shrink_encoder_buf (uint8_t *buf)
 Shrink the buffer pointer to length of encoded cbor data. More...
 
size_t oc_rep_to_json (const oc_rep_t *rep, char *buf, size_t buf_size, bool pretty_print)
 Convert an oc_rep_t to JSON encoded string. More...
 

Variables

int g_err
 
CborEncoder links_array
 
CborEncoder root_map
 

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.

◆ oc_rep_add_boolean

#define oc_rep_add_boolean (   parent,
  value 
)     g_err |= oc_rep_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:810
#define oc_rep_start_root_object()
This macro has been replaced with oc_rep_begin_root_object.
Definition: oc_rep.h:583
#define oc_rep_close_array(parent, key)
Close the array object.
Definition: oc_rep.h:870
#define oc_rep_end_root_object()
End the root object.
Definition: oc_rep.h:600
#define oc_rep_open_array(parent, key)
Open a cbor array object belonging to parent object under the key name.
Definition: oc_rep.h:860
See also
oc_rep_open_array
oc_rep_close_array
oc_rep_set_bool_array

◆ oc_rep_add_byte_string

#define oc_rep_add_byte_string (   parent,
  value,
  value_len 
)     g_err |= oc_rep_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:636
See also
oc_rep_open_array
oc_rep_close_array

◆ oc_rep_add_double

#define oc_rep_add_double (   parent,
  value 
)     g_err |= oc_rep_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:735
See also
oc_rep_open_array
oc_rep_close_array
oc_rep_set_double_array

◆ oc_rep_add_int

#define oc_rep_add_int (   parent,
  value 
)     g_err |= oc_rep_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:773
See also
oc_rep_open_array
oc_rep_close_array
oc_rep_set_int_array

◆ oc_rep_add_text_string

#define oc_rep_add_text_string (   parent,
  value 
)     oc_rep_add_text_string_v1(parent, value, (value) != NULL ? strlen(value) : 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:689
See also
oc_rep_open_array
oc_rep_close_array

◆ oc_rep_add_text_string_v1

#define oc_rep_add_text_string_v1 (   parent,
  value,
  value_len 
)
Value:
&parent##_array, (value) != NULL ? (value) : "", (value_len))
CborError oc_rep_encode_text_string(CborEncoder *encoder, const char *string, size_t length)
Encode a C-string.

Alternative to oc_rep_add_text_string in case we know the length of the value.

◆ 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

◆ oc_rep_begin_array

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

This macro is unlikely to be used by outside the IoTivity-lite 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:284
#define oc_rep_end_array(parent, name)
End the array object.
Definition: oc_rep.h:563
#define oc_rep_set_key(parent, key)
End users are very unlikely to use this macro.
Definition: oc_rep.h:836
#define oc_rep_begin_array(parent, name)
This macro is unlikely to be used by outside the IoTivity-lite library.
Definition: oc_rep.h:548

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_int
oc_rep_add_boolean
oc_rep_open_array
oc_rep_close_array

◆ oc_rep_begin_links_array

#define oc_rep_begin_links_array ( )
Value:
g_err |= oc_rep_encoder_create_array(oc_rep_get_encoder(), &links_array, \
CborIndefiniteLength)
CborError oc_rep_encoder_create_array(CborEncoder *encoder, CborEncoder *arrayEncoder, size_t length)
Encode the beginning of an array.
CborEncoder * oc_rep_get_encoder(void) 1
Get the global cbor encoder.

◆ oc_rep_begin_object

#define oc_rep_begin_object (   parent,
  key 
)
Value:
do { \
CborEncoder key##_map; \
memset(&key##_map, 0, sizeof(key##_map)); \
g_err |= oc_rep_encoder_create_map(parent, &key##_map, CborIndefiniteLength)
CborError oc_rep_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder, size_t length)
Encode the beginning of a map.

◆ oc_rep_begin_root_object

#define oc_rep_begin_root_object ( )
Value:
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

◆ 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

◆ 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

◆ oc_rep_end_array

#define oc_rep_end_array (   parent,
  name 
)
Value:
g_err |= oc_rep_encoder_close_container(parent, &name##_array); \
} \
while (0)
CborError oc_rep_encoder_close_container(CborEncoder *encoder, CborEncoder *containerEncoder)
Encode the ending of a container (an array or a map).

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

◆ oc_rep_end_object

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

◆ oc_rep_end_root_object

#define oc_rep_end_root_object ( )     g_err |= oc_rep_encoder_close_container(oc_rep_get_encoder(), &root_map)

End the root object.

Items can no longer be added to the root object.

See also
oc_rep_begin_root_object

◆ 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

◆ 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_set_text_string(object, key, value)
Add an string value to the cbor object under the key name Example:
Definition: oc_rep.h:443
#define oc_rep_object_array_begin_item(key)
Begin a cbor object for an array of cbor objects.
Definition: oc_rep.h:941
#define oc_rep_set_array(object, key)
This macro has been replaced with oc_rep_open_array.
Definition: oc_rep.h:845
#define oc_rep_object_array_end_item(key)
End the cbor object for the key array of cbor objects.
Definition: oc_rep.h:947
See also
oc_rep_object_array_end_item

◆ 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.

◆ 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

◆ oc_rep_open_array

#define oc_rep_open_array (   parent,
  key 
)
Value:
g_err |= oc_rep_encode_text_string(&parent##_map, #key, sizeof(#key) - 1); \
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 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

◆ oc_rep_open_object

#define oc_rep_open_object (   parent,
  key 
)
Value:
g_err |= oc_rep_encode_text_string(&parent##_map, #key, sizeof(#key) - 1); \
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:996
#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:416
#define oc_rep_set_object(object, key)
This macro has been replaced with oc_rep_open_object.
Definition: oc_rep.h:955
#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:370
See also
oc_rep_close_object

◆ 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

◆ oc_rep_set_bool_array

#define oc_rep_set_bool_array (   object,
  key,
  values,
  length 
)
Value:
do { \
g_err |= oc_rep_encode_text_string(&object##_map, #key, sizeof(#key) - 1); \
CborEncoder key##_value_array; \
memset(&key##_value_array, 0, sizeof(key##_value_array)); \
g_err |= \
oc_rep_encoder_create_array(&object##_map, &key##_value_array, length); \
for (size_t i = 0; i < (length); i++) { \
g_err |= oc_rep_encode_boolean(&key##_value_array, (values)[i]); \
} \
g_err |= \
oc_rep_encoder_close_container(&object##_map, &key##_value_array); \
} while (0)
CborError oc_rep_encode_boolean(CborEncoder *encoder, bool value)
Encode a boolean value.

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:1061

◆ oc_rep_set_boolean

#define oc_rep_set_boolean (   object,
  key,
  value 
)
Value:
g_err |= \
oc_rep_object_set_boolean(&object##_map, #key, sizeof(#key) - 1, value)

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:

oc_rep_set_boolean(root, door_open, false);
#define oc_rep_begin_root_object()
Begin the root object.
Definition: oc_rep.h:591
See also
oc_rep_get_bool

◆ oc_rep_set_byte_string

#define oc_rep_set_byte_string (   object,
  key,
  value,
  length 
)
Value:
g_err |= oc_rep_object_set_byte_string(&object##_map, #key, \
sizeof(#key) - 1, value, length)
CborError oc_rep_object_set_byte_string(CborEncoder *object, const char *key, size_t key_len, const uint8_t *value, size_t length)
Add an byte array value to the cbor object under the key name.

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));
#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:468

◆ oc_rep_set_double

#define oc_rep_set_double (   object,
  key,
  value 
)
Value:
g_err |= \
oc_rep_object_set_double(&object##_map, #key, sizeof(#key) - 1, value)

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:347

◆ oc_rep_set_double_array

#define oc_rep_set_double_array (   object,
  key,
  values,
  length 
)
Value:
do { \
g_err |= oc_rep_encode_text_string(&object##_map, #key, sizeof(#key) - 1); \
CborEncoder key##_value_array; \
memset(&key##_value_array, 0, sizeof(key##_value_array)); \
g_err |= \
oc_rep_encoder_create_array(&object##_map, &key##_value_array, length); \
for (size_t i = 0; i < (length); i++) { \
g_err |= oc_rep_encode_double(&key##_value_array, (values)[i]); \
} \
g_err |= \
oc_rep_encoder_close_container(&object##_map, &key##_value_array); \
} while (0)
CborError oc_rep_encode_double(CborEncoder *encoder, double value)
Encode a double value.

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:1100

◆ oc_rep_set_int

#define oc_rep_set_int (   object,
  key,
  value 
)     g_err |= oc_rep_object_set_int(&object##_map, #key, sizeof(#key) - 1, value)

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

◆ oc_rep_set_int_array

#define oc_rep_set_int_array (   object,
  key,
  values,
  length 
)
Value:
do { \
g_err |= oc_rep_encode_text_string(&object##_map, #key, sizeof(#key) - 1); \
CborEncoder key##_value_array; \
memset(&key##_value_array, 0, sizeof(key##_value_array)); \
g_err |= \
oc_rep_encoder_create_array(&object##_map, &key##_value_array, length); \
int i; \
for (i = 0; i < (length); i++) { \
g_err |= oc_rep_encode_int(&key##_value_array, (values)[i]); \
} \
g_err |= \
oc_rep_encoder_close_container(&object##_map, &key##_value_array); \
} while (0)
CborError oc_rep_encode_int(CborEncoder *encoder, int64_t value)
Encode a signed integer value.

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:1022

◆ oc_rep_set_key

#define oc_rep_set_key (   parent,
  key 
)     oc_rep_set_key_v1(parent, key, (key) != NULL ? strlen(key) : 0)

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

◆ oc_rep_set_key_v1

#define oc_rep_set_key_v1 (   parent,
  key,
  key_len 
)
Value:
do { \
if ((const char *)(key) != NULL) { \
g_err |= oc_rep_encode_text_string((parent), (key), (key_len)); \
} \
} while (0)

Alternative to oc_rep_set_key in case we know the length of the key.

◆ oc_rep_set_null

#define oc_rep_set_null (   object,
  key 
)     g_err |= oc_rep_object_set_null(&object##_map, #key, sizeof(#key) - 1)

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

To build the an object with the following cbor value

{
  "nothing": null
}

The following code could be used:

oc_rep_set_null(root, nothing);
#define oc_rep_set_null(object, key)
Add an null value to the cbor object under the key name Example:
Definition: oc_rep.h:491
See also
oc_rep_is_null

◆ 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

◆ oc_rep_set_string_array

#define oc_rep_set_string_array (   object,
  key,
  values 
)
Value:
g_err |= oc_rep_object_set_string_array(&object##_map, #key, \
sizeof(#key) - 1, &(values))
CborError oc_rep_object_set_string_array(CborEncoder *object, const char *key, size_t key_len, const oc_string_array_t *array)
Add a string array using an oc_string_array_t as values to the cbor object under the key name.

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_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 objec
oc_rep_set_string_array(root, quotes, quotes);
#define oc_free_string_array(ocstringarray)
free oc string array
Definition: oc_helpers.h:152
#define oc_new_string_array(ocstringarray, size)
new oc string array
Definition: oc_helpers.h:145
#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:1160
An allocation block.
Definition: oc_mmem.h:59
See also
oc_string_array_t
oc_new_string_array
oc_free_string_array
oc_string_array_add_item

◆ oc_rep_set_text_string

#define oc_rep_set_text_string (   object,
  key,
  value 
)
Value:
oc_rep_set_text_string_v1(object, key, value, \
(value) != NULL ? strlen(value) : 0)
#define oc_rep_set_text_string_v1(object, key, value, value_len)
Alternative to oc_rep_set_text_string in case we know the length of the value.
Definition: oc_rep.h:422

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:

◆ oc_rep_set_text_string_v1

#define oc_rep_set_text_string_v1 (   object,
  key,
  value,
  value_len 
)
Value:
g_err |= oc_rep_object_set_text_string(&object##_map, #key, \
sizeof(#key) - 1, value, value_len)
CborError oc_rep_object_set_text_string(CborEncoder *object, const char *key, size_t key_len, const char *value, size_t length)
Add an string value to the cbor object under the key name.

Alternative to oc_rep_set_text_string in case we know the length of the value.

◆ oc_rep_set_uint

#define oc_rep_set_uint (   object,
  key,
  value 
)     g_err |= oc_rep_object_set_uint(&object##_map, #key, sizeof(#key) - 1, value)

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:

oc_rep_set_uint(root, power, 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:394

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.

◆ oc_rep_set_value_text_string_v1

#define oc_rep_set_value_text_string_v1 (   parent,
  value,
  value_len 
)
Value:
&parent##_map, (value) != NULL ? (value) : "", (value_len))

Alternative to oc_rep_set_value_text_string in case we know the length of the value.

◆ 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

◆ 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

◆ oc_rep_start_root_object

#define oc_rep_start_root_object ( )    oc_rep_begin_root_object()

This macro has been replaced with oc_rep_begin_root_object.

See also
oc_rep_begin_root_object
oc_rep_end_root_object

Function Documentation

◆ oc_rep_encode_boolean()

CborError oc_rep_encode_boolean ( CborEncoder *  encoder,
bool  value 
)

Encode a boolean value.

Parameters
encoderInternal Iotivity-lite encoder to store the value
valuevalue to encode
Returns
CborError encoding error

◆ oc_rep_encode_byte_string()

CborError oc_rep_encode_byte_string ( CborEncoder *  encoder,
const uint8_t *  string,
size_t  length 
)

Encode a byte string.

Parameters
encoderInternal Iotivity-lite encoder to store the value
stringbyte string to encode
lengthlength of the byte string
Returns
CborError encoding error

◆ oc_rep_encode_double()

CborError oc_rep_encode_double ( CborEncoder *  encoder,
double  value 
)

Encode a double value.

Parameters
encoderInternal Iotivity-lite encoder to store the value
valuevalue to encode
Returns
CborError encoding error

◆ oc_rep_encode_floating_point()

CborError oc_rep_encode_floating_point ( CborEncoder *  encoder,
CborType  fpType,
const void *  value 
)

Encode floating point value.

Parameters
encoderInternal Iotivity-lite encoder to store the value
fpTypefloating point type (CborHalfFloatType, CborFloatType or CborDoubleType)
valuevalue to encode
Returns
CborError encoding error

◆ oc_rep_encode_int()

CborError oc_rep_encode_int ( CborEncoder *  encoder,
int64_t  value 
)

Encode a signed integer value.

Parameters
encoderInternal Iotivity-lite encoder to store the value
valuevalue to encode
Returns
CborError encoding error

◆ oc_rep_encode_null()

CborError oc_rep_encode_null ( CborEncoder *  encoder)

Encode a NULL value.

Parameters
encoderInternal Iotivity-lite encoder to store the value
Returns
CborError encoding error

◆ oc_rep_encode_raw()

void oc_rep_encode_raw ( const uint8_t *  data,
size_t  len 
)

Encode raw data to the global encoder, as if it was already encoded.

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

◆ oc_rep_encode_text_string()

CborError oc_rep_encode_text_string ( CborEncoder *  encoder,
const char *  string,
size_t  length 
)

Encode a C-string.

Parameters
encoderInternal Iotivity-lite encoder to store the value
stringC-string to encode
lengthlength of the C-string
Returns
CborError encoding error

◆ oc_rep_encode_uint()

CborError oc_rep_encode_uint ( CborEncoder *  encoder,
uint64_t  value 
)

Encode an unsigned integer value.

Parameters
encoderInternal Iotivity-lite encoder to store the value
valuevalue to encode
Returns
CborError encoding error

◆ oc_rep_encoder_close_container()

CborError oc_rep_encoder_close_container ( CborEncoder *  encoder,
CborEncoder *  containerEncoder 
)

Encode the ending of a container (an array or a map).

Parameters
encoderParent encoder
containerEncoderContainer encoder to be closed (must have been created either by oc_rep_encoder_create_array or by oc_rep_encoder_create_map).
Returns
CborError encoding error
See also
oc_rep_encoder_create_array
oc_rep_encoder_create_map

◆ oc_rep_encoder_create_array()

CborError oc_rep_encoder_create_array ( CborEncoder *  encoder,
CborEncoder *  arrayEncoder,
size_t  length 
)

Encode the beginning of an array.

Parameters
[in]encoderParent encoder to store the array.
[out]arrayEncoderEncoder for the created array.
[in]lengthNumber of items in the array (use CborIndefiniteLength if it is unknown)
Returns
CborError encoding error
Note
Must be closed by oc_rep_encoder_close_container

◆ oc_rep_encoder_create_map()

CborError oc_rep_encoder_create_map ( CborEncoder *  encoder,
CborEncoder *  mapEncoder,
size_t  length 
)

Encode the beginning of a map.

Parameters
[in]encoderParent encoder to store the map.
[out]mapEncoderEncoder for the created map.
[in]lengthNumber of items in the map (use CborIndefiniteLength if it is unknown)
Returns
CborError encoding error
Note
Must be closed by oc_rep_encoder_close_container

◆ oc_rep_encoder_get_type()

oc_rep_encoder_type_t oc_rep_encoder_get_type ( void  )

Get the encoder type used to encode the response payloads.

Returns
encoder type

◆ oc_rep_encoder_set_type()

void oc_rep_encoder_set_type ( oc_rep_encoder_type_t  type)

Set the global encoder used to encode the response payloads.

Parameters
typeencoder type

◆ oc_rep_get_bool()

bool oc_rep_get_bool ( const 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(const 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 ( const 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(const 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_by_type_and_key()

const oc_rep_t* oc_rep_get_by_type_and_key ( const oc_rep_t *  rep,
oc_rep_value_type_t  type,
const char *  key,
size_t  key_len 
)

Get the value of a property by type and key.

Parameters
repoc_rep_t to read the value from
typethe type of the value to read
keythe key for the value
key_lenthe length of key
Returns
the value of the property if found
NULL otherwise

◆ oc_rep_get_byte_string()

bool oc_rep_get_byte_string ( const 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 (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(const 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 ( const oc_rep_t *  rep,
const char *  key,
oc_byte_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;
if (oc_rep_get_byte_string_array(rep, "barray", &barray_out,
&barray_len)) {
for (size_t i = 0; i < barray_len)
; i++)
{
const 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(const oc_rep_t *rep, const char *key, oc_byte_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 double 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 ( const 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 the value for 'pi' is : %f\n", pi_out);
}
bool oc_rep_get_double(const 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 ( const 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(const 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()

CborEncoder* oc_rep_get_encoder ( void  )

Get the global cbor encoder.

Returns
global cbor encoder

◆ 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 IoTivity-lite library.

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

◆ oc_rep_get_encoder_buffer_size()

int oc_rep_get_encoder_buffer_size ( void  )

Get the size of the encoder buffer.

Returns
  • the size of the encoder buffer.
See also
oc_rep_new_realloc

◆ oc_rep_get_int()

bool oc_rep_get_int ( const 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(const 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 ( const 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(const 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_object()

bool oc_rep_get_object ( const 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_object(const oc_rep_t *rep, const char *key, oc_rep_t **value)
Read a object value from an oc_rep_t
bool oc_rep_get_string(const oc_rep_t *rep, const char *key, char **value, size_t *size)
Read a text string 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 ( const 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(const 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 ( const 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 ( const 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(const 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 double 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_is_null()

bool oc_rep_is_null ( const oc_rep_t *  rep,
const char *  key,
bool *  is_null 
)

Check for a null value from an oc_rep_t

Example:

bool is_null = false;
if( true == oc_rep_is_null(rep, "nothing", &is_null)) {
printf("Nothing is: %s\n", is_null ? "null": "not null");
}
bool oc_rep_is_null(const oc_rep_t *rep, const char *key, bool *is_null)
Check for a null value from an oc_rep_t
Parameters
repoc_rep_t to check for null value
keythe key name for the null value
is_nullthe return null check value
Returns
true if key and value are found and returned.
See also
oc_rep_set_null

◆ oc_rep_new()

void oc_rep_new ( uint8_t *  payload,
int  size 
)

Initialize the buffer used to hold the cbor encoded data without reallocation.

Deprecated:
replaced by oc_rep_new_v1 in v2.2.5.6

◆ oc_rep_new_realloc()

void oc_rep_new_realloc ( uint8_t **  payload,
int  size,
int  max_size 
)

Initialize the buffer used to hold the cbor encoded data with reallocation.

Deprecated:
replaced by oc_rep_new_realloc_v1 in v2.2.5.6

◆ oc_rep_new_realloc_v1()

void oc_rep_new_realloc_v1 ( uint8_t **  payload,
size_t  size,
size_t  max_size 
)

Initialize the buffer used to hold the cbor encoded data with reallocation.

Unlikely to be used by outside the IoTivity-lite library.

Parameters
[in]payloaddouble pointer to payload buffer
[in]sizesize of the payload buffer
[in]max_sizemaximum size the encoder buffer

◆ oc_rep_new_v1()

void oc_rep_new_v1 ( uint8_t *  payload,
size_t  size 
)

Initialize the buffer used to hold the cbor encoded data without reallocation.

Unlikely to be used by outside the IoTivity-lite library.

Parameters
[in]payloadpointer to payload buffer
[in]sizesize of the payload buffer

◆ oc_rep_object_set_boolean()

CborError oc_rep_object_set_boolean ( CborEncoder *  object,
const char *  key,
size_t  key_len,
bool  value 
)

Add an boolean value to the cbor object under the key name.

◆ oc_rep_object_set_byte_string()

CborError oc_rep_object_set_byte_string ( CborEncoder *  object,
const char *  key,
size_t  key_len,
const uint8_t *  value,
size_t  length 
)

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

◆ oc_rep_object_set_double()

CborError oc_rep_object_set_double ( CborEncoder *  object,
const char *  key,
size_t  key_len,
double  value 
)

Add a double value to the cbor object under the key name.

◆ oc_rep_object_set_int()

CborError oc_rep_object_set_int ( CborEncoder *  object,
const char *  key,
size_t  key_len,
int64_t  value 
)

Add an integer value to the cbor object under the key name.

◆ oc_rep_object_set_null()

CborError oc_rep_object_set_null ( CborEncoder *  object,
const char *  key,
size_t  key_len 
)

Add an integer value to the cbor object under the key name.

◆ oc_rep_object_set_string_array()

CborError oc_rep_object_set_string_array ( CborEncoder *  object,
const char *  key,
size_t  key_len,
const oc_string_array_t array 
)

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

◆ oc_rep_object_set_text_string()

CborError oc_rep_object_set_text_string ( CborEncoder *  object,
const char *  key,
size_t  key_len,
const char *  value,
size_t  length 
)

Add an string value to the cbor object under the key name.

◆ oc_rep_object_set_uint()

CborError oc_rep_object_set_uint ( CborEncoder *  object,
const char *  key,
size_t  key_len,
uint64_t  value 
)

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

◆ oc_rep_shrink_encoder_buf()

uint8_t* oc_rep_shrink_encoder_buf ( uint8_t *  buf)

Shrink the buffer pointer to length of 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 IoTivity-lite library.

Parameters
[in]bufpointer to cbor encoded buffer
Returns
pointer to the start of the shrinked cbor encoded buffer
See also
oc_parse_rep

◆ oc_rep_to_json()

size_t oc_rep_to_json ( const 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.

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(const 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).