KPN Things Device SDK
cbor.cpp File Reference
#include "cbor.h"
#include "../senml/senml_helpers.h"
#include <string.h>

Macros

#define htons(x)   ( ((x)<<8 & 0xFF00) | ((x)>>8 & 0x00FF) )
 
#define htonl(x)
 
#define htonll(x)   ((((uint64_t)htonl(x)) << 32) + htonl((x) >> 32))
 
#define ntohl(x)
 

Functions

float binary_to_float (uint32_t x)
 
uint32_t float_to_binary (float x)
 
double binary_to_double (uint64_t x)
 
uint16_t half_float_to_binary (float val)
 
float binary_to_half_float (uint16_t val)
 
float ntohd_avr (uint64_t x)
 
bool is_lossless_to_half_float (double value)
 
bool is_lossless_to_float (double value)
 
double decode_float_half (unsigned char *halfp)
 
size_t decode_int (uint64_t *val)
 
size_t cbor_serialize_array (size_t array_length)
 Serialize array of length array_length. More...
 
size_t cbor_serialize_map (size_t map_length)
 Serialize map of length map_length. More...
 
size_t cbor_serialize_int (int val)
 Serializes an integer. More...
 
size_t cbor_serialize_unicode_string (const char *const val)
 Serializes a unicode string. More...
 
size_t cbor_serialize_half_float (float val)
 Serializes a half precision floating point value. More...
 
size_t cbor_serialize_float (float val)
 Serializes a single precision floating point value. More...
 
size_t cbor_serialize_double (double val)
 Serializes a double precision floating point value. More...
 
size_t cbor_deserialize_float_half (float *val)
 
size_t cbor_deserialize_float (float *val)
 
size_t cbor_deserialize_double (double *val)
 
size_t cbor_deserialize_int64_t (int64_t *val)
 Deserialize signed 64 bit values from stream to val. More...
 
size_t cbor_deserialize_uint64_t (uint64_t *val)
 Deserialize unsigned 64 bit values from stream to val. More...
 
size_t cbor_serialize_bool (bool val)
 Serializes a boolean value. More...
 
size_t cbor_serialize_byte_string (const char *val, int length)
 Serializes a byte string. More...
 

Macro Definition Documentation

◆ htonl

#define htonl (   x)
Value:
( ((x)<<24 & 0xFF000000UL) | \
((x)<< 8 & 0x00FF0000UL) | \
((x)>> 8 & 0x0000FF00UL) | \
((x)>>24 & 0x000000FFUL) )

◆ htonll

#define htonll (   x)    ((((uint64_t)htonl(x)) << 32) + htonl((x) >> 32))

◆ htons

#define htons (   x)    ( ((x)<<8 & 0xFF00) | ((x)>>8 & 0x00FF) )

◆ ntohl

#define ntohl (   x)
Value:
( ((x) & 0xFF000000UL) >> 24 | \
((x) & 0x00FF0000UL) >> 8 | \
((x) & 0x0000FF00UL) << 8 | \
((x) & 0x000000FFUL) << 24 )

Function Documentation

◆ binary_to_double()

double binary_to_double ( uint64_t  x)
inline

Convert double x to host format

◆ binary_to_float()

float binary_to_float ( uint32_t  x)
inline

Convert float x to host format

◆ binary_to_half_float()

float binary_to_half_float ( uint16_t  val)
inline

◆ cbor_deserialize_double()

size_t cbor_deserialize_double ( double *  val)

◆ cbor_deserialize_float()

size_t cbor_deserialize_float ( float *  val)

◆ cbor_deserialize_float_half()

size_t cbor_deserialize_float_half ( float *  val)

◆ cbor_deserialize_int64_t()

size_t cbor_deserialize_int64_t ( int64_t *  val)

Deserialize signed 64 bit values from stream to val.

Parameters
[out]valPointer to destination array
Returns
Number of bytes read from stream

