From c59eb37c60702128ec9484c3d2e5c5feadf6e74b Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Fri, 28 Apr 2023 20:36:42 -0700 Subject: [PATCH] [mle] add parent request callback config (#8989) This commit adds `MLE_PARENT_RESPONSE_CALLBACK_API_ENABLE` config which enables support for `otThreadRegisterParentResponseCallback()` API. This API registers a callback to notify user of received Parent Response message(s) during attach. This API is mainly intended for debugging and therefore is is disabled by default. It is enabled in `toranj-config` so to validate the code (i.e. it causes no build errors) during CI GitHub action runs. --- include/openthread/instance.h | 2 +- include/openthread/thread.h | 4 ++++ src/core/api/thread_api.cpp | 2 ++ src/core/config/mle.h | 12 ++++++++++++ src/core/thread/mle.cpp | 3 ++- src/core/thread/mle.hpp | 4 ++++ src/ncp/ncp_base.cpp | 2 ++ src/ncp/ncp_base.hpp | 2 ++ src/ncp/ncp_base_ftd.cpp | 2 ++ tests/toranj/openthread-core-toranj-config.h | 9 +++++++++ 10 files changed, 40 insertions(+), 2 deletions(-) diff --git a/include/openthread/instance.h b/include/openthread/instance.h index 54343a166..9440c7bc7 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 (314) +#define OPENTHREAD_API_VERSION (315) /** * @addtogroup api-instance diff --git a/include/openthread/thread.h b/include/openthread/thread.h index ed45a8c50..7725a2851 100644 --- a/include/openthread/thread.h +++ b/include/openthread/thread.h @@ -922,6 +922,8 @@ void otThreadResetMleCounters(otInstance *aInstance); /** * This function pointer is called every time an MLE Parent Response message is received. * + * This is used in `otThreadRegisterParentResponseCallback()`. + * * @param[in] aInfo A pointer to a location on stack holding the stats data. * @param[in] aContext A pointer to callback client-specific context. * @@ -931,6 +933,8 @@ typedef void (*otThreadParentResponseCallback)(otThreadParentResponseInfo *aInfo /** * This function registers a callback to receive MLE Parent Response data. * + * This function requires `OPENTHREAD_CONFIG_MLE_PARENT_RESPONSE_CALLBACK_API_ENABLE`. + * * @param[in] aInstance A pointer to an OpenThread instance. * @param[in] aCallback A pointer to a function that is called upon receiving an MLE Parent Response message. * @param[in] aContext A pointer to callback client-specific context. diff --git a/src/core/api/thread_api.cpp b/src/core/api/thread_api.cpp index 0d1a584c3..6ded641c3 100644 --- a/src/core/api/thread_api.cpp +++ b/src/core/api/thread_api.cpp @@ -436,12 +436,14 @@ const otMleCounters *otThreadGetMleCounters(otInstance *aInstance) void otThreadResetMleCounters(otInstance *aInstance) { AsCoreType(aInstance).Get().ResetCounters(); } +#if OPENTHREAD_CONFIG_MLE_PARENT_RESPONSE_CALLBACK_API_ENABLE void otThreadRegisterParentResponseCallback(otInstance *aInstance, otThreadParentResponseCallback aCallback, void *aContext) { AsCoreType(aInstance).Get().RegisterParentResponseStatsCallback(aCallback, aContext); } +#endif #if OPENTHREAD_CONFIG_TMF_ANYCAST_LOCATOR_ENABLE otError otThreadLocateAnycastDestination(otInstance *aInstance, diff --git a/src/core/config/mle.h b/src/core/config/mle.h index 490fcdcab..57e1e507e 100644 --- a/src/core/config/mle.h +++ b/src/core/config/mle.h @@ -272,6 +272,18 @@ #define OPENTHREAD_CONFIG_MLE_INFORM_PREVIOUS_PARENT_ON_REATTACH 1 #endif +/** + * @def OPENTHREAD_CONFIG_MLE_PARENT_RESPONSE_CALLBACK_API_ENABLE + * + * Define as 1 to support `otThreadRegisterParentResponseCallback()` API which registers a callback to notify user + * of received Parent Response message(s) during attach. This API is mainly intended for debugging and therefore is + * disabled by default. + * + */ +#ifndef OPENTHREAD_CONFIG_MLE_PARENT_RESPONSE_CALLBACK_API_ENABLE +#define OPENTHREAD_CONFIG_MLE_PARENT_RESPONSE_CALLBACK_API_ENABLE 0 +#endif + /** * @def OPENTHREAD_CONFIG_MLE_LINK_METRICS_INITIATOR_ENABLE * diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 068231994..9ff77af46 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -3153,7 +3153,7 @@ void Mle::HandleParentResponse(RxInfo &aRxInfo) cslAccuracy.Init(); #endif - // Share data with application, if requested. +#if OPENTHREAD_CONFIG_MLE_PARENT_RESPONSE_CALLBACK_API_ENABLE if (mParentResponseCallback.IsSet()) { otThreadParentResponseInfo parentinfo; @@ -3169,6 +3169,7 @@ void Mle::HandleParentResponse(RxInfo &aRxInfo) mParentResponseCallback.Invoke(&parentinfo); } +#endif aRxInfo.mClass = RxInfo::kAuthoritativeMessage; diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index c7b76c327..aa011e515 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -624,6 +624,7 @@ public: */ void ResetCounters(void) { memset(&mCounters, 0, sizeof(mCounters)); } +#if OPENTHREAD_CONFIG_MLE_PARENT_RESPONSE_CALLBACK_API_ENABLE /** * This function registers the client callback that is called when processing an MLE Parent Response message. * @@ -635,6 +636,7 @@ public: { mParentResponseCallback.Set(aCallback, aContext); } +#endif /** * This method requests MLE layer to prepare and send a shorter version of Child ID Request message by only @@ -2135,7 +2137,9 @@ private: DetachGracefullyTimer mDetachGracefullyTimer; Callback mDetachGracefullyCallback; +#if OPENTHREAD_CONFIG_MLE_PARENT_RESPONSE_CALLBACK_API_ENABLE Callback mParentResponseCallback; +#endif }; } // namespace Mle diff --git a/src/ncp/ncp_base.cpp b/src/ncp/ncp_base.cpp index f805e0537..73541aed7 100644 --- a/src/ncp/ncp_base.cpp +++ b/src/ncp/ncp_base.cpp @@ -285,7 +285,9 @@ NcpBase::NcpBase(Instance *aInstance) #if OPENTHREAD_CONFIG_MLE_STEERING_DATA_SET_OOB_ENABLE memset(&mSteeringDataAddress, 0, sizeof(mSteeringDataAddress)); #endif +#if OPENTHREAD_CONFIG_MLE_PARENT_RESPONSE_CALLBACK_API_ENABLE otThreadRegisterParentResponseCallback(mInstance, &NcpBase::HandleParentResponseInfo, static_cast(this)); +#endif #endif // OPENTHREAD_FTD #if OPENTHREAD_CONFIG_SRP_CLIENT_ENABLE otSrpClientSetCallback(mInstance, HandleSrpClientCallback, this); diff --git a/src/ncp/ncp_base.hpp b/src/ncp/ncp_base.hpp index ebbe99c29..995dbc4d1 100644 --- a/src/ncp/ncp_base.hpp +++ b/src/ncp/ncp_base.hpp @@ -272,8 +272,10 @@ protected: static void HandleNeighborTableChanged(otNeighborTableEvent aEvent, const otNeighborTableEntryInfo *aEntry); void HandleNeighborTableChanged(otNeighborTableEvent aEvent, const otNeighborTableEntryInfo &aEntry); +#if OPENTHREAD_CONFIG_MLE_PARENT_RESPONSE_CALLBACK_API_ENABLE static void HandleParentResponseInfo(otThreadParentResponseInfo *aInfo, void *aContext); void HandleParentResponseInfo(const otThreadParentResponseInfo &aInfo); +#endif #endif static void HandleDatagramFromStack(otMessage *aMessage, void *aContext); diff --git a/src/ncp/ncp_base_ftd.cpp b/src/ncp/ncp_base_ftd.cpp index ffd42559c..fb763c449 100644 --- a/src/ncp/ncp_base_ftd.cpp +++ b/src/ncp/ncp_base_ftd.cpp @@ -85,6 +85,7 @@ exit: // MARK: Property/Status Changed // ---------------------------------------------------------------------------- +#if OPENTHREAD_CONFIG_MLE_PARENT_RESPONSE_CALLBACK_API_ENABLE void NcpBase::HandleParentResponseInfo(otThreadParentResponseInfo *aInfo, void *aContext) { VerifyOrExit(aInfo && aContext); @@ -116,6 +117,7 @@ void NcpBase::HandleParentResponseInfo(const otThreadParentResponseInfo &aInfo) exit: return; } +#endif void NcpBase::HandleNeighborTableChanged(otNeighborTableEvent aEvent, const otNeighborTableEntryInfo *aEntry) { diff --git a/tests/toranj/openthread-core-toranj-config.h b/tests/toranj/openthread-core-toranj-config.h index 5ae92b26d..20d351210 100644 --- a/tests/toranj/openthread-core-toranj-config.h +++ b/tests/toranj/openthread-core-toranj-config.h @@ -557,6 +557,15 @@ */ #define OPENTHREAD_CONFIG_CLI_REGISTER_IP6_RECV_CALLBACK 1 +/** + * @def OPENTHREAD_CONFIG_MLE_PARENT_RESPONSE_CALLBACK_API_ENABLE + * + * Define as 1 to support `otThreadRegisterParentResponseCallback()` API which registers a callback to notify user + * of received Parent Response message(s) during attach. + * + */ +#define OPENTHREAD_CONFIG_MLE_PARENT_RESPONSE_CALLBACK_API_ENABLE 1 + #if OPENTHREAD_RADIO /** * @def OPENTHREAD_CONFIG_MAC_SOFTWARE_ACK_TIMEOUT_ENABLE