fix(vd960DBN): iot_mqtt_publish_sensor 栈溢出 — data_json[2KB] + payload[2KB] = 4KB 远超 2KB 栈
每次主循环调 iot_mqtt_publish_sensor 都分配 4KB 栈变量, 直接栈溢出踩烂全局变量(含 g_iot_dev_serial→clientId=空), 最终在 iot_mqtt_send_connect→WCHNET_SocketSend 时 hard fault。 改为 static 彻底消除栈压力。 另加 iot_mqtt_send 调试日志。
This commit is contained in:
@@ -71,7 +71,9 @@ static int iot_make_topic(char *out, uint16_t out_len, const char *direction,
|
|||||||
/* 发送 MQTT 报文到 broker */
|
/* 发送 MQTT 报文到 broker */
|
||||||
static int iot_mqtt_send(const uint8_t *buf, uint16_t len) {
|
static int iot_mqtt_send(const uint8_t *buf, uint16_t len) {
|
||||||
uint16_t slen = len;
|
uint16_t slen = len;
|
||||||
|
PRINT("IOT: SocketSend sock=%d len=%d\n", g_iot_socket, len);
|
||||||
uint8_t ret = WCHNET_SocketSend(g_iot_socket, (uint8_t *)buf, &slen);
|
uint8_t ret = WCHNET_SocketSend(g_iot_socket, (uint8_t *)buf, &slen);
|
||||||
|
PRINT("IOT: SocketSend ret=0x%02X sent=%d\n", ret, slen);
|
||||||
return (ret == WCHNET_ERR_SUCCESS) ? 0 : -1;
|
return (ret == WCHNET_ERR_SUCCESS) ? 0 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,7 +392,7 @@ void iot_mqtt_publish_sensor(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 构建 loop_data JSON */
|
/* 构建 loop_data JSON */
|
||||||
char data_json[2048];
|
static char data_json[2048]; // static: 避免 2KB 栈溢出
|
||||||
char *p = data_json;
|
char *p = data_json;
|
||||||
int remaining = sizeof(data_json);
|
int remaining = sizeof(data_json);
|
||||||
int written;
|
int written;
|
||||||
@@ -433,7 +435,7 @@ void iot_mqtt_publish_sensor(void) {
|
|||||||
snprintf(p, remaining, "]}");
|
snprintf(p, remaining, "]}");
|
||||||
|
|
||||||
/* 包装为通用消息格式 */
|
/* 包装为通用消息格式 */
|
||||||
char payload[IOT_MQTT_SEND_BUF_LEN];
|
static char payload[IOT_MQTT_SEND_BUF_LEN]; // static: 避免 2KB 栈溢出
|
||||||
snprintf(payload, sizeof(payload),
|
snprintf(payload, sizeof(payload),
|
||||||
"{\"msg_id\":%d,\"cmd\":\"loop_data\",\"ts\":%lu,\"data\":%s}",
|
"{\"msg_id\":%d,\"cmd\":\"loop_data\",\"ts\":%lu,\"data\":%s}",
|
||||||
++g_iot_msg_id, mstick() / 1000, data_json);
|
++g_iot_msg_id, mstick() / 1000, data_json);
|
||||||
|
|||||||
Reference in New Issue
Block a user