diff --git a/examples/platforms/nrf52811/diag.c b/examples/platforms/nrf52811/diag.c index 232f160a3..9401dd4e5 100644 --- a/examples/platforms/nrf52811/diag.c +++ b/examples/platforms/nrf52811/diag.c @@ -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]); } diff --git a/examples/platforms/nrf52840/diag.c b/examples/platforms/nrf52840/diag.c index 636363a62..e76aa1c6a 100644 --- a/examples/platforms/nrf52840/diag.c +++ b/examples/platforms/nrf52840/diag.c @@ -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]); } diff --git a/examples/platforms/utils/code_utils.h b/examples/platforms/utils/code_utils.h index 30a9e0168..90c4827e0 100644 --- a/examples/platforms/utils/code_utils.h +++ b/examples/platforms/utils/code_utils.h @@ -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 diff --git a/src/diag/diag_process.cpp b/src/diag/diag_process.cpp index d7e071b25..16cf2396b 100644 --- a/src/diag/diag_process.cpp +++ b/src/diag/diag_process.cpp @@ -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(); } }