mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-15 00:03:34 +08:00
parameters: tinybson print errors and count imported types
This commit is contained in:
@@ -79,7 +79,7 @@ static int
|
||||
param_export_internal(bool only_unsaved, param_filter_func filter)
|
||||
{
|
||||
struct param_wbuf_s *s = nullptr;
|
||||
struct bson_encoder_s encoder;
|
||||
bson_encoder_s encoder{};
|
||||
int result = -1;
|
||||
|
||||
/* Use realloc */
|
||||
@@ -245,7 +245,7 @@ param_import_callback(bson_decoder_t decoder, void *priv, bson_node_t node)
|
||||
goto out;
|
||||
}
|
||||
|
||||
i = node->i;
|
||||
i = node->i32;
|
||||
v = &i;
|
||||
break;
|
||||
|
||||
@@ -280,7 +280,7 @@ out:
|
||||
static int
|
||||
param_import_internal(bool mark_saved)
|
||||
{
|
||||
struct bson_decoder_s decoder;
|
||||
bson_decoder_s decoder{};
|
||||
int result = -1;
|
||||
struct param_import_state state;
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ bool param_modify_on_import(bson_node_t node)
|
||||
} __attribute__((packed)) struct_value;
|
||||
int32_t param_value;
|
||||
} old_param;
|
||||
old_param.param_value = node->i;
|
||||
old_param.param_value = node->i32;
|
||||
|
||||
int32_t method = old_param.struct_value.authentication_method;
|
||||
param_set_no_notification(param_find("COM_ARM_AUTH_MET"), &method);
|
||||
@@ -62,7 +62,7 @@ bool param_modify_on_import(bson_node_t node)
|
||||
param_set_no_notification(param_find("COM_ARM_AUTH_TO"), &timeout);
|
||||
|
||||
strcpy(node->name, "COM_ARM_AUTH_ID");
|
||||
node->i = old_param.struct_value.authorizer_system_id;
|
||||
node->i32 = old_param.struct_value.authorizer_system_id;
|
||||
|
||||
PX4_INFO("migrating COM_ARM_AUTH: %" PRId32 " -> COM_ARM_AUTH_ID:%" PRId8 ", COM_ARM_AUTH_MET: %" PRId32
|
||||
" and COM_ARM_AUTH_TO: %f",
|
||||
@@ -77,7 +77,7 @@ bool param_modify_on_import(bson_node_t node)
|
||||
if (node->type == BSON_INT32) {
|
||||
if (strcmp("LED_RGB_MAXBRT", node->name) == 0) {
|
||||
// convert integer (0-15) to float percentage
|
||||
node->d = math::constrain(static_cast<double>(node->i) / 15., 0., 1.);
|
||||
node->d = math::constrain(static_cast<double>(node->i32) / 15., 0., 1.);
|
||||
node->type = BSON_DOUBLE;
|
||||
strcpy(node->name, "SYS_RGB_MAXBRT");
|
||||
PX4_INFO("param migrating LED_RGB_MAXBRT (removed) -> SYS_RGB_MAXBRT: value=%.3f", node->d);
|
||||
|
||||
@@ -1201,7 +1201,7 @@ param_export(int fd, bool only_unsaved, param_filter_func filter)
|
||||
}
|
||||
|
||||
param_wbuf_s *s = nullptr;
|
||||
struct bson_encoder_s encoder;
|
||||
bson_encoder_s encoder{};
|
||||
|
||||
int shutdown_lock_ret = px4_shutdown_lock();
|
||||
|
||||
@@ -1366,7 +1366,7 @@ param_import_callback(bson_decoder_t decoder, void *priv, bson_node_t node)
|
||||
goto out;
|
||||
}
|
||||
|
||||
i = node->i;
|
||||
i = node->i32;
|
||||
v = &i;
|
||||
|
||||
PX4_DEBUG("Imported %s with value %d", param_name(param), i);
|
||||
@@ -1408,7 +1408,7 @@ static int
|
||||
param_import_internal(int fd, bool mark_saved)
|
||||
{
|
||||
for (int attempt = 1; attempt < 5; attempt++) {
|
||||
bson_decoder_s decoder;
|
||||
bson_decoder_s decoder{};
|
||||
param_import_state state;
|
||||
|
||||
if (bson_decoder_init_file(&decoder, fd, param_import_callback, &state) == 0) {
|
||||
@@ -1422,8 +1422,10 @@ param_import_internal(int fd, bool mark_saved)
|
||||
} while (result > 0);
|
||||
|
||||
if (result == 0) {
|
||||
PX4_INFO("BSON document size %" PRId32 " bytes, decoded %" PRId32 " bytes", decoder.total_document_size,
|
||||
decoder.total_decoded_size);
|
||||
PX4_INFO("BSON document size %" PRId32 " bytes, decoded %" PRId32 " bytes (INT32:%" PRIu16 ", FLOAT:%" PRIu16 ")",
|
||||
decoder.total_document_size, decoder.total_decoded_size,
|
||||
decoder.count_node_int32, decoder.count_node_double);
|
||||
|
||||
return 0;
|
||||
|
||||
} else if (result == -ENODATA) {
|
||||
|
||||
@@ -51,8 +51,8 @@
|
||||
# define debug(fmt, args...) do { } while(0)
|
||||
#endif
|
||||
|
||||
#define CODER_CHECK(_c) do { if (_c->dead) { debug("coder dead"); return -1; }} while(0)
|
||||
#define CODER_KILL(_c, _reason) do { debug("killed: %s", _reason); _c->dead = true; return -1; } while(0)
|
||||
#define CODER_CHECK(_c) do { if (_c->dead) { PX4_ERR("coder dead"); return -1; }} while(0)
|
||||
#define CODER_KILL(_c, _reason) do { PX4_ERR("killed: %s", _reason); _c->dead = true; return -1; } while(0)
|
||||
|
||||
static int
|
||||
read_x(bson_decoder_t decoder, void *p, size_t s)
|
||||
@@ -118,18 +118,12 @@ int
|
||||
bson_decoder_init_file(bson_decoder_t decoder, int fd, bson_decoder_callback callback, void *priv)
|
||||
{
|
||||
decoder->fd = fd;
|
||||
decoder->buf = nullptr;
|
||||
decoder->dead = false;
|
||||
decoder->callback = callback;
|
||||
decoder->priv = priv;
|
||||
decoder->nesting = 1;
|
||||
decoder->pending = 0;
|
||||
decoder->node.type = BSON_UNDEFINED;
|
||||
decoder->total_decoded_size = 0;
|
||||
|
||||
// read document size
|
||||
decoder->total_document_size = 0;
|
||||
|
||||
if (read_int32(decoder, &decoder->total_document_size)) {
|
||||
CODER_KILL(decoder, "failed reading length");
|
||||
}
|
||||
@@ -190,7 +184,6 @@ int
|
||||
bson_decoder_next(bson_decoder_t decoder)
|
||||
{
|
||||
int8_t tbyte;
|
||||
int32_t tint;
|
||||
unsigned nlen;
|
||||
|
||||
CODER_CHECK(decoder);
|
||||
@@ -263,34 +256,12 @@ bson_decoder_next(bson_decoder_t decoder)
|
||||
debug("got name '%s'", decoder->node.name);
|
||||
|
||||
switch (decoder->node.type) {
|
||||
case BSON_BOOL:
|
||||
if (read_int8(decoder, &tbyte)) {
|
||||
CODER_KILL(decoder, "read error on BSON_BOOL");
|
||||
}
|
||||
|
||||
decoder->node.b = (tbyte != 0);
|
||||
break;
|
||||
|
||||
case BSON_INT32:
|
||||
if (read_int32(decoder, &tint)) {
|
||||
CODER_KILL(decoder, "read error on BSON_INT");
|
||||
}
|
||||
|
||||
decoder->node.i = tint;
|
||||
break;
|
||||
|
||||
case BSON_INT64:
|
||||
if (read_int64(decoder, &decoder->node.i)) {
|
||||
CODER_KILL(decoder, "read error on BSON_INT");
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case BSON_DOUBLE:
|
||||
if (read_double(decoder, &decoder->node.d)) {
|
||||
CODER_KILL(decoder, "read error on BSON_DOUBLE");
|
||||
}
|
||||
|
||||
decoder->count_node_double++;
|
||||
break;
|
||||
|
||||
case BSON_STRING:
|
||||
@@ -298,6 +269,7 @@ bson_decoder_next(bson_decoder_t decoder)
|
||||
CODER_KILL(decoder, "read error on BSON_STRING length");
|
||||
}
|
||||
|
||||
decoder->count_node_string++;
|
||||
break;
|
||||
|
||||
case BSON_BINDATA:
|
||||
@@ -310,6 +282,32 @@ bson_decoder_next(bson_decoder_t decoder)
|
||||
}
|
||||
|
||||
decoder->node.subtype = (bson_binary_subtype_t)tbyte;
|
||||
decoder->count_node_bindata++;
|
||||
break;
|
||||
|
||||
case BSON_BOOL:
|
||||
if (read_int8(decoder, &tbyte)) {
|
||||
CODER_KILL(decoder, "read error on BSON_BOOL");
|
||||
}
|
||||
|
||||
decoder->node.b = (tbyte != 0);
|
||||
decoder->count_node_bool++;
|
||||
break;
|
||||
|
||||
case BSON_INT32:
|
||||
if (read_int32(decoder, &decoder->node.i32)) {
|
||||
CODER_KILL(decoder, "read error on BSON_INT");
|
||||
}
|
||||
|
||||
decoder->count_node_int32++;
|
||||
break;
|
||||
|
||||
case BSON_INT64:
|
||||
if (read_int64(decoder, &decoder->node.i64)) {
|
||||
CODER_KILL(decoder, "read error on BSON_INT");
|
||||
}
|
||||
|
||||
decoder->count_node_int64++;
|
||||
break;
|
||||
|
||||
/* XXX currently not supporting other types */
|
||||
@@ -483,22 +481,15 @@ bson_encoder_init_buf_file(bson_encoder_t encoder, int fd, void *buf, unsigned b
|
||||
int
|
||||
bson_encoder_init_buf(bson_encoder_t encoder, void *buf, unsigned bufsize)
|
||||
{
|
||||
encoder->fd = -1;
|
||||
encoder->buf = (uint8_t *)buf;
|
||||
encoder->bufpos = 0;
|
||||
encoder->dead = false;
|
||||
|
||||
if (encoder->buf == nullptr) {
|
||||
encoder->bufsize = 0;
|
||||
encoder->realloc_ok = true;
|
||||
|
||||
} else {
|
||||
encoder->bufsize = bufsize;
|
||||
encoder->realloc_ok = false;
|
||||
}
|
||||
|
||||
encoder->total_document_size = 0;
|
||||
|
||||
if (write_int32(encoder, 0)) {
|
||||
CODER_KILL(encoder, "write error on document length");
|
||||
}
|
||||
|
||||
@@ -49,18 +49,19 @@
|
||||
|
||||
/** subset of the BSON node types we might care about */
|
||||
typedef enum {
|
||||
BSON_EOO = 0,
|
||||
BSON_DOUBLE = 1,
|
||||
BSON_STRING = 2,
|
||||
BSON_OBJECT = 3,
|
||||
BSON_ARRAY = 4,
|
||||
BSON_BINDATA = 5,
|
||||
BSON_EOO = 0,
|
||||
BSON_DOUBLE = 1,
|
||||
BSON_STRING = 2,
|
||||
BSON_OBJECT = 3,
|
||||
BSON_ARRAY = 4,
|
||||
BSON_BINDATA = 5,
|
||||
BSON_UNDEFINED = 6,
|
||||
BSON_BOOL = 8,
|
||||
BSON_DATE = 9,
|
||||
BSON_nullptr = 10,
|
||||
BSON_INT32 = 16,
|
||||
BSON_INT64 = 18
|
||||
BSON_BOOL = 8,
|
||||
BSON_DATE = 9,
|
||||
BSON_nullptr = 10,
|
||||
BSON_INT32 = 16,
|
||||
BSON_TIMESTAMP = 17,
|
||||
BSON_INT64 = 18
|
||||
} bson_type_t;
|
||||
|
||||
typedef enum bson_binary_subtype {
|
||||
@@ -86,9 +87,10 @@ typedef struct bson_node_s {
|
||||
bson_type_t type;
|
||||
bson_binary_subtype_t subtype;
|
||||
union {
|
||||
int64_t i;
|
||||
double d;
|
||||
bool b;
|
||||
double d; // bson type 1
|
||||
bool b; // bson type 8
|
||||
int32_t i32; // bson type 16
|
||||
int64_t i64; // bson type 18
|
||||
};
|
||||
} *bson_node_t;
|
||||
|
||||
@@ -103,22 +105,29 @@ typedef int (* bson_decoder_callback)(bson_decoder_t decoder, void *priv, bson_n
|
||||
|
||||
struct bson_decoder_s {
|
||||
/* file reader state */
|
||||
int fd;
|
||||
int fd{-1};
|
||||
|
||||
/* buffer reader state */
|
||||
uint8_t *buf;
|
||||
size_t bufsize;
|
||||
unsigned bufpos;
|
||||
uint8_t *buf{nullptr};
|
||||
size_t bufsize{0};
|
||||
unsigned bufpos{0};
|
||||
|
||||
bool dead;
|
||||
bool dead{false};
|
||||
bson_decoder_callback callback;
|
||||
void *priv;
|
||||
unsigned nesting;
|
||||
struct bson_node_s node;
|
||||
int32_t pending;
|
||||
void *priv{nullptr};
|
||||
unsigned nesting{0};
|
||||
struct bson_node_s node {};
|
||||
int32_t pending{0};
|
||||
|
||||
int32_t total_document_size;
|
||||
int32_t total_decoded_size;
|
||||
int32_t total_document_size{0};
|
||||
int32_t total_decoded_size{0};
|
||||
|
||||
uint16_t count_node_double{0};
|
||||
uint16_t count_node_string{0};
|
||||
uint16_t count_node_bindata{0};
|
||||
uint16_t count_node_bool{0};
|
||||
uint16_t count_node_int32{0};
|
||||
uint16_t count_node_int64{0};
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -175,17 +184,17 @@ __EXPORT size_t bson_decoder_data_pending(bson_decoder_t decoder);
|
||||
*/
|
||||
typedef struct bson_encoder_s {
|
||||
/* file writer state */
|
||||
int fd;
|
||||
int fd{-1};
|
||||
|
||||
/* buffer writer state */
|
||||
uint8_t *buf;
|
||||
unsigned bufsize;
|
||||
unsigned bufpos;
|
||||
uint8_t *buf{nullptr};
|
||||
unsigned bufsize{0};
|
||||
unsigned bufpos{0};
|
||||
|
||||
bool realloc_ok;
|
||||
bool dead;
|
||||
bool realloc_ok{false};
|
||||
bool dead{false};
|
||||
|
||||
int32_t total_document_size;
|
||||
int32_t total_document_size{0};
|
||||
|
||||
} *bson_encoder_t;
|
||||
|
||||
|
||||
@@ -123,8 +123,8 @@ decode_callback(bson_decoder_t decoder, void *priv, bson_node_t node)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (node->i != sample_small_int) {
|
||||
PX4_ERR("FAIL: decoder: int1 value %" PRIu64 ", expected %" PRIi32 "", node->i, sample_small_int);
|
||||
if (node->i32 != sample_small_int) {
|
||||
PX4_ERR("FAIL: decoder: int1 value %" PRIi32 ", expected %" PRIi32 "", node->i32, sample_small_int);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -138,8 +138,8 @@ decode_callback(bson_decoder_t decoder, void *priv, bson_node_t node)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (node->i != sample_big_int) {
|
||||
PX4_ERR("FAIL: decoder: int2 value %" PRIu64 ", expected %" PRIu64, node->i, sample_big_int);
|
||||
if (node->i64 != sample_big_int) {
|
||||
PX4_ERR("FAIL: decoder: int2 value %" PRIi64 ", expected %" PRIu64, node->i64, sample_big_int);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -260,8 +260,8 @@ decode(bson_decoder_t decoder)
|
||||
int
|
||||
test_bson(int argc, char *argv[])
|
||||
{
|
||||
struct bson_encoder_s encoder;
|
||||
struct bson_decoder_s decoder;
|
||||
bson_encoder_s encoder{};
|
||||
bson_decoder_s decoder{};
|
||||
void *buf;
|
||||
int len;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user