feat(vd960DBN): TCP/MQTT log_* 命令支持快照流 stream=snapshot (2026-08-18)

- snapshot.c/h: 新增 snap_rec_to_json() — SnapRec 64B → JSON (channels 对齐 0xC0, variation 3B 符号扩展, misc_type 全枚举)
- tcp_json_srv.c: handle_log_stat/query/clear 加 stream 解析 + snapshot 分支
- iot_mqtt_srv.c: log_stat/query/clear 加 stream 解析 + snapshot 分支
- 事件流 count=0 按上限处理 (与 BLE 对齐); 快照 QUERY count≤2, CLEAR ~45ms
- 新增 gcc 隔离单测 tests/test_snap_to_json.c 34 断言全过
- devlog V4.1
This commit is contained in:
wangfq
2026-08-18 09:01:22 +08:00
parent e437dce556
commit f1c9358aad
6 changed files with 444 additions and 65 deletions
@@ -19,6 +19,7 @@
#include "simple_json.h"
#include "storage.h"
#include "offlog.h"
#include "snapshot.h"
#include "fault_diag.h"
#include <string.h>
#include <stdlib.h>
@@ -638,59 +639,120 @@ static void handle_report_config(uint8_t socket, uint32_t msg_id, const char *js
g_report_cfg.enable, g_report_cfg.interval, g_report_cfg.sensor_type);
}
/* 4.16 log_stat — 查询脱机事件日志统计 */
/* 4.16 log_stat — 查询脱机日志统计 (event/snapshot 流, 协议 V1.03) */
static void handle_log_stat(uint8_t socket, uint32_t msg_id, const char *json) {
char data_json[256];
uint32_t count = offlog_count();
uint32_t seq_last = offlog_seq_last();
uint32_t seq_first = (count > 0) ? (seq_last - count + 1) : 0;
snprintf(data_json, sizeof(data_json),
"{\"stream\":\"event\",\"enabled\":%s,\"boot_seq\":%lu,"
"\"count\":%lu,\"capacity\":%u,\"seq_first\":%lu,\"seq_last\":%lu}",
offlog_enabled() ? "true" : "false",
(unsigned long)offlog_boot_seq(),
(unsigned long)count, (unsigned)OFFLOG_MAX_RECORDS,
(unsigned long)seq_first, (unsigned long)seq_last);
char stream_buf[16];
uint32_t count, seq_last, seq_first, capacity;
const char *stream = "event";
memset(stream_buf, 0, sizeof(stream_buf));
simple_parse_json(json, "\"stream\"", stream_buf);
if (strcmp(stream_buf, "snapshot") == 0) stream = "snapshot";
if (strcmp(stream, "snapshot") == 0) {
count = snap_count();
seq_last = snap_seq_last();
seq_first = (count > 0) ? (seq_last - count + 1) : 0;
capacity = SNAP_MAX_RECORDS;
snprintf(data_json, sizeof(data_json),
"{\"stream\":\"snapshot\",\"enabled\":%s,\"boot_seq\":%lu,"
"\"count\":%lu,\"capacity\":%lu,\"seq_first\":%lu,\"seq_last\":%lu}",
snap_enabled() ? "true" : "false",
(unsigned long)snap_boot_seq(),
(unsigned long)count, (unsigned long)capacity,
(unsigned long)seq_first, (unsigned long)seq_last);
PRINT("JSON: log_stat(snapshot) count=%lu seq_first=%lu seq_last=%lu\n",
(unsigned long)count, (unsigned long)seq_first, (unsigned long)seq_last);
} else {
count = offlog_count();
seq_last = offlog_seq_last();
seq_first = (count > 0) ? (seq_last - count + 1) : 0;
capacity = OFFLOG_MAX_RECORDS;
snprintf(data_json, sizeof(data_json),
"{\"stream\":\"event\",\"enabled\":%s,\"boot_seq\":%lu,"
"\"count\":%lu,\"capacity\":%lu,\"seq_first\":%lu,\"seq_last\":%lu}",
offlog_enabled() ? "true" : "false",
(unsigned long)offlog_boot_seq(),
(unsigned long)count, (unsigned long)capacity,
(unsigned long)seq_first, (unsigned long)seq_last);
PRINT("JSON: log_stat count=%lu seq_first=%lu seq_last=%lu\n",
(unsigned long)count, (unsigned long)seq_first, (unsigned long)seq_last);
}
json_send_ok(socket, msg_id, "log_stat", data_json);
PRINT("JSON: log_stat count=%lu seq_first=%lu seq_last=%lu\n",
(unsigned long)count, (unsigned long)seq_first, (unsigned long)seq_last);
}
/* 4.17 log_query — 分页拉取脱机事件日志 (按全局序号, count≤4) */
/* 4.17 log_query — 分页拉取脱机日志 (event/snapshot 流; 事件≤4, 快照≤2, 协议 V1.03) */
static void handle_log_query(uint8_t socket, uint32_t msg_id, const char *json) {
char data_json[TCP_JSON_DATA_BUF_LEN];
char stream_buf[16];
uint32_t start_seq = json_get_uint_field(json, "\"start_seq\"");
uint32_t req_count = json_get_uint_field(json, "\"count\"");
uint32_t total = offlog_count();
uint32_t seq_last = offlog_seq_last();
uint32_t seq_first = (total > 0) ? (seq_last - total + 1) : 0;
if (req_count > OFFLOG_MAX_QUERY_RECORDS) req_count = OFFLOG_MAX_QUERY_RECORDS;
int is_snap = 0;
memset(stream_buf, 0, sizeof(stream_buf));
simple_parse_json(json, "\"stream\"", stream_buf);
if (strcmp(stream_buf, "snapshot") == 0) is_snap = 1;
int pos = snprintf(data_json, sizeof(data_json),
"{\"start_seq\":%lu,\"records\":[",
(unsigned long)start_seq);
uint32_t fetched = 0;
if (total > 0 && start_seq >= seq_first && start_seq <= seq_last) {
uint32_t idx = start_seq - seq_first; /* 逻辑索引 = 全局序号 - seq_first */
while (fetched < req_count && (idx + fetched) < total) {
OfflogEvt evt;
if (offlog_read_idx((uint16_t)(idx + fetched), &evt) != 0) break;
if (fetched > 0) pos += snprintf(data_json + pos, sizeof(data_json) - pos, ",");
pos += offlog_evt_to_json(&evt, data_json + pos, sizeof(data_json) - pos);
fetched++;
if (is_snap) {
uint32_t total = snap_count();
uint32_t seq_last = snap_seq_last();
uint32_t seq_first = (total > 0) ? (seq_last - total + 1) : 0;
if (req_count > SNAP_MAX_QUERY_RECORDS) req_count = SNAP_MAX_QUERY_RECORDS;
if (req_count == 0) req_count = SNAP_MAX_QUERY_RECORDS;
if (snap_enabled() && total > 0 && start_seq >= seq_first && start_seq <= seq_last) {
uint32_t idx = start_seq - seq_first; /* 逻辑索引 = 全局序号 - seq_first */
while (fetched < req_count && (idx + fetched) < total) {
SnapRec rec;
if (snap_read_idx(idx + fetched, &rec) != 0) break;
if (fetched > 0) pos += snprintf(data_json + pos, sizeof(data_json) - pos, ",");
pos += snap_rec_to_json(&rec, data_json + pos, sizeof(data_json) - pos);
fetched++;
}
}
PRINT("JSON: log_query(snapshot) start_seq=%lu req=%lu fetched=%lu\n",
(unsigned long)start_seq, (unsigned long)req_count, (unsigned long)fetched);
} else {
uint32_t total = offlog_count();
uint32_t seq_last = offlog_seq_last();
uint32_t seq_first = (total > 0) ? (seq_last - total + 1) : 0;
if (req_count > OFFLOG_MAX_QUERY_RECORDS) req_count = OFFLOG_MAX_QUERY_RECORDS;
if (req_count == 0) req_count = OFFLOG_MAX_QUERY_RECORDS;
if (total > 0 && start_seq >= seq_first && start_seq <= seq_last) {
uint32_t idx = start_seq - seq_first; /* 逻辑索引 = 全局序号 - seq_first */
while (fetched < req_count && (idx + fetched) < total) {
OfflogEvt evt;
if (offlog_read_idx((uint16_t)(idx + fetched), &evt) != 0) break;
if (fetched > 0) pos += snprintf(data_json + pos, sizeof(data_json) - pos, ",");
pos += offlog_evt_to_json(&evt, data_json + pos, sizeof(data_json) - pos);
fetched++;
}
}
PRINT("JSON: log_query start_seq=%lu req=%lu fetched=%lu\n",
(unsigned long)start_seq, (unsigned long)req_count, (unsigned long)fetched);
}
snprintf(data_json + pos, sizeof(data_json) - pos, "]}");
json_send_ok(socket, msg_id, "log_query", data_json);
PRINT("JSON: log_query start_seq=%lu req=%lu fetched=%lu\n",
(unsigned long)start_seq, (unsigned long)req_count, (unsigned long)fetched);
}
/* 4.18 log_clear — 清除脱机事件日志 (审计留痕, 阻塞 ~2.8s) */
/* 4.18 log_clear — 清除脱机日志 (event/snapshot 流, 审计留痕, 协议 V1.03) */
static void handle_log_clear(uint8_t socket, uint32_t msg_id, const char *json) {
offlog_clear();
char stream_buf[16];
memset(stream_buf, 0, sizeof(stream_buf));
simple_parse_json(json, "\"stream\"", stream_buf);
if (strcmp(stream_buf, "snapshot") == 0) {
snap_clear(); /* 阻塞 ~45ms (逻辑清除 + 当前写扇区) */
PRINT("JSON: log_clear(snapshot) done\n");
} else {
offlog_clear(); /* 阻塞 ~2.8s (63 扇区擦除) */
PRINT("JSON: log_clear done\n");
}
json_send_ok(socket, msg_id, "log_clear", NULL);
PRINT("JSON: log_clear done\n");
}
/* 4.19 loop_version_query — 获取地感MCU版本号 (CMD 0x4A) */