raw memory access API implementation

This commit is contained in:
Bogdan Pilyugin 2024-03-12 19:54:20 +02:00
parent 06ecf10ae5
commit c32accd232
5 changed files with 149 additions and 52 deletions

View File

@ -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

View File

@ -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_ */

View File

@ -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)
{
}

137
src/RawMemAPI.c Normal file
View File

@ -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 <SysConfiguration.h>
#include <webguiapp.h>
#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;
}
}

View File

@ -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 },
};