Skip to content

Commit 7866fd6

Browse files
committed
feat(esp_repl): Add option for esp_repl to register quit cmd
1 parent a1eae93 commit 7866fd6

File tree

4 files changed

+58
-25
lines changed

4 files changed

+58
-25
lines changed

esp_repl/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
menu "esp_repl configuration"
2+
3+
config ESP_REPL_HAS_QUIT_CMD
4+
bool "Register quit command"
5+
default n
6+
help
7+
Register a static command "quit" that allows the user to return from the esp_repl main loop.
8+
The command is registered through the ESP_COMMAND_REGISTER macro provided by esp_commands component
9+
and is placed in the dedicated flash section.
10+
endmenu

esp_repl/esp_repl.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,31 @@ typedef struct esp_repl_instance {
2929
esp_repl_state_t state;
3030
} esp_repl_instance_t;
3131

32+
#if CONFIG_ESP_REPL_HAS_QUIT_CMD
33+
#define _ESP_REPL_STRINGIFY(x) #x
34+
#define ESP_REPL_GET_STR(x) _ESP_REPL_STRINGIFY(x)
35+
36+
#define ESP_REPL_QUIT_CMD quit
37+
#define ESP_REPL_QUIT_CMD_STR ESP_REPL_GET_STR(ESP_REPL_QUIT_CMD)
38+
#define ESP_REPL_QUIT_CMD_SIZE strlen(ESP_REPL_QUIT_CMD_STR)
39+
40+
/* dummy command function callback to allow the registration for the quit command
41+
* to succeed */
42+
static int esp_repl_quit_cmd(void *context, const int fd_out, int argc, char **argv)
43+
{
44+
return 0;
45+
}
46+
47+
ESP_COMMAND_REGISTER(ESP_REPL_QUIT_CMD,
48+
"esp_repl",
49+
"This command will trigger the exit mechanism to exit from esp_repl() main loop",
50+
esp_repl_quit_cmd,
51+
NULL,
52+
NULL,
53+
NULL
54+
);
55+
#endif // CONFIG_ESP_REPL_HAS_QUIT_CMD
56+
3257
#define ESP_REPL_CHECK_INSTANCE(handle) do { \
3358
if(handle == NULL) { \
3459
return ESP_ERR_INVALID_ARG; \
@@ -204,9 +229,26 @@ void esp_repl(esp_repl_handle_t handle)
204229
continue;
205230
}
206231

232+
#if CONFIG_ESP_REPL_HAS_QUIT_CMD
233+
/* evaluate the command name. make sure that the first argument of the cmd_line
234+
* is ESP_REPL_QUIT_CMD_STR and the character following that is either a space
235+
* or a null character */
236+
if ((strncmp(ESP_REPL_QUIT_CMD_STR, cmd_line, ESP_REPL_QUIT_CMD_SIZE) == 0) &&
237+
((cmd_line[ESP_REPL_QUIT_CMD_SIZE] == ' ') || (cmd_line[ESP_REPL_QUIT_CMD_SIZE] == '\0'))) {
238+
/* quit command received, call esp_repl_stop() */
239+
if (esp_repl_stop(handle) == ESP_OK) {
240+
/* if esp_repl_stop() was successful, retry the while condition.
241+
* the esp_repl state should have been changed which will force
242+
* the while to break */
243+
continue;
244+
}
245+
}
246+
#endif // CONFIG_ESP_REPL_HAS_QUIT_CMD
247+
207248
/* try to run the command */
208249
int cmd_func_ret;
209-
const esp_err_t exec_ret = esp_commands_execute(c_set, -1, cmd_line, &cmd_func_ret);
250+
const int out_fd = esp_linenoise_get_out_fd(handle->config.linenoise_handle);
251+
const esp_err_t exec_ret = esp_commands_execute(c_set, out_fd, cmd_line, &cmd_func_ret);
210252

211253
/* forward the raw command line to the post executor callback (e.g., save in history).
212254
* this callback is not necessary for the user to register, continue if it isn't */

esp_repl/test_apps/main/test_esp_repl.c

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ static void test_send_characters(int socket_fd, const char *msg)
7171
TEST_ASSERT_EQUAL(msg_len, nwrite);
7272
}
7373

74-
esp_err_t test_pre_executor(void *ctx, char *buf, const esp_err_t reader_ret_val)
74+
esp_err_t test_pre_executor(void *ctx, const char *buf, esp_err_t reader_ret_val)
7575
{
7676
s_pre_executor_nb_of_calls++;
7777
return ESP_OK;
7878
}
7979

80-
esp_err_t test_post_executor(void *ctx, const char *buf, const esp_err_t executor_ret_val, const int cmd_ret_val)
80+
esp_err_t test_post_executor(void *ctx, const char *buf, esp_err_t executor_ret_val, int cmd_ret_val)
8181
{
8282
s_post_executor_nb_of_calls++;
8383
return ESP_OK;
@@ -222,14 +222,6 @@ TEST_CASE("esp_repl() loop calls all callbacks and exit on call to esp_repl_stop
222222
test_socket_teardown(s_socket_fd);
223223
}
224224

225-
static int quit_command(void *context, const int fd_out, int argc, char **argv)
226-
{
227-
esp_repl_handle_t repl_handle = (esp_repl_handle_t)context;
228-
TEST_ASSERT_EQUAL(ESP_OK, esp_repl_stop(repl_handle));
229-
230-
return 0;
231-
}
232-
233225
TEST_CASE("esp_repl() exits when esp_repl_stop() called from the task running esp_repl()", "[esp_repl]")
234226
{
235227
/* create semaphores */
@@ -274,18 +266,6 @@ TEST_CASE("esp_repl() exits when esp_repl_stop() called from the task running es
274266
BaseType_t rc = xTaskCreate(repl_task, "repl_task", 4096, &args, 5, NULL);
275267
TEST_ASSERT_EQUAL(pdPASS, rc);
276268

277-
/* register a quit command to esp_commands */
278-
esp_command_t quit_cmd = {
279-
.name = "quit",
280-
.group = "quit",
281-
.help = "-",
282-
.func = quit_command,
283-
.func_ctx = repl_handle,
284-
.hint_cb = NULL,
285-
.glossary_cb = NULL
286-
};
287-
TEST_ASSERT_EQUAL(ESP_OK, esp_commands_register_cmd(&quit_cmd));
288-
289269
/* start repl */
290270
TEST_ASSERT_EQUAL(ESP_OK, esp_repl_start(repl_handle));
291271

@@ -295,7 +275,7 @@ TEST_CASE("esp_repl() exits when esp_repl_stop() called from the task running es
295275
TEST_ASSERT_TRUE(xSemaphoreTake(start_sem, pdMS_TO_TICKS(2000)));
296276

297277
/* send the quit command */
298-
const char *quit_cmd_line = "quit\n";
278+
const char *quit_cmd_line = "quit \n";
299279
test_send_characters(s_socket_fd[1], quit_cmd_line);
300280

301281
/* wait for the repl task to signal completion */
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
CONFIG_ESP_TASK_WDT_EN=n
1+
CONFIG_ESP_TASK_WDT_EN=n
2+
CONFIG_ESP_REPL_HAS_QUIT_CMD=y

0 commit comments

Comments
 (0)