openhood 0.1.0 → 0.1.1
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/openhood.js +75 -9
- package/package.json +1 -1
package/openhood.js
CHANGED
|
@@ -4556,18 +4556,24 @@ function isCanProtocol(dpn) {
|
|
|
4556
4556
|
}
|
|
4557
4557
|
var ElmLink = class {
|
|
4558
4558
|
constructor(stream, onTraffic = () => {
|
|
4559
|
-
}) {
|
|
4559
|
+
}, route) {
|
|
4560
4560
|
this.stream = stream;
|
|
4561
4561
|
this.onTraffic = onTraffic;
|
|
4562
|
+
this.route = route;
|
|
4562
4563
|
}
|
|
4563
4564
|
stream;
|
|
4564
4565
|
onTraffic;
|
|
4566
|
+
route;
|
|
4565
4567
|
adapter = "elm327";
|
|
4566
4568
|
init = { kind: "adapter", can: false, protocol: "" };
|
|
4567
4569
|
/** The adapter's own name, e.g. "ELM327 v1.5". */
|
|
4568
4570
|
version = "";
|
|
4569
4571
|
/** True once the adapter is printing CAN frames with their sender (ATH1). See parseElmCanFrames. */
|
|
4570
4572
|
headers = false;
|
|
4573
|
+
/** The header last set with ATSH, for a manufacturer route. */
|
|
4574
|
+
header = "";
|
|
4575
|
+
/** The first request of a manufacturer route wakes the ECU, which takes a few seconds. */
|
|
4576
|
+
woken = false;
|
|
4571
4577
|
/** Send one line, return the lines printed before the ">" prompt. */
|
|
4572
4578
|
async command(cmd, timeoutMs = 2e3, note, priv = false) {
|
|
4573
4579
|
this.stream.flush();
|
|
@@ -4592,6 +4598,7 @@ var ElmLink = class {
|
|
|
4592
4598
|
this.version = name(await this.command("ATZ", 4e3, "reset adapter"));
|
|
4593
4599
|
if (!this.version) this.version = name(await this.command("ATI", 2e3, "adapter name"));
|
|
4594
4600
|
if (!this.version) throw new AdapterError("This doesn't look like an ELM327 adapter.");
|
|
4601
|
+
if (this.route) return this.openRoute(this.route, connect);
|
|
4595
4602
|
for (const c of ["ATE0", "ATL0", "ATS0", "ATH0", "ATAT1", "ATSP0"]) await this.command(c);
|
|
4596
4603
|
const first = parseElmResponse(await this.command("0100", 15e3, "find the car's protocol"));
|
|
4597
4604
|
if (!first.some((r) => r[0] === 65)) throw new TimeoutError(0, 0, "The car didn't answer the adapter. Is the ignition on?");
|
|
@@ -4608,11 +4615,34 @@ var ElmLink = class {
|
|
|
4608
4615
|
this.init = { kind: "adapter", can, protocol: desc.replace(/^AUTO,\s*/i, "") };
|
|
4609
4616
|
await connect(this, this.init);
|
|
4610
4617
|
}
|
|
4618
|
+
/** A manufacturer protocol: fixed protocol, wake-up address and keep-alive; headers per request. */
|
|
4619
|
+
async openRoute(r, connect) {
|
|
4620
|
+
const h = (n) => n.toString(16).toUpperCase().padStart(2, "0");
|
|
4621
|
+
const cmds = ["ATE0", "ATL0", "ATS0", "ATH0", "ATAT1", `ATSP${r.protocol}`];
|
|
4622
|
+
if (r.initAddress !== void 0) cmds.push(`ATIIA${h(r.initAddress)}`);
|
|
4623
|
+
if (r.testerAddress !== void 0) cmds.push(`ATTA${h(r.testerAddress)}`);
|
|
4624
|
+
if (r.keepAlive) cmds.push(`ATWM${[...r.header(r.keepAlive.length), ...r.keepAlive].map(h).join("")}`);
|
|
4625
|
+
for (const c of cmds) await this.command(c);
|
|
4626
|
+
this.header = "";
|
|
4627
|
+
this.woken = false;
|
|
4628
|
+
const names = { 3: "ISO 9141-2, 5-baud wake-up", 4: "ISO 14230, 5-baud wake-up", 5: "ISO 14230, fast wake-up" };
|
|
4629
|
+
this.init = { kind: "adapter", can: false, protocol: names[r.protocol] };
|
|
4630
|
+
await connect(this, this.init);
|
|
4631
|
+
}
|
|
4611
4632
|
close() {
|
|
4612
4633
|
return this.stream.close();
|
|
4613
4634
|
}
|
|
4614
4635
|
async ask(payload, opts) {
|
|
4615
|
-
|
|
4636
|
+
if (this.route) {
|
|
4637
|
+
const header = this.route.header(payload.length).map((b) => b.toString(16).toUpperCase().padStart(2, "0")).join("");
|
|
4638
|
+
if (header !== this.header) {
|
|
4639
|
+
await this.command(`ATSH${header}`);
|
|
4640
|
+
this.header = header;
|
|
4641
|
+
}
|
|
4642
|
+
}
|
|
4643
|
+
const timeout = this.route && !this.woken ? 1e4 : 3e3;
|
|
4644
|
+
this.woken = true;
|
|
4645
|
+
const lines = await this.command(hex(Uint8Array.from(payload)).replace(/\s/g, ""), timeout, opts.note, identifies(payload));
|
|
4616
4646
|
const sid = payload[0];
|
|
4617
4647
|
const answers = this.headers ? parseElmCanFrames(lines) : parseElmResponse(lines);
|
|
4618
4648
|
const ok = answers.filter((r) => r[0] === positive(sid));
|
|
@@ -5460,6 +5490,8 @@ var SimulatedElm = class {
|
|
|
5460
5490
|
echo = true;
|
|
5461
5491
|
headers = false;
|
|
5462
5492
|
line = "";
|
|
5493
|
+
/** What a manufacturer route set: protocol, wake-up address and header. */
|
|
5494
|
+
set = {};
|
|
5463
5495
|
async open() {
|
|
5464
5496
|
}
|
|
5465
5497
|
async close() {
|
|
@@ -5491,8 +5523,12 @@ var SimulatedElm = class {
|
|
|
5491
5523
|
this.searched = false;
|
|
5492
5524
|
this.ecu = null;
|
|
5493
5525
|
this.headers = false;
|
|
5526
|
+
this.set = {};
|
|
5494
5527
|
return ["", this.opts.junkAfterReset ? "\xFF\xFE?" : "ELM327 v1.5"];
|
|
5495
5528
|
}
|
|
5529
|
+
if (cmd.startsWith("ATSP")) this.set.sp = cmd.slice(4);
|
|
5530
|
+
if (cmd.startsWith("ATIIA")) this.set.iia = cmd.slice(5);
|
|
5531
|
+
if (cmd.startsWith("ATSH")) this.set.sh = cmd.slice(4);
|
|
5496
5532
|
if (cmd === "ATH1" || cmd === "ATH0") {
|
|
5497
5533
|
if (!this.opts.ignoresHeaders) this.headers = cmd === "ATH1";
|
|
5498
5534
|
return ["OK"];
|
|
@@ -5506,6 +5542,8 @@ var SimulatedElm = class {
|
|
|
5506
5542
|
if (cmd === "ATDP") return [this.opts.can ? "AUTO, ISO 15765-4 (CAN 11/500)" : "AUTO, ISO 14230-4 (KWP FAST)"];
|
|
5507
5543
|
if (cmd.startsWith("AT")) return ["OK"];
|
|
5508
5544
|
if (!/^([0-9A-F]{2})+$/.test(cmd)) return ["?"];
|
|
5545
|
+
const route = this.profile.link.elm;
|
|
5546
|
+
if (route) return this.answerRoute(route, cmd);
|
|
5509
5547
|
const prefix = [];
|
|
5510
5548
|
if (!this.searched) {
|
|
5511
5549
|
this.searched = true;
|
|
@@ -5520,6 +5558,24 @@ var SimulatedElm = class {
|
|
|
5520
5558
|
if (this.opts.can && [67, 71, 74].includes(frames[0]?.[0])) frames = [toCanDtcAnswer(frames)];
|
|
5521
5559
|
return [...prefix, ...frames.flatMap((f) => this.print(f))];
|
|
5522
5560
|
}
|
|
5561
|
+
/** A manufacturer protocol through the adapter: only answers when it was set up the way the route says. */
|
|
5562
|
+
answerRoute(route, cmd) {
|
|
5563
|
+
const payload = [];
|
|
5564
|
+
for (let i = 0; i < cmd.length; i += 2) payload.push(parseInt(cmd.slice(i, i + 2), 16));
|
|
5565
|
+
const want = route.header(payload.length).map(h22).join("");
|
|
5566
|
+
const iia = route.initAddress === void 0 ? void 0 : h22(route.initAddress);
|
|
5567
|
+
if (this.set.sp !== String(route.protocol) || this.set.iia !== iia || this.set.sh !== want) return ["NO DATA"];
|
|
5568
|
+
const prefix = [];
|
|
5569
|
+
if (!this.searched) {
|
|
5570
|
+
this.searched = true;
|
|
5571
|
+
this.ecu = this.profile.simulator();
|
|
5572
|
+
prefix.push("BUS INIT: ...OK");
|
|
5573
|
+
}
|
|
5574
|
+
const res = this.ecu?.handle(Uint8Array.from(payload));
|
|
5575
|
+
if (!res) return [...prefix, "NO DATA"];
|
|
5576
|
+
const frames = Array.isArray(res[0]) ? res : [res];
|
|
5577
|
+
return [...prefix, ...frames.map((f) => f.map(h22).join(""))];
|
|
5578
|
+
}
|
|
5523
5579
|
print(f) {
|
|
5524
5580
|
if (!this.opts.can) return [this.headers ? legacyFrame(f) : f.map(h22).join("")];
|
|
5525
5581
|
if (this.headers) return canFrames(f);
|
|
@@ -8024,10 +8080,10 @@ function d3b0Profile(o) {
|
|
|
8024
8080
|
covers: `850 \xB7 1996 to 1997, S70, V70, C70 \xB7 1997 to 1998`,
|
|
8025
8081
|
vehicles: YEARS.map((y) => ({ ...y, engine: "Any engine", system: o.system })),
|
|
8026
8082
|
ecu: o.ecu,
|
|
8027
|
-
//
|
|
8028
|
-
//
|
|
8029
|
-
adapters: ["kkl"],
|
|
8030
|
-
hardware: "
|
|
8083
|
+
// Most people own an ELM327, which carries this protocol when set up by hand (link.elm, from
|
|
8084
|
+
// jonesrh's command sequence). A KKL cable works too.
|
|
8085
|
+
adapters: ["elm327", "kkl"],
|
|
8086
|
+
hardware: "An ELM327 adapter or a KKL cable in the diagnostic socket.",
|
|
8031
8087
|
status: "experimental",
|
|
8032
8088
|
link: {
|
|
8033
8089
|
baudRate: 10400,
|
|
@@ -8037,7 +8093,17 @@ function d3b0Profile(o) {
|
|
|
8037
8093
|
alwaysAddressed: true,
|
|
8038
8094
|
lengthOffset: 1,
|
|
8039
8095
|
// There is no live data to poll yet, only the keep-alive: no need to send it flat out.
|
|
8040
|
-
interMessageMs: 250
|
|
8096
|
+
interMessageMs: 250,
|
|
8097
|
+
// Through an ELM327, as jonesrh does it: ISO 9141 with a 5-baud wake-up at the module's
|
|
8098
|
+
// address (ATSP 3, ATIIA), tester 13 (ATTA 13), a header per request length (ATSH 83 01 13
|
|
8099
|
+
// before AE01) and the adapter repeating the keep-alive itself (ATWM 82 01 13 A1).
|
|
8100
|
+
elm: {
|
|
8101
|
+
protocol: 3,
|
|
8102
|
+
initAddress: o.address,
|
|
8103
|
+
testerAddress: 19,
|
|
8104
|
+
header: (n) => [129 + n, o.address, 19],
|
|
8105
|
+
keepAlive: D3B0.keepAlive
|
|
8106
|
+
}
|
|
8041
8107
|
},
|
|
8042
8108
|
async connect(ecu) {
|
|
8043
8109
|
await ecu.request(D3B0.keepAlive, { note: "hello" });
|
|
@@ -8715,7 +8781,7 @@ var Runtime = class {
|
|
|
8715
8781
|
if (!o.port) {
|
|
8716
8782
|
if (o.simulateElmCan === void 0) return kLineLink(profile, new SimulatedTransport(profile), this.log);
|
|
8717
8783
|
if (!allowed.includes("elm327")) throw new UserError(`${profile.name} can't run through an ELM327 adapter.`);
|
|
8718
|
-
return new ElmLink(new SimulatedElm(profile, { can: o.simulateElmCan }), this.log);
|
|
8784
|
+
return new ElmLink(new SimulatedElm(profile, { can: o.simulateElmCan }), this.log, profile.link.elm);
|
|
8719
8785
|
}
|
|
8720
8786
|
const path = o.port;
|
|
8721
8787
|
let kind = o.adapter && o.adapter !== "auto" ? o.adapter : void 0;
|
|
@@ -8728,7 +8794,7 @@ var Runtime = class {
|
|
|
8728
8794
|
} else if (kind === "elm327") baudRate = 38400;
|
|
8729
8795
|
if (!allowed.includes(kind))
|
|
8730
8796
|
throw new UserError(`${profile.name} needs ${allowed.includes("kkl") ? "a KKL cable" : "an ELM327-type adapter"}, but this is ${kind === "kkl" ? "a KKL cable (or nothing answered)" : "an ELM327 adapter"}.`);
|
|
8731
|
-
return kind === "elm327" ? new ElmLink(new NodeSerialTransport({ path, baudRate, echo: false }), this.log) : kLineLink(profile, new NodeSerialTransport({ path, baudRate: profile.link.baudRate }), this.log);
|
|
8797
|
+
return kind === "elm327" ? new ElmLink(new NodeSerialTransport({ path, baudRate, echo: false }), this.log, profile.link.elm) : kLineLink(profile, new NodeSerialTransport({ path, baudRate: profile.link.baudRate }), this.log);
|
|
8732
8798
|
}
|
|
8733
8799
|
async disconnect() {
|
|
8734
8800
|
const s = this.session;
|