diff options
author | Lorenz Meier <lm@inf.ethz.ch> | 2012-10-31 16:31:21 +0100 |
---|---|---|
committer | Lorenz Meier <lm@inf.ethz.ch> | 2012-10-31 16:31:21 +0100 |
commit | 0ddfd7c75c1f692fe83fcc88f832b42e2b04f0af (patch) | |
tree | e574326c207421f7cb8f3bd02d4fb4658823c253 /apps/systemlib | |
parent | 8dcde7f8cd72e73ced0ea534a84257ef43210ab6 (diff) | |
download | px4-firmware-0ddfd7c75c1f692fe83fcc88f832b42e2b04f0af.tar.gz px4-firmware-0ddfd7c75c1f692fe83fcc88f832b42e2b04f0af.tar.bz2 px4-firmware-0ddfd7c75c1f692fe83fcc88f832b42e2b04f0af.zip |
New param interface for microSD and EEPROM
Diffstat (limited to 'apps/systemlib')
-rw-r--r-- | apps/systemlib/param/param.c | 78 | ||||
-rw-r--r-- | apps/systemlib/param/param.h | 19 |
2 files changed, 97 insertions, 0 deletions
diff --git a/apps/systemlib/param/param.c b/apps/systemlib/param/param.c index 365bd3d19..c63e7ca8d 100644 --- a/apps/systemlib/param/param.c +++ b/apps/systemlib/param/param.c @@ -47,6 +47,7 @@ #include <fcntl.h> #include <unistd.h> #include <err.h> +#include <errno.h> #include <sys/stat.h> @@ -480,6 +481,83 @@ param_reset_all(void) param_notify_changes(); } +static char param_default_file_name[50] = "/eeprom/parameters"; + +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; + } + return 0; +} + +int +param_save_default(void) +{ + /* delete the file in case it exists */ + unlink(param_default_file_name); + + /* create the file */ + int fd = open(param_default_file_name, O_WRONLY | O_CREAT | O_EXCL); + + if (fd < 0) { + warn("opening '%s' for writing failed", param_default_file_name); + return -1; + } + + int result = param_export(fd, false); + /* should not be necessary, over-careful here */ + fsync(fd); + close(fd); + + if (result != 0) { + unlink(param_default_file_name); + warn("error exporting parameters to '%s'", param_default_file_name); + return -2; + } + + return 0; +} + +/** + * @return 0 on success, 1 if all params have not yet been stored, -1 if device open failed, -2 if writing parameters failed + */ +int +param_load_default(void) +{ + int fd = open(param_default_file_name, 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); + return -1; + } + return 1; + } + + int result = param_load(fd); + close(fd); + + if (result != 0) { + warn("error reading parameters from '%s'", param_default_file_name); + return -2; + } + + return 0; +} + int param_export(int fd, bool only_unsaved) { diff --git a/apps/systemlib/param/param.h b/apps/systemlib/param/param.h index 41e268db0..64bb77834 100644 --- a/apps/systemlib/param/param.h +++ b/apps/systemlib/param/param.h @@ -234,6 +234,25 @@ __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. + */ +__EXPORT int param_set_default_file(const char* filename); + +/** + * Import parameters from the default file name. + */ +__EXPORT int param_load_default(void); + + /* * Macros creating static parameter definitions. * |