diff options
author | Thomas Gubler <thomasgubler@gmail.com> | 2012-10-30 18:30:35 +0100 |
---|---|---|
committer | Thomas Gubler <thomasgubler@gmail.com> | 2012-10-30 18:30:35 +0100 |
commit | ee1e98babb3b46de0c64dbd0c5be3c678fc7c727 (patch) | |
tree | 64d899e3ba2fbb75c06427f22be6178dd68a34ac /apps/systemlib/bson/tinybson.h | |
parent | e716bd02ce378918c1f2a31a6586a94f48bcb226 (diff) | |
parent | edd2715f84532f6c4c748cc97f0fe8a2982aa885 (diff) | |
download | px4-firmware-ee1e98babb3b46de0c64dbd0c5be3c678fc7c727.tar.gz px4-firmware-ee1e98babb3b46de0c64dbd0c5be3c678fc7c727.tar.bz2 px4-firmware-ee1e98babb3b46de0c64dbd0c5be3c678fc7c727.zip |
Merge branch 'master' of https://github.com/PX4/Firmware into fw_control
Diffstat (limited to 'apps/systemlib/bson/tinybson.h')
-rw-r--r-- | apps/systemlib/bson/tinybson.h | 113 |
1 files changed, 104 insertions, 9 deletions
diff --git a/apps/systemlib/bson/tinybson.h b/apps/systemlib/bson/tinybson.h index b6229dc50..666f8191a 100644 --- a/apps/systemlib/bson/tinybson.h +++ b/apps/systemlib/bson/tinybson.h @@ -59,9 +59,8 @@ typedef enum { BSON_BOOL = 8, BSON_DATE = 9, BSON_NULL = 10, - BSON_INT = 16, - BSON_TIMESTAMP = 17, - BSON_LONG = 18 + BSON_INT32 = 16, + BSON_INT64 = 18 } bson_type_t; typedef enum bson_binary_subtype { @@ -75,6 +74,11 @@ typedef enum bson_binary_subtype { #define BSON_MAXNAME 32 /** + * Buffer growth increment when writing to a buffer. + */ +#define BSON_BUF_INCREMENT 128 + +/** * Node structure passed to the callback. */ typedef struct bson_node_s { @@ -82,7 +86,7 @@ typedef struct bson_node_s { bson_type_t type; bson_binary_subtype_t subtype; union { - int32_t i; + int64_t i; double d; bool b; }; @@ -92,11 +96,21 @@ typedef struct bson_decoder_s *bson_decoder_t; /** * Node callback. + * + * The node callback function's return value is returned by bson_decoder_next. */ typedef int (* bson_decoder_callback)(bson_decoder_t decoder, void *private, bson_node_t node); struct bson_decoder_s { + /* file reader state */ int fd; + + /* buffer reader state */ + uint8_t *buf; + size_t bufsize; + unsigned bufpos; + + bool dead; bson_decoder_callback callback; void *private; unsigned nesting; @@ -105,7 +119,7 @@ struct bson_decoder_s { }; /** - * Initialise the decoder. + * Initialise the decoder to read from a file. * * @param decoder Decoder state structure to be initialised. * @param fd File to read BSON data from. @@ -113,7 +127,21 @@ struct bson_decoder_s { * @param private Callback private data, stored in node. * @return Zero on success. */ -__EXPORT int bson_decoder_init(bson_decoder_t decoder, int fd, bson_decoder_callback callback, void *private); +__EXPORT int bson_decoder_init_file(bson_decoder_t decoder, int fd, bson_decoder_callback callback, void *private); + +/** + * Initialise the decoder to read from a buffer in memory. + * + * @param decoder Decoder state structure to be initialised. + * @param buf Buffer to read from. + * @param bufsize Size 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. + * @param callback Callback to be invoked by bson_decoder_next + * @param private Callback private data, stored in node. + * @return Zero on success. + */ +__EXPORT int bson_decoder_init_buf(bson_decoder_t decoder, void *buf, unsigned bufsize, bson_decoder_callback callback, void *private); /** * Process the next node from the stream and invoke the callback. @@ -142,37 +170,104 @@ __EXPORT size_t bson_decoder_data_pending(bson_decoder_t decoder); * Encoder state structure. */ typedef struct bson_encoder_s { + /* file writer state */ int fd; + /* buffer writer state */ + uint8_t *buf; + unsigned bufsize; + unsigned bufpos; + + bool realloc_ok; + bool dead; + } *bson_encoder_t; /** - * Initialze the encoder. + * Initialze the encoder for writing to a file. + * + * @param encoder Encoder state structure to be initialised. + * @param fd File to write to. + * @return Zero on success. */ -__EXPORT int bson_encoder_init(bson_encoder_t encoder, int fd); +__EXPORT int bson_encoder_init_file(bson_encoder_t encoder, int fd); + +/** + * Initialze the encoder for writing to a buffer. + * + * @param encoder Encoder state structure to be initialised. + * @param buf Buffer pointer to use, or NULL if the buffer + * should be allocated by the encoder. + * @param bufsize Maximum buffer size, or zero for no limit. If + * the buffer is supplied, the size of the supplied buffer. + * @return Zero on success. + */ +__EXPORT int bson_encoder_init_buf(bson_encoder_t encoder, void *buf, unsigned bufsize); /** * Finalise the encoded stream. + * + * @param encoder The encoder to finalise. */ __EXPORT int bson_encoder_fini(bson_encoder_t encoder); /** + * Fetch the size of the encoded object; only valid for buffer operations. + */ +__EXPORT int bson_encoder_buf_size(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. + */ +__EXPORT void *bson_encoder_buf_data(bson_encoder_t encoder); + +/** + * Append a boolean to the encoded stream. + * + * @param encoder Encoder state. + * @param name Node name. + * @param value Value to be encoded. + */ +__EXPORT int bson_encoder_append_bool(bson_encoder_t encoder, const char *name, bool value); + +/** * Append an integer to the encoded stream. + * + * @param encoder Encoder state. + * @param name Node name. + * @param value Value to be encoded. */ -__EXPORT int bson_encoder_append_int(bson_encoder_t encoder, const char *name, int32_t value); +__EXPORT int bson_encoder_append_int(bson_encoder_t encoder, const char *name, int64_t value); /** * Append a double to the encoded stream + * + * @param encoder Encoder state. + * @param name Node name. + * @param value Value to be encoded. */ __EXPORT int bson_encoder_append_double(bson_encoder_t encoder, const char *name, double value); /** * Append a string to the encoded stream. + * + * @param encoder Encoder state. + * @param name Node name. + * @param string Nul-terminated C string. */ __EXPORT int bson_encoder_append_string(bson_encoder_t encoder, const char *name, const char *string); /** * Append a binary blob to the encoded stream. + * + * @param encoder Encoder state. + * @param name Node name. + * @param subtype Binary data subtype. + * @param size Data size. + * @param data Buffer containing data to be encoded. */ __EXPORT int bson_encoder_append_binary(bson_encoder_t encoder, const char *name, bson_binary_subtype_t subtype, size_t size, const void *data); |