fix(mqtt): 修复 loop_data>512B 溢出发垃圾包致 broker RST 重连风暴 (现场P0)
根因(非平台推测的环形缓冲/event_report队列):
mqtt_publish 的 mqttBuf=512B < loop_data(4通道)604B
→ MQTTSerialize_publish 返回 -2 不写 buf
→ len 为 uint32_t, -2 变 42.9亿
→ WCHNET 把清零缓冲+越界相邻全局(temp_guide=report_config→report_c)
当 520B 垃圾包发出 → broker 见非法类型0x00 RST → 重连风暴
修复(net_srv.c):
- MAX_MQTTBUF_LEN 512→1024 (容纳 604B loop_data)
- mqtt_publish: len uint32→int + 守卫 if(len<=0)return (序列化失败绝不发残缓冲)
- keepalive 9→60 (报告建议, 9s过激)
event_report 协议违规修复(iot_mqtt_srv.c):
- 重连重发保持原 msg_id/ts (V1.04 §5.3-2), 不再作废换号致平台去重失效
验证: tests/test_mqtt_publish_overflow.c 复现旧垃圾包+验证守卫/大缓冲;
tests/test_event_report.c T8 断言重连同 msg_id. 均全过.
遗留: ts=上电秒数非Unix时间戳, 待定方案(设备无RTC/SNTP)
Refs: docs/incidents/2026-07-15-DC045A49718F-protocol-error.md
This commit is contained in:
@@ -210,9 +210,17 @@ static void iot_evt_process(void) {
|
||||
uint8_t ready = (g_iot_state == IOT_STATE_READY);
|
||||
uint32_t now = mstick();
|
||||
|
||||
if (ready && !_was_ready) { // 重连沿: 作废未决包, 解除挂起, 全量重报
|
||||
_evt_pend_id = 0;
|
||||
_evt_gaveup = 0;
|
||||
if (ready && !_was_ready) { // 重连沿
|
||||
_evt_gaveup = 0;
|
||||
if (_evt_pend_id) {
|
||||
/* 有未决包 → 用【原 msg_id/原 ts】立即重发 (协议 V1.04 §5.3-2:
|
||||
跨重连也须保持同 msg_id, 否则平台 (sn,msg_id) 去重失效致重复入库) */
|
||||
_evt_retry = 0; // 重连后重试计数归零, 不吃掉本次
|
||||
iot_evt_send(_evt_pend_id, _evt_pend_ts, _evt_pend_n);
|
||||
_evt_sent_ms = now;
|
||||
_was_ready = ready;
|
||||
return;
|
||||
}
|
||||
}
|
||||
_was_ready = ready;
|
||||
if (!ready) return;
|
||||
|
||||
Reference in New Issue
Block a user