diff --git a/platforms/posix/CMakeLists.txt b/platforms/posix/CMakeLists.txt index 00e8286635..62353fe77a 100644 --- a/platforms/posix/CMakeLists.txt +++ b/platforms/posix/CMakeLists.txt @@ -38,6 +38,8 @@ if (("${BOARD}" STREQUAL "eagle") OR ("${BOARD}" STREQUAL "excelsior")) apps.cpp LINK_LIBS -Wl,--start-group + px4_layer + parameters ${module_libraries} ${df_driver_libs} ${FASTRPC_ARM_LIBS} diff --git a/platforms/posix/src/px4_layer/CMakeLists.txt b/platforms/posix/src/px4_layer/CMakeLists.txt index dc752e595b..f31a645dba 100644 --- a/platforms/posix/src/px4_layer/CMakeLists.txt +++ b/platforms/posix/src/px4_layer/CMakeLists.txt @@ -38,10 +38,9 @@ if("${CONFIG_SHMEM}" STREQUAL "1") include_directories(${HEXAGON_SDK_INCLUDES}) include_directories(${PX4_BINARY_DIR}/platforms/posix) list(APPEND SHMEM_SRCS - shmem_posix.c + shmem_posix.cpp ) - # TODO: This didn't seem to be tracked correctly from posix_eagle_release.cmake - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCONFIG_SHMEM") + add_definitions(-DCONFIG_SHMEM=1) set(EXTRA_DEPENDS generate_px4muorb_stubs) endif() diff --git a/platforms/posix/src/px4_layer/px4_posix_impl.cpp b/platforms/posix/src/px4_layer/px4_posix_impl.cpp index 852dd12151..199626b7e9 100644 --- a/platforms/posix/src/px4_layer/px4_posix_impl.cpp +++ b/platforms/posix/src/px4_layer/px4_posix_impl.cpp @@ -58,10 +58,6 @@ __BEGIN_DECLS long PX4_TICKS_PER_SEC = sysconf(_SC_CLK_TCK); -#ifdef CONFIG_SHMEM -extern void init_params(void); -#endif - __END_DECLS namespace px4 @@ -77,11 +73,6 @@ void init_once() hrt_work_queue_init(); hrt_init(); param_init(); - -#ifdef CONFIG_SHMEM - PX4_DEBUG("Syncing params to shared memory\n"); - init_params(); -#endif } void init(int argc, char *argv[], const char *app_name) diff --git a/platforms/posix/src/px4_layer/shmem_posix.c b/platforms/posix/src/px4_layer/shmem_posix.cpp similarity index 95% rename from platforms/posix/src/px4_layer/shmem_posix.c rename to platforms/posix/src/px4_layer/shmem_posix.cpp index d59a9c8916..3a8b14fa8c 100644 --- a/platforms/posix/src/px4_layer/shmem_posix.c +++ b/platforms/posix/src/px4_layer/shmem_posix.cpp @@ -69,8 +69,7 @@ struct param_wbuf_s { /*update value and param's change bit in shared memory*/ void update_to_shmem(param_t param, union param_value_u value) { - if (px4muorb_param_update_to_shmem(param, (unsigned char *) &value, - sizeof(value))) { + if (px4muorb_param_update_to_shmem(param, (unsigned char *) &value, sizeof(value))) { PX4_ERR("krait update param %u failed", param); } } @@ -82,14 +81,12 @@ void update_index_from_shmem(void) return; } - px4muorb_param_update_index_from_shmem(adsp_changed_index, - PARAM_BUFFER_SIZE); + px4muorb_param_update_index_from_shmem(adsp_changed_index, PARAM_BUFFER_SIZE); } static void update_value_from_shmem(param_t param, union param_value_u *value) { - if (px4muorb_param_update_value_from_shmem(param, (unsigned char *) value, - sizeof(union param_value_u))) { + if (px4muorb_param_update_value_from_shmem(param, (unsigned char *) value, sizeof(union param_value_u))) { PX4_ERR("%s get param failed", __FUNCTION__); } } @@ -128,4 +125,3 @@ int update_from_shmem(param_t param, union param_value_u *value) return retval; } - diff --git a/platforms/qurt/CMakeLists.txt b/platforms/qurt/CMakeLists.txt index befa9d7b8d..64387f780b 100644 --- a/platforms/qurt/CMakeLists.txt +++ b/platforms/qurt/CMakeLists.txt @@ -43,10 +43,10 @@ else() SOURCES ${PX4_BINARY_DIR}/apps.cpp LINK_LIBS + modules__muorb__adsp ${module_libraries} ${df_driver_libs} m - modules__muorb__adsp ) add_custom_target(upload diff --git a/platforms/qurt/src/px4_layer/CMakeLists.txt b/platforms/qurt/src/px4_layer/CMakeLists.txt index 8c238a1a81..b7b1fd83c7 100644 --- a/platforms/qurt/src/px4_layer/CMakeLists.txt +++ b/platforms/qurt/src/px4_layer/CMakeLists.txt @@ -43,7 +43,7 @@ set(QURT_LAYER_SRCS ../../../posix/src/px4_layer/drv_hrt.c qurt_stubs.c main.cpp - shmem_qurt.c + shmem_qurt.cpp ) if ("${QURT_ENABLE_STUBS}" STREQUAL "1") diff --git a/platforms/qurt/src/px4_layer/px4_qurt_impl.cpp b/platforms/qurt/src/px4_layer/px4_qurt_impl.cpp index 4408c6d4aa..56515da2b1 100644 --- a/platforms/qurt/src/px4_layer/px4_qurt_impl.cpp +++ b/platforms/qurt/src/px4_layer/px4_qurt_impl.cpp @@ -71,7 +71,6 @@ unsigned int sleep(unsigned int sec) } extern void hrt_init(void); -extern void init_params(); #if 0 void qurt_log(const char *fmt, ...) @@ -108,9 +107,6 @@ void init_once(void) hrt_work_queue_init(); hrt_init(); param_init(); - - /* Shared memory param sync*/ - init_params(); } void init(int argc, char *argv[], const char *app_name) diff --git a/platforms/qurt/src/px4_layer/shmem_qurt.c b/platforms/qurt/src/px4_layer/shmem_qurt.cpp similarity index 97% rename from platforms/qurt/src/px4_layer/shmem_qurt.c rename to platforms/qurt/src/px4_layer/shmem_qurt.cpp index e4eb4edc4c..1695ab5b95 100644 --- a/platforms/qurt/src/px4_layer/shmem_qurt.c +++ b/platforms/qurt/src/px4_layer/shmem_qurt.cpp @@ -56,11 +56,11 @@ unsigned char *map_base, *virt_addr; struct shmem_info *shmem_info_p; int get_shmem_lock(const char *caller_file_name, int caller_line_number); void release_shmem_lock(const char *caller_file_name, int caller_line_number); -void init_shared_memory(void); -void copy_params_to_shmem(struct param_info_s *); +void init_shared_memory(); +void copy_params_to_shmem(param_info_s *); void update_to_shmem(param_t param, union param_value_u value); int update_from_shmem(param_t param, union param_value_u *value); -void update_index_from_shmem(void); +void update_index_from_shmem(); uint64_t update_from_shmem_prev_time = 0, update_from_shmem_current_time = 0; static unsigned char krait_changed_index[MAX_SHMEM_PARAMS / 8 + 1]; @@ -136,7 +136,7 @@ void init_shared_memory(void) } //virt_addr = map_memory(MAP_ADDRESS); - map_base = calloc(MAP_SIZE, 1); //16KB + map_base = (unsigned char *)calloc(MAP_SIZE, 1); //16KB if (map_base == NULL) { PX4_INFO("adsp memory malloc failed\n"); @@ -153,10 +153,9 @@ void init_shared_memory(void) } PX4_INFO("adsp memory mapped\n"); - } -void copy_params_to_shmem(struct param_info_s *param_info_base) +void copy_params_to_shmem(const param_info_s *param_info_base) { param_t param; unsigned int i; @@ -350,4 +349,3 @@ int update_from_shmem(param_t param, union param_value_u *value) return retval; } - diff --git a/src/lib/parameters/CMakeLists.txt b/src/lib/parameters/CMakeLists.txt index 6338f649f2..0f8260c85c 100644 --- a/src/lib/parameters/CMakeLists.txt +++ b/src/lib/parameters/CMakeLists.txt @@ -105,9 +105,10 @@ add_custom_command(OUTPUT px4_parameters.c px4_parameters.h px4_parameters_publi set(SRCS) if ("${CONFIG_SHMEM}" STREQUAL "1") message(STATUS "parameters shared memory enabled") - list(APPEND SRCS param_shmem.c) + add_definitions(-DCONFIG_SHMEM=1) + list(APPEND SRCS parameters_shmem.cpp) else() - list(APPEND SRCS param.c) + list(APPEND SRCS parameters.cpp) endif() if(${OS} STREQUAL "nuttx") @@ -136,5 +137,5 @@ endif() add_dependencies(parameters prebuild_targets) if(${OS} STREQUAL "nuttx") - target_link_libraries(parameters PRIVATE flashparams) + target_link_libraries(parameters PRIVATE flashparams tinybson) endif() diff --git a/src/lib/parameters/flashparams/CMakeLists.txt b/src/lib/parameters/flashparams/CMakeLists.txt index 5e91b7d326..8a10dfbad3 100644 --- a/src/lib/parameters/flashparams/CMakeLists.txt +++ b/src/lib/parameters/flashparams/CMakeLists.txt @@ -33,7 +33,7 @@ add_library(flashparams flashfs.c - flashparams.c + flashparams.cpp ) add_dependencies(flashparams prebuild_targets) target_compile_definitions(flashparams PRIVATE -DMODULE_NAME="flashparams") diff --git a/src/lib/parameters/flashparams/flashparams.c b/src/lib/parameters/flashparams/flashparams.cpp similarity index 95% rename from src/lib/parameters/flashparams/flashparams.c rename to src/lib/parameters/flashparams/flashparams.cpp index 874493c9ee..6593a67c5b 100644 --- a/src/lib/parameters/flashparams/flashparams.c +++ b/src/lib/parameters/flashparams/flashparams.cpp @@ -75,21 +75,21 @@ struct param_wbuf_s { static int param_export_internal(bool only_unsaved) { - struct param_wbuf_s *s = NULL; + struct param_wbuf_s *s = nullptr; struct bson_encoder_s encoder; int result = -1; /* Use realloc */ - bson_encoder_init_buf(&encoder, NULL, 0); + bson_encoder_init_buf(&encoder, nullptr, 0); /* no modified parameters -> we are done */ - if (param_values == NULL) { + if (param_values == nullptr) { result = 0; goto out; } - while ((s = (struct param_wbuf_s *)utarray_next(param_values, s)) != NULL) { + while ((s = (struct param_wbuf_s *)utarray_next(param_values, s)) != nullptr) { int32_t i; float f; @@ -209,13 +209,13 @@ struct param_import_state { }; static int -param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node) +param_import_callback(bson_decoder_t decoder, void *priv, bson_node_t node) { float f; int32_t i; - void *v, *tmp = NULL; + void *v, *tmp = nullptr; int result = -1; - struct param_import_state *state = (struct param_import_state *)private; + struct param_import_state *state = (struct param_import_state *)priv; /* * EOO means the end of the parameter object. (Currently not supporting @@ -280,7 +280,7 @@ param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node) /* XXX check actual file data size? */ tmp = malloc(param_size(param)); - if (tmp == NULL) { + if (tmp == nullptr) { debug("failed allocating for '%s'", node->name); goto out; } @@ -304,9 +304,9 @@ param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node) goto out; } - if (tmp != NULL) { + if (tmp != nullptr) { free(tmp); - tmp = NULL; + tmp = nullptr; } /* don't return zero, that means EOF */ @@ -314,7 +314,7 @@ param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node) out: - if (tmp != NULL) { + if (tmp != nullptr) { free(tmp); } @@ -353,18 +353,18 @@ out: return result; } -int flash_param_save(void) +int flash_param_save() { return param_export_internal(false); } -int flash_param_load(void) +int flash_param_load() { param_reset_all(); return param_import_internal(true); } -int flash_param_import(void) +int flash_param_import() { return -1; } diff --git a/src/lib/parameters/flashparams/flashparams.h b/src/lib/parameters/flashparams/flashparams.h index 64588057a8..fd960d50b9 100644 --- a/src/lib/parameters/flashparams/flashparams.h +++ b/src/lib/parameters/flashparams/flashparams.h @@ -58,15 +58,15 @@ __BEGIN_DECLS * the param_values and 2 functions to be global */ -#define FLASH_PARAMS_EXPOSE __EXPORT - __EXPORT extern UT_array *param_values; __EXPORT int param_set_external(param_t param, const void *val, bool mark_saved, bool notify_changes); __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(void); -__EXPORT int flash_param_load(void); -__EXPORT int flash_param_import(void); +__EXPORT int flash_param_save(); +__EXPORT int flash_param_load(); +__EXPORT int flash_param_import(); + __END_DECLS + #endif /* _SYSTEMLIB_FLASHPARAMS_FLASHPARAMS_H */ diff --git a/src/lib/parameters/param.h b/src/lib/parameters/param.h index be2cf9e901..16dc48b7a2 100644 --- a/src/lib/parameters/param.h +++ b/src/lib/parameters/param.h @@ -482,21 +482,18 @@ __END_DECLS // param is a C-interface. This means there is no overloading, and thus no type-safety for param_get(). // So for C++ code we redefine param_get() to inlined overloaded versions, which gives us type-safety // w/o having to use a different interface -static inline int param_get_cplusplus(param_t param, float *val) +static inline int param_get(param_t param, float *val) { CHECK_PARAM_TYPE(param, PARAM_TYPE_FLOAT); - return param_get(param, val); + return param_get(param, (void *)val); } -static inline int param_get_cplusplus(param_t param, int32_t *val) +static inline int param_get(param_t param, int32_t *val) { CHECK_PARAM_TYPE(param, PARAM_TYPE_INT32); - return param_get(param, val); + return param_get(param, (void *)val); } #undef CHECK_PARAM_TYPE -#define param_get(param, val) param_get_cplusplus(param, val) - - #endif /* __cplusplus */ #endif diff --git a/src/lib/parameters/param.c b/src/lib/parameters/parameters.cpp similarity index 85% rename from src/lib/parameters/param.c rename to src/lib/parameters/parameters.cpp index dc2b95bfdb..c587a8751a 100644 --- a/src/lib/parameters/param.c +++ b/src/lib/parameters/parameters.cpp @@ -67,20 +67,12 @@ # include "uORB/topics/parameter_update.h" #endif -#if !defined(FLASH_BASED_PARAMS) -# define FLASH_PARAMS_EXPOSE -#else -# include "flashparams/flashparams.h" +#if defined(FLASH_BASED_PARAMS) +#include "flashparams/flashparams.h" #endif static const char *param_default_file = PX4_ROOTFSDIR"/eeprom/parameters"; -static char *param_user_file = NULL; - -#if 0 -# define debug(fmt, args...) do { PX4_INFO(fmt, ##args); } while(0) -#else -# define debug(fmt, args...) do { } while(0) -#endif +static char *param_user_file = nullptr; #ifdef __PX4_QURT #define PARAM_OPEN px4_open @@ -102,7 +94,7 @@ static bool autosave_disabled = false; /** * Array of static parameter info. */ -static const struct param_info_s *param_info_base = (const struct param_info_s *) &px4_parameters; +static const param_info_s *param_info_base = (const param_info_s *) &px4_parameters; #define param_info_count px4_parameters.param_count /** @@ -115,24 +107,24 @@ struct param_wbuf_s { }; -uint8_t *param_changed_storage = NULL; +uint8_t *param_changed_storage = nullptr; int size_param_changed_storage_bytes = 0; const int bits_per_allocation_unit = (sizeof(*param_changed_storage) * 8); static unsigned -get_param_info_count(void) +get_param_info_count() { /* Singleton creation of and array of bits to track changed values */ if (!param_changed_storage) { /* Note that we have a (highly unlikely) race condition here: in the worst case the allocation is done twice */ size_param_changed_storage_bytes = (param_info_count / bits_per_allocation_unit) + 1; - param_changed_storage = calloc(size_param_changed_storage_bytes, 1); + param_changed_storage = (uint8_t *)calloc(size_param_changed_storage_bytes, 1); /* If the allocation fails we need to indicate failure in the * API by returning PARAM_INVALID */ - if (param_changed_storage == NULL) { + if (param_changed_storage == nullptr) { return 0; } } @@ -141,14 +133,14 @@ get_param_info_count(void) } /** flexible array holding modified parameter values */ -FLASH_PARAMS_EXPOSE UT_array *param_values; +UT_array *param_values{nullptr}; /** array info for the modified parameters array */ -FLASH_PARAMS_EXPOSE const UT_icd param_icd = {sizeof(struct param_wbuf_s), NULL, NULL, NULL}; +const UT_icd param_icd = {sizeof(param_wbuf_s), nullptr, nullptr, nullptr}; #if !defined(PARAM_NO_ORB) /** parameter update topic handle */ -static orb_advert_t param_topic = NULL; +static orb_advert_t param_topic = nullptr; static unsigned int param_instance = 0; #endif @@ -175,7 +167,7 @@ static px4_sem_t param_sem_save; ///< this protects against concurrent param sav /** lock the parameter store for read access */ static void -param_lock_reader(void) +param_lock_reader() { do {} while (px4_sem_wait(&reader_lock_holders_lock) != 0); @@ -191,14 +183,14 @@ param_lock_reader(void) /** lock the parameter store for write access */ static void -param_lock_writer(void) +param_lock_writer() { do {} while (px4_sem_wait(¶m_sem) != 0); } /** unlock the parameter store */ static void -param_unlock_reader(void) +param_unlock_reader() { do {} while (px4_sem_wait(&reader_lock_holders_lock) != 0); @@ -214,20 +206,20 @@ param_unlock_reader(void) /** unlock the parameter store */ static void -param_unlock_writer(void) +param_unlock_writer() { px4_sem_post(¶m_sem); } /** assert that the parameter store is locked */ static void -param_assert_locked(void) +param_assert_locked() { /* XXX */ } void -param_init(void) +param_init() { px4_sem_init(¶m_sem, 0, 1); px4_sem_init(¶m_sem_save, 0, 1); @@ -279,38 +271,37 @@ param_compare_values(const void *a, const void *b) * * @param param The parameter being searched. * @return The structure holding the modified value, or - * NULL if the parameter has not been modified. + * nullptr if the parameter has not been modified. */ -static struct param_wbuf_s * +static param_wbuf_s * param_find_changed(param_t param) { - struct param_wbuf_s *s = NULL; + param_wbuf_s *s = nullptr; param_assert_locked(); - if (param_values != NULL) { - struct param_wbuf_s key; + if (param_values != nullptr) { + param_wbuf_s key{}; key.param = param; - s = utarray_find(param_values, &key, param_compare_values); + s = (param_wbuf_s *)utarray_find(param_values, &key, param_compare_values); } return s; } static void -_param_notify_changes(void) +_param_notify_changes() { #if !defined(PARAM_NO_ORB) - struct parameter_update_s pup = { - .timestamp = hrt_absolute_time(), - .instance = param_instance++, - }; + parameter_update_s pup = {}; + pup.timestamp = hrt_absolute_time(); + pup.instance = param_instance++; /* * If we don't have a handle to our topic, create one now; otherwise * just publish. */ - if (param_topic == NULL) { + if (param_topic == nullptr) { param_topic = orb_advertise(ORB_ID(parameter_update), &pup); } else { @@ -321,7 +312,7 @@ _param_notify_changes(void) } void -param_notify_changes(void) +param_notify_changes() { _param_notify_changes(); } @@ -380,13 +371,13 @@ param_find_no_notification(const char *name) } unsigned -param_count(void) +param_count() { return get_param_info_count(); } unsigned -param_count_used(void) +param_count_used() { unsigned count = 0; @@ -486,7 +477,7 @@ param_get_used_index(param_t param) const char * param_name(param_t param) { - return handle_in_range(param) ? param_info_base[param].name : NULL; + return handle_in_range(param) ? param_info_base[param].name : nullptr; } bool @@ -549,13 +540,13 @@ param_size(param_t param) * Obtain a pointer to the storage allocated for a parameter. * * @param param The parameter whose storage is sought. - * @return A pointer to the parameter value, or NULL + * @return A pointer to the parameter value, or nullptr * if the parameter does not exist. */ static const void * param_get_value_ptr(param_t param) { - const void *result = NULL; + const void *result = nullptr; param_assert_locked(); @@ -566,7 +557,7 @@ param_get_value_ptr(param_t param) /* work out whether we're fetching the default or a written value */ struct param_wbuf_s *s = param_find_changed(param); - if (s != NULL) { + if (s != nullptr) { v = &s->val; } else { @@ -607,7 +598,6 @@ param_get(param_t param, void *val) return result; } - #ifndef PARAM_NO_AUTOSAVE /** * worker callback method to save the parameters @@ -644,7 +634,7 @@ autosave_worker(void *arg) * needs to be the same lock as autosave_worker() and param_control_autosave() use). */ static void -param_autosave(void) +param_autosave() { #ifndef PARAM_NO_AUTOSAVE @@ -666,7 +656,7 @@ param_autosave(void) } autosave_scheduled = true; - work_queue(LPWORK, &autosave_work, (worker_t)&autosave_worker, NULL, USEC2TICK(delay)); + work_queue(LPWORK, &autosave_work, (worker_t)&autosave_worker, nullptr, USEC2TICK(delay)); #endif /* PARAM_NO_AUTOSAVE */ } @@ -695,27 +685,25 @@ param_set_internal(param_t param, const void *val, bool mark_saved, bool notify_ param_lock_writer(); perf_begin(param_set_perf); - if (param_values == NULL) { + if (param_values == nullptr) { utarray_new(param_values, ¶m_icd); } - if (param_values == NULL) { + if (param_values == nullptr) { PX4_ERR("failed to allocate modified values array"); goto out; } if (handle_in_range(param)) { - struct param_wbuf_s *s = param_find_changed(param); + param_wbuf_s *s = param_find_changed(param); - if (s == NULL) { + if (s == nullptr) { /* construct a new parameter */ - struct param_wbuf_s buf = { - .param = param, - .val.p = NULL, - .unsaved = false - }; + param_wbuf_s buf = {}; + buf.param = param; + params_changed = true; /* add it to the array and sort */ @@ -740,17 +728,17 @@ param_set_internal(param_t param, const void *val, bool mark_saved, bool notify_ break; case PARAM_TYPE_STRUCT ... PARAM_TYPE_STRUCT_MAX: - if (s->val.p == NULL) { + if (s->val.p == nullptr) { size_t psize = param_size(param); if (psize > 0) { s->val.p = malloc(psize); } else { - s->val.p = NULL; + s->val.p = nullptr; } - if (s->val.p == NULL) { + if (s->val.p == nullptr) { PX4_ERR("failed to allocate parameter storage"); goto out; } @@ -845,7 +833,7 @@ void param_set_used_internal(param_t param) int param_reset(param_t param) { - struct param_wbuf_s *s = NULL; + param_wbuf_s *s = nullptr; bool param_found = false; param_lock_writer(); @@ -856,7 +844,7 @@ param_reset(param_t param) s = param_find_changed(param); /* if we found one, erase it */ - if (s != NULL) { + if (s != nullptr) { int pos = utarray_eltidx(param_values, s); utarray_erase(param_values, pos, 1); } @@ -868,7 +856,7 @@ param_reset(param_t param) param_unlock_writer(); - if (s != NULL) { + if (s != nullptr) { _param_notify_changes(); } @@ -879,12 +867,12 @@ param_reset_all_internal(bool auto_save) { param_lock_writer(); - if (param_values != NULL) { + if (param_values != nullptr) { utarray_free(param_values); } /* mark as reset / deleted */ - param_values = NULL; + param_values = nullptr; if (auto_save) { param_autosave(); @@ -896,7 +884,7 @@ param_reset_all_internal(bool auto_save) } void -param_reset_all(void) +param_reset_all() { param_reset_all_internal(true); } @@ -933,10 +921,10 @@ param_reset_excludes(const char *excludes[], int num_excludes) int param_set_default_file(const char *filename) { - if (param_user_file != NULL) { + if (param_user_file != nullptr) { // we assume this is not in use by some other thread free(param_user_file); - param_user_file = NULL; + param_user_file = nullptr; } if (filename) { @@ -947,13 +935,13 @@ param_set_default_file(const char *filename) } const char * -param_get_default_file(void) +param_get_default_file() { - return (param_user_file != NULL) ? param_user_file : param_default_file; + return (param_user_file != nullptr) ? param_user_file : param_default_file; } int -param_save_default(void) +param_save_default() { int res = PX4_ERROR; #if !defined(FLASH_BASED_PARAMS) @@ -998,7 +986,7 @@ param_save_default(void) * @return 0 on success, 1 if all params have not yet been stored, -1 if device open failed, -2 if writing parameters failed */ int -param_load_default(void) +param_load_default() { int res = 0; #if !defined(FLASH_BASED_PARAMS) @@ -1034,7 +1022,7 @@ param_export(int fd, bool only_unsaved) { perf_begin(param_export_perf); - struct param_wbuf_s *s = NULL; + param_wbuf_s *s = nullptr; int result = -1; struct bson_encoder_s encoder; @@ -1054,12 +1042,12 @@ param_export(int fd, bool only_unsaved) bson_encoder_init_buf_file(&encoder, fd, &bson_buffer, sizeof(bson_buffer)); /* no modified parameters -> we are done */ - if (param_values == NULL) { + if (param_values == nullptr) { result = 0; goto out; } - while ((s = (struct param_wbuf_s *)utarray_next(param_values, s)) != NULL) { + while ((s = (struct param_wbuf_s *)utarray_next(param_values, s)) != nullptr) { /* * If we are only saving values changed since last save, and this * one hasn't, then skip it @@ -1079,7 +1067,7 @@ param_export(int fd, bool only_unsaved) case PARAM_TYPE_INT32: { const int32_t i = s->val.i; - debug("exporting: %s (%d) size: %d val: %d", name, s->param, size, i); + PX4_DEBUG("exporting: %s (%d) size: %d val: %d", name, s->param, size, i); if (bson_encoder_append_int(&encoder, name, i)) { PX4_ERR("BSON append failed for '%s'", name); @@ -1091,7 +1079,7 @@ param_export(int fd, bool only_unsaved) case PARAM_TYPE_FLOAT: { const double f = (double)s->val.f; - debug("exporting: %s (%d) size: %d val: %.3f", name, s->param, size, (double)f); + PX4_DEBUG("exporting: %s (%d) size: %d val: %.3f", name, s->param, size, (double)f); if (bson_encoder_append_double(&encoder, name, f)) { PX4_ERR("BSON append failed for '%s'", name); @@ -1150,20 +1138,21 @@ struct param_import_state { }; static int -param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node) +param_import_callback(bson_decoder_t decoder, void *priv, bson_node_t node) { - float f; - int32_t i; - void *v, *tmp = NULL; + float f = 0.0f; + int32_t i = 0; + void *tmp = nullptr; + void *v = nullptr; int result = -1; - struct param_import_state *state = (struct param_import_state *)private; + param_import_state *state = (param_import_state *)priv; /* * EOO means the end of the parameter object. (Currently not supporting * nested BSON objects). */ if (node->type == BSON_EOO) { - debug("end of parameters"); + PX4_DEBUG("end of parameters"); return 0; } @@ -1174,7 +1163,7 @@ param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node) param_t param = param_find_no_notification(node->name); if (param == PARAM_INVALID) { - debug("ignoring unrecognised parameter '%s'", node->name); + PX4_ERR("ignoring unrecognised parameter '%s'", node->name); return 1; } @@ -1183,81 +1172,84 @@ param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node) */ switch (node->type) { - case BSON_INT32: - if (param_type(param) != PARAM_TYPE_INT32) { - PX4_WARN("unexpected type for %s", node->name); - result = 1; // just skip this entry - goto out; + case BSON_INT32: { + if (param_type(param) != PARAM_TYPE_INT32) { + PX4_WARN("unexpected type for %s", node->name); + result = 1; // just skip this entry + goto out; + } + + i = node->i; + v = &i; + + PX4_DEBUG("Imported %s with value %d", param_name(param), i); } - - i = node->i; - v = &i; - - debug("importing: %s (%d) = %d", node->name, param, i); break; - case BSON_DOUBLE: - if (param_type(param) != PARAM_TYPE_FLOAT) { - PX4_WARN("unexpected type for %s", node->name); - result = 1; // just skip this entry - goto out; + case BSON_DOUBLE: { + if (param_type(param) != PARAM_TYPE_FLOAT) { + PX4_WARN("unexpected type for %s", node->name); + result = 1; // just skip this entry + goto out; + } + + f = node->d; + v = &f; + + PX4_DEBUG("Imported %s with value %f", param_name(param), (double)f); } - - f = node->d; - v = &f; - - debug("importing: %s (%d) = %.3f", node->name, param, (double)f); break; - case BSON_BINDATA: - if (node->subtype != BSON_BIN_BINARY) { - PX4_WARN("unexpected subtype for %s", node->name); - result = 1; // just skip this entry - goto out; + case BSON_BINDATA: { + if (node->subtype != BSON_BIN_BINARY) { + PX4_WARN("unexpected subtype for %s", node->name); + result = 1; // just skip this entry + goto out; + } + + if (bson_decoder_data_pending(decoder) != param_size(param)) { + PX4_WARN("bad size for '%s'", node->name); + result = 1; // just skip this entry + goto out; + } + + /* XXX check actual file data size? */ + size_t psize = param_size(param); + + if (psize > 0) { + tmp = malloc(psize); + + } else { + tmp = nullptr; + } + + if (tmp == nullptr) { + PX4_ERR("failed allocating for '%s'", node->name); + goto out; + } + + if (bson_decoder_copy_data(decoder, tmp)) { + PX4_ERR("failed copying data for '%s'", node->name); + goto out; + } + + v = tmp; } - - if (bson_decoder_data_pending(decoder) != param_size(param)) { - PX4_WARN("bad size for '%s'", node->name); - result = 1; // just skip this entry - goto out; - } - - /* XXX check actual file data size? */ - size_t psize = param_size(param); - - if (psize > 0) { - tmp = malloc(psize); - - } else { - tmp = NULL; - } - - if (tmp == NULL) { - PX4_ERR("failed allocating for '%s'", node->name); - goto out; - } - - if (bson_decoder_copy_data(decoder, tmp)) { - PX4_ERR("failed copying data for '%s'", node->name); - goto out; - } - - v = tmp; break; default: - debug("unrecognised node type"); + PX4_DEBUG("unrecognised node type"); goto out; } if (param_set_internal(param, v, state->mark_saved, true)) { - debug("error setting value for '%s'", node->name); + PX4_DEBUG("error setting value for '%s'", node->name); goto out; } - if (tmp != NULL) { + if (tmp != nullptr) { free(tmp); - tmp = NULL; + tmp = nullptr; } /* don't return zero, that means EOF */ @@ -1265,7 +1257,7 @@ param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node) out: - if (tmp != NULL) { + if (tmp != nullptr) { free(tmp); } @@ -1275,13 +1267,13 @@ out: static int param_import_internal(int fd, bool mark_saved) { - struct bson_decoder_s decoder; - struct param_import_state state; + bson_decoder_s decoder; + param_import_state state; int result = -1; if (bson_decoder_init_file(&decoder, fd, param_import_callback, &state)) { PX4_ERR("decoder init failed"); - return -ENODATA; + return PX4_ERROR; } state.mark_saved = mark_saved; @@ -1322,7 +1314,7 @@ param_foreach(void (*func)(void *arg, param_t param), void *arg, bool only_chang for (param = 0; handle_in_range(param); param++) { /* if requested, skip unchanged values */ - if (only_changed && (param_find_changed(param) == NULL)) { + if (only_changed && (param_find_changed(param) == nullptr)) { continue; } @@ -1334,7 +1326,7 @@ param_foreach(void (*func)(void *arg, param_t param), void *arg, bool only_chang } } -uint32_t param_hash_check(void) +uint32_t param_hash_check() { uint32_t param_hash = 0; @@ -1349,7 +1341,7 @@ uint32_t param_hash_check(void) const char *name = param_name(param); const void *val = param_get_value_ptr(param); param_hash = crc32part((const uint8_t *)name, strlen(name), param_hash); - param_hash = crc32part(val, param_size(param), param_hash); + param_hash = crc32part((const uint8_t *)val, param_size(param), param_hash); } param_unlock_reader(); diff --git a/src/lib/parameters/param_shmem.c b/src/lib/parameters/parameters_shmem.cpp similarity index 66% rename from src/lib/parameters/param_shmem.c rename to src/lib/parameters/parameters_shmem.cpp index 85726fe76f..0c83371e72 100644 --- a/src/lib/parameters/param_shmem.c +++ b/src/lib/parameters/parameters_shmem.cpp @@ -41,32 +41,37 @@ * and background parameter saving. */ -//#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include - #include "param.h" -#include "systemlib/uthash/utarray.h" +#include #include "tinybson/tinybson.h" -#include "uORB/uORB.h" -#include "uORB/topics/parameter_update.h" -#include - #include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +//#define PARAM_NO_ORB ///< if defined, avoid uorb dependency. This disables publication of parameter_update on param change +//#define PARAM_NO_AUTOSAVE ///< if defined, do not autosave (avoids LP work queue dependency) + +#if !defined(PARAM_NO_ORB) +# include "uORB/uORB.h" +# include "uORB/topics/parameter_update.h" +#endif + +#if defined(FLASH_BASED_PARAMS) +#include "flashparams/flashparams.h" +#endif + +#include #include "shmem.h" @@ -75,9 +80,7 @@ static const char *param_default_file = "/dev/fs/params"; #else static const char *param_default_file = "/usr/share/data/adsp/params"; #endif -static char *param_user_file = NULL; - -#define debug(fmt, args...) do { } while(0) +static char *param_user_file = nullptr; #ifdef __PX4_QURT //Mode not supported by qurt @@ -87,18 +90,20 @@ static char *param_user_file = NULL; #endif #define PARAM_CLOSE close +#ifndef PARAM_NO_AUTOSAVE #include /* autosaving variables */ static hrt_abstime last_autosave_timestamp = 0; static struct work_s autosave_work; static bool autosave_scheduled = false; static bool autosave_disabled = false; +#endif /* PARAM_NO_AUTOSAVE */ /** * Array of static parameter info. */ -static struct param_info_s *param_info_base = (struct param_info_s *) &px4_parameters; -#define param_info_count px4_parameters.param_count +static const param_info_s *param_info_base = (const param_info_s *) &px4_parameters; +#define param_info_count px4_parameters.param_count /** * Storage for modified parameters. @@ -110,7 +115,7 @@ struct param_wbuf_s { }; -uint8_t *param_changed_storage = 0; +uint8_t *param_changed_storage = nullptr; int size_param_changed_storage_bytes = 0; const int bits_per_allocation_unit = (sizeof(*param_changed_storage) * 8); @@ -119,20 +124,17 @@ const int bits_per_allocation_unit = (sizeof(*param_changed_storage) * 8); extern int get_shmem_lock(const char *caller_file_name, int caller_line_number); extern void release_shmem_lock(const char *caller_file_name, int caller_line_number); -struct param_wbuf_s *param_find_changed(param_t param); +static void init_params(); +extern void init_shared_memory(); -void init_params(void); -extern void init_shared_memory(void); - -extern void copy_params_to_shmem(struct param_info_s *param_info_base); +extern void copy_params_to_shmem(const param_info_s *param_info_base); extern struct shmem_info *shmem_info_p; uint64_t sync_other_prev_time = 0, sync_other_current_time = 0; -int param_instance = 0; extern void update_to_shmem(param_t param, union param_value_u value); extern int update_from_shmem(param_t param, union param_value_u *value); -extern void update_index_from_shmem(void); +extern void update_index_from_shmem(); static int param_set_internal(param_t param, const void *val, bool mark_saved, bool notify_changes); unsigned char set_called_from_get = 0; @@ -140,20 +142,21 @@ unsigned char set_called_from_get = 0; static int param_import_done = 0; /*at startup, params are loaded from file, if present. we dont want to send notifications that time since muorb is not ready*/ -static int param_load_default_no_notify(void); +static int param_load_default_no_notify(); static unsigned -get_param_info_count(void) +get_param_info_count() { /* Singleton creation of and array of bits to track changed values */ if (!param_changed_storage) { + /* Note that we have a (highly unlikely) race condition here: in the worst case the allocation is done twice */ size_param_changed_storage_bytes = (param_info_count / bits_per_allocation_unit) + 1; - param_changed_storage = calloc(size_param_changed_storage_bytes, 1); + param_changed_storage = (uint8_t *)calloc(size_param_changed_storage_bytes, 1); /* If the allocation fails we need to indicate failure in the * API by returning PARAM_INVALID */ - if (param_changed_storage == NULL) { + if (param_changed_storage == nullptr) { return 0; } } @@ -162,49 +165,122 @@ get_param_info_count(void) } /** flexible array holding modified parameter values */ -UT_array *param_values; +UT_array *param_values{nullptr}; /** array info for the modified parameters array */ -const UT_icd param_icd = {sizeof(struct param_wbuf_s), NULL, NULL, NULL}; +const UT_icd param_icd = {sizeof(param_wbuf_s), nullptr, nullptr, nullptr}; +#if !defined(PARAM_NO_ORB) /** parameter update topic handle */ -static orb_advert_t param_topic = NULL; +static orb_advert_t param_topic = nullptr; +static unsigned int param_instance = 0; +#endif static void param_set_used_internal(param_t param); static param_t param_find_internal(const char *name, bool notification); // TODO: not working on Snappy just yet -//static px4_sem_t param_sem; ///< this protects against concurrent access to param_values and param save +// the following implements an RW-lock using 2 semaphores (used as mutexes). It gives +// priority to readers, meaning a writer could suffer from starvation, but in our use-case +// we only have short periods of reads and writes are rare. +//static px4_sem_t param_sem; ///< this protects against concurrent access to param_values +//static int reader_lock_holders = 0; +//static px4_sem_t reader_lock_holders_lock; ///< this protects against concurrent access to reader_lock_holders -/** lock the parameter store */ +static perf_counter_t param_export_perf; +static perf_counter_t param_find_perf; +static perf_counter_t param_get_perf; +static perf_counter_t param_set_perf; + +//static px4_sem_t param_sem_save; ///< this protects against concurrent param saves (file or flash access). +///< we use a separate lock to allow concurrent param reads and saves. +///< a param_set could still be blocked by a param save, because it +///< needs to take the reader lock + +/** lock the parameter store for read access */ static void -param_lock(void) +param_lock_reader() { // TODO: this doesn't seem to work on Snappy - //do {} while (px4_sem_wait(¶m_sem) != 0); +#if 0 + do {} while (px4_sem_wait(&reader_lock_holders_lock) != 0); + + ++reader_lock_holders; + + if (reader_lock_holders == 1) { + // the first reader takes the lock, the next ones are allowed to just continue + do {} while (px4_sem_wait(¶m_sem) != 0); + } + + px4_sem_post(&reader_lock_holders_lock); +#endif +} + +/** lock the parameter store for write access */ +static void +param_lock_writer() +{ + // TODO: this doesn't seem to work on Snappy +#if 0 + do {} while (px4_sem_wait(¶m_sem) != 0); + +#endif } /** unlock the parameter store */ static void -param_unlock(void) +param_unlock_reader() { // TODO: this doesn't seem to work on Snappy - //px4_sem_post(¶m_sem); +#if 0 + do {} while (px4_sem_wait(&reader_lock_holders_lock) != 0); + + --reader_lock_holders; + + if (reader_lock_holders == 0) { + // the last reader releases the lock + px4_sem_post(¶m_sem); + } + + px4_sem_post(&reader_lock_holders_lock); +#endif +} + +/** unlock the parameter store */ +static void +param_unlock_writer() +{ + // TODO: this doesn't seem to work on Snappy +#if 0 + px4_sem_post(¶m_sem); +#endif } /** assert that the parameter store is locked */ static void -param_assert_locked(void) +param_assert_locked() { - /* TODO */ + /* XXX */ } void -param_init(void) +param_init() { // TODO: not needed on Snappy yet. - // px4_sem_init(¶m_sem, 0, 1); + //px4_sem_init(¶m_sem, 0, 1); + //px4_sem_init(¶m_sem_save, 0, 1); + //px4_sem_init(&reader_lock_holders_lock, 0, 1); + + param_export_perf = perf_alloc(PC_ELAPSED, "param_export"); + param_find_perf = perf_alloc(PC_ELAPSED, "param_find"); + param_get_perf = perf_alloc(PC_ELAPSED, "param_get"); + param_set_perf = perf_alloc(PC_ELAPSED, "param_set"); + +#ifdef CONFIG_SHMEM + PX4_DEBUG("Syncing params to shared memory\n"); + init_params(); +#endif } /** @@ -216,7 +292,7 @@ param_init(void) bool handle_in_range(param_t param) { - int count = get_param_info_count(); + unsigned count = get_param_info_count(); return (count && param < count); } @@ -247,53 +323,57 @@ param_compare_values(const void *a, const void *b) * * @param param The parameter being searched. * @return The structure holding the modified value, or - * NULL if the parameter has not been modified. + * nullptr if the parameter has not been modified. */ -struct param_wbuf_s * +static param_wbuf_s * param_find_changed(param_t param) { - struct param_wbuf_s *s = NULL; + param_wbuf_s *s = nullptr; param_assert_locked(); - if (param_values != NULL) { - while ((s = (struct param_wbuf_s *)utarray_next(param_values, s)) != NULL) { - if (s->param == param) { - break; - } - } + if (param_values != nullptr) { + param_wbuf_s key{}; + key.param = param; + s = (param_wbuf_s *)utarray_find(param_values, &key, param_compare_values); } return s; } static void -_param_notify_changes(void) +_param_notify_changes() { - struct parameter_update_s pup = { .timestamp = hrt_absolute_time(), .instance = param_instance++ }; +#if !defined(PARAM_NO_ORB) + parameter_update_s pup = {}; + pup.timestamp = hrt_absolute_time(); + pup.instance = param_instance++; /* * If we don't have a handle to our topic, create one now; otherwise * just publish. */ - if (param_topic == NULL) { + if (param_topic == nullptr) { param_topic = orb_advertise(ORB_ID(parameter_update), &pup); } else { orb_publish(ORB_ID(parameter_update), param_topic, &pup); } + +#endif } void -param_notify_changes(void) +param_notify_changes() { _param_notify_changes(); } - param_t param_find_internal(const char *name, bool notification) { + perf_begin(param_find_perf); + param_t param; /* perform a linear search of the known parameters */ @@ -303,10 +383,13 @@ param_find_internal(const char *name, bool notification) param_set_used_internal(param); } + perf_end(param_find_perf); return param; } } + perf_end(param_find_perf); + /* not found */ return PARAM_INVALID; } @@ -324,13 +407,13 @@ param_find_no_notification(const char *name) } unsigned -param_count(void) +param_count() { return get_param_info_count(); } unsigned -param_count_used(void) +param_count_used() { //TODO FIXME: all params used right now #if 0 @@ -339,8 +422,8 @@ param_count_used(void) // ensure the allocation has been done if (get_param_info_count()) { - for (unsigned i = 0; i < size_param_changed_storage_bytes; i++) { - for (unsigned j = 0; j < bits_per_allocation_unit; j++) { + for (int i = 0; i < size_param_changed_storage_bytes; i++) { + for (int j = 0; j < bits_per_allocation_unit; j++) { if (param_changed_storage[i] & (1 << j)) { count++; } @@ -372,12 +455,12 @@ param_for_used_index(unsigned index) #if 0 int count = get_param_info_count(); - if (count && index < count) { + if (count && (int)index < count) { /* walk all params and count used params */ unsigned used_count = 0; - for (unsigned i = 0; i < (unsigned)size_param_changed_storage_bytes; i++) { - for (unsigned j = 0; j < bits_per_allocation_unit; j++) { + for (int i = 0; i < size_param_changed_storage_bytes; i++) { + for (int j = 0; j < bits_per_allocation_unit; j++) { if (param_changed_storage[i] & (1 << j)) { /* we found the right used count, @@ -422,12 +505,11 @@ param_get_used_index(param_t param) /* walk all params and count, now knowing that it has a valid index */ int used_count = 0; - for (unsigned i = 0; i < (unsigned)size_param_changed_storage_bytes; i++) { - for (unsigned j = 0; j < bits_per_allocation_unit; j++) { - + for (int i = 0; i < size_param_changed_storage_bytes; i++) { + for (int j = 0; j < bits_per_allocation_unit; j++) { if (param_changed_storage[i] & (1 << j)) { - if ((unsigned)param == i * bits_per_allocation_unit + j) { + if ((int)param == i * bits_per_allocation_unit + j) { return used_count; } @@ -446,16 +528,22 @@ param_get_used_index(param_t param) const char * param_name(param_t param) { - return handle_in_range(param) ? param_info_base[param].name : NULL; + return handle_in_range(param) ? param_info_base[param].name : nullptr; +} + +bool +param_is_volatile(param_t param) +{ + return handle_in_range(param) ? param_info_base[param].volatile_param : false; } bool param_value_is_default(param_t param) { struct param_wbuf_s *s; - param_lock(); + param_lock_reader(); s = param_find_changed(param); - param_unlock(); + param_unlock_reader(); return s ? false : true; } @@ -463,10 +551,10 @@ bool param_value_unsaved(param_t param) { struct param_wbuf_s *s; - param_lock(); + param_lock_reader(); s = param_find_changed(param); bool ret = s && s->unsaved; - param_unlock(); + param_unlock_reader(); return ret; } @@ -503,13 +591,13 @@ param_size(param_t param) * Obtain a pointer to the storage allocated for a parameter. * * @param param The parameter whose storage is sought. - * @return A pointer to the parameter value, or NULL + * @return A pointer to the parameter value, or nullptr * if the parameter does not exist. */ static const void * param_get_value_ptr(param_t param) { - const void *result = NULL; + const void *result = nullptr; param_assert_locked(); @@ -520,7 +608,7 @@ param_get_value_ptr(param_t param) /* work out whether we're fetching the default or a written value */ struct param_wbuf_s *s = param_find_changed(param); - if (s != NULL) { + if (s != nullptr) { v = &s->val; } else { @@ -545,7 +633,8 @@ param_get(param_t param, void *val) { int result = -1; - param_lock(); + param_lock_reader(); + perf_begin(param_get_perf); if (!handle_in_range(param)) { return result; @@ -559,7 +648,6 @@ param_get(param_t param, void *val) set_called_from_get = 0; } - const void *v = param_get_value_ptr(param); if (val && v) { @@ -583,25 +671,27 @@ param_get(param_t param, void *val) #endif - param_unlock(); + perf_end(param_get_perf); + param_unlock_reader(); return result; } - +#ifndef PARAM_NO_AUTOSAVE /** * worker callback method to save the parameters * @param arg unused */ -static void autosave_worker(void *arg) +static void +autosave_worker(void *arg) { bool disabled = false; - param_lock(); + param_lock_writer(); last_autosave_timestamp = hrt_absolute_time(); autosave_scheduled = false; disabled = autosave_disabled; - param_unlock(); + param_unlock_writer(); if (disabled) { return; @@ -614,6 +704,7 @@ static void autosave_worker(void *arg) PX4_ERR("param save failed (%i)", ret); } } +#endif /* PARAM_NO_AUTOSAVE */ /** * Automatically save the parameters after a timeout and limited rate. @@ -621,8 +712,11 @@ static void autosave_worker(void *arg) * This needs to be called with the writer lock held (it's not necessary that it's the writer lock, but it * needs to be the same lock as autosave_worker() and param_control_autosave() use). */ -static void param_autosave(void) +static void +param_autosave() { +#ifndef PARAM_NO_AUTOSAVE + if (autosave_scheduled || autosave_disabled) { return; } @@ -641,13 +735,15 @@ static void param_autosave(void) } autosave_scheduled = true; - work_queue(LPWORK, &autosave_work, (worker_t)&autosave_worker, NULL, USEC2TICK(delay)); + work_queue(LPWORK, &autosave_work, (worker_t)&autosave_worker, nullptr, USEC2TICK(delay)); +#endif /* PARAM_NO_AUTOSAVE */ } void param_control_autosave(bool enable) { - param_lock(); +#ifndef PARAM_NO_AUTOSAVE + param_lock_writer(); if (!enable && autosave_scheduled) { work_cancel(LPWORK, &autosave_work); @@ -655,7 +751,8 @@ param_control_autosave(bool enable) } autosave_disabled = !enable; - param_unlock(); + param_unlock_writer(); +#endif /* PARAM_NO_AUTOSAVE */ } static int @@ -664,36 +761,29 @@ param_set_internal(param_t param, const void *val, bool mark_saved, bool notify_ int result = -1; bool params_changed = false; - PX4_DEBUG("param_set_internal params: param = %d, val = 0x%X, mark_saved: %d, notify_changes: %d", - param, val, (int)mark_saved, (int)notify_changes); + param_lock_writer(); + perf_begin(param_set_perf); - param_lock(); - - if (!handle_in_range(param)) { - return result; - } - - if (param_values == NULL) { + if (param_values == nullptr) { utarray_new(param_values, ¶m_icd); } - if (param_values == NULL) { - debug("failed to allocate modified values array"); + if (param_values == nullptr) { + PX4_ERR("failed to allocate modified values array"); goto out; } if (handle_in_range(param)) { - struct param_wbuf_s *s = param_find_changed(param); + param_wbuf_s *s = param_find_changed(param); - if (s == NULL) { + if (s == nullptr) { /* construct a new parameter */ - struct param_wbuf_s buf = { - .param = param, - .val.p = NULL, - .unsaved = false - }; + param_wbuf_s buf = {}; + buf.param = param; + + params_changed = true; /* add it to the array and sort */ utarray_push_back(param_values, &buf); @@ -707,24 +797,34 @@ param_set_internal(param_t param, const void *val, bool mark_saved, bool notify_ switch (param_type(param)) { case PARAM_TYPE_INT32: + params_changed = params_changed || s->val.i != *(int32_t *)val; s->val.i = *(int32_t *)val; break; case PARAM_TYPE_FLOAT: + params_changed = params_changed || fabsf(s->val.f - * (float *)val) > FLT_EPSILON; s->val.f = *(float *)val; break; case PARAM_TYPE_STRUCT ... PARAM_TYPE_STRUCT_MAX: - if (s->val.p == NULL) { - s->val.p = malloc(param_size(param)); + if (s->val.p == nullptr) { + size_t psize = param_size(param); - if (s->val.p == NULL) { - debug("failed to allocate parameter storage"); + if (psize > 0) { + s->val.p = malloc(psize); + + } else { + s->val.p = nullptr; + } + + if (s->val.p == nullptr) { + PX4_ERR("failed to allocate parameter storage"); goto out; } } memcpy(s->val.p, val, param_size(param)); + params_changed = true; break; default: @@ -732,7 +832,6 @@ param_set_internal(param_t param, const void *val, bool mark_saved, bool notify_ } s->unsaved = !mark_saved; - params_changed = true; result = 0; if (!mark_saved) { // this is false when importing parameters @@ -741,7 +840,8 @@ param_set_internal(param_t param, const void *val, bool mark_saved, bool notify_ } out: - param_unlock(); + perf_end(param_set_perf); + param_unlock_writer(); /* * If we set something, now that we have unlocked, go ahead and advertise that @@ -777,6 +877,18 @@ out: return result; } +#if defined(FLASH_BASED_PARAMS) +int param_set_external(param_t param, const void *val, bool mark_saved, bool notify_changes) +{ + return param_set_internal(param, val, mark_saved, notify_changes); +} + +const void *param_get_value_ptr_external(param_t param) +{ + return param_get_value_ptr(param); +} +#endif + int param_set(param_t param, const void *val) { @@ -818,6 +930,7 @@ void param_set_used_internal(param_t param) return; } + // FIXME: this needs locking too param_changed_storage[param_index / bits_per_allocation_unit] |= (1 << param_index % bits_per_allocation_unit); } @@ -825,10 +938,10 @@ void param_set_used_internal(param_t param) int param_reset(param_t param) { - struct param_wbuf_s *s = NULL; + param_wbuf_s *s = nullptr; bool param_found = false; - param_lock(); + param_lock_writer(); if (handle_in_range(param)) { @@ -836,7 +949,7 @@ param_reset(param_t param) s = param_find_changed(param); /* if we found one, erase it */ - if (s != NULL) { + if (s != nullptr) { int pos = utarray_eltidx(param_values, s); utarray_erase(param_values, pos, 1); } @@ -846,9 +959,9 @@ param_reset(param_t param) param_autosave(); - param_unlock(); + param_unlock_writer(); - if (s != NULL) { + if (s != nullptr) { _param_notify_changes(); } @@ -857,26 +970,26 @@ param_reset(param_t param) static void param_reset_all_internal(bool auto_save) { - param_lock(); + param_lock_writer(); - if (param_values != NULL) { + if (param_values != nullptr) { utarray_free(param_values); } /* mark as reset / deleted */ - param_values = NULL; + param_values = nullptr; if (auto_save) { param_autosave(); } - param_unlock(); + param_unlock_writer(); _param_notify_changes(); } void -param_reset_all(void) +param_reset_all() { param_reset_all_internal(true); } @@ -896,6 +1009,7 @@ param_reset_excludes(const char *excludes[], int num_excludes) if ((excludes[index][len - 1] == '*' && strncmp(name, excludes[index], len - 1) == 0) || strcmp(name, excludes[index]) == 0) { + exclude = true; break; } @@ -912,10 +1026,10 @@ param_reset_excludes(const char *excludes[], int num_excludes) int param_set_default_file(const char *filename) { - if (param_user_file != NULL) { + if (param_user_file != nullptr) { // we assume this is not in use by some other thread free(param_user_file); - param_user_file = NULL; + param_user_file = nullptr; } if (filename) { @@ -926,13 +1040,13 @@ param_set_default_file(const char *filename) } const char * -param_get_default_file(void) +param_get_default_file() { - return (param_user_file != NULL) ? param_user_file : param_default_file; + return (param_user_file != nullptr) ? param_user_file : param_default_file; } int -param_save_default(void) +param_save_default() { int res = OK; int fd = -1; @@ -975,8 +1089,10 @@ do_exit: * @return 0 on success, 1 if all params have not yet been stored, -1 if device open failed, -2 if writing parameters failed */ int -param_load_default(void) +param_load_default() { + int res = 0; +#if !defined(FLASH_BASED_PARAMS) int fd_load = PARAM_OPEN(param_get_default_file(), O_RDONLY); if (fd_load < 0) { @@ -990,7 +1106,6 @@ param_load_default(void) } int result = param_load(fd_load); - PARAM_CLOSE(fd_load); if (result != 0) { @@ -998,14 +1113,18 @@ param_load_default(void) return -2; } - return 0; +#else + // no need for locking + res = flash_param_load(); +#endif + return res; } /** * @return 0 on success, 1 if all params have not yet been stored, -1 if device open failed, -2 if writing parameters failed */ static int -param_load_default_no_notify(void) +param_load_default_no_notify() { int fd_load = open(param_get_default_file(), O_RDONLY); @@ -1040,22 +1159,28 @@ param_load_default_no_notify(void) int param_export(int fd, bool only_unsaved) { - struct param_wbuf_s *s = NULL; - struct bson_encoder_s encoder; + perf_begin(param_export_perf); + + param_wbuf_s *s = nullptr; int result = -1; + struct bson_encoder_s encoder; + int shutdown_lock_ret = px4_shutdown_lock(); if (shutdown_lock_ret) { PX4_ERR("px4_shutdown_lock() failed (%i)", shutdown_lock_ret); } - param_lock(); + // take the file lock + //do {} while (px4_sem_wait(¶m_sem_save) != 0); + + param_lock_reader(); bson_encoder_init_file(&encoder, fd); /* no modified parameters -> we are done */ - if (param_values == NULL) { + if (param_values == nullptr) { result = 0; goto out; } @@ -1064,11 +1189,7 @@ param_export(int fd, bool only_unsaved) * that have recently been changed. */ update_index_from_shmem(); - while ((s = (struct param_wbuf_s *)utarray_next(param_values, s)) != NULL) { - - int32_t i; - float f; - + while ((s = (struct param_wbuf_s *)utarray_next(param_values, s)) != nullptr) { /* * If we are only saving values changed since last save, and this * one hasn't, then skip it @@ -1082,44 +1203,54 @@ param_export(int fd, bool only_unsaved) /* Make sure to get latest from shmem before saving. */ update_from_shmem(s->param, &s->val); - /* append the appropriate BSON type object */ + const char *name = param_name(s->param); + const size_t size = param_size(s->param); + /* append the appropriate BSON type object */ switch (param_type(s->param)) { - case PARAM_TYPE_INT32: - i = s->val.i; + case PARAM_TYPE_INT32: { + const int32_t i = s->val.i; - if (bson_encoder_append_int(&encoder, param_name(s->param), i)) { - PX4_DEBUG("BSON append failed for '%s'", param_name(s->param)); - goto out; + PX4_DEBUG("exporting: %s (%d) size: %d val: %d", name, s->param, size, i); + + if (bson_encoder_append_int(&encoder, name, i)) { + PX4_ERR("BSON append failed for '%s'", name); + goto out; + } } - break; - case PARAM_TYPE_FLOAT: - f = s->val.f; + case PARAM_TYPE_FLOAT: { + const double f = (double)s->val.f; - if (bson_encoder_append_double(&encoder, param_name(s->param), f)) { - PX4_DEBUG("BSON append failed for '%s'", param_name(s->param)); - goto out; + PX4_DEBUG("exporting: %s (%d) size: %d val: %.3f", name, s->param, size, (double)f); + + if (bson_encoder_append_double(&encoder, name, f)) { + PX4_ERR("BSON append failed for '%s'", name); + goto out; + } } - break; - case PARAM_TYPE_STRUCT ... PARAM_TYPE_STRUCT_MAX: - if (bson_encoder_append_binary(&encoder, - param_name(s->param), - BSON_BIN_BINARY, - param_size(s->param), - param_get_value_ptr(s->param))) { - PX4_DEBUG("BSON append failed for '%s'", param_name(s->param)); - goto out; - } + case PARAM_TYPE_STRUCT ... PARAM_TYPE_STRUCT_MAX: { + const void *value_ptr = param_get_value_ptr(s->param); + /* lock as short as possible */ + if (bson_encoder_append_binary(&encoder, + name, + BSON_BIN_BINARY, + size, + value_ptr)) { + + PX4_ERR("BSON append failed for '%s'", name); + goto out; + } + } break; default: - PX4_DEBUG("unrecognized parameter type"); + PX4_ERR("unrecognized parameter type"); goto out; } } @@ -1127,7 +1258,9 @@ param_export(int fd, bool only_unsaved) result = 0; out: - param_unlock(); + param_unlock_reader(); + + //px4_sem_post(¶m_sem_save); fsync(fd); // make sure the data is flushed before releasing the shutdown lock @@ -1139,6 +1272,8 @@ out: result = bson_encoder_fini(&encoder); } + perf_end(param_export_perf); + return result; } @@ -1147,13 +1282,14 @@ struct param_import_state { }; static int -param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node) +param_import_callback(bson_decoder_t decoder, void *priv, bson_node_t node) { - float f; - int32_t i; - void *v, *tmp = NULL; + float f = 0.0f; + int32_t i = 0; + void *tmp = nullptr; + void *v = nullptr; int result = -1; - struct param_import_state *state = (struct param_import_state *)private; + param_import_state *state = (param_import_state *)priv; /* * EOO means the end of the parameter object. (Currently not supporting @@ -1171,7 +1307,7 @@ param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node) param_t param = param_find_no_notification(node->name); if (param == PARAM_INVALID) { - PX4_DEBUG("ignoring unrecognised parameter '%s'", node->name); + PX4_ERR("ignoring unrecognised parameter '%s'", node->name); return 1; } @@ -1180,57 +1316,69 @@ param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node) */ switch (node->type) { - case BSON_INT32: - if (param_type(param) != PARAM_TYPE_INT32) { - PX4_WARN("unexpected type for %s", node->name); - result = 1; // just skip this entry - goto out; - } + case BSON_INT32: { + if (param_type(param) != PARAM_TYPE_INT32) { + PX4_WARN("unexpected type for %s", node->name); + result = 1; // just skip this entry + goto out; + } - i = node->i; - v = &i; - PX4_DEBUG("Imported %s with value %d", param_name(param), i); + i = node->i; + v = &i; + + PX4_DEBUG("Imported %s with value %d", param_name(param), i); + } break; - case BSON_DOUBLE: - if (param_type(param) != PARAM_TYPE_FLOAT) { - PX4_WARN("unexpected type for %s", node->name); - result = 1; // just skip this entry - goto out; - } + case BSON_DOUBLE: { + if (param_type(param) != PARAM_TYPE_FLOAT) { + PX4_WARN("unexpected type for %s", node->name); + result = 1; // just skip this entry + goto out; + } - f = node->d; - v = &f; - PX4_DEBUG("Imported %s with value %f", param_name(param), (double)f); + f = node->d; + v = &f; + + PX4_DEBUG("Imported %s with value %f", param_name(param), (double)f); + } break; - case BSON_BINDATA: - if (node->subtype != BSON_BIN_BINARY) { - PX4_WARN("unexpected type for %s", node->name); - result = 1; // just skip this entry - goto out; + case BSON_BINDATA: { + if (node->subtype != BSON_BIN_BINARY) { + PX4_WARN("unexpected subtype for %s", node->name); + result = 1; // just skip this entry + goto out; + } + + if (bson_decoder_data_pending(decoder) != param_size(param)) { + PX4_WARN("bad size for '%s'", node->name); + result = 1; // just skip this entry + goto out; + } + + /* XXX check actual file data size? */ + size_t psize = param_size(param); + + if (psize > 0) { + tmp = malloc(psize); + + } else { + tmp = nullptr; + } + + if (tmp == nullptr) { + PX4_ERR("failed allocating for '%s'", node->name); + goto out; + } + + if (bson_decoder_copy_data(decoder, tmp)) { + PX4_ERR("failed copying data for '%s'", node->name); + goto out; + } + + v = tmp; } - - if (bson_decoder_data_pending(decoder) != param_size(param)) { - PX4_WARN("bad size for '%s'", node->name); - result = 1; // just skip this entry - goto out; - } - - /* XXX check actual file data size? */ - tmp = malloc(param_size(param)); - - if (tmp == NULL) { - PX4_DEBUG("failed allocating for '%s'", node->name); - goto out; - } - - if (bson_decoder_copy_data(decoder, tmp)) { - PX4_DEBUG("failed copying data for '%s'", node->name); - goto out; - } - - v = tmp; break; default: @@ -1243,9 +1391,9 @@ param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node) goto out; } - if (tmp != NULL) { + if (tmp != nullptr) { free(tmp); - tmp = NULL; + tmp = nullptr; } /* don't return zero, that means EOF */ @@ -1253,7 +1401,7 @@ param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node) out: - if (tmp != NULL) { + if (tmp != nullptr) { free(tmp); } @@ -1263,35 +1411,36 @@ out: static int param_import_internal(int fd, bool mark_saved) { - struct bson_decoder_s decoder; + bson_decoder_s decoder; + param_import_state state; int result = -1; - struct param_import_state state; if (bson_decoder_init_file(&decoder, fd, param_import_callback, &state)) { - PX4_DEBUG("decoder init failed"); - goto out; + PX4_ERR("decoder init failed"); + return PX4_ERROR; } state.mark_saved = mark_saved; do { result = bson_decoder_next(&decoder); + usleep(1); } while (result > 0); -out: - - if (result < 0) { - PX4_DEBUG("BSON error decoding parameters"); - } - return result; } int param_import(int fd) { +#if !defined(FLASH_BASED_PARAMS) return param_import_internal(fd, false); +#else + (void)fd; // unused + // no need for locking here + return flash_param_import(); +#endif } int @@ -1309,7 +1458,7 @@ param_foreach(void (*func)(void *arg, param_t param), void *arg, bool only_chang for (param = 0; handle_in_range(param); param++) { /* if requested, skip unchanged values */ - if (only_changed && (param_find_changed(param) == NULL)) { + if (only_changed && (param_find_changed(param) == nullptr)) { continue; } @@ -1321,30 +1470,30 @@ param_foreach(void (*func)(void *arg, param_t param), void *arg, bool only_chang } } -uint32_t param_hash_check(void) +uint32_t param_hash_check() { uint32_t param_hash = 0; - param_lock(); + param_lock_reader(); /* compute the CRC32 over all string param names and 4 byte values */ for (param_t param = 0; handle_in_range(param); param++) { - if (!param_used(param)) { + if (!param_used(param) || param_is_volatile(param)) { continue; } const char *name = param_name(param); const void *val = param_get_value_ptr(param); param_hash = crc32part((const uint8_t *)name, strlen(name), param_hash); - param_hash = crc32part(val, sizeof(union param_value_u), param_hash); + param_hash = crc32part((const uint8_t *)val, param_size(param), param_hash); } - param_unlock(); + param_unlock_reader(); return param_hash; } -void init_params(void) +void init_params() { #ifdef __PX4_QURT //copy params to shared memory @@ -1355,19 +1504,19 @@ void init_params(void) #ifdef __PX4_POSIX param_load_default_no_notify(); #endif + param_import_done = 1; #ifdef __PX4_QURT copy_params_to_shmem(param_info_base); - #ifdef ENABLE_SHMEM_DEBUG PX4_INFO("Offsets:"); PX4_INFO("params_val %lu, krait_changed %lu, adsp_changed %lu", (unsigned char *)shmem_info_p->params_val - (unsigned char *)shmem_info_p, (unsigned char *)&shmem_info_p->krait_changed_index - (unsigned char *)shmem_info_p, (unsigned char *)&shmem_info_p->adsp_changed_index - (unsigned char *)shmem_info_p); -#endif -#endif -} +#endif /* ENABLE_SHMEM_DEBUG */ +#endif /* __PX4_QURT */ +} diff --git a/src/lib/parameters/tinybson/CMakeLists.txt b/src/lib/parameters/tinybson/CMakeLists.txt index 8fb9a8c1f7..a8d5b43e79 100644 --- a/src/lib/parameters/tinybson/CMakeLists.txt +++ b/src/lib/parameters/tinybson/CMakeLists.txt @@ -31,7 +31,7 @@ # ############################################################################ -add_library(tinybson tinybson.c) +add_library(tinybson tinybson.cpp) target_compile_definitions(tinybson PRIVATE -DMODULE_NAME="tinybson") target_compile_options(tinybson PRIVATE -Wno-sign-compare) # TODO: fix this add_dependencies(tinybson prebuild_targets) diff --git a/src/lib/parameters/tinybson/tinybson.c b/src/lib/parameters/tinybson/tinybson.cpp similarity index 96% rename from src/lib/parameters/tinybson/tinybson.c rename to src/lib/parameters/tinybson/tinybson.cpp index 8fc2eace46..e0b27595aa 100644 --- a/src/lib/parameters/tinybson/tinybson.c +++ b/src/lib/parameters/tinybson/tinybson.cpp @@ -67,7 +67,7 @@ read_x(bson_decoder_t decoder, void *p, size_t s) return (BSON_READ(decoder->fd, p, s) == s) ? 0 : -1; } - if (decoder->buf != NULL) { + if (decoder->buf != nullptr) { /* staged operations to avoid integer overflow for corrupt data */ if (s >= decoder->bufsize) { CODER_KILL(decoder, "buffer too small for read"); @@ -116,7 +116,7 @@ bson_decoder_init_file(bson_decoder_t decoder, int fd, bson_decoder_callback cal int32_t junk; decoder->fd = fd; - decoder->buf = NULL; + decoder->buf = nullptr; decoder->dead = false; decoder->callback = callback; decoder->priv = priv; @@ -140,7 +140,7 @@ bson_decoder_init_buf(bson_decoder_t decoder, void *buf, unsigned bufsize, bson_ int32_t len; /* argument sanity */ - if ((buf == NULL) || (callback == NULL)) { + if ((buf == nullptr) || (callback == nullptr)) { return -1; } @@ -216,7 +216,7 @@ bson_decoder_next(bson_decoder_t decoder) CODER_KILL(decoder, "read error on type byte"); } - decoder->node.type = tbyte; + decoder->node.type = (bson_type_t)tbyte; decoder->pending = 0; debug("got type byte 0x%02x", decoder->node.type); @@ -295,7 +295,7 @@ bson_decoder_next(bson_decoder_t decoder) CODER_KILL(decoder, "read error on BSON_BINDATA subtype"); } - decoder->node.subtype = tbyte; + decoder->node.subtype = (bson_binary_subtype_t)tbyte; break; /* XXX currently not supporting other types */ @@ -339,7 +339,7 @@ write_x(bson_encoder_t encoder, const void *p, size_t s) CODER_CHECK(encoder); /* bson file encoder (non-buffered) */ - if (encoder->fd > -1 && encoder->buf == NULL) { + if (encoder->fd > -1 && encoder->buf == nullptr) { return (BSON_WRITE(encoder->fd, p, s) == (int)s) ? 0 : -1; } @@ -371,9 +371,9 @@ write_x(bson_encoder_t encoder, const void *p, size_t s) CODER_KILL(encoder, "fixed-size buffer overflow"); } - uint8_t *newbuf = realloc(encoder->buf, encoder->bufsize + BSON_BUF_INCREMENT); + uint8_t *newbuf = (uint8_t *)realloc(encoder->buf, encoder->bufsize + BSON_BUF_INCREMENT); - if (newbuf == NULL) { + if (newbuf == nullptr) { CODER_KILL(encoder, "could not grow buffer"); } @@ -429,7 +429,7 @@ int bson_encoder_init_file(bson_encoder_t encoder, int fd) { encoder->fd = fd; - encoder->buf = NULL; + encoder->buf = nullptr; encoder->dead = false; if (write_int32(encoder, 0)) { @@ -464,7 +464,7 @@ bson_encoder_init_buf(bson_encoder_t encoder, void *buf, unsigned bufsize) encoder->bufpos = 0; encoder->dead = false; - if (encoder->buf == NULL) { + if (encoder->buf == nullptr) { encoder->bufsize = 0; encoder->realloc_ok = true; @@ -489,7 +489,7 @@ bson_encoder_fini(bson_encoder_t encoder) CODER_KILL(encoder, "write error on document terminator"); } - if (encoder->fd > -1 && encoder->buf != NULL) { + if (encoder->fd > -1 && encoder->buf != nullptr) { /* write final buffer to disk */ int ret = BSON_WRITE(encoder->fd, encoder->buf, encoder->bufpos); @@ -497,7 +497,7 @@ bson_encoder_fini(bson_encoder_t encoder) CODER_KILL(encoder, "write error"); } - } else if (encoder->buf != NULL) { + } else if (encoder->buf != nullptr) { /* update buffer length */ int32_t len = bson_encoder_buf_size(encoder); memcpy(encoder->buf, &len, sizeof(len)); @@ -529,7 +529,7 @@ bson_encoder_buf_data(bson_encoder_t encoder) /* note, no CODER_CHECK here as the caller has to clean up dead buffers */ if (encoder->fd > -1) { - return NULL; + return nullptr; } return encoder->buf; diff --git a/src/lib/parameters/tinybson/tinybson.h b/src/lib/parameters/tinybson/tinybson.h index 79b7e5a2ca..4d44374960 100644 --- a/src/lib/parameters/tinybson/tinybson.h +++ b/src/lib/parameters/tinybson/tinybson.h @@ -58,7 +58,7 @@ typedef enum { BSON_UNDEFINED = 6, BSON_BOOL = 8, BSON_DATE = 9, - BSON_NULL = 10, + BSON_nullptr = 10, BSON_INT32 = 16, BSON_INT64 = 18 } bson_type_t; @@ -198,7 +198,7 @@ __EXPORT int bson_encoder_init_file(bson_encoder_t encoder, int fd); * * @param encoder Encoder state structure to be initialised. * @param fd File to write to. - * @param buf Buffer pointer to use, can't be NULL + * @param buf Buffer pointer to use, can't be nullptr * @param bufsize Supplied buffer size * @return Zero on success. */ @@ -208,7 +208,7 @@ __EXPORT int bson_encoder_init_buf_file(bson_encoder_t encoder, int fd, void *bu * Initialze the encoder for writing to a buffer. * * @param encoder Encoder state structure to be initialised. - * @param buf Buffer pointer to use, or NULL if the buffer + * @param buf Buffer pointer to use, or nullptr if the buffer * should be allocated by the encoder. * @param bufsize Maximum buffer size, or zero for no limit. If * the buffer is supplied, the size of the supplied buffer. diff --git a/src/systemcmds/tests/CMakeLists.txt b/src/systemcmds/tests/CMakeLists.txt index 79a9418d3e..4e5495a7cc 100644 --- a/src/systemcmds/tests/CMakeLists.txt +++ b/src/systemcmds/tests/CMakeLists.txt @@ -35,7 +35,7 @@ set(srcs test_adc.c test_autodeclination.cpp test_bezierQuad.cpp - test_bson.c + test_bson.cpp test_conv.cpp test_dataman.c test_file.c diff --git a/src/systemcmds/tests/test_bson.c b/src/systemcmds/tests/test_bson.cpp similarity index 97% rename from src/systemcmds/tests/test_bson.c rename to src/systemcmds/tests/test_bson.cpp index 7dc2bd44ce..de793cf812 100644 --- a/src/systemcmds/tests/test_bson.c +++ b/src/systemcmds/tests/test_bson.cpp @@ -98,7 +98,7 @@ encode(bson_encoder_t encoder) } static int -decode_callback(bson_decoder_t decoder, void *private, bson_node_t node) +decode_callback(bson_decoder_t decoder, void *priv, bson_node_t node) { unsigned len; @@ -194,7 +194,7 @@ decode_callback(bson_decoder_t decoder, void *private, bson_node_t node) return 1; } - if (strcmp(sbuf, sample_string)) { + if (strcmp(sbuf, sample_string) != 0) { PX4_ERR("FAIL: decoder: string1 value '%s', expected '%s'", sbuf, sample_string); return 1; } @@ -268,7 +268,7 @@ test_bson(int argc, char *argv[]) int len; /* encode data to a memory buffer */ - if (bson_encoder_init_buf(&encoder, NULL, 0)) { + if (bson_encoder_init_buf(&encoder, nullptr, 0)) { PX4_ERR("FAIL: bson_encoder_init_buf"); return 1; } @@ -283,13 +283,13 @@ test_bson(int argc, char *argv[]) buf = bson_encoder_buf_data(&encoder); - if (buf == NULL) { + if (buf == nullptr) { PX4_ERR("FAIL: bson_encoder_buf_data"); return 1; } /* now test-decode it */ - if (bson_decoder_init_buf(&decoder, buf, len, decode_callback, NULL)) { + if (bson_decoder_init_buf(&decoder, buf, len, decode_callback, nullptr)) { PX4_ERR("FAIL: bson_decoder_init_buf"); return 1; }