feat(vd960DBN): offlog 协议导出命令分发 — log_stat/log_query/log_clear 落地
协议先行 (08893d5, MQTT V1.06 + TCP JSON V1.02) 的固件侧实现:
- offlog 新增导出 API: enabled/seq_last/type_str/evt_to_json
(seq_first=seq_last-count+1, idx=start_seq-seq_first, count≤4)
- MQTT iot_handle_publish 三分支: log_stat/log_query/log_clear
(log_query 响应 static buf 防大栈; log_clear 阻塞~2.8s 主循环可接受)
- TCP JSON 三 handler + cmd 表 (需鉴权)
- 单测新增 test_export_json (8 例全过): boot rst 大端/coil sub/evt_retry/data:null/seq 公式
- devlog V3.9
This commit is contained in:
@@ -232,6 +232,64 @@ static void test_time_anchor_exact(void)
|
||||
printf("PASS test_time_anchor_exact\n");
|
||||
}
|
||||
|
||||
/* 测试8: 协议导出 — enabled/seq_last/type_str/evt_to_json (log_stat/log_query 数据源) */
|
||||
static void test_export_json(void)
|
||||
{
|
||||
OfflogEvt e;
|
||||
char buf[256];
|
||||
|
||||
reset_flash();
|
||||
offlog_init();
|
||||
CHECK(offlog_enabled() == 1);
|
||||
CHECK(offlog_seq_last() == 0); /* 尚无记录 */
|
||||
|
||||
offlog_boot(0x80000000UL); /* IWDG 复位 → rst=2147483648 */
|
||||
CHECK(offlog_count() == 1);
|
||||
CHECK(offlog_seq_last() == 1);
|
||||
CHECK(offlog_read_idx(0, &e) == 0);
|
||||
CHECK(strcmp(offlog_type_str(e.type), "boot") == 0);
|
||||
offlog_evt_to_json(&e, buf, sizeof(buf));
|
||||
CHECK(strstr(buf, "\"type\":\"boot\"") != NULL);
|
||||
CHECK(strstr(buf, "\"data\":{\"rst\":2147483648}") != NULL);
|
||||
|
||||
/* coil: payload = sub(1=进) ch(2) value(97, 大端) */
|
||||
{
|
||||
uint8_t p[6] = {1, 2, 0, 0, 0, 97};
|
||||
offlog_evt(OFFLOG_EVT_COIL, p, 6);
|
||||
}
|
||||
CHECK(offlog_read_idx(1, &e) == 0);
|
||||
CHECK(strcmp(offlog_type_str(e.type), "coil") == 0);
|
||||
offlog_evt_to_json(&e, buf, sizeof(buf));
|
||||
CHECK(strstr(buf, "\"type\":\"coil\"") != NULL);
|
||||
CHECK(strstr(buf, "\"sub\":\"car_enter\"") != NULL);
|
||||
CHECK(strstr(buf, "\"ch\":2") != NULL);
|
||||
CHECK(strstr(buf, "\"value\":97") != NULL);
|
||||
|
||||
/* evt_retry: msg_id=5 retry=2 */
|
||||
{
|
||||
uint8_t p[5] = {0, 0, 0, 5, 2};
|
||||
offlog_evt(OFFLOG_EVT_EVT_RETRY, p, 5);
|
||||
}
|
||||
CHECK(offlog_read_idx(2, &e) == 0);
|
||||
offlog_evt_to_json(&e, buf, sizeof(buf));
|
||||
CHECK(strstr(buf, "\"msg_id\":5") != NULL);
|
||||
CHECK(strstr(buf, "\"retry\":2") != NULL);
|
||||
|
||||
/* 无 payload 事件 → data:null */
|
||||
offlog_iot_ready();
|
||||
CHECK(offlog_read_idx(3, &e) == 0);
|
||||
offlog_evt_to_json(&e, buf, sizeof(buf));
|
||||
CHECK(strstr(buf, "\"type\":\"iot_ready\"") != NULL);
|
||||
CHECK(strstr(buf, "\"data\":null") != NULL);
|
||||
|
||||
/* seq_first 公式: seq_last - count + 1 (4 条: seq 1..4) */
|
||||
CHECK(offlog_seq_last() == 4);
|
||||
CHECK(offlog_count() == 4);
|
||||
printf("PASS test_export_json (seq_first=%lu seq_last=%lu)\n",
|
||||
(unsigned long)(offlog_seq_last() - offlog_count() + 1),
|
||||
(unsigned long)offlog_seq_last());
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_fresh_init();
|
||||
@@ -241,6 +299,7 @@ int main(void)
|
||||
test_clear();
|
||||
test_power_loss_mid_sector();
|
||||
test_time_anchor_exact();
|
||||
test_export_json();
|
||||
|
||||
if (failures) {
|
||||
printf("\n%d FAILURE(S)\n", failures);
|
||||
|
||||
Reference in New Issue
Block a user