From c32accd2320c2bf1d07d1667c0f50ff18be0a25a Mon Sep 17 00:00:00 2001 From: bogdan Date: Tue, 12 Mar 2024 19:54:20 +0200 Subject: [PATCH] raw memory access API implementation --- CMakeLists.txt | 1 + include/{FilesAPI.h => RawMemAPI.h} | 19 ++-- src/FilesAPI.c | 30 ------ src/RawMemAPI.c | 137 ++++++++++++++++++++++++++++ src/RestApiHandler.c | 14 +-- 5 files changed, 149 insertions(+), 52 deletions(-) rename include/{FilesAPI.h => RawMemAPI.h} (76%) delete mode 100644 src/FilesAPI.c create mode 100644 src/RawMemAPI.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c616cc..4127542 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ idf_component_register( "src/MQTT.c" "src/CronTimers.c" "src/SerialPort.c" + src/RawMemAPI.c src/OTA.c src/RestApiHandler.c src/SysComm.c diff --git a/include/FilesAPI.h b/include/RawMemAPI.h similarity index 76% rename from include/FilesAPI.h rename to include/RawMemAPI.h index 240f17c..772c2ae 100644 --- a/include/FilesAPI.h +++ b/include/RawMemAPI.h @@ -12,17 +12,16 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * File name: MQTTFiles.h + * File name: RawMemAPI.h * Project: WebguiappTemplate * Created on: 2024-03-11 * Author: bogd * Description: */ -#ifndef COMPONENTS_WEBGUIAPP_INCLUDE_FILESAPI_H_ -#define COMPONENTS_WEBGUIAPP_INCLUDE_FILESAPI_H_ - - +#ifndef COMPONENTS_WEBGUIAPP_INCLUDE_RAWMEMAPI_H_ +#define COMPONENTS_WEBGUIAPP_INCLUDE_RAWMEMAPI_H_ +#include "webguiapp.h" typedef struct { @@ -30,14 +29,8 @@ typedef struct int operType; int dataLengthTotal; int dataLengthReady; - - } mqtt_files_cb; +void RawDataHandler(char *argres, int rw); - - - - - -#endif /* COMPONENTS_WEBGUIAPP_INCLUDE_FILESAPI_H_ */ +#endif /* COMPONENTS_WEBGUIAPP_INCLUDE_RAWMEMAPI_H_ */ diff --git a/src/FilesAPI.c b/src/FilesAPI.c deleted file mode 100644 index faf53e2..0000000 --- a/src/FilesAPI.c +++ /dev/null @@ -1,30 +0,0 @@ - /* Copyright 2024 Bogdan Pilyugin - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * File name: MQTTFiles.c - * Project: WebguiappTemplate - * Created on: 2024-03-11 - * Author: bogd - * Description: - */ - -#include "FilesAPI.h" - - - -void FileAPIHandler(char *argres, int rw) -{ - - -} diff --git a/src/RawMemAPI.c b/src/RawMemAPI.c new file mode 100644 index 0000000..707c6f2 --- /dev/null +++ b/src/RawMemAPI.c @@ -0,0 +1,137 @@ +/* Copyright 2024 Bogdan Pilyugin + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * File name: RawMemAPI.c + * Project: WebguiappTemplate + * Created on: 2024-03-11 + * Author: bogd + * Description: + */ + +#include "RawMemAPI.h" +#include "SystemApplication.h" +#include +#include +#include "esp_vfs.h" + +#define TAG "RAW_MEM_API" + +/* + { + "operation" : 1, + "mem_object": "testfile.txt", + "offset" : 2000, + "size : 4096, + "data" : "" , + } + */ + +static const char *dirpath = "/data/"; +#define MEM_OBLECT_MAX_LENGTH 32 + +void RawDataHandler(char *argres, int rw) +{ + int operation = 0; + int offset; + int size; + char mem_object[MEM_OBLECT_MAX_LENGTH]; + char filepath[FILE_PATH_MAX]; + struct stat file_stat; + + struct jReadElement result; + jRead(argres, "", &result); + if (result.dataType != JREAD_OBJECT) + { + snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"ERROR:not an object\""); + return; + } + + jRead(argres, "{'operation'", &result); + if (result.elements == 1) + { + operation = atoi((char*) result.pValue); + if (operation < 1 || operation > 2) + { + snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"ERROR:'operation' value not in [1...2]\""); + return; + } + } + else + { + snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"ERROR:key 'operation' not found\""); + return; + } + + jRead(argres, "{'mem_object'", &result); + if (result.elements == 1) + { + if (result.bytelen > 0 && result.bytelen < MEM_OBLECT_MAX_LENGTH) + { + memcpy(mem_object, (char*) result.pValue, result.bytelen); + mem_object[result.bytelen] = 0x00; + } + else + { + snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"ERROR:'mem_object' length out of range\""); + return; + } + } + else + { + snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"ERROR:key 'mem_object' not found\""); + return; + } + + jRead(argres, "{'offset'", &result); + if (result.elements == 1) + { + offset = atoi((char*) result.pValue); + } + else + { + snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"ERROR:key 'offset' not found\""); + return; + } + + jRead(argres, "{'size'", &result); + if (result.elements == 1) + { + size = atoi((char*) result.pValue); + } + else + { + snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"ERROR:key 'size' not found\""); + return; + } + + ESP_LOGI(TAG, "Got memory object %s, with offest %d and size %d", mem_object, offset, size); + + strcpy(filepath, dirpath); + strcat(filepath, mem_object); + + if (stat(filepath, &file_stat) == -1) + { + ESP_LOGE("FILE_API", "File does not exist : %s", mem_object); + snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"ERROR:DIR_NOT_FOUND\""); + return; + } + + FILE *f = fopen(filepath, "r"); + if (f == NULL) + { + ESP_LOGE(TAG, "Failed to open file %s for writing", mem_object); + return; + } + +} diff --git a/src/RestApiHandler.c b/src/RestApiHandler.c index ecec206..3fadc87 100644 --- a/src/RestApiHandler.c +++ b/src/RestApiHandler.c @@ -31,7 +31,7 @@ #include "esp_idf_version.h" #include "NetTransport.h" #include "esp_vfs.h" -#include "FilesAPI.h" +#include "RawMemAPI.h" extern SYS_CONFIG SysConfig; @@ -481,9 +481,6 @@ static void funct_file_get(char *argres, int rw) /* Respond with 400 Bad Request */ return; } - - - } static void funct_file_put(char *argres, int rw) @@ -491,11 +488,9 @@ static void funct_file_put(char *argres, int rw) } -static void funct_file_operation(char *argres, int rw) +static void funct_raw_data(char *argres, int rw) { - - - +RawDataHandler(argres, rw); } const int hw_rev = CONFIG_BOARD_HARDWARE_REVISION; @@ -688,7 +683,8 @@ const rest_var_t SystemVariables[] = { 0, "file_delete", &funct_file_delete, VAR_FUNCT, R, 0, 0 }, { 0, "file_get", &funct_file_get, VAR_FUNCT, R, 0, 0 }, { 0, "file_put", &funct_file_put, VAR_FUNCT, R, 0, 0 }, - { 0, "file_operation", &funct_file_operation, VAR_FUNCT, R, 0, 0 }, + + { 0, "raw_data", &funct_raw_data, VAR_FUNCT, R, 0, 0 }, };