PX4 Firmware
PX4 Autopilot Software http://px4.io
tinybson.cpp File Reference
#include <px4_platform_common/posix.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <systemlib/err.h>
#include "tinybson.h"
Include dependency graph for tinybson.cpp:

Go to the source code of this file.

Macros

#define debug(fmt, args...)   do { } while(0)
 
#define CODER_CHECK(_c)   do { if (_c->dead) { debug("coder dead"); return -1; }} while(0)
 
#define CODER_KILL(_c, _reason)   do { debug("killed: %s", _reason); _c->dead = true; return -1; } while(0)
 
#define BSON_READ   read
 
#define BSON_WRITE   write
 
#define BSON_FSYNC   fsync
 

Functions

static int read_x (bson_decoder_t decoder, void *p, size_t s)
 
static int read_int8 (bson_decoder_t decoder, int8_t *b)
 
static int read_int32 (bson_decoder_t decoder, int32_t *i)
 
static int read_int64 (bson_decoder_t decoder, int64_t *i)
 
static int read_double (bson_decoder_t decoder, double *d)
 
int bson_decoder_init_file (bson_decoder_t decoder, int fd, bson_decoder_callback callback, void *priv)
 Initialise the decoder to read from a file. More...
 
int bson_decoder_init_buf (bson_decoder_t decoder, void *buf, unsigned bufsize, bson_decoder_callback callback, void *priv)
 Initialise the decoder to read from a buffer in memory. More...
 
int bson_decoder_next (bson_decoder_t decoder)
 Process the next node from the stream and invoke the callback. More...
 
int bson_decoder_copy_data (bson_decoder_t decoder, void *buf)
 Copy node data. More...
 
size_t bson_decoder_data_pending (bson_decoder_t decoder)
 Report copyable data size. More...
 
static int write_x (bson_encoder_t encoder, const void *p, size_t s)
 
static int write_int8 (bson_encoder_t encoder, int8_t b)
 
static int write_int32 (bson_encoder_t encoder, int32_t i)
 
static int write_int64 (bson_encoder_t encoder, int64_t i)
 
static int write_double (bson_encoder_t encoder, double d)
 
static int write_name (bson_encoder_t encoder, const char *name)
 
int bson_encoder_init_file (bson_encoder_t encoder, int fd)
 Initialze the encoder for writing to a file. More...
 
int bson_encoder_init_buf_file (bson_encoder_t encoder, int fd, void *buf, unsigned bufsize)
 Initialze the encoder for writing to a file. More...
 
int bson_encoder_init_buf (bson_encoder_t encoder, void *buf, unsigned bufsize)
 Initialze the encoder for writing to a buffer. More...
 
int bson_encoder_fini (bson_encoder_t encoder)
 Finalise the encoded stream. More...
 
int bson_encoder_buf_size (bson_encoder_t encoder)
 Fetch the size of the encoded object; only valid for buffer operations. More...
 
void * bson_encoder_buf_data (bson_encoder_t encoder)
 Get a pointer to the encoded object buffer. More...
 
int bson_encoder_append_bool (bson_encoder_t encoder, const char *name, bool value)
 Append a boolean to the encoded stream. More...
 
int bson_encoder_append_int (bson_encoder_t encoder, const char *name, int64_t value)
 Append an integer to the encoded stream. More...
 
int bson_encoder_append_double (bson_encoder_t encoder, const char *name, double value)
 Append a double to the encoded stream. More...
 
int bson_encoder_append_string (bson_encoder_t encoder, const char *name, const char *string)
 Append a string to the encoded stream. More...
 
int bson_encoder_append_binary (bson_encoder_t encoder, const char *name, bson_binary_subtype_t subtype, size_t size, const void *data)
 Append a binary blob to the encoded stream. More...
 

Macro Definition Documentation

◆ BSON_FSYNC

#define BSON_FSYNC   fsync

Definition at line 59 of file tinybson.cpp.

Referenced by bson_encoder_fini().

◆ BSON_READ

#define BSON_READ   read

Definition at line 57 of file tinybson.cpp.

Referenced by read_x().

◆ BSON_WRITE

#define BSON_WRITE   write

Definition at line 58 of file tinybson.cpp.

Referenced by bson_encoder_fini(), and write_x().

◆ CODER_CHECK

◆ CODER_KILL

◆ debug

#define debug (   fmt,
  args... 
)    do { } while(0)

Function Documentation