◆ cbor_deserialize_uint64_t()

size_t cbor_deserialize_uint64_t ( uint64_t *  val)

Deserialize unsigned 64 bit values from stream to val.

Parameters
[out]valPointer to destination array
Returns
Number of bytes read from stream

◆ cbor_serialize_array()

size_t cbor_serialize_array ( size_t  array_length)

Serialize array of length array_length.

Basic usage:

cbor_serialize_array(2); // array of length 2 follows
cbor_serialize_int(1)); // write item 1
cbor_serialize_int(2)); // write item 2
size_t cbor_serialize_int(int val)
Serializes an integer.
Definition: cbor.cpp:321
size_t cbor_serialize_array(size_t array_length)
Serialize array of length array_length.
Definition: cbor.cpp:309
Note
You have to make sure to serialize the correct amount of items. If you exceed the length array_length, items will just be appened as normal
Parameters
[in]array_lengthLength of the array of items which follows
Returns
Number of bytes written to stream stream

◆ cbor_serialize_bool()

size_t cbor_serialize_bool ( bool  val)

Serializes a boolean value.

Parameters
[in]valThe boolean value to serialize
Returns
Number of bytes written to stream stream

◆ cbor_serialize_byte_string()

size_t cbor_serialize_byte_string ( const char *  val,
int  length 
)

Serializes a byte string.

Parameters
[in]valThe byte string start pointer
[in]lengthThe length of the string
Returns
Number of bytes written to stream stream

◆ cbor_serialize_double()

size_t cbor_serialize_double ( double  val)

Serializes a double precision floating point value.

Parameters
[in]valThe double to serialize
Returns
Number of bytes written to stream stream

◆ cbor_serialize_float()

size_t cbor_serialize_float ( float  val)

Serializes a single precision floating point value.

Parameters
[in]valThe float to serialize
Returns
Number of bytes written to stream stream

◆ cbor_serialize_half_float()

size_t cbor_serialize_half_float ( float  val)

Serializes a half precision floating point value.

Parameters
[in]valThe float to serialize
Returns
Number of bytes written to stream stream

◆ cbor_serialize_int()

size_t cbor_serialize_int ( int  val)

Serializes an integer.

Parameters
[in]valThe integer to serialize
Returns
Number of bytes written to stream stream

◆ cbor_serialize_map()

size_t cbor_serialize_map ( size_t  map_length)

Serialize map of length map_length.

Basic usage:

cbor_serialize_map(2); // map of length 2 follows
cbor_serialize_int(1)); // write key 1
cbor_serialize_byte_string("1")); // write value 1
cbor_serialize_int(2)); // write key 2
cbor_serialize_byte_string("2")); // write value 2
size_t cbor_serialize_byte_string(const char *val, int length)
Serializes a byte string.
Definition: cbor.cpp:457
size_t cbor_serialize_map(size_t map_length)
Serialize map of length map_length.
Definition: cbor.cpp:315
Parameters
map_lengthLength of the map of items which follows
Returns
Number of bytes written to stream stream

◆ cbor_serialize_unicode_string()

size_t cbor_serialize_unicode_string ( const char *  val)

Serializes a unicode string.

                 string
Parameters
[out]valThe zero-terminated unicode string to serialize.
Returns
Number of bytes written to stream stream

◆ decode_float_half()

double decode_float_half ( unsigned char *  halfp)

Source: CBOR RFC reference implementation

◆ decode_int()

size_t decode_int ( uint64_t *  val)

◆ float_to_binary()

uint32_t float_to_binary ( float  x)
inline

◆ half_float_to_binary()

uint16_t half_float_to_binary ( float  val)
inline

◆ is_lossless_to_float()

bool is_lossless_to_float ( double  value)

◆ is_lossless_to_half_float()

bool is_lossless_to_half_float ( double  value)

◆ ntohd_avr()

float ntohd_avr ( uint64_t  x)
inline