From 8bfceef89cc1fd2422863a99d99039d18a1301bc Mon Sep 17 00:00:00 2001 From: px4dev Date: Wed, 31 Oct 2012 12:59:24 -0700 Subject: [PATCH] Remove the arbitrary limit on the path to the default parameter file. Add a verb to the param command to set the default parameter file. --- apps/systemcmds/param/param.c | 21 ++++++++++++------ apps/systemlib/param/param.c | 42 +++++++++++++++++------------------ apps/systemlib/param/param.h | 35 ++++++++++++++++++++--------- 3 files changed, 60 insertions(+), 38 deletions(-) diff --git a/apps/systemcmds/param/param.c b/apps/systemcmds/param/param.c index 9cb280933e..92313e45a6 100644 --- a/apps/systemcmds/param/param.c +++ b/apps/systemcmds/param/param.c @@ -62,8 +62,6 @@ static void do_import(const char* param_file_name); static void do_show(void); static void do_show_print(void *arg, param_t param); -static const char *param_file_name_default = "/eeprom/parameters"; - int param_main(int argc, char *argv[]) { @@ -72,7 +70,7 @@ param_main(int argc, char *argv[]) if (argc >= 3) { do_save(argv[2]); } else { - do_save(param_file_name_default); + do_save(param_get_default_file()); } } @@ -80,7 +78,7 @@ param_main(int argc, char *argv[]) if (argc >= 3) { do_load(argv[2]); } else { - do_load(param_file_name_default); + do_load(param_get_default_file()); } } @@ -88,17 +86,26 @@ param_main(int argc, char *argv[]) if (argc >= 3) { do_import(argv[2]); } else { - do_import(param_file_name_default); + do_import(param_get_default_file()); } } + if (!strcmp(argv[1], "select")) { + if (argc >= 3) { + param_set_default_file(argv[2]); + } else { + param_set_default_file(NULL); + } + warnx("selected parameter file %s", param_get_default_file()); + } + if (!strcmp(argv[1], "show")) { do_show(); } } - - errx(1, "expected a command, try 'load', 'import', 'show' or 'save'\n"); + + errx(1, "expected a command, try 'load', 'import', 'show', 'select' or 'save'"); } static void diff --git a/apps/systemlib/param/param.c b/apps/systemlib/param/param.c index c63e7ca8df..9a00c91a5d 100644 --- a/apps/systemlib/param/param.c +++ b/apps/systemlib/param/param.c @@ -481,38 +481,38 @@ param_reset_all(void) param_notify_changes(); } -static char param_default_file_name[50] = "/eeprom/parameters"; +static const char *param_default_file = "/eeprom/parameters"; +static char *param_user_file; int param_set_default_file(const char* filename) { - if (filename) { - if (strlen(filename) < sizeof(param_default_file_name)) - { - strcpy(param_default_file_name, filename); - } else { - warnx("param file name too long"); - return 1; - } - return 0; - } else { - warnx("no valid param file name"); - return 1; + if (param_user_file != NULL) { + free(param_user_file); + param_user_file = NULL; } + if (filename) + param_user_file = strdup(filename); return 0; } +const char * +param_get_default_file(void) +{ + return (param_user_file != NULL) ? param_user_file : param_default_file; +} + int param_save_default(void) { /* delete the file in case it exists */ - unlink(param_default_file_name); + unlink(param_get_default_file()); /* create the file */ - int fd = open(param_default_file_name, O_WRONLY | O_CREAT | O_EXCL); + int fd = open(param_get_default_file(), O_WRONLY | O_CREAT | O_EXCL); if (fd < 0) { - warn("opening '%s' for writing failed", param_default_file_name); + warn("opening '%s' for writing failed", param_get_default_file()); return -1; } @@ -522,8 +522,8 @@ param_save_default(void) close(fd); if (result != 0) { - unlink(param_default_file_name); - warn("error exporting parameters to '%s'", param_default_file_name); + unlink(param_get_default_file()); + warn("error exporting parameters to '%s'", param_get_default_file()); return -2; } @@ -536,12 +536,12 @@ param_save_default(void) int param_load_default(void) { - int fd = open(param_default_file_name, O_RDONLY); + int fd = open(param_get_default_file(), O_RDONLY); if (fd < 0) { /* no parameter file is OK, otherwise this is an error */ if (errno != ENOENT) { - warn("open '%s' for reading failed", param_default_file_name); + warn("open '%s' for reading failed", param_get_default_file()); return -1; } return 1; @@ -551,7 +551,7 @@ param_load_default(void) close(fd); if (result != 0) { - warn("error reading parameters from '%s'", param_default_file_name); + warn("error reading parameters from '%s'", param_get_default_file()); return -2; } diff --git a/apps/systemlib/param/param.h b/apps/systemlib/param/param.h index 64bb77834e..6fa73b5a48 100644 --- a/apps/systemlib/param/param.h +++ b/apps/systemlib/param/param.h @@ -234,25 +234,40 @@ __EXPORT int param_load(int fd); */ __EXPORT void param_foreach(void (*func)(void *arg, param_t param), void *arg, bool only_changed); -/** - * Export parameters to the default file name. - * - * - * @param - */ -__EXPORT int param_save_default(void); - /** * Set the default parameter file name. + * + * @param filename Path to the default parameter file. The file is not require to + * exist. + * @return Zero on success. */ __EXPORT int param_set_default_file(const char* filename); /** - * Import parameters from the default file name. + * Get the default parameter file name. + * + * @return The path to the current default parameter file; either as + * a result of a call to param_set_default_file, or the + * built-in default. + */ +__EXPORT const char *param_get_default_file(void); + +/** + * Save parameters to the default file. + * + * This function saves all parameters with non-default values. + * + * @return Zero on success. + */ +__EXPORT int param_save_default(void); + +/** + * Load parameters from the default parameter file. + * + * @return Zero on success. */ __EXPORT int param_load_default(void); - /* * Macros creating static parameter definitions. *