Skip to content

Commit b45a1ad

Browse files
authored
[cli] handle duplicate registration in Interpreter::SetUserCommands() (openthread#13018)
This commit updates the `Interpreter::SetUserCommands()` method to detect if a set of CLI commands is already registered. If a duplicate registration is detected, the method now returns `OT_ERROR_NONE` and exits early without consuming an additional slot in the user commands table. It also ensures that `OT_ERROR_FAILED` is only returned when there are no available slots for a new registration.
1 parent 69dd336 commit b45a1ad

4 files changed

Lines changed: 19 additions & 12 deletions

File tree

include/openthread/cli.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ void otCliInputLine(char *aBuf);
9999
* Set a user command table.
100100
*
101101
* @param[in] aUserCommands A pointer to an array with user commands.
102-
* @param[in] aLength @p aUserCommands length.
103-
* @param[in] aContext @p The context passed to the handler.
102+
* @param[in] aLength The @p aUserCommands length.
103+
* @param[in] aContext The context passed to the handler.
104104
*
105-
* @retval OT_ERROR_NONE Successfully updated command table with commands from @p aUserCommands.
106-
* @retval OT_ERROR_FAILED Maximum number of command entries have already been set.
105+
* @retval OT_ERROR_NONE Successfully updated command table with commands from @p aUserCommands.
106+
* @retval OT_ERROR_NO_BUFS Maximum number of command entries have already been set.
107107
*/
108108
otError otCliSetUserCommands(const otCliCommand *aUserCommands, uint8_t aLength, void *aContext);
109109

include/openthread/instance.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ extern "C" {
5252
*
5353
* @note This number versions both OpenThread platform and user APIs.
5454
*/
55-
#define OPENTHREAD_API_VERSION (593)
55+
#define OPENTHREAD_API_VERSION (594)
5656

5757
/**
5858
* @addtogroup api-instance

src/cli/cli.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,21 +400,28 @@ otError Interpreter::ProcessUserCommands(Arg aArgs[])
400400

401401
otError Interpreter::SetUserCommands(const otCliCommand *aCommands, uint8_t aLength, void *aContext)
402402
{
403-
otError error = OT_ERROR_FAILED;
403+
otError error = OT_ERROR_NONE;
404404

405405
for (UserCommandsEntry &entry : mUserCommands)
406406
{
407+
if (entry.mCommands == aCommands)
408+
{
409+
// Ignore if already registered.
410+
ExitNow();
411+
}
412+
407413
if (entry.mCommands == nullptr)
408414
{
409415
entry.mCommands = aCommands;
410416
entry.mLength = aLength;
411417
entry.mContext = aContext;
412-
413-
error = OT_ERROR_NONE;
414-
break;
418+
ExitNow();
415419
}
416420
}
417421

422+
error = OT_ERROR_NO_BUFS;
423+
424+
exit:
418425
return error;
419426
}
420427

src/cli/cli.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ class Interpreter : public OutputImplementer, public Utils
176176
*
177177
* @param[in] aCommands A pointer to an array with user commands.
178178
* @param[in] aLength @p aUserCommands length.
179-
* @param[in] aContext @p aUserCommands length.
179+
* @param[in] aContext Context to use when invoking the command handler.
180180
*
181-
* @retval OT_ERROR_NONE Successfully updated command table with commands from @p aCommands.
182-
* @retval OT_ERROR_FAILED No available UserCommandsEntry to register requested user commands.
181+
* @retval OT_ERROR_NONE Successfully updated command table with commands from @p aCommands.
182+
* @retval OT_ERROR_NO_BUFS No available `UserCommandsEntry` to register the requested user commands.
183183
*/
184184
otError SetUserCommands(const otCliCommand *aCommands, uint8_t aLength, void *aContext);
185185

0 commit comments

Comments
 (0)