dsh-stm32 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +127 -29
- package/README_EN.md +125 -29
- package/client/client.js +682 -0
- package/docs/development-notes.md +1385 -5
- package/docs/logic-audit.md +607 -0
- package/docs/reading-guide.md +738 -0
- package/lib/client.js +682 -0
- package/lib/commands/register.js +214 -12
- package/lib/data/chips.js +134 -4
- package/lib/data/clock-f1.js +158 -0
- package/lib/data/cmsis-sources.js +316 -0
- package/lib/data/f1-ram.generated.js +34 -0
- package/lib/data/firmware-packages.js +352 -0
- package/lib/data/install-guides.js +190 -0
- package/lib/data/search-roots.js +135 -15
- package/lib/data/stm32f1.js +186 -0
- package/lib/data/toolchain-config.js +310 -0
- package/lib/engine/build.js +121 -16
- package/lib/engine/cache.js +336 -0
- package/lib/engine/clock-plan.js +608 -0
- package/lib/engine/cmake-sources.js +428 -0
- package/lib/engine/code-patch.js +18 -6
- package/lib/engine/debug-session.js +163 -4
- package/lib/engine/debug.js +84 -19
- package/lib/engine/doctor.js +395 -4
- package/lib/engine/fetch.js +287 -0
- package/lib/engine/firmware-install.js +668 -0
- package/lib/engine/flash.js +145 -15
- package/lib/engine/fs-longpath.js +107 -0
- package/lib/engine/generate-cache.js +118 -0
- package/lib/engine/generate-script.js +30 -8
- package/lib/engine/generate.js +296 -14
- package/lib/engine/hal-chip.js +230 -0
- package/lib/engine/ioc-plan.js +47 -8
- package/lib/engine/msys2-install.js +1870 -0
- package/lib/engine/probe.js +57 -6
- package/lib/engine/project-export.js +129 -0
- package/lib/engine/register-clock.js +132 -0
- package/lib/engine/register-linker.js +129 -0
- package/lib/engine/register-template.js +449 -0
- package/lib/engine/regs.js +18 -4
- package/lib/engine/route-plan.js +553 -0
- package/lib/engine/scaffold.js +303 -72
- package/lib/engine/subprocess-runner.js +2 -0
- package/lib/engine/tar.js +250 -0
- package/lib/engine/text-file.js +150 -0
- package/lib/engine/thirdparty.js +243 -0
- package/lib/engine/tool-env.js +51 -0
- package/lib/engine/toolchain-gate.js +386 -0
- package/lib/engine/vscode-config.js +99 -0
- package/lib/engine/web-api.js +375 -0
- package/lib/engine/web-routes.js +100 -0
- package/lib/engine/xcube-catalog.js +102 -0
- package/lib/engine/xcube.js +254 -0
- package/lib/engine/zip.js +142 -0
- package/lib/index.js +44 -2
- package/lib/skill/stm32-workflow.js +3 -3
- package/lib/tools/build.js +55 -3
- package/lib/tools/cache.js +250 -0
- package/lib/tools/code-patch.js +29 -2
- package/lib/tools/debug-session.js +56 -14
- package/lib/tools/debug.js +43 -2
- package/lib/tools/doctor.js +16 -3
- package/lib/tools/env-install.js +472 -0
- package/lib/tools/env-probe.js +6 -2
- package/lib/tools/firmware-install.js +255 -0
- package/lib/tools/flash.js +41 -2
- package/lib/tools/generate.js +96 -10
- package/lib/tools/ioc-apply.js +27 -11
- package/lib/tools/ioc-inspect.js +4 -2
- package/lib/tools/ioc-plan.js +209 -11
- package/lib/tools/project-create.js +229 -21
- package/lib/tools/project-dir.js +95 -0
- package/lib/tools/project-export.js +100 -0
- package/lib/tools/regs.js +82 -7
- package/lib/tools/route-plan.js +467 -0
- package/package.json +11 -4
- package/templates/register/CMakeLists.txt +52 -0
- package/templates/register/Core/Src/main.c +72 -0
- package/templates/scaffold/openocd.cfg +8 -0
- package/templates/scaffold/vscode/launch.json +8 -6
- package/templates/scaffold/vscode/settings.json +5 -11
package/lib/tools/build.js
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { runBuild } from "../engine/build.js";
|
|
7
7
|
import { releaseProbeFor } from "../engine/debug-session.js";
|
|
8
|
+
import { ensureToolchain, gateReportNotes } from "../engine/toolchain-gate.js";
|
|
9
|
+
import { resolveProjectDir } from "./project-dir.js";
|
|
8
10
|
export const BUILD_TOOL = "stm32_build";
|
|
9
11
|
const memorySchema = {
|
|
10
12
|
type: "object",
|
|
@@ -41,6 +43,10 @@ const reportSchema = {
|
|
|
41
43
|
errors: { type: "array", items: { type: "string" } },
|
|
42
44
|
warningCount: { type: "number" },
|
|
43
45
|
memoryUsage: { type: "array", items: memorySchema },
|
|
46
|
+
converted: {
|
|
47
|
+
type: "array",
|
|
48
|
+
items: { type: "object", properties: { kind: { type: "string" }, path: { type: "string" }, size: { type: "number" } }, required: ["kind", "path", "size"], additionalProperties: false },
|
|
49
|
+
},
|
|
44
50
|
elf: elfSchema,
|
|
45
51
|
message: { type: "string" },
|
|
46
52
|
logTail: { type: "string" },
|
|
@@ -59,6 +65,12 @@ export const outputSchema = {
|
|
|
59
65
|
},
|
|
60
66
|
required: ["mode", "jobId", "message"],
|
|
61
67
|
};
|
|
68
|
+
/** 门禁档位参数:三个工具共用一份说明文字,直接内联进各自的 parameters。 */
|
|
69
|
+
const ENV_CHECK_SCHEMA = {
|
|
70
|
+
type: "string",
|
|
71
|
+
enum: ["block", "warn", "off"],
|
|
72
|
+
description: "工具链检查:block 缺件就拒绝(默认)、warn 只警告、off 不检查",
|
|
73
|
+
};
|
|
62
74
|
function formatReport(report) {
|
|
63
75
|
const lines = [];
|
|
64
76
|
lines.push((report.ok ? "编译成功" : "编译失败") + ":" + report.message);
|
|
@@ -82,6 +94,14 @@ function formatReport(report) {
|
|
|
82
94
|
}
|
|
83
95
|
return lines;
|
|
84
96
|
}
|
|
97
|
+
/** 门禁拒绝时的报告:字段按 outputSchema.required 逐项给值,别让宿主报 "is not lossless JSON"。 */
|
|
98
|
+
function blockedReport(projectDir, gate) {
|
|
99
|
+
return {
|
|
100
|
+
ok: false, projectDir, buildDir: "", command: "", durationMs: 0,
|
|
101
|
+
errors: [], warningCount: 0, memoryUsage: [],
|
|
102
|
+
message: gate.message, logTail: "", notes: gateReportNotes(gate),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
85
105
|
export function registerBuild(ctx) {
|
|
86
106
|
ctx.tools.register({
|
|
87
107
|
name: BUILD_TOOL,
|
|
@@ -94,8 +114,15 @@ export function registerBuild(ctx) {
|
|
|
94
114
|
buildDir: { type: "string", description: "显式构建目录;缺省自动推断(优先含 Debug 的 preset 目录)。" },
|
|
95
115
|
target: { type: "string", description: "只构建某个 CMake target(例如 demo.elf)。" },
|
|
96
116
|
clean: { type: "boolean", description: "先 clean 再编译(全量,慢)。" },
|
|
117
|
+
artifacts: {
|
|
118
|
+
type: "array",
|
|
119
|
+
items: { type: "string", enum: ["elf", "hex", "bin"] },
|
|
120
|
+
description: "编译成功后额外转出的产物格式(默认只有 elf):hex 给产线烧录器 / CubeProgrammer,bin 给串口 ISP 与 OTA 镜像。转换失败不判编译失败,只写 warning。",
|
|
121
|
+
additionalProperties: false,
|
|
122
|
+
},
|
|
97
123
|
foreground: { type: "boolean", description: "同步返回完整报告(默认 false,走后台 job)。" },
|
|
98
124
|
binDirs: { type: "array", items: { type: "string" }, description: "工具链目录候选,优先于默认值/环境变量。" },
|
|
125
|
+
envCheck: ENV_CHECK_SCHEMA,
|
|
99
126
|
timeoutMs: { type: "number", description: "超时毫秒数,默认 300000(5 分钟)。" },
|
|
100
127
|
},
|
|
101
128
|
required: [],
|
|
@@ -117,7 +144,21 @@ export function registerBuild(ctx) {
|
|
|
117
144
|
const subprocess = ctx.get("subprocess");
|
|
118
145
|
if (subprocess === undefined)
|
|
119
146
|
return { mode: "report", jobId: "", message: "宿主缺少 subprocess 服务,无法编译" };
|
|
120
|
-
const
|
|
147
|
+
const resolution = await resolveProjectDir(ctx, args.projectDir, exec);
|
|
148
|
+
const projectDir = resolution.projectDir;
|
|
149
|
+
// 门禁:编译前先确认这台机器有 gcc / CMake / Ninja。缺件且 envCheck=block 时连 cmake 都不起,
|
|
150
|
+
// 免得用户在一屏 CMake 报错里找原因;warn 档只带一句警告继续。
|
|
151
|
+
const gate = await ensureToolchain(ctx, {
|
|
152
|
+
capability: "build",
|
|
153
|
+
...(args.binDirs === undefined ? {} : { binDirs: args.binDirs }),
|
|
154
|
+
envCheck: args.envCheck,
|
|
155
|
+
signal: exec.signal,
|
|
156
|
+
});
|
|
157
|
+
if (!gate.ok && gate.mode === "block") {
|
|
158
|
+
const blocked = blockedReport(projectDir, gate);
|
|
159
|
+
return { mode: "report", jobId: "", message: blocked.message, report: blocked };
|
|
160
|
+
}
|
|
161
|
+
const gateNote = gate.ok ? null : gate.message;
|
|
121
162
|
// 编译本身不碰探针,但 openocd 常驻会让 ninja 的进程环境变复杂;先让会话让出来更稳。
|
|
122
163
|
const released = await releaseProbeFor("stm32_build");
|
|
123
164
|
const jobs = ctx.get("jobs");
|
|
@@ -127,12 +168,17 @@ export function registerBuild(ctx) {
|
|
|
127
168
|
...(args.buildDir === undefined ? {} : { buildDir: args.buildDir }),
|
|
128
169
|
...(args.target === undefined ? {} : { target: args.target }),
|
|
129
170
|
...(args.clean === undefined ? {} : { clean: args.clean }),
|
|
171
|
+
...(args.artifacts === undefined ? {} : { artifacts: args.artifacts }),
|
|
130
172
|
...(args.binDirs === undefined ? {} : { binDirs: args.binDirs }),
|
|
131
173
|
...(args.timeoutMs === undefined ? {} : { timeoutMs: args.timeoutMs }),
|
|
132
174
|
signal: exec.signal,
|
|
133
175
|
});
|
|
176
|
+
if (gateNote !== null)
|
|
177
|
+
report.notes.unshift(gateNote);
|
|
134
178
|
if (released !== null)
|
|
135
179
|
report.notes.push(released);
|
|
180
|
+
if (resolution.note !== null)
|
|
181
|
+
report.notes.push(resolution.note);
|
|
136
182
|
return { mode: "report", jobId: "", message: report.message, report };
|
|
137
183
|
}
|
|
138
184
|
const controller = new AbortController();
|
|
@@ -141,12 +187,18 @@ export function registerBuild(ctx) {
|
|
|
141
187
|
label: "stm32_build " + projectDir,
|
|
142
188
|
...(exec.agent === undefined ? {} : { owner: exec.agent.id }),
|
|
143
189
|
run: (handle) => {
|
|
144
|
-
|
|
190
|
+
if (gateNote !== null)
|
|
191
|
+
handle.append(gateNote + "\n");
|
|
192
|
+
// A35:后台模式也要看得到探针让出的说明。
|
|
193
|
+
if (released !== null)
|
|
194
|
+
handle.append(released + "\n");
|
|
195
|
+
handle.append("开始编译:" + projectDir + "\n" + (resolution.note === null ? "" : resolution.note + "\n"));
|
|
145
196
|
const done = runBuild(subprocess, {
|
|
146
197
|
projectDir,
|
|
147
198
|
...(args.buildDir === undefined ? {} : { buildDir: args.buildDir }),
|
|
148
199
|
...(args.target === undefined ? {} : { target: args.target }),
|
|
149
200
|
...(args.clean === undefined ? {} : { clean: args.clean }),
|
|
201
|
+
...(args.artifacts === undefined ? {} : { artifacts: args.artifacts }),
|
|
150
202
|
...(args.binDirs === undefined ? {} : { binDirs: args.binDirs }),
|
|
151
203
|
...(args.timeoutMs === undefined ? {} : { timeoutMs: args.timeoutMs }),
|
|
152
204
|
signal: controller.signal,
|
|
@@ -164,7 +216,7 @@ export function registerBuild(ctx) {
|
|
|
164
216
|
};
|
|
165
217
|
},
|
|
166
218
|
});
|
|
167
|
-
return { mode: "job", jobId, message: "已在后台开始编译 " + projectDir };
|
|
219
|
+
return { mode: "job", jobId, message: "已在后台开始编译 " + projectDir + (resolution.note === null ? "" : "(" + resolution.note + ")") };
|
|
168
220
|
},
|
|
169
221
|
});
|
|
170
222
|
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L3:stm32_cache —— 看/验/拉/清 下载缓存。
|
|
3
|
+
*
|
|
4
|
+
* 缓存里放的是"不属于插件、但建工程要用"的东西:目前是 CMSIS 设备头 + 启动文件 + 内核头。
|
|
5
|
+
* 用户要能自己看见"下到哪了、对不对、占多大、能不能清",所以这四个动作必须是一等公民,
|
|
6
|
+
* 而不是藏在 error 信息里。
|
|
7
|
+
*/
|
|
8
|
+
import { cacheRootInfo, checkCacheEntry, describeCacheBytes, pruneLegacyEntries, readCacheIndex, removeCacheEntry, resolveCacheEntry, } from "../engine/cache.js";
|
|
9
|
+
import { ensureCmsisTemplate } from "../engine/register-template.js";
|
|
10
|
+
export const CACHE_TOOL = "stm32_cache";
|
|
11
|
+
const entrySchema = {
|
|
12
|
+
type: "object",
|
|
13
|
+
additionalProperties: false,
|
|
14
|
+
properties: {
|
|
15
|
+
id: { type: "string" },
|
|
16
|
+
kind: { type: "string" },
|
|
17
|
+
family: { type: "string" },
|
|
18
|
+
label: { type: "string" },
|
|
19
|
+
root: { type: "string" },
|
|
20
|
+
files: { type: "number" },
|
|
21
|
+
bytes: { type: "number" },
|
|
22
|
+
createdAt: { type: "string" },
|
|
23
|
+
status: { type: "string" },
|
|
24
|
+
detail: { type: "string" },
|
|
25
|
+
},
|
|
26
|
+
required: ["id", "kind", "family", "label", "root", "files", "bytes", "createdAt"],
|
|
27
|
+
};
|
|
28
|
+
export const outputSchema = {
|
|
29
|
+
type: "object",
|
|
30
|
+
additionalProperties: false,
|
|
31
|
+
properties: {
|
|
32
|
+
action: { type: "string" },
|
|
33
|
+
ok: { type: "boolean" },
|
|
34
|
+
cacheRoot: { type: "string" },
|
|
35
|
+
indexPath: { type: "string" },
|
|
36
|
+
origin: { type: "string" },
|
|
37
|
+
totalBytes: { type: "number" },
|
|
38
|
+
entries: { type: "array", items: entrySchema },
|
|
39
|
+
message: { type: "string" },
|
|
40
|
+
},
|
|
41
|
+
required: ["action", "ok", "cacheRoot", "indexPath", "origin", "totalBytes", "entries", "message"],
|
|
42
|
+
};
|
|
43
|
+
function listEntries(index, verify) {
|
|
44
|
+
return index.entries.map((entry) => {
|
|
45
|
+
const base = {
|
|
46
|
+
id: entry.id,
|
|
47
|
+
kind: entry.kind,
|
|
48
|
+
family: entry.family,
|
|
49
|
+
label: entry.label,
|
|
50
|
+
root: entry.root,
|
|
51
|
+
files: entry.files.length,
|
|
52
|
+
bytes: entry.totalBytes,
|
|
53
|
+
createdAt: entry.createdAt,
|
|
54
|
+
};
|
|
55
|
+
if (!verify)
|
|
56
|
+
return base;
|
|
57
|
+
const health = checkCacheEntry(entry);
|
|
58
|
+
return {
|
|
59
|
+
...base,
|
|
60
|
+
status: health.ok ? "ok" : "broken",
|
|
61
|
+
detail: health.ok
|
|
62
|
+
? "文件齐全、大小对得上"
|
|
63
|
+
: "缺 " + String(health.missing.length) + " 个、大小不对 " + String(health.changed.length) + " 个:" + [...health.missing, ...health.changed].slice(0, 5).join(";"),
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
export function registerCache(ctx) {
|
|
68
|
+
ctx.tools.register({
|
|
69
|
+
name: CACHE_TOOL,
|
|
70
|
+
description: "看 / 验 / 拉 / 清 插件的下载缓存(默认 %LOCALAPPDATA%\\dsh-stm32\\cache,可用 DSH_STM32_CACHE 覆盖):寄存器版模板(CMSIS 设备头 + 启动文件 + 内核头)第一次用时从网上下载到这里,之后复用。action=list 列出条目、verify 逐文件校验、fetch 按型号拉一次(已缓存则直接复用)、purge 删条目(不给 id 就清空)。",
|
|
71
|
+
parameters: {
|
|
72
|
+
type: "object",
|
|
73
|
+
additionalProperties: false,
|
|
74
|
+
properties: {
|
|
75
|
+
action: { type: "string", enum: ["list", "verify", "fetch", "gc", "purge"], description: "list(默认)/ verify / fetch / gc(清掉已被新 id 取代的旧条目)/ purge" },
|
|
76
|
+
id: { type: "string", description: "条目 id,例如 cmsis:F1:103xb(purge/verify 用);也认 cmsis:F1:startup_stm32f103xb、F1:B、STM32F103C8T6 这些写法。" },
|
|
77
|
+
mcu: { type: "string", description: "芯片型号,例如 STM32F103C8T6(fetch 用)。" },
|
|
78
|
+
family: { type: "string", description: "只给族时写这里,例如 F1(fetch 用;按中容量档处理)。" },
|
|
79
|
+
confirm: { type: "boolean", description: "purge 且不给 id 时(清空整个缓存)必须为 true;只删一条用 id 即可。" },
|
|
80
|
+
},
|
|
81
|
+
required: [],
|
|
82
|
+
},
|
|
83
|
+
timeoutMs: 5 * 60_000,
|
|
84
|
+
isConcurrencySafe: () => false,
|
|
85
|
+
output: {
|
|
86
|
+
schema: outputSchema,
|
|
87
|
+
render: (_args, value) => {
|
|
88
|
+
const typed = value;
|
|
89
|
+
const lines = [typed.message];
|
|
90
|
+
lines.push("缓存目录:" + typed.cacheRoot + "(来源:" + typed.origin + ")");
|
|
91
|
+
lines.push("索引:" + typed.indexPath + ";共 " + String(typed.entries.length) + " 个条目、" + describeCacheBytes(typed.totalBytes));
|
|
92
|
+
for (const entry of typed.entries) {
|
|
93
|
+
lines.push("· " + entry.id + "(" + entry.kind + ")" + describeCacheBytes(entry.bytes) + "、" + String(entry.files) + " 个文件" + (entry.status === undefined ? "" : " → " + entry.status));
|
|
94
|
+
if (entry.detail !== undefined)
|
|
95
|
+
lines.push(" " + entry.detail);
|
|
96
|
+
}
|
|
97
|
+
return [{ type: "text", text: lines.join("\n") }];
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
async execute(rawArgs, exec) {
|
|
101
|
+
const args = rawArgs ?? {};
|
|
102
|
+
const action = (args.action ?? "list").trim().toLowerCase();
|
|
103
|
+
const info = cacheRootInfo(process.env);
|
|
104
|
+
const { index, error } = readCacheIndex(process.env);
|
|
105
|
+
const empty = (message, ok = false) => ({
|
|
106
|
+
action,
|
|
107
|
+
ok,
|
|
108
|
+
cacheRoot: info.root,
|
|
109
|
+
indexPath: info.indexPath,
|
|
110
|
+
origin: info.origin,
|
|
111
|
+
totalBytes: 0,
|
|
112
|
+
entries: [],
|
|
113
|
+
message,
|
|
114
|
+
});
|
|
115
|
+
if (action === "list" || action === "verify") {
|
|
116
|
+
const entries = listEntries(index, action === "verify");
|
|
117
|
+
if (args.id !== undefined) {
|
|
118
|
+
// 按"用户可能怎么写"解析(cmsis:F1:103xb / 0.1 的 startup_stm32f103xb / F1:B / 型号)。
|
|
119
|
+
const target = resolveCacheEntry(index, args.id);
|
|
120
|
+
const kept = target === null ? [] : entries.filter((entry) => entry.id === target.id);
|
|
121
|
+
const hint = target === null ? "" : target.id === args.id ? "" : "(已按别名解析成 " + target.id + ")";
|
|
122
|
+
return {
|
|
123
|
+
action,
|
|
124
|
+
ok: error === null,
|
|
125
|
+
cacheRoot: info.root,
|
|
126
|
+
indexPath: info.indexPath,
|
|
127
|
+
origin: info.origin,
|
|
128
|
+
totalBytes: kept.reduce((sum, entry) => sum + entry.bytes, 0),
|
|
129
|
+
entries: kept,
|
|
130
|
+
message: (error ?? "") + (kept.length === 0 ? "缓存里没有条目 " + args.id + "。" : "缓存里有 " + String(kept.length) + " 个条目" + hint + "。"),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
action,
|
|
135
|
+
ok: error === null,
|
|
136
|
+
cacheRoot: info.root,
|
|
137
|
+
indexPath: info.indexPath,
|
|
138
|
+
origin: info.origin,
|
|
139
|
+
totalBytes: entries.reduce((sum, entry) => sum + entry.bytes, 0),
|
|
140
|
+
entries,
|
|
141
|
+
message: (error ?? "") + (entries.length === 0 ? "缓存是空的:第一次建寄存器版工程(或 action=fetch)时会从网上下载模板。" : "缓存里有 " + String(entries.length) + " 个条目。"),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
if (action === "fetch") {
|
|
145
|
+
if ((args.mcu ?? "") === "" && (args.family ?? "") === "") {
|
|
146
|
+
return empty("fetch 至少给一个 mcu(例如 STM32F103C8T6)或 family(例如 F1)。");
|
|
147
|
+
}
|
|
148
|
+
const result = await ensureCmsisTemplate({
|
|
149
|
+
...(args.mcu === undefined ? {} : { mcu: args.mcu }),
|
|
150
|
+
...(args.family === undefined ? {} : { family: args.family }),
|
|
151
|
+
// 取消要能真的传到网络层(A34):模板 10 个文件、4 个候选源,用户按取消时不能还慢慢下。
|
|
152
|
+
signal: exec.signal,
|
|
153
|
+
});
|
|
154
|
+
const after = readCacheIndex(process.env).index;
|
|
155
|
+
const entries = listEntries(after, false);
|
|
156
|
+
return {
|
|
157
|
+
action,
|
|
158
|
+
ok: result.ok,
|
|
159
|
+
cacheRoot: info.root,
|
|
160
|
+
indexPath: info.indexPath,
|
|
161
|
+
origin: info.origin,
|
|
162
|
+
totalBytes: entries.reduce((sum, entry) => sum + entry.bytes, 0),
|
|
163
|
+
entries: entries.filter((entry) => result.id === "" || entry.id === result.id),
|
|
164
|
+
message: result.message,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
if (action === "gc") {
|
|
168
|
+
const pruned = pruneLegacyEntries(process.env);
|
|
169
|
+
const after = readCacheIndex(process.env).index;
|
|
170
|
+
const entries = listEntries(after, false);
|
|
171
|
+
const parts = [];
|
|
172
|
+
if (pruned.removed.length === 0) {
|
|
173
|
+
parts.push(pruned.kept.length === 0 ? "没有需要清理的旧条目" : "旧条目还被用着(新 id 不存在),保留 " + String(pruned.kept.length) + " 个");
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
parts.push("清掉 " + String(pruned.removed.length) + " 个旧条目(" + describeCacheBytes(pruned.reclaimedBytes) + "):" + pruned.removed.join("、"));
|
|
177
|
+
}
|
|
178
|
+
if (pruned.kept.length > 0 && pruned.removed.length > 0)
|
|
179
|
+
parts.push("保留 " + String(pruned.kept.length) + " 个仍在用的旧条目");
|
|
180
|
+
return {
|
|
181
|
+
action,
|
|
182
|
+
ok: true,
|
|
183
|
+
cacheRoot: info.root,
|
|
184
|
+
indexPath: info.indexPath,
|
|
185
|
+
origin: info.origin,
|
|
186
|
+
totalBytes: entries.reduce((sum, entry) => sum + entry.bytes, 0),
|
|
187
|
+
entries,
|
|
188
|
+
message: parts.join(";") + "。",
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
if (action !== "purge") {
|
|
192
|
+
// 以前这里直接掉进 purge 分支:action 拼错一个字母(或模型传了不认识的值)就等于清空缓存(A8)。
|
|
193
|
+
return empty("不认识的 action:" + action + "(只有 list / verify / fetch / gc / purge)。什么都没动。");
|
|
194
|
+
}
|
|
195
|
+
// purge
|
|
196
|
+
if (args.id !== undefined) {
|
|
197
|
+
// 同样先解析别名:0.1 的 id 写法也得删得掉,否则用户以为删了、下次还是旧文件。
|
|
198
|
+
const target = resolveCacheEntry(index, args.id);
|
|
199
|
+
const removed = target === null ? { removed: false, indexPath: info.indexPath } : removeCacheEntry(target.id, process.env);
|
|
200
|
+
const after = readCacheIndex(process.env).index;
|
|
201
|
+
const entries = listEntries(after, false);
|
|
202
|
+
return {
|
|
203
|
+
action: "purge",
|
|
204
|
+
ok: true,
|
|
205
|
+
cacheRoot: info.root,
|
|
206
|
+
indexPath: info.indexPath,
|
|
207
|
+
origin: info.origin,
|
|
208
|
+
totalBytes: entries.reduce((sum, entry) => sum + entry.bytes, 0),
|
|
209
|
+
entries,
|
|
210
|
+
message: removed.removed
|
|
211
|
+
? "已删掉缓存条目 " + (target === null ? args.id : target.id) + "(目录也清了)。"
|
|
212
|
+
: "缓存里没有条目 " + args.id + ",什么都没删。",
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
const all = readCacheIndex(process.env).index.entries;
|
|
216
|
+
if (all.length === 0) {
|
|
217
|
+
return empty("缓存本来就是空的。", true);
|
|
218
|
+
}
|
|
219
|
+
if (args.confirm !== true) {
|
|
220
|
+
// 清空是破坏性操作:没有 confirm 就说清楚要什么,不删(A8)。
|
|
221
|
+
const entries = listEntries(index, false);
|
|
222
|
+
return {
|
|
223
|
+
action: "purge",
|
|
224
|
+
ok: false,
|
|
225
|
+
cacheRoot: info.root,
|
|
226
|
+
indexPath: info.indexPath,
|
|
227
|
+
origin: info.origin,
|
|
228
|
+
totalBytes: entries.reduce((sum, entry) => sum + entry.bytes, 0),
|
|
229
|
+
entries,
|
|
230
|
+
message: "要清空整个缓存(" + String(all.length) + " 个条目、共 " + describeCacheBytes(entries.reduce((sum, entry) => sum + entry.bytes, 0)) + ")必须带 confirm:true;只想删一个条目就给 id。什么都没删。",
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
let removed = 0;
|
|
234
|
+
for (const entry of all)
|
|
235
|
+
if (removeCacheEntry(entry.id, process.env).removed)
|
|
236
|
+
removed += 1;
|
|
237
|
+
return {
|
|
238
|
+
action: "purge",
|
|
239
|
+
ok: true,
|
|
240
|
+
cacheRoot: info.root,
|
|
241
|
+
indexPath: info.indexPath,
|
|
242
|
+
origin: info.origin,
|
|
243
|
+
totalBytes: 0,
|
|
244
|
+
entries: [],
|
|
245
|
+
message: "已清空缓存(索引里原有 " + String(all.length) + " 个条目,实际删掉 " + String(removed) + " 个)。下次建工程会重新下载。"
|
|
246
|
+
+ (removed === all.length ? "" : "剩下 " + String(all.length - removed) + " 个只删了索引记录(目录可能不在了或被占用)。"),
|
|
247
|
+
};
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
}
|
package/lib/tools/code-patch.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* 这个工具把"只改 USER CODE 块"变成工具契约:块不存在就失败,块外字节原样保留。
|
|
7
7
|
*/
|
|
8
8
|
import { patchUserCode } from "../engine/code-patch.js";
|
|
9
|
+
import { resolveProjectDir, validateToolArgs } from "./project-dir.js";
|
|
9
10
|
export const CODE_PATCH_TOOL = "stm32_code_patch";
|
|
10
11
|
const reportSchema = {
|
|
11
12
|
type: "object",
|
|
@@ -71,15 +72,41 @@ export function registerCodePatch(ctx) {
|
|
|
71
72
|
return [{ type: "text", text: formatCodePatchReport(typed.report).join("\n") }];
|
|
72
73
|
},
|
|
73
74
|
},
|
|
74
|
-
async execute(rawArgs,
|
|
75
|
+
async execute(rawArgs, exec) {
|
|
75
76
|
const args = rawArgs ?? {};
|
|
77
|
+
// 洞 C:以前参数名写错(例如把 file/block/code 写成 edits)会把 undefined 塞进报告,宿主序列化
|
|
78
|
+
// 时抛 "value is not lossless JSON",用户完全看不到真正原因。
|
|
79
|
+
const invalid = validateToolArgs(CODE_PATCH_TOOL, args, [
|
|
80
|
+
{ name: "file", kind: "string" },
|
|
81
|
+
{ name: "block", kind: "string" },
|
|
82
|
+
{ name: "code", kind: "string" },
|
|
83
|
+
]);
|
|
84
|
+
if (invalid !== null) {
|
|
85
|
+
return {
|
|
86
|
+
message: invalid,
|
|
87
|
+
report: {
|
|
88
|
+
ok: false,
|
|
89
|
+
file: typeof args.file === "string" ? args.file : "",
|
|
90
|
+
block: typeof args.block === "string" ? args.block : "",
|
|
91
|
+
mode: args.mode === "append" ? "append" : "replace",
|
|
92
|
+
labels: [],
|
|
93
|
+
linesBefore: 0,
|
|
94
|
+
linesAfter: 0,
|
|
95
|
+
errors: [invalid],
|
|
96
|
+
message: invalid,
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const resolution = await resolveProjectDir(ctx, args.projectDir, exec);
|
|
76
101
|
const report = patchUserCode({
|
|
77
|
-
projectDir:
|
|
102
|
+
projectDir: resolution.projectDir,
|
|
78
103
|
file: args.file,
|
|
79
104
|
block: args.block,
|
|
80
105
|
code: args.code,
|
|
81
106
|
...(args.mode === undefined ? {} : { mode: args.mode }),
|
|
82
107
|
});
|
|
108
|
+
if (resolution.note !== null)
|
|
109
|
+
report.message = report.message + "\n· " + resolution.note;
|
|
83
110
|
return { message: report.message, report };
|
|
84
111
|
},
|
|
85
112
|
});
|
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
* 探针一定是空闲的。build / flash / generate 动手前也会先让会话让出探针。
|
|
10
10
|
*/
|
|
11
11
|
import { debugSessions } from "../engine/debug-session.js";
|
|
12
|
+
import { ensureToolchain, gateReportNotes } from "../engine/toolchain-gate.js";
|
|
13
|
+
import { resolveProjectDir } from "./project-dir.js";
|
|
12
14
|
export const DEBUG_SESSION_TOOL = "stm32_debug_session";
|
|
13
15
|
export const DEBUG_EVAL_TOOL = "stm32_debug_eval";
|
|
14
16
|
export const DEBUG_STOP_TOOL = "stm32_debug_stop";
|
|
@@ -72,6 +74,12 @@ export const stopOutputSchema = {
|
|
|
72
74
|
},
|
|
73
75
|
required: ["message", "ok", "stopped"],
|
|
74
76
|
};
|
|
77
|
+
/** 门禁档位参数:三个档位的说明文字与 build 工具保持一致。 */
|
|
78
|
+
const ENV_CHECK_SCHEMA = {
|
|
79
|
+
type: "string",
|
|
80
|
+
enum: ["block", "warn", "off"],
|
|
81
|
+
description: "工具链检查:block 缺件就拒绝(默认)、warn 只警告、off 不检查",
|
|
82
|
+
};
|
|
75
83
|
function describeSession(report) {
|
|
76
84
|
const lines = [report.message];
|
|
77
85
|
if (report.active) {
|
|
@@ -84,6 +92,18 @@ function describeSession(report) {
|
|
|
84
92
|
lines.push("x " + error);
|
|
85
93
|
return lines;
|
|
86
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* 门禁拒绝时的会话报告。
|
|
97
|
+
* sessionSchema 是 additionalProperties:false 且没有 notes,门禁的补充说明只能落在 errors 里
|
|
98
|
+
* (首行就是 message,跳过以免重复)。
|
|
99
|
+
*/
|
|
100
|
+
function blockedSession(projectDir, gate) {
|
|
101
|
+
return {
|
|
102
|
+
ok: false, active: false, sessionId: "", port: 0, projectDir, elfPath: "",
|
|
103
|
+
startedAt: 0, idleTtlMs: 0, hardTtlMs: 0, remainingMs: 0,
|
|
104
|
+
errors: gateReportNotes(gate).slice(1), message: gate.message,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
87
107
|
export function registerDebugSession(ctx) {
|
|
88
108
|
ctx.tools.register({
|
|
89
109
|
name: DEBUG_SESSION_TOOL,
|
|
@@ -101,6 +121,7 @@ export function registerDebugSession(ctx) {
|
|
|
101
121
|
hardTtlMs: { type: "number", description: "总时长上限毫秒数,默认 600000。" },
|
|
102
122
|
reset: { type: "string", enum: ["none", "halt", "run"], description: "起会话时对目标做什么,默认 none(不动芯片)。" },
|
|
103
123
|
binDirs: { type: "array", items: { type: "string" }, description: "工具链目录候选。" },
|
|
124
|
+
envCheck: ENV_CHECK_SCHEMA,
|
|
104
125
|
},
|
|
105
126
|
},
|
|
106
127
|
timeoutMs: 60_000,
|
|
@@ -113,20 +134,39 @@ export function registerDebugSession(ctx) {
|
|
|
113
134
|
},
|
|
114
135
|
},
|
|
115
136
|
async execute(rawArgs, exec) {
|
|
116
|
-
|
|
117
|
-
|
|
137
|
+
// 宿主可能不传 arguments(无参数调用)→ 兜成 {},让"缺参数"表现成正常报告而不是 TypeError(审计 A31)。
|
|
138
|
+
const args = rawArgs ?? {};
|
|
139
|
+
const resolution = await resolveProjectDir(ctx, args.projectDir, exec);
|
|
140
|
+
const projectDir = resolution.projectDir;
|
|
141
|
+
// 门禁:长调试会话同样要 openocd + GDB。开着会话再报"缺 openocd"只会让人以为探针坏了。
|
|
142
|
+
const gate = await ensureToolchain(ctx, {
|
|
143
|
+
capability: "debug",
|
|
144
|
+
...(args.binDirs === undefined ? {} : { binDirs: args.binDirs }),
|
|
145
|
+
envCheck: args.envCheck,
|
|
146
|
+
signal: exec.signal,
|
|
147
|
+
});
|
|
148
|
+
if (!gate.ok && gate.mode === "block") {
|
|
149
|
+
return { message: gate.message, session: blockedSession(resolution.projectDir, gate) };
|
|
150
|
+
}
|
|
151
|
+
debugSessions.configure(ctx.subprocess, args.binDirs ?? []);
|
|
118
152
|
const session = await debugSessions.start({
|
|
119
153
|
projectDir,
|
|
120
|
-
...(
|
|
121
|
-
...(
|
|
122
|
-
...(
|
|
123
|
-
...(
|
|
124
|
-
...(
|
|
125
|
-
...(
|
|
126
|
-
...(
|
|
127
|
-
...(
|
|
154
|
+
...(args.elfPath === undefined ? {} : { elfPath: args.elfPath }),
|
|
155
|
+
...(args.port === undefined ? {} : { port: args.port }),
|
|
156
|
+
...(args.adapterSpeed === undefined ? {} : { adapterSpeed: args.adapterSpeed }),
|
|
157
|
+
...(args.configFile === undefined ? {} : { configFile: args.configFile }),
|
|
158
|
+
...(args.idleTtlMs === undefined ? {} : { idleTtlMs: args.idleTtlMs }),
|
|
159
|
+
...(args.hardTtlMs === undefined ? {} : { hardTtlMs: args.hardTtlMs }),
|
|
160
|
+
...(args.reset === undefined ? {} : { reset: args.reset }),
|
|
161
|
+
...(args.binDirs === undefined ? {} : { binDirs: args.binDirs }),
|
|
162
|
+
// A9:记下是谁开的会话,turn/end 只关自己的那个。
|
|
163
|
+
...(exec.agent?.id === undefined ? {} : { owner: exec.agent.id }),
|
|
128
164
|
});
|
|
129
165
|
void exec;
|
|
166
|
+
const base = resolution.note === null ? session.message : session.message + "\n· " + resolution.note;
|
|
167
|
+
// 会话报告的 schema 关了额外属性、又没有 notes 字段,warn 档只能把警告并回 message;
|
|
168
|
+
// 而且 render 只打 session.message(不看外层 message),所以警告必须写进 session 里才看得见。
|
|
169
|
+
session.message = gate.ok ? base : gate.message + "\n" + base;
|
|
130
170
|
return { message: session.message, session };
|
|
131
171
|
},
|
|
132
172
|
});
|
|
@@ -156,11 +196,13 @@ export function registerDebugSession(ctx) {
|
|
|
156
196
|
},
|
|
157
197
|
},
|
|
158
198
|
async execute(rawArgs, exec) {
|
|
199
|
+
// 同 A31:无参数调用兜成 {}。没带 commands/expression 时引擎会给出"没执行"的报告,不是崩溃。
|
|
200
|
+
const args = rawArgs ?? {};
|
|
159
201
|
const report = await debugSessions.eval({
|
|
160
|
-
...(
|
|
161
|
-
...(
|
|
162
|
-
...(
|
|
163
|
-
...(
|
|
202
|
+
...(args.commands === undefined ? {} : { commands: args.commands }),
|
|
203
|
+
...(args.expression === undefined ? {} : { expression: args.expression }),
|
|
204
|
+
...(args.keepHalted === undefined ? {} : { keepHalted: args.keepHalted }),
|
|
205
|
+
...(args.timeoutMs === undefined ? {} : { timeoutMs: args.timeoutMs }),
|
|
164
206
|
signal: exec.signal,
|
|
165
207
|
});
|
|
166
208
|
return { message: report.message, report };
|
package/lib/tools/debug.js
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { runDebugRun } from "../engine/debug.js";
|
|
8
8
|
import { releaseProbeFor } from "../engine/debug-session.js";
|
|
9
|
+
import { ensureToolchain, gateReportNotes } from "../engine/toolchain-gate.js";
|
|
10
|
+
import { resolveProjectDir } from "./project-dir.js";
|
|
9
11
|
export const DEBUG_RUN_TOOL = "stm32_debug_run";
|
|
10
12
|
const registerSchema = {
|
|
11
13
|
type: "object",
|
|
@@ -33,6 +35,11 @@ const reportSchema = {
|
|
|
33
35
|
gdbCommand: { type: "string" },
|
|
34
36
|
gdbOutput: { type: "string" },
|
|
35
37
|
openOcdLogTail: { type: "string" },
|
|
38
|
+
// F2:失败时带结构化错误码(与 stm32_flash 同一套枚举)。可选字段,成功路径不出现。
|
|
39
|
+
errorCode: {
|
|
40
|
+
type: "string",
|
|
41
|
+
enum: ["", "no-probe", "probe-busy", "swd-locked", "flash-protected", "no-power", "target-mismatch", "unknown"],
|
|
42
|
+
},
|
|
36
43
|
durationMs: { type: "number" },
|
|
37
44
|
errors: { type: "array", items: { type: "string" } },
|
|
38
45
|
message: { type: "string" },
|
|
@@ -49,11 +56,19 @@ export const outputSchema = {
|
|
|
49
56
|
},
|
|
50
57
|
required: ["message", "report"],
|
|
51
58
|
};
|
|
59
|
+
/** 门禁档位参数:三个档位的说明文字与 build 工具保持一致。 */
|
|
60
|
+
const ENV_CHECK_SCHEMA = {
|
|
61
|
+
type: "string",
|
|
62
|
+
enum: ["block", "warn", "off"],
|
|
63
|
+
description: "工具链检查:block 缺件就拒绝(默认)、warn 只警告、off 不检查",
|
|
64
|
+
};
|
|
52
65
|
export function formatDebugReport(report) {
|
|
53
66
|
const lines = [];
|
|
54
67
|
lines.push((report.ok ? "调试快照完成" : "调试失败") + ":" + report.message);
|
|
55
68
|
if (report.elfPath !== "")
|
|
56
69
|
lines.push("固件:" + report.elfPath);
|
|
70
|
+
if (report.errorCode !== undefined && report.errorCode !== "")
|
|
71
|
+
lines.push("错误码:" + report.errorCode);
|
|
57
72
|
if (report.port !== 0)
|
|
58
73
|
lines.push("端口:" + String(report.port) + ",断点:" + report.breakAt);
|
|
59
74
|
if (report.location !== "")
|
|
@@ -81,6 +96,15 @@ export function formatDebugReport(report) {
|
|
|
81
96
|
}
|
|
82
97
|
return lines;
|
|
83
98
|
}
|
|
99
|
+
/** 门禁拒绝时的报告:形如 emptyReport,但 notes 换成"缺什么、装在哪"。 */
|
|
100
|
+
function blockedReport(projectDir, gate) {
|
|
101
|
+
return {
|
|
102
|
+
ok: false, projectDir, elfPath: "", port: 0, breakAt: "main",
|
|
103
|
+
hitBreakpoint: false, location: "", registers: [], backtrace: [], locals: [],
|
|
104
|
+
gdbCommand: "", gdbOutput: "", openOcdLogTail: "", durationMs: 0,
|
|
105
|
+
errors: [], message: gate.message, notes: gateReportNotes(gate),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
84
108
|
function emptyReport(message) {
|
|
85
109
|
return {
|
|
86
110
|
ok: false, projectDir: "", elfPath: "", port: 0, breakAt: "main",
|
|
@@ -107,6 +131,7 @@ export function registerDebugRun(ctx) {
|
|
|
107
131
|
adapterSpeed: { type: "number", description: "adapter speed(kHz)。" },
|
|
108
132
|
timeoutMs: { type: "number", description: "gdb 超时,默认 45000。" },
|
|
109
133
|
binDirs: { type: "array", items: { type: "string" }, description: "工具链目录候选。" },
|
|
134
|
+
envCheck: ENV_CHECK_SCHEMA,
|
|
110
135
|
},
|
|
111
136
|
required: [],
|
|
112
137
|
},
|
|
@@ -126,10 +151,22 @@ export function registerDebugRun(ctx) {
|
|
|
126
151
|
const report = emptyReport("宿主缺少 subprocess 服务,无法调试");
|
|
127
152
|
return { message: report.message, report };
|
|
128
153
|
}
|
|
129
|
-
|
|
154
|
+
const resolution = await resolveProjectDir(ctx, args.projectDir, exec);
|
|
155
|
+
// 门禁:调试要 openocd + GDB 两件。缺件且 envCheck=block 时不占探针、不起进程。
|
|
156
|
+
const gate = await ensureToolchain(ctx, {
|
|
157
|
+
capability: "debug",
|
|
158
|
+
...(args.binDirs === undefined ? {} : { binDirs: args.binDirs }),
|
|
159
|
+
envCheck: args.envCheck,
|
|
160
|
+
signal: exec.signal,
|
|
161
|
+
});
|
|
162
|
+
if (!gate.ok && gate.mode === "block") {
|
|
163
|
+
const blocked = blockedReport(resolution.projectDir, gate);
|
|
164
|
+
return { message: blocked.message, report: blocked };
|
|
165
|
+
}
|
|
166
|
+
// debug_run 自己也要独占探针;缺件退出时不动别人的会话(B16)。
|
|
130
167
|
const released = await releaseProbeFor("stm32_debug_run");
|
|
131
168
|
const report = await runDebugRun(subprocess, {
|
|
132
|
-
projectDir:
|
|
169
|
+
projectDir: resolution.projectDir,
|
|
133
170
|
...(args.elfPath === undefined ? {} : { elfPath: args.elfPath }),
|
|
134
171
|
...(args.buildDir === undefined ? {} : { buildDir: args.buildDir }),
|
|
135
172
|
...(args.breakAt === undefined ? {} : { breakAt: args.breakAt }),
|
|
@@ -141,8 +178,12 @@ export function registerDebugRun(ctx) {
|
|
|
141
178
|
...(args.binDirs === undefined ? {} : { binDirs: args.binDirs }),
|
|
142
179
|
signal: exec.signal,
|
|
143
180
|
});
|
|
181
|
+
if (!gate.ok)
|
|
182
|
+
report.notes.unshift(gate.message);
|
|
144
183
|
if (released !== null)
|
|
145
184
|
report.notes.push(released);
|
|
185
|
+
if (resolution.note !== null)
|
|
186
|
+
report.notes.push(resolution.note);
|
|
146
187
|
return { message: report.message, report };
|
|
147
188
|
},
|
|
148
189
|
});
|