45 #include <parameters/px4_parameters.h> 53 #include <px4_platform_common/px4_config.h> 54 #include <px4_platform_common/defines.h> 55 #include <px4_platform_common/posix.h> 56 #include <px4_platform_common/sem.h> 57 #include <px4_platform_common/shutdown.h> 65 #if !defined(PARAM_NO_ORB) 70 #if defined(FLASH_BASED_PARAMS) 76 #include <px4_platform_common/shmem.h> 87 #define PARAM_OPEN(a, b, ...) open(a, b) 89 #define PARAM_OPEN open 91 #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 159 #if !defined(PARAM_NO_ORB) 199 do {}
while (px4_sem_wait(&
param_sem) != 0);
212 do {}
while (px4_sem_wait(&
param_sem) != 0);
267 PX4_DEBUG(
"Syncing params to shared memory\n");
282 return (count && param < count);
333 #if !defined(PARAM_NO_ORB) 367 if (!strcmp(param_info_base[param].name, name)) {
431 if (count && index < count) {
444 if (count && (
int)index < count) {
446 unsigned used_count = 0;
455 if (index == used_count) {
456 return (
param_t)(i * bits_per_allocation_unit + j);
475 return (
unsigned)
param;
498 if ((
int)param == i * bits_per_allocation_unit + j) {
533 return s ? false :
true;
586 const void *result =
nullptr;
631 if (update_from_shmem(param, &value)) {
644 #ifdef ENABLE_SHMEM_DEBUG 655 PX4_INFO(
"Unknown param type for %s",
param_name(param));
666 #ifndef PARAM_NO_AUTOSAVE 674 bool disabled =
false;
686 PX4_DEBUG(
"Autosaving params");
690 PX4_ERR(
"param save failed (%i)", ret);
704 #ifndef PARAM_NO_AUTOSAVE 719 if (last_save_elapsed < rate_limit && rate_limit > last_save_elapsed + delay) {
720 delay = rate_limit - last_save_elapsed;
731 #ifndef PARAM_NO_AUTOSAVE 748 bool params_changed =
false;
758 PX4_ERR(
"failed to allocate modified values array");
772 params_changed =
true;
786 params_changed = params_changed || s->
val.
i != *(int32_t *)val;
787 s->
val.
i = *(int32_t *)val;
791 params_changed = params_changed || fabsf(s->
val.
f - * (
float *)val) >
FLT_EPSILON;
792 s->
val.
f = *(
float *)val;
796 if (s->
val.
p ==
nullptr) {
800 s->
val.
p = malloc(psize);
806 if (s->
val.
p ==
nullptr) {
807 PX4_ERR(
"failed to allocate parameter storage");
813 params_changed =
true;
839 if (params_changed && notify_changes) {
847 #ifdef ENABLE_SHMEM_DEBUG 858 PX4_INFO(
"Unknown param type for %s",
param_name(param));
866 #if defined(FLASH_BASED_PARAMS) 898 if (param_index < 0) {
915 if (param_index < 0) {
928 bool param_found =
false;
954 return (!param_found);
990 bool exclude =
false;
992 for (
int index = 0; index < num_excludes; index ++) {
993 int len = strlen(excludes[index]);
995 if ((excludes[index][len - 1] ==
'*' 996 && strncmp(name, excludes[index], len - 1) == 0)
997 || strcmp(name, excludes[index]) == 0) {
1042 fd =
PARAM_OPEN(filename, O_WRONLY | O_CREAT, PX4_O_MODE_666);
1045 PX4_ERR(
"failed to open param file: %s", filename);
1052 PX4_ERR(
"failed to write parameters to file: %s", filename);
1068 PX4_DEBUG(
"saving params completed successfully");
1081 #if !defined(FLASH_BASED_PARAMS) 1086 if (errno != ENOENT) {
1119 release_shmem_lock(__FILE__, __LINE__);
1123 if (errno != ENOENT) {
1135 PX4_DEBUG(
"param loading done");
1155 int shutdown_lock_ret = px4_shutdown_lock();
1157 if (shutdown_lock_ret) {
1158 PX4_ERR(
"px4_shutdown_lock() failed (%i)", shutdown_lock_ret);
1176 update_index_from_shmem();
1183 if (only_unsaved && !s->
unsaved) {
1190 update_from_shmem(s->
param, &s->
val);
1199 const int32_t i = s->
val.
i;
1201 PX4_DEBUG(
"exporting: %s (%d) size: %d val: %d", name, s->
param, size, i);
1204 PX4_ERR(
"BSON append failed for '%s'", name);
1211 const double f = (double)s->
val.
f;
1213 PX4_DEBUG(
"exporting: %s (%d) size: %d val: %.3f", name, s->
param, size, (
double)
f);
1216 PX4_ERR(
"BSON append failed for '%s'", name);
1232 PX4_ERR(
"BSON append failed for '%s'", name);
1239 PX4_ERR(
"unrecognized parameter type");
1253 if (shutdown_lock_ret == 0) {
1254 px4_shutdown_unlock();
1275 void *tmp =
nullptr;
1285 PX4_DEBUG(
"end of parameters");
1296 PX4_ERR(
"ignoring unrecognised parameter '%s'", node->
name);
1304 switch (node->
type) {
1307 PX4_WARN(
"unexpected type for %s", node->
name);
1315 PX4_DEBUG(
"Imported %s with value %d",
param_name(param), i);
1321 PX4_WARN(
"unexpected type for %s", node->
name);
1329 PX4_DEBUG(
"Imported %s with value %f",
param_name(param), (
double)f);
1335 PX4_WARN(
"unexpected subtype for %s", node->
name);
1341 PX4_WARN(
"bad size for '%s'", node->
name);
1350 tmp = malloc(psize);
1356 if (tmp ==
nullptr) {
1357 PX4_ERR(
"failed allocating for '%s'", node->
name);
1362 PX4_ERR(
"failed copying data for '%s'", node->
name);
1371 PX4_DEBUG(
"unrecognised node type");
1376 PX4_DEBUG(
"error setting value for '%s'", node->
name);
1380 if (tmp !=
nullptr) {
1390 if (tmp !=
nullptr) {
1405 PX4_ERR(
"decoder init failed");
1415 }
while (result > 0);
1423 #if !defined(FLASH_BASED_PARAMS) 1461 uint32_t param_hash = 0;
1473 param_hash = crc32part((
const uint8_t *)name, strlen(name), param_hash);
1474 param_hash = crc32part((
const uint8_t *)val,
param_size(
param), param_hash);
1486 #ifndef FLASH_BASED_PARAMS 1489 if (filename !=
nullptr) {
1496 PX4_INFO(
"storage array: %d/%d elements (%zu bytes total)",
1500 #ifndef PARAM_NO_AUTOSAVE 1519 init_shared_memory();
1530 copy_params_to_shmem(param_info_base);
1532 #ifdef ENABLE_SHMEM_DEBUG 1533 PX4_INFO(
"Offsets:");
1534 PX4_INFO(
"params_val %lu, krait_changed %lu, adsp_changed %lu",
1535 (
unsigned char *)shmem_info_p->params_val - (
unsigned char *)shmem_info_p,
1536 (
unsigned char *)&shmem_info_p->krait_changed_index - (
unsigned char *)shmem_info_p,
1537 (
unsigned char *)&shmem_info_p->adsp_changed_index - (
unsigned char *)shmem_info_p);
static void param_lock_writer()
lock the parameter store for write access
#define PARAM_INVALID
Handle returned when a parameter cannot be found.
#define utarray_find(a, v, cmp)
static int param_import_callback(bson_decoder_t decoder, void *priv, bson_node_t node)
void param_set_used(param_t param)
Mark a parameter as used.
void param_control_autosave(bool enable)
Enable/disable the param autosaving.
static int param_import_internal(int fd, bool mark_saved)
static unsigned int param_instance
const UT_icd param_icd
array info for the modified parameters array
static char * param_user_file
uint32_t param_hash_check()
Generate the hash of all parameters and their values.
int param_import(int fd)
Import parameters from a file, discarding any unrecognized parameters.
bool param_value_unsaved(param_t param)
Test whether a parameter's value has been changed but not saved.
int param_reset(param_t param)
Reset a parameter to its default value.
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 void param_lock_reader()
< we use a separate lock to allow concurrent param reads and saves.
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.
const char * param_name(param_t param)
Obtain the name of a parameter.
param_t param_find_no_notification(const char *name)
Look up a parameter by name.
static const param_info_s * param_info_base
Array of static parameter info.
#define PARAM_TYPE_STRUCT
int param_export(int fd, bool only_unsaved)
Export changed parameters to a file.
Storage for modified parameters.
static void param_reset_all_internal(bool auto_save)
bson_binary_subtype_t subtype
param_t param_for_used_index(unsigned index)
Look up an used parameter by index.
__EXPORT int param_set_external(param_t param, const void *val, bool mark_saved, bool notify_changes)
#define utarray_sort(a, cmp)
static unsigned get_param_info_count()
static const void * param_get_value_ptr(param_t param)
Obtain a pointer to the storage allocated for a parameter.
orb_advert_t orb_advertise(const struct orb_metadata *meta, const void *data)
int param_set_default_file(const char *filename)
Set the default parameter file name.
param_type_t param_type(param_t param)
Obtain the type of a parameter.
int param_load_default()
Load parameters from the default parameter file.
__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.
void param_init()
Initialize the param backend.
static void param_unlock_writer()
unlock the parameter store
High-resolution timer with callouts and timekeeping.
void param_notify_changes()
Notify the system about parameter changes.
int size_param_changed_storage_bytes
Node structure passed to the callback.
param_t param_for_index(unsigned index)
Look up a parameter by index.
A simple subset SAX-style BSON parser and generator.
static void param_unlock_reader()
unlock the parameter store
Global flash based parameter store.
void param_reset_excludes(const char *excludes[], int num_excludes)
Reset all parameters to their default values except for excluded parameters.
int bson_encoder_append_double(bson_encoder_t encoder, const char *name, double value)
Append a double to the encoded stream.
Static parameter definition structure.
static int param_import_done
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
param_wbuf_s * param_find_changed(param_t param)
Locate the modified parameter structure for a parameter, if it exists.
int param_set(param_t param, const void *val)
Set the value of a parameter.
static void _param_notify_changes()
UT_array * param_values
flexible array holding modified parameter values
static bool autosave_disabled
static int reader_lock_holders
static int param_set_internal(param_t param, const void *val, bool mark_saved, bool notify_changes)
#define utarray_erase(a, pos, len)
static perf_counter_t param_find_perf
static void init_params()
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.
#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 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.
#define PARAM_TYPE_STRUCT_MAX
bool param_value_is_default(param_t param)
Test whether a parameter's value has changed from the default.
#define utarray_next(a, e)
void param_print_status()
Print the status of the param system.
void perf_end(perf_counter_t handle)
End a performance event.
bool handle_in_range(param_t param)
Test whether a param_t is value.
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
int param_save_default()
Save parameters to the default file.
__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.
static perf_counter_t param_get_perf
bool param_used(param_t param)
Wether a parameter is in use in the system.
const char * param_get_default_file()
Get the default parameter file name.
static unsigned char set_called_from_get
int param_load(int fd)
Load parameters from a file.
int param_get(param_t param, void *val)
Copy the value of a parameter.
const int bits_per_allocation_unit
int param_get_index(param_t param)
Look up the index of a 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.
size_t param_size(param_t param)
Determine the size of a parameter.
unsigned param_count_used()
Return the actually used number of parameters.
static orb_advert_t param_topic
parameter update topic handle
int bson_encoder_init_file(bson_encoder_t encoder, int fd)
Initialze the encoder for writing to a file.
void param_reset_all()
Reset all parameters to their default values.
static perf_counter_t param_set_perf
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 void param_assert_locked()
assert that the parameter store is locked
static hrt_abstime last_autosave_timestamp
static const char * param_default_file
static perf_counter_t param_export_perf
param_t param_find(const char *name)
Look up a parameter by name.
int param_get_used_index(param_t param)
Look up the index of an used parameter.
static bool autosave_scheduled
static void param_autosave()
Automatically save the parameters after a timeout and limited rate.
uint8_t * param_changed_storage
static void param_set_used_internal(param_t param)
static px4_sem_t reader_lock_holders_lock
this protects against concurrent access to reader_lock_holders
static void autosave_worker(void *arg)
worker callback method to save the parameters
#define utarray_eltidx(a, e)
void perf_begin(perf_counter_t handle)
Begin a performance event.
unsigned param_count()
Return the total number of parameters.
int bson_encoder_append_int(bson_encoder_t encoder, const char *name, int64_t value)
Append an integer to the encoded stream.
static int param_compare_values(const void *a, const void *b)
Compare two modifid parameter structures to determine ordering.
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
static int param_load_default_no_notify()
uint32_t param_t
Parameter handle.
#define utarray_push_back(a, p)
bool param_is_volatile(param_t param)
Obtain the volatile state of a parameter.
Performance measuring tools.