mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-05-24 15:40:31 +08:00
param_export: add optional filter method
This commit is contained in:
@@ -76,7 +76,7 @@ struct param_wbuf_s {
|
||||
};
|
||||
|
||||
static int
|
||||
param_export_internal(bool only_unsaved)
|
||||
param_export_internal(bool only_unsaved, param_filter_func filter)
|
||||
{
|
||||
struct param_wbuf_s *s = nullptr;
|
||||
struct bson_encoder_s encoder;
|
||||
@@ -105,6 +105,10 @@ param_export_internal(bool only_unsaved)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (filter && !filter(s->param)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
s->unsaved = false;
|
||||
|
||||
/* append the appropriate BSON type object */
|
||||
@@ -345,9 +349,9 @@ out:
|
||||
return result;
|
||||
}
|
||||
|
||||
int flash_param_save(bool only_unsaved)
|
||||
int flash_param_save(bool only_unsaved, param_filter_func filter)
|
||||
{
|
||||
return param_export_internal(only_unsaved);
|
||||
return param_export_internal(only_unsaved, filter);
|
||||
}
|
||||
|
||||
int flash_param_load()
|
||||
|
||||
@@ -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(bool only_unsaved);
|
||||
__EXPORT int flash_param_save(bool only_unsaved, param_filter_func filter);
|
||||
__EXPORT int flash_param_load();
|
||||
__EXPORT int flash_param_import();
|
||||
|
||||
|
||||
@@ -278,6 +278,8 @@ __EXPORT void param_reset_all(void);
|
||||
*/
|
||||
__EXPORT void param_reset_excludes(const char *excludes[], int num_excludes);
|
||||
|
||||
typedef bool(*param_filter_func)(param_t handle);
|
||||
|
||||
/**
|
||||
* Reset only specific parameters to their default values.
|
||||
*
|
||||
@@ -294,9 +296,11 @@ __EXPORT void param_reset_specific(const char *resets[], int num_resets);
|
||||
*
|
||||
* @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.
|
||||
* @param filter Filter parameters to be exported. The method should return true if
|
||||
* the parameter should be exported. No filtering if nullptr is passed.
|
||||
* @return Zero on success, nonzero on failure.
|
||||
*/
|
||||
__EXPORT int param_export(int fd, bool only_unsaved);
|
||||
__EXPORT int param_export(int fd, bool only_unsaved, param_filter_func filter);
|
||||
|
||||
/**
|
||||
* Import parameters from a file, discarding any unrecognized parameters.
|
||||
|
||||
@@ -71,7 +71,7 @@ using namespace time_literals;
|
||||
#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_save(bool only_unsaved, param_filter_func filter) { 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";
|
||||
@@ -948,7 +948,7 @@ int param_save_default()
|
||||
if (!filename) {
|
||||
perf_begin(param_export_perf);
|
||||
param_lock_writer();
|
||||
res = flash_param_save(false);
|
||||
res = flash_param_save(false, nullptr);
|
||||
param_unlock_writer();
|
||||
perf_end(param_export_perf);
|
||||
return res;
|
||||
@@ -976,7 +976,7 @@ int param_save_default()
|
||||
int attempts = 5;
|
||||
|
||||
while (res != OK && attempts > 0) {
|
||||
res = param_export(fd, false);
|
||||
res = param_export(fd, false, nullptr);
|
||||
attempts--;
|
||||
|
||||
if (res != PX4_OK) {
|
||||
@@ -1035,7 +1035,7 @@ param_load_default()
|
||||
}
|
||||
|
||||
int
|
||||
param_export(int fd, bool only_unsaved)
|
||||
param_export(int fd, bool only_unsaved, param_filter_func filter)
|
||||
{
|
||||
int result = -1;
|
||||
perf_begin(param_export_perf);
|
||||
@@ -1043,7 +1043,7 @@ param_export(int fd, bool only_unsaved)
|
||||
if (fd < 0) {
|
||||
param_lock_writer();
|
||||
// flash_param_save() will take the shutdown lock
|
||||
result = flash_param_save(only_unsaved);
|
||||
result = flash_param_save(only_unsaved, filter);
|
||||
param_unlock_writer();
|
||||
perf_end(param_export_perf);
|
||||
return result;
|
||||
@@ -1081,6 +1081,10 @@ param_export(int fd, bool only_unsaved)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (filter && !filter(s->param)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
s->unsaved = false;
|
||||
|
||||
const char *name = param_name(s->param);
|
||||
|
||||
@@ -991,7 +991,7 @@ param_save_default()
|
||||
goto do_exit;
|
||||
}
|
||||
|
||||
res = param_export(fd, false);
|
||||
res = param_export(fd, false, nullptr);
|
||||
|
||||
if (res != OK) {
|
||||
PX4_ERR("failed to write parameters to file: %s", filename);
|
||||
@@ -1088,7 +1088,7 @@ param_load_default_no_notify()
|
||||
}
|
||||
|
||||
int
|
||||
param_export(int fd, bool only_unsaved)
|
||||
param_export(int fd, bool only_unsaved, param_filter_func filter)
|
||||
{
|
||||
perf_begin(param_export_perf);
|
||||
|
||||
@@ -1129,6 +1129,10 @@ param_export(int fd, bool only_unsaved)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (filter && !filter(s->param)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
s->unsaved = false;
|
||||
|
||||
/* Make sure to get latest from shmem before saving. */
|
||||
|
||||
@@ -381,7 +381,7 @@ do_save(const char *param_file_name)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int result = param_export(fd, false);
|
||||
int result = param_export(fd, false, nullptr);
|
||||
close(fd);
|
||||
|
||||
if (result < 0) {
|
||||
|
||||
Reference in New Issue
Block a user