mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-11-07 02:41:37 +08:00
param_export: add optional filter method
This commit is contained in:
@@ -76,7 +76,7 @@ struct param_wbuf_s {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static int
|
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 param_wbuf_s *s = nullptr;
|
||||||
struct bson_encoder_s encoder;
|
struct bson_encoder_s encoder;
|
||||||
@@ -105,6 +105,10 @@ param_export_internal(bool only_unsaved)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (filter && !filter(s->param)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
s->unsaved = false;
|
s->unsaved = false;
|
||||||
|
|
||||||
/* append the appropriate BSON type object */
|
/* append the appropriate BSON type object */
|
||||||
@@ -345,9 +349,9 @@ out:
|
|||||||
return result;
|
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()
|
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);
|
__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 */
|
/* 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_load();
|
||||||
__EXPORT int flash_param_import();
|
__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);
|
__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.
|
* 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 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 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.
|
* @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.
|
* Import parameters from a file, discarding any unrecognized parameters.
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ using namespace time_literals;
|
|||||||
#include "flashparams/flashparams.h"
|
#include "flashparams/flashparams.h"
|
||||||
static const char *param_default_file = nullptr; // nullptr means to store to FLASH
|
static const char *param_default_file = nullptr; // nullptr means to store to FLASH
|
||||||
#else
|
#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_load() { return -1; }
|
||||||
inline static int flash_param_import() { return -1; }
|
inline static int flash_param_import() { return -1; }
|
||||||
static const char *param_default_file = PX4_ROOTFSDIR"/eeprom/parameters";
|
static const char *param_default_file = PX4_ROOTFSDIR"/eeprom/parameters";
|
||||||
@@ -948,7 +948,7 @@ int param_save_default()
|
|||||||
if (!filename) {
|
if (!filename) {
|
||||||
perf_begin(param_export_perf);
|
perf_begin(param_export_perf);
|
||||||
param_lock_writer();
|
param_lock_writer();
|
||||||
res = flash_param_save(false);
|
res = flash_param_save(false, nullptr);
|
||||||
param_unlock_writer();
|
param_unlock_writer();
|
||||||
perf_end(param_export_perf);
|
perf_end(param_export_perf);
|
||||||
return res;
|
return res;
|
||||||
@@ -976,7 +976,7 @@ int param_save_default()
|
|||||||
int attempts = 5;
|
int attempts = 5;
|
||||||
|
|
||||||
while (res != OK && attempts > 0) {
|
while (res != OK && attempts > 0) {
|
||||||
res = param_export(fd, false);
|
res = param_export(fd, false, nullptr);
|
||||||
attempts--;
|
attempts--;
|
||||||
|
|
||||||
if (res != PX4_OK) {
|
if (res != PX4_OK) {
|
||||||
@@ -1035,7 +1035,7 @@ param_load_default()
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
param_export(int fd, bool only_unsaved)
|
param_export(int fd, bool only_unsaved, param_filter_func filter)
|
||||||
{
|
{
|
||||||
int result = -1;
|
int result = -1;
|
||||||
perf_begin(param_export_perf);
|
perf_begin(param_export_perf);
|
||||||
@@ -1043,7 +1043,7 @@ param_export(int fd, bool only_unsaved)
|
|||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
param_lock_writer();
|
param_lock_writer();
|
||||||
// flash_param_save() will take the shutdown lock
|
// flash_param_save() will take the shutdown lock
|
||||||
result = flash_param_save(only_unsaved);
|
result = flash_param_save(only_unsaved, filter);
|
||||||
param_unlock_writer();
|
param_unlock_writer();
|
||||||
perf_end(param_export_perf);
|
perf_end(param_export_perf);
|
||||||
return result;
|
return result;
|
||||||
@@ -1081,6 +1081,10 @@ param_export(int fd, bool only_unsaved)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (filter && !filter(s->param)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
s->unsaved = false;
|
s->unsaved = false;
|
||||||
|
|
||||||
const char *name = param_name(s->param);
|
const char *name = param_name(s->param);
|
||||||
|
|||||||
@@ -991,7 +991,7 @@ param_save_default()
|
|||||||
goto do_exit;
|
goto do_exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
res = param_export(fd, false);
|
res = param_export(fd, false, nullptr);
|
||||||
|
|
||||||
if (res != OK) {
|
if (res != OK) {
|
||||||
PX4_ERR("failed to write parameters to file: %s", filename);
|
PX4_ERR("failed to write parameters to file: %s", filename);
|
||||||
@@ -1088,7 +1088,7 @@ param_load_default_no_notify()
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
param_export(int fd, bool only_unsaved)
|
param_export(int fd, bool only_unsaved, param_filter_func filter)
|
||||||
{
|
{
|
||||||
perf_begin(param_export_perf);
|
perf_begin(param_export_perf);
|
||||||
|
|
||||||
@@ -1129,6 +1129,10 @@ param_export(int fd, bool only_unsaved)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (filter && !filter(s->param)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
s->unsaved = false;
|
s->unsaved = false;
|
||||||
|
|
||||||
/* Make sure to get latest from shmem before saving. */
|
/* Make sure to get latest from shmem before saving. */
|
||||||
|
|||||||
@@ -381,7 +381,7 @@ do_save(const char *param_file_name)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int result = param_export(fd, false);
|
int result = param_export(fd, false, nullptr);
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user