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

Astarte BSON serializer functions. More...

#include "astarte.h"
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
Include dependency graph for astarte_bson_serializer.h:

Go to the source code of this file.

Data Structures

struct  astarte_byte_array_t
 
struct  astarte_bson_serializer_t
 

Functions

void astarte_bson_serializer_init (struct astarte_bson_serializer_t *bs)
 initialize given BSON serializer. More...
 
void astarte_bson_serializer_destroy (struct astarte_bson_serializer_t *bs)
 destroy given BSON serializer. More...
 
const void * astarte_bson_serializer_get_document (const struct astarte_bson_serializer_t *bs, int *size)
 return bson serializer internal buffer. More...
 
astarte_err_t astarte_bson_serializer_write_document (const struct astarte_bson_serializer_t *bs, void *out_buf, int out_buf_len, int *out_doc_size)
 copy bson serializer internal buffer to a different buffer. More...
 
int astarte_bson_serializer_document_size (const struct astarte_bson_serializer_t *bs)
 return document size More...
 
void astarte_bson_serializer_append_end_of_document (struct astarte_bson_serializer_t *bs)
 append end of document marker. More...
 
void astarte_bson_serializer_append_double (struct astarte_bson_serializer_t *bs, const char *name, double value)
 append a double value More...
 
void astarte_bson_serializer_append_int32 (struct astarte_bson_serializer_t *bs, const char *name, int32_t value)
 append an int32 value More...
 
void astarte_bson_serializer_append_int64 (struct astarte_bson_serializer_t *bs, const char *name, int64_t value)
 append an int64 value More...
 
void astarte_bson_serializer_append_binary (struct astarte_bson_serializer_t *bs, const char *name, const void *value, int size)
 append a binary blob value More...
 
void astarte_bson_serializer_append_string (struct astarte_bson_serializer_t *bs, const char *name, const char *string)
 append an UTF-8 string More...
 
void astarte_bson_serializer_append_datetime (struct astarte_bson_serializer_t *bs, const char *name, uint64_t epoch_millis)
 append a date time value More...
 
void astarte_bson_serializer_append_boolean (struct astarte_bson_serializer_t *bs, const char *name, bool value)
 append a boolean value More...
 
void astarte_bson_serializer_append_document (struct astarte_bson_serializer_t *bs, const char *name, const void *document)
 append a sub-BSON document. More...
 
void astarte_bson_serializer_append_double_array (struct astarte_bson_serializer_t *bs, const char *name, const double *double_array, int count)
 append a double array More...
 
void astarte_bson_serializer_append_int32_array (struct astarte_bson_serializer_t *bs, const char *name, const int32_t *int32_array, int count)
 append an int32 array More...
 
void astarte_bson_serializer_append_int64_array (struct astarte_bson_serializer_t *bs, const char *name, const int64_t *int64_array, int count)
 append an int64 array More...
 
void astarte_bson_serializer_append_string_array (struct astarte_bson_serializer_t *bs, const char *name, const char *const *string_array, int count)
 append a string array More...
 
void astarte_bson_serializer_append_binary_array (struct astarte_bson_serializer_t *bs, const char *name, const void *const *binary_array, const int *sizes, int count)
 append a binary blob array More...
 
void astarte_bson_serializer_append_datetime_array (struct astarte_bson_serializer_t *bs, const char *name, const int64_t *epoch_millis_array, int count)
 append a date time array More...
 
void astarte_bson_serializer_append_boolean_array (struct astarte_bson_serializer_t *bs, const char *name, const bool *boolean_array, int count)
 append a boolean array More...
 

Detailed Description

Astarte BSON serializer functions.

Function Documentation

◆ astarte_bson_serializer_append_binary()

void astarte_bson_serializer_append_binary ( struct astarte_bson_serializer_t bs,
const char *  name,
const void *  value,
int  size 
)

append a binary blob value

This function appends a binary blob to the document. Stored value can be fetched using given key.

Parameters
bsa astarte_bson_serializer_t.
nameBSON key, which is a C string.
valuethe buffer that holds the binary blob.
binaryblob size in bytes.

◆ astarte_bson_serializer_append_binary_array()

