Firmware
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 
43 #ifndef _TINYBSON_H
44 #define _TINYBSON_H
45 
46 #include <sys/types.h>
47 #include <stdint.h>
48 #include <stdbool.h>
49 
51 typedef enum {
52  BSON_EOO = 0,
53  BSON_DOUBLE = 1,
54  BSON_STRING = 2,
55  BSON_OBJECT = 3,
56  BSON_ARRAY = 4,
57  BSON_BINDATA = 5,
58  BSON_UNDEFINED = 6,
59  BSON_BOOL = 8,
60  BSON_DATE = 9,
61  BSON_nullptr = 10,
62  BSON_INT32 = 16,
63  BSON_INT64 = 18
64 } bson_type_t;
65 
66 typedef enum bson_binary_subtype {
67  BSON_BIN_BINARY = 0,
68  BSON_BIN_USER = 128
69 } bson_binary_subtype_t;
70 
74 #define BSON_MAXNAME 32
75 
79 #define BSON_BUF_INCREMENT 128
80 
84 typedef struct bson_node_s {
85  char name[BSON_MAXNAME];
86  bson_type_t type;
87  bson_binary_subtype_t subtype;
88  union {
89  int64_t i;
90  double d;
91  bool b;
92  };
93 } *bson_node_t;
94 
95 typedef struct bson_decoder_s *bson_decoder_t;
96 
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;
114  bson_decoder_callback callback;
115  void *priv;
116  unsigned nesting;
117  struct bson_node_s node;
118  int32_t pending;
119 };
120 
130 __EXPORT int bson_decoder_init_file(bson_decoder_t decoder, int fd, bson_decoder_callback callback, void *priv);
131 
144 __EXPORT int bson_decoder_init_buf(bson_decoder_t decoder, void *buf, unsigned bufsize, bson_decoder_callback callback,
145  void *priv);
146 
154 __EXPORT int bson_decoder_next(bson_decoder_t decoder);
155 
161 __EXPORT int bson_decoder_copy_data(bson_decoder_t decoder, void *buf);
162 
168 __EXPORT size_t bson_decoder_data_pending(bson_decoder_t decoder);
169 
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 
182  bool realloc_ok;
183  bool dead;
184 
185 } *bson_encoder_t;
186 
194 __EXPORT int bson_encoder_init_file(bson_encoder_t encoder, int fd);
195 
205 __EXPORT int bson_encoder_init_buf_file(bson_encoder_t encoder, int fd, void *buf, unsigned bufsize);
206 
217 __EXPORT int bson_encoder_init_buf(bson_encoder_t encoder, void *buf, unsigned bufsize);
218 
225 
230 
238 
246 __EXPORT int bson_encoder_append_bool(bson_encoder_t encoder, const char *name, bool value);
247 
255 __EXPORT int bson_encoder_append_int(bson_encoder_t encoder, const char *name, int64_t value);
256 
264 __EXPORT int bson_encoder_append_double(bson_encoder_t encoder, const char *name, double value);
265 
273 __EXPORT int bson_encoder_append_string(bson_encoder_t encoder, const char *name, const char *string);
274 
284 __EXPORT int bson_encoder_append_binary(bson_encoder_t encoder, const char *name, bson_binary_subtype_t subtype,
285  size_t size, const void *data);
286 
287 
288 #endif
__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
__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
__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
Definition: tinybson.h:104
#define BSON_MAXNAME
Maximum node name length.
Definition: tinybson.h:74
struct bson_node_s * bson_node_t
Node structure passed to the callback.
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
Definition: I2C.hpp:51
__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
__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
struct bson_encoder_s * bson_encoder_t
Encoder state structure.
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
__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
__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