Skip to content

Commit 768174b

Browse files
committed
Command callback functions support
1 parent 41c24b6 commit 768174b

2 files changed

Lines changed: 155 additions & 4 deletions

File tree

src/minbasecli.cpp

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/**
33
* @file minbasecli.cpp
44
* @author Jose Miguel Rios Rubio <jrios.github@gmail.com>
5-
* @date 02-04-2022
6-
* @version 1.1.1
5+
* @date 09-07-2022
6+
* @version 1.2.0
77
*
88
* @section DESCRIPTION
99
*
@@ -59,6 +59,10 @@ MINBASECLI::MINBASECLI()
5959
{
6060
this->initialized = false;
6161
this->received_bytes = 0;
62+
this->num_added_commands = 0;
63+
this->cli_result.argc = 0U;
64+
this->cli_result.argv[0][0] = '\0';
65+
this->cli_result.cmd[0] = '\0';
6266
this->rx_read[0] = '\0';
6367
}
6468

@@ -78,6 +82,93 @@ bool MINBASECLI::setup(void* iface, const uint32_t baud_rate)
7882
return true;
7983
}
8084

85+
/**
86+
* @details
87+
* This function check if provided arguments are valid and if they there is
88+
* enough space in the added commands array to store a new command callback
89+
* info, and add a new command callback element to the list according to
90+
* provided arguments.
91+
*/
92+
bool MINBASECLI::add_cmd(const char* command,
93+
void (*callback)(int argc, char* argv[]),
94+
const char* description)
95+
{
96+
t_cmd_cb_info cmd_cb_info;
97+
size_t cmd_len = 0U;
98+
size_t cmd_description_len = 0U;
99+
100+
// Check if there is enough space to add a new command
101+
if (num_added_commands >= MINBASECLI_MAX_CMD_TO_ADD)
102+
return false;
103+
104+
// Check if provided argument are valid
105+
if ( (command == NULL) || (callback == NULL) || (description == NULL) )
106+
return false;
107+
108+
// Check and limit provided arguments lengths
109+
cmd_len = strlen(command);
110+
cmd_description_len = strlen(description);
111+
if (cmd_len >= MINBASECLI_MAX_CMD_LEN)
112+
cmd_len = MINBASECLI_MAX_CMD_LEN - 1U;
113+
if (cmd_description_len >= MINBASECLI_MAX_CMD_DESCRIPTION)
114+
cmd_description_len = MINBASECLI_MAX_CMD_DESCRIPTION - 1U;
115+
116+
// Create a new t_cmd_cb_info element with provided command data
117+
strncpy(cmd_cb_info.command, command, cmd_len);
118+
cmd_cb_info.command[cmd_len] = '\0';
119+
strncpy(cmd_cb_info.description, description, cmd_description_len);
120+
cmd_cb_info.description[cmd_description_len] = '\0';
121+
cmd_cb_info.callback = callback;
122+
123+
// Add the new command to the list of binded commands and increase the
124+
// number of added commands
125+
added_commands[num_added_commands] = cmd_cb_info;
126+
num_added_commands = num_added_commands + 1U;
127+
128+
return true;
129+
}
130+
131+
/**
132+
* @details
133+
* This function calls to manage the CLI to check if there is any new command
134+
* received avaliable to be handled, then check if the received command is one
135+
* of the added inside CLI component to be handle through a callback, and call
136+
* to the corresponding callback for it.
137+
*/
138+
bool MINBASECLI::run()
139+
{
140+
bool cmd_found = false;
141+
142+
// Do nothing if there is no added commands
143+
if (num_added_commands == 0U)
144+
return false;
145+
146+
// Check if there is any new command received by the CLI
147+
if (manage(&cli_result) == false)
148+
return false;
149+
150+
// Check if th command is added in the callback handle list
151+
for (uint8_t i = 0U; i < num_added_commands; i++)
152+
{
153+
// If command is found in the callbacks list, call to the callback
154+
if (strcmp(cli_result.cmd, added_commands[i].command) == 0U)
155+
{
156+
// Compose array of pointer for arguments
157+
char* ptr_argv[MINBASECLI_MAX_ARGV];
158+
for (int i = 0; i < MINBASECLI_MAX_ARGV; i++)
159+
ptr_argv[i] = cli_result.argv[i];
160+
161+
// Call to command callback
162+
added_commands[i].callback(cli_result.argc, ptr_argv);
163+
164+
cmd_found = true;
165+
break;
166+
}
167+
}
168+
169+
return cmd_found;
170+
}
171+
81172
/**
82173
* @details
83174
* This function checks and get any received data from the CLI interface and

src/minbasecli.h

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/**
33
* @file minbasecli.h
44
* @author Jose Miguel Rios Rubio <jrios.github@gmail.com>
5-
* @date 02-04-2022
6-
* @version 1.1.1
5+
* @date 09-07-2022
6+
* @version 1.2.0
77
*
88
* @section DESCRIPTION
99
*
@@ -118,6 +118,16 @@
118118
#define MINBASECLI_MAX_PRINT_SIZE 22
119119
#endif
120120

121+
// Maximum number of commands that can be added to the CLI
122+
#if !defined(MINBASECLI_MAX_CMD_TO_ADD)
123+
#define MINBASECLI_MAX_CMD_TO_ADD 16
124+
#endif
125+
126+
// Maximum length of command description text
127+
#if !defined(MINBASECLI_MAX_CMD_DESCRIPTION)
128+
#define MINBASECLI_MAX_CMD_DESCRIPTION 64
129+
#endif
130+
121131
/*****************************************************************************/
122132