void astarte_bson_serializer_append_binary_array ( struct astarte_bson_serializer_t bs,
const char *  name,
const void *const *  binary_array,
const int *  sizes,
int  count 
)

append a binary blob array

This function appends a binary blob array to the document. Stored value can be fetched using given key.

Parameters
bsa astarte_bson_serializer_t.
nameBSON key, which is a C string.
binarty_arrayan array of binary blobs (void *).
sizesan array with the sizes of each binary in binary_array parameter.
countthe number of items stored in binary_array.

◆ astarte_bson_serializer_append_boolean()

void astarte_bson_serializer_append_boolean ( struct astarte_bson_serializer_t bs,
const char *  name,
bool  value 
)

append a boolean value

This function appends a boolean value to the document. Stored value can be fetched using given key.

Parameters
bsa astarte_bson_serializer_t.
nameBSON key, which is a C string.
value0 as false value, not 0 as true value.

◆ astarte_bson_serializer_append_boolean_array()

void astarte_bson_serializer_append_boolean_array ( struct astarte_bson_serializer_t bs,
const char *  name,
const bool *  boolean_array,
int  count 
)

append a boolean array

This function appends a boolean array to the document. Stored value can be fetched using given key.

Parameters
bsa astarte_bson_serializer_t.
nameBSON key, which is a C string.
boolean_arrayan array of stdbool booleans.
countthe number of items stored in boolean_array.

◆ astarte_bson_serializer_append_datetime()

void astarte_bson_serializer_append_datetime ( struct astarte_bson_serializer_t bs,
const char *  name,
uint64_t  epoch_millis 
)

append a date time value

This function appends a date time value to the document. Stored value can be fetched using given key.

Parameters
bsa astarte_bson_serializer_t.
nameBSON key, which is a C string.
valuea 64 bits unsigned integer storing date time in milliseconds since epoch.

◆ astarte_bson_serializer_append_datetime_array()

void astarte_bson_serializer_append_datetime_array ( struct astarte_bson_serializer_t bs,
const char *  name,
const int64_t *  epoch_millis_array,
int  count 
)

append a date time array

This function appends a date time array to the document. Stored value can be fetched using given key.

Parameters
bsa astarte_bson_serializer_t.
nameBSON key, which is a C string.
epoch_millis_arrayan array of 64 bits unsigned integer storing date time in milliseconds since epoch.
countthe number of items stored in epoch_millis_array.

◆ astarte_bson_serializer_append_document()

void astarte_bson_serializer_append_document ( struct astarte_bson_serializer_t bs,
const char *  name,
const void *  document 
)

append a sub-BSON document.

This function appends a BSON subdocument to the document. Stored value can be fetched using given key.

Parameters
bsa astarte_bson_serializer_t.
nameBSON key, which is a C string.
documenta valid BSON document (that has been already terminated).

◆ astarte_bson_serializer_append_double()

void astarte_bson_serializer_append_double ( struct astarte_bson_serializer_t bs,
const char *  name,
double  value 
)

append a double value

This function appends a double value to the document. Stored value can be fetched using given key.

Parameters
bsa astarte_bson_serializer_t.
nameBSON key, which is a C string.
valuea double floating point value.

◆ astarte_bson_serializer_append_double_array()

void astarte_bson_serializer_append_double_array ( struct astarte_bson_serializer_t bs,
const char *  name,
const double *  double_array,
int  count 
)

append a double array

This function appends a double array to the document. Stored value can be fetched using given key.

Parameters
bsa astarte_bson_serializer_t.
nameBSON key, which is a C string.
double_arrayan array of doubles.
countthe number of items stored in double_array.

◆ astarte_bson_serializer_append_end_of_document()

void astarte_bson_serializer_append_end_of_document ( struct astarte_bson_serializer_t bs)

append end of document marker.

BSON document MUST be manually terminated with an end of document marker.

Parameters
bsa astarte_bson_serializer_t.

◆ astarte_bson_serializer_append_int32()

void astarte_bson_serializer_append_int32 ( struct astarte_bson_serializer_t bs,
const char *  name,
int32_t  value 
)

append an int32 value

This function appends an int32 value to the document. Stored value can be fetched using given key.

