fix(vd960DBN): IoT MQTT 命令处理完善 — report_config + event_report ACK + initialize 走 IoT 路径
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 不回复
This commit is contained in:
@@ -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");
|
g_sub_code_enable.iot_enable ? "true" : "false");
|
||||||
iot_mqtt_publish(resp_topic, data_json, strlen(data_json), 1);
|
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) {
|
} else if (strcmp(cmd_str, "pwd_verify") == 0) {
|
||||||
// 验证密码
|
// 验证密码
|
||||||
char password[16] = {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 */
|
/* 处理 MQTT SUBACK */
|
||||||
static void iot_handle_suback(void) {
|
static void iot_handle_suback(void) {
|
||||||
PRINT("IOT: ← SUBACK, topics subscribed\n");
|
PRINT("IOT: ← SUBACK, topics subscribed\n");
|
||||||
g_iot_state = IOT_STATE_READY;
|
g_iot_state = IOT_STATE_READY;
|
||||||
_iot_reconnect_backoff = 0; // 连接成功, 重置退避
|
_iot_reconnect_backoff = 0; // 连接成功, 重置退避
|
||||||
|
|
||||||
// V1.03: 订阅成功后发布 initialize 告知服务器上线
|
// V1.03: 订阅成功后发布 initialize 告知服务器上线 (IoT 路径)
|
||||||
dev_initialize_pub();
|
iot_send_initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 处理收到的 MQTT 数据 */
|
/* 处理收到的 MQTT 数据 */
|
||||||
|
|||||||
Reference in New Issue
Block a user