mirror of
https://github.com/espressif/openthread.git
synced 2026-07-28 14:47:46 +00:00
[diag] use OT_ARRAY_LENGTH to unify code and shrink image size (#3677)
This commit is contained in:
committed by
Jonathan Hui
parent
6dc3b28b3f
commit
c02a985609
@@ -380,9 +380,9 @@ const struct PlatformDiagCommand sCommands[] = {
|
||||
|
||||
void otPlatDiagProcess(otInstance *aInstance, int argc, char *argv[], char *aOutput, size_t aOutputMaxLen)
|
||||
{
|
||||
uint32_t i;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(sCommands) / sizeof(sCommands[0]); i++)
|
||||
for (i = 0; i < otARRAY_LENGTH(sCommands); i++)
|
||||
{
|
||||
if (strcmp(argv[0], sCommands[i].mName) == 0)
|
||||
{
|
||||
@@ -391,7 +391,7 @@ void otPlatDiagProcess(otInstance *aInstance, int argc, char *argv[], char *aOut
|
||||
}
|
||||
}
|
||||
|
||||
if (i == sizeof(sCommands) / sizeof(sCommands[0]))
|
||||
if (i == otARRAY_LENGTH(sCommands))
|
||||
{
|
||||
snprintf(aOutput, aOutputMaxLen, "diag feature '%s' is not supported\r\n", argv[0]);
|
||||
}
|
||||
|
||||
@@ -380,9 +380,9 @@ const struct PlatformDiagCommand sCommands[] = {
|
||||
|
||||
void otPlatDiagProcess(otInstance *aInstance, int argc, char *argv[], char *aOutput, size_t aOutputMaxLen)
|
||||
{
|
||||
uint32_t i;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(sCommands) / sizeof(sCommands[0]); i++)
|
||||
for (i = 0; i < otARRAY_LENGTH(sCommands); i++)
|
||||
{
|
||||
if (strcmp(argv[0], sCommands[i].mName) == 0)
|
||||
{
|
||||
@@ -391,7 +391,7 @@ void otPlatDiagProcess(otInstance *aInstance, int argc, char *argv[], char *aOut
|
||||
}
|
||||
}
|
||||
|
||||
if (i == sizeof(sCommands) / sizeof(sCommands[0]))
|
||||
if (i == otARRAY_LENGTH(sCommands))
|
||||
{
|
||||
snprintf(aOutput, aOutputMaxLen, "diag feature '%s' is not supported\r\n", argv[0]);
|
||||
}
|
||||
|
||||
@@ -71,4 +71,14 @@
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* This macro calculates the number of elements in an array.
|
||||
*
|
||||
* @param[in] aArray Name of the array variable.
|
||||
*
|
||||
* @returns Number of elements in the array.
|
||||
*
|
||||
*/
|
||||
#define otARRAY_LENGTH(aArray) (sizeof(aArray) / sizeof(aArray[0]))
|
||||
|
||||
#endif // CODE_UTILS_H
|
||||
|
||||
@@ -46,9 +46,8 @@ namespace ot {
|
||||
namespace Diagnostics {
|
||||
|
||||
const struct Diag::Command Diag::sCommands[] = {
|
||||
{"start", &ProcessStart}, {"stop", &ProcessStop}, {"channel", &ProcessChannel},
|
||||
{"power", &ProcessPower}, {"send", &ProcessSend}, {"repeat", &ProcessRepeat},
|
||||
{"stats", &ProcessStats}, {"radio", &ProcessRadio}, {NULL, NULL},
|
||||
{"start", &ProcessStart}, {"stop", &ProcessStop}, {"channel", &ProcessChannel}, {"power", &ProcessPower},
|
||||
{"send", &ProcessSend}, {"repeat", &ProcessRepeat}, {"stats", &ProcessStats}, {"radio", &ProcessRadio},
|
||||
};
|
||||
|
||||
struct Diag::DiagStats Diag::sStats;
|
||||
@@ -86,11 +85,11 @@ void Diag::ProcessCmd(int aArgCount, char *aArgVector[], char *aOutput, size_t a
|
||||
ExitNow();
|
||||
}
|
||||
|
||||
for (const Command *command = &sCommands[0]; command->mName != NULL; command++)
|
||||
for (size_t i = 0; i < OT_ARRAY_LENGTH(sCommands); i++)
|
||||
{
|
||||
if (strcmp(aArgVector[0], command->mName) == 0)
|
||||
if (strcmp(aArgVector[0], sCommands[i].mName) == 0)
|
||||
{
|
||||
command->mHandler(aArgCount - 1, (aArgCount > 1) ? &aArgVector[1] : NULL, aOutput, aOutputMaxLen);
|
||||
sCommands[i].mHandler(aArgCount - 1, (aArgCount > 1) ? &aArgVector[1] : NULL, aOutput, aOutputMaxLen);
|
||||
ExitNow();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user