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:
wangfq
2026-07-08 10:28:28 +08:00
parent 555cdb741d
commit 0df580b1f6
3 changed files with 64 additions and 61 deletions
@@ -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) {