123133
/* Data Types */
@@ -130,6 +140,14 @@ typedef struct t_cli_result
130140
uint8_t argc;
131141
} t_cli_result;
132142

143+
// Command function callback information
144+
typedef struct t_cmd_cb_info
145+
{
146+
char command[MINBASECLI_MAX_CMD_LEN];
147+
char description[MINBASECLI_MAX_CMD_DESCRIPTION];
148+
void (*callback)(int argc, char* argv[]);
149+
} t_cmd_cb_info;
150+
133151
/*****************************************************************************/
134152

135153
/* MinBaseCLI Class Interface */
@@ -160,6 +178,31 @@ class MINBASECLI : public MINBASECLI_HAL
160178
bool setup(void* iface,
161179
const uint32_t baud_rate=MINBASECLI_DEFAULT_BAUDS);
162180

181+
/**
182+
* @brief Add and bind a new command to a callback function.
183+
* @param command Command text that fires the callback.
184+
* @param callback Pointer to function that must be executed when the
185+
* command text is received through the CLI.
186+
* @param description Command description text that will be shown on
187+
* help command execution.
188+
* @return true if the command has been succssfully added/binded.
189+
* @return false if the command can't be added/binded (the command
190+
* already exists or there is no more memory space for a new command).
191+
*/
192+
bool add_cmd(const char* command,
193+
void (*callback)(int argc, char* argv[]),
194+
const char* description);
195+
196+
/**
197+
* @brief Let the Command Line Interface run an execution iteration to
198+
* check if an added command has been received and then call the
199+
* corresponding command function callback.
200+
* @return true if an added command has been detected and handled by
201+
* callback.
202+
* @return false if no added command has been detected.
203+
*/
204+
bool run();
205+
163206
/**
164207
* @brief Let the Command Line Interface run an execution iteration to
165208
* check for any incoming command from the CLI and get it.
@@ -193,6 +236,23 @@ class MINBASECLI : public MINBASECLI_HAL
193236
*/
194237
uint32_t received_bytes;
195238

239+
/**
240+
* @brief Current number of commands added to the CLI through add()
241+
* function.
242+
*/
243+
uint8_t num_added_commands;
244+
245+
/**
246+
* @brief Array of commands that are added to be handle through
247+
* callbacks by the add() function.
248+
*/
249+
t_cmd_cb_info added_commands[MINBASECLI_MAX_CMD_TO_ADD];
250+
251+
/**
252+
* @brief Last received command result.
253+
*/
254+
t_cli_result cli_result;
255+
196256
/**
197257
* @brief CLI data reception buffer.
198258
*/

0 commit comments

Comments
 (0)