[utils] adding 'LookupTable' class and use it in CLI (#5602)

This commit adds `LookupTable` module which provides helper methods to
perform binary search in a generic sorted lookup table for specific
entry matching a given name string. It also provides `constexpr`
methods to check (at build-time) whether a lookup table array is
sorted. This commit also updates CLI modules to use the `LookupTable`
methods for command lookup. It also adds a unit test for the lookup
table.
This commit is contained in:
Abtin Keshavarzian
2020-10-02 17:35:35 -07:00
committed by GitHub
parent 946141ed32
commit 4db030c6c9
26 changed files with 602 additions and 260 deletions
+11 -24
View File
@@ -44,13 +44,7 @@ using ot::Encoding::BigEndian::HostSwap16;
namespace ot {
namespace Cli {
const UdpExample::Command UdpExample::sCommands[] = {{"help", &UdpExample::ProcessHelp},
{"bind", &UdpExample::ProcessBind},
{"close", &UdpExample::ProcessClose},
{"connect", &UdpExample::ProcessConnect},
{"linksecurity", &UdpExample::ProcessLinkSecurity},
{"open", &UdpExample::ProcessOpen},
{"send", &UdpExample::ProcessSend}};
constexpr UdpExample::Command UdpExample::sCommands[];
UdpExample::UdpExample(Interpreter &aInterpreter)
: mInterpreter(aInterpreter)
@@ -289,24 +283,17 @@ exit:
otError UdpExample::Process(uint8_t aArgsLength, char *aArgs[])
{
otError error = OT_ERROR_INVALID_COMMAND;
otError error = OT_ERROR_INVALID_ARGS;
const Command *command;
if (aArgsLength < 1)
{
IgnoreError(ProcessHelp(0, nullptr));
error = OT_ERROR_INVALID_ARGS;
}
else
{
for (const Command &command : sCommands)
{
if (strcmp(aArgs[0], command.mName) == 0)
{
error = (this->*command.mCommand)(aArgsLength - 1, aArgs + 1);
break;
}
}
}
VerifyOrExit(aArgsLength != 0, IgnoreError(ProcessHelp(0, nullptr)));
command = Utils::LookupTable::Find(aArgs[0], sCommands);
VerifyOrExit(command != nullptr, error = OT_ERROR_INVALID_COMMAND);
error = (this->*command->mHandler)(aArgsLength - 1, aArgs + 1);
exit:
return error;
}