◆ bson_decoder_copy_data()

int bson_decoder_copy_data ( bson_decoder_t  decoder,
void *  buf 
)

Copy node data.

Parameters
decoderDecoder state, must have been initialised with bson_decoder_init.

Definition at line 312 of file tinybson.cpp.

References CODER_CHECK, CODER_KILL, bson_decoder_s::pending, and read_x().

Referenced by decode_callback(), and param_import_callback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bson_decoder_data_pending()

size_t bson_decoder_data_pending ( bson_decoder_t  decoder)

Report copyable data size.

Parameters
decoderDecoder state, must have been initialised with bson_decoder_init.

Definition at line 331 of file tinybson.cpp.

References bson_decoder_s::pending.

Referenced by decode_callback(), and param_import_callback().

Here is the caller graph for this function:

◆ bson_decoder_init_buf()

int bson_decoder_init_buf ( bson_decoder_t  decoder,
void *  buf,
unsigned  bufsize,
bson_decoder_callback  callback,
void *  priv 
)

Initialise the decoder to read from a buffer in memory.

Parameters
decoderDecoder state structure to be initialised.
bufBuffer to read from.
bufsizeSize of the buffer (BSON object may be smaller). May be passed as zero if the buffer size should be extracted from the BSON header only.
callbackCallback to be invoked by bson_decoder_next
privCallback private data, stored in node.
Returns
Zero on success.

Definition at line 137 of file tinybson.cpp.

References BSON_UNDEFINED, bson_decoder_s::buf, bson_decoder_s::bufpos, bson_decoder_s::bufsize, bson_decoder_s::callback, CODER_KILL, bson_decoder_s::dead, debug, bson_decoder_s::fd, bson_decoder_s::nesting, bson_decoder_s::node, bson_decoder_s::pending, bson_decoder_s::priv, read_int32(), and bson_node_s::type.

Referenced by param_import_internal(), and test_bson().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bson_decoder_init_file()

int bson_decoder_init_file ( bson_decoder_t  decoder,
int  fd,
bson_decoder_callback  callback,
void *  priv 
)

Initialise the decoder to read from a file.

Parameters
decoderDecoder state structure to be initialised.
fdFile to read BSON data from.
callbackCallback to be invoked by bson_decoder_next
privCallback private data, stored in node.
Returns
Zero on success.

Definition at line 114 of file tinybson.cpp.

References BSON_UNDEFINED, bson_decoder_s::buf, bson_decoder_s::callback, CODER_KILL, bson_decoder_s::dead, bson_decoder_s::fd, fd, bson_decoder_s::nesting, bson_decoder_s::node, bson_decoder_s::pending, bson_decoder_s::priv, read_int32(), and bson_node_s::type.

Referenced by param_import_internal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bson_decoder_next()

int bson_decoder_next ( bson_decoder_t  decoder)

Process the next node from the stream and invoke the callback.

Parameters
decoderDecoder state, must have been initialised with bson_decoder_init.
Returns
-1 if parsing encountered an error, 0 if the BSON stream has ended, otherwise the return value from the callback.

Definition at line 180 of file tinybson.cpp.

References bson_node_s::b, BSON_BINDATA, BSON_BOOL, BSON_DOUBLE, BSON_EOO, BSON_INT32, BSON_INT64, BSON_MAXNAME, BSON_STRING, bson_decoder_s::callback, CODER_CHECK, CODER_KILL, bson_node_s::d, debug, bson_decoder_s::fd, bson_node_s::i, bson_node_s::name, bson_decoder_s::nesting, bson_decoder_s::node, bson_decoder_s::pending, bson_decoder_s::priv, read_double(), read_int32(), read_int64(), read_int8(), bson_node_s::subtype, and bson_node_s::type.

Referenced by decode(), and param_import_internal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bson_encoder_append_binary()

int bson_encoder_append_binary ( bson_encoder_t  encoder,
const char *  name,
bson_binary_subtype_t  subtype,
size_t  size,
const void *  data 
)

Append a binary blob to the encoded stream.

Parameters
encoderEncoder state.
nameNode name.
subtypeBinary data subtype.
sizeData size.
dataBuffer containing data to be encoded.

Definition at line 614 of file tinybson.cpp.

References BSON_BINDATA, CODER_CHECK, CODER_KILL, write_int32(), write_int8(), write_name(), and write_x().

Referenced by encode(), param_export(), and param_export_internal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bson_encoder_append_bool()

