From aa448f3cbec98bc6161617fa77c45bf849f392cb Mon Sep 17 00:00:00 2001 From: Bogdan Pilyugin Date: Tue, 5 Sep 2023 13:29:23 +0200 Subject: [PATCH] synchronized io buffer on http and mqtt, fixed 'msgid' and 'applytype' variables names in outgoing data --- include/HTTPServer.h | 4 ++-- include/SystemApplication.h | 5 ++++- src/HTTPAPISystem.c | 4 ++-- src/MQTT.c | 6 ++++-- src/SysComm.c | 8 ++++++-- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/include/HTTPServer.h b/include/HTTPServer.h index 87a687e..a4ffc88 100644 --- a/include/HTTPServer.h +++ b/include/HTTPServer.h @@ -41,7 +41,7 @@ #include "esp_netif.h" #include "esp_eth.h" #include "mbedtls/base64.h" -#include "SysConfiguration.h" +#include "SystemApplication.h" #define MAX_DYNVAR_NAME_LENGTH 32 #define MAX_DYNVAR_LENGTH 256 @@ -53,7 +53,7 @@ #define MAX_FILE_SIZE_STR "200KB" /* Scratch buffer size */ -#define SCRATCH_BUFSIZE 4096 +#define SCRATCH_BUFSIZE EXPECTED_MAX_DATA_SIZE #define AUTH_DATA_MAX_LENGTH 16 #define HTTP_SERVER_DEBUG_LEVEL 0 diff --git a/include/SystemApplication.h b/include/SystemApplication.h index 5e2baed..b378dc9 100644 --- a/include/SystemApplication.h +++ b/include/SystemApplication.h @@ -30,7 +30,10 @@ #define REAST_API_DEBUG_MODE 0 -#define EXPECTED_MAX_DATA_RESPONSE_SIZE (4096 + 1024) +#define EXPECTED_MAX_HEADER_SIZE 512 +#define EXPECTED_MAX_PAYLOAD_SIZE 4096 + +#define EXPECTED_MAX_DATA_SIZE (EXPECTED_MAX_HEADER_SIZE + EXPECTED_MAX_PAYLOAD_SIZE) #define VAR_MAX_NAME_LENGTH MAX_DYNVAR_NAME_LENGTH #define VAR_MAX_VALUE_LENGTH (2048) diff --git a/src/HTTPAPISystem.c b/src/HTTPAPISystem.c index 71f2e1d..769b6aa 100644 --- a/src/HTTPAPISystem.c +++ b/src/HTTPAPISystem.c @@ -31,7 +31,7 @@ HTTP_IO_RESULT HTTPPostSysAPI(httpd_req_t *req, char *PostData) httpd_req_get_hdr_value_str(req, "Content-Type", (char*) data, 31); if (!memcmp(data, "application/json", sizeof("application/json"))) { - char *respbuf = malloc(EXPECTED_MAX_DATA_RESPONSE_SIZE); + char *respbuf = malloc(EXPECTED_MAX_DATA_SIZE); if (respbuf) { data_message_t M = { 0 }; @@ -39,7 +39,7 @@ HTTP_IO_RESULT HTTPPostSysAPI(httpd_req_t *req, char *PostData) M.inputDataLength = strlen(PostData); M.chlidx = 100; M.outputDataBuffer = respbuf; - M.outputDataLength = EXPECTED_MAX_DATA_RESPONSE_SIZE; + M.outputDataLength = EXPECTED_MAX_DATA_SIZE; ServiceDataHandler(&M); httpd_resp_set_type(req, "application/json"); httpd_resp_sendstr(req, respbuf); diff --git a/src/MQTT.c b/src/MQTT.c index 17a20a2..619481b 100644 --- a/src/MQTT.c +++ b/src/MQTT.c @@ -235,7 +235,7 @@ static void mqtt_system_event_handler(int idx, void *handler_args, esp_event_bas if (!memcmp(topic, event->topic, event->topic_len)) { //SystemDataHandler(event->data, event->data_len, idx); //Old API - char *respbuf = malloc(EXPECTED_MAX_DATA_RESPONSE_SIZE); + char *respbuf = malloc(EXPECTED_MAX_DATA_SIZE); if (respbuf != NULL) { data_message_t M = { 0 }; @@ -243,7 +243,7 @@ static void mqtt_system_event_handler(int idx, void *handler_args, esp_event_bas M.inputDataLength = event->data_len; M.chlidx = idx; M.outputDataBuffer = respbuf; - M.outputDataLength = EXPECTED_MAX_DATA_RESPONSE_SIZE; + M.outputDataLength = EXPECTED_MAX_DATA_SIZE; ServiceDataHandler(&M); SysServiceMQTTSend(M.outputDataBuffer, strlen(M.outputDataBuffer), idx); free(respbuf); @@ -371,6 +371,8 @@ static void start_mqtt() strcat(url, ":"); strcat(url, tmp); #if ESP_IDF_VERSION_MAJOR >= 5 + mqtt_cfg.buffer.out_size = EXPECTED_MAX_DATA_SIZE; + mqtt_cfg.buffer.size = EXPECTED_MAX_DATA_SIZE; mqtt_cfg.broker.address.uri = url; mqtt_cfg.credentials.username = GetSysConf()->mqttStation[i].UserName; mqtt_cfg.credentials.authentication.password = GetSysConf()->mqttStation[i].UserPass; diff --git a/src/SysComm.c b/src/SysComm.c index 4c9237e..a9047cc 100644 --- a/src/SysComm.c +++ b/src/SysComm.c @@ -74,9 +74,10 @@ static sys_error_code PayloadType_1_Handler(data_message_t *MSG) char time[RFC3339_TIMESTAMP_LENGTH]; GetRFC3339Time(time); jwObj_string(&jwc, "time", time); - jwObj_int(&jwc, "messtype", DATA_MESSAGE_TYPE_RESPONSE); + jwObj_int(&jwc, "msgtype", DATA_MESSAGE_TYPE_RESPONSE); jwObj_int(&jwc, "payloadtype", 1); jwObj_object(&jwc, "payload"); + jwObj_int(&jwc, "applytype", 0); jwObj_object(&jwc, "variables"); jRead(MSG->inputDataBuffer, "{'data'{'payload'{'variables'", &result); @@ -222,7 +223,10 @@ static sys_error_code DataHeaderParser(data_message_t *MSG) jRead(MSG->inputDataBuffer, "{'signature'", &result); if (result.elements == 1) { - //ESP_LOGI(TAG, "Signature is %.*s", 64, (char* )result.pValue); +#if REAST_API_DEBUG_MODE + ESP_LOGI(TAG, "Signature is %.*s", 64, (char* )result.pValue); +#endif + //Here compare calculated and received signature; } else