Skip to content

Commit f6b37f7

Browse files
committed
Pointer to CLI in command callbacks
1 parent c424431 commit f6b37f7

2 files changed

Lines changed: 44 additions & 23 deletions

File tree

src/minbasecli.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ bool MINBASECLI::setup(void* iface, const uint32_t baud_rate)
102102
* info, and add a new command callback element to the list according to
103103
* provided arguments.
104104
*/
105-
bool MINBASECLI::add_cmd(const char* command,
106-
void (*callback)(int argc, char* argv[]),
105+
bool MINBASECLI::add_cmd(const char* command, t_command_callback callback,
107106
const char* description)
108107
{
109108
t_cmd_cb_info cmd_cb_info;
@@ -191,7 +190,7 @@ bool MINBASECLI::run()
191190
if (strcmp(cli_result.cmd, added_commands[i].command) == 0U)
192191
{
193192
// Call to command callback
194-
added_commands[i].callback(cli_result.argc, ptr_argv);
193+
added_commands[i].callback(this, cli_result.argc, ptr_argv);
195194
cmd_found = true;
196195
break;
197196
}

src/minbasecli.h

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -146,22 +146,28 @@ static const char CMD_HELP_DESCRIPTION[] = "Shows current info.";
146146

147147
/* Data Types */
148148

149-
// CLI manage result data
150-
typedef struct t_cli_result
151-
{
152-
char cmd[MINBASECLI_MAX_CMD_LEN];
153-
char argv[MINBASECLI_MAX_ARGV][MINBASECLI_MAX_ARGV_LEN];
154-
uint8_t argc;
155-
} t_cli_result;
149+
// Forward Declaration of current class
150+
class MINBASECLI;
151+
152+
// Command callbacks type
153+
typedef void (*t_command_callback)(MINBASECLI* Cli, int argc, char* argv[]);
156154

157155
// Command function callback information
158156
typedef struct t_cmd_cb_info
159157
{
160158
char command[MINBASECLI_MAX_CMD_LEN];
161159
char description[MINBASECLI_MAX_CMD_DESCRIPTION];
162-
void (*callback)(int argc, char* argv[]);
160+
t_command_callback callback;
163161
} t_cmd_cb_info;
164162

163+
// CLI manage result data
164+
typedef struct t_cli_result
165+
{
166+
char cmd[MINBASECLI_MAX_CMD_LEN];
167+
char argv[MINBASECLI_MAX_ARGV][MINBASECLI_MAX_ARGV_LEN];
168+
uint8_t argc;
169+
} t_cli_result;
170+
165171
/*****************************************************************************/
166172

167173
/* MinBaseCLI Class Interface */
@@ -190,8 +196,10 @@ class MINBASECLI : public MINBASECLI_HAL
190196
* @param baud_rate Communication speed for the CLI.
191197
* @return Setup result success/fail (true/false).
192198
*/
193-
bool setup(void* iface=MINBASECLI_DEFAULT_IFACE,
194-
const uint32_t baud_rate=MINBASECLI_DEFAULT_BAUDS);
199+
bool setup(
200+
void* iface=MINBASECLI_DEFAULT_IFACE,
201+
const uint32_t baud_rate=MINBASECLI_DEFAULT_BAUDS
202+
);
195203

196204
/**
197205
* @brief Add and bind a new command to a callback function.
@@ -204,9 +212,11 @@ class MINBASECLI : public MINBASECLI_HAL
204212
* @return false if the command can't be added/bind (the command
205213
* already exists or there is no more memory space for a new command).
206214
*/
207-
bool add_cmd(const char* command,
208-
void (*callback)(int argc, char* argv[]),
209-
const char* description);
215+
bool add_cmd(
216+
const char* command,
217+
t_command_callback callback,
218+
const char* description
219+
);
210220

211221
/**
212222
* @brief Let the Command Line Interface run an execution iteration to
@@ -344,9 +354,13 @@ class MINBASECLI : public MINBASECLI_HAL
344354
* @param str_read_size Max size of read buffer.
345355
* @return If character "until_c" was found (true/false).
346356
*/
347-
bool str_read_until_char(char* str, const size_t str_len,
348-
const char until_c, char* str_read,
349-
const size_t str_read_size);
357+
bool str_read_until_char(
358+
char* str,
359+
const size_t str_len,
360+
const char until_c,
361+
char* str_read,
362+
const size_t str_read_size
363+
);
350364

351365
/**
352366
* @brief Print a string.
@@ -365,8 +379,12 @@ class MINBASECLI : public MINBASECLI_HAL
365379
* etc.).
366380
* @return Conversion result (false - fail; true - success).
367381
*/
368-
bool u64toa(uint64_t number, char* str, const uint8_t str_max_size,
369-
const uint8_t base);
382+
bool u64toa(
383+
uint64_t number,
384+
char* str,
385+
const uint8_t str_max_size,
386+
const uint8_t base
387+
);
370388

371389
/**
372390
* @brief Convert a signed integer of 64 bits (int64_t) into a string
@@ -379,8 +397,12 @@ class MINBASECLI : public MINBASECLI_HAL
379397
* etc.).
380398
* @return Conversion result (false - fail; true - success).
381399
*/
382-
bool i64toa(int64_t number, char* str, const uint8_t str_max_size,
383-
const uint8_t base);
400+
bool i64toa(
401+
int64_t number,
402+
char* str,
403+
const uint8_t str_max_size,
404+
const uint8_t base
405+
);
384406

385407
/**
386408
* @brief Reverse string characters ("ABCD" -> "DCBA").

0 commit comments

Comments
 (0)