From 13223b01041f5cad69fd2b7b03821d837f0f8376 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Fri, 4 Nov 2022 08:51:04 -0700 Subject: [PATCH] [net-diag] add a common `SendDiagnosticCommand()` method (#8357) This commit adds a common `SendDiagnosticCommand()` which is then used for sending `DiagnosticGet` or `DiagnosticReset` commands. --- src/core/thread/network_diagnostic.cpp | 79 +++++++++++++------------- src/core/thread/network_diagnostic.hpp | 10 ++++ 2 files changed, 49 insertions(+), 40 deletions(-) diff --git a/src/core/thread/network_diagnostic.cpp b/src/core/thread/network_diagnostic.cpp index 835bf38fb..1d3ae9162 100644 --- a/src/core/thread/network_diagnostic.cpp +++ b/src/core/thread/network_diagnostic.cpp @@ -69,21 +69,48 @@ Error NetworkDiagnostic::SendDiagnosticGet(const Ip6::Address & aDesti uint8_t aCount, otReceiveDiagnosticGetCallback aCallback, void * aCallbackContext) +{ + Error error; + + SuccessOrExit(error = SendDiagnosticCommand(kDiagnosticGet, aDestination, aTlvTypes, aCount)); + + mReceiveDiagnosticGetCallback = aCallback; + mReceiveDiagnosticGetCallbackContext = aCallbackContext; + + LogInfo("Sent diagnostic get"); + +exit: + return error; +} + +Error NetworkDiagnostic::SendDiagnosticCommand(CommandType aCommandType, + const Ip6::Address &aDestination, + const uint8_t aTlvTypes[], + uint8_t aCount) { Error error; Coap::Message * message = nullptr; Tmf::MessageInfo messageInfo(GetInstance()); - otCoapResponseHandler handler = nullptr; + Coap::ResponseHandler handler = nullptr; - if (aDestination.IsMulticast()) + switch (aCommandType) { - message = Get().NewNonConfirmablePostMessage(kUriDiagnosticGetQuery); - messageInfo.SetMulticastLoop(true); - } - else - { - handler = &NetworkDiagnostic::HandleDiagnosticGetResponse; - message = Get().NewConfirmablePostMessage(kUriDiagnosticGetRequest); + case kDiagnosticGet: + if (aDestination.IsMulticast()) + { + message = Get().NewNonConfirmablePostMessage(kUriDiagnosticGetQuery); + messageInfo.SetMulticastLoop(true); + } + else + { + handler = &NetworkDiagnostic::HandleDiagnosticGetResponse; + message = Get().NewConfirmablePostMessage(kUriDiagnosticGetRequest); + } + break; + + case kDiagnosticReset: + message = Get().NewConfirmablePostMessage(kUriDiagnosticReset); + break; } VerifyOrExit(message != nullptr, error = kErrorNoBufs); @@ -104,12 +131,7 @@ Error NetworkDiagnostic::SendDiagnosticGet(const Ip6::Address & aDesti messageInfo.SetPeerAddr(aDestination); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo, handler, this)); - - mReceiveDiagnosticGetCallback = aCallback; - mReceiveDiagnosticGetCallbackContext = aCallbackContext; - - LogInfo("Sent diagnostic get"); + error = Get().SendMessage(*message, messageInfo, handler, this); exit: FreeMessageOnError(message, error); @@ -514,35 +536,12 @@ Error NetworkDiagnostic::SendDiagnosticReset(const Ip6::Address &aDestination, const uint8_t aTlvTypes[], uint8_t aCount) { - Error error; - Coap::Message * message = nullptr; - Tmf::MessageInfo messageInfo(GetInstance()); - - message = Get().NewConfirmablePostMessage(kUriDiagnosticReset); - VerifyOrExit(message != nullptr, error = kErrorNoBufs); - - if (aCount > 0) - { - SuccessOrExit(error = Tlv::Append(*message, aTlvTypes, aCount)); - } - - if (aDestination.IsLinkLocal() || aDestination.IsLinkLocalMulticast()) - { - messageInfo.SetSockAddr(Get().GetLinkLocalAddress()); - } - else - { - messageInfo.SetSockAddrToRloc(); - } - - messageInfo.SetPeerAddr(aDestination); - - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + Error error; + SuccessOrExit(error = SendDiagnosticCommand(kDiagnosticReset, aDestination, aTlvTypes, aCount)); LogInfo("Sent network diagnostic reset"); exit: - FreeMessageOnError(message, error); return error; } diff --git a/src/core/thread/network_diagnostic.hpp b/src/core/thread/network_diagnostic.hpp index 0f0c0def6..ac9084b8c 100644 --- a/src/core/thread/network_diagnostic.hpp +++ b/src/core/thread/network_diagnostic.hpp @@ -126,6 +126,16 @@ public: static Error GetNextDiagTlv(const Coap::Message &aMessage, Iterator &aIterator, otNetworkDiagTlv &aNetworkDiagTlv); private: + enum CommandType : uint8_t + { + kDiagnosticGet, + kDiagnosticReset, + }; + + Error SendDiagnosticCommand(CommandType aCommandType, + const Ip6::Address &aDestination, + const uint8_t aTlvTypes[], + uint8_t aCount); Error AppendIp6AddressList(Message &aMessage); Error AppendChildTable(Message &aMessage); void FillMacCountersTlv(MacCountersTlv &aMacCountersTlv);