fix(vd960DBN): SocketSend 连续3次失败 → 主动断连重连

root cause: WCHNET_SocketSend 返回 ret=0x11(sent=0) 后
  TCP超时要等~2分钟才触发 SINT_STAT_TIM_OUT, 期间所有
  loop_data/event_report 全丢, 平台收不到任何数据

fix: iot_mqtt_send 追踪连续失败计数, 3次→强制 close socket
  + g_iot_state=DISCONNECTED, 触发 iot_mqtt_poll 重连
  (成功一次就清零, 新连接也清零)
This commit is contained in:
wangfq
2026-07-23 17:14:49 +08:00
parent 29d1f6a783
commit 7c0de6c566
@@ -292,22 +292,33 @@ static int iot_make_topic(char *out, uint16_t out_len, const char *direction,
*===========================================================================*/ *===========================================================================*/
/* 发送 MQTT 报文到 broker — 循环重试防 WCHNET 部分发送 /* 发送 MQTT 报文到 broker — 循环重试防 WCHNET 部分发送
(2026-07-23: SocketSend 只发 520/607B → 残留拼到下行 → broker 见垃圾 → _raw+RST) */ (2026-07-23: SocketSend 只发 520/607B → 残留拼到下行 → broker 见垃圾 → _raw+RST)
连续失败追踪: ret=0x11 表示 TCP 发送队列满/连接断开, 3次连续失败 → 主动断连重连
(TCP超时要等~2分钟, 太慢了, 导致长时间丢数据) */
static int _iot_send_fail_cnt = 0; // 连续发送失败计数
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 total_sent = 0; uint16_t total_sent = 0;
int retries = 0; int retries = 0;
PRINT("IOT: SocketSend sock=%d len=%u\n", g_iot_socket, len);
while (total_sent < len) { while (total_sent < len) {
uint32_t slen = len - total_sent; uint32_t slen = len - total_sent;
uint8_t ret = WCHNET_SocketSend(g_iot_socket, (uint8_t *)(buf + total_sent), &slen); uint8_t ret = WCHNET_SocketSend(g_iot_socket, (uint8_t *)(buf + total_sent), &slen);
total_sent += (uint16_t)slen; total_sent += (uint16_t)slen;
if (slen == 0 || ret != WCHNET_ERR_SUCCESS || ++retries > 10) { if (slen == 0 || ret != WCHNET_ERR_SUCCESS || ++retries > 10) {
PRINT("IOT: SocketSend PARTIAL ret=0x%02X sent=%u/%u retries=%d\n", _iot_send_fail_cnt++;
ret, total_sent, len, retries); PRINT("IOT: SocketSend FAIL#%d ret=0x%02X sent=%u/%u\n",
_iot_send_fail_cnt, ret, total_sent, len);
if (_iot_send_fail_cnt >= 3) {
PRINT("IOT: SocketSend 3 consecutive failures, forcing reconnect\n");
WCHNET_SocketClose(g_iot_socket, TCP_CLOSE_NORMAL);
g_iot_socket = 0xFF;
g_iot_state = IOT_STATE_DISCONNECTED;
_iot_send_fail_cnt = 0;
}
return -1; return -1;
} }
} }
PRINT("IOT: SocketSend OK sent=%u\n", total_sent); _iot_send_fail_cnt = 0; // 成功一次就清零
return 0; return 0;
} }
@@ -838,6 +849,7 @@ void iot_mqtt_handle_sock_int(uint8_t socketid, uint8_t intstat) {
PRINT("IOT: TCP connected (sock=%d)\n", socketid); PRINT("IOT: TCP connected (sock=%d)\n", socketid);
g_iot_state = IOT_STATE_TCP_CONNECTED; g_iot_state = IOT_STATE_TCP_CONNECTED;
_iot_recv_len = 0; _iot_recv_len = 0;
_iot_send_fail_cnt = 0; // 新连接, 重置失败计数
iot_mqtt_send_connect(); // 在中断上下文发, 不延后到 poll iot_mqtt_send_connect(); // 在中断上下文发, 不延后到 poll
g_iot_state = IOT_STATE_MQTT_CONNECTING; g_iot_state = IOT_STATE_MQTT_CONNECTING;
} }