systemcmds/bsondump: new command line utility (extracted from parameters)

This commit is contained in:
Daniel Agar
2022-12-19 17:09:23 -05:00
parent f5524fa605
commit f2cd7667dc
61 changed files with 286 additions and 194 deletions
+1
View File
@@ -68,6 +68,7 @@ add_subdirectory(system_identification EXCLUDE_FROM_ALL)
add_subdirectory(tecs EXCLUDE_FROM_ALL)
add_subdirectory(terrain_estimation EXCLUDE_FROM_ALL)
add_subdirectory(timesync EXCLUDE_FROM_ALL)
add_subdirectory(tinybson EXCLUDE_FROM_ALL)
add_subdirectory(tunes EXCLUDE_FROM_ALL)
add_subdirectory(version EXCLUDE_FROM_ALL)
add_subdirectory(weather_vane EXCLUDE_FROM_ALL)
-2
View File
@@ -33,8 +33,6 @@
add_compile_options(${MAX_CUSTOM_OPT_LEVEL})
add_subdirectory(tinybson)
if (NOT PARAM_DEFAULT_OVERRIDES)
set(PARAM_DEFAULT_OVERRIDES "{}")
endif()
@@ -55,7 +55,7 @@
#include <parameters/param.h>
#include "../uthash/utarray.h"
#include <parameters/tinybson/tinybson.h>
#include <lib/tinybson/tinybson.h>
#include "flashparams.h"
#include "flashfs.h"
#include "../param_translation.h"
-11
View File
@@ -347,17 +347,6 @@ __EXPORT int param_import(int fd);
*/
__EXPORT int param_load(int fd);
/**
* Read saved parameters from file and dump to console.
*
* This function reads the file and dumps all contents to console
* values from a file.
*
* @param fd File descriptor to read from.
* @return Zero on success, nonzero if an error occurred during import.
*/
__EXPORT int param_dump(int fd);
/**
* Apply a function to each parameter.
*
+1 -69
View File
@@ -45,7 +45,7 @@
#include "param.h"
#include "param_translation.h"
#include <parameters/px4_parameters.hpp>
#include "tinybson/tinybson.h"
#include <lib/tinybson/tinybson.h>
#include <crc32.h>
#include <float.h>
@@ -1521,38 +1521,6 @@ param_import_callback(bson_decoder_t decoder, bson_node_t node)
return 1;
}
static int
param_dump_callback(bson_decoder_t decoder, 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)
{
@@ -1630,42 +1598,6 @@ param_load(int fd)
return param_import_internal(fd);
}
int
param_dump(int fd)
{
bson_decoder_s decoder{};
if (bson_decoder_init_file(&decoder, fd, param_dump_callback) == 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)
{
@@ -1,6 +1,6 @@
############################################################################
#
# Copyright (c) 2018 PX4 Development Team. All rights reserved.
# Copyright (c) 2018-2022 PX4 Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
@@ -31,7 +31,15 @@
#
############################################################################
add_library(tinybson tinybson.cpp)
add_library(tinybson
tinybson.cpp
tinybson.h
)
target_compile_definitions(tinybson PRIVATE -DMODULE_NAME="tinybson")
target_compile_options(tinybson PRIVATE -Wno-sign-compare) # TODO: fix this
target_compile_options(tinybson
PRIVATE
-Wno-sign-compare # TODO: fix this
${MAX_CUSTOM_OPT_LEVEL}
)
add_dependencies(tinybson prebuild_targets)
+42
View File
@@ -0,0 +1,42 @@
############################################################################
#
# Copyright (c) 2022 PX4 Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name PX4 nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
px4_add_module(
MODULE systemcmds__bsondump
MAIN bsondump
STACK_MAIN 4096
COMPILE_FLAGS
SRCS
bsondump.cpp
DEPENDS
tinybson
)
+12
View File
@@ -0,0 +1,12 @@
menuconfig SYSTEMCMDS_BSONDUMP
bool "bsondump"
default n
---help---
Enable bsondump
menuconfig USER_BSONDUMP
bool "bsondump running as userspace module"
default n
depends on BOARD_PROTECTED && SYSTEMCMDS_BSONDUMP
---help---
Put bsondump in userspace memory
+140
View File
@@ -0,0 +1,140 @@
/****************************************************************************
*
* Copyright (c) 2022 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/log.h>
#include <px4_platform_common/module.h>
#include <lib/tinybson/tinybson.h>
#include <fcntl.h>
static void print_usage(const char *reason = nullptr)
{
if (reason) {
PX4_ERR("%s", reason);
}
PRINT_MODULE_DESCRIPTION("read BSON from a file and print in human form");
PRINT_MODULE_USAGE_NAME_SIMPLE("bsondump", "command");
PRINT_MODULE_USAGE_ARG("<file>", "File name", false);
}
static int bson_print_callback(bson_decoder_t decoder, 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;
}
extern "C" __EXPORT int bsondump_main(int argc, char *argv[])
{
if (argc != 2) {
print_usage();
} else {
char *file_name = argv[1];
if (file_name) {
int fd = open(file_name, O_RDONLY);
if (fd < 0) {
PX4_ERR("open '%s' failed (%i)", file_name, errno);
return 1;
} else {
PX4_INFO_RAW("[bsondump] reading from %s\n", file_name);
bson_decoder_s decoder{};
if (bson_decoder_init_file(&decoder, fd, bson_print_callback) == 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);
close(fd);
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("no data");
return -1;
} else {
PX4_ERR("failed (%d)", result);
return -1;
}
}
}
}
}
return -1;
}
-48
View File
@@ -83,7 +83,6 @@ enum class COMPARE_ERROR_LEVEL {
static int do_save(const char *param_file_name);
static int do_save_default();
static int do_dump(const char *param_file_name);
static int do_load(const char *param_file_name);
static int do_import(const char *param_file_name = nullptr);
static int do_show(const char *search_string, bool only_changed);
@@ -136,8 +135,6 @@ $ reboot
PRINT_MODULE_USAGE_ARG("<file>", "File name (use default if not given)", true);
PRINT_MODULE_USAGE_COMMAND_DESCR("save", "Save params to a file");
PRINT_MODULE_USAGE_ARG("<file>", "File name (use default if not given)", true);
PRINT_MODULE_USAGE_COMMAND_DESCR("dump", "Dump params from a file");
PRINT_MODULE_USAGE_ARG("<file>", "File name (use default if not given)", true);
PRINT_MODULE_USAGE_COMMAND_DESCR("select", "Select default file");
PRINT_MODULE_USAGE_ARG("<file>", "File name", true);
@@ -212,15 +209,6 @@ param_main(int argc, char *argv[])
}
}
if (!strcmp(argv[1], "dump")) {
if (argc >= 3) {
return do_dump(argv[2]);
} else {
return do_dump(param_get_default_file());
}
}
if (!strcmp(argv[1], "load")) {
if (argc >= 3) {
return do_load(argv[2]);
@@ -443,42 +431,6 @@ do_save(const char *param_file_name)
return 0;
}
static int
do_dump(const char *param_file_name)
{
int fd = -1;
if (param_file_name) { // passing NULL means to select the flash storage
fd = open(param_file_name, O_RDONLY);
if (fd < 0) {
PX4_ERR("open '%s' failed (%i)", param_file_name, errno);
return 1;
} else {
PX4_INFO_RAW("[param] reading from %s\n\n", param_file_name);
}
}
int result = param_dump(fd);
if (fd >= 0) {
close(fd);
}
if (result < 0) {
if (param_file_name) {
PX4_ERR("reading from '%s' failed (%i)", param_file_name, result);
} else {
PX4_ERR("reading failed (%i)", result);
}
return 1;
}
return 0;
}
static int
do_load(const char *param_file_name)
+1 -1
View File
@@ -45,7 +45,7 @@
#include <math.h>
#include <systemlib/err.h>
#include <parameters/tinybson/tinybson.h>
#include <lib/tinybson/tinybson.h>
#include "tests_main.h"