Skip to content

Commit ec78c33

Browse files
Merge pull request espressif#402 from espressif-abhikroy/components/console_simple_init_plugin
feat(console): Added runtime component registration support in console_simple_init
2 parents 0584da5 + 057873c commit ec78c33

File tree

7 files changed

+98
-16
lines changed

7 files changed

+98
-16
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
idf_component_register(SRCS "console_simple_init.c"
22
INCLUDE_DIRS "."
3-
PRIV_REQUIRES console)
3+
PRIV_REQUIRES console
4+
LDFRAGMENTS linker.lf)

components/console_simple_init/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,28 @@ It also provides an api to register an user provided command.
3232
// This allows you to execute the do_user_cmd function when the "user" command is invoked
3333
ESP_ERROR_CHECK(console_cmd_user_register("user", do_user_cmd));
3434

35+
// Register any other plugin command added to your project
36+
ESP_ERROR_CHECK(console_cmd_all_register());
3537

3638
ESP_ERROR_CHECK(console_cmd_start()); // Start console
3739
```
40+
41+
### Automatic registration of console commands
42+
The `console_simple_init` component includes a utility function named `console_cmd_all_register()`. This function automates the registration of all commands that are linked into the application. To use this functionality, the application can call `console_cmd_all_register()` as demonstrated above.
43+
44+
When creating a new component, you can ensure that its commands are registered automatically by placing the registration function into the `.console_cmd_desc` section within the output binary.
45+
46+
To achieve this, follow these steps:
47+
1. Add the following lines to the main file of the component
48+
```
49+
static const console_cmd_plugin_desc_t __attribute__((section(".console_cmd_desc"), used)) PLUGIN = {
50+
.name = "cmd_name_string",
51+
.plugin_regd_fn = &cmd_registration_function
52+
};̌
53+
```
54+
2. Add the `WHOLE_ARCHIVE` flag to CMakeLists.txt of the component.
55+
56+
57+
For more details refer:
58+
* [IDF Component Manager](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-component-manager.html)
59+
* [Linker Script Generation](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/linker-script-generation.html)

components/console_simple_init/console_simple_init.c

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
#include "esp_log.h"
99
#include "console_simple_init.h"
1010

11+
1112
static esp_console_repl_t *repl = NULL;
1213
static const char *TAG = "console_simple_init";
1314

1415
/**
1516
* @brief Initializes the esp console
16-
* @return
17-
* - esp_err_t
17+
* @return ESP_OK on success
1818
*/
1919
esp_err_t console_cmd_init(void)
2020
{
@@ -40,13 +40,12 @@ esp_err_t console_cmd_init(void)
4040
}
4141

4242
/**
43-
* @brief Initialize Ethernet driver based on Espressif IoT Development Framework Configuration
43+
* @brief Register a user supplied command
4444
*
4545
* @param[in] cmd string that is the user defined command
4646
* @param[in] do_user_cmd Function pointer for a user-defined command callback function
4747
*
48-
* @return
49-
* - esp_err_t
48+
* @return ESP_OK on success
5049
*/
5150
esp_err_t console_cmd_user_register(char *cmd, esp_console_cmd_func_t do_user_cmd)
5251
{
@@ -67,10 +66,36 @@ esp_err_t console_cmd_user_register(char *cmd, esp_console_cmd_func_t do_user_cm
6766
return ret;
6867
}
6968

69+
70+
/**
71+
* @brief Register all the console commands in .console_cmd_desc section
72+
*
73+
* @return ESP_OK on success
74+
*/
75+
esp_err_t console_cmd_all_register(void)
76+
{
77+
esp_err_t ret = ESP_FAIL;
78+
extern const console_cmd_plugin_desc_t _console_cmd_array_start;
79+
extern const console_cmd_plugin_desc_t _console_cmd_array_end;
80+
81+
ESP_LOGI(TAG, "List of Console commands:\n");
82+
for (const console_cmd_plugin_desc_t *it = &_console_cmd_array_start; it != &_console_cmd_array_end; ++it) {
83+
ESP_LOGI(TAG, "- Command '%s', function plugin_regd_fn=%p\n", it->name, it->plugin_regd_fn);
84+
if (it->plugin_regd_fn != NULL) {
85+
ret = (it->plugin_regd_fn)();
86+
if (ret != ESP_OK) {
87+
return ret;
88+
}
89+
}
90+
}
91+
92+
return ESP_OK;
93+
}
94+
95+
7096
/**
7197
* @brief Starts the esp console
72-
* @return
73-
* - esp_err_t
98+
* @return ESP_OK on success
7499
*/
75100
esp_err_t console_cmd_start(void)
76101
{

components/console_simple_init/console_simple_init.h

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,46 @@
1313
extern "C" {
1414
#endif
1515

16+
17+
/* This stucture describes the plugin to the rest of the application */
18+
typedef struct {
19+
/* A pointer to the name of the command */
20+
const char *name;
21+
22+
/* A function which performs auto-registration of console commands */
23+
esp_err_t (*plugin_regd_fn)(void);
24+
} console_cmd_plugin_desc_t;
25+
26+
1627
/**
1728
* @brief Initializes the esp console
18-
* @return
19-
* - esp_err_t
29+
* @return ESP_OK on success
2030
*/
2131
esp_err_t console_cmd_init(void);
2232

33+
2334
/**
24-
* @brief Initialize Ethernet driver based on Espressif IoT Development Framework Configuration
35+
* @brief Register a user supplied command
2536
*
2637
* @param[in] cmd string that is the user defined command
2738
* @param[in] do_user_cmd Function pointer for a user-defined command callback function
2839
*
29-
* @return
30-
* - esp_err_t
40+
* @return ESP_OK on success
3141
*/
3242
esp_err_t console_cmd_user_register(char *user_cmd, esp_console_cmd_func_t do_user_cmd);
3343

44+
45+
/**
46+
* @brief Register all the console commands in .console_cmd_desc section
47+
*
48+
* @return ESP_OK on success
49+
*/
50+
esp_err_t console_cmd_all_register(void);
51+
52+
3453
/**
3554
* @brief Starts the esp console
36-
* @return
37-
* - esp_err_t
55+
* @return ESP_OK on success
3856
*/
3957
esp_err_t console_cmd_start(void);
4058

components/console_simple_init/examples/console_basic/main/console_basic.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ void app_main(void)
3030
// Register user command
3131
ESP_ERROR_CHECK(console_cmd_user_register("user", do_user_cmd));
3232

33+
// Register all the plugin commands added to this example
34+
ESP_ERROR_CHECK(console_cmd_all_register());
35+
3336
// start console REPL
3437
ESP_ERROR_CHECK(console_cmd_start());
3538

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
dependencies:
22
idf:
3-
version: '*'
3+
version: ">=5.0"
44
console_simple_init:
55
version: "*"
66
override_path: '../../../'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[sections:console_cmd_desc]
2+
entries:
3+
.console_cmd_desc
4+
5+
[scheme:console_cmd_desc_default]
6+
entries:
7+
console_cmd_desc -> flash_rodata
8+
9+
[mapping:console_cmd_desc]
10+
archive: *
11+
entries:
12+
* (console_cmd_desc_default);
13+
console_cmd_desc -> flash_rodata KEEP() SORT(name) SURROUND(console_cmd_array)

0 commit comments

Comments
 (0)