|
|
@@ -1,206 +1,157 @@
|
|
|
# -*- coding: utf-8 -*-
|
|
|
"""
|
|
|
-升级标志(update_flag)三通道改造的宿主校验(本机无 arm-none-eabi)
|
|
|
+升级机制现状检查(原 upgrade_flag 三通道机制已废弃后的替代校验)
|
|
|
|
|
|
-[A] gcc -fsyntax-only: 抽 hmi.c 里 Upgrade_Flag_Set / Upgrade_Flag_Clear 两个函数 +
|
|
|
- BKP 宏定义, 配最小桩编译, 验证语法与 device_status.update_flag 字段真实存在
|
|
|
-[B] hmi.c / dtu_uart2.c 大括号配平
|
|
|
-[C] 关键符号与调用点核对
|
|
|
-[D] 逻辑风险项核对(防死循环 / 防误清零 / 三通道齐全)
|
|
|
+背景:
|
|
|
+ 2026-09-24 曾用"EasyFlash + 参数快照 + BKP_DR3"三通道持久化 update_flag,
|
|
|
+ 靠"置标志 -> 重启 -> 开机读回来进 ymodem"。2026-09-24 晚改为
|
|
|
+ **收到 update 指令直接在本线程进 ymodem**, 不再置标志/不重启;
|
|
|
+ 随后 hmi.c 里的 Upgrade_Flag_Set/Clear 定义、BKP 宏、相关 msh 命令被移除。
|
|
|
|
|
|
-中间文件只写 .txt, 不在工程树留 .c/.h(CDT 会扫进去导致 multiple definition)
|
|
|
+ 函数删了但调用点还在 = 链接期 undefined reference, 这是最容易漏的一类问题。
|
|
|
+ 本脚本就是盯住它。
|
|
|
+
|
|
|
+[A] 确认 Upgrade_Flag_Set/Clear 的定义已移除(机制废弃)
|
|
|
+[B] ★确认全工程无对它们的"代码级"调用(只允许注释里提到)
|
|
|
+[C] 升级入口核对: update 分支的停机 + 进 ymodem 链路完整
|
|
|
+[D] 括号配平
|
|
|
"""
|
|
|
-import io, os, re, subprocess, sys
|
|
|
+import os, re, sys
|
|
|
|
|
|
-ROOT = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", ".."))
|
|
|
-GCC = r"D:\Strawberry\c\bin\gcc.exe"
|
|
|
-TMP = os.path.join(ROOT, ".workbuddy", "tmp_upg.txt")
|
|
|
-OUT = os.path.join(ROOT, ".workbuddy", "upgrade_out.txt")
|
|
|
+ROOT = r"D:\RT-ThreadStudio\workspace\IOT_CBD_BZY_CTRL\Application_cbd_apm32f1"
|
|
|
+OUT = os.path.join(ROOT, ".workbuddy", "upgrade_state_out.txt")
|
|
|
+SCAN = ["applications", "drivers", os.path.join("packages", "syswatch-latest")]
|
|
|
|
|
|
lines = []
|
|
|
def log(s=""):
|
|
|
lines.append(s)
|
|
|
|
|
|
-def read(p):
|
|
|
- with io.open(os.path.join(ROOT, p), "r", encoding="utf-8", errors="ignore") as f:
|
|
|
+def read(*p):
|
|
|
+ with open(os.path.join(ROOT, *p), "r", encoding="utf-8", errors="ignore") as f:
|
|
|
return f.read()
|
|
|
|
|
|
-def grab_func(text, sig):
|
|
|
- """从 sig 所在行开始, 大括号配平切出整个函数体。
|
|
|
- ★ sig 到 '{' 之间不能出现 ';', 否则会命中前向声明"""
|
|
|
- m = re.search(sig + r"[^;{]*\{", text)
|
|
|
- if not m:
|
|
|
- return None
|
|
|
- i = m.start()
|
|
|
- j = m.end() - 1
|
|
|
- depth, k = 0, j
|
|
|
- while k < len(text):
|
|
|
- if text[k] == "{":
|
|
|
- depth += 1
|
|
|
- elif text[k] == "}":
|
|
|
- depth -= 1
|
|
|
- if depth == 0:
|
|
|
- return text[i:k + 1]
|
|
|
- k += 1
|
|
|
- return None
|
|
|
-
|
|
|
-def balance(text):
|
|
|
- pairs = [("{", "}"), ("(", ")")]
|
|
|
- res = {}
|
|
|
- for a, b in pairs:
|
|
|
- res[a] = text.count(a) - text.count(b)
|
|
|
- return res
|
|
|
-
|
|
|
-# ---------------- 抽真实结构体 ----------------
|
|
|
-ctrl = read(os.path.join("applications", "control.h"))
|
|
|
-try:
|
|
|
- a = ctrl.index("typedef struct")
|
|
|
- b = ctrl.index("}Device_Status_typedef;") + len("}Device_Status_typedef;")
|
|
|
- struct_def = ctrl[a:b]
|
|
|
-except ValueError:
|
|
|
- raise SystemExit("未找到 Device_Status_typedef 定义")
|
|
|
-
|
|
|
-hmi = read(os.path.join("applications", "hmi.c"))
|
|
|
-dtu = read(os.path.join("applications", "dtu_uart2.c"))
|
|
|
-hmih = read(os.path.join("applications", "hmi.h"))
|
|
|
-
|
|
|
-# ---------------- [A] gcc 语法检查 ----------------
|
|
|
-fset = grab_func(hmi, r"void\s+Upgrade_Flag_Set\s*\(void\)")
|
|
|
-fclear = grab_func(hmi, r"void\s+Upgrade_Flag_Clear\s*\(void\)")
|
|
|
-
|
|
|
-macros = ""
|
|
|
-# 只抓 #define, 不能抓 #ifndef/#endif —— 单独抽出来会留下未闭合的条件编译
|
|
|
-for name in ("BKP_BASE", "UPG_BKP_DR3", "UPG_BKP_DR4", "UPG_BKP_MAGIC", "UPG_TRY_MAX"):
|
|
|
- mm = re.search(r"#define\s+" + name + r"\b.*", hmi)
|
|
|
- if mm:
|
|
|
- macros += mm.group(0) + "\n"
|
|
|
-if "BKP_BASE" not in macros:
|
|
|
- macros += "#define BKP_BASE (0x40006C00UL)\n"
|
|
|
-if "UPG_BKP_DR3" not in macros:
|
|
|
- macros += "#define UPG_BKP_DR3 (*(volatile unsigned short *)(0x40006C00UL + 4U*3U))\n"
|
|
|
-if "UPG_BKP_DR4" not in macros:
|
|
|
- macros += "#define UPG_BKP_DR4 (*(volatile unsigned short *)(0x40006C00UL + 4U*4U))\n"
|
|
|
-if "UPG_BKP_MAGIC" not in macros:
|
|
|
- macros += "#define UPG_BKP_MAGIC (0x5AA5U)\n"
|
|
|
-
|
|
|
-STUB = r"""
|
|
|
-#include <stdint.h>
|
|
|
-#include <stddef.h>
|
|
|
-#include <string.h>
|
|
|
-/* CMSIS: __IO 即 volatile(真实工程来自芯片头文件) */
|
|
|
-#define __IO volatile
|
|
|
-#define LOG_I(...) ((void)0)
|
|
|
-#define LOG_E(...) ((void)0)
|
|
|
-#define LOG_W(...) ((void)0)
|
|
|
-#define LOG_D(...) ((void)0)
|
|
|
-typedef unsigned int rt_uint32_t;
|
|
|
-typedef unsigned short rt_uint16_t;
|
|
|
-typedef unsigned char rt_uint8_t;
|
|
|
-typedef signed int rt_int32_t;
|
|
|
-typedef signed short rt_int16_t;
|
|
|
-typedef int rt_err_t;
|
|
|
-typedef rt_uint32_t rt_size_t;
|
|
|
-#define RT_NULL ((void*)0)
|
|
|
-#define RT_EOK 0
|
|
|
-int ef_set_env_blob(const char *key, const void *value_buf, size_t buf_len);
|
|
|
-size_t ef_get_env_blob(const char *key, void *value_buf, size_t buf_len, size_t *saved_len);
|
|
|
-void param_mark_dirty(void);
|
|
|
-int param_flush_force(void);
|
|
|
-typedef struct { int dummy; } param_persist_t;
|
|
|
-""" + struct_def + r"""
|
|
|
-extern Device_Status_typedef device_status;
|
|
|
-"""
|
|
|
+def strip_comments(t):
|
|
|
+ t = re.sub(r"/\*.*?\*/", "", t, flags=re.S)
|
|
|
+ t = re.sub(r"//[^\n]*", "", t)
|
|
|
+ return t
|
|
|
+
|
|
|
+def walk_c():
|
|
|
+ for sub in SCAN:
|
|
|
+ base = os.path.join(ROOT, sub)
|
|
|
+ if not os.path.isdir(base):
|
|
|
+ continue
|
|
|
+ for dirpath, dirnames, filenames in os.walk(base):
|
|
|
+ for f in filenames:
|
|
|
+ if f.endswith((".c", ".h")):
|
|
|
+ yield os.path.join(dirpath, f)
|
|
|
+
|
|
|
+log("=" * 66)
|
|
|
+log("升级机制现状检查(三通道机制废弃后)")
|
|
|
+log("=" * 66)
|
|
|
+all_ok = True
|
|
|
+
|
|
|
+# ---------------- [A] 定义已移除 ----------------
|
|
|
+log("")
|
|
|
+log("[A] Upgrade_Flag_Set/Clear 的定义是否已移除")
|
|
|
+defs = []
|
|
|
+for p in walk_c():
|
|
|
+ txt = strip_comments(open(p, "r", encoding="utf-8", errors="ignore").read())
|
|
|
+ for m in re.finditer(r"\bvoid\s+(Upgrade_Flag_(?:Set|Clear))\s*\([^;]*\)\s*\{", txt):
|
|
|
+ defs.append((m.group(1), os.path.relpath(p, ROOT)))
|
|
|
+if defs:
|
|
|
+ for n, p in defs:
|
|
|
+ log(" **FAIL**: 仍有定义 %s 在 %s" % (n, p))
|
|
|
+ all_ok = False
|
|
|
+else:
|
|
|
+ log(" 未发现函数定义 -> 机制确已废弃 OK")
|
|
|
|
|
|
-src = STUB + "\n" + macros + "\n" + (fset or "") + "\n" + (fclear or "") + "\n"
|
|
|
+# 声明是否也清了
|
|
|
+hmi_h_body = strip_comments(read("applications", "hmi.h"))
|
|
|
+want = ("void Upgrade_Flag_Set(void);" in hmi_h_body
|
|
|
+ or "void Upgrade_Flag_Clear(void);" in hmi_h_body)
|
|
|
+log(" %-52s %s" % ("hmi.h 里的悬空声明已清除", "OK" if not want else "**FAIL**"))
|
|
|
+if want:
|
|
|
+ all_ok = False
|
|
|
|
|
|
-ok_a = False
|
|
|
-if not fset or not fclear:
|
|
|
- log("[A] 未抽到 Upgrade_Flag_Set / Clear 函数体 -> FAIL")
|
|
|
-else:
|
|
|
- with io.open(TMP, "w", encoding="utf-8") as f:
|
|
|
- f.write(src)
|
|
|
- r = subprocess.run([GCC, "-x", "c", "-fsyntax-only", "-std=gnu99",
|
|
|
- "-Wall", "-Wextra", "-Wno-unused-parameter", TMP],
|
|
|
- capture_output=True, text=True, encoding="utf-8", errors="ignore")
|
|
|
- log("[A] gcc 语法检查 exit=%d" % r.returncode)
|
|
|
- if r.stderr.strip():
|
|
|
- log(r.stderr.strip()[:1200])
|
|
|
- ok_a = (r.returncode == 0)
|
|
|
- log(" -> %s" % ("PASS" if ok_a else "FAIL"))
|
|
|
-
|
|
|
-# ---------------- [B] 括号配平 ----------------
|
|
|
+# ---------------- [B] 无代码级调用 ----------------
|
|
|
log("")
|
|
|
-log("[B] 括号配平")
|
|
|
-ok_b = True
|
|
|
-for name, txt in (("hmi.c", hmi), ("dtu_uart2.c", dtu)):
|
|
|
- b = balance(txt)
|
|
|
- bad = {k: v for k, v in b.items() if v != 0}
|
|
|
- log(" %-12s %s" % (name, bad if bad else "OK"))
|
|
|
- if bad:
|
|
|
- ok_b = False
|
|
|
-log(" -> %s" % ("PASS" if ok_b else "FAIL"))
|
|
|
-
|
|
|
-# ---------------- [C] 符号与调用点 ----------------
|
|
|
+log("[B] 全工程是否还有对它们的代码级调用(注释不算)")
|
|
|
+hits = []
|
|
|
+for p in walk_c():
|
|
|
+ raw = open(p, "r", encoding="utf-8", errors="ignore").read()
|
|
|
+ code = strip_comments(raw)
|
|
|
+ for m in re.finditer(r"\b(Upgrade_Flag_(?:Set|Clear))\s*\(", code):
|
|
|
+ # 排除函数定义本身
|
|
|
+ pre = code[max(0, m.start() - 40): m.start()]
|
|
|
+ if re.search(r"\bvoid\s*$", pre):
|
|
|
+ continue
|
|
|
+ ln = code[:m.start()].count("\n") + 1
|
|
|
+ hits.append((os.path.relpath(p, ROOT), ln, m.group(1)))
|
|
|
+if hits:
|
|
|
+ for p, ln, n in hits:
|
|
|
+ log(" **FAIL**: %s:%d 调用了 %s (但定义已删 -> 链接会失败)" % (p, ln, n))
|
|
|
+ all_ok = False
|
|
|
+else:
|
|
|
+ log(" 没有任何代码级调用 -> 不会有 undefined reference OK")
|
|
|
+
|
|
|
+# ---------------- [C] 升级入口链路 ----------------
|
|
|
log("")
|
|
|
-log("[C] 符号与调用点核对")
|
|
|
-checks = [
|
|
|
- ("hmi.h 声明 Upgrade_Flag_Set",
|
|
|
- "void Upgrade_Flag_Set(void);" in hmih),
|
|
|
- ("hmi.h 声明 Upgrade_Flag_Clear",
|
|
|
- "void Upgrade_Flag_Clear(void);" in hmih),
|
|
|
- ("hmi.c 屏幕 hmi_update 走 Upgrade_Flag_Set",
|
|
|
- re.search(r'hmi_update"\)!=NULL\)\s*\{[^}]*Upgrade_Flag_Set\(\);', hmi, re.S) is not None),
|
|
|
- ("dtu_uart2.c update 走 Upgrade_Flag_Set",
|
|
|
- "Upgrade_Flag_Set();" in dtu),
|
|
|
- ("dtu_uart2.c 已删旧的裸 ef_set_env_blob(\"update_flag\")",
|
|
|
- 'ef_set_env_blob("update_flag", &device_status.update_flag' not in dtu),
|
|
|
- ("dtu_uart2.c include hmi.h(可见声明)",
|
|
|
- '#include "hmi.h"' in dtu),
|
|
|
- ("BKP 通道宏已定义(DR3/DR4)",
|
|
|
- "#define UPG_BKP_DR3" in hmi and "#define UPG_BKP_DR4" in hmi),
|
|
|
- ("连续失败上限 UPG_TRY_MAX 已定义",
|
|
|
- "#define UPG_TRY_MAX" in hmi),
|
|
|
- ("开机裁决用了三通道 OR",
|
|
|
- "up_request = (up_flag_ef || up_flag_snap || up_flag_bkp);" in hmi),
|
|
|
- ("进入 ymodem 前先清标志",
|
|
|
- re.search(r"Upgrade_Flag_Clear\(\);\s*\n\s*if \(!nor_flash0_is_ready", hmi) is not None),
|
|
|
+log("[C] 升级入口(remote update)链路核对")
|
|
|
+dtu = read("applications", "dtu_uart2.c")
|
|
|
+i = dtu.find('strstr(item_cmd->valuestring,"update")')
|
|
|
+branch, code_only = None, ""
|
|
|
+if i >= 0:
|
|
|
+ j = dtu.find("{", i)
|
|
|
+ d, k = 0, j
|
|
|
+ while k < len(dtu):
|
|
|
+ if dtu[k] == "{":
|
|
|
+ d += 1
|
|
|
+ elif dtu[k] == "}":
|
|
|
+ d -= 1
|
|
|
+ if d == 0:
|
|
|
+ break
|
|
|
+ k += 1
|
|
|
+ branch = dtu[dtu.rfind("else if", 0, i):k + 1]
|
|
|
+ code_only = strip_comments(branch)
|
|
|
+
|
|
|
+ck = [
|
|
|
+ ("找到 update 分支", branch is not None),
|
|
|
+ ("停掉 uart2 上的 dtu_send_data", "rt_thread_delete(dtu_send_data)" in (code_only or "")),
|
|
|
+ ("停掉 uart2 周期上报 timer_uart2", "rt_timer_stop(timer_uart2)" in (code_only or "")),
|
|
|
+ ("调 thread_stop_all_for_ota() 停全部业务线程",
|
|
|
+ "thread_stop_all_for_ota()" in (code_only or "")),
|
|
|
+ ("分支内无 rt_kprintf(会污染 ymodem)", "rt_kprintf" not in (code_only or "")),
|
|
|
+ ("末行进 ymodem_ota(1,0)", "ymodem_ota(1,0)" in (code_only or "")),
|
|
|
+ ("不再走'置标志 + 重启'", "rt_hw_cpu_reset" not in (code_only or "")),
|
|
|
+ ("control.h 已声明 thread_stop_all_for_ota",
|
|
|
+ "int thread_stop_all_for_ota(void);" in strip_comments(read("applications", "control.h"))),
|
|
|
+ ("control.c 已实现 thread_stop_all_for_ota",
|
|
|
+ "int thread_stop_all_for_ota(void)" in strip_comments(read("applications", "control.c"))),
|
|
|
]
|
|
|
-ok_c = True
|
|
|
-for name, ok in checks:
|
|
|
- log(" %-52s %s" % (name, "OK" if ok else "MISSING"))
|
|
|
+for desc, ok in ck:
|
|
|
+ log(" %-52s %s" % (desc, "OK" if ok else "**FAIL**"))
|
|
|
if not ok:
|
|
|
- ok_c = False
|
|
|
-log(" -> %s" % ("PASS" if ok_c else "FAIL"))
|
|
|
+ all_ok = False
|
|
|
|
|
|
-# ---------------- [D] 逻辑风险 ----------------
|
|
|
+# ---------------- [D] 配平 ----------------
|
|
|
log("")
|
|
|
-log("[D] 逻辑风险项")
|
|
|
-# 默认配置分支里不应再有孤立的无条件清零
|
|
|
-m2 = re.search(r"device_status\.ds = 1;\s*//设备状态\s*\n(.*?)device_status\.update_flag\s*=", hmi, re.S)
|
|
|
-still_forced = bool(re.search(r"device_status\.update_flag = 0;\s*\n\s*/\*孢子仪\*/", hmi))
|
|
|
-log(" 默认配置分支仍无条件清零 update_flag: %s (期望 False)" % still_forced)
|
|
|
-# 快照回滚不再覆盖 update_flag
|
|
|
-or_back = "if(uf != 0)" in hmi
|
|
|
-log(" param_unpack 后保留 update_flag: %s (期望 True)" % or_back)
|
|
|
-# 三通道置位
|
|
|
-set_all = ("ef_set_env_blob" in (fset or "")) and ("param_flush_force" in (fset or "")) \
|
|
|
- and ("UPG_BKP_DR3" in (fset or ""))
|
|
|
-log(" 置位覆盖 easyflash+快照+BKP: %s (期望 True)" % set_all)
|
|
|
-ok_d = (not still_forced) and or_back and set_all
|
|
|
-log(" -> %s" % ("PASS" if ok_d else "FAIL"))
|
|
|
+log("[D] 括号配平")
|
|
|
+for rel in (("applications", "hmi.c"), ("applications", "hmi.h"),
|
|
|
+ ("applications", "control.c"), ("applications", "control.h"),
|
|
|
+ ("applications", "dtu_uart2.c")):
|
|
|
+ t = open(os.path.join(ROOT, *rel), "r", encoding="utf-8", errors="ignore").read()
|
|
|
+ bal = t.count("{") - t.count("}")
|
|
|
+ ok = (bal == 0)
|
|
|
+ log(" %-24s {}-}=%d %s" % (rel[-1], bal, "OK" if ok else "**FAIL**"))
|
|
|
+ if not ok:
|
|
|
+ all_ok = False
|
|
|
|
|
|
log("")
|
|
|
-log("=== 汇总: A=%s B=%s C=%s D=%s ===" % (
|
|
|
- "PASS" if ok_a else "FAIL", "PASS" if ok_b else "FAIL",
|
|
|
- "PASS" if ok_c else "FAIL", "PASS" if ok_d else "FAIL"))
|
|
|
-
|
|
|
-try:
|
|
|
- os.remove(TMP)
|
|
|
-except OSError:
|
|
|
- pass
|
|
|
+log("=" * 66)
|
|
|
+log("总体: %s" % ("全部通过" if all_ok else "存在失败项"))
|
|
|
+log("=" * 66)
|
|
|
|
|
|
-with io.open(OUT, "w", encoding="utf-8") as f:
|
|
|
+with open(OUT, "w", encoding="utf-8") as f:
|
|
|
f.write("\n".join(lines))
|
|
|
-
|
|
|
print("\n".join(lines))
|
|
|
-sys.exit(0 if (ok_a and ok_b and ok_c and ok_d) else 1)
|
|
|
+sys.exit(0 if all_ok else 1)
|