@@ -10,170 +10,10 @@ extern "C" {
1010#endif
1111
1212#include <stdbool.h>
13+ #include "esp_commands_utils.h"
1314#include "esp_heap_caps.h"
1415#include "esp_err.h"
1516
16- #define _STRINGIFY (name ) #name
17-
18- /**
19- * @brief Function pointer type for writing bytes.
20- *
21- * @param fd File descriptor.
22- * @param buf Buffer containing bytes to write.
23- * @param count Number of bytes to write.
24- * @return Number of bytes written, or -1 on error.
25- */
26- typedef ssize_t (* esp_commands_write_t )(int fd , const void * buf , size_t count );
27-
28- /**
29- * @brief Structure containing dynamic argument necessary for the\
30- * command callback to execute properly
31- */
32- typedef struct esp_commands_exec_arg {
33- int out_fd ; /*!< file descriptor that the command function has to use to output data */
34- esp_commands_write_t write_func ; /*!< write function the command function has to use to output datga */
35- void * dynamic_ctx ; /*!< dynamic context passed to the command function */
36- } esp_commands_exec_arg_t ;
37-
38- /**
39- * @brief Console command main function type with user context
40- *
41- * This function type is used to implement a console command.
42- *
43- * @param context User-defined context passed at invocation
44- * @param cmd_arg Structure containing dynamic arguments necessary for the command
45- * @param argc Number of arguments
46- * @param argv Array of argc entries, each pointing to a null-terminated string argument
47- * @return Return code of the console command; 0 indicates success
48- */
49- typedef int (* esp_command_func_t )(void * context , esp_commands_exec_arg_t * cmd_arg , int argc , char * * argv );
50-
51- /**
52- * @brief Callback to generate a command hint
53- *
54- * This function is called to retrieve a short hint for a command,
55- * typically used for auto-completion or UI help.
56- *
57- * @param context Context registered when the command was registered
58- * @return Persistent string containing the generated hint
59- */
60- typedef const char * (* esp_command_hint_t )(void * context );
61-
62- /**
63- * @brief Callback to generate a command glossary entry
64- *
65- * This function is called to retrieve detailed description or glossary
66- * information for a command.
67- *
68- * @param context Context registered when the command was registered
69- * @return Persistent string containing the generated glossary
70- */
71- typedef const char * (* esp_command_glossary_t )(void * context );
72-
73- /**
74- * @brief Structure describing a console command
75- *
76- * @note The `group` field allows categorizing commands into groups,
77- * which can simplify filtering or listing commands.
78- */
79- typedef struct esp_command {
80- const char * name ; /*!< Name of the command */
81- const char * group ; /*!< Command group to which this command belongs */
82- const char * help ; /*!< Short help text for the command */
83- esp_command_func_t func ; /*!< Function implementing the command */
84- void * func_ctx ; /*!< User-defined context for the command function */
85- esp_command_hint_t hint_cb ; /*!< Callback returning the hint for the command */
86- esp_command_glossary_t glossary_cb ; /*!< Callback returning the glossary for the command */
87- } esp_command_t ;
88-
89- /**
90- * @brief Macro to define a forced inline accessor for a string field of esp_command_t
91- *
92- * @param NAME Field name of the esp_command_t structure
93- */
94- #define DEFINE_FIELD_ACCESSOR (NAME ) \
95- static inline __attribute__((always_inline)) \
96- const char *get_##NAME(const esp_command_t *cmd) { \
97- if (!cmd) { \
98- return NULL; \
99- } \
100- return cmd->NAME; \
101- }
102-
103- /**
104- * @brief Macro expanding to
105- * static inline __attribute__((always_inline)) const char *get_name(esp_command_t *cmd) {
106- * if (!cmd) {
107- * return NULL;
108- * }
109- * return cmd->name;
110- * }
111- */
112- DEFINE_FIELD_ACCESSOR (name )
113-
114- /**
115- * @brief Macro expanding to
116- * static inline __attribute__((always_inline)) const char *get_group(esp_command_t *cmd) {
117- * if (!cmd) {
118- * return NULL;
119- * }
120- * return cmd->group;
121- * }
122- */
123- DEFINE_FIELD_ACCESSOR (group )
124-
125- /**
126- * @brief Macro expanding to
127- * static inline __attribute__((always_inline)) const char *get_help(esp_command_t *cmd) {
128- * if (!cmd) {
129- * return NULL;
130- * }
131- * return cmd->help;
132- * }
133- */
134- DEFINE_FIELD_ACCESSOR (help )
135-
136- /**
137- * @brief Macro to create the accessor function name for a field of esp_command_t
138- *
139- * @param NAME Field name of esp_command_t
140- */
141- #define FIELD_ACCESSOR (NAME ) get_ ##NAME
142-
143- /**
144- * @brief Configuration parameters for esp_commands_manager initialization
145- */
146- typedef struct esp_commands_config {
147- uint32_t heap_caps_used ; /*!< Set of heap capabilities to be used to perform internal allocations */
148- size_t max_cmdline_length ; /*!< Maximum length of the command line buffer, in bytes */
149- size_t max_cmdline_args ; /*!< Maximum number of command line arguments to parse */
150- int hint_color ; /*!< ANSI color code used for hint text */
151- bool hint_bold ; /*!< If true, display hint text in bold */
152- } esp_commands_config_t ;
153-
154- /**
155- * @brief Callback for a completed command name
156- *
157- * This callback is called when a command is successfully completed.
158- *
159- * @param cb_ctx Opaque pointer pointing at the context passed to the callback
160- * @param completed_cmd_name Completed command name
161- */
162- typedef void (* esp_command_get_completion_t )(void * cb_ctx , const char * completed_cmd_name );
163-
164- /**
165- * @brief Callback to retrieve a string field of esp_command_t
166- *
167- * @param cmd Command object
168- * @return Value of the requested string field
169- */
170- typedef const char * (* esp_commands_get_field_t )(const esp_command_t * cmd );
171-
172- /**
173- * @brief Opaque handle to a set of commands
174- */
175- typedef struct esp_command_sets * esp_command_set_handle_t ;
176-
17717/**
17818 * @brief Update the component configuration
17919 *
@@ -187,9 +27,6 @@ esp_err_t esp_commands_update_config(const esp_commands_config_t *config);
18727 * @brief macro registering a command and placing it in a specific section of flash.rodata
18828 * @note see the linker.lf file for more information concerning the section characteristics
18929 */
190- #define _ESP_REPL_STRINGIFY (x ) #x
191- #define ESP_REPL_STRINGIFY (x ) _ESP_REPL_STRINGIFY (x )
192-
19330#define _ESP_COMMAND_REGISTER (cmd_name , cmd_group , cmd_help , cmd_func , cmd_func_ctx , cmd_hint_cb , cmd_glossary_cb ) \
19431 static_assert((cmd_func) != NULL); \
19532 static const esp_command_t cmd_name __attribute__((used, section(".esp_commands" "." _ESP_REPL_STRINGIFY(cmd_name)), aligned(4))) = { \
0 commit comments