feat(mqtt): 设备时钟同步方案B — 平台经 report_config 下发 Unix ts (协议V1.05)

设备无RTC/SNTP, 原上行 ts=mstick()/1000(上电秒数)。方案B:
- net_srv.c 新增 dev_time_sync()/dev_time_now(): 门槛≥1600000000挡上电秒数,
  已校准返回真Unix, 未校准退回上电秒数; 无符号相减处理mstick 49.7天回绕
- manage_mqtt_recv_message 从下行命令信封 ts 校准 (report_config主同步点)
- 全部上行 ts (14处命令响应+initialize+loop_data+event_report) 改 dev_time_now()
  heartbeat uptime 字段保留 mstick()/1000 (运行时长非时间戳)
- 协议 V1.05 §2.3 时间同步 + report_config 兼任同步点职责
- tests/test_dev_time_sync.c 6组全过

平台侧配合: 收到 initialize 后下发 report_config 时 ts 填当前 Unix 时间
This commit is contained in:
wangfq
2026-07-15 15:25:44 +08:00
parent e3eaa111fe
commit 3e28174b40
6 changed files with 167 additions and 22 deletions
@@ -245,7 +245,7 @@ static void iot_evt_process(void) {
/* 发新包: 队列头 N 条合并 */
_evt_pend_n = (_evt_count > IOT_EVT_MAX_PER_PKT) ? IOT_EVT_MAX_PER_PKT : _evt_count;
_evt_pend_id = ++_evt_msg_id;
_evt_pend_ts = now / 1000;
_evt_pend_ts = dev_time_now(); // 首发时刻 Unix 时间(已校准), 重发保持不刷新
_evt_retry = 0;
iot_evt_send(_evt_pend_id, _evt_pend_ts, _evt_pend_n);
_evt_sent_ms = now;
@@ -412,7 +412,7 @@ static void iot_handle_publish(const char *topic, uint8_t *payload, int payload_
"\"model\":\"%s\",\"product_code\":\"960001\","
"\"sub_code\":{\"net\":%s,\"iot\":%s},"
"\"bus\":{\"bus1\":0,\"bus2\":0,\"bus3\":0,\"bus4\":0}}}",
msg_id, mstick() / 1000,
msg_id, dev_time_now(),
g_dev_number_str, HARDWARE_VER, FIRMWARE_VER, PRODUCT_MODEL,
g_sub_code_enable.net_enable ? "true" : "false",
g_sub_code_enable.iot_enable ? "true" : "false");
@@ -443,7 +443,7 @@ static void iot_handle_publish(const char *topic, uint8_t *payload, int payload_
snprintf(resp, sizeof(resp),
"{\"msg_id\":%lu,\"cmd\":\"pwd_verify\","
"\"ts\":%lu,\"code\":0,\"msg\":\"success\"}",
msg_id, mstick() / 1000);
msg_id, dev_time_now());
iot_mqtt_publish(resp_topic, resp, strlen(resp), 1);
PRINT("IOT: Auth success via MQTT\n");
} else {
@@ -451,7 +451,7 @@ static void iot_handle_publish(const char *topic, uint8_t *payload, int payload_
snprintf(resp, sizeof(resp),
"{\"msg_id\":%lu,\"cmd\":\"pwd_verify\","
"\"ts\":%lu,\"code\":2,\"msg\":\"password incorrect\"}",
msg_id, mstick() / 1000);
msg_id, dev_time_now());
iot_mqtt_publish(resp_topic, resp, strlen(resp), 1);
}
@@ -461,7 +461,7 @@ static void iot_handle_publish(const char *topic, uint8_t *payload, int payload_
snprintf(resp, sizeof(resp),
"{\"msg_id\":%lu,\"cmd\":\"%s\","
"\"ts\":%lu,\"code\":4,\"msg\":\"unsupported command\"}",
msg_id, cmd_str, mstick() / 1000);
msg_id, cmd_str, dev_time_now());
iot_mqtt_publish(resp_topic, resp, strlen(resp), 1);
}
}
@@ -708,7 +708,7 @@ void iot_mqtt_publish_sensor(void) {
/* Wrap and publish — V1.01 双主题模型: dld960/{sn}/dev */
snprintf(payload, sizeof(payload),
"{\"msg_id\":%d,\"cmd\":\"loop_data\",\"ts\":%lu,\"data\":%s}",
++g_iot_msg_id, mstick() / 1000, data_json);
++g_iot_msg_id, dev_time_now(), data_json);
{
char topic[IOT_MQTT_TOPIC_MAX_LEN];
@@ -736,7 +736,7 @@ static void iot_send_heartbeat(void) {
"\"ts\":%lu,\"data\":{"
"\"uptime\":%lu,\"loop_status\":[%s,%s,%s,%s],"
"\"net_status\":true,\"iot_status\":true}}",
++g_iot_msg_id, mstick() / 1000,
++g_iot_msg_id, dev_time_now(),
mstick() / 1000,
loop_ok[0] ? "true" : "false", loop_ok[1] ? "true" : "false",
loop_ok[2] ? "true" : "false", loop_ok[3] ? "true" : "false");