Parameters
bsa astarte_bson_serializer_t.
nameBSON key, which is a C string.
valuea 32 bits signed integer value.

◆ astarte_bson_serializer_append_int32_array()

void astarte_bson_serializer_append_int32_array ( struct astarte_bson_serializer_t bs,
const char *  name,
const int32_t *  int32_array,
int  count 
)

append an int32 array

This function appends an int32 array to the document. Stored value can be fetched using given key.

Parameters
bsa astarte_bson_serializer_t.
nameBSON key, which is a C string.
int32_arrayan array of signed 32 bit integers.
countthe number of items stored in int32_array.

◆ astarte_bson_serializer_append_int64()

void astarte_bson_serializer_append_int64 ( struct astarte_bson_serializer_t bs,
const char *  name,
int64_t  value 
)

append an int64 value

This function appends an int64 value to the document. Stored value can be fetched using given key.

Parameters
bsa astarte_bson_serializer_t.
nameBSON key, which is a C string.
valuea 64 bits signed integer value.

◆ astarte_bson_serializer_append_int64_array()

void astarte_bson_serializer_append_int64_array ( struct astarte_bson_serializer_t bs,
const char *  name,
const int64_t *  int64_array,
int  count 
)

append an int64 array

This function appends an int64 array to the document. Stored value can be fetched using given key.

Parameters
bsa astarte_bson_serializer_t.
nameBSON key, which is a C string.
int64_arrayan array of signed 64 bit integers.
countthe number of items stored in int64_array.

◆ astarte_bson_serializer_append_string()

void astarte_bson_serializer_append_string ( struct astarte_bson_serializer_t bs,
const char *  name,
const char *  string 
)

append an UTF-8 string

This function appends an UTF-8 string to the document. Stored value can be fetched using given key.

Parameters
bsa astarte_bson_serializer_t.
nameBSON key, which is a C string.
valuea 0 terminated UTF-8 string.

◆ astarte_bson_serializer_append_string_array()

void astarte_bson_serializer_append_string_array ( struct astarte_bson_serializer_t bs,
const char *  name,
const char *const *  string_array,
int  count 
)

append a string array

This function appends a string array to the document. Stored value can be fetched using given key.

Parameters
bsa astarte_bson_serializer_t.
nameBSON key, which is a C string.
string_arrayan array of 0 terminated UTF-8 strings.
countthe number of items stored in string_array.

◆ astarte_bson_serializer_destroy()

void astarte_bson_serializer_destroy ( struct astarte_bson_serializer_t bs)

destroy given BSON serializer.

This function has to be called to destroy and free astarte_bson_serializer_t memory.

Parameters
bsan astarte_bson_serializer_t that will be destroyed.

◆ astarte_bson_serializer_document_size()

int astarte_bson_serializer_document_size ( const struct astarte_bson_serializer_t bs)

return document size

This function returns BSON document size in bytes.

Parameters
bsa astarte_bson_serializer_t.

◆ astarte_bson_serializer_get_document()

const void* astarte_bson_serializer_get_document ( const struct astarte_bson_serializer_t bs,
int *  size 
)

return bson serializer internal buffer.

This function might be used to get internal buffer without any data copy. Returned buffer will be invalid after serializer destruction.

Parameters
bsa astarte_bson_serializer_t.

◆ astarte_bson_serializer_init()

void astarte_bson_serializer_init ( struct astarte_bson_serializer_t bs)

initialize given BSON serializer.

This function has to be called to initialize and allocate memory for astarte_bson_serializer_t.

Parameters
bsan astarte_bson_serializer_t that will be initialized.

◆ astarte_bson_serializer_write_document()

astarte_err_t astarte_bson_serializer_write_document ( const struct astarte_bson_serializer_t bs,
void *  out_buf,
int  out_buf_len,
int *  out_doc_size 
)

copy bson serializer internal buffer to a different buffer.

This function should be used to get a copy of the BSON document data. end of document should be called before calling this function.

Parameters
bsa astarte_bson_serializer_t.
out_bufdestination buffer.
out_buf_lendestination buffer length.
out_doc_sizeBSON document size (that is <= out_buf_len).
Returns
astarte_err_t ASTARTE_OK on success, otherwise an error is returned.