int bson_encoder_append_bool ( bson_encoder_t  encoder,
const char *  name,
bool  value 
)

Append a boolean to the encoded stream.

Parameters
encoderEncoder state.
nameNode name.
valueValue to be encoded.

Definition at line 538 of file tinybson.cpp.

References BSON_BOOL, CODER_CHECK, CODER_KILL, write_int8(), and write_name().

Referenced by encode().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bson_encoder_append_double()

int bson_encoder_append_double ( bson_encoder_t  encoder,
const char *  name,
double  value 
)

Append a double to the encoded stream.

Parameters
encoderEncoder state.
nameNode name.
valueValue to be encoded.

Definition at line 580 of file tinybson.cpp.

References BSON_DOUBLE, CODER_CHECK, CODER_KILL, write_double(), write_int8(), and write_name().

Referenced by encode(), param_export(), and param_export_internal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bson_encoder_append_int()

int bson_encoder_append_int ( bson_encoder_t  encoder,
const char *  name,
int64_t  value 
)

Append an integer to the encoded stream.

Parameters
encoderEncoder state.
nameNode name.
valueValue to be encoded.

Definition at line 552 of file tinybson.cpp.

References BSON_INT32, BSON_INT64, CODER_CHECK, CODER_KILL, debug, write_int32(), write_int64(), write_int8(), and write_name().

Referenced by encode(), param_export(), and param_export_internal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bson_encoder_append_string()

int bson_encoder_append_string ( bson_encoder_t  encoder,
const char *  name,
const char *  string 
)

Append a string to the encoded stream.

Parameters
encoderEncoder state.
nameNode name.
stringNul-terminated C string.

Definition at line 595 of file tinybson.cpp.

References BSON_STRING, CODER_CHECK, CODER_KILL, write_int32(), write_int8(), write_name(), and write_x().

Referenced by encode().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bson_encoder_buf_data()

void* bson_encoder_buf_data ( bson_encoder_t  encoder)

Get a pointer to the encoded object buffer.

Note that if the buffer was allocated by the encoder, it is the caller's responsibility to free this buffer.

Definition at line 527 of file tinybson.cpp.

References bson_encoder_s::buf, and bson_encoder_s::fd.

Referenced by param_export_internal(), and test_bson().

Here is the caller graph for this function:

◆ bson_encoder_buf_size()

int bson_encoder_buf_size ( bson_encoder_t  encoder)

Fetch the size of the encoded object; only valid for buffer operations.

Definition at line 515 of file tinybson.cpp.

References bson_encoder_s::bufpos, CODER_CHECK, and bson_encoder_s::fd.

Referenced by bson_encoder_fini(), param_export_internal(), and test_bson().

Here is the caller graph for this function:

◆ bson_encoder_fini()

int bson_encoder_fini ( bson_encoder_t  encoder)

Finalise the encoded stream.

Parameters
encoderThe encoder to finalise.

Definition at line 484 of file tinybson.cpp.

References bson_encoder_buf_size(), BSON_EOO, BSON_FSYNC, BSON_WRITE, bson_encoder_s::buf, bson_encoder_s::bufpos, CODER_CHECK, CODER_KILL, bson_encoder_s::fd, and write_int8().

Referenced by encode(), param_export(), and param_export_internal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bson_encoder_init_buf()

int bson_encoder_init_buf ( bson_encoder_t  encoder,
void *  buf,
unsigned  bufsize 
)

Initialze the encoder for writing to a buffer.

Parameters
encoderEncoder state structure to be initialised.
bufBuffer pointer to use, or nullptr if the buffer should be allocated by the encoder.
bufsizeMaximum buffer size, or zero for no limit. If the buffer is supplied, the size of the supplied buffer.
Returns
Zero on success.

Definition at line 460 of file tinybson.cpp.

References bson_encoder_s::buf, bson_encoder_s::bufpos, bson_encoder_s::bufsize, CODER_KILL, bson_encoder_s::dead, bson_encoder_s::fd, bson_encoder_s::realloc_ok, and write_int32().

Referenced by param_export_internal(), and test_bson().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bson_encoder_init_buf_file()

int bson_encoder_init_buf_file ( bson_encoder_t  encoder,
int  fd,
void *  buf,
unsigned  bufsize 
)

Initialze the encoder for writing to a file.

Parameters
encoderEncoder state structure to be initialised.
fdFile to write to.
bufBuffer pointer to use, can't be nullptr
bufsizeSupplied buffer size
Returns
Zero on success.

