[cli] networkdiagnostic: pass tlv types directly (#2261)

This commit is contained in:
rongli
2017-10-16 07:52:56 -07:00
committed by Jonathan Hui
parent a39e1f59da
commit 11924b09ff
4 changed files with 49 additions and 36 deletions
+1 -1
View File
@@ -512,7 +512,7 @@ void otThreadSetReceiveDiagnosticGetCallback(otInstance *aInstance, otReceiveDia
/**
* Send a Network Diagnostic Get request.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @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
+2 -4
View File
@@ -273,11 +273,9 @@ typedef enum otError
#define OT_MASTER_KEY_SIZE 16 ///< Size of the Thread Master Key (bytes)
/**
* Concatenated List of Type Identifiers of Other Diagnostics TLVs Used to Request or Reset Multiple Diagnostic Values.
* Maximum Number of Network Diagnostic TLV Types to Request or Reset.
*/
#define OT_NETWORK_DIAGNOSTIC_TYPELIST_TYPE 18
#define OT_NETWORK_DIAGNOSTIC_TYPELIST_MAX_ENTRIES 19 ///< Maximum Number of Other Network Diagnostic TLV Types
#define OT_NETWORK_DIAGNOSTIC_TYPELIST_MAX_ENTRIES 19
/**
* @struct otMasterKey
+14 -16
View File
@@ -3387,36 +3387,34 @@ exit:
void Interpreter::ProcessNetworkDiagnostic(int argc, char *argv[])
{
otError error = OT_ERROR_NONE;
struct otIp6Address address;
uint8_t payload[2 + OT_NETWORK_DIAGNOSTIC_TYPELIST_MAX_ENTRIES]; // TypeList Type(1B), len(1B), type list
uint8_t payloadIndex = 0;
uint8_t paramIndex = 0;
struct otIp6Address address;
uint8_t tlvTypes[OT_NETWORK_DIAGNOSTIC_TYPELIST_MAX_ENTRIES];
uint8_t count = 0;
uint8_t argvIndex = 0;
VerifyOrExit(argc > 1 + 1, error = OT_ERROR_PARSE);
// Include operation, address and type tlv list.
VerifyOrExit(argc > 2, error = OT_ERROR_PARSE);
SuccessOrExit(error = otIp6AddressFromString(argv[1], &address));
payloadIndex = 2;
paramIndex = 2;
argvIndex = 2;
while (paramIndex < argc && payloadIndex < sizeof(payload))
while (argvIndex < argc && count < sizeof(tlvTypes))
{
long value;
SuccessOrExit(error = ParseLong(argv[paramIndex++], value));
payload[payloadIndex++] = static_cast<uint8_t>(value);
SuccessOrExit(error = ParseLong(argv[argvIndex++], value));
tlvTypes[count++] = static_cast<uint8_t>(value);
}
payload[0] = OT_NETWORK_DIAGNOSTIC_TYPELIST_TYPE; // TypeList TLV Type
payload[1] = payloadIndex - 2; // length
if (strcmp(argv[0], "get") == 0)
{
otThreadSendDiagnosticGet(mInstance, &address, payload, payloadIndex);
otThreadSendDiagnosticGet(mInstance, &address, tlvTypes, count);
// Intentionally exit here for display response.
return;
}
else if (strcmp(argv[0], "reset") == 0)
{
otThreadSendDiagnosticReset(mInstance, &address, payload, payloadIndex);
otThreadSendDiagnosticReset(mInstance, &address, tlvTypes, count);
}
exit:
@@ -3439,7 +3437,7 @@ void Interpreter::HandleDiagnosticGetResponse(Message &aMessage, const Ip6::Mess
uint16_t bytesPrinted = 0;
uint16_t length = aMessage.GetLength() - aMessage.GetOffset();
mServer->OutputFormat("DIAG_GET.rsp: ");
mServer->OutputFormat("DIAG_GET.rsp/ans: ");
while (length > 0)
{
+32 -15
View File
@@ -88,7 +88,7 @@ otError NetworkDiagnostic::SendDiagnosticGet(const Ip6::Address &aDestination, c
{
ThreadNetif &netif = GetNetif();
otError error;
Message *message;
Message *message = NULL;
Coap::Header header;
Ip6::MessageInfo messageInfo;
otCoapResponseHandler handler = NULL;
@@ -114,7 +114,15 @@ otError NetworkDiagnostic::SendDiagnosticGet(const Ip6::Address &aDestination, c
VerifyOrExit((message = netif.GetCoap().NewMessage(header)) != NULL, error = OT_ERROR_NO_BUFS);
SuccessOrExit(error = message->Append(aTlvTypes, aCount));
if (aCount > 0)
{
TypeListTlv tlv;
tlv.Init();
tlv.SetLength(aCount);
SuccessOrExit(error = message->Append(&tlv, sizeof(tlv)));
SuccessOrExit(error = message->Append(aTlvTypes, aCount));
}
messageInfo.SetPeerAddr(aDestination);
messageInfo.SetPeerPort(kCoapUdpPort);
@@ -278,7 +286,7 @@ otError NetworkDiagnostic::FillRequestedTlvs(Message &aRequest, Message &aRespon
{
VerifyOrExit(aRequest.Read(offset, sizeof(type), &type) == sizeof(type), error = OT_ERROR_DROP);
otLogInfoNetDiag(GetInstance(), "Received diagnostic get type %d", type);
otLogInfoNetDiag(GetInstance(), "Type %d", type);
switch (type)
{
@@ -377,21 +385,16 @@ otError NetworkDiagnostic::FillRequestedTlvs(Message &aRequest, Message &aRespon
case NetworkDiagnosticTlv::kBatteryLevel:
{
// TODO Need more api from driver
BatteryLevelTlv tlv;
tlv.Init();
tlv.SetBatteryLevel(100);
SuccessOrExit(error = aResponse.Append(&tlv, tlv.GetSize()));
// Thread 1.1.1 Specification Section 10.11.4.2:
// Omitted if the battery level is not measured, is unknown or the device does not
// operate on battery power.
break;
}
case NetworkDiagnosticTlv::kSupplyVoltage:
{
// TODO Need more api from driver
SupplyVoltageTlv tlv;
tlv.Init();
tlv.SetSupplyVoltage(0);
SuccessOrExit(error = aResponse.Append(&tlv, tlv.GetSize()));
// Thread 1.1.1 Specification Section 10.11.4.3:
// Omitted if the battery level is not measured, is unknown.
break;
}
@@ -492,6 +495,12 @@ void NetworkDiagnostic::HandleDiagnosticGetQuery(Coap::Header &aHeader, Message
SuccessOrExit(error = FillRequestedTlvs(aMessage, *message, networkDiagnosticTlv));
if (message->GetLength() == header.GetLength())
{
// Remove Payload Marker if payload is actually empty.
message->SetLength(header.GetLength() - 1);
}
SuccessOrExit(error = netif.GetCoap().SendMessage(*message, messageInfo, NULL, this));
otLogInfoNetDiag(GetInstance(), "Sent diagnostic get answer");
@@ -564,7 +573,7 @@ otError NetworkDiagnostic::SendDiagnosticReset(const Ip6::Address &aDestination,
{
ThreadNetif &netif = GetNetif();
otError error;
Message *message;
Message *message = NULL;
Coap::Header header;
Ip6::MessageInfo messageInfo;
@@ -579,7 +588,15 @@ otError NetworkDiagnostic::SendDiagnosticReset(const Ip6::Address &aDestination,
VerifyOrExit((message = netif.GetCoap().NewMessage(header)) != NULL, error = OT_ERROR_NO_BUFS);
SuccessOrExit(error = message->Append(aTlvTypes, aCount));
if (aCount > 0)
{
TypeListTlv tlv;
tlv.Init();
tlv.SetLength(aCount);
SuccessOrExit(error = message->Append(&tlv, sizeof(tlv)));
SuccessOrExit(error = message->Append(aTlvTypes, aCount));
}
messageInfo.SetPeerAddr(aDestination);
messageInfo.SetPeerPort(kCoapUdpPort);