45 #include <parameters/px4_parameters.h> 54 #include <px4_platform_common/px4_config.h> 55 #include <px4_platform_common/defines.h> 56 #include <px4_platform_common/posix.h> 57 #include <px4_platform_common/sem.h> 58 #include <px4_platform_common/shutdown.h> 66 #if !defined(PARAM_NO_ORB) 73 #if defined(FLASH_BASED_PARAMS) 86 #define PARAM_OPEN px4_open 87 #define PARAM_CLOSE px4_close 89 #define PARAM_OPEN open 90 #define PARAM_CLOSE close 93 #ifndef PARAM_NO_AUTOSAVE 94 #include <px4_platform_common/workqueue.h> 106 #define param_info_count px4_parameters.param_count 149 #if !defined(PARAM_NO_ORB) 186 do {}
while (px4_sem_wait(&
param_sem) != 0);
196 do {}
while (px4_sem_wait(&
param_sem) != 0);
252 return (count && param < count);
303 #if !defined(PARAM_NO_ORB) 339 while (front <= last) {
340 middle = front + (last - front) / 2;
341 int ret = strcmp(name, param_info_base[middle].name);
351 }
else if (middle == front) {
355 }
else if (ret < 0) {
412 if (count && index < count) {
424 if (count && (
int)index < count) {
426 unsigned used_count = 0;
435 if (index == used_count) {
436 return (
param_t)(i * bits_per_allocation_unit + j);
452 return (
unsigned)
param;
473 if ((
int)param == i * bits_per_allocation_unit + j) {
557 const void *result =
nullptr;
609 #ifndef PARAM_NO_AUTOSAVE 617 bool disabled =
false;
619 #if !defined(PARAM_NO_ORB) 626 if (armed_sub.get().armed) {
627 work_queue(LPWORK, &autosave_work, (worker_t)&
autosave_worker,
nullptr, USEC2TICK(1_s));
644 PX4_DEBUG(
"Autosaving params");
648 PX4_ERR(
"param auto save failed (%i)", ret);
662 #ifndef PARAM_NO_AUTOSAVE 674 static constexpr
const hrt_abstime rate_limit = 2_s;
677 if (last_save_elapsed < rate_limit && rate_limit > last_save_elapsed + delay) {
678 delay = rate_limit - last_save_elapsed;
682 work_queue(LPWORK, &autosave_work, (worker_t)&
autosave_worker,
nullptr, USEC2TICK(delay));
689 #ifndef PARAM_NO_AUTOSAVE 693 work_cancel(LPWORK, &autosave_work);
706 bool params_changed =
false;
716 PX4_ERR(
"failed to allocate modified values array");
730 params_changed =
true;
744 params_changed = params_changed || s->
val.
i != *(int32_t *)val;
745 s->
val.
i = *(int32_t *)val;
749 params_changed = params_changed || fabsf(s->
val.
f - * (
float *)val) >
FLT_EPSILON;
750 s->
val.
f = *(
float *)val;
754 if (s->
val.
p ==
nullptr) {
758 s->
val.
p = malloc(psize);
764 if (s->
val.
p ==
nullptr) {
765 PX4_ERR(
"failed to allocate parameter storage");
771 params_changed =
true;
794 if (params_changed && notify_changes) {
801 #if defined(FLASH_BASED_PARAMS) 830 if (param_index < 0) {
847 if (param_index < 0) {
860 bool param_found =
false;
886 return (!param_found);
922 bool exclude =
false;
924 for (
int index = 0; index < num_excludes; index ++) {
925 int len = strlen(excludes[index]);
927 if ((excludes[index][len - 1] ==
'*' 928 && strncmp(name, excludes[index], len - 1) == 0)
929 || strcmp(name, excludes[index]) == 0) {
947 #ifdef FLASH_BASED_PARAMS 990 int fd =
PARAM_OPEN(filename, O_WRONLY | O_CREAT, PX4_O_MODE_666);
993 PX4_ERR(
"failed to open param file: %s", filename);
999 while (res !=
OK && attempts > 0) {
1003 if (res != PX4_OK) {
1004 PX4_ERR(
"param_export failed, retrying %d", attempts);
1005 lseek(fd, 0, SEEK_SET);
1010 PX4_ERR(
"failed to write parameters to file: %s", filename);
1031 int fd_load =
PARAM_OPEN(filename, O_RDONLY);
1035 if (errno != ENOENT) {
1036 PX4_ERR(
"open '%s' for reading failed", filename);
1047 PX4_ERR(
"error reading parameters from '%s'", filename);
1072 int shutdown_lock_ret = px4_shutdown_lock();
1074 if (shutdown_lock_ret) {
1075 PX4_ERR(
"px4_shutdown_lock() failed (%i)", shutdown_lock_ret);
1083 uint8_t bson_buffer[256];
1097 if (only_unsaved && !s->
unsaved) {
1110 const int32_t i = s->
val.
i;
1112 PX4_DEBUG(
"exporting: %s (%d) size: %d val: %d", name, s->
param, size, i);
1115 PX4_ERR(
"BSON append failed for '%s'", name);
1122 const double f = (double)s->
val.
f;
1124 PX4_DEBUG(
"exporting: %s (%d) size: %d val: %.3f", name, s->
param, size, (
double)
f);
1127 PX4_ERR(
"BSON append failed for '%s'", name);
1143 PX4_ERR(
"BSON append failed for '%s'", name);
1150 PX4_ERR(
"unrecognized parameter type");
1161 PX4_ERR(
"bson encoder finish failed");
1169 if (shutdown_lock_ret == 0) {
1170 px4_shutdown_unlock();
1187 void *tmp =
nullptr;
1197 PX4_DEBUG(
"end of parameters");
1208 PX4_ERR(
"ignoring unrecognised parameter '%s'", node->
name);
1216 switch (node->
type) {
1219 PX4_WARN(
"unexpected type for %s", node->
name);
1227 PX4_DEBUG(
"Imported %s with value %d",
param_name(param), i);
1233 PX4_WARN(
"unexpected type for %s", node->
name);
1241 PX4_DEBUG(
"Imported %s with value %f",
param_name(param), (
double)f);
1247 PX4_WARN(
"unexpected subtype for %s", node->
name);
1253 PX4_WARN(
"bad size for '%s'", node->
name);
1262 tmp = malloc(psize);
1268 if (tmp ==
nullptr) {
1269 PX4_ERR(
"failed allocating for '%s'", node->
name);
1274 PX4_ERR(
"failed copying data for '%s'", node->
name);
1283 PX4_DEBUG(
"unrecognised node type");
1288 PX4_DEBUG(
"error setting value for '%s'", node->
name);
1292 if (tmp !=
nullptr) {
1302 if (tmp !=
nullptr) {
1317 PX4_ERR(
"decoder init failed");
1326 }
while (result > 0);
1374 uint32_t param_hash = 0;
1386 param_hash = crc32part((
const uint8_t *)name, strlen(name), param_hash);
1387 param_hash = crc32part((
const uint8_t *)val,
param_size(param), param_hash);
1399 #ifndef FLASH_BASED_PARAMS 1402 if (filename !=
nullptr) {
1409 PX4_INFO(
"storage array: %d/%d elements (%zu bytes total)",
1413 #ifndef PARAM_NO_AUTOSAVE param_t param_for_index(unsigned index)
Look up a parameter by index.
void param_set_used(param_t param)
Mark a parameter as used.
static px4_sem_t param_sem_save
this protects against concurrent param saves (file or flash access).
#define PARAM_INVALID
Handle returned when a parameter cannot be found.
#define utarray_find(a, v, cmp)
int bson_encoder_init_buf_file(bson_encoder_t encoder, int fd, void *buf, unsigned bufsize)
Initialze the encoder for writing to a file.
unsigned param_count()
Return the total number of parameters.
int param_export(int fd, bool only_unsaved)
Export changed parameters to a file.
int param_save_default()
Save parameters to the default file.
param_t param_find_no_notification(const char *name)
Look up a parameter by name.
static void param_unlock_reader()
unlock the parameter store
int bson_decoder_next(bson_decoder_t decoder)
Process the next node from the stream and invoke the callback.
#define PARAM_TYPE_UNKNOWN
measure the time elapsed performing an event
#define PARAM_TYPE_INT32
Parameter types.
int bson_encoder_append_binary(bson_encoder_t encoder, const char *name, bson_binary_subtype_t subtype, size_t size, const void *data)
Append a binary blob to the encoded stream.
static char * param_user_file
API for the uORB lightweight object broker.
int bson_decoder_init_file(bson_decoder_t decoder, int fd, bson_decoder_callback callback, void *priv)
Initialise the decoder to read from a file.
void param_init()
Initialize the param backend.
int param_get(param_t param, void *val)
Copy the value of a parameter.
#define PARAM_TYPE_STRUCT
Storage for modified parameters.
static volatile bool autosave_scheduled
static int param_import_internal(int fd, bool mark_saved)
bson_binary_subtype_t subtype
static void param_set_used_internal(param_t param)
__EXPORT int param_set_external(param_t param, const void *val, bool mark_saved, bool notify_changes)
#define utarray_sort(a, cmp)
static bool handle_in_range(param_t param)
Test whether a param_t is value.
orb_advert_t orb_advertise(const struct orb_metadata *meta, const void *data)
static const param_info_s * param_info_base
Array of static parameter info.
bool param_is_volatile(param_t param)
Obtain the volatile state of a parameter.
static orb_advert_t param_topic
parameter update topic handle
__EXPORT const void * param_get_value_ptr_external(param_t param)
int bson_decoder_copy_data(bson_decoder_t decoder, void *buf)
Copy node data.
static perf_counter_t param_find_perf
High-resolution timer with callouts and timekeeping.
Node structure passed to the callback.
param_t param_for_used_index(unsigned index)
Look up an used parameter by index.
A simple subset SAX-style BSON parser and generator.
static perf_counter_t param_export_perf
Global flash based parameter store.
unsigned param_count_used()
Return the actually used number of parameters.
static void param_reset_all_internal(bool auto_save)
int bson_encoder_append_double(bson_encoder_t encoder, const char *name, double value)
Append a double to the encoded stream.
static void autosave_worker(void *arg)
worker callback method to save the parameters
Static parameter definition structure.
const char * param_get_default_file()
Get the default parameter file name.
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
int param_reset(param_t param)
Reset a parameter to its default value.
uint32_t param_hash_check()
Generate the hash of all parameters and their values.
static void param_unlock_writer()
unlock the parameter store
const int bits_per_allocation_unit
const UT_icd param_icd
array info for the modified parameters array
static int reader_lock_holders
static param_wbuf_s * param_find_changed(param_t param)
Locate the modified parameter structure for a parameter, if it exists.
param_type_t param_type(param_t param)
Obtain the type of a parameter.
static bool autosave_disabled
#define utarray_erase(a, pos, len)
void param_reset_excludes(const char *excludes[], int num_excludes)
Reset all parameters to their default values except for excluded parameters.
#define utarray_new(a, _icd)
static px4_sem_t param_sem
this protects against concurrent access to param_values
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
static int param_compare_values(const void *a, const void *b)
Compare two modifid parameter structures to determine ordering.
static hrt_abstime hrt_elapsed_time(const hrt_abstime *then)
Compute the delta between a timestamp taken in the past and now.
size_t bson_decoder_data_pending(bson_decoder_t decoder)
Report copyable data size.
static int flash_param_load()
#define PARAM_TYPE_STRUCT_MAX
param_t param_find(const char *name)
Look up a parameter by name.
void param_print_status()
Print the status of the param system.
static void param_lock_reader()
lock the parameter store for read access
const char * param_name(param_t param)
Obtain the name of a parameter.
#define utarray_next(a, e)
static void param_lock_writer()
lock the parameter store for write access
void perf_end(perf_counter_t handle)
End a performance event.
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
bool param_value_is_default(param_t param)
Test whether a parameter's value has changed from the default.
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
int orb_publish(const struct orb_metadata *meta, orb_advert_t handle, const void *data)
int bson_encoder_fini(bson_encoder_t encoder)
Finalise the encoded stream.
bool param_value_unsaved(param_t param)
Test whether a parameter's value has been changed but not saved.
int param_set(param_t param, const void *val)
Set the value of a parameter.
static unsigned get_param_info_count()
int param_load_default()
Load parameters from the default parameter file.
static void param_assert_locked()
assert that the parameter store is locked
static int param_set_internal(param_t param, const void *val, bool mark_saved, bool notify_changes)
void param_control_autosave(bool enable)
Enable/disable the param autosaving.
static perf_counter_t param_set_perf
static int flash_param_import()
int param_set_no_notification(param_t param, const void *val)
Set the value of a parameter, but do not notify the system about the change.
int param_set_default_file(const char *filename)
Set the default parameter file name.
uint8_t * param_changed_storage
bool param_used(param_t param)
Wether a parameter is in use in the system.
void param_foreach(void(*func)(void *arg, param_t param), void *arg, bool only_changed, bool only_used)
Apply a function to each parameter.
static param_t param_find_internal(const char *name, bool notification)
void perf_print_counter(perf_counter_t handle)
Print one performance counter to stdout.
static unsigned int param_instance
static void _param_notify_changes()
static perf_counter_t param_get_perf
void param_notify_changes()
Notify the system about parameter changes.
static void param_autosave()
Automatically save the parameters after a timeout and limited rate.
int param_get_index(param_t param)
Look up the index of a parameter.
static hrt_abstime last_autosave_timestamp
UT_array * param_values
flexible array holding modified parameter values
size_t param_size(param_t param)
Determine the size of a parameter.
static const void * param_get_value_ptr(param_t param)
Obtain a pointer to the storage allocated for a parameter.
int param_import(int fd)
Import parameters from a file, discarding any unrecognized parameters.
static const char * param_default_file
int size_param_changed_storage_bytes
int param_get_used_index(param_t param)
Look up the index of an used parameter.
int param_load(int fd)
Load parameters from a file.
void param_reset_all()
Reset all parameters to their default values.
static px4_sem_t reader_lock_holders_lock
this protects against concurrent access to reader_lock_holders
#define utarray_eltidx(a, e)
void perf_begin(perf_counter_t handle)
Begin a performance event.
static int param_import_callback(bson_decoder_t decoder, void *priv, bson_node_t node)
int bson_encoder_append_int(bson_encoder_t encoder, const char *name, int64_t value)
Append an integer to the encoded stream.
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
uint32_t param_t
Parameter handle.
#define utarray_push_back(a, p)
static int flash_param_save(bool only_unsaved)
Performance measuring tools.