From 6712ed4b42f1f47bd910b38f112d25dac7b55840 Mon Sep 17 00:00:00 2001 From: bogd Date: Mon, 11 Mar 2024 14:41:32 +0200 Subject: [PATCH] added topic for the files operations, file list and delete implemented --- src/CommandProcSys.c | 2 +- src/MQTT.c | 18 +++++++++- src/RestApiHandler.c | 85 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 2 deletions(-) diff --git a/src/CommandProcSys.c b/src/CommandProcSys.c index 4d5c90b..0bdced0 100644 --- a/src/CommandProcSys.c +++ b/src/CommandProcSys.c @@ -144,7 +144,7 @@ int ExecCommand(char *cmd) static int ExecCommandParse(char *cmd) { char *obj = NULL, *com = NULL, *arg = NULL; - int err = 0; + //int err = 0; int commlen = strlen(cmd); if (commlen > CONFIG_WEBGUIAPP_MAX_COMMAND_STRING_LENGTH) return 1; diff --git a/src/MQTT.c b/src/MQTT.c index a720e45..92ce7b7 100644 --- a/src/MQTT.c +++ b/src/MQTT.c @@ -28,6 +28,7 @@ #define TAG "MQTT" #define SERVICE_NAME "SYSTEM" // Dedicated service name +#define FILE_SERVICE_NAME "FILE" #define EXTERNAL_SERVICE_NAME "RS485" #define UPLINK_SUBTOPIC "UPLINK" // Device publish to this topic #define DOWNLINK_SUBTOPIC "DWLINK" // Device listen from this topic @@ -219,6 +220,14 @@ static void mqtt_system_event_handler(int idx, void *handler_args, esp_event_bas ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); ESP_LOGI(TAG, "Subscribe to %s", topic); #endif + + ComposeTopic(topic, idx, FILE_SERVICE_NAME, DOWNLINK_SUBTOPIC); + msg_id = esp_mqtt_client_subscribe(client, (char*) topic, 0); +#if MQTT_DEBUG_MODE > 0 + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + ESP_LOGI(TAG, "Subscribe to %s", topic); +#endif + #ifdef CONFIG_WEBGUIAPP_UART_TRANSPORT_ENABLE if (GetSysConf()->serialSettings.Flags.IsBridgeEnabled) { @@ -291,8 +300,15 @@ static void mqtt_system_event_handler(int idx, void *handler_args, esp_event_bas } else ESP_LOGE(TAG, "Out of free RAM for MQTT API handle"); - } + + ComposeTopic(topic, idx, FILE_SERVICE_NAME, DOWNLINK_SUBTOPIC); + if (!memcmp(topic, event->topic, event->topic_len)) + { + ESP_LOGI(TAG, "Got data for FILE_SYSTEM_SERVICE:%s", event->data); + //Here handler of file system operations + } + #ifdef CONFIG_WEBGUIAPP_UART_TRANSPORT_ENABLE if (GetSysConf()->serialSettings.Flags.IsBridgeEnabled) { diff --git a/src/RestApiHandler.c b/src/RestApiHandler.c index 2ccfc3b..d097692 100644 --- a/src/RestApiHandler.c +++ b/src/RestApiHandler.c @@ -30,6 +30,7 @@ #include "romfs.h" #include "esp_idf_version.h" #include "NetTransport.h" +#include "esp_vfs.h" extern SYS_CONFIG SysConfig; @@ -371,6 +372,87 @@ static void funct_exec(char *argres, int rw) snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"EXECUTED\""); } +static const char *dirpath = "/data/"; + +static void funct_file_list(char *argres, int rw) +{ + + char entrypath[FILE_PATH_MAX]; + char entrysize[16]; + const char *entrytype = "file"; + struct dirent *entry; + struct stat entry_stat; + DIR *dir = opendir(dirpath); + const size_t dirpath_len = strlen(dirpath); + strlcpy(entrypath, dirpath, sizeof(entrypath)); + + if (!dir) + { + ESP_LOGE("FILE_API", "Failed to stat dir : %s", dirpath); + snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"ERROR:DIR_NOT_FOUND\""); + return; + } + + struct jWriteControl jwc; + jwOpen(&jwc, argres, VAR_MAX_VALUE_LENGTH, JW_ARRAY, JW_COMPACT); + while ((entry = readdir(dir)) != NULL) + { + strlcpy(entrypath + dirpath_len, entry->d_name, sizeof(entrypath) - dirpath_len); + entrytype = (entry->d_type == DT_DIR ? "directory" : "file"); + if (stat(entrypath, &entry_stat) == -1) + { + ESP_LOGE("FILE_API", "Failed to stat %s : %s", entrytype, entry->d_name); + continue; + } + + jwArr_object(&jwc); + jwObj_string(&jwc, "name", (char*) entry->d_name); + jwObj_string(&jwc, "type", entrytype); + jwObj_int(&jwc, "size", entry_stat.st_size); + jwEnd(&jwc); + } + jwClose(&jwc); + + +} + +static void funct_file_delete(char *argres, int rw) +{ + char filepath[FILE_PATH_MAX]; + struct stat file_stat; + + const char *filename = argres; + if (!filename) + { + snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"ERROR:DIR_NOT_FOUND\""); + return; + } + + if (filename[strlen(filename) - 1] == '/') + { + ESP_LOGE("FILE_API", "Invalid filename : %s", filename); + snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"ERROR:DIR_NOT_FOUND\""); + return; + } + + strcpy(filepath, dirpath); + strcat(filepath, filename); + + ESP_LOGI("FILE_API", " filepath to delete : %s", filepath); + + if (stat(filepath, &file_stat) == -1) + { + ESP_LOGE("FILE_API", "File does not exist : %s", filename); + snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"ERROR:DIR_NOT_FOUND\""); + /* Respond with 400 Bad Request */ + return; + } + unlink(filepath); + +} + + + const int hw_rev = CONFIG_BOARD_HARDWARE_REVISION; const bool VAR_TRUE = true; const bool VAR_FALSE = false; @@ -557,6 +639,9 @@ const rest_var_t SystemVariables[] = { 0, "cronrecs", &funct_cronrecs, VAR_FUNCT, RW, 0, 0 }, { 0, "objsinfo", &funct_objsinfo, VAR_FUNCT, R, 0, 0 }, + { 0, "file_list", &funct_file_list, VAR_FUNCT, R, 0, 0 }, + { 0, "file_delete", &funct_file_delete, VAR_FUNCT, R, 0, 0 }, + };