From 5b4a77a67bbc5455e9248787989ed973d5605af2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Thu, 20 Dec 2018 11:16:10 +0100 Subject: [PATCH] params: add possibility to access files if flash-based params are enabled - the flash-backend is selected by specifying nullptr as file or -1 as fd - the default file is nullptr, and thus the FLASH - 'param select' has no effect, FLASH is always the default Thus there should be no functional change to existing setups. --- .../parameters/flashparams/flashparams.cpp | 6 +- src/lib/parameters/flashparams/flashparams.h | 2 +- src/lib/parameters/param.h | 9 +-- src/lib/parameters/parameters.cpp | 70 +++++++++++++------ src/systemcmds/param/param.cpp | 64 ++++++++--------- 5 files changed, 84 insertions(+), 67 deletions(-) diff --git a/src/lib/parameters/flashparams/flashparams.cpp b/src/lib/parameters/flashparams/flashparams.cpp index 6593a67c5b..5e5e61d373 100644 --- a/src/lib/parameters/flashparams/flashparams.cpp +++ b/src/lib/parameters/flashparams/flashparams.cpp @@ -353,9 +353,9 @@ out: return result; } -int flash_param_save() +int flash_param_save(bool only_unsaved) { - return param_export_internal(false); + return param_export_internal(only_unsaved); } int flash_param_load() @@ -366,5 +366,5 @@ int flash_param_load() int flash_param_import() { - return -1; + return param_import_internal(false); } diff --git a/src/lib/parameters/flashparams/flashparams.h b/src/lib/parameters/flashparams/flashparams.h index fd960d50b9..22c447f7a7 100644 --- a/src/lib/parameters/flashparams/flashparams.h +++ b/src/lib/parameters/flashparams/flashparams.h @@ -63,7 +63,7 @@ __EXPORT int param_set_external(param_t param, const void *val, bool mark_saved, __EXPORT const void *param_get_value_ptr_external(param_t param); /* The interface hooks to the Flash based storage. The caller is responsible for locking */ -__EXPORT int flash_param_save(); +__EXPORT int flash_param_save(bool only_unsaved); __EXPORT int flash_param_load(); __EXPORT int flash_param_import(); diff --git a/src/lib/parameters/param.h b/src/lib/parameters/param.h index 16dc48b7a2..1dc33c8989 100644 --- a/src/lib/parameters/param.h +++ b/src/lib/parameters/param.h @@ -309,7 +309,7 @@ __EXPORT void param_reset_excludes(const char *excludes[], int num_excludes); * Export changed parameters to a file. * Note: this method requires a large amount of stack size! * - * @param fd File descriptor to export to. + * @param fd File descriptor to export to (-1 selects the FLASH storage). * @param only_unsaved Only export changed parameters that have not yet been exported. * @return Zero on success, nonzero on failure. */ @@ -320,7 +320,7 @@ __EXPORT int param_export(int fd, bool only_unsaved); * * This function merges the imported parameters with the current parameter set. * - * @param fd File descriptor to import from. (Currently expected to be a file.) + * @param fd File descriptor to import from (-1 selects the FLASH storage). * @return Zero on success, nonzero if an error occurred during import. * Note that in the failure case, parameters may be inconsistent. */ @@ -332,7 +332,7 @@ __EXPORT int param_import(int fd); * This function resets all parameters to their default values, then loads new * values from a file. * - * @param fd File descriptor to import from. (Currently expected to be a file.) + * @param fd File descriptor to import from (-1 selects the FLASH storage). * @return Zero on success, nonzero if an error occurred during import. * Note that in the failure case, parameters may be inconsistent. */ @@ -356,8 +356,9 @@ __EXPORT void param_foreach(void (*func)(void *arg, param_t param), void *arg, /** * Set the default parameter file name. + * This has no effect if the FLASH-based storage is enabled. * - * @param filename Path to the default parameter file. The file is not require to + * @param filename Path to the default parameter file. The file is not required to * exist. * @return Zero on success. */ diff --git a/src/lib/parameters/parameters.cpp b/src/lib/parameters/parameters.cpp index e3b1f911dd..27fbe90684 100644 --- a/src/lib/parameters/parameters.cpp +++ b/src/lib/parameters/parameters.cpp @@ -69,9 +69,14 @@ #if defined(FLASH_BASED_PARAMS) #include "flashparams/flashparams.h" +static const char *param_default_file = nullptr; // nullptr means to store to FLASH +#else +inline static int flash_param_save(bool only_unsaved) { return -1; } +inline static int flash_param_load() { return -1; } +inline static int flash_param_import() { return -1; } +static const char *param_default_file = PX4_ROOTFSDIR"/eeprom/parameters"; #endif -static const char *param_default_file = PX4_ROOTFSDIR"/eeprom/parameters"; static char *param_user_file = nullptr; #ifdef __PX4_QURT @@ -921,6 +926,11 @@ param_reset_excludes(const char *excludes[], int num_excludes) int param_set_default_file(const char *filename) { +#ifdef FLASH_BASED_PARAMS + // the default for flash-based params is always the FLASH + (void)filename; +#else + if (param_user_file != nullptr) { // we assume this is not in use by some other thread free(param_user_file); @@ -931,6 +941,8 @@ param_set_default_file(const char *filename) param_user_file = strdup(filename); } +#endif /* FLASH_BASED_PARAMS */ + return 0; } @@ -944,10 +956,16 @@ int param_save_default() { int res = PX4_ERROR; -#if !defined(FLASH_BASED_PARAMS) const char *filename = param_get_default_file(); + if (!filename) { + param_lock_writer(); + res = flash_param_save(false); + param_unlock_writer(); + return res; + } + /* write parameters to temp file */ int fd = PARAM_OPEN(filename, O_WRONLY | O_CREAT, PX4_O_MODE_666); @@ -973,11 +991,6 @@ param_save_default() } PARAM_CLOSE(fd); -#else - param_lock_writer(); - res = flash_param_save(); - param_unlock_writer(); -#endif return res; } @@ -989,13 +1002,18 @@ int param_load_default() { int res = 0; -#if !defined(FLASH_BASED_PARAMS) - int fd_load = PARAM_OPEN(param_get_default_file(), O_RDONLY); + const char *filename = param_get_default_file(); + + if (!filename) { + return flash_param_load(); + } + + int fd_load = PARAM_OPEN(filename, O_RDONLY); if (fd_load < 0) { /* no parameter file is OK, otherwise this is an error */ if (errno != ENOENT) { - PX4_ERR("open '%s' for reading failed", param_get_default_file()); + PX4_ERR("open '%s' for reading failed", filename); return -1; } @@ -1006,25 +1024,29 @@ param_load_default() PARAM_CLOSE(fd_load); if (result != 0) { - PX4_ERR("error reading parameters from '%s'", param_get_default_file()); + PX4_ERR("error reading parameters from '%s'", filename); return -2; } -#else - // no need for locking - res = flash_param_load(); -#endif return res; } int param_export(int fd, bool only_unsaved) { + int result = -1; perf_begin(param_export_perf); - param_wbuf_s *s = nullptr; - int result = -1; + if (fd < 0) { + param_lock_writer(); + // flash_param_save() will take the shutdown lock + result = flash_param_save(only_unsaved); + param_unlock_writer(); + perf_end(param_export_perf); + return result; + } + param_wbuf_s *s = nullptr; struct bson_encoder_s encoder; int shutdown_lock_ret = px4_shutdown_lock(); @@ -1289,18 +1311,20 @@ param_import_internal(int fd, bool mark_saved) int param_import(int fd) { -#if !defined(FLASH_BASED_PARAMS) + if (fd < 0) { + return flash_param_import(); + } + return param_import_internal(fd, false); -#else - (void)fd; // unused - // no need for locking here - return flash_param_import(); -#endif } int param_load(int fd) { + if (fd < 0) { + return flash_param_load(); + } + param_reset_all_internal(false); return param_import_internal(fd, true); } diff --git a/src/systemcmds/param/param.cpp b/src/systemcmds/param/param.cpp index 4a692516eb..5daff12174 100644 --- a/src/systemcmds/param/param.cpp +++ b/src/systemcmds/param/param.cpp @@ -104,6 +104,10 @@ Parameters are automatically saved when changed, eg. with `param set`. They are or to the SD card. `param select` can be used to change the storage location for subsequent saves (this will need to be (re-)configured on every boot). +If the FLASH-based backend is enabled (which is done at compile time, e.g. for the Intel Aero or Omnibus), +`param select` has no effect and the default is always the FLASH backend. However `param save/load ` +can still be used to write to/read from files. + Each parameter has a 'used' flag, which is set when it's read during boot. It is used to only show relevant parameters to a ground control station. @@ -205,7 +209,10 @@ param_main(int argc, char *argv[]) param_set_default_file(nullptr); } - PX4_INFO("selected parameter default file %s", param_get_default_file()); + const char *default_file = param_get_default_file(); + if (default_file) { + PX4_INFO("selected parameter default file %s", default_file); + } return 0; } @@ -332,30 +339,6 @@ param_main(int argc, char *argv[]) return 1; } -#if defined(FLASH_BASED_PARAMS) -/* If flash based parameters are uses we have to change some of the calls to the - * default param calls, which will in turn take care of locking and calling to the - * flash backend. - */ -static int -do_save(const char *param_file_name) -{ - return param_save_default(); -} - -static int -do_load(const char *param_file_name) -{ - return param_load_default(); -} - -static int -do_import(const char *param_file_name) -{ - return param_import(-1); -} -#else - static int do_save(const char *param_file_name) { @@ -384,15 +367,20 @@ do_save(const char *param_file_name) static int do_load(const char *param_file_name) { - int fd = open(param_file_name, O_RDONLY); + 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; + if (fd < 0) { + PX4_ERR("open '%s' failed (%i)", param_file_name, errno); + return 1; + } } int result = param_load(fd); - close(fd); + if (fd >= 0) { + close(fd); + } if (result < 0) { PX4_ERR("importing from '%s' failed (%i)", param_file_name, result); @@ -405,15 +393,20 @@ do_load(const char *param_file_name) static int do_import(const char *param_file_name) { - int fd = open(param_file_name, O_RDONLY); + 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; + if (fd < 0) { + PX4_ERR("open '%s' failed (%i)", param_file_name, errno); + return 1; + } } int result = param_import(fd); - close(fd); + if (fd >= 0) { + close(fd); + } if (result < 0) { PX4_ERR("importing from '%s' failed (%i)", param_file_name, result); @@ -422,7 +415,6 @@ do_import(const char *param_file_name) return 0; } -#endif static int do_save_default()