Skip to content

Commit 43be15b

Browse files
committed
feat(esp_repl): Add esp_linenoise and esp_commands dependencies
1 parent 6553880 commit 43be15b

File tree

5 files changed

+41
-77
lines changed

5 files changed

+41
-77
lines changed

esp_repl/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ set(srcs "esp_repl.c")
44

55
idf_component_register(
66
SRCS ${srcs}
7-
INCLUDE_DIRS include
8-
PRIV_INCLUDE_DIRS private_include)
7+
INCLUDE_DIRS include)

esp_repl/esp_repl.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#include "freertos/semphr.h"
1010
#include "esp_repl.h"
1111
#include "esp_err.h"
12-
12+
#include "esp_commands.h"
13+
#include "esp_linenoise.h"
1314
typedef enum {
1415
ESP_REPL_STATE_RUNNING,
1516
ESP_REPL_STATE_STOPPED
@@ -34,8 +35,8 @@ typedef struct esp_repl_instance {
3435

3536
esp_err_t esp_repl_create(esp_repl_instance_handle_t *handle, const esp_repl_config_t *config)
3637
{
37-
if ((config->executor.func == NULL) ||
38-
(config->reader.func == NULL) ||
38+
if ((config->linenoise_handle == NULL) ||
39+
(config->commands_handle == NULL) ||
3940
(config->max_cmd_line_size == 0)) {
4041
return ESP_ERR_INVALID_ARG;
4142
}
@@ -140,11 +141,22 @@ void esp_repl(esp_repl_instance_handle_t handle)
140141
* function is called. */
141142
xSemaphoreTake(state->mux, portMAX_DELAY);
142143

144+
esp_linenoise_handle_t l_hdl = config->linenoise_handle;
145+
esp_command_set_handle_t c_set = config->command_set_handle;
146+
143147
/* REPL loop */
144148
while (state->state == ESP_REPL_STATE_RUNNING) {
145149

146150
/* try to read a command line */
147-
const esp_err_t read_ret = config->reader.func(config->reader.ctx, cmd_line, cmd_line_size);
151+
const esp_err_t read_ret = esp_linenoise_get_line(l_hdl, cmd_line, cmd_line_size);
152+
153+
/* Add the command to the history */
154+
esp_linenoise_history_add(l_hdl, cmd_line);
155+
156+
/* Save command history to filesystem */
157+
if (config->history_save_path) {
158+
esp_linenoise_history_save(l_hdl, config->history_save_path);
159+
}
148160

149161
/* forward the raw command line to the pre executor callback (e.g., save in history).
150162
* this callback is not necessary for the user to register, continue if it isn't */
@@ -159,7 +171,7 @@ void esp_repl(esp_repl_instance_handle_t handle)
159171

160172
/* try to run the command */
161173
int cmd_func_ret;
162-
const esp_err_t exec_ret = config->executor.func(config->executor.ctx, cmd_line, &cmd_func_ret);
174+
const esp_err_t exec_ret = esp_commands_execute(c_set, cmd_line, &cmd_func_ret);
163175

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

esp_repl/idf_component.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ version: "1.0.0"
22
description: "esp_repl - Read Eval Print Loop component"
33
url: https://github.com/espressif/idf-extra-components/tree/master/esp_repl
44
dependencies:
5-
idf: ">=6.0"
5+
SoucheSouche/test_esp_linenoise:
6+
version: "*"
7+
registry_url: https://components-staging.espressif.com
8+
SoucheSouche/test_esp_commands:
9+
version: "*"
10+
registry_url: https://components-staging.espressif.com
611
sbom:
712
manifests:
813
- path: sbom_esp_repl.yml

esp_repl/include/esp_repl.h

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,13 @@ extern "C" {
1111

1212
#include <stdbool.h>
1313
#include "esp_err.h"
14+
#include "esp_linenoise.h"
1415

1516
/**
1617
* @brief Handle to a REPL instance.
1718
*/
1819
typedef struct esp_repl_instance *esp_repl_instance_handle_t;
1920

20-
/**
21-
* @brief Function prototype for reading input for the REPL.
22-
*
23-
* @param ctx User-defined context pointer.
24-
* @param buf Buffer to store the read data.
25-
* @param buf_size Size of the buffer in bytes.
26-
*
27-
* @return ESP_OK on success, error code otherwise.
28-
*/
29-
typedef esp_err_t (*esp_repl_reader_fn)(void *ctx, char *buf, size_t buf_size);
30-
31-
/**
32-
* @brief Reader configuration structure for the REPL.
33-
*/
34-
typedef struct esp_repl_reader {
35-
esp_repl_reader_fn func; /**!< Function to read input */
36-
void *ctx; /**!< Context passed to the reader function */
37-
} esp_repl_reader_t;
38-
3921
/**
4022
* @brief Function prototype called before executing a command.
4123
*
@@ -55,25 +37,6 @@ typedef struct esp_repl_pre_executor {
5537
void *ctx; /**!< Context passed to the pre-executor function */
5638
} esp_repl_pre_executor_t;
5739

58-
/**
59-
* @brief Function prototype to execute a REPL command.
60-
*
61-
* @param ctx User-defined context pointer.
62-
* @param buf Null-terminated command string.
63-
* @param ret_val Pointer to store the command return value.
64-
*
65-
* @return ESP_OK on success, error code otherwise.
66-
*/
67-
typedef esp_err_t (*esp_repl_executor_fn)(void *ctx, const char *buf, int *ret_val);
68-
69-
/**
70-
* @brief Executor configuration structure for the REPL.
71-
*/
72-
typedef struct esp_repl_executor {
73-
esp_repl_executor_fn func; /**!< Function to execute commands */
74-
void *ctx; /**!< Context passed to the executor function */
75-
} esp_repl_executor_t;
76-
7740
/**
7841
* @brief Function prototype called after executing a command.
7942
*
@@ -133,13 +96,14 @@ typedef struct esp_repl_on_exit {
13396
* @brief Configuration structure to initialize a REPL instance.
13497
*/
13598
typedef struct esp_repl_config {
136-
size_t max_cmd_line_size; /**!< Maximum allowed command line size */
137-
esp_repl_reader_t reader; /**!< Reader callback and context */
138-
esp_repl_pre_executor_t pre_executor; /**!< Pre-executor callback and context */
139-
esp_repl_executor_t executor; /**!< Executor callback and context */
140-
esp_repl_post_executor_t post_executor; /**!< Post-executor callback and context */
141-
esp_repl_on_stop_t on_stop; /**!< Stop callback and context */
142-
esp_repl_on_exit_t on_exit; /**!< Exit callback and context */
99+
esp_linenoise_handle_t linenoise_handle; /**!< Handle to the esp_linenoise instance */
100+
esp_commands_handle_t command_set_handle; /**!< Handle to a set of commands */
101+
size_t max_cmd_line_size; /**!< Maximum allowed command line size */
102+
const char *history_save_path; /**!< Path to file to save the history */
103+
esp_repl_pre_executor_t pre_executor; /**!< Pre-executor callback and context */
104+
esp_repl_post_executor_t post_executor; /**!< Post-executor callback and context */
105+
esp_repl_on_stop_t on_stop; /**!< Stop callback and context */
106+
esp_repl_on_exit_t on_exit; /**!< Exit callback and context */
143107
} esp_repl_config_t;
144108

145109
/**

esp_repl/test_apps/main/test_esp_repl.c

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,14 @@
1111
#include "freertos/semphr.h"
1212
#include "unity.h"
1313
#include "esp_repl.h"
14-
15-
typedef struct esp_linenoise_dummy {
16-
size_t value;
17-
} esp_linenoise_dummy_t;
18-
typedef struct esp_linenoise_dummy *esp_linenoise_handle_t;
19-
20-
typedef struct esp_commands_dummy {
21-
size_t value;
22-
} esp_commands_dummy_t;
23-
typedef struct esp_commands_dummy *esp_commands_handle_t;
24-
25-
esp_err_t test_reader_non_blocking(esp_linenoise_handle_t handle, char *buf, size_t buf_size)
26-
{
27-
return ESP_OK;
28-
}
14+
#include "esp_linenoise.h"
15+
#include "esp_commands.h"
2916

3017
esp_err_t test_pre_executor(void *ctx, char *buf, const esp_err_t reader_ret_val)
3118
{
3219
return ESP_OK;
3320
}
3421

35-
esp_err_t test_executor(esp_commands_handle_t handle, const char *buf, int *ret_val)
36-
{
37-
return ESP_OK;
38-
}
39-
4022
esp_err_t test_post_executor(void *ctx, const char *buf, const esp_err_t executor_ret_val, const int cmd_ret_val)
4123
{
4224
return ESP_OK;
@@ -54,13 +36,15 @@ void test_on_exit(void *ctx, esp_repl_instance_handle_t handle)
5436

5537
TEST_CASE("esp_repl() called after successful init, with non blocking reader", "[esp_repl]")
5638
{
57-
esp_commands_dummy_t dummy_esp_linenoise = {.value = 0x01 };
58-
esp_commands_dummy_t dummy_esp_commands = {.value = 0x02 };
39+
esp_linenoise_config_t config = esp_linenoise_get_instance_config_default();
40+
esp_linenoise_handle_t esp_linenoise_hdl = esp_linenoise_create_instance(&config);
41+
42+
esp_command_set_handle_t cmd_set_hdl = esp_commands_
43+
44+
esp_commands_dummy_t dummy_esp_commands = {.value = 0x02 };
5945
esp_repl_config_t config = {
6046
.max_cmd_line_size = 256,
61-
.reader = { .func = (esp_repl_reader_fn)test_reader_non_blocking, .ctx = &dummy_esp_linenoise },
6247
.pre_executor = { .func = test_pre_executor, .ctx = NULL },
63-
.executor = { .func = (esp_repl_executor_fn)test_executor, .ctx = &dummy_esp_commands },
6448
.post_executor = { .func = test_post_executor, .ctx = NULL },
6549
.on_stop = { .func = test_on_stop, .ctx = NULL },
6650
.on_exit = { .func = test_on_exit, .ctx = NULL }

0 commit comments

Comments
 (0)