41 #include <px4_platform_common/defines.h> 65 PX4_ERR(
"FAIL: encoder: append bool failed");
70 PX4_ERR(
"FAIL: encoder: append int failed");
75 PX4_ERR(
"FAIL: encoder: append int failed");
80 PX4_ERR(
"FAIL: encoder: append double failed");
85 PX4_ERR(
"FAIL: encoder: append string failed");
90 PX4_ERR(
"FAIL: encoder: append data failed");
104 if (!strcmp(node->
name,
"bool1")) {
106 PX4_ERR(
"FAIL: decoder: bool1 type %d, expected %d", node->
type,
BSON_BOOL);
111 PX4_ERR(
"FAIL: decoder: bool1 value %s, expected %s",
112 (node->
b ?
"true" :
"false"),
117 PX4_INFO(
"PASS: decoder: bool1");
121 if (!strcmp(node->
name,
"int1")) {
123 PX4_ERR(
"FAIL: decoder: int1 type %d, expected %d", node->
type,
BSON_INT32);
128 PX4_ERR(
"FAIL: decoder: int1 value %" PRIu64
", expected %d", node->
i,
sample_small_int);
132 warnx(
"PASS: decoder: int1");
136 if (!strcmp(node->
name,
"int2")) {
138 PX4_ERR(
"FAIL: decoder: int2 type %d, expected %d", node->
type,
BSON_INT64);
143 PX4_ERR(
"FAIL: decoder: int2 value %" PRIu64
", expected %" PRIu64, node->
i,
sample_big_int);
147 warnx(
"PASS: decoder: int2");
151 if (!strcmp(node->
name,
"double1")) {
153 PX4_ERR(
"FAIL: decoder: double1 type %d, expected %d", node->
type,
BSON_DOUBLE);
158 PX4_ERR(
"FAIL: decoder: double1 value %f, expected %f", node->
d,
sample_double);
162 warnx(
"PASS: decoder: double1");
166 if (!strcmp(node->
name,
"string1")) {
168 PX4_ERR(
"FAIL: decoder: string1 type %d, expected %d", node->
type,
BSON_STRING);
175 PX4_ERR(
"FAIL: decoder: string1 length %d wrong, expected %zd", len, strlen(
sample_string) + 1);
182 PX4_ERR(
"FAIL: decoder: string1 copy failed");
187 PX4_ERR(
"FAIL: decoder: string1 copy did not exhaust all data");
191 if (sbuf[len - 1] !=
'\0') {
192 PX4_ERR(
"FAIL: decoder: string1 not 0-terminated");
197 PX4_ERR(
"FAIL: decoder: string1 value '%s', expected '%s'", sbuf,
sample_string);
201 warnx(
"PASS: decoder: string1");
205 if (!strcmp(node->
name,
"data1")) {
207 PX4_ERR(
"FAIL: decoder: data1 type %d, expected %d", node->
type,
BSON_BINDATA);
214 PX4_ERR(
"FAIL: decoder: data1 length %d, expected %zu", len,
sizeof(
sample_data));
226 PX4_ERR(
"FAIL: decoder: data1 copy failed");
231 PX4_ERR(
"FAIL: decoder: data1 copy did not exhaust all data");
236 PX4_ERR(
"FAIL: decoder: data1 compare fail");
240 PX4_INFO(
"PASS: decoder: data1");
245 PX4_ERR(
"FAIL: decoder: unexpected node name '%s'", node->
name);
258 }
while (result > 0);
271 PX4_ERR(
"FAIL: bson_encoder_init_buf");
279 PX4_ERR(
"FAIL: bson_encoder_buf_len");
285 if (buf ==
nullptr) {
286 PX4_ERR(
"FAIL: bson_encoder_buf_data");
292 PX4_ERR(
"FAIL: bson_decoder_init_buf");
static const uint8_t sample_data[256]
void * bson_encoder_buf_data(bson_encoder_t encoder)
Get a pointer to the encoded object buffer.
int bson_decoder_next(bson_decoder_t decoder)
Process the next node from the stream and invoke the callback.
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.
bson_binary_subtype_t subtype
int bson_decoder_copy_data(bson_decoder_t decoder, void *buf)
Copy node data.
Node structure passed to the callback.
int bson_encoder_append_string(bson_encoder_t encoder, const char *name, const char *string)
Append a string to the encoded stream.
A simple subset SAX-style BSON parser and generator.
int bson_encoder_append_double(bson_encoder_t encoder, const char *name, double value)
Append a double to the encoded stream.
static const int64_t sample_big_int
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.
static const bool sample_bool
int bson_encoder_append_bool(bson_encoder_t encoder, const char *name, bool value)
Append a boolean to the encoded stream.
size_t bson_decoder_data_pending(bson_decoder_t decoder)
Report copyable data size.
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
int test_bson(int argc, char *argv[])
static const char * sample_string
int bson_encoder_fini(bson_encoder_t encoder)
Finalise the encoded stream.
int bson_encoder_buf_size(bson_encoder_t encoder)
Fetch the size of the encoded object; only valid for buffer operations.
int bson_encoder_init_buf(bson_encoder_t encoder, void *buf, unsigned bufsize)
Initialze the encoder for writing to a buffer.
static const int32_t sample_small_int
static const double sample_double
static int decode_callback(bson_decoder_t decoder, void *priv, bson_node_t node)
static int encode(bson_encoder_t encoder)
static void decode(bson_decoder_t decoder)
int bson_encoder_append_int(bson_encoder_t encoder, const char *name, int64_t value)
Append an integer to the encoded stream.