42 #include <px4_platform_common/px4_config.h> 43 #include <px4_platform_common/log.h> 44 #include <px4_platform_common/module.h> 45 #include <px4_platform_common/posix.h> 59 #include <arch/board/board.h> 80 #define PARAM_PRINT PX4_INFO 82 #define PARAM_PRINT PX4_INFO_RAW 85 static int do_save(
const char *param_file_name);
87 static int do_load(
const char *param_file_name);
88 static int do_import(
const char *param_file_name);
89 static int do_show(
const char *search_string,
bool only_changed);
94 static int do_set(
const char *
name,
const char *val,
bool fail_on_not_found);
97 static int do_reset(
const char *excludes[],
int num_excludes);
98 static int do_touch(
const char *params[],
int num_params);
104 PRINT_MODULE_DESCRIPTION(
107 Command to access and manipulate parameters via shell or script. 109 This is used for example in the startup script to set airframe-specific parameters. 111 Parameters are automatically saved when changed, eg. with `param set`. They are typically stored to FRAM 112 or to the SD card. `param select` can be used to change the storage location for subsequent saves (this will 113 need to be (re-)configured on every boot). 115 If the FLASH-based backend is enabled (which is done at compile time, e.g. for the Intel Aero or Omnibus), 116 `param select` has no effect and the default is always the FLASH backend. However `param save/load <file>` 117 can still be used to write to/read from files. 119 Each parameter has a 'used' flag, which is set when it's read during boot. It is used to only show relevant 120 parameters to a ground control station. 123 Change the airframe and make sure the airframe's default parameters are loaded: 124 $ param set SYS_AUTOSTART 4001 125 $ param set SYS_AUTOCONFIG 1 129 PRINT_MODULE_USAGE_NAME("param",
"command");
130 PRINT_MODULE_USAGE_COMMAND_DESCR(
"load",
"Load params from a file (overwrite all)");
131 PRINT_MODULE_USAGE_ARG(
"<file>",
"File name (use default if not given)",
true);
132 PRINT_MODULE_USAGE_COMMAND_DESCR(
"import",
"Import params from a file");
133 PRINT_MODULE_USAGE_ARG(
"<file>",
"File name (use default if not given)",
true);
134 PRINT_MODULE_USAGE_COMMAND_DESCR(
"save",
"Save params to a file");
135 PRINT_MODULE_USAGE_ARG(
"<file>",
"File name (use default if not given)",
true);
137 PRINT_MODULE_USAGE_COMMAND_DESCR(
"select",
"Select default file");
138 PRINT_MODULE_USAGE_ARG(
"<file>",
"File name (use <root>/eeprom/parameters if not given)",
true);
140 PRINT_MODULE_USAGE_COMMAND_DESCR(
"show",
"Show parameter values");
141 PRINT_MODULE_USAGE_PARAM_FLAG(
'a',
"Show all parameters (not just used)",
true);
142 PRINT_MODULE_USAGE_PARAM_FLAG(
'c',
"Show only changed and used params",
true);
143 PRINT_MODULE_USAGE_PARAM_FLAG(
'q',
"quiet mode, print only param value (name needs to be exact)",
true);
144 PRINT_MODULE_USAGE_ARG(
"<filter>",
"Filter by param name (wildcard at end allowed, eg. sys_*)",
true);
146 PRINT_MODULE_USAGE_COMMAND_DESCR(
"status",
"Print status of parameter system");
148 PRINT_MODULE_USAGE_COMMAND_DESCR(
"set",
"Set parameter to a value");
149 PRINT_MODULE_USAGE_ARG(
"<param_name> <value>",
"Parameter name and value to set",
false);
150 PRINT_MODULE_USAGE_ARG(
"fail",
"If provided, let the command fail if param is not found",
true);
152 PRINT_MODULE_USAGE_COMMAND_DESCR(
"compare",
"Compare a param with a value. Command will succeed if equal");
153 PRINT_MODULE_USAGE_PARAM_FLAG(
's',
"If provided, silent errors if parameter doesn't exists",
true);
154 PRINT_MODULE_USAGE_ARG(
"<param_name> <value>",
"Parameter name and value to compare",
false);
156 PRINT_MODULE_USAGE_COMMAND_DESCR(
"greater",
157 "Compare a param with a value. Command will succeed if param is greater than the value");
158 PRINT_MODULE_USAGE_PARAM_FLAG(
's',
"If provided, silent errors if parameter doesn't exists",
true);
159 PRINT_MODULE_USAGE_ARG(
"<param_name> <value>",
"Parameter name and value to compare",
false);
161 PRINT_MODULE_USAGE_ARG(
"<param_name> <value>",
"Parameter name and value to compare",
false);
163 PRINT_MODULE_USAGE_COMMAND_DESCR(
"touch",
"Mark a parameter as used");
164 PRINT_MODULE_USAGE_ARG(
"<param_name1> [<param_name2>]",
"Parameter name (one or more)",
true);
166 PRINT_MODULE_USAGE_COMMAND_DESCR(
"reset",
"Reset params to default");
167 PRINT_MODULE_USAGE_ARG(
"<exclude1> [<exclude2>]",
"Do not reset matching params (wildcard at end allowed)",
true);
168 PRINT_MODULE_USAGE_COMMAND_DESCR(
"reset_nostart",
169 "Reset params to default, but keep SYS_AUTOSTART and SYS_AUTOCONFIG");
170 PRINT_MODULE_USAGE_ARG(
"<exclude1> [<exclude2>]",
"Do not reset matching params (wildcard at end allowed)",
true);
172 PRINT_MODULE_USAGE_COMMAND_DESCR(
"index",
"Show param for a given index");
173 PRINT_MODULE_USAGE_ARG(
"<index>",
"Index: an integer >= 0",
false);
174 PRINT_MODULE_USAGE_COMMAND_DESCR(
"index_used",
"Show used param for a given index");
175 PRINT_MODULE_USAGE_ARG(
"<index>",
"Index: an integer >= 0",
false);
176 PRINT_MODULE_USAGE_COMMAND_DESCR(
"find",
"Show index of a param");
177 PRINT_MODULE_USAGE_ARG(
"<param>",
"param name",
false);
184 if (!strcmp(argv[1],
"save")) {
192 PX4_ERR(
"Param save failed (%i)", ret);
201 if (!strcmp(argv[1],
"load")) {
210 if (!strcmp(argv[1],
"import")) {
219 if (!strcmp(argv[1],
"select")) {
229 PX4_INFO(
"selected parameter default file %s", default_file);
234 if (!strcmp(argv[1],
"show")) {
237 if (!strcmp(argv[2],
"-c")) {
245 }
else if (!strcmp(argv[2],
"-a")) {
248 }
else if (!strcmp(argv[2],
"-q")) {
253 return do_show(argv[2],
false);
257 return do_show(
nullptr,
false);
261 if (!strcmp(argv[1],
"status")) {
266 if (!strcmp(argv[1],
"set")) {
270 bool fail = !strcmp(argv[4],
"fail");
272 return do_set(argv[2], argv[3], fail);
274 }
else if (argc >= 4) {
275 return do_set(argv[2], argv[3],
false);
278 PX4_ERR(
"not enough arguments.\nTry 'param set PARAM_NAME 3 [fail]'");
283 if (!strcmp(argv[1],
"compare")) {
284 if(argc >= 5 && !strcmp(argv[2],
"-s")) {
286 }
else if (argc >= 4) {
289 PX4_ERR(
"not enough arguments.\nTry 'param compare PARAM_NAME 3'");
294 if (!strcmp(argv[1],
"greater")) {
295 if(argc >= 5 && !strcmp(argv[2],
"-s")) {
297 }
else if (argc >= 4) {
300 PX4_ERR(
"not enough arguments.\nTry 'param greater PARAM_NAME 3'");
305 if (!strcmp(argv[1],
"reset")) {
307 return do_reset((
const char **) &argv[2], argc - 2);
314 if (!strcmp(argv[1],
"touch")) {
316 return do_touch((
const char **) &argv[2], argc - 2);
318 PX4_ERR(
"not enough arguments.");
323 if (!strcmp(argv[1],
"reset_nostart")) {
332 if (!strcmp(argv[1],
"index_used")) {
337 PX4_ERR(
"no index provided");
342 if (!strcmp(argv[1],
"index")) {
347 PX4_ERR(
"no index provided");
352 if (!strcmp(argv[1],
"find")) {
357 PX4_ERR(
"not enough arguments.\nTry 'param find PARAM_NAME'");
371 int fd = open(param_file_name, O_WRONLY | O_CREAT, PX4_O_MODE_666);
374 PX4_ERR(
"open '%s' failed (%i)", param_file_name, errno);
383 (void)unlink(param_file_name);
385 PX4_ERR(
"exporting to '%s' failed (%i)", param_file_name, result);
396 if (param_file_name) {
397 fd = open(param_file_name, O_RDONLY);
400 PX4_ERR(
"open '%s' failed (%i)", param_file_name, errno);
411 if (param_file_name) {
412 PX4_ERR(
"importing from '%s' failed (%i)", param_file_name, result);
414 PX4_ERR(
"importing failed (%i)", result);
426 if (param_file_name) {
427 fd = open(param_file_name, O_RDONLY);
430 PX4_ERR(
"open '%s' failed (%i)", param_file_name, errno);
441 if (param_file_name) {
442 PX4_ERR(
"importing from '%s' failed (%i)", param_file_name, result);
444 PX4_ERR(
"importing failed (%i)", result);
459 do_show(
const char *search_string,
bool only_changed)
461 PARAM_PRINT(
"Symbols: x = used, + = saved, * = unsaved\n");
471 PARAM_PRINT(
"Symbols: x = used, + = saved, * = unsaved\n");
518 PX4_ERR(
"Parameter %s not found", name);
522 PARAM_PRINT(
"Found param %s at index %i\n", name, (
int)ret);
530 int i = strtol(index, &end, 10);
543 PX4_ERR(
"param not found for index %u", i);
578 const char *search_string = (
const char *)arg;
579 const char *p_name = (
const char *)
param_name(param);
582 if (!(arg ==
nullptr)) {
585 const char *ss = search_string;
586 const char *pp = p_name;
589 while (*ss !=
'\0' && *pp !=
'\0') {
592 if (toupper(*ss) == *pp) {
596 }
else if (*ss ==
'*') {
597 if (*(ss + 1) !=
'\0') {
598 PX4_WARN(
"* symbol only allowed at end of search string");
612 if (!(*ss ==
'\0' || *ss ==
'*') || *pp !=
'\0') {
651 PARAM_PRINT(
"<error fetching parameter %lu>\n", (
unsigned long)param);
655 do_set(
const char *
name,
const char *val,
bool fail_on_not_found)
664 PX4_ERR(
"Parameter %s not found.", name);
665 return (fail_on_not_found) ? 1 : 0;
678 int32_t newval = strtol(val, &end, 10);
697 float newval = strtod(val, &end);
698 #pragma GCC diagnostic push 699 #pragma GCC diagnostic ignored "-Wfloat-equal" 702 #pragma GCC diagnostic pop 716 PX4_ERR(
"<unknown / unsupported type %d>\n", 0 +
param_type(param));
735 PX4_ERR(
"Parameter %s not found", name);
753 for (
unsigned k = 0; k < comparisons; k++) {
755 int j = strtol(vals[k], &end, 10);
759 PX4_DEBUG(
" %ld: ", (
long)i);
773 for (
unsigned k = 0; k < comparisons; k++) {
775 float g = strtod(vals[k], &end);
779 PX4_DEBUG(
" %4.4f: ", (
double)f);
788 PX4_ERR(
"<unknown / unsupported type %d>", 0 +
param_type(param));
793 PX4_DEBUG(
"%c %s: match\n",
804 if (num_excludes > 0) {
817 for (
int i = 0; i < num_params; ++i) {
819 PX4_ERR(
"param %s not found", params[i]);
834 if (num_excludes > 0) {
__EXPORT param_t param_find_no_notification(const char *name)
Look up a parameter by name.
#define PARAM_INVALID
Handle returned when a parameter cannot be found.
__EXPORT size_t param_size(param_t param)
Determine the size of a parameter.
static int do_show(const char *search_string, bool only_changed)
static int do_show_index(const char *index, bool used_index)
__EXPORT bool param_value_unsaved(param_t param)
Test whether a parameter's value has been changed but not saved.
__EXPORT int param_import(int fd)
Import parameters from a file, discarding any unrecognized parameters.
__EXPORT void param_foreach(void(*func)(void *arg, param_t param), void *arg, bool only_changed, bool only_used)
Apply a function to each parameter.
#define PARAM_TYPE_INT32
Parameter types.
__EXPORT void param_print_status(void)
Print the status of the param system.
__EXPORT int param_get(param_t param, void *val)
Copy the value of a parameter.
#define PARAM_TYPE_STRUCT
static void do_show_print(void *arg, param_t param)
__EXPORT int param_get_index(param_t param)
Look up the index of a parameter.
static int do_compare(const char *name, char *vals[], unsigned comparisons, enum COMPARE_OPERATOR cmd_op, enum COMPARE_ERROR_LEVEL err_level)
__EXPORT const char * param_get_default_file(void)
Get the default parameter file name.
__EXPORT int param_set(param_t param, const void *val)
Set the value of a parameter.
__EXPORT param_t param_for_index(unsigned index)
Look up a parameter by index.
__EXPORT bool param_value_is_default(param_t param)
Test whether a parameter's value has changed from the default.
__EXPORT int param_get_used_index(param_t param)
Look up the index of an used parameter.
__EXPORT unsigned param_count(void)
Return the total number of parameters.
Global flash based parameter store.
static int do_reset_nostart(const char *excludes[], int num_excludes)
__EXPORT const char * param_name(param_t param)
Obtain the name of a parameter.
__EXPORT void param_reset_excludes(const char *excludes[], int num_excludes)
Reset all parameters to their default values except for excluded parameters.
static int do_show_quiet(const char *param_name)
__EXPORT int param_export(int fd, bool only_unsaved)
Export changed parameters to a file.
__EXPORT unsigned param_count_used(void)
Return the actually used number of parameters.
__EXPORT int param_set_default_file(const char *filename)
Set the default parameter file name.
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
#define PARAM_TYPE_STRUCT_MAX
__BEGIN_DECLS __EXPORT int param_main(int argc, char *argv[])
__EXPORT param_type_t param_type(param_t param)
Obtain the type of a parameter.
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
__EXPORT int param_save_default(void)
Save parameters to the default file.
static int do_touch(const char *params[], int num_params)
static int do_import(const char *param_file_name)
__EXPORT param_t param_find(const char *name)
Look up a parameter by name.
static int do_reset(const char *excludes[], int num_excludes)
static int do_load(const char *param_file_name)
static int do_set(const char *name, const char *val, bool fail_on_not_found)
static int do_save(const char *param_file_name)
__EXPORT void param_reset_all(void)
Reset all parameters to their default values.
static void print_usage()
static int do_find(const char *name)
static int do_save_default()
__EXPORT param_t param_for_used_index(unsigned index)
Look up an used parameter by index.
__EXPORT int param_load(int fd)
Load parameters from a file.
uint32_t param_t
Parameter handle.
__EXPORT bool param_used(param_t param)
Wether a parameter is in use in the system.