Astarte Device SDK ESP32
ESP32 device SDK for the Astarte platform
astarte_bson_deserializer.h File Reference

Astarte BSON deserialization functions. More...

#include "astarte.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
Include dependency graph for astarte_bson_deserializer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  astarte_bson_document_t
 
struct  astarte_bson_element_t
 

Functions

bool astarte_bson_deserializer_check_validity (const void *buffer, int buffer_size)
 Perform some checks on the validity of the BSON. More...
 
astarte_bson_document_t astarte_bson_deserializer_init_doc (const void *buffer)
 Initialize a document type from a BSON data buffer. More...
 
astarte_err_t astarte_bson_deserializer_first_element (astarte_bson_document_t document, astarte_bson_element_t *element)
 Get the first element in a document's list. More...
 
astarte_err_t astarte_bson_deserializer_next_element (astarte_bson_document_t document, astarte_bson_element_t curr_element, astarte_bson_element_t *next_element)
 Get the next element in a list. More...
 
double astarte_bson_deserializer_element_to_double (astarte_bson_element_t element)
 Extract the value from the passed element. More...
 
const char * astarte_bson_deserializer_element_to_string (astarte_bson_element_t element, uint32_t *len)
 Extract the value from the passed element. More...
 
astarte_bson_document_t astarte_bson_deserializer_element_to_document (astarte_bson_element_t element)
 Extract the value from the passed element. More...
 
astarte_bson_document_t astarte_bson_deserializer_element_to_array (astarte_bson_element_t element)
 Extract the value from the passed element. More...
 
const uint8_t * astarte_bson_deserializer_element_to_binary (astarte_bson_element_t element, uint32_t *len)
 Extract the value from the passed element. More...
 
bool astarte_bson_deserializer_element_to_bool (astarte_bson_element_t element)
 Extract the value from the passed element. More...
 
int64_t astarte_bson_deserializer_element_to_datetime (astarte_bson_element_t element)
 Extract the value from the passed element. More...
 
int32_t astarte_bson_deserializer_element_to_int32 (astarte_bson_element_t element)
 Extract the value from the passed element. More...
 
int64_t astarte_bson_deserializer_element_to_int64 (astarte_bson_element_t element)
 Extract the value from the passed element. More...
 
astarte_err_t astarte_bson_deserializer_element_lookup (astarte_bson_document_t document, const char *key, astarte_bson_element_t *element)
 Fetch the element with name corresponding to the specified key from the document. More...
 

Detailed Description

Astarte BSON deserialization functions.

This library follows the v1.1 of the BSON standard, but does not provide support for the full specification, only for a smaller subset. For more information regarding the BSON format specifications see: https://bsonspec.org/spec.html.

Function Documentation

◆ astarte_bson_deserializer_check_validity()

bool astarte_bson_deserializer_check_validity ( const void *  buffer,
int  buffer_size 
)

Perform some checks on the validity of the BSON.

Note
This function performs a very rough validation check. It is quite possible a malformed bson file would still pass this check.
Parameters
[in]bufferBuffer containing the document to check.
[in]buffer_sizeSize of the allocated buffer containing the document.
Returns
True when BSON file is valid, false otherwise.

◆ astarte_bson_deserializer_element_lookup()

astarte_err_t astarte_bson_deserializer_element_lookup ( astarte_bson_document_t  document,
const char *  key,
astarte_bson_element_t element 
)

Fetch the element with name corresponding to the specified key from the document.

Parameters
[in]documentDocument to use for the search.
[in]keyElement name to use as key for the lookup.
[out]elementUsed to return the found element, should be a pointer to an uninitialized element.
Returns
ASTARTE_OK if successful, ASTARTE_ERR_NOT_FOUND if the element does not exist.

◆ astarte_bson_deserializer_element_to_array()

astarte_bson_document_t astarte_bson_deserializer_element_to_array ( astarte_bson_element_t  element)

Extract the value from the passed element.

Note
An array is encoded as a document, so a document type is returned.
Parameters
[in]elementElement to extract the value from.
Returns
Extracted value.

◆ astarte_bson_deserializer_element_to_binary()

const uint8_t* astarte_bson_deserializer_element_to_binary ( astarte_bson_element_t  element,
uint32_t *  len 
)

Extract the value from the passed element.

Parameters
[in]elementElement to extract the value from.
[out]lenReturned bytes array length. Optional, pass NULL if not used.
Returns
Extracted value.

◆ astarte_bson_deserializer_element_to_bool()

bool astarte_bson_deserializer_element_to_bool ( astarte_bson_element_t  element)

Extract the value from the passed element.

Note
The actual returned value is an uint8_t.
Parameters
[in]elementElement to extract the value from.
Returns
Extracted value.

◆ astarte_bson_deserializer_element_to_datetime()

int64_t astarte_bson_deserializer_element_to_datetime ( astarte_bson_element_t  element)

Extract the value from the passed element.

Note
UTC datetime is encoded as a timestamp in an int64_t.
Parameters
[in]elementElement to extract the value from.
Returns
Extracted value.

◆ astarte_bson_deserializer_element_to_document()

astarte_bson_document_t astarte_bson_deserializer_element_to_document ( astarte_bson_element_t  element)

Extract the value from the passed element.

Parameters
[in]elementElement to extract the value from.
Returns
Extracted value.

◆ astarte_bson_deserializer_element_to_double()

double astarte_bson_deserializer_element_to_double ( astarte_bson_element_t  element)

Extract the value from the passed element.

Parameters
[in]elementElement to extract the value from.
Returns
Extracted value.

◆ astarte_bson_deserializer_element_to_int32()

int32_t astarte_bson_deserializer_element_to_int32 ( astarte_bson_element_t  element)

Extract the value from the passed element.

Parameters
[in]elementElement to extract the value from.
Returns
Extracted value.

◆ astarte_bson_deserializer_element_to_int64()

int64_t astarte_bson_deserializer_element_to_int64 ( astarte_bson_element_t  element)

Extract the value from the passed element.

Parameters
[in]elementElement to extract the value from.
Returns
Extracted value.

◆ astarte_bson_deserializer_element_to_string()

const char* astarte_bson_deserializer_element_to_string ( astarte_bson_element_t  element,
uint32_t *  len 
)

Extract the value from the passed element.

Parameters
[in]elementElement to extract the value from.
[out]lenReturned string length. Optional, pass NULL if not used.
Returns
Extracted value.

◆ astarte_bson_deserializer_first_element()

astarte_err_t astarte_bson_deserializer_first_element ( astarte_bson_document_t  document,
astarte_bson_element_t element 
)

Get the first element in a document's list.

Parameters
[in]documentDocument from which to extract the element.
[out]elementUsed to store the extracted element.
Returns
ASTARTE_OK if successful, ASTARTE_ERR_NOT_FOUND if the document is empty.

◆ astarte_bson_deserializer_init_doc()

astarte_bson_document_t astarte_bson_deserializer_init_doc ( const void *  buffer)

Initialize a document type from a BSON data buffer.

Parameters
[in]bufferBuffer containing the BSON data.
Returns
Inizialized document struct.

◆ astarte_bson_deserializer_next_element()

astarte_err_t astarte_bson_deserializer_next_element ( astarte_bson_document_t  document,
astarte_bson_element_t  curr_element,
astarte_bson_element_t next_element 
)

Get the next element in a list.

Parameters
[in]documentDocument containing the list.
[in]curr_elementPointer to the current element.
[in]next_elementUsed to store the extracted element.
Returns
ASTARTE_OK if successful, ASTARTE_ERR_NOT_FOUND if no next element exists.