Skip to content

Commit 0d6dbd1

Browse files
committed
Merge branch 'refactor_esp_matter_console' into 'main'
console: refactor esp_matter_console See merge request app-frameworks/esp-matter!170
2 parents ec70d32 + 47a93e6 commit 0d6dbd1

File tree

12 files changed

+377
-245
lines changed

12 files changed

+377
-245
lines changed

components/esp_matter/esp_matter_attribute_utils.cpp

Lines changed: 182 additions & 155 deletions
Large diffs are not rendered by default.
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
idf_component_register(SRCS esp_matter_console.cpp esp_matter_console_diagnostics.cpp
1+
set(srcs_list )
2+
if (CONFIG_ENABLE_CHIP_SHELL)
3+
list(APPEND srcs_list esp_matter_console.cpp esp_matter_console_diagnostics.cpp)
4+
endif()
5+
idf_component_register(SRCS ${srcs_list}
26
INCLUDE_DIRS .
3-
PRIV_REQUIRES chip esp32_mbedtls esp_timer)
7+
PRIV_REQUIRES chip esp32_mbedtls esp_timer bt openthread)

components/esp_matter_console/esp_matter_console.cpp

Lines changed: 67 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,80 +19,100 @@
1919

2020
#include <esp_matter_console.h>
2121
#include <lib/shell/Engine.h>
22+
#include <src/platform/ESP32/ESP32Utils.h>
2223

