parameters: add param dump to read and decode bson to console

This commit is contained in:
Daniel Agar
2021-11-19 13:46:44 -05:00
parent 7560d45c61
commit 7bfebf5289
3 changed files with 129 additions and 0 deletions
+69
View File
@@ -1405,6 +1405,38 @@ out:
return result;
}
static int
param_dump_callback(bson_decoder_t decoder, void *priv, bson_node_t node)
{
switch (node->type) {
case BSON_EOO:
PX4_INFO_RAW("BSON_EOO\n");
return 0;
case BSON_DOUBLE:
PX4_INFO_RAW("BSON_DOUBLE: %s = %.6f\n", node->name, node->d);
return 1;
case BSON_BOOL:
PX4_INFO_RAW("BSON_BOOL: %s = %d\n", node->name, node->b);
return 1;
case BSON_INT32:
PX4_INFO_RAW("BSON_INT32: %s = %" PRIi32 "\n", node->name, node->i32);
return 1;
case BSON_INT64:
PX4_INFO_RAW("BSON_INT64: %s = %" PRIi64 "\n", node->name, node->i64);
return 1;
default:
PX4_INFO_RAW("ERROR %s unhandled bson type %d\n", node->name, node->type);
return 1; // just skip this entry
}
return -1;
}
static int
param_import_internal(int fd, bool mark_saved)
{
@@ -1468,6 +1500,43 @@ param_load(int fd)
return param_import_internal(fd, true);
}
int
param_dump(int fd)
{
bson_decoder_s decoder{};
param_import_state state;
if (bson_decoder_init_file(&decoder, fd, param_dump_callback, &state) == 0) {
PX4_INFO_RAW("BSON document size %" PRId32 "\n", decoder.total_document_size);
int result = -1;
do {
result = bson_decoder_next(&decoder);
} while (result > 0);
if (result == 0) {
PX4_INFO_RAW("BSON decoded %" PRId32 " bytes (double:%" PRIu16 ", string:%" PRIu16 ", bin:%" PRIu16 ", bool:%" PRIu16
", int32:%" PRIu16 ", int64:%" PRIu16 ")\n",
decoder.total_decoded_size,
decoder.count_node_double, decoder.count_node_string, decoder.count_node_bindata, decoder.count_node_bool,
decoder.count_node_int32, decoder.count_node_int64);
return 0;
} else if (result == -ENODATA) {
PX4_WARN("BSON: no data");
return 0;
} else {
PX4_ERR("param dump failed (%d)", result);
}
}
return -1;
}
void
param_foreach(void (*func)(void *arg, param_t param), void *arg, bool only_changed, bool only_used)
{