IoTivity-Lite
|
#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 |
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 |
#define OC_PRETTY_PRINT_TAB_CHARACTER " " |
Tab character(s) used for oc_rep_to_json function when doing pretty_print.
#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:
#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:
#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:
#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:
#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:
#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.
#define oc_rep_array | ( | name | ) | &name##_array |
Get a pointer to the cbor array object with the given name
#define oc_rep_begin_array | ( | parent, | |
name | |||
) |
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:
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.
#define oc_rep_begin_links_array | ( | ) |
#define oc_rep_begin_object | ( | parent, | |
key | |||
) |
#define oc_rep_begin_root_object | ( | ) |
Begin the root object.
Items can be added to the root object till oc_rep_end_root_object is called
#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.
#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.
#define oc_rep_end_array | ( | parent, | |
name | |||
) |
End the array object.
No additional items can be added to the array after this is called.
#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.
Items can no longer be added to the root object.
#define oc_rep_object | ( | name | ) | &name##_map |
Get a pointer to the cbor object with the given name
#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:
#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.
#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.
#define oc_rep_open_array | ( | parent, | |
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.
#define oc_rep_open_object | ( | parent, | |
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:
#define oc_rep_set_array | ( | object, | |
key | |||
) | oc_rep_open_array(object, key) |
This macro has been replaced with oc_rep_open_array.
#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.
Example:
To build the an object with the following cbor value
{ "flip": [ false, false, true, false, false ] }
The following code could be used:
#define oc_rep_set_boolean | ( | object, | |
key, | |||
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:
#define oc_rep_set_byte_string | ( | object, | |
key, | |||
value, | |||
length | |||
) |
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:
#define oc_rep_set_double | ( | object, | |
key, | |||
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:
#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.
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:
#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:
#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.
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:
#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
#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.
#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:
#define oc_rep_set_object | ( | object, | |
key | |||
) | oc_rep_open_object(object, key) |
This macro has been replaced with oc_rep_open_object.
#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.
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:
#define oc_rep_set_text_string | ( | object, | |
key, | |||
value | |||
) |
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:
#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.
#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:
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.
#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.
#define oc_rep_start_array | ( | parent, | |
name | |||
) | oc_rep_begin_array(parent, name) |
This macro has been replaced with oc_rep_begin_array.
#define oc_rep_start_object | ( | parent, | |
key | |||
) | oc_rep_begin_object(parent, key) |
This macro has been replaced with oc_rep_begin_object.
#define oc_rep_start_root_object | ( | ) | oc_rep_begin_root_object() |
This macro has been replaced with oc_rep_begin_root_object.
CborError oc_rep_encode_boolean | ( | CborEncoder * | encoder, |
bool | value | ||
) |
Encode a boolean value.
encoder | Internal Iotivity-lite encoder to store the value |
value | value to encode |
CborError oc_rep_encode_byte_string | ( | CborEncoder * | encoder, |
const uint8_t * | string, | ||
size_t | length | ||
) |
Encode a byte string.
encoder | Internal Iotivity-lite encoder to store the value |
string | byte string to encode |
length | length of the byte string |
CborError oc_rep_encode_double | ( | CborEncoder * | encoder, |
double | value | ||
) |
Encode a double value.
encoder | Internal Iotivity-lite encoder to store the value |
value | value to encode |
CborError oc_rep_encode_floating_point | ( | CborEncoder * | encoder, |
CborType | fpType, | ||
const void * | value | ||
) |
Encode floating point value.
encoder | Internal Iotivity-lite encoder to store the value |
fpType | floating point type (CborHalfFloatType, CborFloatType or CborDoubleType) |
value | value to encode |
CborError oc_rep_encode_int | ( | CborEncoder * | encoder, |
int64_t | value | ||
) |
Encode a signed integer value.
encoder | Internal Iotivity-lite encoder to store the value |
value | value to encode |
CborError oc_rep_encode_null | ( | CborEncoder * | encoder | ) |
Encode a NULL value.
encoder | Internal Iotivity-lite encoder to store the value |
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.
data | Pointer to data to be encoded. Will be copied into the global buffer. |
len | Length of data. |
CborError oc_rep_encode_text_string | ( | CborEncoder * | encoder, |
const char * | string, | ||
size_t | length | ||
) |
Encode a C-string.
encoder | Internal Iotivity-lite encoder to store the value |
string | C-string to encode |
length | length of the C-string |
CborError oc_rep_encode_uint | ( | CborEncoder * | encoder, |
uint64_t | value | ||
) |
Encode an unsigned integer value.
encoder | Internal Iotivity-lite encoder to store the value |
value | value to encode |
CborError oc_rep_encoder_close_container | ( | CborEncoder * | encoder, |
CborEncoder * | containerEncoder | ||
) |
Encode the ending of a container (an array or a map).
encoder | Parent encoder |
containerEncoder | Container encoder to be closed (must have been created either by oc_rep_encoder_create_array or by oc_rep_encoder_create_map). |
CborError oc_rep_encoder_create_array | ( | CborEncoder * | encoder, |
CborEncoder * | arrayEncoder, | ||
size_t | length | ||
) |
Encode the beginning of an array.
[in] | encoder | Parent encoder to store the array. |
[out] | arrayEncoder | Encoder for the created array. |
[in] | length | Number of items in the array (use CborIndefiniteLength if it is unknown) |
CborError oc_rep_encoder_create_map | ( | CborEncoder * | encoder, |
CborEncoder * | mapEncoder, | ||
size_t | length | ||
) |
Encode the beginning of a map.
[in] | encoder | Parent encoder to store the map. |
[out] | mapEncoder | Encoder for the created map. |
[in] | length | Number of items in the map (use CborIndefiniteLength if it is unknown) |
oc_rep_encoder_type_t oc_rep_encoder_get_type | ( | void | ) |
Get the encoder type used to encode the response payloads.
void oc_rep_encoder_set_type | ( | oc_rep_encoder_type_t | type | ) |
Set the global encoder used to encode the response payloads.
type | encoder type |
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:
rep | oc_rep_t to read boolean value from |
key | the key name for the boolean value |
value | the return boolean value |
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:
rep | oc_rep_t to boolean array value from |
key | the key name for the boolean array value |
value | the return boolean array value |
size | the size of the boolean array |
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.
rep | oc_rep_t to read the value from |
type | the type of the value to read |
key | the key for the value |
key_len | the length of key |
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:
rep | oc_rep_t to read byte string value from |
key | the key name for the byte string value |
value | the return byte string value |
size | the size of the 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
Example:
rep | oc_rep_t to byte string array value from |
key | the key name for the byte string array value |
value | the return double array value |
size | the size of the byte string array |
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
.
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:
rep | oc_rep_t to read double value from |
key | the key name for the double value |
value | the return double value |
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:
rep | oc_rep_t to double array value from |
key | the key name for the double array value |
value | the return double array value |
size | the size of the double array |
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.
CborEncoder* oc_rep_get_encoder | ( | void | ) |
Get the global cbor encoder.
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.
int oc_rep_get_encoder_buffer_size | ( | void | ) |
Get the size of the encoder buffer.
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:
rep | oc_rep_t to read int value from |
key | the key name for the integer value |
value | the return integer value |
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:
rep | oc_rep_t to integer array value from |
key | the key name for the integer array value |
value | the return integer array value |
size | the size of the integer array |
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:
rep | oc_rep_t to read object value from |
key | the key name for the object value |
value | the return object value |
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:
rep | oc_rep_t to read object array value from |
key | the key name for the object array value |
value | the return object array value |
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:
rep | oc_rep_t to read string value from |
key | the key name for the string value |
value | the return string value |
size | the size of the string |
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:
rep | oc_rep_t to string array value from |
key | the key name for the string array value |
value | the return double array value |
size | the size of the string array |
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:
rep | oc_rep_t to check for null value |
key | the key name for the null value |
is_null | the return null check value |
void oc_rep_new | ( | uint8_t * | payload, |
int | size | ||
) |
Initialize the buffer used to hold the cbor encoded data without reallocation.
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.
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.
[in] | payload | double pointer to payload buffer |
[in] | size | size of the payload buffer |
[in] | max_size | maximum size the encoder buffer |
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.
[in] | payload | pointer to payload buffer |
[in] | size | size of the payload buffer |
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.
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.
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.
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.
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.
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.
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.
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.
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.
[in] | buf | pointer to cbor encoded buffer |
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.
[in] | rep | the oc_rep_t object to be converted to JSON |
[out] | buf | a char array that will hold the JSON encoded string. |
[in] | buf_size | the size of the passed in char array |
[in] | pretty_print | if 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: