#!/usr/bin/env python3 """从 dbn_ble_srv.c 提取脱机日志 3 case 文本, 供 test_ble_offlog.c 隔离验证。 用法: python3 extract_offlog_cases.py 输出: ./offlog_cases_embedded.c (在 tests/ 目录下运行) 原理: 测的是源文件真实代码而非拷贝 — 提取 `case CMD_DBN_OFFLOG_STAT` 到 `default:` 之间的文本 (含 STAT/QUERY/CLEAR 3 case), 嵌入测试框架编译。 """ import os SRC = os.path.join(os.path.dirname(__file__), "../BLE/OnlyUpdateApp_Peripheral/APP/dbn_ble_srv.c") OUT = os.path.join(os.path.dirname(__file__), "offlog_cases_embedded.c") with open(SRC, "rb") as f: content = f.read() text = content.decode("utf-8", errors="replace").replace("\r\n", "\n") start = text.find("case CMD_DBN_OFFLOG_STAT") end = text.find("default:\n", start) if start == -1 or end == -1: raise SystemExit("ERROR: 未找到 offlog 3 case (确认 dbn_ble_srv.c 已修改)") cases = text[start:end] for c in ("CMD_DBN_OFFLOG_STAT", "CMD_DBN_OFFLOG_QUERY", "CMD_DBN_OFFLOG_CLEAR"): if c not in cases: raise SystemExit(f"ERROR: 提取内容缺少 {c}") with open(OUT, "w") as f: f.write(cases) print(f"OK: {OUT} ({len(cases)} bytes, 3 cases)")