can-opener-js 0.0.0-stage → 2.0.0
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 +510 -2
- package/dist/ble/index.cjs +1778 -0
- package/dist/ble/index.cjs.map +1 -0
- package/dist/ble/index.d.cts +234 -0
- package/dist/ble/index.d.ts +234 -0
- package/dist/ble/index.js +877 -0
- package/dist/ble/index.js.map +1 -0
- package/dist/chunk-5VWMNZJV.js +1004 -0
- package/dist/chunk-5VWMNZJV.js.map +1 -0
- package/dist/chunk-GK35KBVI.js +54 -0
- package/dist/chunk-GK35KBVI.js.map +1 -0
- package/dist/chunk-H53CBMCZ.js +348 -0
- package/dist/chunk-H53CBMCZ.js.map +1 -0
- package/dist/index.cjs +5250 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1001 -0
- package/dist/index.d.ts +1001 -0
- package/dist/index.js +5132 -0
- package/dist/index.js.map +1 -0
- package/dist/pairing-XB_GL-if.d.cts +359 -0
- package/dist/pairing-XB_GL-if.d.ts +359 -0
- package/dist/react-native-ble/index.cjs +150 -0
- package/dist/react-native-ble/index.cjs.map +1 -0
- package/dist/react-native-ble/index.d.cts +48 -0
- package/dist/react-native-ble/index.d.ts +48 -0
- package/dist/react-native-ble/index.js +104 -0
- package/dist/react-native-ble/index.js.map +1 -0
- package/dist/security/index.cjs +1204 -0
- package/dist/security/index.cjs.map +1 -0
- package/dist/security/index.d.cts +88 -0
- package/dist/security/index.d.ts +88 -0
- package/dist/security/index.js +172 -0
- package/dist/security/index.js.map +1 -0
- package/dist/transport/index.cjs +1177 -0
- package/dist/transport/index.cjs.map +1 -0
- package/dist/transport/index.d.cts +147 -0
- package/dist/transport/index.d.ts +147 -0
- package/dist/transport/index.js +1131 -0
- package/dist/transport/index.js.map +1 -0
- package/dist/types-Bv18cLeE.d.cts +278 -0
- package/dist/types-Bv18cLeE.d.ts +278 -0
- package/package.json +69 -4
package/README.md
CHANGED
|
@@ -1,3 +1,511 @@
|
|
|
1
|
-
#
|
|
1
|
+
# can-opener-js
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React-style vehicle state management for cars. `can-opener-js` gives TypeScript apps a
|
|
4
|
+
virtual vehicle object that monitors the state of a car through named signals and
|
|
5
|
+
uses named actions to control that state. It abstracts DBC files, CAN IDs, byte order,
|
|
6
|
+
scaling, masks, and bit-level signal packing behind named vehicle state.
|
|
7
|
+
|
|
8
|
+
Under the hood, it is a signal-first library for modeling virtual vehicles on
|
|
9
|
+
top of a interpreter layer. Can Opener interfaces with the OBD device.
|
|
10
|
+
Each connected vehicle owns its own vehicle profile, transport, controllers, APIs, and state, so multi-vehicle
|
|
11
|
+
apps do not need globals or shared registries. That makes vehicle scripts
|
|
12
|
+
portable: the same code can run across different cars by swapping the profile,
|
|
13
|
+
DBC files, and transport.
|
|
14
|
+
|
|
15
|
+
It turns signal names like `ENGINE_RPM`, `TURN_SIGNAL_LEFT`, and
|
|
16
|
+
`VEHICLE_SPEED` into DBC-aware state reads, subscriptions, queries, and actions while
|
|
17
|
+
leaving real CAN, BLE, firmware, and ISO-TP details to the transport layer.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npm install can-opener-js
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { VirtualVehicleManager } from "can-opener-js";
|
|
29
|
+
import { MockTransport } from "can-opener-js/transport";
|
|
30
|
+
|
|
31
|
+
const vv = new VirtualVehicleManager();
|
|
32
|
+
const car = await vv.connect({
|
|
33
|
+
id: "tesla-1",
|
|
34
|
+
transport: new MockTransport(),
|
|
35
|
+
profiles: [vehicleProfile],
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Subscribe to one signal. Firmware owns the streaming cadence.
|
|
39
|
+
const rpmSubscribed = await car.subscribe("ENGINE_RPM"); // true
|
|
40
|
+
|
|
41
|
+
// Or subscribe to related signals at once.
|
|
42
|
+
const bodySubscribed = await car.subscribe(["TURN_SIGNAL_LEFT", "Vehicle.Body.Lights.Beam.High.IsOn"]); // true
|
|
43
|
+
const activeSubscriptions = car.subscriptionCount();
|
|
44
|
+
|
|
45
|
+
const speed = await car.query("VEHICLE_SPEED");
|
|
46
|
+
const rpm = car.state.engine_rpm;
|
|
47
|
+
const alsoRpm = car.state.get<number>("ENGINE_RPM");
|
|
48
|
+
|
|
49
|
+
await car.set("Vehicle.Body.Horn.IsActive", true);
|
|
50
|
+
|
|
51
|
+
await car.unsubscribe("ENGINE_RPM");
|
|
52
|
+
await car.unsubscribe(["TURN_SIGNAL_LEFT", "Vehicle.Body.Lights.Beam.High.IsOn"]);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Profile formats
|
|
56
|
+
|
|
57
|
+
Format 2 of the Vehicle Profile Standard is the default. The canonical
|
|
58
|
+
specification, the VSS catalog, the keyword vocabulary, and example packages
|
|
59
|
+
live in [the-can-opener/vehicle-profile-spec](https://github.com/the-can-opener/vehicle-profile-spec).
|
|
60
|
+
|
|
61
|
+
A format 2 package is a manifest (`manifest: 1`, `format: 2`) with the
|
|
62
|
+
documents and DBC files it lists. Pass the manifest as `content` and each
|
|
63
|
+
listed document under `documents`, named by the path the manifest gives it:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
const car = await vv.connect({
|
|
67
|
+
id: "versa",
|
|
68
|
+
transport,
|
|
69
|
+
profiles: [{
|
|
70
|
+
name: "org.canopener.nissan.versa",
|
|
71
|
+
content: manifestYaml,
|
|
72
|
+
documents: [
|
|
73
|
+
{ name: "base.yaml", content: baseYaml },
|
|
74
|
+
{ name: "body.yaml", content: bodyYaml },
|
|
75
|
+
],
|
|
76
|
+
dbcFiles: [{ name: "signals.dbc", content: signalsDbc }],
|
|
77
|
+
}],
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
A loaded vehicle exposes properties keyed by VSS path, plus named queries and
|
|
82
|
+
named actions for what has no VSS path. The VSS methods take VSS paths only:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
await car.subscribe("Vehicle.Speed");
|
|
86
|
+
car.state.get<number>("Vehicle.Speed");
|
|
87
|
+
|
|
88
|
+
await car.read<number>("Vehicle.Speed"); // asks the ECU when the property has a query
|
|
89
|
+
await car.set("Vehicle.Cabin.IsCentralLockEngaged", true); // "acknowledged" | "sent" | "rejected" | "unknown"
|
|
90
|
+
await car.invoke("SIGNAL_OFF"); // named action
|
|
91
|
+
await car.query("SUPPORTED_PIDS_01_20"); // named query
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
A write never changes `car.state`; only an observation of the property does.
|
|
95
|
+
Setting a target the package has no request for throws and sends nothing.
|
|
96
|
+
|
|
97
|
+
The alias methods take legacy keywords only, never a VSS path. A property
|
|
98
|
+
alias names a path (`SPEED`, `LOCKED`). A command alias names one write target
|
|
99
|
+
(`LOCK` writes `true`, `UNLOCK` writes `false`, `HORN` writes `true`) or a
|
|
100
|
+
named action (`SIGNAL_OFF`):
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
await car.alias.read("SPEED");
|
|
104
|
+
await car.alias.subscribe("SPEED"); // the value lands in state under "Vehicle.Speed"
|
|
105
|
+
await car.alias.set("DRIVER_TEMP_SETTING", 22);
|
|
106
|
+
await car.alias.command("LOCK"); // same request as set("Vehicle.Cabin.IsCentralLockEngaged", true)
|
|
107
|
+
car.alias.list(); // every alias the loaded profiles answer to
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Passing `SPEED` or `LOCK` to a VSS method throws, and so does passing a VSS
|
|
111
|
+
path or a command alias to `alias.read`, `alias.subscribe`, or `alias.set`.
|
|
112
|
+
|
|
113
|
+
`allowedKeys` on a profile source limits what loads from it: `signals` and
|
|
114
|
+
`queries` list the property paths (and named queries) to keep, `actions` lists
|
|
115
|
+
the writable paths and named actions. A write case name such as
|
|
116
|
+
`Vehicle.Cabin.IsCentralLockEngaged=true` in `actions` keeps that one case of
|
|
117
|
+
the property. An omitted list keeps everything of that kind.
|
|
118
|
+
|
|
119
|
+
Format 1 (`version: 1` and `version: 1.1`) is legacy and still loads. The
|
|
120
|
+
loader maps its keywords onto VSS paths with the spec vocabulary: a `SPEED`
|
|
121
|
+
query becomes the query of `Vehicle.Speed`, `DOORS.lock` becomes the `true`
|
|
122
|
+
target of `Vehicle.Cabin.IsCentralLockEngaged`. A keyword the vocabulary gives
|
|
123
|
+
no path keeps its original name, so `ENGINE_RPM` and `VEHICLE_SPEED` in the
|
|
124
|
+
examples below still work as written. The rest of this README describes format
|
|
125
|
+
1 profiles.
|
|
126
|
+
|
|
127
|
+
Not supported yet in format 2: a `repeat` write and a request longer than one
|
|
128
|
+
ISO-TP frame load as unavailable and throw with the reason when used. A
|
|
129
|
+
package whose `depends` id is not loaded before it is skipped with a warning.
|
|
130
|
+
|
|
131
|
+
## Core Ideas
|
|
132
|
+
|
|
133
|
+
- Treat the vehicle like app state: subscribe to signals, read the latest values
|
|
134
|
+
from `car.state`, and run queries or actions through a consistent API.
|
|
135
|
+
- Applications use signal names. The DBC controller maps those names to CAN IDs,
|
|
136
|
+
bit layout, protocol metadata, and diagnostic bindings.
|
|
137
|
+
- `frame` signals are decoded from incoming CAN frames and can be subscribed to.
|
|
138
|
+
- Diagnostic signals are requested on demand through `query()` or polled through
|
|
139
|
+
`subscribeQuery()`.
|
|
140
|
+
- A transport only sends and receives frames. It does not need to parse DBC
|
|
141
|
+
files, know app-level signal names, or implement vehicle state.
|
|
142
|
+
- Multiple vehicles can be connected at the same time. Each vehicle has isolated
|
|
143
|
+
state and DBC bindings.
|
|
144
|
+
|
|
145
|
+
## Profiles And DBC
|
|
146
|
+
|
|
147
|
+
DBC files define received CAN messages and signal bit layouts. YAML profiles
|
|
148
|
+
define the executable capabilities: endpoints, request/response queries,
|
|
149
|
+
actions, monitor subscriptions, applicability, and DBC mapping references. The
|
|
150
|
+
library does not ship a built-in catalog of PIDs, actions, request bytes, or
|
|
151
|
+
DBC mappings; loaded YAML+DBC files are the source of truth.
|
|
152
|
+
|
|
153
|
+
```yaml
|
|
154
|
+
version: 1
|
|
155
|
+
|
|
156
|
+
dbc:
|
|
157
|
+
files:
|
|
158
|
+
- path: signals.dbc
|
|
159
|
+
|
|
160
|
+
endpoints:
|
|
161
|
+
obd:
|
|
162
|
+
request_id: 0x7DF
|
|
163
|
+
response_ids:
|
|
164
|
+
- range: [0x7E8, 0x7EF]
|
|
165
|
+
timeout_ms: 500
|
|
166
|
+
|
|
167
|
+
queries:
|
|
168
|
+
SPEED:
|
|
169
|
+
endpoint: obd
|
|
170
|
+
send: [0x02, 0x01, 0x0D, 0, 0, 0, 0, 0]
|
|
171
|
+
expect: [0x41, 0x0D]
|
|
172
|
+
dbc_mapping:
|
|
173
|
+
message: OBD_Response_7E8
|
|
174
|
+
signal: SPEED
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
```dbc
|
|
178
|
+
BO_ 2024 OBD_Response_7E8: 8 ECU
|
|
179
|
+
SG_ RESPONSE_SERVICE : 0|8@1+ (1,0) [0|255] "" Vector__XXX
|
|
180
|
+
SG_ PID M : 8|8@1+ (1,0) [0|255] "" Vector__XXX
|
|
181
|
+
SG_ SPEED m13 : 16|8@1+ (1,0) [0|255] "km/h" Vector__XXX
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Raw DBC-only loading is still supported for simple frame subscriptions and
|
|
185
|
+
legacy tests. In profile-backed vehicles, executable names come from YAML
|
|
186
|
+
`queries`, `actions`, and `signals` declarations.
|
|
187
|
+
|
|
188
|
+
See the [Vehicle Profile and Library Specification](docs/vehicle-profile-spec.md)
|
|
189
|
+
for legacy format 1 YAML profiles, DBC files, capabilities, and the library contract, and
|
|
190
|
+
[Vehicle Keywords](docs/vehicle-keywords.md) for the common capability names
|
|
191
|
+
profiles should use. See the
|
|
192
|
+
[BLE Transport Protocol](docs/can-opener-se-ble-protocol.md) and
|
|
193
|
+
[Security Protocol](docs/can-opener-se-security-protocol.md) when implementing
|
|
194
|
+
an app for current Can Opener SE firmware.
|
|
195
|
+
|
|
196
|
+
DBC `VAL_` entries expose enum-like signal values as labels by default. Numeric
|
|
197
|
+
signals without matching `VAL_` entries return their DBC-scaled physical values:
|
|
198
|
+
|
|
199
|
+
```dbc
|
|
200
|
+
BO_ 100 DRIVE_STATUS: 8 BCM
|
|
201
|
+
SG_ DRIVE_MODE : 0|2@1+ (1,0) [0|3] "" Vector__XXX
|
|
202
|
+
|
|
203
|
+
VAL_ 100 DRIVE_MODE 0 "park" 1 "reverse" 2 "drive" 3 "low";
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
`DRIVE_MODE` remains the physical DBC signal, and subscribing to it updates
|
|
207
|
+
state to labels like `"park"` or `"drive"`. Profile `normalize` blocks can
|
|
208
|
+
remap car-specific DBC labels when a portable script needs a canonical
|
|
209
|
+
vocabulary.
|
|
210
|
+
|
|
211
|
+
The test fixtures include `tests/fixtures/vehicles/universal/pid/profile.yaml`
|
|
212
|
+
and `tests/fixtures/vehicles/universal/pid/signals.dbc` as a starter profile
|
|
213
|
+
pair for common OBD-II PID queries.
|
|
214
|
+
|
|
215
|
+
## Connecting Vehicles
|
|
216
|
+
|
|
217
|
+
Use `VirtualVehicleManager` to connect and track vehicles:
|
|
218
|
+
|
|
219
|
+
```ts
|
|
220
|
+
const manager = new VirtualVehicleManager();
|
|
221
|
+
|
|
222
|
+
const car = await manager.connect({
|
|
223
|
+
id: "car-a",
|
|
224
|
+
transport: new MockTransport(),
|
|
225
|
+
profiles: [profile],
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
manager.get("car-a");
|
|
229
|
+
manager.list();
|
|
230
|
+
await manager.disconnect("car-a");
|
|
231
|
+
await manager.disconnectAll();
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Calling `connect()` loads profile-declared DBC files, creates isolated
|
|
235
|
+
controllers and state, connects the transport, and wires incoming transport
|
|
236
|
+
frames into subscription decoding.
|
|
237
|
+
|
|
238
|
+
## Subscriptions
|
|
239
|
+
|
|
240
|
+
Frame subscriptions are requested by signal name, and the underlying transport
|
|
241
|
+
subscription is tracked per CAN frame. Firmware owns the streaming cadence, so
|
|
242
|
+
`subscribe()` returns `true` once the firmware monitor list has accepted the
|
|
243
|
+
request. Frame subscriptions stay open until `unsubscribe()` is called.
|
|
244
|
+
If `TURN_SIGNAL_LEFT` and `HIGH_BEAMS` are encoded in the same CAN ID, the
|
|
245
|
+
vehicle keeps one frame subscription and modifies it as signal names are added
|
|
246
|
+
or removed. Diagnostic query polling is intentionally a separate API.
|
|
247
|
+
|
|
248
|
+
```ts
|
|
249
|
+
const subscribed = await car.subscribe("ENGINE_RPM"); // true
|
|
250
|
+
const activeCount = car.subscriptionCount(); // 1
|
|
251
|
+
|
|
252
|
+
await car.unsubscribe("ENGINE_RPM");
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
You can also declare related subscriptions together:
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
const subscribed = await car.subscribe(["TURN_SIGNAL_LEFT", "Vehicle.Body.Lights.Beam.High.IsOn"]); // true
|
|
259
|
+
const activeCount = car.subscriptionCount(); // 2
|
|
260
|
+
|
|
261
|
+
await car.unsubscribe(["TURN_SIGNAL_LEFT", "Vehicle.Body.Lights.Beam.High.IsOn"]);
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
Profile-backed subscriptions use names declared under YAML `signals`. Without
|
|
265
|
+
profile normalization, incoming values are the DBC-decoded values: enum labels
|
|
266
|
+
from `VAL_` when present, otherwise scaled physical numbers. A profile can add
|
|
267
|
+
`normalize.enum` to remap enum labels before values are written to `car.state`.
|
|
268
|
+
|
|
269
|
+
When multiple signals share a CAN frame, the transport receives one merged
|
|
270
|
+
subscription request for that frame. Removing the last active signal for a CAN
|
|
271
|
+
frame unsubscribes the transport from that frame.
|
|
272
|
+
|
|
273
|
+
Incoming frames update `car.state` only for actively subscribed signals:
|
|
274
|
+
|
|
275
|
+
```ts
|
|
276
|
+
transport.emitFrame(car.dbc.encodeSignal("ENGINE_RPM", 900));
|
|
277
|
+
|
|
278
|
+
car.state.get<number>("ENGINE_RPM"); // 900
|
|
279
|
+
car.state.engine_rpm; // 900
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
Query polling subscriptions use `subscribeQuery()`, which returns a handle that
|
|
283
|
+
can be passed to `unsubscribeQuery()` later:
|
|
284
|
+
|
|
285
|
+
```ts
|
|
286
|
+
const speedSubscription = await car.subscribeQuery("VEHICLE_SPEED", {
|
|
287
|
+
frequencyHz: 2,
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
car.unsubscribeQuery(speedSubscription);
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## Queries
|
|
294
|
+
|
|
295
|
+
Use `read()` for a property with a query binding, and `query()` for a named
|
|
296
|
+
query, which is a name declared under profile `queries` that has no VSS path:
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
const speed = await car.read<number>("Vehicle.Speed");
|
|
300
|
+
const sameSpeed = await car.alias.read<number>("SPEED");
|
|
301
|
+
const supported = await car.query("SUPPORTED_PIDS_01_20");
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
The YAML profile describes the endpoint, request bytes, accepted response IDs,
|
|
305
|
+
expected response prefix, and either a `dbc_mapping` or built-in `decoder`. The
|
|
306
|
+
transport receives a raw request frame through `sendRequest()` with
|
|
307
|
+
`expectCanResponse: true`. The response is validated against `expect` and
|
|
308
|
+
decoded by the referenced DBC signal or built-in decoder.
|
|
309
|
+
|
|
310
|
+
Calling `query()` for a name not declared in the profile throws an unknown
|
|
311
|
+
signal error.
|
|
312
|
+
|
|
313
|
+
## Actions
|
|
314
|
+
|
|
315
|
+
Use `set()` to write a property and `invoke()` for a named action, which is a
|
|
316
|
+
name declared under profile `actions` that has no single VSS actuator:
|
|
317
|
+
|
|
318
|
+
```ts
|
|
319
|
+
await car.set("Vehicle.Body.Horn.IsActive", true);
|
|
320
|
+
await car.invoke("FLASH_LIGHTS");
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
Actions can be request-only, verified request/response, or multi-step flows.
|
|
324
|
+
Raw DBC-only vehicles, with no profile loaded, can still use `action()` with
|
|
325
|
+
action options to send encoded frame signals.
|
|
326
|
+
|
|
327
|
+
## Vehicle State
|
|
328
|
+
|
|
329
|
+
Each vehicle exposes a `VehicleState` instance backed by `valtio/vanilla`:
|
|
330
|
+
|
|
331
|
+
```ts
|
|
332
|
+
const rpm = car.state.get<number>("ENGINE_RPM");
|
|
333
|
+
const sameRpm = car.state.engine_rpm;
|
|
334
|
+
|
|
335
|
+
const dispose = car.state.subscribe(() => {
|
|
336
|
+
console.log(car.state.snapshot());
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
dispose();
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
State keys are stored by the subscribed name. In profile-backed vehicles, that
|
|
343
|
+
is the VSS path, or the YAML `signals` or `queries` key for a format 1 keyword
|
|
344
|
+
outside the vocabulary. Property access also accepts common
|
|
345
|
+
camelCase or snake_case variants by converting them to upper snake case.
|
|
346
|
+
|
|
347
|
+
## Transport
|
|
348
|
+
|
|
349
|
+
The core transport is intentionally dumb:
|
|
350
|
+
|
|
351
|
+
- it connects and disconnects;
|
|
352
|
+
- it sends raw CAN requests through one request path;
|
|
353
|
+
- it configures the firmware monitor list for subscriptions;
|
|
354
|
+
- it receives monitor snapshots containing the latest raw CAN frames;
|
|
355
|
+
- it does not parse DBC, ISO-TP, UDS, or app-level signal names.
|
|
356
|
+
|
|
357
|
+
`MockTransport` ships with the library for tests and local simulations.
|
|
358
|
+
`SlcanTransport` speaks Lawicel SLCAN over a byte port, including ISO-TP
|
|
359
|
+
reassembly for `sendRequest()`. A browser opens that port with Web Serial:
|
|
360
|
+
|
|
361
|
+
```ts
|
|
362
|
+
import { SlcanTransport, openWebSerialSlcanPort, requestWebSerialPort } from "can-opener-js/transport";
|
|
363
|
+
|
|
364
|
+
const port = await requestWebSerialPort();
|
|
365
|
+
const transport = new SlcanTransport(await openWebSerialSlcanPort(port), { bitrate: 500_000 });
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
Pass `canFd: true` when the adapter runs CANable 2 firmware. The transport then
|
|
369
|
+
sends CAN FD requests with that firmware's `d`/`b` commands, accepts
|
|
370
|
+
`dataBitrate` 2 or 5 Mbit/s in `configureBus` (`Y2`/`Y5`, which turns on bit
|
|
371
|
+
rate switching), and parses received CAN FD frames. Without it, CAN FD requests
|
|
372
|
+
are rejected, because CANable 1 firmware ignores those commands silently.
|
|
373
|
+
|
|
374
|
+
`examples/slcan-web` is a small page that connects a CANable and runs the Nissan
|
|
375
|
+
Versa profile through this transport. `npm run dev:slcan` starts it.
|
|
376
|
+
|
|
377
|
+
### Can Opener SE over BLE
|
|
378
|
+
|
|
379
|
+
`CanOpenerBleTransport` (`can-opener-js/ble`) speaks the
|
|
380
|
+
[BLE Transport Protocol](docs/can-opener-se-ble-protocol.md): requests,
|
|
381
|
+
CAN FD payloads, batches, monitoring, and bus timing. It does not depend on a
|
|
382
|
+
BLE stack. The app connects the adapter, discovers services, and passes the
|
|
383
|
+
transport a `CanOpenerGattLink`. `can-opener-js/react-native-ble` provides one
|
|
384
|
+
for react-native-ble-plx:
|
|
385
|
+
|
|
386
|
+
```ts
|
|
387
|
+
import { Platform } from "react-native";
|
|
388
|
+
import { BleManager } from "react-native-ble-plx";
|
|
389
|
+
import { CanOpenerBleTransport } from "can-opener-js/ble";
|
|
390
|
+
import { bleplxGattLink, bleplxSecurityWritePolicy } from "can-opener-js/react-native-ble";
|
|
391
|
+
import { credentialAuthorizer } from "can-opener-js/security";
|
|
392
|
+
|
|
393
|
+
const ble = new BleManager();
|
|
394
|
+
const device = await ble.connectToDevice(deviceId, { requestMTU: 247 });
|
|
395
|
+
await device.discoverAllServicesAndCharacteristics();
|
|
396
|
+
|
|
397
|
+
const transport = new CanOpenerBleTransport(bleplxGattLink(ble, device.id), {
|
|
398
|
+
authorize: credentialAuthorizer(credentialStore),
|
|
399
|
+
securityWritePolicy: bleplxSecurityWritePolicy(Platform.OS === "android" ? "android" : "ios"),
|
|
400
|
+
subscribeSettleMs: Platform.OS === "android" ? 80 : 0,
|
|
401
|
+
});
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
The adapter rejects vehicle notifications until the phone proves a key on the
|
|
405
|
+
security characteristic, so `authorize` runs inside `connect()` before they are
|
|
406
|
+
enabled. `credentialStore` is the app's `CredentialStore`: it lists the keys
|
|
407
|
+
saved for adapters and saves what authentication reports, such as a redeemed
|
|
408
|
+
guest invite. `can-opener-js/security` also covers claiming an adapter
|
|
409
|
+
(`withSecuritySession(link, (session) => session.claim(seed))`), backup codes,
|
|
410
|
+
invite links, and guest management through `transport.accessSession`.
|
|
411
|
+
|
|
412
|
+
The firmware matches replies by sequence byte only. When another client shares
|
|
413
|
+
the link, such as an iOS Siri extension using `0x80`-`0xFF`, give each one a
|
|
414
|
+
disjoint `sequenceRange`; the default is `0x00`-`0x7F`.
|
|
415
|
+
|
|
416
|
+
Other transports implement `VehicleTransport` the same way. `CanOpenerBleTransport`
|
|
417
|
+
maps its methods onto the firmware characteristics:
|
|
418
|
+
|
|
419
|
+
- `sendRequest()` maps to the Request characteristic. Actions use
|
|
420
|
+
`expectCanResponse: false`; PID and diagnostic reads use
|
|
421
|
+
`expectCanResponse: true`.
|
|
422
|
+
- `updateMonitor()` maps to the Monitor Control characteristic with `add`,
|
|
423
|
+
`remove`, and `clear` operations.
|
|
424
|
+
- `onMonitorSnapshot()` maps to Monitor Data notifications. Each snapshot
|
|
425
|
+
contains the current monitored frame set, with CAN ID, DLC, and up to 8 data
|
|
426
|
+
bytes per frame.
|
|
427
|
+
|
|
428
|
+
```ts
|
|
429
|
+
import type {
|
|
430
|
+
CanPayload,
|
|
431
|
+
MonitorControlRequest,
|
|
432
|
+
MonitorControlResponse,
|
|
433
|
+
MonitorSnapshot,
|
|
434
|
+
VehicleRequest,
|
|
435
|
+
VehicleTransport,
|
|
436
|
+
} from "can-opener-js/transport";
|
|
437
|
+
|
|
438
|
+
class MyTransport implements VehicleTransport {
|
|
439
|
+
async connect(): Promise<void> {}
|
|
440
|
+
async disconnect(): Promise<void> {}
|
|
441
|
+
async sendRequest(req: VehicleRequest): Promise<CanPayload | undefined> {
|
|
442
|
+
if (!req.expectCanResponse) {
|
|
443
|
+
return undefined;
|
|
444
|
+
}
|
|
445
|
+
if (req.diagnostic?.transport === "isotp") {
|
|
446
|
+
// Gather and return the reassembled logical diagnostic payload here.
|
|
447
|
+
}
|
|
448
|
+
return new Uint8Array();
|
|
449
|
+
}
|
|
450
|
+
async updateMonitor(req: MonitorControlRequest): Promise<MonitorControlResponse> {
|
|
451
|
+
return { status: "ok", currentMonitorCount: 0 };
|
|
452
|
+
}
|
|
453
|
+
onMonitorSnapshot(cb: (snapshot: MonitorSnapshot) => void): () => void {
|
|
454
|
+
return () => {};
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
`onMonitorSnapshot()` should register a callback for monitor data notifications
|
|
460
|
+
and return a dispose function. The vehicle calls that dispose function when
|
|
461
|
+
disconnected.
|
|
462
|
+
|
|
463
|
+
## Multiple vehicles
|
|
464
|
+
|
|
465
|
+
`VirtualVehicleManager` never uses globals. Each connected vehicle has isolated
|
|
466
|
+
state, DBC registry, controller instances, and transport.
|
|
467
|
+
|
|
468
|
+
```ts
|
|
469
|
+
const vehicleA = await manager.connect({
|
|
470
|
+
id: "car-a",
|
|
471
|
+
transport: transportA,
|
|
472
|
+
dbcFiles,
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
const vehicleB = await manager.connect({
|
|
476
|
+
id: "car-b",
|
|
477
|
+
transport: transportB,
|
|
478
|
+
dbcFiles,
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
vehicleA.state.engine_rpm;
|
|
482
|
+
vehicleB.state.engine_rpm;
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
## Development
|
|
486
|
+
|
|
487
|
+
```sh
|
|
488
|
+
npm test
|
|
489
|
+
npm run typecheck
|
|
490
|
+
npm run build
|
|
491
|
+
```
|
|
492
|
+
|
|
493
|
+
The test suite uses `MockTransport` to simulate PID responses, action sends,
|
|
494
|
+
subscription registration, and incoming CAN frames. The example packages of
|
|
495
|
+
the vehicle-profile-spec repository are copied under `tests/fixtures/spec` and
|
|
496
|
+
their fixture files run as tests.
|
|
497
|
+
|
|
498
|
+
The keyword vocabulary and the VSS leaf map under `src/profile/loaders` are
|
|
499
|
+
generated from a checkout of the spec repository:
|
|
500
|
+
|
|
501
|
+
```sh
|
|
502
|
+
node scripts/generate-vss-leaves.mjs ../vehicle-profile-spec
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
## Out of scope for v1
|
|
506
|
+
|
|
507
|
+
- BLE link adapters other than react-native-ble-plx. Web Bluetooth or Node BLE
|
|
508
|
+
only need a `CanOpenerGattLink`; `CanOpenerBleTransport` stays the same.
|
|
509
|
+
- ISO-TP assembly/disassembly, which should remain firmware-side.
|
|
510
|
+
- React bindings. The core uses `valtio/vanilla`, so a React entry point can be
|
|
511
|
+
added without changing the vehicle model.
|