@@ -13,6 +13,179 @@ extern "C" {
1313#include "esp_err.h"
1414
1515
16+ /**
17+ * @brief Function prototype for reading input for the REPL.
18+ *
19+ * @param ctx User-defined context pointer.
20+ * @param buf Buffer to store the read data.
21+ * @param buf_size Size of the buffer in bytes.
22+ *
23+ * @return ESP_OK on success, error code otherwise.
24+ */
25+ typedef esp_err_t (* esp_repl_reader_fn )(void * ctx , char * buf , size_t buf_size );
26+
27+ /**
28+ * @brief Reader configuration structure for the REPL.
29+ */
30+ typedef struct esp_repl_reader {
31+ esp_repl_reader_fn func ; /**!< Function to read input */
32+ void * ctx ; /**!< Context passed to the reader function */
33+ } esp_repl_reader_t ;
34+
35+ /**
36+ * @brief Function prototype called before executing a command.
37+ *
38+ * @param ctx User-defined context pointer.
39+ * @param buf Buffer containing the command.
40+ * @param reader_ret_val Return value from the reader function.
41+ *
42+ * @return ESP_OK to continue execution, error code to abort.
43+ */
44+ typedef esp_err_t (* esp_repl_pre_executor_fn )(void * ctx , char * buf , const esp_err_t reader_ret_val );
45+
46+ /**
47+ * @brief Pre-executor configuration structure for the REPL.
48+ */
49+ typedef struct esp_repl_pre_executor {
50+ esp_repl_pre_executor_fn func ; /**!< Function to run before command execution */
51+ void * ctx ; /**!< Context passed to the pre-executor function */
52+ } esp_repl_pre_executor_t ;
53+
54+ /**
55+ * @brief Function prototype to execute a REPL command.
56+ *
57+ * @param ctx User-defined context pointer.
58+ * @param buf Null-terminated command string.
59+ * @param ret_val Pointer to store the command return value.
60+ *
61+ * @return ESP_OK on success, error code otherwise.
62+ */
63+ typedef esp_err_t (* esp_repl_executor_fn )(void * ctx , const char * buf , int * ret_val );
64+
65+ /**
66+ * @brief Executor configuration structure for the REPL.
67+ */
68+ typedef struct esp_repl_executor {
69+ esp_repl_executor_fn func ; /**!< Function to execute commands */
70+ void * ctx ; /**!< Context passed to the executor function */
71+ } esp_repl_executor_t ;
72+
73+ /**
74+ * @brief Function prototype called after executing a command.
75+ *
76+ * @param ctx User-defined context pointer.
77+ * @param buf Command that was executed.
78+ * @param executor_ret_val Return value from the executor function.
79+ * @param cmd_ret_val Command-specific return value.
80+ *
81+ * @return ESP_OK on success, error code otherwise.
82+ */
83+ typedef esp_err_t (* esp_repl_post_executor_fn )(void * ctx , const char * buf , const esp_err_t executor_ret_val , const int cmd_ret_val );
84+
85+ /**
86+ * @brief Post-executor configuration structure for the REPL.
87+ */
88+ typedef struct esp_repl_post_executor {
89+ esp_repl_post_executor_fn func ; /**!< Function called after command execution */
90+ void * ctx ; /**!< Context passed to the post-executor function */
91+ } esp_repl_post_executor_t ;
92+
93+ /**
94+ * @brief Function prototype called when the REPL is stopping.
95+ *
96+ * This callback allows the user to unblock the reader (or perform other
97+ * cleanup) so that the REPL can return from `esp_repl()`.
98+ *
99+ * @param ctx User-defined context pointer.
100+ * @param handle Handle to the REPL instance.
101+ */
102+ typedef void (* esp_repl_on_stop_fn )(void * ctx , esp_linenoise_handle_t handle );
103+
104+ /**
105+ * @brief Stop callback configuration structure for the REPL.
106+ */
107+ typedef struct esp_repl_on_stop {
108+ esp_repl_on_stop_fn func ; /**!< Function called when REPL stop is requested */
109+ void * ctx ; /**!< Context passed to the on_stop function */
110+ } esp_repl_on_stop_t ;
111+
112+ /**
113+ * @brief Function prototype called when the REPL exits.
114+ *
115+ * @param ctx User-defined context pointer.
116+ * @param handle Handle to the REPL instance.
117+ */
118+ typedef void (* esp_repl_on_exit_fn )(void * ctx , esp_linenoise_handle_t handle );
119+
120+ /**
121+ * @brief Exit callback configuration structure for the REPL.
122+ */
123+ typedef struct esp_repl_on_exit {
124+ esp_repl_on_exit_fn func ; /**!< Function called on REPL exit */
125+ void * ctx ; /**!< Context passed to the exit function */
126+ } esp_repl_on_exit_t ;
127+
128+ /**
129+ * @brief Configuration structure to initialize a REPL instance.
130+ */
131+ typedef struct esp_repl_config {
132+ size_t max_cmd_line_size ; /**!< Maximum allowed command line size */
133+ esp_repl_reader_t reader ; /**!< Reader callback and context */
134+ esp_repl_pre_executor_t pre_executor ; /**!< Pre-executor callback and context */
135+ esp_repl_executor_t executor ; /**!< Executor callback and context */
136+ esp_repl_post_executor_t post_executor ; /**!< Post-executor callback and context */
137+ esp_repl_on_stop_t on_stop ; /**!< Stop callback and context */
138+ esp_repl_on_exit_t on_exit ; /**!< Exit callback and context */
139+ } esp_repl_config_t ;
140+
141+ /**
142+ * @brief Handle to a REPL instance.
143+ */
144+ typedef struct esp_repl_instance esp_repl_instance_handle_t ;
145+
146+ /**
147+ * @brief Create a REPL instance.
148+ *
149+ * @param handle Pointer to store the created REPL instance handle.
150+ * @param config Pointer to the configuration structure.
151+ *
152+ * @return ESP_OK on success, error code otherwise.
153+ */
154+ esp_err_t esp_repl_create (esp_repl_instance_handle_t * handle , const esp_repl_config_t * config );
155+
156+ /**
157+ * @brief Destroy a REPL instance.
158+ *
159+ * @param handle REPL instance handle to destroy.
160+ *
161+ * @return ESP_OK on success, error code otherwise.
162+ */
163+ esp_err_t esp_repl_destroy (esp_repl_instance_handle_t handle );
164+
165+ /**
166+ * @brief Start a REPL instance.
167+ *
168+ * @param handle REPL instance handle.
169+ *
170+ * @return ESP_OK on success, error code otherwise.
171+ */
172+ esp_err_t esp_repl_start (esp_repl_instance_handle_t handle );
173+
174+ /**
175+ * @brief Stop a REPL instance.
176+ *
177+ * @param handle REPL instance handle.
178+ *
179+ * @return ESP_OK on success, error code otherwise.
180+ */
181+ esp_err_t esp_repl_stop (esp_repl_instance_handle_t handle );
182+
183+ /**
184+ * @brief Run the REPL loop.
185+ *
186+ * @param handle REPL instance handle.
187+ */
188+ void esp_repl (esp_repl_instance_handle_t handle );
16189
17190#ifdef __cplusplus
18191}
0 commit comments