23-
#define MAX_CONSOLE_COMMANDS CONFIG_ESP_MATTER_CONSOLE_MAX_COMMANDS
24+
namespace esp_matter {
25+
namespace console {
2426

2527
static const char *TAG = "esp_matter_console";
26-
static esp_matter_console_command_t commands[MAX_CONSOLE_COMMANDS];
27-
static int total_added_commands = 0;
28+
static engine base_engine;
2829

29-
esp_err_t esp_matter_console_add_command(esp_matter_console_command_t *command)
30+
void engine::for_each_command(command_iterator_t *on_command, void *arg)
3031
{
31-
if (total_added_commands + 1 > MAX_CONSOLE_COMMANDS) {
32-
ESP_LOGE(TAG, "Could not add command. Increase the max limit to add more.");
32+
for (unsigned i = 0; i < _command_set_count; ++i) {
33+
for (unsigned j = 0; j < _command_set_size[i]; ++j) {
34+
if (on_command(&_command_set[i][j], arg) != ESP_OK) {
35+
return;
36+
}
37+
}
38+
}
39+
}
40+
41+
esp_err_t engine::exec_command(int argc, char *argv[])
42+
{
43+
esp_err_t err = ESP_ERR_INVALID_ARG;
44+
if (argc <= 0) {
45+
return err;
46+
}
47+
// find the command from the command set
48+
for (unsigned i = 0; i < _command_set_count; ++i) {
49+
for (unsigned j = 0; j < _command_set_size[i]; ++j) {
50+
if (strcmp(argv[0], _command_set[i][j].name) == 0 && _command_set[i][j].handler) {
51+
err = _command_set[i][j].handler(argc - 1, &argv[1]);
52+
break;
53+
}
54+
}
55+
}
56+
return err;
57+
}
58+
59+
esp_err_t engine::register_commands(const command_t *command_set, unsigned count)
60+
{
61+
if (_command_set_count >= CONSOLE_MAX_COMMAND_SETS) {
62+
ESP_LOGE(TAG, "Max number of command sets reached");
3363
return ESP_FAIL;
3464
}
35-
/* Since the strings in esp_matter_console_command_t are constants, this will work */
36-
commands[total_added_commands] = *command;
37-
total_added_commands++;
65+
66+
_command_set[_command_set_count] = command_set;
67+
_command_set_size[_command_set_count] = count;
68+
++_command_set_count;
3869
return ESP_OK;
3970
}
4071

41-
static void esp_matter_console_print_help()
72+
esp_err_t add_commands(const command_t *command_set, unsigned count)
4273
{
43-
ESP_LOGI(TAG, "Usage: matter esp <sub_command>");
44-
ESP_LOGI(TAG, "Sub commands:");
45-
for (int i = 0; i < total_added_commands; i++) {
46-
ESP_LOGI(TAG, "\t%s: %s", commands[i].name, commands[i].description);
47-
}
74+
return base_engine.register_commands(command_set, count);
75+
}
76+
77+
esp_err_t print_description(const command_t *command, void *arg)
78+
{
79+
ESP_LOGI(TAG, "\t%s: %s", command->name, command->description);
80+
return ESP_OK;
4881
}
4982

50-
static esp_err_t esp_matter_console_help_handler(int argc, char **argv)
83+
84+
static esp_err_t help_handler(int argc, char **argv)
5185
{
52-
esp_matter_console_print_help();
86+
base_engine.for_each_command(print_description, NULL);
5387
return ESP_OK;
5488
}
5589

56-
static esp_err_t esp_matter_console_register_default_commands()
90+
static esp_err_t register_default_commands()
5791
{
58-
esp_matter_console_command_t command = {
92+
static const command_t command= {
5993
.name = "help",
6094
.description = "Print help",
61-
.handler = esp_matter_console_help_handler,
95+
.handler = help_handler,
6296
};
63-
return esp_matter_console_add_command(&command);
97+
return add_commands(&command, 1);
6498
}
6599

66-
static CHIP_ERROR esp_matter_console_common_handler(int argc, char **argv)
100+
static CHIP_ERROR common_handler(int argc, char **argv)
67101
{
68102
/* This common handler is added to avoid adding `CHIP_ERROR` and its component requirements in other esp-matter
69103
* components */
70104
if (argc <= 0) {
71-
esp_matter_console_print_help();
105+
help_handler(argc, argv);
72106
return CHIP_NO_ERROR;
73107
}
74-
for (int i = 0; i < total_added_commands; i++) {
75-
if (strncmp(argv[0], commands[i].name, strlen(commands[i].name) + 1) == 0) {
76-
if (commands[i].handler == NULL) {
77-
ESP_LOGW(TAG, "No handler set for the command: %s", argv[0]);
78-
return CHIP_NO_ERROR;
79-
}
80-
if (commands[i].handler(argc - 1, &argv[1]) == ESP_OK) { /* Removing the first argument from argv */
81-
return CHIP_NO_ERROR;
82-
}
83-
/* The command handler returned error */
84-
return CHIP_ERROR_INVALID_ARGUMENT;
85-
}
86-
}
87-
ESP_LOGE(TAG, "Could not find the command: %s. Try the help command for more details: matter esp help", argv[0]);
88-
return CHIP_ERROR_INVALID_ARGUMENT;
108+
return chip::DeviceLayer::Internal::ESP32Utils::MapError(base_engine.exec_command(argc, argv));
89109
}
90110

91-
static esp_err_t esp_matter_console_register_common_shell_handler()
111+
static esp_err_t register_common_shell_handler()
92112
{
93-
static chip::Shell::shell_command_t cmds[] = {
113+
static const chip::Shell::shell_command_t cmds[] = {
94114
{
95-
.cmd_func = esp_matter_console_common_handler,
115+
.cmd_func = common_handler,
96116
.cmd_name = "esp",
97117
.cmd_help = "Usage: matter esp <sub_command>",
98118
},
@@ -107,14 +127,14 @@ static void ChipShellTask(void *args)
107127
chip::Shell::Engine::Root().RunMainLoop();
108128
}
109129

110-
esp_err_t esp_matter_console_init()
130+
esp_err_t init()
111131
{
112132
esp_err_t err = ESP_OK;
113-
err = esp_matter_console_register_default_commands();
133+
err = register_default_commands();
114134
if (err != ESP_OK) {
115135
ESP_LOGE(TAG, "Couldn't register default console commands");
116136
}
117-
err = esp_matter_console_register_common_shell_handler();
137+
err = register_common_shell_handler();
118138
if (err != ESP_OK) {
119139
ESP_LOGE(TAG, "Couldn't register common handler");
120140
return err;
@@ -126,3 +146,6 @@ esp_err_t esp_matter_console_init()
126146
}
127147
return err;
128148
}
149+
150+
} // namespace console
151+
} // namespace esp_matter

components/esp_matter_console/esp_matter_console.h

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
#pragma once
1616

1717
#include <esp_err.h>
18+
#include <string.h>
1819

19-
#ifdef __cplusplus
20-
extern "C" {
21-
#endif
20+
#define CONSOLE_MAX_COMMAND_SETS CONFIG_ESP_MATTER_CONSOLE_MAX_COMMANDS
21+
22+
namespace esp_matter {
23+
namespace console {
2224

2325
/** Callback for console commands
2426
*
@@ -27,7 +29,7 @@ extern "C" {
2729
* @return ESP_OK on success.
2830
* @return error in case of failure.
2931
*/
30-
typedef esp_err_t (*esp_matter_console_handler_t)(int argc, char **argv);
32+
typedef esp_err_t (*command_handler_t)(int argc, char **argv);
3133

3234
/** ESP Matter Console Command */
3335
typedef struct {
@@ -36,8 +38,64 @@ typedef struct {
3638
/** Command Description/Help */
3739
const char *description;
3840
/** Command Handler */
39-
esp_matter_console_handler_t handler;
40-
} esp_matter_console_command_t;
41+
command_handler_t handler;
42+
} command_t;
43+
44+
/** Command iterator callback for the console commands
45+
*
46+
* @param command The console command being iterated.
47+
* @param arg A context variable passed to the iterator function.
48+
*
49+
* @return ESP_OK to continue iteration; anything else to break iteration.
50+
*/
51+
typedef esp_err_t command_iterator_t(const command_t *command, void *arg);
52+
53+
class engine
54+
{
55+
protected:
56+
const command_t *_command_set[CONSOLE_MAX_COMMAND_SETS];
57+
unsigned _command_set_size[CONSOLE_MAX_COMMAND_SETS];
58+
unsigned _command_set_count;
59+
public:
60+
engine(): _command_set_count(0) {}
61+
62+
/** Execution callback for a console command.
63+
*
64+
* @param[in] on_command An iterator callback to be called for each command.
65+
* @param[in] arg A context variable to be passed to each command iterated.
66+
*/
67+
void for_each_command(command_iterator_t *on_command, void *arg);
68+
69+
/** Dispatch and execute the command for the given argument list.
70+
*
71+
* @param[in] argc Number of arguments in argv.
72+
* @param[in] argv Array of arguments in the tokenized command line to execute.
73+
*
74+
* @return ESP_OK on success
75+
* @return error in case of failure.
76+
*/
77+
esp_err_t exec_command(int argc, char *argv[]);
78+
79+
/** Registers a command set, or array of commands with the console.
80+
*
81+
* @param command_set[in] An array of commands to add to the console.
82+
* @param count[in] The number of commands in the command set array.
83+
*
84+
* @return ESP_OK on success
85+
* @return error in case of failure.
86+
*/
87+
esp_err_t register_commands(const command_t *command_set, unsigned count);
88+
};
89+
90+
/** Print the description of a command
91+
*
92+
* @param command[in] The command which's description will be printed.
93+
* @param arg[in] A context variable passed to the iterator function.
94+
*
95+
* @return ESP_OK on success
96+
* @return error in case of failure.
97+
*/
98+
esp_err_t print_description(const command_t *command, void *arg);
4199

42100
/** Initialize Console
43101
*
@@ -46,19 +104,20 @@ typedef struct {
46104
* @return ESP_OK on success.
47105
* @return error in case of failure.
48106
*/
49-
esp_err_t esp_matter_console_init(void);
107+
esp_err_t init(void);
50108

51-
/** Add Console Command
109+
/** Add Console Command Set
52110
*
53111
* Add a new console command.
54112
* This can be done before calling `esp_matter_console_init()` but the commands will not work until initialized.
55113
*
56-
* @param[in] command Pointer to command struct
114+
* @param[in] command_set Command struct set array pointer
115+
* @param[in] count Command struct set array size
57116
*
58117
* @return ESP_OK on success.
59118
* @return error in case of failure.
60119
*/
61-
esp_err_t esp_matter_console_add_command(esp_matter_console_command_t *command);
120+
esp_err_t add_commands(const command_t *command_set, unsigned count);
62121

63122
/** Add Diagnostics Commands
64123
*
@@ -67,8 +126,7 @@ esp_err_t esp_matter_console_add_command(esp_matter_console_command_t *command);
67126
* @return ESP_OK on success.
68127
* @return error in case of failure.
69128
*/
70-
esp_err_t esp_matter_console_diagnostics_register_commands();
129+
esp_err_t diagnostics_register_commands();
71130

72-
#ifdef __cplusplus
73-
}
74-
#endif
131+
} // namespace console
132+
} // namespace esp_matter

components/esp_matter_console/esp_matter_console_diagnostics.cpp

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
#include <esp_timer.h>
1919
#include <string.h>
2020

21+
namespace esp_matter {
22+
namespace console {
23+
2124
static const char *TAG = "esp_matter_console_diagnostics";
25+
static engine diagnostics_console;
2226

2327
static esp_err_t mem_dump_console_handler(int argc, char *argv[])
2428
{
@@ -39,26 +43,38 @@ static esp_err_t up_time_console_handler(int argc, char *argv[])
3943
return ESP_OK;
4044
}
4145

42-
static esp_err_t esp_matter_console_diagnostics_handler(int argc, char **argv)
46+
static esp_err_t diagnostics_dispatch(int argc, char **argv)
4347
{
44-
if (argc == 1 && strncmp(argv[0], "mem-dump", sizeof("mem-dump")) == 0) {
45-
return mem_dump_console_handler(argc, argv);
46-
} else if (argc == 1 && strncmp(argv[0], "up-time", sizeof("up-time")) == 0) {
47-
return up_time_console_handler(argc, argv);
48-
} else {
49-
ESP_LOGE(TAG, "Incorrect arguments");
50-
return ESP_FAIL;
48+
if (argc <= 0) {
49+
diagnostics_console.for_each_command(print_description, NULL);
50+
return ESP_OK;
5151
}
52-
return ESP_OK;
52+
return diagnostics_console.exec_command(argc, argv);
5353
}
5454

55-
esp_err_t esp_matter_console_diagnostics_register_commands()
55+
esp_err_t diagnostics_register_commands()
5656
{
57-
esp_matter_console_command_t command = {
57+
static const command_t command = {
5858
.name = "diagnostics",
59-
.description = "Diagnostic commands. Usage matter esp diagnostics <diagnostic_command>. Diagnostics commands: "
60-
"mem-dump, up-time",
61-
.handler = esp_matter_console_diagnostics_handler,
59+
.description = "Diagnostic commands. Usage matter esp diagnostics <diagnostic_command>.",
60+
.handler = diagnostics_dispatch,
6261
};
63-
return esp_matter_console_add_command(&command);
62+
63+
static const command_t diagnostics_commands[] = {
64+
{
65+
.name = "mem-dump",
66+
.description = "help for memory analysis",
67+
.handler = mem_dump_console_handler,
68+
},
69+
{
70+
.name = "up-time",
71+
.description = "print the uptime of the device",
72+
.handler = up_time_console_handler,
73+
},
74+
};
75+
diagnostics_console.register_commands(diagnostics_commands, sizeof(diagnostics_commands)/sizeof(command_t));
76+
77+
return add_commands(&command, 1);
6478
}
79+
} // namespace console
80+
} // namespace esp_matter

examples/blemesh_bridge/main/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
set(PRIV_REQUIRES_LIST device esp_matter esp_matter_console route_hook app_bridge)
1+
set(PRIV_REQUIRES_LIST device esp_matter route_hook app_bridge)
22

33
idf_component_register(SRC_DIRS "."
44
PRIV_INCLUDE_DIRS "."

examples/blemesh_bridge/main/app_main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ extern "C" void app_main()
103103
}
104104

105105
#if CONFIG_ENABLE_CHIP_SHELL
106-
esp_matter_console_diagnostics_register_commands();
107-
esp_matter_console_init();
106+
esp_matter::console::diagnostics_register_commands();
107+
esp_matter::console::init();
108108
#endif
109109
}

examples/light/main/app_main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ extern "C" void app_main()
131131
app_driver_light_set_defaults(light_endpoint_id);
132132

133133
#if CONFIG_ENABLE_CHIP_SHELL
134-
esp_matter_console_diagnostics_register_commands();
135-
esp_matter_console_init();
134+
esp_matter::console::diagnostics_register_commands();
135+
esp_matter::console::init();
136136
#endif
137137
}

0 commit comments

Comments
 (0)