PX4 Firmware
PX4 Autopilot Software http://px4.io
param.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2015 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file param.cpp
36  * @author Lorenz Meier <lorenz@px4.io>
37  * @author Andreas Antener <andreas@uaventure.com>
38  *
39  * Parameter tool.
40  */
41 
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>
46 
47 #include <errno.h>
48 #include <stdio.h>
49 #include <ctype.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <stdbool.h>
53 #include <unistd.h>
54 #include <fcntl.h>
55 #include <math.h>
56 #include <inttypes.h>
57 #include <sys/stat.h>
58 
59 #include <arch/board/board.h>
60 
61 #include <parameters/param.h>
62 #include "systemlib/err.h"
63 
65 __EXPORT int param_main(int argc, char *argv[]);
67 
68 enum class COMPARE_OPERATOR {
69  EQUAL = 0,
70  GREATER = 1,
71 };
72 
73 enum class COMPARE_ERROR_LEVEL {
74  DO_ERROR = 0,
75  SILENT = 1,
76 };
77 
78 
79 #ifdef __PX4_QURT
80 #define PARAM_PRINT PX4_INFO
81 #else
82 #define PARAM_PRINT PX4_INFO_RAW
83 #endif
84 
85 static int do_save(const char *param_file_name);
86 static int do_save_default();
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);
90 static int do_show_all();
91 static int do_show_quiet(const char *param_name);
92 static int do_show_index(const char *index, bool used_index);
93 static void do_show_print(void *arg, param_t param);
94 static int do_set(const char *name, const char *val, bool fail_on_not_found);
95 static int do_compare(const char *name, char *vals[], unsigned comparisons, enum COMPARE_OPERATOR cmd_op,
96  enum COMPARE_ERROR_LEVEL err_level);
97 static int do_reset(const char *excludes[], int num_excludes);
98 static int do_touch(const char *params[], int num_params);
99 static int do_reset_nostart(const char *excludes[], int num_excludes);
100 static int do_find(const char *name);
101 
102 static void print_usage()
103 {
104  PRINT_MODULE_DESCRIPTION(
105  R"DESCR_STR(
106 ### Description
107 Command to access and manipulate parameters via shell or script.
108 
109 This is used for example in the startup script to set airframe-specific parameters.
110 
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).
114 
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.
118 
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.
121 
122 ### Examples
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
126 $ reboot
127 )DESCR_STR");
128 
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);
136 
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);
139 
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);
145 
146  PRINT_MODULE_USAGE_COMMAND_DESCR("status", "Print status of parameter system");
147 
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);
151 
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);
155 
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);
160 
161  PRINT_MODULE_USAGE_ARG("<param_name> <value>", "Parameter name and value to compare", false);
162 
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);
165 
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);
171 
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);
178 }
179 
180 int
181 param_main(int argc, char *argv[])
182 {
183  if (argc >= 2) {
184  if (!strcmp(argv[1], "save")) {
185  if (argc >= 3) {
186  return do_save(argv[2]);
187 
188  } else {
189  int ret = do_save_default();
190 
191  if (ret) {
192  PX4_ERR("Param save failed (%i)", ret);
193  return 1;
194 
195  } else {
196  return 0;
197  }
198  }
199  }
200 
201  if (!strcmp(argv[1], "load")) {
202  if (argc >= 3) {
203  return do_load(argv[2]);
204 
205  } else {
207  }
208  }
209 
210  if (!strcmp(argv[1], "import")) {
211  if (argc >= 3) {
212  return do_import(argv[2]);
213 
214  } else {
216  }
217  }
218 
219  if (!strcmp(argv[1], "select")) {
220  if (argc >= 3) {
221  param_set_default_file(argv[2]);
222 
223  } else {
224  param_set_default_file(nullptr);
225  }
226 
227  const char *default_file = param_get_default_file();
228  if (default_file) {
229  PX4_INFO("selected parameter default file %s", default_file);
230  }
231  return 0;
232  }
233 
234  if (!strcmp(argv[1], "show")) {
235  if (argc >= 3) {
236  // optional argument -c to show only non-default params
237  if (!strcmp(argv[2], "-c")) {
238  if (argc >= 4) {
239  return do_show(argv[3], true);
240 
241  } else {
242  return do_show(nullptr, true);
243  }
244 
245  } else if (!strcmp(argv[2], "-a")) {
246  return do_show_all();
247 
248  } else if (!strcmp(argv[2], "-q")) {
249  if (argc >= 4) {
250  return do_show_quiet(argv[3]);
251  }
252  } else {
253  return do_show(argv[2], false);
254  }
255 
256  } else {
257  return do_show(nullptr, false);
258  }
259  }
260 
261  if (!strcmp(argv[1], "status")) {
263  return PX4_OK;
264  }
265 
266  if (!strcmp(argv[1], "set")) {
267  if (argc >= 5) {
268 
269  /* if the fail switch is provided, fails the command if not found */
270  bool fail = !strcmp(argv[4], "fail");
271 
272  return do_set(argv[2], argv[3], fail);
273 
274  } else if (argc >= 4) {
275  return do_set(argv[2], argv[3], false);
276 
277  } else {
278  PX4_ERR("not enough arguments.\nTry 'param set PARAM_NAME 3 [fail]'");
279  return 1;
280  }
281  }
282 
283  if (!strcmp(argv[1], "compare")) {
284  if(argc >= 5 && !strcmp(argv[2], "-s")) {
285  return do_compare(argv[3], &argv[4], argc - 4, COMPARE_OPERATOR::EQUAL, COMPARE_ERROR_LEVEL::SILENT);
286  } else if (argc >= 4) {
287  return do_compare(argv[2], &argv[3], argc - 3, COMPARE_OPERATOR::EQUAL, COMPARE_ERROR_LEVEL::DO_ERROR);
288  } else {
289  PX4_ERR("not enough arguments.\nTry 'param compare PARAM_NAME 3'");
290  return 1;
291  }
292  }
293 
294  if (!strcmp(argv[1], "greater")) {
295  if(argc >= 5 && !strcmp(argv[2], "-s")) {
296  return do_compare(argv[3], &argv[4], argc - 4, COMPARE_OPERATOR::GREATER, COMPARE_ERROR_LEVEL::SILENT);
297  } else if (argc >= 4) {
298  return do_compare(argv[2], &argv[3], argc - 3, COMPARE_OPERATOR::GREATER, COMPARE_ERROR_LEVEL::DO_ERROR);
299  } else {
300  PX4_ERR("not enough arguments.\nTry 'param greater PARAM_NAME 3'");
301  return 1;
302  }
303  }
304 
305  if (!strcmp(argv[1], "reset")) {
306  if (argc >= 3) {
307  return do_reset((const char **) &argv[2], argc - 2);
308 
309  } else {
310  return do_reset(nullptr, 0);
311  }
312  }
313 
314  if (!strcmp(argv[1], "touch")) {
315  if (argc >= 3) {
316  return do_touch((const char **) &argv[2], argc - 2);
317  } else {
318  PX4_ERR("not enough arguments.");
319  return 1;
320  }
321  }
322 
323  if (!strcmp(argv[1], "reset_nostart")) {
324  if (argc >= 3) {
325  return do_reset_nostart((const char **) &argv[2], argc - 2);
326 
327  } else {
328  return do_reset_nostart(nullptr, 0);
329  }
330  }
331 
332  if (!strcmp(argv[1], "index_used")) {
333  if (argc >= 3) {
334  return do_show_index(argv[2], true);
335 
336  } else {
337  PX4_ERR("no index provided");
338  return 1;
339  }
340  }
341 
342  if (!strcmp(argv[1], "index")) {
343  if (argc >= 3) {
344  return do_show_index(argv[2], false);
345 
346  } else {
347  PX4_ERR("no index provided");
348  return 1;
349  }
350  }
351 
352  if (!strcmp(argv[1], "find")) {
353  if (argc >= 3) {
354  return do_find(argv[2]);
355 
356  } else {
357  PX4_ERR("not enough arguments.\nTry 'param find PARAM_NAME'");
358  return 1;
359  }
360  }
361  }
362 
363  print_usage();
364  return 1;
365 }
366 
367 static int
368 do_save(const char *param_file_name)
369 {
370  /* create the file */
371  int fd = open(param_file_name, O_WRONLY | O_CREAT, PX4_O_MODE_666);
372 
373  if (fd < 0) {
374  PX4_ERR("open '%s' failed (%i)", param_file_name, errno);
375  return 1;
376  }
377 
378  int result = param_export(fd, false);
379  close(fd);
380 
381  if (result < 0) {
382 #ifndef __PX4_QURT
383  (void)unlink(param_file_name);
384 #endif
385  PX4_ERR("exporting to '%s' failed (%i)", param_file_name, result);
386  return 1;
387  }
388 
389  return 0;
390 }
391 
392 static int
393 do_load(const char *param_file_name)
394 {
395  int fd = -1;
396  if (param_file_name) { // passing NULL means to select the flash storage
397  fd = open(param_file_name, O_RDONLY);
398 
399  if (fd < 0) {
400  PX4_ERR("open '%s' failed (%i)", param_file_name, errno);
401  return 1;
402  }
403  }
404 
405  int result = param_load(fd);
406  if (fd >= 0) {
407  close(fd);
408  }
409 
410  if (result < 0) {
411  if (param_file_name) {
412  PX4_ERR("importing from '%s' failed (%i)", param_file_name, result);
413  } else {
414  PX4_ERR("importing failed (%i)", result);
415  }
416  return 1;
417  }
418 
419  return 0;
420 }
421 
422 static int
423 do_import(const char *param_file_name)
424 {
425  int fd = -1;
426  if (param_file_name) { // passing NULL means to select the flash storage
427  fd = open(param_file_name, O_RDONLY);
428 
429  if (fd < 0) {
430  PX4_ERR("open '%s' failed (%i)", param_file_name, errno);
431  return 1;
432  }
433  }
434 
435  int result = param_import(fd);
436  if (fd >= 0) {
437  close(fd);
438  }
439 
440  if (result < 0) {
441  if (param_file_name) {
442  PX4_ERR("importing from '%s' failed (%i)", param_file_name, result);
443  } else {
444  PX4_ERR("importing failed (%i)", result);
445  }
446  return 1;
447  }
448 
449  return 0;
450 }
451 
452 static int
454 {
455  return param_save_default();
456 }
457 
458 static int
459 do_show(const char *search_string, bool only_changed)
460 {
461  PARAM_PRINT("Symbols: x = used, + = saved, * = unsaved\n");
462  param_foreach(do_show_print, (char *)search_string, only_changed, true);
463  PARAM_PRINT("\n %u/%u parameters used.\n", param_count_used(), param_count());
464 
465  return 0;
466 }
467 
468 static int
470 {
471  PARAM_PRINT("Symbols: x = used, + = saved, * = unsaved\n");
472  param_foreach(do_show_print, nullptr, false, false);
473  PARAM_PRINT("\n %u parameters total, %u used.\n", param_count(), param_count_used());
474 
475  return 0;
476 }
477 
478 static int
480 {
481  param_t param = param_find_no_notification(param_name);
482  int32_t ii;
483  float ff;
484  // Print only the param value (can be used in scripts)
485 
486  if (param == PARAM_INVALID) {
487  return 1;
488  }
489 
490  switch (param_type(param)) {
491  case PARAM_TYPE_INT32:
492  if (!param_get(param, &ii)) {
493  PARAM_PRINT("%ld", (long)ii);
494  }
495 
496  break;
497 
498  case PARAM_TYPE_FLOAT:
499  if (!param_get(param, &ff)) {
500  PARAM_PRINT("%4.4f", (double)ff);
501  }
502 
503  break;
504 
505  default:
506  return 1;
507  }
508 
509  return 0;
510 }
511 
512 static int
513 do_find(const char *name)
514 {
516 
517  if (ret == PARAM_INVALID) {
518  PX4_ERR("Parameter %s not found", name);
519  return 1;
520  }
521 
522  PARAM_PRINT("Found param %s at index %i\n", name, (int)ret);
523  return 0;
524 }
525 
526 static int
527 do_show_index(const char *index, bool used_index)
528 {
529  char *end;
530  int i = strtol(index, &end, 10);
531  param_t param;
532  int32_t ii;
533  float ff;
534 
535  if (used_index) {
536  param = param_for_used_index(i);
537 
538  } else {
539  param = param_for_index(i);
540  }
541 
542  if (param == PARAM_INVALID) {
543  PX4_ERR("param not found for index %u", i);
544  return 1;
545  }
546 
547  PARAM_PRINT("index %d: %c %c %s [%d,%d] : ", i, (param_used(param) ? 'x' : ' '),
548  param_value_unsaved(param) ? '*' : (param_value_is_default(param) ? ' ' : '+'),
549  param_name(param), param_get_used_index(param), param_get_index(param));
550 
551  switch (param_type(param)) {
552  case PARAM_TYPE_INT32:
553  if (!param_get(param, &ii)) {
554  PARAM_PRINT("%ld\n", (long)ii);
555  }
556 
557  break;
558 
559  case PARAM_TYPE_FLOAT:
560  if (!param_get(param, &ff)) {
561  PARAM_PRINT("%4.4f\n", (double)ff);
562  }
563 
564  break;
565 
566  default:
567  PARAM_PRINT("<unknown type %d>\n", 0 + param_type(param));
568  }
569 
570  return 0;
571 }
572 
573 static void
574 do_show_print(void *arg, param_t param)
575 {
576  int32_t i;
577  float f;
578  const char *search_string = (const char *)arg;
579  const char *p_name = (const char *)param_name(param);
580 
581  /* print nothing if search string is invalid and not matching */
582  if (!(arg == nullptr)) {
583 
584  /* start search */
585  const char *ss = search_string;
586  const char *pp = p_name;
587 
588  /* XXX this comparison is only ok for trailing wildcards */
589  while (*ss != '\0' && *pp != '\0') {
590 
591  // case insensitive comparison (param_name is always upper case)
592  if (toupper(*ss) == *pp) {
593  ss++;
594  pp++;
595 
596  } else if (*ss == '*') {
597  if (*(ss + 1) != '\0') {
598  PX4_WARN("* symbol only allowed at end of search string");
599  // FIXME - should exit
600  return;
601  }
602 
603  pp++;
604 
605  } else {
606  /* param not found */
607  return;
608  }
609  }
610 
611  /* the search string must have been consumed */
612  if (!(*ss == '\0' || *ss == '*') || *pp != '\0') {
613  return;
614  }
615  }
616 
617  PARAM_PRINT("%c %c %s [%d,%d] : ", (param_used(param) ? 'x' : ' '),
618  param_value_unsaved(param) ? '*' : (param_value_is_default(param) ? ' ' : '+'),
619  param_name(param), param_get_used_index(param), param_get_index(param));
620 
621  /*
622  * This case can be expanded to handle printing common structure types.
623  */
624 
625  switch (param_type(param)) {
626  case PARAM_TYPE_INT32:
627  if (!param_get(param, &i)) {
628  PARAM_PRINT("%ld\n", (long)i);
629  return;
630  }
631 
632  break;
633 
634  case PARAM_TYPE_FLOAT:
635  if (!param_get(param, &f)) {
636  PARAM_PRINT("%4.4f\n", (double)f);
637  return;
638  }
639 
640  break;
641 
643  PARAM_PRINT("<struct type %d size %zu>\n", 0 + param_type(param), param_size(param));
644  return;
645 
646  default:
647  PARAM_PRINT("<unknown type %d>\n", 0 + param_type(param));
648  return;
649  }
650 
651  PARAM_PRINT("<error fetching parameter %lu>\n", (unsigned long)param);
652 }
653 
654 static int
655 do_set(const char *name, const char *val, bool fail_on_not_found)
656 {
657  int32_t i;
658  float f;
659  param_t param = param_find(name);
660 
661  /* set nothing if parameter cannot be found */
662  if (param == PARAM_INVALID) {
663  /* param not found - fail silenty in scripts as it prevents booting */
664  PX4_ERR("Parameter %s not found.", name);
665  return (fail_on_not_found) ? 1 : 0;
666  }
667 
668  /*
669  * Set parameter if type is known and conversion from string to value turns out fine
670  */
671 
672  switch (param_type(param)) {
673  case PARAM_TYPE_INT32:
674  if (!param_get(param, &i)) {
675 
676  /* convert string */
677  char *end;
678  int32_t newval = strtol(val, &end, 10);
679 
680  if (i != newval) {
681  PARAM_PRINT("%c %s: ",
682  param_value_unsaved(param) ? '*' : (param_value_is_default(param) ? ' ' : '+'),
683  param_name(param));
684  PARAM_PRINT("curr: %ld", (long)i);
685  param_set(param, &newval);
686  PARAM_PRINT(" -> new: %ld\n", (long)newval);
687  }
688  }
689 
690  break;
691 
692  case PARAM_TYPE_FLOAT:
693  if (!param_get(param, &f)) {
694 
695  /* convert string */
696  char *end;
697  float newval = strtod(val, &end);
698 #pragma GCC diagnostic push
699 #pragma GCC diagnostic ignored "-Wfloat-equal"
700 
701  if (f != newval) {
702 #pragma GCC diagnostic pop
703  PARAM_PRINT("%c %s: ",
704  param_value_unsaved(param) ? '*' : (param_value_is_default(param) ? ' ' : '+'),
705  param_name(param));
706  PARAM_PRINT("curr: %4.4f", (double)f);
707  param_set(param, &newval);
708  PARAM_PRINT(" -> new: %4.4f\n", (double)newval);
709  }
710 
711  }
712 
713  break;
714 
715  default:
716  PX4_ERR("<unknown / unsupported type %d>\n", 0 + param_type(param));
717  return 1;
718  }
719 
720  return 0;
721 }
722 
723 static int
724 do_compare(const char *name, char *vals[], unsigned comparisons, enum COMPARE_OPERATOR cmp_op, enum COMPARE_ERROR_LEVEL err_level)
725 {
726  int32_t i;
727  float f;
728  param_t param = param_find(name);
729 
730  /* set nothing if parameter cannot be found */
731  if (param == PARAM_INVALID) {
732  /* param not found */
733  if(err_level == COMPARE_ERROR_LEVEL::DO_ERROR)
734  {
735  PX4_ERR("Parameter %s not found", name);
736  }
737  return 1;
738  }
739 
740  /*
741  * Set parameter if type is known and conversion from string to value turns out fine
742  */
743 
744  int ret = 1;
745 
746  switch (param_type(param)) {
747  case PARAM_TYPE_INT32:
748  if (!param_get(param, &i)) {
749 
750  /* convert string */
751  char *end;
752 
753  for (unsigned k = 0; k < comparisons; k++) {
754 
755  int j = strtol(vals[k], &end, 10);
756 
757  if (((cmp_op == COMPARE_OPERATOR::EQUAL) && (i == j)) ||
758  ((cmp_op == COMPARE_OPERATOR::GREATER) && (i > j))) {
759  PX4_DEBUG(" %ld: ", (long)i);
760  ret = 0;
761  }
762  }
763  }
764 
765  break;
766 
767  case PARAM_TYPE_FLOAT:
768  if (!param_get(param, &f)) {
769 
770  /* convert string */
771  char *end;
772 
773  for (unsigned k = 0; k < comparisons; k++) {
774 
775  float g = strtod(vals[k], &end);
776 
777  if (((cmp_op == COMPARE_OPERATOR::EQUAL) && (fabsf(f - g) < 1e-7f)) ||
778  ((cmp_op == COMPARE_OPERATOR::GREATER) && (f > g))) {
779  PX4_DEBUG(" %4.4f: ", (double)f);
780  ret = 0;
781  }
782  }
783  }
784 
785  break;
786 
787  default:
788  PX4_ERR("<unknown / unsupported type %d>", 0 + param_type(param));
789  return 1;
790  }
791 
792  if (ret == 0) {
793  PX4_DEBUG("%c %s: match\n",
794  param_value_unsaved(param) ? '*' : (param_value_is_default(param) ? ' ' : '+'),
795  param_name(param));
796  }
797 
798  return ret;
799 }
800 
801 static int
802 do_reset(const char *excludes[], int num_excludes)
803 {
804  if (num_excludes > 0) {
805  param_reset_excludes(excludes, num_excludes);
806 
807  } else {
808  param_reset_all();
809  }
810 
811  return 0;
812 }
813 
814 static int
815 do_touch(const char *params[], int num_params)
816 {
817  for (int i = 0; i < num_params; ++i) {
818  if (param_find(params[i]) == PARAM_INVALID) {
819  PX4_ERR("param %s not found", params[i]);
820  }
821  }
822  return 0;
823 }
824 
825 static int
826 do_reset_nostart(const char *excludes[], int num_excludes)
827 {
828  int32_t autostart;
829  int32_t autoconfig;
830 
831  (void)param_get(param_find("SYS_AUTOSTART"), &autostart);
832  (void)param_get(param_find("SYS_AUTOCONFIG"), &autoconfig);
833 
834  if (num_excludes > 0) {
835  param_reset_excludes(excludes, num_excludes);
836 
837  } else {
838  param_reset_all();
839  }
840 
841  (void)param_set(param_find("SYS_AUTOSTART"), &autostart);
842  (void)param_set(param_find("SYS_AUTOCONFIG"), &autoconfig);
843 
844  return 0;
845 }
__EXPORT param_t param_find_no_notification(const char *name)
Look up a parameter by name.
Definition: parameters.cpp:376
#define PARAM_INVALID
Handle returned when a parameter cannot be found.
Definition: param.h:103
__EXPORT size_t param_size(param_t param)
Determine the size of a parameter.
Definition: parameters.cpp:525
static int do_show(const char *search_string, bool only_changed)
Definition: param.cpp:459
static int do_show_index(const char *index, bool used_index)
Definition: param.cpp:527
__EXPORT bool param_value_unsaved(param_t param)
Test whether a parameter&#39;s value has been changed but not saved.
Definition: parameters.cpp:508
__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 __END_DECLS
Definition: visibility.h:59
#define PARAM_TYPE_INT32
Parameter types.
Definition: param.h:60
__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.
Definition: parameters.cpp:589
COMPARE_ERROR_LEVEL
Definition: param.cpp:73
#define PARAM_TYPE_STRUCT
Definition: param.h:62
static void do_show_print(void *arg, param_t param)
Definition: param.cpp:574
__EXPORT int param_get_index(param_t param)
Look up the index of a parameter.
Definition: parameters.cpp:449
static int do_compare(const char *name, char *vals[], unsigned comparisons, enum COMPARE_OPERATOR cmd_op, enum COMPARE_ERROR_LEVEL err_level)
Definition: param.cpp:724
__EXPORT const char * param_get_default_file(void)
Get the default parameter file name.
Definition: parameters.cpp:968
__EXPORT int param_set(param_t param, const void *val)
Set the value of a parameter.
Definition: parameters.cpp:814
Definition: I2C.hpp:51
__EXPORT param_t param_for_index(unsigned index)
Look up a parameter by index.
Definition: parameters.cpp:408
__EXPORT bool param_value_is_default(param_t param)
Test whether a parameter&#39;s value has changed from the default.
Definition: parameters.cpp:498
static int do_show_all()
Definition: param.cpp:469
__EXPORT int param_get_used_index(param_t param)
Look up the index of an used parameter.
Definition: parameters.cpp:459
__EXPORT unsigned param_count(void)
Return the total number of parameters.
Definition: parameters.cpp:382
Global flash based parameter store.
static int do_reset_nostart(const char *excludes[], int num_excludes)
Definition: param.cpp:826
__EXPORT const char * param_name(param_t param)
Obtain the name of a parameter.
Definition: parameters.cpp:486
__EXPORT void param_reset_excludes(const char *excludes[], int num_excludes)
Reset all parameters to their default values except for excluded parameters.
Definition: parameters.cpp:916
static int do_show_quiet(const char *param_name)
Definition: param.cpp:479
__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.
Definition: parameters.cpp:388
#define __BEGIN_DECLS
Definition: visibility.h:58
__EXPORT int param_set_default_file(const char *filename)
Set the default parameter file name.
Definition: parameters.cpp:945
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
#define PARAM_TYPE_STRUCT_MAX
Definition: param.h:63
__BEGIN_DECLS __EXPORT int param_main(int argc, char *argv[])
Definition: param.cpp:181
#define PARAM_PRINT
Definition: param.cpp:82
__EXPORT param_type_t param_type(param_t param)
Obtain the type of a parameter.
Definition: parameters.cpp:519
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.
Definition: parameters.cpp:974
int fd
Definition: dataman.cpp:146
static int do_touch(const char *params[], int num_params)
Definition: param.cpp:815
static int do_import(const char *param_file_name)
Definition: param.cpp:423
__EXPORT param_t param_find(const char *name)
Look up a parameter by name.
Definition: parameters.cpp:370
static int do_reset(const char *excludes[], int num_excludes)
Definition: param.cpp:802
const char * name
Definition: tests_main.c:58
static int do_load(const char *param_file_name)
Definition: param.cpp:393
#define PARAM_TYPE_FLOAT
Definition: param.h:61
static int do_set(const char *name, const char *val, bool fail_on_not_found)
Definition: param.cpp:655
static int do_save(const char *param_file_name)
Definition: param.cpp:368
__EXPORT void param_reset_all(void)
Reset all parameters to their default values.
Definition: parameters.cpp:910
static void print_usage()
Definition: param.cpp:102
static int do_find(const char *name)
Definition: param.cpp:513
static int do_save_default()
Definition: param.cpp:453
__EXPORT param_t param_for_used_index(unsigned index)
Look up an used parameter by index.
Definition: parameters.cpp:420
__EXPORT int param_load(int fd)
Load parameters from a file.
uint32_t param_t
Parameter handle.
Definition: param.h:98
COMPARE_OPERATOR
Definition: param.cpp:68
__EXPORT bool param_used(param_t param)
Wether a parameter is in use in the system.
Definition: parameters.cpp:826