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
2527static 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
0 commit comments