From ab2b0908e25b27b641caea0f33828b57f79caf50 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 30 Aug 2023 10:57:26 -0700 Subject: [PATCH] [mle] add `OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE` This commit adds a build configuration flag for the feature to set and get device properties, which are then used to calculate the local leader weight on a device. The feature is enabled by default on Thread 1.3.1 or later. The new configuration flag allows OpenThread project integrators to enable this feature on earlier versions if desired. --- etc/cmake/options.cmake | 1 + examples/config/ot-core-config-check-size-br.h | 1 + examples/config/ot-core-config-check-size-ftd.h | 1 + examples/config/ot-core-config-check-size-mtd.h | 1 + include/openthread/instance.h | 2 +- include/openthread/thread_ftd.h | 4 ++-- src/cli/cli.cpp | 6 +++--- src/core/api/thread_ftd_api.cpp | 2 +- src/core/config/mle.h | 14 ++++++++++++++ src/core/thread/mle_router.cpp | 4 ++-- src/core/thread/mle_router.hpp | 4 ++-- src/core/thread/mle_types.cpp | 4 ++-- src/core/thread/mle_types.hpp | 6 +++--- tests/toranj/openthread-core-toranj-config.h | 2 ++ tests/unit/test_mle.cpp | 6 +++--- 15 files changed, 39 insertions(+), 19 deletions(-) diff --git a/etc/cmake/options.cmake b/etc/cmake/options.cmake index 3740dacb42a..d2c6bfb322b 100644 --- a/etc/cmake/options.cmake +++ b/etc/cmake/options.cmake @@ -189,6 +189,7 @@ ot_option(OT_CSL_AUTO_SYNC OPENTHREAD_CONFIG_MAC_CSL_AUTO_SYNC_ENABLE "data poll ot_option(OT_CSL_DEBUG OPENTHREAD_CONFIG_MAC_CSL_DEBUG_ENABLE "csl debug") ot_option(OT_CSL_RECEIVER OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE "csl receiver") ot_option(OT_DATASET_UPDATER OPENTHREAD_CONFIG_DATASET_UPDATER_ENABLE "dataset updater") +ot_option(OT_DEVICE_PROP_LEADER_WEIGHT OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE "device prop for leader weight") ot_option(OT_DHCP6_CLIENT OPENTHREAD_CONFIG_DHCP6_CLIENT_ENABLE "DHCP6 client") ot_option(OT_DHCP6_SERVER OPENTHREAD_CONFIG_DHCP6_SERVER_ENABLE "DHCP6 server") ot_option(OT_DIAGNOSTIC OPENTHREAD_CONFIG_DIAG_ENABLE "diagnostic") diff --git a/examples/config/ot-core-config-check-size-br.h b/examples/config/ot-core-config-check-size-br.h index e6bb8a6ea79..a86f26f23ae 100644 --- a/examples/config/ot-core-config-check-size-br.h +++ b/examples/config/ot-core-config-check-size-br.h @@ -73,6 +73,7 @@ #define OPENTHREAD_CONFIG_MAC_FILTER_ENABLE 1 #define OPENTHREAD_CONFIG_MESH_DIAG_ENABLE 1 #define OPENTHREAD_CONFIG_MESSAGE_USE_HEAP_ENABLE 1 +#define OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE 1 #define OPENTHREAD_CONFIG_MLE_LINK_METRICS_INITIATOR_ENABLE 1 #define OPENTHREAD_CONFIG_MLE_LINK_METRICS_SUBJECT_ENABLE 1 #define OPENTHREAD_CONFIG_MLR_ENABLE 1 diff --git a/examples/config/ot-core-config-check-size-ftd.h b/examples/config/ot-core-config-check-size-ftd.h index 210df7972be..12a83754d83 100644 --- a/examples/config/ot-core-config-check-size-ftd.h +++ b/examples/config/ot-core-config-check-size-ftd.h @@ -73,6 +73,7 @@ #define OPENTHREAD_CONFIG_MAC_FILTER_ENABLE 1 #define OPENTHREAD_CONFIG_MESH_DIAG_ENABLE 1 #define OPENTHREAD_CONFIG_MESSAGE_USE_HEAP_ENABLE 1 +#define OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE 1 #define OPENTHREAD_CONFIG_MLE_LINK_METRICS_INITIATOR_ENABLE 1 #define OPENTHREAD_CONFIG_MLE_LINK_METRICS_SUBJECT_ENABLE 1 #define OPENTHREAD_CONFIG_MLR_ENABLE 1 diff --git a/examples/config/ot-core-config-check-size-mtd.h b/examples/config/ot-core-config-check-size-mtd.h index 7c2b8cd9dcc..baf958ab186 100644 --- a/examples/config/ot-core-config-check-size-mtd.h +++ b/examples/config/ot-core-config-check-size-mtd.h @@ -73,6 +73,7 @@ #define OPENTHREAD_CONFIG_MAC_FILTER_ENABLE 1 #define OPENTHREAD_CONFIG_MESH_DIAG_ENABLE 0 #define OPENTHREAD_CONFIG_MESSAGE_USE_HEAP_ENABLE 1 +#define OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE 0 #define OPENTHREAD_CONFIG_MLE_LINK_METRICS_INITIATOR_ENABLE 0 #define OPENTHREAD_CONFIG_MLE_LINK_METRICS_SUBJECT_ENABLE 1 #define OPENTHREAD_CONFIG_MLR_ENABLE 1 diff --git a/include/openthread/instance.h b/include/openthread/instance.h index f4154748282..5b6bb1e4425 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -53,7 +53,7 @@ extern "C" { * @note This number versions both OpenThread platform and user APIs. * */ -#define OPENTHREAD_API_VERSION (354) +#define OPENTHREAD_API_VERSION (355) /** * @addtogroup api-instance diff --git a/include/openthread/thread_ftd.h b/include/openthread/thread_ftd.h index ea0aa974a58..3b1687a4d29 100644 --- a/include/openthread/thread_ftd.h +++ b/include/openthread/thread_ftd.h @@ -237,7 +237,7 @@ typedef struct otDeviceProperties /** * Get the current device properties. * - * Requires `OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1`. + * Requires `OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE`. * * @returns The device properties `otDeviceProperties`. * @@ -247,7 +247,7 @@ const otDeviceProperties *otThreadGetDeviceProperties(otInstance *aInstance); /** * Set the device properties which are then used to determine and set the Leader Weight. * - * Requires `OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1`. + * Requires `OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE`. * * @param[in] aInstance A pointer to an OpenThread instance. * @param[in] aDeviceProperties The device properties. diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 21e74126582..301cbb5d6d3 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -3523,7 +3523,7 @@ template <> otError Interpreter::Process(Arg aArgs[]) return ProcessGetSet(aArgs, otThreadGetLocalLeaderWeight, otThreadSetLocalLeaderWeight); } -#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1) +#if OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE template <> otError Interpreter::Process(Arg aArgs[]) { static const char *const kPowerSupplyStrings[4] = { @@ -3622,7 +3622,7 @@ template <> otError Interpreter::Process(Arg aArgs[]) exit: return error; } -#endif // #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1) +#endif // OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE #endif // OPENTHREAD_FTD @@ -8203,7 +8203,7 @@ otError Interpreter::ProcessCommand(Arg aArgs[]) #endif CmdEntry("detach"), #endif // OPENTHREAD_FTD || OPENTHREAD_MTD -#if OPENTHREAD_FTD && (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1) +#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE CmdEntry("deviceprops"), #endif #if OPENTHREAD_CONFIG_DIAG_ENABLE diff --git a/src/core/api/thread_ftd_api.cpp b/src/core/api/thread_ftd_api.cpp index 814e80a94b1..8d448fc0cb0 100644 --- a/src/core/api/thread_ftd_api.cpp +++ b/src/core/api/thread_ftd_api.cpp @@ -79,7 +79,7 @@ otError otThreadSetPreferredRouterId(otInstance *aInstance, uint8_t aRouterId) return AsCoreType(aInstance).Get().SetPreferredRouterId(aRouterId); } -#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1) +#if OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE const otDeviceProperties *otThreadGetDeviceProperties(otInstance *aInstance) { return &AsCoreType(aInstance).Get().GetDeviceProperties(); diff --git a/src/core/config/mle.h b/src/core/config/mle.h index 85bc9e59bd9..7a7915a140e 100644 --- a/src/core/config/mle.h +++ b/src/core/config/mle.h @@ -88,6 +88,20 @@ #define OPENTHREAD_CONFIG_MLE_IP_ADDRS_TO_REGISTER (OPENTHREAD_CONFIG_MLE_IP_ADDRS_PER_CHILD) #endif +/** + * @def OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE + * + * Define as 1 to enable feature to set device properties which are used for calculating the local leader weight on a + * device. + * + * It is enabled by default on Thread Version 1.3.1 or later. + * + */ +#ifndef OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE +#define OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE \ + (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1) +#endif + /** * @def OPENTHREAD_CONFIG_MLE_DEFAULT_LEADER_WEIGHT_ADJUSTMENT * diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index 6cbc1ffca2d..a293dd94bc5 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -95,7 +95,7 @@ MleRouter::MleRouter(Instance &aInstance) { mDeviceMode.Set(mDeviceMode.Get() | DeviceMode::kModeFullThreadDevice | DeviceMode::kModeFullNetworkData); -#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1) +#if OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE mLeaderWeight = mDeviceProperties.CalculateLeaderWeight(); #else mLeaderWeight = kDefaultLeaderWeight; @@ -212,7 +212,7 @@ void MleRouter::HandleSecurityPolicyChanged(void) return; } -#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1) +#if OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE void MleRouter::SetDeviceProperties(const DeviceProperties &aDeviceProperties) { mDeviceProperties = aDeviceProperties; diff --git a/src/core/thread/mle_router.hpp b/src/core/thread/mle_router.hpp index 4264648c89b..9afbe132a47 100644 --- a/src/core/thread/mle_router.hpp +++ b/src/core/thread/mle_router.hpp @@ -144,7 +144,7 @@ class MleRouter : public Mle */ Error BecomeLeader(void); -#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1) +#if OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE /** * Gets the device properties which are used to determine the Leader Weight. * @@ -705,7 +705,7 @@ class MleRouter : public Mle TrickleTimer mAdvertiseTrickleTimer; -#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1) +#if OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE DeviceProperties mDeviceProperties; #endif diff --git a/src/core/thread/mle_types.cpp b/src/core/thread/mle_types.cpp index 2cb0a41294e..0c87ce6e968 100644 --- a/src/core/thread/mle_types.cpp +++ b/src/core/thread/mle_types.cpp @@ -72,7 +72,7 @@ DeviceMode::InfoString DeviceMode::ToString(void) const //--------------------------------------------------------------------------------------------------------------------- // DeviceProperties -#if OPENTHREAD_FTD && (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1) +#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE DeviceProperties::DeviceProperties(void) { @@ -135,7 +135,7 @@ uint8_t DeviceProperties::CalculateLeaderWeight(void) const return weight; } -#endif // #if OPENTHREAD_FTD && (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1) +#endif // #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE //--------------------------------------------------------------------------------------------------------------------- // RouterIdSet diff --git a/src/core/thread/mle_types.hpp b/src/core/thread/mle_types.hpp index 91c5dc69f9f..ae4dfdfa31d 100644 --- a/src/core/thread/mle_types.hpp +++ b/src/core/thread/mle_types.hpp @@ -287,7 +287,7 @@ class DeviceMode : public Equatable uint8_t mMode; }; -#if OPENTHREAD_FTD && (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1) +#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE /** * Represents device properties. * @@ -346,7 +346,7 @@ class DeviceProperties : public otDeviceProperties, public Clearable= OT_THREAD_VERSION_1_3_1) +#endif // #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE /** * Represents the Thread Leader Data. @@ -706,7 +706,7 @@ const char *RoleToString(DeviceRole aRole); DefineCoreType(otLeaderData, Mle::LeaderData); DefineMapEnum(otDeviceRole, Mle::DeviceRole); -#if OPENTHREAD_FTD && (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1) +#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE DefineCoreType(otDeviceProperties, Mle::DeviceProperties); DefineMapEnum(otPowerSupply, Mle::DeviceProperties::PowerSupply); #endif diff --git a/tests/toranj/openthread-core-toranj-config.h b/tests/toranj/openthread-core-toranj-config.h index f008ca24454..52e766be582 100644 --- a/tests/toranj/openthread-core-toranj-config.h +++ b/tests/toranj/openthread-core-toranj-config.h @@ -95,6 +95,8 @@ #define OPENTHREAD_CONFIG_TMF_SNOOP_CACHE_ENTRY_TIMEOUT 3 +#define OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE 1 + #define OPENTHREAD_CONFIG_MLE_MAX_CHILDREN 32 #define OPENTHREAD_CONFIG_MLE_CHILD_TIMEOUT_DEFAULT 120 diff --git a/tests/unit/test_mle.cpp b/tests/unit/test_mle.cpp index d5b2d3b8a36..5bb7203693c 100644 --- a/tests/unit/test_mle.cpp +++ b/tests/unit/test_mle.cpp @@ -36,7 +36,7 @@ namespace ot { -#if OPENTHREAD_FTD && (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1) +#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE void TestDefaultDeviceProperties(void) { @@ -170,13 +170,13 @@ void TestLeaderWeightCalculation(void) printf("TestLeaderWeightCalculation passed\n"); } -#endif // #if OPENTHREAD_FTD && (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1) +#endif // #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE } // namespace ot int main(void) { -#if OPENTHREAD_FTD && (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_3_1) +#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE ot::TestDefaultDeviceProperties(); ot::TestLeaderWeightCalculation(); #endif