Definition at line 443 of file tinybson.cpp.

References bson_encoder_s::buf, bson_encoder_s::bufpos, bson_encoder_s::bufsize, CODER_KILL, bson_encoder_s::dead, fd, bson_encoder_s::fd, bson_encoder_s::realloc_ok, and write_int32().

Referenced by param_export().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bson_encoder_init_file()

int bson_encoder_init_file ( bson_encoder_t  encoder,
int  fd 
)

Initialze the encoder for writing to a file.

Parameters
encoderEncoder state structure to be initialised.
fdFile to write to.
Returns
Zero on success.

Definition at line 429 of file tinybson.cpp.

References bson_encoder_s::buf, CODER_KILL, bson_encoder_s::dead, fd, bson_encoder_s::fd, and write_int32().

Referenced by param_export().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_double()

static int read_double ( bson_decoder_t  decoder,
double *  d 
)
static

Definition at line 108 of file tinybson.cpp.

References read_x().

Referenced by bson_decoder_next().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_int32()

static int read_int32 ( bson_decoder_t  decoder,
int32_t *  i 
)
static

Definition at line 96 of file tinybson.cpp.

References read_x().

Referenced by bson_decoder_init_buf(), bson_decoder_init_file(), and bson_decoder_next().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_int64()

static int read_int64 ( bson_decoder_t  decoder,
int64_t *  i 
)
static

Definition at line 102 of file tinybson.cpp.

References read_x().

Referenced by bson_decoder_next().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_int8()

static int read_int8 ( bson_decoder_t  decoder,
int8_t *  b 
)
static

Definition at line 90 of file tinybson.cpp.

References read_x().

Referenced by bson_decoder_next().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_x()

static int read_x ( bson_decoder_t  decoder,
void *  p,
size_t  s 
)
static

Definition at line 62 of file tinybson.cpp.

References BSON_READ, bson_decoder_s::buf, bson_decoder_s::bufpos, bson_decoder_s::bufsize, CODER_CHECK, CODER_KILL, debug, and bson_decoder_s::fd.

Referenced by bson_decoder_copy_data(), read_double(), read_int32(), read_int64(), and read_int8().

Here is the caller graph for this function:

◆ write_double()

static int write_double ( bson_encoder_t  encoder,
double  d 
)
static

Definition at line 411 of file tinybson.cpp.

References write_x().

Referenced by bson_encoder_append_double().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_int32()

static int write_int32 ( bson_encoder_t  encoder,
int32_t  i 
)
static

Definition at line 399 of file tinybson.cpp.

References write_x().

Referenced by bson_encoder_append_binary(), bson_encoder_append_int(), bson_encoder_append_string(), bson_encoder_init_buf(), bson_encoder_init_buf_file(), and bson_encoder_init_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_int64()

static int write_int64 ( bson_encoder_t  encoder,
int64_t  i 
)
static

Definition at line 405 of file tinybson.cpp.

References write_x().

Referenced by bson_encoder_append_int().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_int8()

static int write_int8 ( bson_encoder_t  encoder,
int8_t  b 
)
static

Definition at line 393 of file tinybson.cpp.

References write_x().

Referenced by bson_encoder_append_binary(), bson_encoder_append_bool(), bson_encoder_append_double(), bson_encoder_append_int(), bson_encoder_append_string(), and bson_encoder_fini().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_name()

static int write_name ( bson_encoder_t  encoder,
const char *  name 
)
static

Definition at line 417 of file tinybson.cpp.

References BSON_MAXNAME, CODER_KILL, and write_x().

Referenced by bson_encoder_append_binary(), bson_encoder_append_bool(), bson_encoder_append_double(), bson_encoder_append_int(), and bson_encoder_append_string().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_x()

static int write_x ( bson_encoder_t  encoder,
const void *  p,
size_t  s 
)
static

Definition at line 337 of file tinybson.cpp.

References BSON_BUF_INCREMENT, BSON_WRITE, bson_encoder_s::buf, bson_encoder_s::bufpos, bson_encoder_s::bufsize, CODER_CHECK, CODER_KILL, debug, bson_encoder_s::fd, and bson_encoder_s::realloc_ok.

Referenced by bson_encoder_append_binary(), bson_encoder_append_string(), write_double(), write_int32(), write_int64(), write_int8(), and write_name().

Here is the caller graph for this function: