diff --git a/Kconfig b/Kconfig index 4804e26..fc3a627 100644 --- a/Kconfig +++ b/Kconfig @@ -166,6 +166,29 @@ menu "WebGUIApp" help Timezone in hours. Positive for East and negative for the West endmenu + + menu "Command Processor settings" + config WEBGUIAPP_MAX_OBJECTS_NUM + int "Max OBJECTS number" + range 4 32 + default 8 + help + Max number of OBJECTS for both system and application + + config WEBGUIAPP_MAX_COMMANDS_NUM + int "Max COMMANDS number" + range 4 32 + default 8 + help + Max number of COMMANDS per OBJECT for both system and application + + config WEBGUIAPP_MAX_COMMAND_STRING_LENGTH + int "Max COMMAND string length" + range 32 128 + default 64 + help + Max length of COMMAND string total, 1/4 for object name, 1/4 for command name, 1/2 for argument + endmenu menu "CRON settings" config WEBGUIAPP_CRON_ENABLE diff --git a/include/CommandProcSys.h b/include/CommandProcSys.h index 692b3db..e32597c 100644 --- a/include/CommandProcSys.h +++ b/include/CommandProcSys.h @@ -25,15 +25,19 @@ #include "esp_err.h" -#define EXEC_COMMAND_MAX_LENGTH (64) -#define EXEC_OBJECT_NAME_MAX_LENGTH (EXEC_COMMAND_MAX_LENGTH/4) -#define EXEC_ACTION_NAME_MAX_LENGTH (EXEC_COMMAND_MAX_LENGTH/4) -#define EXEC_ARGUMENT_MAX_LENGTH (EXEC_COMMAND_MAX_LENGTH/2) - +#define EXEC_ARGUMENT_MAX_LENGTH (CONFIG_WEBGUIAPP_MAX_COMMAND_STRING_LENGTH/2) +typedef struct +{ + int index; + char object_name[CONFIG_WEBGUIAPP_MAX_COMMAND_STRING_LENGTH/4]; + char allowed_actions[CONFIG_WEBGUIAPP_MAX_COMMANDS_NUM][CONFIG_WEBGUIAPP_MAX_COMMAND_STRING_LENGTH/4]; + void (*command_handlers[CONFIG_WEBGUIAPP_MAX_COMMANDS_NUM])(char *obj, char *com, char *arg); +} obj_struct_t; int ExecCommand(char *cmd); -void GetSysObjectsInfo(char *data); +void GetObjectsInfo(char *data); +void SetCustomObjects(obj_struct_t *obj); #endif /* COMPONENTS_WEBGUIAPP_INCLUDE_COMMANDPROCSYS_H_ */ diff --git a/include/UserCallbacks.h b/include/UserCallbacks.h index 1197f5c..c66fa21 100644 --- a/include/UserCallbacks.h +++ b/include/UserCallbacks.h @@ -45,7 +45,4 @@ void regCustomPayloadTypeHandler(sys_error_code (*payload_handler)(data_message_ //User handler for save App configuration void regCustomSaveConf(void (*custom_saveconf)(void)); -//User handler for execute command -void regCustomExecCommand(int(*custom_exec)(char *cmd)); - #endif /* COMPONENTS_WEBGUIAPP_INCLUDE_USERCALLBACKS_H_ */ diff --git a/src/CommandProcSys.c b/src/CommandProcSys.c index 814b7a9..59a131c 100644 --- a/src/CommandProcSys.c +++ b/src/CommandProcSys.c @@ -24,8 +24,8 @@ #define TAG "COMMAND_PROC_SYS" -#define OBJECTS_NUMBER_SYS (1) -#define EXEC_ACTIONS_MAX_NUMBER_SYS (2) +//#define MAX_OBJECTS_NUMBER CONFIG_WEBGUIAPP_MAX_OBJECTS_NUM +//#define MAX_COMMANDS_NUMBER CONFIG_WEBGUIAPP_MAX_COMMANDS_NUM const char *exec_errors[] = { "executed OK", @@ -38,7 +38,6 @@ const char *exec_errors[] = { }; static int ExecCommandParse(char *cmd); -static int ExecSysCommand(char *cmd); int (*CustomExecCommand)(char *cmd); void regCustomExecCommand(int (*custom_exec)(char *cmd)) @@ -48,40 +47,59 @@ void regCustomExecCommand(int (*custom_exec)(char *cmd)) static void SYSTEM_TEST_handle(char *obj, char *com, char *arg) { - ESP_LOGI(TAG, "Object:%s, Command:%s, Argument %s",obj, com, arg); + ESP_LOGI(TAG, "Object:%s, Command:%s, Argument %s", obj, com, arg); } static void SYSTEM_REBOOT_handle(char *obj, char *com, char *arg) { - ESP_LOGI(TAG, "Object:%s, Command:%s, Argument %s",obj, com, arg); + ESP_LOGI(TAG, "Object:%s, Command:%s, Argument %s", obj, com, arg); } -typedef struct +obj_struct_t *custom_com_obj_arr = NULL; +void SetCustomObjects(obj_struct_t *obj) { - int index; - char object_name[EXEC_OBJECT_NAME_MAX_LENGTH]; - char allowed_actions[EXEC_ACTIONS_MAX_NUMBER_SYS][EXEC_ACTION_NAME_MAX_LENGTH]; - void (*command_handlers[EXEC_ACTIONS_MAX_NUMBER_SYS])(char *obj, char *com, char *arg); -} obj_struct_t; + custom_com_obj_arr = obj; +} const obj_struct_t com_obj_arr[] = { { .index = 0, .object_name = "SYSTEM", - .allowed_actions = { "TEST", "REBOOT" }, + .allowed_actions = { "TEST", "REBOOT", "TEST2", "TEST3", "TEST4", "TEST5" }, .command_handlers = { &SYSTEM_TEST_handle, &SYSTEM_REBOOT_handle } - } + }, + { + .index = 0, + .object_name = "SYSTEM1", + .allowed_actions = { "TEST", "REBOOT", "TEST2", "TEST3", "TEST4" }, + .command_handlers = { &SYSTEM_TEST_handle, &SYSTEM_REBOOT_handle } + }, + { + .index = 0, + .object_name = "SYSTEM2", + .allowed_actions = { "TEST", "REBOOT", "TEST2", "TEST3", "TEST4" }, + .command_handlers = { &SYSTEM_TEST_handle, &SYSTEM_REBOOT_handle } + }, + { + .index = 0, + .object_name = "SYSTEM3", + .allowed_actions = { "TEST", "REBOOT", "TEST2", "TEST3", "TEST4", "TEST5" }, + .command_handlers = { &SYSTEM_TEST_handle, &SYSTEM_REBOOT_handle } + }, + { 0 } }; -void GetSysObjectsInfo(char *data) +void GetObjectsInfo(char *data) { struct jWriteControl jwc; jwOpen(&jwc, data, VAR_MAX_VALUE_LENGTH, JW_ARRAY, JW_COMPACT); - for (int idx = 0; idx < OBJECTS_NUMBER_SYS; idx++) + for (int idx = 0; idx < CONFIG_WEBGUIAPP_MAX_OBJECTS_NUM; idx++) { + if (com_obj_arr[idx].object_name[0] == NULL) + break; jwArr_object(&jwc); jwObj_string(&jwc, "object", com_obj_arr[idx].object_name); jwObj_array(&jwc, "actions"); - for (int i = 0; i < EXEC_ACTIONS_MAX_NUMBER_SYS; i++) + for (int i = 0; i < CONFIG_WEBGUIAPP_MAX_COMMANDS_NUM; i++) { if ((com_obj_arr[idx].allowed_actions[i])[0] != NULL) jwArr_string(&jwc, com_obj_arr[idx].allowed_actions[i]); @@ -89,40 +107,42 @@ void GetSysObjectsInfo(char *data) jwEnd(&jwc); jwEnd(&jwc); } + for (int idx = 0; idx < CONFIG_WEBGUIAPP_MAX_OBJECTS_NUM; idx++) + { + if (custom_com_obj_arr[idx].object_name[0] == NULL) + break; + jwArr_object(&jwc); + jwObj_string(&jwc, "object", custom_com_obj_arr[idx].object_name); + jwObj_array(&jwc, "actions"); + for (int i = 0; i < CONFIG_WEBGUIAPP_MAX_COMMANDS_NUM; i++) + { + if ((custom_com_obj_arr[idx].allowed_actions[i])[0] != NULL) + jwArr_string(&jwc, custom_com_obj_arr[idx].allowed_actions[i]); + } + jwEnd(&jwc); + jwEnd(&jwc); + } + jwClose(&jwc); } int ExecCommand(char *cmd) { - int err = ExecSysCommand(cmd); - if(err != 4) - { + int err = ExecCommandParse(cmd); + if(err) if (err > 0) ESP_LOGW(TAG, "Command execution ERROR: %s",exec_errors[err]); - return err; - } - - if (CustomExecCommand != 0) - err = CustomExecCommand(cmd); - - if (err > 0) - ESP_LOGW(TAG, "Command execution ERROR: %s",exec_errors[err]); return err; } -static int ExecSysCommand(char *cmd) -{ - return ExecCommandParse(cmd); -} - static int ExecCommandParse(char *cmd) { char *obj = NULL, *com = NULL, *arg = NULL; int err = 0; int commlen = strlen(cmd); - if (commlen > EXEC_COMMAND_MAX_LENGTH) + if (commlen > CONFIG_WEBGUIAPP_MAX_COMMAND_STRING_LENGTH) return 1; - char comm[EXEC_COMMAND_MAX_LENGTH + 1]; + char comm[CONFIG_WEBGUIAPP_MAX_COMMAND_STRING_LENGTH + 1]; const char del1 = ','; const char del2 = 0x00; strcpy(comm, cmd); @@ -134,27 +154,47 @@ static int ExecCommandParse(char *cmd) if (!com) return 3; - err = 4; - for (int idx = 0; idx < OBJECTS_NUMBER_SYS; idx++) + for (int idx = 0; idx < CONFIG_WEBGUIAPP_MAX_OBJECTS_NUM; idx++) { if (!strcmp(obj, com_obj_arr[idx].object_name)) { - err = 5; - for (int i = 0; i < EXEC_ACTIONS_MAX_NUMBER_SYS; i++) + for (int i = 0; i < CONFIG_WEBGUIAPP_MAX_COMMANDS_NUM; i++) { if (!strcmp(com, com_obj_arr[idx].allowed_actions[i])) { if (com_obj_arr[idx].command_handlers[i] != NULL) { com_obj_arr[idx].command_handlers[i](obj, com, arg); - err = 0; + return 0; } else - err = 6; + return 6; } } + return 5; } } - return err; + + for (int idx = 0; idx < CONFIG_WEBGUIAPP_MAX_OBJECTS_NUM; idx++) + { + if (!strcmp(obj, custom_com_obj_arr[idx].object_name)) + { + for (int i = 0; i < CONFIG_WEBGUIAPP_MAX_COMMANDS_NUM; i++) + { + if (!strcmp(com, custom_com_obj_arr[idx].allowed_actions[i])) + { + if (custom_com_obj_arr[idx].command_handlers[i] != NULL) + { + custom_com_obj_arr[idx].command_handlers[i](obj, com, arg); + return 0; + } + else + return 6; + } + } + return 5; + } + } + return 4; } diff --git a/src/RestApiHandler.c b/src/RestApiHandler.c index ade193f..cfbff06 100644 --- a/src/RestApiHandler.c +++ b/src/RestApiHandler.c @@ -333,7 +333,7 @@ static void funct_cronrecs(char *argres, int rw) static void funct_objsinfo(char *argres, int rw) { - GetSysObjectsInfo(argres); + GetObjectsInfo(argres); } diff --git a/src/SysConfiguration.c b/src/SysConfiguration.c index a4ce141..7853f81 100644 --- a/src/SysConfiguration.c +++ b/src/SysConfiguration.c @@ -396,7 +396,7 @@ esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &C #endif Conf->modbusSettings.ModbusTCPPort = CONFIG_WEBGUIAPP_MBTCP_SERVER_PORT; #endif - for (int i = 0; i < 16; i++ ) + for (int i = 0; i < CONFIG_WEBGUIAPP_CRON_NUMBER; i++ ) { Conf->Timers[i].num = i+1; Conf->Timers[i].del = true;