Skip to content

Commit

Permalink
pvs and unit tests fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
portasynthinca3 committed Dec 24, 2024
1 parent 58bd664 commit 0916aeb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 59 deletions.
29 changes: 11 additions & 18 deletions applications/services/cli/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,24 @@ void cli_add_command(
CliCommandFlag flags,
CliExecuteCallback callback,
void* context) {
// missing checks performed by cli_add_command_ex
furi_check(cli);
furi_check(name);
furi_check(callback);

CliCommand cmd = {
FuriString* name_str;
name_str = furi_string_alloc_set(name);
// command cannot contain spaces
furi_check(furi_string_search_char(name_str, ' ') == FURI_STRING_FAILURE);

CliCommand command = {
.name = name,
.context = context,
.execute_callback = callback,
.complete_callback = NULL,
.flags = flags,
};
cli_add_command_ex(cli, &cmd);
}

void cli_add_command_ex(Cli* cli, CliCommand* command) {
furi_check(cli);
furi_check(command);
furi_check(command->name);
furi_check(command->execute_callback);

FuriString* name_str;
name_str = furi_string_alloc_set(command->name);
// command cannot contain spaces
furi_check(furi_string_search_char(name_str, ' ') == FURI_STRING_FAILURE);

furi_check(furi_mutex_acquire(cli->mutex, FuriWaitForever) == FuriStatusOk);
CliCommandTree_set_at(cli->commands, name_str, *command);
CliCommandTree_set_at(cli->commands, name_str, command);
furi_check(furi_mutex_release(cli->mutex) == FuriStatusOk);

furi_string_free(name_str);
Expand Down Expand Up @@ -75,7 +68,7 @@ bool cli_get_command(Cli* cli, FuriString* command, CliCommand* result) {
furi_check(furi_mutex_acquire(cli->mutex, FuriWaitForever) == FuriStatusOk);
CliCommand* data = CliCommandTree_get(cli->commands, command);
if(data) *result = *data;

furi_check(furi_mutex_release(cli->mutex) == FuriStatusOk);

return !!data;
Expand Down
40 changes: 0 additions & 40 deletions applications/services/cli/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,46 +60,6 @@ void cli_add_command(
CliExecuteCallback callback,
void* context);

ARRAY_DEF(CommandCompletions, FuriString*, FURI_STRING_OPLIST); // -V524
#define M_OPL_CommandCompletions_t() ARRAY_OPLIST(CommandCompletions)

/**
* @brief Command autocomplete callback.
*
* This callback will be called from the shell thread.
*
* @param [in] partial_args Input after the name of the command up to the point
* where TAB was pressed.
* @param [out] full_args An initialized empty array that you fill up with
* suggestions for the entire `args`.
*/
typedef void (*CliCompleteCallback)(
PipeSide* pipe,
FuriString* partial_args,
CommandCompletions_t full_args,
void* context);

/**
* @brief Extended command descriptor for `cli_add_command_ex`
*/
typedef struct {
const char* name; //<! Command name
void* context; //<! Context passed to callbacks
CliExecuteCallback execute_callback; //<! Callback for command execution
CliCompleteCallback complete_callback; //<! Callback for command completion. May be `NULL`
CliCommandFlag flags;
} CliCommand;

/**
* @brief Registers a command with the CLI. Provides more options than
* `cli_add_command`
*
* @param [in] cli Pointer to CLI instance
* @param [in] command Pointer to command descriptor. Not required to be valid
* after this function returns.
*/
void cli_add_command_ex(Cli* cli, CliCommand* command);

/**
* @brief Deletes a cli command
*
Expand Down
12 changes: 11 additions & 1 deletion applications/services/cli/cli_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,29 @@
extern "C" {
#endif

typedef struct {
const char* name; //<! Command name
void* context; //<! Context passed to callbacks
CliExecuteCallback execute_callback; //<! Callback for command execution
CliCommandFlag flags;
} CliCommand;

#define CLI_COMMANDS_TREE_RANK 4

// -V:BPTREE_DEF2:1103
// -V:BPTREE_DEF2:524
BPTREE_DEF2(
CliCommandTree,
CLI_COMMANDS_TREE_RANK,
FuriString*,
FURI_STRING_OPLIST,
CliCommand,
M_POD_OPLIST);

#define M_OPL_CliCommandTree_t() BPTREE_OPLIST(CliCommandTree, M_POD_OPLIST)

ARRAY_DEF(CommandCompletions, FuriString*, FURI_STRING_OPLIST); // -V524
#define M_OPL_CommandCompletions_t() ARRAY_OPLIST(CommandCompletions)

bool cli_get_command(Cli* cli, FuriString* command, CliCommand* result);

void cli_lock_commands(Cli* cli);
Expand Down

0 comments on commit 0916aeb

Please sign in to comment.