From dfadc26717e6e5e44c2d972f9e7a149495e1a13c Mon Sep 17 00:00:00 2001 From: wangfq Date: Mon, 6 Jul 2026 11:27:08 +0800 Subject: [PATCH] =?UTF-8?q?fix(vd960DBN):=20MQTT=20buffer=20=E4=BB=8E?= =?UTF-8?q?=E6=A0=88=E6=94=B9=E4=B8=BA=20static=EF=BC=8C=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E6=A0=88=E6=BA=A2=E5=87=BA=20(=E6=A0=88=E4=BB=85=202KB)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit iot_mqtt_send_connect: opts(~85B) + buf[256] → static (~340B 省) iot_mqtt_send_subscribe: buf[256] → static (256B 省) iot_mqtt_publish: buf[2048] → static (2KB 省) 调用链 WCHNET_HandleGlobalInt → iot_mqtt_handle_sock_int → iot_mqtt_send_connect 会叠加 WCHNET 协议栈内部开销, 2048B 栈极易溢出导致 hard fault(现象:乱码+truncated日志+重启) --- .../BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c index 5ac1b67..387824b 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c @@ -77,10 +77,12 @@ static int iot_mqtt_send(const uint8_t *buf, uint16_t len) { /* 发送 MQTT CONNECT */ static int iot_mqtt_send_connect(void) { - MQTTPacket_connectData opts = MQTTPacket_connectData_initializer; - uint8_t buf[256]; + static MQTTPacket_connectData opts; // static: 避免 85字节栈开销 + static uint8_t buf[256]; // static: 避免 256字节栈开销 int len; + opts = MQTTPacket_connectData_initializer; + opts.MQTTVersion = 4; // MQTT 3.1.1 opts.keepAliveInterval = IOT_MQTT_KEEPALIVE_SEC; opts.cleansession = 1; @@ -112,7 +114,7 @@ static int iot_mqtt_send_connect(void) { /* 发送 MQTT SUBSCRIBE */ static int iot_mqtt_send_subscribe(void) { - uint8_t buf[256]; + static uint8_t buf[256]; // static: 避免栈溢出 int len; char topic[IOT_MQTT_TOPIC_MAX_LEN]; MQTTString topics[3]; @@ -142,7 +144,7 @@ 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) { - uint8_t buf[IOT_MQTT_SEND_BUF_LEN]; + static uint8_t buf[IOT_MQTT_SEND_BUF_LEN]; // static: 避免 2KB 栈溢出 int len; MQTTString mqtt_topic = MQTTString_initializer; mqtt_topic.cstring = (char *)topic;