33#include "esp_heap_caps.h"
44#include "esp_err.h"
55
6- #define ANSI_COLOR_DEFAULT 39 /** Default foreground color */
6+ /* Default foreground color */
7+ #define ANSI_COLOR_DEFAULT 39
78
89/* Pointers to the first and last command in the dedicated section.
910 * See linker.lf for detailed information about the section */
1011extern esp_command_t _esp_commands_start ;
1112extern esp_command_t _esp_commands_end ;
1213
13- /**
14- * @brief go through all commands registered in the
15- * memory section starting at _esp_commands_start
16- * and ending at _esp_commands_end
17- */
18- #define FOR_EACH_COMMAND_IN_SECTION (cmd ) \
19- for ((cmd) = &_esp_commands_start; \
20- (cmd) < &_esp_commands_end; \
21- (esp_command_t*)(cmd)++)
22-
23- #define FOR_EACH_COMMAND_IN_SET (cmd , cmd_set ) \
24- for ((cmd) = cmd_set->cmd_ptr_set[0]; \
25- (cmd) < cmd_set->cmd_ptr_set[cmd_set->cmd_prt_set_size - 1]; \
26- (esp_command_t*)(cmd)++)
27-
2814/**
2915 * @brief Array of pointers to command defining
3016 * a set of command. Created while calling
@@ -36,12 +22,10 @@ typedef struct esp_command_set {
3622} esp_command_set_t ;
3723
3824/**
39- * @brief find a command by its name in the list of registered
40- * commands
41- *
42- * @param name name of the command to find
43- * @return esp_command_t* pointer to the command matching the command
44- * name, NULL if the command is not found
25+ * @brief go through all commands registered in the
26+ * memory section starting at _esp_commands_start
27+ * and ending at _esp_commands_end OR go through all
28+ * the commands listed in cmd_set if not NULL
4529 */
4630#define FOR_EACH_COMMAND (cmd_set , cmd ) \
4731 for (size_t _i = 0; \
@@ -52,44 +36,50 @@ typedef struct esp_command_set {
5236 _i < (cmd_set)->cmd_set_size)); \
5337 ++_i)
5438
39+ /**
40+ * @brief returns the number of commands registered
41+ * in the .esp_commands section
42+ */
43+ #define ESP_COMMANDS_COUNT (size_t)(&_esp_commands_end - &_esp_commands_start)
44+
5545/**
5646 * @brief find a command by its name in the list of registered
57- * commands
47+ * commands or in a command set if the parameter cmd_set is set.
48+ *
49+ * @note If cmd_set is set to NULL by the caller, then the function
50+ * will try to find a command by name from the list of registered
51+ * commands in the .esp_commands section
5852 *
53+ * @param cmd_set command set to find a command from
5954 * @param name name of the command to find
6055 * @return esp_command_t* pointer to the command matching the command
6156 * name, NULL if the command is not found
6257 */
63- static esp_command_t * find_command_by_name_in_set (esp_command_set_t * cmd_set , const char * name )
58+ static esp_command_t * find_command_by_name (esp_command_set_t * cmd_set , const char * name )
6459{
6560 /* no need to check that cmd_set is NULL, if it is, then FOR_EACH_COMMAND will go through
6661 * the commands registered in the .esp_commands section */
6762 if (!name ) {
6863 return NULL ;
6964 }
7065 esp_command_t * cmd = NULL ;
71- FOR_EACH_COMMAND_IN_SET (cmd , cmd_set ) {
72- if (!cmd ) {
73- /* this happens if a command name passed in a set was not found,
74- * the pointer to command is set to NULL */
75- continue ;
76- }
77- if (strcmp (cmd -> command , name ) == 0 ) {
66+ FOR_EACH_COMMAND (cmd_set , cmd ) {
67+ if (strcmp (cmd -> name , name ) == 0 ) {
7868 return cmd ;
7969 }
8070 }
8171 return NULL ;
8272}
8373
8474/** run-time configuration options */
85- static esp_console_config_t s_config = {
75+ static esp_commands_config_t s_config = {
8676 .heap_alloc_caps = MALLOC_CAP_DEFAULT
8777};
8878
8979/** temporary buffer used for command line parsing */
9080static char * s_tmp_line_buf ;
9181
92- esp_err_t esp_console_init (const esp_console_config_t * config )
82+ esp_err_t esp_commands_init (const esp_commands_config_t * config )
9383{
9484 if (!config ) {
9585 return ESP_ERR_INVALID_ARG ;
@@ -111,7 +101,7 @@ esp_err_t esp_console_init(const esp_console_config_t *config)
111101 return ESP_OK ;
112102}
113103
114- esp_err_t esp_console_deinit (void )
104+ esp_err_t esp_commands_deinit (void )
115105{
116106 if (!s_tmp_line_buf ) {
117107 return ESP_ERR_INVALID_STATE ;
@@ -133,8 +123,8 @@ esp_err_t esp_commands_execute(esp_command_set_handle_t cmd_set, const char *cmd
133123 }
134124 strlcpy (s_tmp_line_buf , cmdline , s_config .max_cmdline_length );
135125
136- size_t argc = esp_console_split_argv (s_tmp_line_buf , argv ,
137- s_config .max_cmdline_args );
126+ size_t argc = esp_commands_split_argv (s_tmp_line_buf , argv ,
127+ s_config .max_cmdline_args );
138128 if (argc == 0 ) {
139129 free (argv );
140130 return ESP_ERR_INVALID_ARG ;
@@ -163,14 +153,14 @@ esp_err_t esp_commands_execute(esp_command_set_handle_t cmd_set, const char *cmd
163153 // executing help command, pass the cmd_set as context
164154 * cmd_ret = (* cmd -> func_w_ctx )(cmd_set , argc , argv );
165155 } else {
166- * cmd_ret = (* cmd -> func_w_context )(cmd -> context , argc , argv );
156+ * cmd_ret = (* cmd -> func_w_ctx )(cmd -> func_ctx , argc , argv );
167157 }
168158 }
169159 free (argv );
170160 return ESP_OK ;
171161}
172162
173- esp_command_set_handle_t esp_commands_create_cmd_set (const char * * cmd_name_set , const size_t cmd_name_set_size , esp_commands_get_field_t get_field )
163+ esp_command_set_handle_t esp_commands_create_cmd_set (const char * * cmd_set , const size_t cmd_set_size , esp_commands_get_field_t get_field )
174164{
175165 if (!cmd_set || cmd_set_size == 0 ) {
176166 return NULL ;
@@ -190,8 +180,8 @@ esp_command_set_handle_t esp_commands_create_cmd_set(const char **cmd_name_set,
190180 FOR_EACH_COMMAND ((esp_command_set_t * )NULL , it ) {
191181 if (strcmp (get_field (it ), cmd_set [i ]) == 0 ) {
192182 // it's a match, add the pointer to command to the cmd ptr set
193- cmd_ptrs [ i ] = it ;
194- command_found = true ;
183+ cmd_ptrs_temp [ cmd_ptr_count ] = it ;
184+ cmd_ptr_count ++ ;
195185 continue ;
196186 }
197187 }
@@ -257,7 +247,7 @@ esp_command_set_handle_t esp_commands_concat_cmd_set(esp_command_set_handle_t cm
257247 esp_commands_destroy_cmd_set (& cmd_set_a );
258248 esp_commands_destroy_cmd_set (& cmd_set_b );
259249
260- return (esp_command_set_t ) cmd_set ;
250+ return (esp_command_set_handle_t ) concat_cmd_set ;
261251}
262252
263253void esp_commands_destroy_cmd_set (esp_command_set_handle_t * cmd_set )
@@ -283,8 +273,8 @@ void esp_commands_get_completion(const char *buf, esp_command_get_completion_t c
283273 esp_command_t * it ;
284274 FOR_EACH_COMMAND ((esp_command_set_t * )NULL , it ) {
285275 /* Check if command starts with buf */
286- if (strncmp (buf , it -> command , len ) == 0 ) {
287- completion_cb (it -> command );
276+ if (strncmp (buf , it -> name , len ) == 0 ) {
277+ completion_cb (it -> name );
288278 }
289279 }
290280}
@@ -301,7 +291,7 @@ const char *esp_commands_get_hint(const char *buf, int *color, int *bold)
301291 }
302292 * color = s_config .hint_color ;
303293 * bold = s_config .hint_bold ;
304- return it -> get_hint_cb ();
294+ return it -> hint_cb ();
305295 }
306296 }
307297 return NULL ;
@@ -317,25 +307,25 @@ static void print_arg_help(esp_command_t *it)
317307 /* First line: command name and hint
318308 * Pad all the hints to the same column
319309 */
320- printf ("%-s" , it -> command );
321- if (it -> get_hint_cb ) {
322- printf (" %s\n" , it -> get_hint_cb ());
310+ printf ("%-s" , it -> name );
311+ if (it -> hint_cb ) {
312+ printf (" %s\n" , it -> hint_cb ());
323313 } else {
324314 printf ("\n" );
325315 }
326316
327317 /* Second line: print help */
328318 /* TODO: replace the simple print with a function that
329319 * replaces arg_print_formatted */
330- if (it -> get_help_cb ) {
331- printf (" %s\n" , it -> get_help_cb () );
320+ if (it -> help ) {
321+ printf (" %s\n" , it -> help );
332322 } else {
333323 printf (" -\n" );
334324 }
335325
336326 /* Third line: print the glossary*/
337- if (it -> get_glossary_cb ) {
338- printf ("%s\n" , it -> get_glossary_cb ());
327+ if (it -> glossary_cb ) {
328+ printf ("%s\n" , it -> glossary_cb ());
339329 } else {
340330 printf (" -\n" );
341331 }
@@ -345,9 +335,9 @@ static void print_arg_help(esp_command_t *it)
345335
346336static void print_arg_command (esp_command_t * it )
347337{
348- printf ("%-s" , it -> command );
349- if (it -> get_hint_cb ) {
350- printf (" %s\n" , it -> get_hint_cb ());
338+ printf ("%-s" , it -> name );
339+ if (it -> hint_cb ) {
340+ printf (" %s\n" , it -> hint_cb ());
351341 }
352342}
353343
@@ -417,21 +407,21 @@ static int help_command(void *context, int argc, char **argv)
417407 * is not NULL, find the command and only print the help for this command. if the
418408 * command is not found, return with error */
419409 bool command_found = false;
420- for ( size_t i = 0 ; i < cmd_set -> cmd_set_size ; i ++ ) {
421-
422- if (!cmd_set -> cmd_ptr_set [ i ] ) {
410+ esp_command_t * it = NULL ;
411+ FOR_EACH_COMMAND ( cmd_set , it ) {
412+ if (!it ) {
423413 /* this happens if a command name passed in a set was not found,
424414 * the pointer to command is set to NULL */
425415 continue ;
426416 }
427417
428418 if (!command_name ) {
429419 /* command_name is empty, print all commands */
430- print_verbose_level_arr [verbose_level ](cmd_set -> cmd_ptr_set [ i ] );
420+ print_verbose_level_arr [verbose_level ](it );
431421 } else if (command_name &&
432- (strcmp (command_name , cmd_set -> cmd_ptr_set [ i ] -> command ) == 0 )) {
422+ (strcmp (command_name , it -> name ) == 0 )) {
433423 /* we found the command name, print the help and return */
434- print_verbose_level_arr [verbose_level ](cmd_set -> cmd_ptr_set [ i ] );
424+ print_verbose_level_arr [verbose_level ](it );
435425 command_found = true;
436426 break ;
437427 }
@@ -456,11 +446,14 @@ static const char *get_help_glossary(void)
456446 " -v, --verbose <0|1> If specified, list console commands with given verbose level" ;;
457447}
458448
459- ESP_COMMAND_REGISTER (help ,
460- NULL , /* the help should be a part of all set, it does not need a group name */
461- "Print the summary of all registered commands if no arguments "
462- "are given, otherwise print summary of given command." ,
463- & help_command ,
449+ static const char help_str [] = "Print the summary of all registered commands if no arguments "
450+ "are given, otherwise print summary of given command." ;
451+
452+ ESP_COMMAND_REGISTER (help , /* name of the heap command */
453+ help , /* group of the help command */
454+ help_str , /* help string of the help command */
455+ NULL , /* func (null since func with context is used) */
456+ help_command , /* func_w_ctx */
464457 NULL , /* the context is null here, it will provided by the exec function */
465- get_help_hint ,
466- get_help_glossary );
458+ get_help_hint , /* hint callback */
459+ get_help_glossary ); /* glossary callback */
0 commit comments