From ccd9eae1856368ff12ef6d1298822902fc94023e Mon Sep 17 00:00:00 2001 From: Jacob Dahl <37091262+dakejahl@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:04:42 -0900 Subject: [PATCH] refactor(platform): load DEFINE_PARAMETERS members from a table (#29554) * fix(cmake): link the functional gtest libraries as a group px4_platform, parameters and uorb_msgs all reference uORB, but uORB is listed before them, so a test only linked when something earlier happened to pull in px4_log.o. Inlined param_get() type checks did that for any test using DEFINE_PARAMETERS; without them PerformanceModel and StickYaw failed with undefined orb_advertise and orb_publish. Assisted-by: Claude:claude-opus-5-5 * refactor(platform): load DEFINE_PARAMETERS members from a table Every parameter cost a param_get_mark_used() call in its class constructor and a param_get() call in updateParamsImpl(), about 12 bytes each, for roughly 2000 parameters. Each Param now takes one pointer-sized slot, so the members a DEFINE_PARAMETERS list declares form an array. A ParamTable member declared right after them holds the handles and loads them all with one call, in the constructor and on update. External parameters keep a pointer instead of a reference so the loader can follow it. Assisted-by: Claude:claude-opus-5-5 * refactor(platform): use param_t for parameter table entries Each entry is a parameter handle with the storage kind in its top bits, so it takes the handle's type. Assisted-by: Claude:claude-opus-5-5 * refactor(platform): keep each Param at its natural size Padding every Param to a pointer-sized slot doubled float and int32 members on 64-bit targets and grew ParamBool from 1 to 4 bytes. The macro now passes the first parameter's address to the table, and the loader steps through the members at each kind's own size and alignment. A table placed before the parameters would instead load through the external parameters before their pointers are set. Assisted-by: Claude:claude-opus-5-5 * refactor(platform): drop the parameter table end marker APPLY_ALL leaves a comma after every entry, so the template argument list needed a closing element, which every class then stored in its entry array. The entries are now a static array of the class, whose braced initializer accepts the trailing comma. Assisted-by: Claude:claude-opus-5-5 * refactor(platform): check the parameter layout at compile time The language leaves padding between members to the compiler, so the table loader relies on the ABI placing each member at the next address its alignment allows. Each class's updateParamsImpl() now checks every parameter's offsetof against where the loader steps, so a compiler that pads differently fails the build instead of loading into the wrong members. Assisted-by: Claude:claude-opus-5-5 * docs(platform): describe how DEFINE_PARAMETERS loads parameters The table loader walks the members of a class from the first parameter's address, which is not obvious from the macros or the loader alone. A block above the macros now states the flash cost it removes, what the macro adds to a class, how the loader steps, and which static_asserts guard the layout it relies on. The namespace comment said a Param does not depend on the macro. That stopped being true when the constructors stopped loading. Assisted-by: Claude:claude-fable-5-2 * docs(platform): drop the flash figure from the parameter table comment A measured saving for one board goes stale as parameters are added. The comment now states what the table replaces the per-parameter calls with. Assisted-by: Claude:claude-fable-5-2 * fix(platform): build the parameter layout check on GCC before 9 GCC 7 and 8 ignore _Pragma diagnostics inside a macro expansion, so the offsetof warning in the DEFINE_PARAMETERS layout check stays an error on the VOXL 2 toolchain. Those compilers now build with -Wno-invalid-offsetof. The static_assert still runs there. Assisted-by: Claude:claude-opus-5-5 --- cmake/px4_add_common_flags.cmake | 6 + cmake/px4_add_gtest.cmake | 35 ++- .../include/px4_platform_common/param.h | 240 ++++++++++++++---- platforms/common/module_params.cpp | 39 +++ src/lib/parameters/ParameterTest.cpp | 138 ++++++++++ 5 files changed, 400 insertions(+), 58 deletions(-) diff --git a/cmake/px4_add_common_flags.cmake b/cmake/px4_add_common_flags.cmake index e79a100157c..3c177acdc88 100644 --- a/cmake/px4_add_common_flags.cmake +++ b/cmake/px4_add_common_flags.cmake @@ -119,6 +119,12 @@ function(px4_add_common_flags) add_compile_options(-Wno-stringop-truncation) endif() + if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9) + # GCC before 9 ignores _Pragma inside a macro expansion, so the DEFINE_PARAMETERS + # layout check cannot silence its own offsetof warning there. + add_compile_options($<$:-Wno-invalid-offsetof>) + endif() + add_compile_options( -fno-builtin-printf -fno-strength-reduce diff --git a/cmake/px4_add_gtest.cmake b/cmake/px4_add_gtest.cmake index 9b89fba11fc..242437a8343 100644 --- a/cmake/px4_add_gtest.cmake +++ b/cmake/px4_add_gtest.cmake @@ -131,20 +131,27 @@ function(px4_add_functional_gtest) add_executable(${TESTNAME} EXCLUDE_FROM_ALL ${SRC} ${EXTRA_SRCS}) # link the libary to test and gtest - set(_FUNCTIONAL_GTEST_LIBS ${LINKLIBS} gtest_functional_main - px4_layer - px4_platform - uORB - systemlib - cdev - px4_work_queue - px4_daemon - work_queue - parameters - events - perf - tinybson - uorb_msgs) + set(_FUNCTIONAL_GTEST_PX4_LIBS px4_layer + px4_platform + uORB + systemlib + cdev + px4_work_queue + px4_daemon + work_queue + parameters + events + perf + tinybson + uorb_msgs) + if(APPLE) + set(_FUNCTIONAL_GTEST_LIBS ${LINKLIBS} gtest_functional_main ${_FUNCTIONAL_GTEST_PX4_LIBS}) + else() + # These libraries reference each other (px4_platform and parameters publish through uORB), + # so which objects a test pulls in depends on the order unless the linker rescans them. + set(_FUNCTIONAL_GTEST_LIBS ${LINKLIBS} gtest_functional_main + -Wl,--start-group ${_FUNCTIONAL_GTEST_PX4_LIBS} -Wl,--end-group) + endif() if(TARGET fuzztest::fuzztest) list(APPEND _FUNCTIONAL_GTEST_LIBS fuzztest::fuzztest) # Do not use link_fuzztest() here because that # also links to fuzztest_gtest_main diff --git a/platforms/common/include/px4_platform_common/param.h b/platforms/common/include/px4_platform_common/param.h index 681f60afb3d..719ccc088de 100644 --- a/platforms/common/include/px4_platform_common/param.h +++ b/platforms/common/include/px4_platform_common/param.h @@ -42,6 +42,8 @@ #include "param_macros.h" #include #include +#include +#include #include @@ -53,21 +55,78 @@ inline static param_t param_handle(px4::params p) return (param_t)p; } - - +/** + * How DEFINE_PARAMETERS loads the parameters of a class + * + * Problem: if every parameter loaded itself, each one would cost a param_get_mark_used() + * call in the class constructor and a param_get() call in updateParamsImpl(). Loading + * them from a table replaces those with one call per class in each place and one table + * entry per parameter. + * + * Approach: the macro declares the parameters as consecutive members, so one shared + * function can load them all. The macro adds three things to the class: + * + * 1. The Param members, in the order listed. Their constructors do not load a value. + * 2. _param_table_entries, a static array with one entry per member. An entry is the + * parameter handle plus the kind of member: a float or int32_t, a bool, or a pointer + * to an external value. + * 3. _param_table, a member declared after the parameters. Its constructor loads them + * with one call to param_table_load(), and updateParamsImpl() makes the same call + * when parameters change. Members are constructed in declaration order, so by then + * every external parameter holds its pointer. + * + * param_table_load() gets the address of the first parameter and no other offsets. + * For each entry it rounds the address up to the alignment of that kind, loads the + * value there and advances by the size of that kind. That is how the compiler places + * consecutive members, so the walk lands on each parameter in turn. + * + * The C++ standard does not guarantee that placement, so the build checks it. + * param_table_entry() asserts that each Param type has the size and alignment the + * loader assumes for its kind. _CHECK_PARAMETER_LAYOUT asserts that the offsetof of + * every parameter is where the walk arrives. A compiler that pads differently fails + * the build instead of loading into the wrong member. + * + * A Param declared outside these macros is never loaded. + */ #define _DEFINE_SINGLE_PARAMETER(x) \ do_not_explicitly_use_this_namespace::PAIR(x); -#define _CALL_UPDATE(x) \ - STRIP(x).update(); +#define _PARAM_TABLE_ENTRY(x) \ + do_not_explicitly_use_this_namespace::param_table_entry(), + +// The member name of the first parameter in a list +#define _FIRST_PARAMETER(...) _FIRST_PARAMETER_H(__VA_ARGS__, unused) +#define _FIRST_PARAMETER_H(x, ...) STRIP(x) + +// The entries go in a static array of the class because APPLY_ALL leaves a trailing comma, +// which only a braced list accepts. +#define _DEFINE_PARAMETER_TABLE(...) \ + static constexpr param_t _param_table_entries[] = {APPLY_ALL(_PARAM_TABLE_ENTRY, __VA_ARGS__)}; \ + do_not_explicitly_use_this_namespace::ParamTable _param_table{_param_table_entries, &_FIRST_PARAMETER(__VA_ARGS__)}; + +#define _PARAM_TABLE_OFFSET(x) \ + offsetof(_param_table_class, STRIP(x)), + +// Fails the build if any parameter is not where the loader steps. It expands inside a +// member function, where the class is complete and decltype(*this) names it. offsetof on +// a class with virtual functions is only conditionally supported. GCC and Clang support +// it but warn. GCC before 9 ignores these pragmas, so the build turns the warning off there. +#define _CHECK_PARAMETER_LAYOUT(...) \ + using _param_table_class = typename do_not_explicitly_use_this_namespace::param_table_class::type; \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Winvalid-offsetof\"") \ + static_assert(do_not_explicitly_use_this_namespace::param_table_layout_matches(_param_table_entries, \ + {APPLY_ALL(_PARAM_TABLE_OFFSET, __VA_ARGS__)}), "Parameters are not laid out where the table loader steps"); \ + _Pragma("GCC diagnostic pop") // define the parameter update method, which will update all parameters. // It is marked as 'final', so that wrong usages lead to a compile error (see below) #define _DEFINE_PARAMETER_UPDATE_METHOD(...) \ protected: \ void updateParamsImpl() final { \ - APPLY_ALL(_CALL_UPDATE, __VA_ARGS__) \ + _CHECK_PARAMETER_LAYOUT(__VA_ARGS__) \ + _param_table.update(_param_table_entries, &_FIRST_PARAMETER(__VA_ARGS__)); \ } \ private: @@ -79,6 +138,7 @@ inline static param_t param_handle(px4::params p) // that tree. #define DEFINE_PARAMETERS(...) \ APPLY_ALL(_DEFINE_SINGLE_PARAMETER, __VA_ARGS__) \ + _DEFINE_PARAMETER_TABLE(__VA_ARGS__) \ _DEFINE_PARAMETER_UPDATE_METHOD(__VA_ARGS__) @@ -86,22 +146,109 @@ inline static param_t param_handle(px4::params p) protected: \ void updateParamsImpl() override { \ parent_class::updateParamsImpl(); \ - APPLY_ALL(_CALL_UPDATE, __VA_ARGS__) \ + _CHECK_PARAMETER_LAYOUT(__VA_ARGS__) \ + _param_table.update(_param_table_entries, &_FIRST_PARAMETER(__VA_ARGS__)); \ } \ private: #define DEFINE_PARAMETERS_CUSTOM_PARENT(parent_class, ...) \ APPLY_ALL(_DEFINE_SINGLE_PARAMETER, __VA_ARGS__) \ + _DEFINE_PARAMETER_TABLE(__VA_ARGS__) \ _DEFINE_PARAMETER_UPDATE_METHOD_CUSTOM_PARENT(parent_class, __VA_ARGS__) // This namespace never needs to be used directly. Use the DEFINE_PARAMETERS_CUSTOM_PARENT and -// DEFINE_PARAMETERS macros instead (the Param classes don't depend on using the macro, the macro just -// makes sure that update() is automatically called). +// DEFINE_PARAMETERS macros instead. Only the table those macros add loads a Param. namespace do_not_explicitly_use_this_namespace { +// Table entry: parameter handle in the low bits, how its member stores the value in the top two +static_assert(sizeof(param_t) >= 2); +static constexpr unsigned param_table_kind_shift = 14; +static constexpr param_t param_table_handle_mask = (1u << param_table_kind_shift) - 1; +static constexpr param_t param_table_value = 0u << param_table_kind_shift; // float or int32_t +static constexpr param_t param_table_bool = 1u << param_table_kind_shift; // bool +static constexpr param_t param_table_external = 2u << param_table_kind_shift; // pointer to the value + +constexpr size_t param_table_member_size(param_t kind) +{ + return kind == param_table_external ? sizeof(void *) : kind == param_table_bool ? sizeof(bool) : sizeof(float); +} + +constexpr size_t param_table_member_align(param_t kind) +{ + return kind == param_table_external ? alignof(void *) : kind == param_table_bool ? alignof(bool) : alignof(float); +} + +// Where a member of `kind` starts after the previous member ends at `end` +constexpr uintptr_t param_table_member_start(uintptr_t end, param_t kind) +{ + return (end + param_table_member_align(kind) - 1) & ~(param_table_member_align(kind) - 1); +} + +/** + * Load `count` parameters into consecutive members starting at `first`, placing each + * member at the next address its alignment allows, as the compiler lays them out. + * Implemented in module_params.cpp. + */ +__EXPORT void param_table_load(const param_t *entries, unsigned count, void *first, bool mark_used); + +template +constexpr param_t param_table_entry() +{ + constexpr param_t kind = P::table_entry & ~param_table_handle_mask; + static_assert(sizeof(P) == param_table_member_size(kind) && alignof(P) == param_table_member_align(kind), + "Param layout must match the table loader"); + return P::table_entry; +} + +/** + * Whether members at `offsets` within their class sit where param_table_load() steps. + * The class is aligned to at least each member's alignment, so stepping through offsets + * pads the same as stepping through addresses. + */ +template +constexpr bool param_table_layout_matches(const param_t (&entries)[N], const size_t (&offsets)[N]) +{ + uintptr_t member = offsets[0]; + + for (size_t i = 0; i < N; i++) { + const param_t kind = entries[i] & ~param_table_handle_mask; + member = param_table_member_start(member, kind); + + if (offsets[i] != member) { + return false; + } + + member += param_table_member_size(kind); + } + + return true; +} + +template +struct param_table_class; + +template +struct param_table_class { + using type = T; +}; + +/** + * Declared after the parameters of a class by DEFINE_PARAMETERS, so it loads them + * once they are all constructed. + */ +class ParamTable +{ +public: + template + ParamTable(const param_t (&entries)[N], void *first) { param_table_load(entries, N, first, true); } + + template + void update(const param_t (&entries)[N], void *first) { param_table_load(entries, N, first, false); } +}; + template class Param { @@ -113,13 +260,14 @@ template class Param { public: + static_assert((int)p <= param_table_handle_mask, "parameter handle does not fit a table entry"); + static constexpr param_t table_entry = (param_t)p | param_table_value; + // static type-check static_assert(px4::parameters_type[(int)p] == PARAM_TYPE_FLOAT, "parameter type must be float"); - Param() - { - param_get_mark_used(handle(), &_val); - } + // loaded by the ParamTable that follows the parameters + Param() = default; float get() const { return _val; } @@ -163,29 +311,32 @@ template class Param { public: + static_assert((int)p <= param_table_handle_mask, "parameter handle does not fit a table entry"); + static constexpr param_t table_entry = (param_t)p | param_table_external; + // static type-check static_assert(px4::parameters_type[(int)p] == PARAM_TYPE_FLOAT, "parameter type must be float"); + // loaded by the ParamTable that follows the parameters Param(float &external_val) - : _val(external_val) + : _val(&external_val) { - param_get_mark_used(handle(), &_val); } - float get() const { return _val; } + float get() const { return *_val; } - const float &reference() const { return _val; } + const float &reference() const { return *_val; } /// Store the parameter value to the parameter storage (@see param_set()) - bool commit() const { return param_set(handle(), &_val) == 0; } + bool commit() const { return param_set(handle(), _val) == 0; } /// Store the parameter value to the parameter storage, w/o notifying the system (@see param_set_no_notification()) - bool commit_no_notification() const { return param_set_no_notification(handle(), &_val) == 0; } + bool commit_no_notification() const { return param_set_no_notification(handle(), _val) == 0; } /// Set and commit a new value. Returns true if the value changed. bool commit_no_notification(float val) { - if (fabsf(val - _val) > FLT_EPSILON) { + if (fabsf(val - *_val) > FLT_EPSILON) { set(val); commit_no_notification(); return true; @@ -194,7 +345,7 @@ public: return false; } - void set(float val) { _val = val; } + void set(float val) { *_val = val; } void reset() { @@ -202,24 +353,25 @@ public: update(); } - bool update() { return param_get(handle(), &_val) == 0; } + bool update() { return param_get(handle(), _val) == 0; } param_t handle() const { return param_handle(p); } private: - float &_val; + float *_val; // a pointer rather than a reference, so the table loader can read it }; template class Param { public: + static_assert((int)p <= param_table_handle_mask, "parameter handle does not fit a table entry"); + static constexpr param_t table_entry = (param_t)p | param_table_value; + // static type-check static_assert(px4::parameters_type[(int)p] == PARAM_TYPE_INT32, "parameter type must be int32_t"); - Param() - { - param_get_mark_used(handle(), &_val); - } + // loaded by the ParamTable that follows the parameters + Param() = default; int32_t get() const { return _val; } @@ -263,29 +415,32 @@ template class Param { public: + static_assert((int)p <= param_table_handle_mask, "parameter handle does not fit a table entry"); + static constexpr param_t table_entry = (param_t)p | param_table_external; + // static type-check static_assert(px4::parameters_type[(int)p] == PARAM_TYPE_INT32, "parameter type must be int32_t"); + // loaded by the ParamTable that follows the parameters Param(int32_t &external_val) - : _val(external_val) + : _val(&external_val) { - param_get_mark_used(handle(), &_val); } - int32_t get() const { return _val; } + int32_t get() const { return *_val; } - const int32_t &reference() const { return _val; } + const int32_t &reference() const { return *_val; } /// Store the parameter value to the parameter storage (@see param_set()) - bool commit() const { return param_set(handle(), &_val) == 0; } + bool commit() const { return param_set(handle(), _val) == 0; } /// Store the parameter value to the parameter storage, w/o notifying the system (@see param_set_no_notification()) - bool commit_no_notification() const { return param_set_no_notification(handle(), &_val) == 0; } + bool commit_no_notification() const { return param_set_no_notification(handle(), _val) == 0; } /// Set and commit a new value. Returns true if the value changed. bool commit_no_notification(int32_t val) { - if (val != _val) { + if (val != *_val) { set(val); commit_no_notification(); return true; @@ -294,7 +449,7 @@ public: return false; } - void set(int32_t val) { _val = val; } + void set(int32_t val) { *_val = val; } void reset() { @@ -302,28 +457,25 @@ public: update(); } - bool update() { return param_get(handle(), &_val) == 0; } + bool update() { return param_get(handle(), _val) == 0; } param_t handle() const { return param_handle(p); } private: - int32_t &_val; + int32_t *_val; // a pointer rather than a reference, so the table loader can read it }; template class Param { public: + static_assert((int)p <= param_table_handle_mask, "parameter handle does not fit a table entry"); + static constexpr param_t table_entry = (param_t)p | param_table_bool; + // static type-check static_assert(px4::parameters_type[(int)p] == PARAM_TYPE_INT32, "parameter type must be int32_t"); - Param() - { - int32_t value_int; - - if (param_get_mark_used(handle(), &value_int) == 0) { - _val = value_int != 0; - } - } + // loaded by the ParamTable that follows the parameters + Param() = default; bool get() const { return _val; } diff --git a/platforms/common/module_params.cpp b/platforms/common/module_params.cpp index 2b0439f4a5d..d1d1a941dd0 100644 --- a/platforms/common/module_params.cpp +++ b/platforms/common/module_params.cpp @@ -32,6 +32,45 @@ ****************************************************************************/ #include +#include + +#include + +namespace do_not_explicitly_use_this_namespace +{ + +void param_table_load(const param_t *entries, unsigned count, void *first, bool mark_used) +{ + uintptr_t member = reinterpret_cast(first); + + for (unsigned i = 0; i < count; i++) { + const param_t handle = entries[i] & param_table_handle_mask; + const param_t kind = entries[i] & ~param_table_handle_mask; + + member = param_table_member_start(member, kind); + uint8_t *slot = reinterpret_cast(member); + member += param_table_member_size(kind); + + void *dest = slot; + int32_t bool_value; + + if (kind == param_table_external) { + memcpy(&dest, slot, sizeof(dest)); + + } else if (kind == param_table_bool) { + dest = &bool_value; + } + + // (param_get) is the C function, not the type-checked C++ wrapper macro + const int ret = mark_used ? param_get_mark_used(handle, dest) : (param_get)(handle, dest); + + if (kind == param_table_bool && ret == 0) { + *reinterpret_cast(slot) = bool_value != 0; + } + } +} + +} // namespace do_not_explicitly_use_this_namespace ModuleParams::ModuleParams(ModuleParams *parent) { diff --git a/src/lib/parameters/ParameterTest.cpp b/src/lib/parameters/ParameterTest.cpp index c3e70d7a8c7..c83f21d8bca 100644 --- a/src/lib/parameters/ParameterTest.cpp +++ b/src/lib/parameters/ParameterTest.cpp @@ -110,6 +110,144 @@ TEST_F(ParameterTest, testUorbSendReceive) EXPECT_EQ(0, memcmp(&message, &obstacle_distance, sizeof(message))); } +// Every kind of DEFINE_PARAMETERS member. The two bools share a word, and on 64 bit +// the external pointers need padding before them. +class ParamTableModule : public ModuleParams +{ +public: + ParamTableModule() : ModuleParams(nullptr), _param_cp_delay(_ext_delay), _param_sys_autocfg(_ext_autoconfig) {} + + void update() { updateParams(); } + + float ext_delay() const { return _ext_delay; } + int32_t ext_autoconfig() const { return _ext_autoconfig; } + + // declared before the parameters, so they exist before the table loads into them + float _ext_delay{-1.f}; + int32_t _ext_autoconfig{-1}; + + DEFINE_PARAMETERS( + (ParamFloat) _param_cp_dist, + (ParamBool) _param_cp_go_no_data, + (ParamBool) _param_sys_has_gps, + (ParamExtFloat) _param_cp_delay, + (ParamInt) _param_sys_autostart, + (ParamExtInt) _param_sys_autocfg, + (ParamFloat) _param_cp_guide_ang + ) +}; + +TEST_F(ParameterTest, testDefineParametersTable) +{ + const float dist = 3.5f, delay = 0.25f, guide_ang = 12.f; + const int32_t go_no_data = 1, has_gps = 0, autostart = 4001, autoconfig = 2; + + // GIVEN: non-default values for each kind of parameter + ASSERT_EQ(0, param_set(param_handle(px4::params::CP_DIST), &dist)); + ASSERT_EQ(0, param_set(param_handle(px4::params::CP_GO_NO_DATA), &go_no_data)); + ASSERT_EQ(0, param_set(param_handle(px4::params::SYS_HAS_GPS), &has_gps)); + ASSERT_EQ(0, param_set(param_handle(px4::params::CP_DELAY), &delay)); + ASSERT_EQ(0, param_set(param_handle(px4::params::SYS_AUTOSTART), &autostart)); + ASSERT_EQ(0, param_set(param_handle(px4::params::SYS_AUTOCONFIG), &autoconfig)); + ASSERT_EQ(0, param_set(param_handle(px4::params::CP_GUIDE_ANG), &guide_ang)); + + // WHEN: the module is constructed + ParamTableModule module; + + // THEN: every member holds the stored value and is marked used + EXPECT_FLOAT_EQ(dist, module._param_cp_dist.get()); + EXPECT_TRUE(module._param_cp_go_no_data.get()); + EXPECT_FALSE(module._param_sys_has_gps.get()); + EXPECT_FLOAT_EQ(delay, module._param_cp_delay.get()); + EXPECT_FLOAT_EQ(delay, module.ext_delay()); + EXPECT_EQ(autostart, module._param_sys_autostart.get()); + EXPECT_EQ(autoconfig, module._param_sys_autocfg.get()); + EXPECT_EQ(autoconfig, module.ext_autoconfig()); + EXPECT_FLOAT_EQ(guide_ang, module._param_cp_guide_ang.get()); + EXPECT_TRUE(param_used(param_handle(px4::params::CP_DIST))); + EXPECT_TRUE(param_used(param_handle(px4::params::CP_GUIDE_ANG))); + + // WHEN: the stored values change and the module updates + const float dist2 = 7.f, delay2 = 0.5f, guide_ang2 = 20.f; + const int32_t go_no_data2 = 0, has_gps2 = 1, autostart2 = 4002, autoconfig2 = 0; + ASSERT_EQ(0, param_set(param_handle(px4::params::CP_DIST), &dist2)); + ASSERT_EQ(0, param_set(param_handle(px4::params::CP_GO_NO_DATA), &go_no_data2)); + ASSERT_EQ(0, param_set(param_handle(px4::params::SYS_HAS_GPS), &has_gps2)); + ASSERT_EQ(0, param_set(param_handle(px4::params::CP_DELAY), &delay2)); + ASSERT_EQ(0, param_set(param_handle(px4::params::SYS_AUTOSTART), &autostart2)); + ASSERT_EQ(0, param_set(param_handle(px4::params::SYS_AUTOCONFIG), &autoconfig2)); + ASSERT_EQ(0, param_set(param_handle(px4::params::CP_GUIDE_ANG), &guide_ang2)); + module.update(); + + // THEN: every member holds the new value + EXPECT_FLOAT_EQ(dist2, module._param_cp_dist.get()); + EXPECT_FALSE(module._param_cp_go_no_data.get()); + EXPECT_TRUE(module._param_sys_has_gps.get()); + EXPECT_FLOAT_EQ(delay2, module.ext_delay()); + EXPECT_EQ(autostart2, module._param_sys_autostart.get()); + EXPECT_EQ(autoconfig2, module.ext_autoconfig()); + EXPECT_FLOAT_EQ(guide_ang2, module._param_cp_guide_ang.get()); +} + +// A parent and a child that each declare parameters, so each class has its own table +class ParamTableParent : public ModuleParams +{ +public: + ParamTableParent() : ModuleParams(nullptr) {} + + void update() { updateParams(); } + + DEFINE_PARAMETERS_CUSTOM_PARENT(ModuleParams, + (ParamFloat) _param_cp_dist, + (ParamBool) _param_cp_go_no_data + ) +}; + +class ParamTableChild : public ParamTableParent +{ +public: + DEFINE_PARAMETERS_CUSTOM_PARENT(ParamTableParent, + (ParamFloat) _param_cp_delay, + (ParamInt) _param_sys_autostart + ) +}; + +TEST_F(ParameterTest, testDefineParametersTableCustomParent) +{ + const float dist = 3.5f, delay = 0.25f; + const int32_t go_no_data = 1, autostart = 4001; + + // GIVEN: non-default values for the parent's and the child's parameters + ASSERT_EQ(0, param_set(param_handle(px4::params::CP_DIST), &dist)); + ASSERT_EQ(0, param_set(param_handle(px4::params::CP_GO_NO_DATA), &go_no_data)); + ASSERT_EQ(0, param_set(param_handle(px4::params::CP_DELAY), &delay)); + ASSERT_EQ(0, param_set(param_handle(px4::params::SYS_AUTOSTART), &autostart)); + + // WHEN: the child is constructed + ParamTableChild child; + + // THEN: both tables are loaded + EXPECT_FLOAT_EQ(dist, child._param_cp_dist.get()); + EXPECT_TRUE(child._param_cp_go_no_data.get()); + EXPECT_FLOAT_EQ(delay, child._param_cp_delay.get()); + EXPECT_EQ(autostart, child._param_sys_autostart.get()); + + // WHEN: the stored values change and the child updates + const float dist2 = 7.f, delay2 = 0.5f; + const int32_t go_no_data2 = 0, autostart2 = 4002; + ASSERT_EQ(0, param_set(param_handle(px4::params::CP_DIST), &dist2)); + ASSERT_EQ(0, param_set(param_handle(px4::params::CP_GO_NO_DATA), &go_no_data2)); + ASSERT_EQ(0, param_set(param_handle(px4::params::CP_DELAY), &delay2)); + ASSERT_EQ(0, param_set(param_handle(px4::params::SYS_AUTOSTART), &autostart2)); + child.update(); + + // THEN: the parent's and the child's parameters hold the new values + EXPECT_FLOAT_EQ(dist2, child._param_cp_dist.get()); + EXPECT_FALSE(child._param_cp_go_no_data.get()); + EXPECT_FLOAT_EQ(delay2, child._param_cp_delay.get()); + EXPECT_EQ(autostart2, child._param_sys_autostart.get()); +} + // A BSON null in an import resets the param (a delta tombstone on flash boards). TEST_F(ParameterTest, testImportTombstoneResets) {