#include "cli_joiner.hpp"
#include "cli/cli.hpp"
#include "cli/cli_server.hpp"
#if OPENTHREAD_CONFIG_JOINER_ENABLE
namespace ot {
namespace Cli {
const struct Joiner::Command Joiner::sCommands[] = {
{"help", &Joiner::ProcessHelp},
{"id", &Joiner::ProcessId},
{"start", &Joiner::ProcessStart},
{"stop", &Joiner::ProcessStop},
};
otError Joiner::ProcessHelp(uint8_t aArgsLength, char *aArgs[])
{
OT_UNUSED_VARIABLE(aArgsLength);
OT_UNUSED_VARIABLE(aArgs);
for (size_t i = 0; i < OT_ARRAY_LENGTH(sCommands); i++)
{
mInterpreter.mServer->OutputFormat("%s\r\n", sCommands[i].mName);
}
return OT_ERROR_NONE;
}
otError Joiner::ProcessId(uint8_t aArgsLength, char *aArgs[])
{
OT_UNUSED_VARIABLE(aArgsLength);
OT_UNUSED_VARIABLE(aArgs);
otExtAddress joinerId;
otJoinerGetId(mInterpreter.mInstance, &joinerId);
mInterpreter.OutputBytes(joinerId.m8, sizeof(joinerId));
mInterpreter.mServer->OutputFormat("\r\n");
return OT_ERROR_NONE;
}
otError Joiner::ProcessStart(uint8_t aArgsLength, char *aArgs[])
{
otError error;
const char *provisioningUrl = NULL;
VerifyOrExit(aArgsLength > 1, error = OT_ERROR_INVALID_ARGS);
if (aArgsLength > 2)
{
provisioningUrl = aArgs[2];
}
error = otJoinerStart(mInterpreter.mInstance, aArgs[1], provisioningUrl, PACKAGE_NAME,
OPENTHREAD_CONFIG_PLATFORM_INFO, PACKAGE_VERSION, NULL, &Joiner::HandleCallback, this);
exit:
return error;
}
otError Joiner::ProcessStop(uint8_t aArgsLength, char *aArgs[])
{
OT_UNUSED_VARIABLE(aArgsLength);
OT_UNUSED_VARIABLE(aArgs);
otJoinerStop(mInterpreter.mInstance);
return OT_ERROR_NONE;
}
otError Joiner::Process(uint8_t aArgsLength, char *aArgs[])
{
otError error = OT_ERROR_INVALID_COMMAND;
if (aArgsLength < 1)
{
IgnoreError(ProcessHelp(0, NULL));
}
else
{
for (size_t i = 0; i < OT_ARRAY_LENGTH(sCommands); i++)
{
if (strcmp(aArgs[0], sCommands[i].mName) == 0)
{
error = (this->*sCommands[i].mCommand)(aArgsLength, aArgs);
break;
}
}
}
return error;
}
void Joiner::HandleCallback(otError aError, void *aContext)
{
static_cast<Joiner *>(aContext)->HandleCallback(aError);
}
void Joiner::HandleCallback(otError aError)
{
switch (aError)
{
case OT_ERROR_NONE:
mInterpreter.mServer->OutputFormat("Join success\r\n");
break;
default:
mInterpreter.mServer->OutputFormat("Join failed [%s]\r\n", otThreadErrorToString(aError));
break;
}
}
} }
#endif