From 8f1bbf75f87f49bf006cfb87cc4e44ec691f1bcb Mon Sep 17 00:00:00 2001 From: wangfq Date: Thu, 23 Jul 2026 15:21:59 +0800 Subject: [PATCH] =?UTF-8?q?refactor(vd960DBN):=20=E7=9C=8B=E9=97=A8?= =?UTF-8?q?=E7=8B=97=E7=B2=BE=E7=AE=80=20=E2=80=94=20=E5=8E=BB=E6=8E=89?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E5=93=8D=E5=BA=94=E8=B6=85=E6=97=B6,=20?= =?UTF-8?q?=E5=8F=AA=E4=BF=9D=E7=95=99=E7=A1=AC=E4=BB=B6=20IWDG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit heartbeat 是单向设备上报, 平台不回复; 用 '3×心跳无平台回复→重启' 逻辑不成立。保活依赖三层已有机制: MQTT层: PINGREQ/PINGRESP (60s) → broker不回 → TCP超时 TCP层: KeepAlive → WCHNET SINT_STAT_DISCONNECT/TIMEOUT 硬件层: IWDG (4s) → 主循环卡死 → 硬件复位 --- .../APP/include/iot_mqtt_srv.h | 2 -- .../APP/iot_mqtt_srv.c | 29 ++++--------------- 2 files changed, 5 insertions(+), 26 deletions(-) diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/iot_mqtt_srv.h b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/iot_mqtt_srv.h index 21b1ed7..27fc883 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/iot_mqtt_srv.h +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/iot_mqtt_srv.h @@ -21,7 +21,6 @@ #define IOT_MQTT_RECONNECT_MIN_MS 5000 // 最小重连间隔 #define IOT_MQTT_RECONNECT_MAX_MS 60000 // 最大重连间隔 (指数退避上限) #define IOT_MQTT_HEARTBEAT_MS 60000 // 心跳上报周期 (60s) -#define IOT_MQTT_WDT_TIMEOUT_MS 180000 // 平台响应看门狗: 3×心跳=180s 无回复→重启 #define IOT_MQTT_RECV_BUF_LEN 1024 // 接收缓冲区 (单次交互 ≤1024) #define IOT_MQTT_SEND_BUF_LEN 1024 // 发送缓冲区 (单次交互 ≤1024) #define IOT_MQTT_TOPIC_MAX_LEN 128 // Topic 最大长度 @@ -54,6 +53,5 @@ void iot_mqtt_handle_sock_int(uint8_t socketid, uint8_t intstat); // Socket void iot_mqtt_publish_sensor(void); // 推送传感器数据到 MQTT void iot_evt_handle_ack(uint32_t msg_id, int code); // event_report 平台应答入口 (V1.04) void iot_watchdog_kick(void); // 喂硬件 IWDG (主循环调用) -void iot_watchdog_check(void); // 平台响应超时检查 (180s无回复→重启) #endif /* __IOT_MQTT_SRV_H__ */ diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c index abf27e8..4b1475f 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c @@ -42,7 +42,6 @@ static uint32_t _iot_last_heartbeat = 0; // 上次心跳时刻 (ms) static uint32_t _iot_reconnect_deadline = 0; static uint32_t _iot_reconnect_backoff = 0; static uint32_t _iot_connect_start = 0; // TCP connect 开始时刻 -static uint32_t _iot_last_recv_ms = 0; // 上次收到平台回复时刻 (看门狗追踪) /*=========================================================================== * 传感器数据缓存 & 上报间隔控制 @@ -209,7 +208,6 @@ void iot_evt_handle_ack(uint32_t msg_id, int code) { return; } /* 确认成功 → 弹出本包事件 */ - _iot_last_recv_ms = mstick(); // 看门狗: 平台对 event_report 的 ACK 也算响应 _evt_head = (_evt_head + _evt_pend_n) % IOT_EVT_QUEUE_DEPTH; _evt_count -= _evt_pend_n; PRINT("EVT: ack ok, %d events dequeued (left=%d)\n", _evt_pend_n, _evt_count); @@ -398,8 +396,6 @@ static void iot_handle_publish(const char *topic, uint8_t *payload, int payload_ PRINT("IOT: PUBLISH topic=%s payload=%s\n", topic, json); - _iot_last_recv_ms = mstick(); // 看门狗: 收到平台任意消息即刷新 - /* 提取 msg_id 和 cmd */ char tmp[256]; uint32_t msg_id = 0; @@ -555,7 +551,6 @@ static void iot_handle_suback(void) { PRINT("IOT: ← SUBACK, topics subscribed\n"); g_iot_state = IOT_STATE_READY; _iot_reconnect_backoff = 0; // 连接成功, 重置退避 - _iot_last_recv_ms = mstick(); // 看门狗: 刚连上, 重置平台响应计时 // V1.03: 订阅成功后发布 initialize 告知服务器上线 (IoT 路径) iot_send_initialize(); @@ -905,12 +900,13 @@ void iot_mqtt_handle_sock_int(uint8_t socketid, uint8_t intstat) { } /*=========================================================================== - * 看门狗 — 硬件 IWDG + 平台响应超时 - * IWDG: ~4s LSI超时, 主循环喂狗, 防死循环/硬故障 - * 平台狗: 3×心跳周期(180s)无平台回复 → NVIC_SystemReset + * 硬件看门狗 IWDG — 主循环卡死保护 + * LSI≈40kHz, Prescaler=256, Reload=625 → ~4s 超时 + * 保活链路: MQTT PINGREQ/PINGRESP → TCP KeepAlive → IWDG + * (heartbeat 是单向设备上报, 平台不回复, 不能用作存活检测) *===========================================================================*/ -/* IWDG 初始化: LSI≈40kHz, Prescaler=256 → 156Hz tick, Reload=625 → 4s 超时 */ +/* IWDG 初始化 */ void iot_watchdog_init(void) { IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); IWDG_SetPrescaler(IWDG_Prescaler_256); @@ -925,27 +921,12 @@ void iot_watchdog_kick(void) { IWDG_ReloadCounter(); } -/* 平台响应超时检查 — iot_mqtt_poll 中调用 */ -void iot_watchdog_check(void) { - if (g_iot_state != IOT_STATE_READY) return; - uint32_t now = mstick(); - if (now - _iot_last_recv_ms > IOT_MQTT_WDT_TIMEOUT_MS) { - PRINT("IOT: WATCHDOG no platform reply for %lu ms (>%u ms), resetting...\n", - (unsigned long)(now - _iot_last_recv_ms), IOT_MQTT_WDT_TIMEOUT_MS); - /* 不再喂狗, IWDG 4s 后自动复位; 或直接 NVIC_SystemReset */ - NVIC_SystemReset(); - } -} - /*=========================================================================== * 主循环轮询 *===========================================================================*/ void iot_mqtt_poll(void) { iot_watchdog_kick(); // 喂硬件 IWDG - /* 平台响应超时检查 */ - iot_watchdog_check(); - /* 断线重连 */ if (g_iot_state == IOT_STATE_DISCONNECTED && g_iot_socket == 0xFF) { if (_iot_reconnect_deadline == 0 || mstick() > _iot_reconnect_deadline) {