refactor: RAM 优化 — 合并分散 buffer 为共享全局 buffer
net_srv.c: - 新增 g_work_buf[1024] 共享全局 buffer - 替换 manage_mqtt_recv_message() 中 14 处 malloc/free + 14 处栈上 resp[N] - 消除 mqtt_data_manage() 中重复的 static MyBuf[1152],复用全局 MyBuf - expand MAX_MQTTBUF_LEN 512→1280 (移到 net_srv.h) - 新增 WORK_BUF_SIZE 宏,修复 sizeof(resp) 指针截断 bug iot_mqtt_srv.c: - iot_mqtt_publish() 复用 net_srv.c 的 mqttBuf[1280],移除自有 static buf[1024] net_srv.h: - 新增 MAX_MQTTBUF_LEN 宏定义 (1280) 收益: - 消除 ~14 次 malloc/free 堆操作 (CH32V208 堆极小,malloc 容易失败) - BSS 节省 ~1152 + 1024 = 2176 字节 - 减少中断上下文栈压力 (移除 ~5KB 栈上临时数组)
This commit is contained in:
@@ -105,6 +105,8 @@ extern IOT_Topic g_iot_topic;
|
||||
|
||||
#define MQTT_KEEPALIVE_INTERVAL 9
|
||||
|
||||
#define MAX_MQTTBUF_LEN 1280 // MQTT send buffer (PUBLISH with sensor JSON)
|
||||
|
||||
|
||||
typedef struct _NET_STATE_
|
||||
{
|
||||
|
||||
@@ -23,6 +23,8 @@
|
||||
#include "simple_json.h"
|
||||
#include "tcp_json_srv.h"
|
||||
#include "storage.h"
|
||||
|
||||
extern uint8_t mqttBuf[]; // from net_srv.c, shared MQTT send buffer (1280 bytes)
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
@@ -145,12 +147,12 @@ static int iot_mqtt_send_subscribe(void) {
|
||||
/* 发送 MQTT PUBLISH */
|
||||
static int iot_mqtt_publish(const char *topic, const char *payload,
|
||||
uint16_t payload_len, uint8_t qos) {
|
||||
static uint8_t buf[IOT_MQTT_SEND_BUF_LEN]; // static: 避免 2KB 栈溢出
|
||||
/* Using extern mqttBuf from net_srv.c (1280 bytes) */
|
||||
int len;
|
||||
MQTTString mqtt_topic = MQTTString_initializer;
|
||||
mqtt_topic.cstring = (char *)topic;
|
||||
|
||||
len = MQTTSerialize_publish(buf, sizeof(buf), 0, qos, 0,
|
||||
len = MQTTSerialize_publish(mqttBuf, MAX_MQTTBUF_LEN, 0, qos, 0,
|
||||
++g_iot_msg_id, mqtt_topic,
|
||||
(unsigned char *)payload, payload_len);
|
||||
if (len <= 0) {
|
||||
|
||||
@@ -64,11 +64,18 @@ uint8_t MyBuf[RECE_BUF_LEN];
|
||||
|
||||
|
||||
#define MAX_TMP_BUF_LEN 64
|
||||
#define MAX_MQTTBUF_LEN 512 //384 //512
|
||||
/* MAX_MQTTBUF_LEN moved to net_srv.h */
|
||||
char mqtt_username[64] = {0};
|
||||
char mqtt_password[32] = {0};
|
||||
char mqtt_clientid[64] = {0};
|
||||
uint8_t mqttBuf[MAX_MQTTBUF_LEN];
|
||||
|
||||
|
||||
/* Shared work buffer - replaces per-handler malloc() + stack resp[N] arrays.
|
||||
Used in manage_mqtt_recv_message() interrupt context (single-thread safe). */
|
||||
#define WORK_BUF_SIZE 1024
|
||||
char g_work_buf[WORK_BUF_SIZE];
|
||||
|
||||
uint8_t TmpBuf[MAX_TMP_BUF_LEN] = "";
|
||||
|
||||
MQTTPacket_connectData mqttData = MQTTPacket_connectData_initializer;
|
||||
@@ -130,12 +137,8 @@ void MQTT_Subscribe(char *topic, unsigned char msgid)
|
||||
|
||||
void dg_subscribe_display_topic(void)
|
||||
{
|
||||
char *mBuff = (char *)malloc(256);
|
||||
if(mBuff == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
memset(mBuff, 0, 256);
|
||||
char *mBuff = g_work_buf; // shared global buffer
|
||||
memset(g_work_buf, 0, 1024);
|
||||
|
||||
// V1.01: 双主题协议 — topic_sub 即为完整订阅主题,不再追加后缀
|
||||
strcpy(mBuff, (char *)g_iot_topic.topic_sub);
|
||||
@@ -192,17 +195,13 @@ void dev_initialize_pub(void)
|
||||
|
||||
mqtt_publish((char *)(g_iot_topic.topic_pub), (char *)mBuff, 0);
|
||||
|
||||
free(mBuff);
|
||||
/* mBuff = g_work_buf - no free */
|
||||
}
|
||||
|
||||
void dev_response_heartbeat(char *dat)
|
||||
{
|
||||
char *mBuff = (char *)malloc(256);
|
||||
if(mBuff == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
memset(mBuff, 0, 256);
|
||||
char *mBuff = g_work_buf; // shared global buffer
|
||||
memset(g_work_buf, 0, 1024);
|
||||
if(dat == NULL)
|
||||
{
|
||||
sprintf((char *)mBuff, "{\"Method\":\"Heartbeat\", \"Data\":{\"Device_id\": \"%s\"}}", g_dev_number_str);
|
||||
@@ -212,7 +211,7 @@ void dev_response_heartbeat(char *dat)
|
||||
// mqtt_publish("gtpc/display/Initialize", gbufSend, 0);
|
||||
mqtt_publish((char *)(g_iot_topic.topic_pub), (char *)mBuff, 0);
|
||||
|
||||
free(mBuff);
|
||||
/* mBuff = g_work_buf - no free */
|
||||
}
|
||||
|
||||
void mqtt_deserialize_publish(char* topic, char* msg, int length)
|
||||
@@ -372,7 +371,7 @@ void reset_net_active_timeup(void)
|
||||
void mqtt_data_manage(uint8_t id)
|
||||
{
|
||||
uint32_t len;
|
||||
static uint8_t MyBuf[RECE_BUF_LEN]; // static: 避免栈溢出
|
||||
/* Use global MyBuf (declared above) - no duplicate static needed */
|
||||
unsigned char dup;
|
||||
unsigned short packetid;
|
||||
int qos;
|
||||
@@ -1106,8 +1105,8 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
|
||||
// --- dev_info_query ---
|
||||
if(strcmp(cmd_str, "dev_info_query") == 0) {
|
||||
char resp[600];
|
||||
snprintf(resp, sizeof(resp),
|
||||
char *resp = g_work_buf;
|
||||
snprintf(resp, WORK_BUF_SIZE,
|
||||
"{\"msg_id\":%lu,\"cmd\":\"dev_info_query\","
|
||||
"\"ts\":%lu,\"code\":0,\"msg\":\"success\","
|
||||
"\"data\":{"
|
||||
@@ -1126,10 +1125,10 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
|
||||
// --- pwd_verify ---
|
||||
if(strcmp(cmd_str, "pwd_verify") == 0) {
|
||||
char *data_buf = (char *)malloc(256);
|
||||
char *data_buf = g_work_buf; // shared global buffer (was malloc)
|
||||
char password[16] = {0};
|
||||
if(data_buf) {
|
||||
memset(data_buf, 0, 256);
|
||||
memset(g_work_buf, 0, 1024); // clear shared work buffer
|
||||
simple_parse_json(msg, "\"data\"", data_buf);
|
||||
if(strlen(data_buf) > 0) {
|
||||
memset(tmp, 0, sizeof(tmp));
|
||||
@@ -1140,20 +1139,20 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
if(plen > 0 && pwd[plen - 1] == '"') pwd[plen - 1] = '\0';
|
||||
strncpy(password, pwd, 15);
|
||||
}
|
||||
free(data_buf);
|
||||
/* data_buf = g_work_buf - no free needed */
|
||||
}
|
||||
|
||||
if(strlen(password) == 6 && memcmp(password, g_dev_password, 6) == 0) {
|
||||
char resp[256];
|
||||
snprintf(resp, sizeof(resp),
|
||||
char *resp = g_work_buf;
|
||||
snprintf(resp, WORK_BUF_SIZE,
|
||||
"{\"msg_id\":%lu,\"cmd\":\"pwd_verify\","
|
||||
"\"ts\":%lu,\"code\":0,\"msg\":\"success\"}",
|
||||
msg_id, mstick() / 1000);
|
||||
mqtt_publish(resp_topic, resp, 1);
|
||||
PRINT("IOT: pwd_verify OK\n");
|
||||
} else {
|
||||
char resp[256];
|
||||
snprintf(resp, sizeof(resp),
|
||||
char *resp = g_work_buf;
|
||||
snprintf(resp, WORK_BUF_SIZE,
|
||||
"{\"msg_id\":%lu,\"cmd\":\"pwd_verify\","
|
||||
"\"ts\":%lu,\"code\":2,\"msg\":\"password incorrect\"}",
|
||||
msg_id, mstick() / 1000);
|
||||
@@ -1165,8 +1164,8 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
// --- dev_serial_set ---
|
||||
if(strcmp(cmd_str, "dev_serial_set") == 0) {
|
||||
// TODO: 实现序列号修改
|
||||
char resp[256];
|
||||
snprintf(resp, sizeof(resp),
|
||||
char *resp = g_work_buf;
|
||||
snprintf(resp, WORK_BUF_SIZE,
|
||||
"{\"msg_id\":%lu,\"cmd\":\"dev_serial_set\","
|
||||
"\"ts\":%lu,\"code\":4,\"msg\":\"not implemented yet\"}",
|
||||
msg_id, mstick() / 1000);
|
||||
@@ -1176,8 +1175,8 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
|
||||
// --- ssc_net_query ---
|
||||
if(strcmp(cmd_str, "ssc_net_query") == 0) {
|
||||
char resp[512];
|
||||
snprintf(resp, sizeof(resp),
|
||||
char *resp = g_work_buf;
|
||||
snprintf(resp, WORK_BUF_SIZE,
|
||||
"{\"msg_id\":%lu,\"cmd\":\"ssc_net_query\","
|
||||
"\"ts\":%lu,\"code\":0,\"msg\":\"success\","
|
||||
"\"data\":{"
|
||||
@@ -1202,9 +1201,9 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
if(strcmp(cmd_str, "ssc_net_set") == 0) {
|
||||
Local_Net_Cfg lcfg = local_net_cfg;
|
||||
NET_CENTER_INFO ccfg = net_center_info;
|
||||
char *data_buf = (char *)malloc(256);
|
||||
char *data_buf = g_work_buf; // shared global buffer (was malloc)
|
||||
if(data_buf) {
|
||||
memset(data_buf, 0, 256);
|
||||
memset(g_work_buf, 0, 1024); // clear shared work buffer
|
||||
simple_parse_json(msg, "\"data\"", data_buf);
|
||||
if(strlen(data_buf) > 0) {
|
||||
char ip_str[32];
|
||||
@@ -1253,14 +1252,14 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
if(p > 0 && p <= 65535) ccfg.tcp_port = (uint16_t)p;
|
||||
}
|
||||
}
|
||||
free(data_buf);
|
||||
/* data_buf = g_work_buf - no free needed */
|
||||
}
|
||||
write_net_config(&lcfg, &ccfg, &iot_net_info, &g_iot_topic);
|
||||
local_net_cfg = lcfg;
|
||||
net_center_info = ccfg;
|
||||
|
||||
char resp[256];
|
||||
snprintf(resp, sizeof(resp),
|
||||
char *resp = g_work_buf;
|
||||
snprintf(resp, WORK_BUF_SIZE,
|
||||
"{\"msg_id\":%lu,\"cmd\":\"ssc_net_set\","
|
||||
"\"ts\":%lu,\"code\":0,\"msg\":\"success\"}",
|
||||
msg_id, mstick() / 1000);
|
||||
@@ -1271,8 +1270,8 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
|
||||
// --- iot_net_query ---
|
||||
if(strcmp(cmd_str, "iot_net_query") == 0) {
|
||||
char resp[512];
|
||||
snprintf(resp, sizeof(resp),
|
||||
char *resp = g_work_buf;
|
||||
snprintf(resp, WORK_BUF_SIZE,
|
||||
"{\"msg_id\":%lu,\"cmd\":\"iot_net_query\","
|
||||
"\"ts\":%lu,\"code\":0,\"msg\":\"success\","
|
||||
"\"data\":{"
|
||||
@@ -1288,9 +1287,9 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
// --- iot_net_set ---
|
||||
if(strcmp(cmd_str, "iot_net_set") == 0) {
|
||||
IOT_NET_INFO icfg = iot_net_info;
|
||||
char *data_buf = (char *)malloc(256);
|
||||
char *data_buf = g_work_buf; // shared global buffer (was malloc)
|
||||
if(data_buf) {
|
||||
memset(data_buf, 0, 256);
|
||||
memset(g_work_buf, 0, 1024); // clear shared work buffer
|
||||
simple_parse_json(msg, "\"data\"", data_buf);
|
||||
if(strlen(data_buf) > 0) {
|
||||
char fld[64];
|
||||
@@ -1332,13 +1331,13 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
strncpy((char *)icfg.password, s, 31);
|
||||
}
|
||||
}
|
||||
free(data_buf);
|
||||
/* data_buf = g_work_buf - no free needed */
|
||||
}
|
||||
write_net_config(&local_net_cfg, &net_center_info, &icfg, &g_iot_topic);
|
||||
iot_net_info = icfg;
|
||||
|
||||
char resp[256];
|
||||
snprintf(resp, sizeof(resp),
|
||||
char *resp = g_work_buf;
|
||||
snprintf(resp, WORK_BUF_SIZE,
|
||||
"{\"msg_id\":%lu,\"cmd\":\"iot_net_set\","
|
||||
"\"ts\":%lu,\"code\":0,\"msg\":\"success\"}",
|
||||
msg_id, mstick() / 1000);
|
||||
@@ -1349,8 +1348,8 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
|
||||
// --- iot_topic_query ---
|
||||
if(strcmp(cmd_str, "iot_topic_query") == 0) {
|
||||
char resp[512];
|
||||
snprintf(resp, sizeof(resp),
|
||||
char *resp = g_work_buf;
|
||||
snprintf(resp, WORK_BUF_SIZE,
|
||||
"{\"msg_id\":%lu,\"cmd\":\"iot_topic_query\","
|
||||
"\"ts\":%lu,\"code\":0,\"msg\":\"success\","
|
||||
"\"data\":{"
|
||||
@@ -1366,9 +1365,9 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
// --- iot_topic_set ---
|
||||
if(strcmp(cmd_str, "iot_topic_set") == 0) {
|
||||
IOT_Topic topic = g_iot_topic;
|
||||
char *data_buf = (char *)malloc(256);
|
||||
char *data_buf = g_work_buf; // shared global buffer (was malloc)
|
||||
if(data_buf) {
|
||||
memset(data_buf, 0, 256);
|
||||
memset(g_work_buf, 0, 1024); // clear shared work buffer
|
||||
simple_parse_json(msg, "\"data\"", data_buf);
|
||||
if(strlen(data_buf) > 0) {
|
||||
char fld[64];
|
||||
@@ -1394,13 +1393,13 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
strncpy((char *)topic.topic_sub, s, 63);
|
||||
}
|
||||
}
|
||||
free(data_buf);
|
||||
/* data_buf = g_work_buf - no free needed */
|
||||
}
|
||||
write_net_config(&local_net_cfg, &net_center_info, &iot_net_info, &topic);
|
||||
g_iot_topic = topic;
|
||||
|
||||
char resp[256];
|
||||
snprintf(resp, sizeof(resp),
|
||||
char *resp = g_work_buf;
|
||||
snprintf(resp, WORK_BUF_SIZE,
|
||||
"{\"msg_id\":%lu,\"cmd\":\"iot_topic_set\","
|
||||
"\"ts\":%lu,\"code\":0,\"msg\":\"success\"}",
|
||||
msg_id, mstick() / 1000);
|
||||
@@ -1411,8 +1410,8 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
|
||||
// --- pwd_set ---
|
||||
if(strcmp(cmd_str, "pwd_set") == 0) {
|
||||
char resp[256];
|
||||
snprintf(resp, sizeof(resp),
|
||||
char *resp = g_work_buf;
|
||||
snprintf(resp, WORK_BUF_SIZE,
|
||||
"{\"msg_id\":%lu,\"cmd\":\"pwd_set\","
|
||||
"\"ts\":%lu,\"code\":4,\"msg\":\"not implemented yet\"}",
|
||||
msg_id, mstick() / 1000);
|
||||
@@ -1422,8 +1421,8 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
|
||||
// --- factory_reset ---
|
||||
if(strcmp(cmd_str, "factory_reset") == 0) {
|
||||
char resp[256];
|
||||
snprintf(resp, sizeof(resp),
|
||||
char *resp = g_work_buf;
|
||||
snprintf(resp, WORK_BUF_SIZE,
|
||||
"{\"msg_id\":%lu,\"cmd\":\"factory_reset\","
|
||||
"\"ts\":%lu,\"code\":0,\"msg\":\"success\"}",
|
||||
msg_id, mstick() / 1000);
|
||||
@@ -1443,9 +1442,9 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
|
||||
// --- report_config ---
|
||||
if(strcmp(cmd_str, "report_config") == 0) {
|
||||
char *data_buf = (char *)malloc(512);
|
||||
char *data_buf = g_work_buf; // shared global buffer (was malloc)
|
||||
if(data_buf) {
|
||||
memset(data_buf, 0, 512);
|
||||
memset(g_work_buf, 0, 1024); // clear shared work buffer
|
||||
simple_parse_json(msg, "\"data\"", data_buf);
|
||||
|
||||
if(strlen(data_buf) > 0) {
|
||||
@@ -1484,11 +1483,11 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
simple_parse_json(data_buf, "\"timeout\"", tmp);
|
||||
if(strlen(tmp) > 0) g_report_cfg.timeout = (uint16_t)strtoul(tmp, NULL, 10);
|
||||
}
|
||||
free(data_buf);
|
||||
/* data_buf = g_work_buf - no free needed */
|
||||
}
|
||||
|
||||
char resp[400];
|
||||
snprintf(resp, sizeof(resp),
|
||||
char *resp = g_work_buf;
|
||||
snprintf(resp, WORK_BUF_SIZE,
|
||||
"{\"msg_id\":%lu,\"cmd\":\"report_config\","
|
||||
"\"ts\":%lu,\"code\":0,\"msg\":\"success\","
|
||||
"\"data\":{"
|
||||
@@ -1510,8 +1509,8 @@ void manage_mqtt_recv_message(char * msg, int length)
|
||||
|
||||
// --- 未支持的命令 ---
|
||||
{
|
||||
char resp[256];
|
||||
snprintf(resp, sizeof(resp),
|
||||
char *resp = g_work_buf;
|
||||
snprintf(resp, WORK_BUF_SIZE,
|
||||
"{\"msg_id\":%lu,\"cmd\":\"%s\","
|
||||
"\"ts\":%lu,\"code\":4,\"msg\":\"unsupported command\"}",
|
||||
msg_id, cmd_str, mstick() / 1000);
|
||||
|
||||
Reference in New Issue
Block a user