PX4 Firmware
PX4 Autopilot Software http://px4.io
tinybson.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35 * @file tinybson.h
36 *
37 * A simple subset SAX-style BSON parser and generator. See http://bsonspec.org
38 *
39 * Some types and defines taken from the standalone BSON parser/generator
40 * in the Mongo C connector.
41 */
42 
43 #ifndef _TINYBSON_H
44 #define _TINYBSON_H
45 
46 #include <sys/types.h>
47 #include <stdint.h>
48 #include <stdbool.h>
49 
50 /** subset of the BSON node types we might care about */
51 typedef enum {
52  BSON_EOO = 0,
59  BSON_BOOL = 8,
60  BSON_DATE = 9,
62  BSON_INT32 = 16,
64 } bson_type_t;
65 
66 typedef enum bson_binary_subtype {
70 
71 /**
72  * Maximum node name length.
73  */
74 #define BSON_MAXNAME 32
75 
76 /**
77  * Buffer growth increment when writing to a buffer.
78  */
79 #define BSON_BUF_INCREMENT 128
80 
81 /**
82  * Node structure passed to the callback.
83  */
84 typedef struct bson_node_s {
88  union {
89  int64_t i;
90  double d;
91  bool b;
92  };
93 } *bson_node_t;
94 
96 
97 /**
98  * Node callback.
99  *
100  * The node callback function's return value is returned by bson_decoder_next.
101  */
102 typedef int (* bson_decoder_callback)(bson_decoder_t decoder, void *priv, bson_node_t node);
103 
105  /* file reader state */
106  int fd;
107 
108  /* buffer reader state */
109  uint8_t *buf;
110  size_t bufsize;
111  unsigned bufpos;
112 
113  bool dead;
115  void *priv;
116  unsigned nesting;
117  struct bson_node_s node;
118  int32_t pending;
119 };
120 
121 /**
122  * Initialise the decoder to read from a file.
123  *
124  * @param decoder Decoder state structure to be initialised.
125  * @param fd File to read BSON data from.
126  * @param callback Callback to be invoked by bson_decoder_next
127  * @param priv Callback private data, stored in node.
128  * @return Zero on success.
129  */
130 __EXPORT int bson_decoder_init_file(bson_decoder_t decoder, int fd, bson_decoder_callback callback, void *priv);
131 
132 /**
133  * Initialise the decoder to read from a buffer in memory.
134  *
135  * @param decoder Decoder state structure to be initialised.
136  * @param buf Buffer to read from.
137  * @param bufsize Size of the buffer (BSON object may be smaller). May be
138  * passed as zero if the buffer size should be extracted from the
139  * BSON header only.
140  * @param callback Callback to be invoked by bson_decoder_next
141  * @param priv Callback private data, stored in node.
142  * @return Zero on success.
143  */
144 __EXPORT int bson_decoder_init_buf(bson_decoder_t decoder, void *buf, unsigned bufsize, bson_decoder_callback callback,
145  void *priv);
146 
147 /**
148  * Process the next node from the stream and invoke the callback.
149  *
150  * @param decoder Decoder state, must have been initialised with bson_decoder_init.
151  * @return -1 if parsing encountered an error, 0 if the BSON stream has ended,
152  * otherwise the return value from the callback.
153  */
154 __EXPORT int bson_decoder_next(bson_decoder_t decoder);
155 
156 /**
157  * Copy node data.
158  *
159  * @param decoder Decoder state, must have been initialised with bson_decoder_init.
160  */
161 __EXPORT int bson_decoder_copy_data(bson_decoder_t decoder, void *buf);
162 
163 /**
164  * Report copyable data size.
165  *
166  * @param decoder Decoder state, must have been initialised with bson_decoder_init.
167  */
168 __EXPORT size_t bson_decoder_data_pending(bson_decoder_t decoder);
169 
170 /**
171  * Encoder state structure.
172  */
173 typedef struct bson_encoder_s {
174  /* file writer state */
175  int fd;
176 
177  /* buffer writer state */
178  uint8_t *buf;
179  unsigned bufsize;
180  unsigned bufpos;
181 
183  bool dead;
184 
185 } *bson_encoder_t;
186 
187 /**
188  * Initialze the encoder for writing to a file.
189  *
190  * @param encoder Encoder state structure to be initialised.
191  * @param fd File to write to.
192  * @return Zero on success.
193  */
195 
196 /**
197  * Initialze the encoder for writing to a file.
198  *
199  * @param encoder Encoder state structure to be initialised.
200  * @param fd File to write to.
201  * @param buf Buffer pointer to use, can't be nullptr
202  * @param bufsize Supplied buffer size
203  * @return Zero on success.
204  */
205 __EXPORT int bson_encoder_init_buf_file(bson_encoder_t encoder, int fd, void *buf, unsigned bufsize);
206 
207 /**
208  * Initialze the encoder for writing to a buffer.
209  *
210  * @param encoder Encoder state structure to be initialised.
211  * @param buf Buffer pointer to use, or nullptr if the buffer
212  * should be allocated by the encoder.
213  * @param bufsize Maximum buffer size, or zero for no limit. If
214  * the buffer is supplied, the size of the supplied buffer.
215  * @return Zero on success.
216  */
217 __EXPORT int bson_encoder_init_buf(bson_encoder_t encoder, void *buf, unsigned bufsize);
218 
219 /**
220  * Finalise the encoded stream.
221  *
222  * @param encoder The encoder to finalise.
223  */
225 
226 /**
227  * Fetch the size of the encoded object; only valid for buffer operations.
228  */
230 
231 /**
232  * Get a pointer to the encoded object buffer.
233  *
234  * Note that if the buffer was allocated by the encoder, it is the caller's responsibility
235  * to free this buffer.
236  */
238 
239 /**
240  * Append a boolean to the encoded stream.
241  *
242  * @param encoder Encoder state.
243  * @param name Node name.
244  * @param value Value to be encoded.
245  */
246 __EXPORT int bson_encoder_append_bool(bson_encoder_t encoder, const char *name, bool value);
247 
248 /**
249  * Append an integer to the encoded stream.
250  *
251  * @param encoder Encoder state.
252  * @param name Node name.
253  * @param value Value to be encoded.
254  */
255 __EXPORT int bson_encoder_append_int(bson_encoder_t encoder, const char *name, int64_t value);
256 
257 /**
258  * Append a double to the encoded stream
259  *
260  * @param encoder Encoder state.
261  * @param name Node name.
262  * @param value Value to be encoded.
263  */
264 __EXPORT int bson_encoder_append_double(bson_encoder_t encoder, const char *name, double value);
265 
266 /**
267  * Append a string to the encoded stream.
268  *
269  * @param encoder Encoder state.
270  * @param name Node name.
271  * @param string Nul-terminated C string.
272  */
273 __EXPORT int bson_encoder_append_string(bson_encoder_t encoder, const char *name, const char *string);
274 
275 /**
276  * Append a binary blob to the encoded stream.
277  *
278  * @param encoder Encoder state.
279  * @param name Node name.
280  * @param subtype Binary data subtype.
281  * @param size Data size.
282  * @param data Buffer containing data to be encoded.
283  */
285  size_t size, const void *data);
286 
287 
288 #endif
char name[BSON_MAXNAME]
Definition: tinybson.h:85
unsigned bufsize
Definition: tinybson.h:179
unsigned bufpos
Definition: tinybson.h:180
struct bson_decoder_s * bson_decoder_t
Definition: tinybson.h:95
__EXPORT int bson_encoder_init_buf(bson_encoder_t encoder, void *buf, unsigned bufsize)
Initialze the encoder for writing to a buffer.
Definition: tinybson.cpp:460
uint8_t * buf
Definition: tinybson.h:109
size_t bufsize
Definition: tinybson.h:110
__EXPORT int bson_encoder_append_double(bson_encoder_t encoder, const char *name, double value)
Append a double to the encoded stream.
Definition: tinybson.cpp:580
bson_binary_subtype_t subtype
Definition: tinybson.h:87
Definition: I2C.hpp:51
unsigned nesting
Definition: tinybson.h:116
__EXPORT size_t bson_decoder_data_pending(bson_decoder_t decoder)
Report copyable data size.
Definition: tinybson.cpp:331
Encoder state structure.
Definition: tinybson.h:173
bool realloc_ok
Definition: tinybson.h:182
#define BSON_MAXNAME
Maximum node name length.
Definition: tinybson.h:74
struct bson_node_s * bson_node_t
Node structure passed to the callback.
void * priv
Definition: tinybson.h:115
struct bson_node_s node
Definition: tinybson.h:117
Node structure passed to the callback.
Definition: tinybson.h:84
__EXPORT int bson_encoder_init_file(bson_encoder_t encoder, int fd)
Initialze the encoder for writing to a file.
Definition: tinybson.cpp:429
int(* bson_decoder_callback)(bson_decoder_t decoder, void *priv, bson_node_t node)
Node callback.
Definition: tinybson.h:102
__EXPORT void * bson_encoder_buf_data(bson_encoder_t encoder)
Get a pointer to the encoded object buffer.
Definition: tinybson.cpp:527
uint8_t * data
Definition: dataman.cpp:149
__EXPORT int bson_decoder_copy_data(bson_decoder_t decoder, void *buf)
Copy node data.
Definition: tinybson.cpp:312
__EXPORT int bson_encoder_fini(bson_encoder_t encoder)
Finalise the encoded stream.
Definition: tinybson.cpp:484
__EXPORT 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.
Definition: tinybson.cpp:137
unsigned bufpos
Definition: tinybson.h:111
__EXPORT 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.
Definition: tinybson.cpp:114
int fd
Definition: dataman.cpp:146
struct bson_encoder_s * bson_encoder_t
Encoder state structure.
bson_type_t type
Definition: tinybson.h:86
bson_type_t
subset of the BSON node types we might care about
Definition: tinybson.h:51
__EXPORT int bson_encoder_init_buf_file(bson_encoder_t encoder, int fd, void *buf, unsigned bufsize)
Initialze the encoder for writing to a file.
Definition: tinybson.cpp:443
__EXPORT 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.
Definition: tinybson.cpp:614
int32_t pending
Definition: tinybson.h:118
bool b
Definition: tinybson.h:91
int64_t i
Definition: tinybson.h:89
bson_binary_subtype
Definition: tinybson.h:66
bson_decoder_callback callback
Definition: tinybson.h:114
__EXPORT int bson_encoder_append_string(bson_encoder_t encoder, const char *name, const char *string)
Append a string to the encoded stream.
Definition: tinybson.cpp:595
__EXPORT int bson_encoder_buf_size(bson_encoder_t encoder)
Fetch the size of the encoded object; only valid for buffer operations.
Definition: tinybson.cpp:515
uint8_t * buf
Definition: tinybson.h:178
double d
Definition: tinybson.h:90
enum bson_binary_subtype bson_binary_subtype_t
__EXPORT int bson_encoder_append_int(bson_encoder_t encoder, const char *name, int64_t value)
Append an integer to the encoded stream.
Definition: tinybson.cpp:552
__EXPORT int bson_encoder_append_bool(bson_encoder_t encoder, const char *name, bool value)
Append a boolean to the encoded stream.
Definition: tinybson.cpp:538
__EXPORT int bson_decoder_next(bson_decoder_t decoder)
Process the next node from the stream and invoke the callback.
Definition: tinybson.cpp:180