diff options
author | px4dev <px4@purgatory.org> | 2012-08-28 22:15:25 -0700 |
---|---|---|
committer | px4dev <px4@purgatory.org> | 2012-08-28 22:15:25 -0700 |
commit | 1a781c6c4a025b06ce8db34473838e8d3891af3a (patch) | |
tree | ede126461d567283533fe410bab6dcc9a2259ad4 | |
parent | f0286d1a10343c573fac6382e3c2cb0b714a1f72 (diff) | |
download | px4-firmware-1a781c6c4a025b06ce8db34473838e8d3891af3a.tar.gz px4-firmware-1a781c6c4a025b06ce8db34473838e8d3891af3a.tar.bz2 px4-firmware-1a781c6c4a025b06ce8db34473838e8d3891af3a.zip |
Make the distinction between "parameter import" which merges parameters, and "parameter load" which blows away any current changes.
-rw-r--r-- | apps/systemcmds/eeprom/eeprom.c | 2 | ||||
-rw-r--r-- | apps/systemlib/param/param.c | 72 | ||||
-rw-r--r-- | apps/systemlib/param/param.h | 34 |
3 files changed, 94 insertions, 14 deletions
diff --git a/apps/systemcmds/eeprom/eeprom.c b/apps/systemcmds/eeprom/eeprom.c index 4a641dd0a..6d1037a13 100644 --- a/apps/systemcmds/eeprom/eeprom.c +++ b/apps/systemcmds/eeprom/eeprom.c @@ -216,7 +216,7 @@ eeprom_load(const char *name) if (fd < 0) err(1, "open '%s'", name); - int result = param_import(fd); + int result = param_load(fd); close(fd); if (result < 0) diff --git a/apps/systemlib/param/param.c b/apps/systemlib/param/param.c index b69efb77c..770b44994 100644 --- a/apps/systemlib/param/param.c +++ b/apps/systemlib/param/param.c @@ -178,6 +178,22 @@ param_find_changed(param_t param) { return s; } +static void +param_notify_changes(void) +{ + struct parameter_update_s pup = { .timestamp = hrt_absolute_time() }; + + /* + * If we don't have a handle to our topic, create one now; otherwise + * just publish. + */ + if (param_topic == -1) { + param_topic = orb_advertise(ORB_ID(parameter_update), &pup); + } else { + orb_publish(ORB_ID(parameter_update), param_topic, &pup); + } +} + param_t param_find(const char *name) { @@ -390,22 +406,48 @@ out: * If we set something, now that we have unlocked, go ahead and advertise that * a thing has been set. */ - if (params_changed) { - struct parameter_update_s pup = { .timestamp = hrt_absolute_time() }; + if (params_changed) + param_notify_changes(); - /* - * If we don't have a handle to our topic, create one now; otherwise - * just publish. - */ - if (param_topic == -1) { - param_topic = orb_advertise(ORB_ID(parameter_update), &pup); - } else { - orb_publish(ORB_ID(parameter_update), param_topic, &pup); + return result; +} + +void +param_reset(param_t param) +{ + struct param_wbuf_s *s = NULL; + + param_lock(); + + if (handle_in_range(param)) { + + /* look for a saved value */ + s = param_find_changed(param); + + /* if we found one, erase it */ + if (s != NULL) { + int pos = utarry_eltidx(param_values, s); + utarray_erase(param_values, pos, 1); } + } + param_unlock(); + + if (s != NULL) + param_notify_changes(); +} + +void +param_reset_all(void) +{ + param_lock(); + if (param_values != NULL) { + utarray_free(param_values); } - return result; + param_unlock(); + + param_notify_changes(); } int @@ -619,9 +661,17 @@ param_import(int fd) out: if (result < 0) debug("BSON error decoding parameters"); + return result; } +int +param_load(int fd) +{ + param_reset_all(); + return param_import(fd); +} + void param_foreach(void (*func)(void *arg, param_t param), void *arg, bool only_changed) { diff --git a/apps/systemlib/param/param.h b/apps/systemlib/param/param.h index 16ab7f5cd..cd94455f8 100644 --- a/apps/systemlib/param/param.h +++ b/apps/systemlib/param/param.h @@ -148,16 +148,32 @@ __EXPORT size_t param_size(param_t param) __attribute__((const)); __EXPORT int param_get(param_t param, void *val); /** - * Set the scalar value of a parameter. + * Set the value of a parameter. * * @param param A handle returned by param_find or passed by param_foreach. * @param val The value to set; assumed to point to a variable of the parameter type. - * For structures, the pointer is assumed to point to a copy of the structure. + * For structures, the pointer is assumed to point to a structure to be copied. * @return Zero if the parameter's value could be set from a scalar, nonzero otherwise. */ __EXPORT int param_set(param_t param, const void *val); /** + * Reset a parameter to its default value. + * + * This function frees any storage used by struct parameters, but scalar parameters + * + * @param param A handle returned by param_find or passed by param_foreach. + */ +__EXPORT void param_reset(param_t param); + +/** + * Reset all parameters to their default values. + * + * This function also releases the storage used by struct parameters. + */ +__EXPORT void param_reset_all(void); + +/** * Export changed parameters to a file. * * @param fd File descriptor to export to. @@ -169,6 +185,8 @@ __EXPORT int param_export(int fd, bool only_unsaved); /** * Import parameters from a file, discarding any unrecognized parameters. * + * This function merges the imported parameters with the current parameter set. + * * @param fd File descriptor to import from. (Currently expected to be a file.) * @return Zero on success, nonzero if an error occurred during import. * Note that in the failure case, parameters may be inconsistent. @@ -176,6 +194,18 @@ __EXPORT int param_export(int fd, bool only_unsaved); __EXPORT int param_import(int fd); /** + * Load parameters from a file. + * + * 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.) + * @return Zero on success, nonzero if an error occurred during import. + * Note that in the failure case, parameters may be inconsistent. + */ +__EXPORT int param_load(int fd); + +/** * Apply a function to each parameter. * * Note that the parameter set is not locked during the traversal. It also does |