From 67976eca8e25bb927bc6453af8772af300af7e82 Mon Sep 17 00:00:00 2001 From: wangfq Date: Thu, 23 Jul 2026 12:24:09 +0800 Subject: [PATCH] =?UTF-8?q?fix(vd960DBN):=20IoT=20MQTT=20=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E5=A4=84=E7=90=86=E5=AE=8C=E5=96=84=20=E2=80=94=20rep?= =?UTF-8?q?ort=5Fconfig=20+=20event=5Freport=20ACK=20+=20initialize=20?= =?UTF-8?q?=E8=B5=B0=20IoT=20=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit issues: 1. iot_handle_suback 调 dev_initialize_pub() → 旧 mqttBuf 路径, 与 IoT 栈混淆 2. report_config 命令无人处理 → 平台收到 code=4 unsupported 3. event_report ACK (code=0) 被当 unsupported command → 重发闭环断裂 fix: 1. 新增 iot_send_initialize() — IoT 路径, 用 iot_mqtt_publish 2. 新增 report_config 处理 — 解析 enable/interval/once/env_eval/sensor_type 3. 新增 event_report ACK 识别 — code=0 → iot_evt_handle_ack; ACK 不回复 --- .../APP/iot_mqtt_srv.c | 61 ++++++++++++++++++- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c index 90806db..f03016d 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c @@ -429,6 +429,48 @@ static void iot_handle_publish(const char *topic, uint8_t *payload, int payload_ g_sub_code_enable.iot_enable ? "true" : "false"); iot_mqtt_publish(resp_topic, data_json, strlen(data_json), 1); + } else if (strcmp(cmd_str, "report_config") == 0) { + /* 平台下发配置 → 更新 g_report_cfg, 回 ACK */ + char data[512] = {0}; + simple_parse_json(json, "\"data\"", data); + if (strlen(data) > 0) { + char tmp2[64]; + memset(tmp2, 0, sizeof(tmp2)); + simple_parse_json(data, "\"enable\"", tmp2); + if (strlen(tmp2) > 0) g_report_cfg.enable = (strcmp(tmp2, "true") == 0); + memset(tmp2, 0, sizeof(tmp2)); + simple_parse_json(data, "\"interval\"", tmp2); + if (strlen(tmp2) > 0) g_report_cfg.interval = (uint16_t)strtoul(tmp2, NULL, 10); + memset(tmp2, 0, sizeof(tmp2)); + simple_parse_json(data, "\"once\"", tmp2); + if (strlen(tmp2) > 0) g_report_cfg.once = (strcmp(tmp2, "true") == 0); + memset(tmp2, 0, sizeof(tmp2)); + simple_parse_json(data, "\"env_eval\"", tmp2); + if (strlen(tmp2) > 0) g_report_cfg.env_eval = (strcmp(tmp2, "true") == 0); + memset(tmp2, 0, sizeof(tmp2)); + simple_parse_json(data, "\"sensor_type\"", tmp2); + if (strlen(tmp2) > 0) g_report_cfg.sensor_type = (uint8_t)strtoul(tmp2, NULL, 10); + } + char resp[256]; + snprintf(resp, sizeof(resp), + "{\"msg_id\":%lu,\"cmd\":\"report_config\"," + "\"ts\":%lu,\"code\":0,\"msg\":\"success\"}", + msg_id, dev_time_now()); + iot_mqtt_publish(resp_topic, resp, strlen(resp), 1); + + } else if (strcmp(cmd_str, "event_report") == 0) { + /* 平台对 event_report 的 ACK: 检查 code 字段 */ + char tmp3[64] = {0}; + int code = -1; + simple_parse_json(json, "\"code\"", tmp3); + if (strlen(tmp3) > 0) code = (int)strtol(tmp3, NULL, 10); + if (code == 0) { + iot_evt_handle_ack(msg_id, 0); + } else { + iot_evt_handle_ack(msg_id, code); + } + // ACK 不回复 (避免乒乓) + } else if (strcmp(cmd_str, "pwd_verify") == 0) { // 验证密码 char password[16] = {0}; @@ -477,14 +519,29 @@ static void iot_handle_publish(const char *topic, uint8_t *payload, int payload_ } } +/* 发送 initialize 上线消息 (IoT 路径) */ +static void iot_send_initialize(void) { + char payload[512]; + char topic[IOT_MQTT_TOPIC_MAX_LEN]; + snprintf(payload, sizeof(payload), + "{\"msg_id\":%lu,\"cmd\":\"initialize\",\"ts\":%lu," + "\"data\":{\"dev_serial\":\"%s\",\"model\":\"%s\"," + "\"hard_ver\":\"%s\",\"soft_ver\":\"%s\"," + "\"extra_info\":{\"code\":\"0\",\"csq\":\"0\",\"location\":\"\"}}}", + (unsigned long)(g_iot_msg_id + 1), dev_time_now(), + g_iot_dev_serial, PRODUCT_MODEL, HARDWARE_VER, FIRMWARE_VER); + snprintf(topic, sizeof(topic), "dld960/%s/dev", g_iot_dev_serial); + iot_mqtt_publish(topic, payload, strlen(payload), 0); +} + /* 处理 MQTT SUBACK */ static void iot_handle_suback(void) { PRINT("IOT: ← SUBACK, topics subscribed\n"); g_iot_state = IOT_STATE_READY; _iot_reconnect_backoff = 0; // 连接成功, 重置退避 - // V1.03: 订阅成功后发布 initialize 告知服务器上线 - dev_initialize_pub(); + // V1.03: 订阅成功后发布 initialize 告知服务器上线 (IoT 路径) + iot_send_initialize(); } /* 处理收到的 MQTT 数据 */