diff options
author | px4dev <px4@purgatory.org> | 2012-08-28 21:52:26 -0700 |
---|---|---|
committer | px4dev <px4@purgatory.org> | 2012-08-28 21:52:26 -0700 |
commit | f0286d1a10343c573fac6382e3c2cb0b714a1f72 (patch) | |
tree | 3cd5c6b02eb1eaf45de99be76c9a28af78bf0336 | |
parent | 952f862dad7b3105ed6ad37f4836effe71938b91 (diff) | |
download | px4-firmware-f0286d1a10343c573fac6382e3c2cb0b714a1f72.tar.gz px4-firmware-f0286d1a10343c573fac6382e3c2cb0b714a1f72.tar.bz2 px4-firmware-f0286d1a10343c573fac6382e3c2cb0b714a1f72.zip |
Distinguish between the end of the top-level BSON object and an error so that parameter loading can complete.
-rw-r--r-- | apps/systemlib/bson/tinybson.c | 12 | ||||
-rw-r--r-- | apps/systemlib/bson/tinybson.h | 14 | ||||
-rw-r--r-- | apps/systemlib/param/param.c | 28 |
3 files changed, 37 insertions, 17 deletions
diff --git a/apps/systemlib/bson/tinybson.c b/apps/systemlib/bson/tinybson.c index 1e5274a12..27878401e 100644 --- a/apps/systemlib/bson/tinybson.c +++ b/apps/systemlib/bson/tinybson.c @@ -45,13 +45,13 @@ #if 1 -# define debug(fmt, args...) do { warnx("BSON:" fmt, ##args); } while(0) +# define debug(fmt, args...) do { warnx("BSON: " fmt, ##args); } while(0) #else # define debug(fmt, args...) do { } while(0) #endif #define CODER_CHECK(_c) do { if (_c->fd == -1) return -1; } while(0) -#define CODER_KILL(_c, _reason) do { debug("killed:%s", _reason); _c->fd = -1; return -1; } while(0) +#define CODER_KILL(_c, _reason) do { debug("killed: %s", _reason); _c->fd = -1; return -1; } while(0) static int read_int8(bson_decoder_t decoder, int8_t *b) @@ -105,8 +105,12 @@ bson_decoder_next(bson_decoder_t decoder) decoder->nesting--; /* if the nesting level is now zero, the top-level document is done */ - if (decoder->nesting == 0) + if (decoder->nesting == 0) { CODER_KILL(decoder, "nesting is zero, document is done"); + + /* return end-of-file to the caller */ + return 0; + } } /* if there are unread bytes pending in the stream, discard them */ @@ -247,7 +251,7 @@ bson_encoder_fini(bson_encoder_t encoder) { CODER_CHECK(encoder); - if (write_int8(encoder, 0)) + if (write_int8(encoder, BSON_EOO)) CODER_KILL(encoder, "write error on document terminator"); return 0; diff --git a/apps/systemlib/bson/tinybson.h b/apps/systemlib/bson/tinybson.h index 6b92e61ef..1b9de5cd3 100644 --- a/apps/systemlib/bson/tinybson.h +++ b/apps/systemlib/bson/tinybson.h @@ -108,21 +108,35 @@ struct bson_decoder_s /** * Initialise the decoder. + * + * @param decoder Decoder state structure to be initialised. + * @param fd File to read BSON data from. + * @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(bson_decoder_t decoder, int fd, bson_decoder_callback callback, void *private); /** * Process the next node from the stream and invoke the callback. + * + * @param decoder Decoder state, must have been initialised with bson_decoder_init. + * @return -1 if parsing encountered an error, 0 if the BSON stream has ended, + * otherwise the return value from the callback. */ __EXPORT int bson_decoder_next(bson_decoder_t decoder); /** * Copy node data. + * + * @param decoder Decoder state, must have been initialised with bson_decoder_init. */ __EXPORT int bson_decoder_copy_data(bson_decoder_t decoder, void *buf); /** * Report copyable data size. + * + * @param decoder Decoder state, must have been initialised with bson_decoder_init. */ __EXPORT size_t bson_decoder_data_pending(bson_decoder_t decoder); diff --git a/apps/systemlib/param/param.c b/apps/systemlib/param/param.c index 9391558e9..b69efb77c 100644 --- a/apps/systemlib/param/param.c +++ b/apps/systemlib/param/param.c @@ -479,14 +479,14 @@ param_export(int fd, bool only_unsaved) } } - if (bson_encoder_fini(&encoder)) - goto out; - result = 0; out: param_unlock(); + if (result == 0) + result = bson_encoder_fini(&encoder); + return result; } @@ -499,7 +499,8 @@ param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node) int result = -1; /* - * EOO means the end of the parameter object. + * EOO means the end of the parameter object. (Currently not supporting + * nested BSON objects). */ if (node->type == BSON_EOO) { *(bool *)private = true; @@ -513,8 +514,10 @@ param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node) */ param_t param = param_find(node->name); - if (param == PARAM_INVALID) - return 0; + if (param == PARAM_INVALID) { + debug("ignoring unrecognised parameter '%s'", node->name); + return 1; + } /* * Handle setting the parameter from the node @@ -583,7 +586,8 @@ param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node) tmp = NULL; } - result = 0; + /* don't return zero, that means EOF */ + result = 1; out: @@ -607,16 +611,14 @@ param_import(int fd) done = false; - while (!done) { + do { result = bson_decoder_next(&decoder); - if (result != 0) { - debug("error during BSON decode"); - break; - } - } + } while(result > 0); out: + if (result < 0) + debug("BSON error decoding parameters"); return result; } |