[net-diag] add a common SendDiagnosticCommand() method (#8357)

This commit adds a common `SendDiagnosticCommand()` which is then
used for sending `DiagnosticGet` or `DiagnosticReset` commands.
This commit is contained in:
Abtin Keshavarzian
2022-11-04 08:51:04 -07:00
committed by GitHub
parent d92520c3e2
commit 13223b0104
2 changed files with 49 additions and 40 deletions
+39 -40
View File
@@ -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<Tmf::Agent>().NewNonConfirmablePostMessage(kUriDiagnosticGetQuery);
messageInfo.SetMulticastLoop(true);
}
else
{
handler = &NetworkDiagnostic::HandleDiagnosticGetResponse;
message = Get<Tmf::Agent>().NewConfirmablePostMessage(kUriDiagnosticGetRequest);
case kDiagnosticGet:
if (aDestination.IsMulticast())
{
message = Get<Tmf::Agent>().NewNonConfirmablePostMessage(kUriDiagnosticGetQuery);
messageInfo.SetMulticastLoop(true);
}
else
{
handler = &NetworkDiagnostic::HandleDiagnosticGetResponse;
message = Get<Tmf::Agent>().NewConfirmablePostMessage(kUriDiagnosticGetRequest);
}
break;
case kDiagnosticReset:
message = Get<Tmf::Agent>().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<Tmf::Agent>().SendMessage(*message, messageInfo, handler, this));
mReceiveDiagnosticGetCallback = aCallback;
mReceiveDiagnosticGetCallbackContext = aCallbackContext;
LogInfo("Sent diagnostic get");
error = Get<Tmf::Agent>().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<Tmf::Agent>().NewConfirmablePostMessage(kUriDiagnosticReset);
VerifyOrExit(message != nullptr, error = kErrorNoBufs);
if (aCount > 0)
{
SuccessOrExit(error = Tlv::Append<TypeListTlv>(*message, aTlvTypes, aCount));
}
if (aDestination.IsLinkLocal() || aDestination.IsLinkLocalMulticast())
{
messageInfo.SetSockAddr(Get<Mle::MleRouter>().GetLinkLocalAddress());
}
else
{
messageInfo.SetSockAddrToRloc();
}
messageInfo.SetPeerAddr(aDestination);
SuccessOrExit(error = Get<Tmf::Agent>().SendMessage(*message, messageInfo));
Error error;
SuccessOrExit(error = SendDiagnosticCommand(kDiagnosticReset, aDestination, aTlvTypes, aCount));
LogInfo("Sent network diagnostic reset");
exit:
FreeMessageOnError(message, error);
return error;
}
+10
View File
@@ -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);