From d24327fd71a96bda0619f6b6331184e313b73909 Mon Sep 17 00:00:00 2001 From: kangping Date: Wed, 24 Feb 2021 14:32:57 +0800 Subject: [PATCH] [netdiag] fix setting DIAG_GET response callback (#6192) This commit sets the DIAG_GET response callback every time the otThreadSendDiagnosticGet is called. This is because the callback may be changed to other modules (e,g. OTBR_REST, OTBR_UBUS). --- include/openthread/instance.h | 2 +- include/openthread/netdiag.h | 28 +++++++++----------------- src/cli/cli.cpp | 6 ++---- src/core/api/netdiag_api.cpp | 21 +++++++------------ src/core/thread/network_diagnostic.cpp | 18 ++++++++--------- src/core/thread/network_diagnostic.hpp | 25 ++++++++++------------- 6 files changed, 39 insertions(+), 61 deletions(-) diff --git a/include/openthread/instance.h b/include/openthread/instance.h index adb550206..186edd09e 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 (78) +#define OPENTHREAD_API_VERSION (79) /** * @addtogroup api-instance diff --git a/include/openthread/netdiag.h b/include/openthread/netdiag.h index 376780f13..2d291a080 100644 --- a/include/openthread/netdiag.h +++ b/include/openthread/netdiag.h @@ -297,34 +297,26 @@ typedef void (*otReceiveDiagnosticGetCallback)(otError aError, void * aContext); /** - * This function registers a callback to provide received raw Network Diagnostic Get response payload. + * Send a Network Diagnostic Get request. * * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aDestination A pointer to destination address. + * @param[in] aTlvTypes An array of Network Diagnostic TLV types. + * @param[in] aCount Number of types in aTlvTypes. * @param[in] aCallback A pointer to a function that is called when Network Diagnostic Get response * is received or NULL to disable the callback. * @param[in] aCallbackContext A pointer to application-specific context. * - */ -void otThreadSetReceiveDiagnosticGetCallback(otInstance * aInstance, - otReceiveDiagnosticGetCallback aCallback, - void * aCallbackContext); - -/** - * Send a Network Diagnostic Get request. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aDestination A pointer to destination address. - * @param[in] aTlvTypes An array of Network Diagnostic TLV types. - * @param[in] aCount Number of types in aTlvTypes. - * * @retval OT_ERROR_NONE Successfully queued the DIAG_GET.req. * @retval OT_ERROR_NO_BUFS Insufficient message buffers available to send DIAG_GET.req. * */ -otError otThreadSendDiagnosticGet(otInstance * aInstance, - const otIp6Address *aDestination, - const uint8_t aTlvTypes[], - uint8_t aCount); +otError otThreadSendDiagnosticGet(otInstance * aInstance, + const otIp6Address * aDestination, + const uint8_t aTlvTypes[], + uint8_t aCount, + otReceiveDiagnosticGetCallback aCallback, + void * aCallbackContext); /** * Send a Network Diagnostic Reset request. diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index f7dcbb9ab..16eecdcc7 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -147,9 +147,6 @@ Interpreter::Interpreter(Instance *aInstance) , mSrpServer(*this) #endif { -#if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE - otThreadSetReceiveDiagnosticGetCallback(mInstance, &Interpreter::HandleDiagnosticGetResponse, this); -#endif #if OPENTHREAD_FTD otThreadSetDiscoveryRequestCallback(mInstance, &Interpreter::HandleDiscoveryRequest, this); #endif @@ -4850,7 +4847,8 @@ otError Interpreter::ProcessNetworkDiagnostic(uint8_t aArgsLength, char *aArgs[] if (strcmp(aArgs[0], "get") == 0) { - IgnoreError(otThreadSendDiagnosticGet(mInstance, &address, tlvTypes, count)); + SuccessOrExit(error = otThreadSendDiagnosticGet(mInstance, &address, tlvTypes, count, + &Interpreter::HandleDiagnosticGetResponse, this)); ExitNow(error = OT_ERROR_PENDING); } else if (strcmp(aArgs[0], "reset") == 0) diff --git a/src/core/api/netdiag_api.cpp b/src/core/api/netdiag_api.cpp index 5b75e0eb5..3e8b83643 100644 --- a/src/core/api/netdiag_api.cpp +++ b/src/core/api/netdiag_api.cpp @@ -49,24 +49,17 @@ otError otThreadGetNextDiagnosticTlv(const otMessage * aMessage, *aIterator, *aNetworkDiagTlv); } -void otThreadSetReceiveDiagnosticGetCallback(otInstance * aInstance, - otReceiveDiagnosticGetCallback aCallback, - void * aCallbackContext) -{ - Instance &instance = *static_cast(aInstance); - - instance.Get().SetReceiveDiagnosticGetCallback(aCallback, aCallbackContext); -} - -otError otThreadSendDiagnosticGet(otInstance * aInstance, - const otIp6Address *aDestination, - const uint8_t aTlvTypes[], - uint8_t aCount) +otError otThreadSendDiagnosticGet(otInstance * aInstance, + const otIp6Address * aDestination, + const uint8_t aTlvTypes[], + uint8_t aCount, + otReceiveDiagnosticGetCallback aCallback, + void * aCallbackContext) { Instance &instance = *static_cast(aInstance); return instance.Get().SendDiagnosticGet( - *static_cast(aDestination), aTlvTypes, aCount); + *static_cast(aDestination), aTlvTypes, aCount, aCallback, aCallbackContext); } otError otThreadSendDiagnosticReset(otInstance * aInstance, diff --git a/src/core/thread/network_diagnostic.cpp b/src/core/thread/network_diagnostic.cpp index a26525bf1..0365549e2 100644 --- a/src/core/thread/network_diagnostic.cpp +++ b/src/core/thread/network_diagnostic.cpp @@ -69,16 +69,11 @@ NetworkDiagnostic::NetworkDiagnostic(Instance &aInstance) Get().AddResource(mDiagnosticReset); } -void NetworkDiagnostic::SetReceiveDiagnosticGetCallback(otReceiveDiagnosticGetCallback aCallback, - void * aCallbackContext) -{ - mReceiveDiagnosticGetCallback = aCallback; - mReceiveDiagnosticGetCallbackContext = aCallbackContext; -} - -otError NetworkDiagnostic::SendDiagnosticGet(const Ip6::Address &aDestination, - const uint8_t aTlvTypes[], - uint8_t aCount) +otError NetworkDiagnostic::SendDiagnosticGet(const Ip6::Address & aDestination, + const uint8_t aTlvTypes[], + uint8_t aCount, + otReceiveDiagnosticGetCallback aCallback, + void * aCallbackContext) { otError error; Coap::Message * message = nullptr; @@ -122,6 +117,9 @@ otError NetworkDiagnostic::SendDiagnosticGet(const Ip6::Address &aDestination, SuccessOrExit(error = Get().SendMessage(*message, messageInfo, handler, this)); + mReceiveDiagnosticGetCallback = aCallback; + mReceiveDiagnosticGetCallbackContext = aCallbackContext; + otLogInfoNetDiag("Sent diagnostic get"); exit: diff --git a/src/core/thread/network_diagnostic.hpp b/src/core/thread/network_diagnostic.hpp index 510a9ea3f..3d7a9cf99 100644 --- a/src/core/thread/network_diagnostic.hpp +++ b/src/core/thread/network_diagnostic.hpp @@ -81,26 +81,23 @@ public: */ explicit NetworkDiagnostic(Instance &aInstance); - /** - * This method registers a callback to provide received raw DIAG_GET.rsp or an DIAG_GET.ans payload. - * - * @param[in] aCallback A pointer to a function that is called when an DIAG_GET.rsp or an DIAG_GET.ans - * is received or nullptr to disable the callback. - * @param[in] aCallbackContext A pointer to application-specific context. - * - */ - void SetReceiveDiagnosticGetCallback(otReceiveDiagnosticGetCallback aCallback, void *aCallbackContext); - /** * This method sends Diagnostic Get request. If the @p aDestination is of multicast type, the DIAG_GET.qry * message is sent or the DIAG_GET.req otherwise. * - * @param[in] aDestination A reference to the destination address. - * @param[in] aTlvTypes An array of Network Diagnostic TLV types. - * @param[in] aCount Number of types in aTlvTypes + * @param[in] aDestination A reference to the destination address. + * @param[in] aTlvTypes An array of Network Diagnostic TLV types. + * @param[in] aCount Number of types in aTlvTypes. + * @param[in] aCallback A pointer to a function that is called when Network Diagnostic Get response + * is received or NULL to disable the callback. + * @param[in] aCallbackContext A pointer to application-specific context. * */ - otError SendDiagnosticGet(const Ip6::Address &aDestination, const uint8_t aTlvTypes[], uint8_t aCount); + otError SendDiagnosticGet(const Ip6::Address & aDestination, + const uint8_t aTlvTypes[], + uint8_t aCount, + otReceiveDiagnosticGetCallback aCallback, + void * aCallbackContext); /** * This method sends Diagnostic Reset request.