diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c index 0b10305..2d6180b 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c @@ -292,22 +292,33 @@ static int iot_make_topic(char *out, uint16_t out_len, const char *direction, *===========================================================================*/ /* 发送 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) { uint16_t total_sent = 0; int retries = 0; - PRINT("IOT: SocketSend sock=%d len=%u\n", g_iot_socket, len); while (total_sent < len) { uint32_t slen = len - total_sent; uint8_t ret = WCHNET_SocketSend(g_iot_socket, (uint8_t *)(buf + total_sent), &slen); total_sent += (uint16_t)slen; if (slen == 0 || ret != WCHNET_ERR_SUCCESS || ++retries > 10) { - PRINT("IOT: SocketSend PARTIAL ret=0x%02X sent=%u/%u retries=%d\n", - ret, total_sent, len, retries); + _iot_send_fail_cnt++; + 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; } } - PRINT("IOT: SocketSend OK sent=%u\n", total_sent); + _iot_send_fail_cnt = 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); g_iot_state = IOT_STATE_TCP_CONNECTED; _iot_recv_len = 0; + _iot_send_fail_cnt = 0; // 新连接, 重置失败计数 iot_mqtt_send_connect(); // 在中断上下文发, 不延后到 poll g_iot_state = IOT_STATE_MQTT_CONNECTING; }