[cli] move commissioner to separate source files (#3862)

This commit is contained in:
Jonathan Hui
2019-05-17 13:20:07 -07:00
committed by Jonathan Hui
parent 9cd11a2761
commit 68beda5b07
10 changed files with 530 additions and 288 deletions
+6 -277
View File
@@ -43,7 +43,6 @@
#include "utils/parse_cmdline.hpp"
#include "utils/wrap_string.h"
#include <openthread/commissioner.h>
#include <openthread/icmp6.h>
#include <openthread/link.h>
#include <openthread/ncp.h>
@@ -304,6 +303,9 @@ Interpreter::Interpreter(Instance *aInstance)
#if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE
, mCoapSecure(*this)
#endif
#if OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
, mCommissioner(*this)
#endif
#if OPENTHREAD_ENABLE_JOINER
, mJoiner(*this)
#endif
@@ -3178,285 +3180,12 @@ void Interpreter::ProcessVersion(int argc, char *argv[])
void Interpreter::ProcessCommissioner(int argc, char *argv[])
{
otError error = OT_ERROR_NONE;
VerifyOrExit(argc > 0, error = OT_ERROR_INVALID_ARGS);
if (strcmp(argv[0], "start") == 0)
{
SuccessOrExit(error = otCommissionerStart(mInstance));
}
else if (strcmp(argv[0], "stop") == 0)
{
SuccessOrExit(error = otCommissionerStop(mInstance));
}
else if (strcmp(argv[0], "joiner") == 0)
{
otExtAddress addr;
const otExtAddress *addrPtr;
VerifyOrExit(argc > 2, error = OT_ERROR_INVALID_ARGS);
if (strcmp(argv[2], "*") == 0)
{
addrPtr = NULL;
}
else
{
VerifyOrExit(Hex2Bin(argv[2], addr.m8, sizeof(addr)) == sizeof(addr), error = OT_ERROR_PARSE);
addrPtr = &addr;
}
if (strcmp(argv[1], "add") == 0)
{
VerifyOrExit(argc > 3, error = OT_ERROR_INVALID_ARGS);
// Timeout parameter is optional - if not specified, use default value.
unsigned long timeout = kDefaultJoinerTimeout;
if (argc > 4)
{
SuccessOrExit(error = ParseUnsignedLong(argv[4], timeout));
}
SuccessOrExit(error = otCommissionerAddJoiner(mInstance, addrPtr, argv[3], static_cast<uint32_t>(timeout)));
}
else if (strcmp(argv[1], "remove") == 0)
{
SuccessOrExit(error = otCommissionerRemoveJoiner(mInstance, addrPtr));
}
}
else if (strcmp(argv[0], "provisioningurl") == 0)
{
SuccessOrExit(error = otCommissionerSetProvisioningUrl(mInstance, (argc > 1) ? argv[1] : NULL));
}
else if (strcmp(argv[0], "announce") == 0)
{
long mask;
long count;
long period;
otIp6Address address;
VerifyOrExit(argc > 4, error = OT_ERROR_INVALID_ARGS);
// mask
SuccessOrExit(error = ParseLong(argv[1], mask));
// count
SuccessOrExit(error = ParseLong(argv[2], count));
// period
SuccessOrExit(error = ParseLong(argv[3], period));
// destination
SuccessOrExit(error = otIp6AddressFromString(argv[4], &address));
SuccessOrExit(error = otCommissionerAnnounceBegin(mInstance, static_cast<uint32_t>(mask),
static_cast<uint8_t>(count), static_cast<uint16_t>(period),
&address));
}
else if (strcmp(argv[0], "energy") == 0)
{
long mask;
long count;
long period;
long scanDuration;
otIp6Address address;
VerifyOrExit(argc > 5, error = OT_ERROR_INVALID_ARGS);
// mask
SuccessOrExit(error = ParseLong(argv[1], mask));
// count
SuccessOrExit(error = ParseLong(argv[2], count));
// period
SuccessOrExit(error = ParseLong(argv[3], period));
// scan duration
SuccessOrExit(error = ParseLong(argv[4], scanDuration));
// destination
SuccessOrExit(error = otIp6AddressFromString(argv[5], &address));
SuccessOrExit(error =
otCommissionerEnergyScan(mInstance, static_cast<uint32_t>(mask), static_cast<uint8_t>(count),
static_cast<uint16_t>(period), static_cast<uint16_t>(scanDuration),
&address, Interpreter::s_HandleEnergyReport, this));
}
else if (strcmp(argv[0], "panid") == 0)
{
long panid;
long mask;
otIp6Address address;
VerifyOrExit(argc > 3, error = OT_ERROR_INVALID_ARGS);
// panid
SuccessOrExit(error = ParseLong(argv[1], panid));
// mask
SuccessOrExit(error = ParseLong(argv[2], mask));
// destination
SuccessOrExit(error = otIp6AddressFromString(argv[3], &address));
SuccessOrExit(error =
otCommissionerPanIdQuery(mInstance, static_cast<uint16_t>(panid), static_cast<uint32_t>(mask),
&address, Interpreter::s_HandlePanIdConflict, this));
}
else if (strcmp(argv[0], "mgmtget") == 0)
{
uint8_t tlvs[32];
long value;
int length = 0;
for (uint8_t index = 1; index < argc; index++)
{
VerifyOrExit(static_cast<size_t>(length) < sizeof(tlvs), error = OT_ERROR_NO_BUFS);
if (strcmp(argv[index], "locator") == 0)
{
tlvs[length++] = OT_MESHCOP_TLV_BORDER_AGENT_RLOC;
}
else if (strcmp(argv[index], "sessionid") == 0)
{
tlvs[length++] = OT_MESHCOP_TLV_COMM_SESSION_ID;
}
else if (strcmp(argv[index], "steeringdata") == 0)
{
tlvs[length++] = OT_MESHCOP_TLV_STEERING_DATA;
}
else if (strcmp(argv[index], "joinerudpport") == 0)
{
tlvs[length++] = OT_MESHCOP_TLV_JOINER_UDP_PORT;
}
else if (strcmp(argv[index], "binary") == 0)
{
VerifyOrExit(++index < argc, error = OT_ERROR_INVALID_ARGS);
value = static_cast<long>(strlen(argv[index]) + 1) / 2;
VerifyOrExit(static_cast<size_t>(value) <= (sizeof(tlvs) - static_cast<size_t>(length)),
error = OT_ERROR_NO_BUFS);
VerifyOrExit(Interpreter::Hex2Bin(argv[index], tlvs + length, static_cast<uint16_t>(value)) >= 0,
error = OT_ERROR_PARSE);
length += value;
}
else
{
ExitNow(error = OT_ERROR_INVALID_ARGS);
}
}
SuccessOrExit(error = otCommissionerSendMgmtGet(mInstance, tlvs, static_cast<uint8_t>(length)));
}
else if (strcmp(argv[0], "mgmtset") == 0)
{
otCommissioningDataset dataset;
uint8_t tlvs[32];
long value;
int length = 0;
VerifyOrExit(argc > 0, error = OT_ERROR_INVALID_ARGS);
memset(&dataset, 0, sizeof(dataset));
for (uint8_t index = 1; index < argc; index++)
{
VerifyOrExit(static_cast<size_t>(length) < sizeof(tlvs), error = OT_ERROR_NO_BUFS);
if (strcmp(argv[index], "locator") == 0)
{
VerifyOrExit(++index < argc, error = OT_ERROR_INVALID_ARGS);
dataset.mIsLocatorSet = true;
SuccessOrExit(error = Interpreter::ParseLong(argv[index], value));
dataset.mLocator = static_cast<uint16_t>(value);
}
else if (strcmp(argv[index], "sessionid") == 0)
{
VerifyOrExit(++index < argc, error = OT_ERROR_INVALID_ARGS);
dataset.mIsSessionIdSet = true;
SuccessOrExit(error = Interpreter::ParseLong(argv[index], value));
dataset.mSessionId = static_cast<uint16_t>(value);
}
else if (strcmp(argv[index], "steeringdata") == 0)
{
VerifyOrExit(++index < argc, error = OT_ERROR_INVALID_ARGS);
dataset.mIsSteeringDataSet = true;
length = static_cast<int>((strlen(argv[index]) + 1) / 2);
VerifyOrExit(static_cast<size_t>(length) <= OT_STEERING_DATA_MAX_LENGTH, error = OT_ERROR_NO_BUFS);
VerifyOrExit(
Interpreter::Hex2Bin(argv[index], dataset.mSteeringData.m8, static_cast<uint16_t>(length)) >= 0,
error = OT_ERROR_PARSE);
dataset.mSteeringData.mLength = static_cast<uint8_t>(length);
length = 0;
}
else if (strcmp(argv[index], "joinerudpport") == 0)
{
VerifyOrExit(++index < argc, error = OT_ERROR_INVALID_ARGS);
dataset.mIsJoinerUdpPortSet = true;
SuccessOrExit(error = Interpreter::ParseLong(argv[index], value));
dataset.mJoinerUdpPort = static_cast<uint16_t>(value);
}
else if (strcmp(argv[index], "binary") == 0)
{
VerifyOrExit(++index < argc, error = OT_ERROR_INVALID_ARGS);
length = static_cast<int>((strlen(argv[index]) + 1) / 2);
VerifyOrExit(static_cast<size_t>(length) <= sizeof(tlvs), error = OT_ERROR_NO_BUFS);
VerifyOrExit(Interpreter::Hex2Bin(argv[index], tlvs, static_cast<uint16_t>(length)) >= 0,
error = OT_ERROR_PARSE);
}
else
{
ExitNow(error = OT_ERROR_INVALID_ARGS);
}
}
SuccessOrExit(error = otCommissionerSendMgmtSet(mInstance, &dataset, tlvs, static_cast<uint8_t>(length)));
}
else if (strcmp(argv[0], "sessionid") == 0)
{
mServer->OutputFormat("%d\r\n", otCommissionerGetSessionId(mInstance));
}
else
{
ExitNow(error = OT_ERROR_INVALID_ARGS);
}
exit:
otError error;
error = mCommissioner.Process(argc, argv);
AppendResult(error);
}
void OTCALL Interpreter::s_HandleEnergyReport(uint32_t aChannelMask,
const uint8_t *aEnergyList,
uint8_t aEnergyListLength,
void * aContext)
{
static_cast<Interpreter *>(aContext)->HandleEnergyReport(aChannelMask, aEnergyList, aEnergyListLength);
}
void Interpreter::HandleEnergyReport(uint32_t aChannelMask, const uint8_t *aEnergyList, uint8_t aEnergyListLength)
{
mServer->OutputFormat("Energy: %08x ", aChannelMask);
for (uint8_t i = 0; i < aEnergyListLength; i++)
{
mServer->OutputFormat("%d ", static_cast<int8_t>(aEnergyList[i]));
}
mServer->OutputFormat("\r\n");
}
void OTCALL Interpreter::s_HandlePanIdConflict(uint16_t aPanId, uint32_t aChannelMask, void *aContext)
{
static_cast<Interpreter *>(aContext)->HandlePanIdConflict(aPanId, aChannelMask);
}
void Interpreter::HandlePanIdConflict(uint16_t aPanId, uint32_t aChannelMask)
{
mServer->OutputFormat("Conflict: %04x, %08x\r\n", aPanId, aChannelMask);
}
#endif // OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
#endif
#if OPENTHREAD_ENABLE_JOINER