can-opener-js 2.0.0 → 2.1.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/dist/index.d.cts CHANGED
@@ -256,6 +256,8 @@ interface ProfileQuery {
256
256
  length?: number;
257
257
  timeoutMs?: number;
258
258
  normalize?: ProfileValueNormalization;
259
+ /** Narrowed numeric observation domain (`read.constraints`). */
260
+ constraints?: ProfileNumberInput;
259
261
  /** Named queries only: the declared datatype and unit of the result. */
260
262
  datatype?: string;
261
263
  unit?: string;
@@ -311,6 +313,8 @@ interface ProfileSignalBase {
311
313
  name: string;
312
314
  /** Logical CAN controller. Omitted values default to bus 0. */
313
315
  bus?: number;
316
+ /** Narrowed numeric observation domain (`read.constraints`). */
317
+ constraints?: ProfileNumberInput;
314
318
  }
315
319
  interface ProfileDbcSignal extends ProfileSignalBase {
316
320
  message: string;
@@ -347,6 +351,8 @@ interface ProfileProperty {
347
351
  allowed?: string[];
348
352
  /** Legacy names the package declares for this property. */
349
353
  aliases?: string[];
354
+ /** Narrowed numeric observation domain (`read.constraints`). */
355
+ readConstraints?: ProfileNumberInput;
350
356
  /** Passive read from broadcast CAN traffic. */
351
357
  signal?: ProfileMonitorSignal;
352
358
  /** Active read: a request sent to an ECU. */
@@ -467,6 +473,7 @@ declare class ActionController {
467
473
  private readonly transport;
468
474
  private readonly capabilities?;
469
475
  private readonly scheduled;
476
+ private readonly endpointLeases;
470
477
  constructor(dbc: DbcController, transport: VehicleTransport, capabilities?: CapabilityRegistry | undefined);
471
478
  send(signal: VehicleSignal, opts: ActionOptions): Promise<void>;
472
479
  clearScheduled(): void;
@@ -570,6 +577,7 @@ declare class QueryController {
570
577
  private poll;
571
578
  private resolveEndpoint;
572
579
  private decodeProfileQuery;
580
+ private decodeProfileQueryValue;
573
581
  }
574
582
 
575
583
  interface QueryRoundRobinStatus {
package/dist/index.d.ts CHANGED
@@ -256,6 +256,8 @@ interface ProfileQuery {
256
256
  length?: number;
257
257
  timeoutMs?: number;
258
258
  normalize?: ProfileValueNormalization;
259
+ /** Narrowed numeric observation domain (`read.constraints`). */
260
+ constraints?: ProfileNumberInput;
259
261
  /** Named queries only: the declared datatype and unit of the result. */
260
262
  datatype?: string;
261
263
  unit?: string;
@@ -311,6 +313,8 @@ interface ProfileSignalBase {
311
313
  name: string;
312
314
  /** Logical CAN controller. Omitted values default to bus 0. */
313
315
  bus?: number;
316
+ /** Narrowed numeric observation domain (`read.constraints`). */
317
+ constraints?: ProfileNumberInput;
314
318
  }
315
319
  interface ProfileDbcSignal extends ProfileSignalBase {
316
320
  message: string;
@@ -347,6 +351,8 @@ interface ProfileProperty {
347
351
  allowed?: string[];
348
352
  /** Legacy names the package declares for this property. */
349
353
  aliases?: string[];
354
+ /** Narrowed numeric observation domain (`read.constraints`). */
355
+ readConstraints?: ProfileNumberInput;
350
356
  /** Passive read from broadcast CAN traffic. */
351
357
  signal?: ProfileMonitorSignal;
352
358
  /** Active read: a request sent to an ECU. */
@@ -467,6 +473,7 @@ declare class ActionController {
467
473
  private readonly transport;
468
474
  private readonly capabilities?;
469
475
  private readonly scheduled;
476
+ private readonly endpointLeases;
470
477
  constructor(dbc: DbcController, transport: VehicleTransport, capabilities?: CapabilityRegistry | undefined);
471
478
  send(signal: VehicleSignal, opts: ActionOptions): Promise<void>;
472
479
  clearScheduled(): void;
@@ -570,6 +577,7 @@ declare class QueryController {
570
577
  private poll;
571
578
  private resolveEndpoint;
572
579
  private decodeProfileQuery;
580
+ private decodeProfileQueryValue;
573
581
  }
574
582
 
575
583
  interface QueryRoundRobinStatus {
package/dist/index.js CHANGED
@@ -208,6 +208,45 @@ function groupStepsByBus(steps) {
208
208
  }
209
209
  return groups;
210
210
  }
211
+ var EndpointLeasePool = class {
212
+ tails = /* @__PURE__ */ new Map();
213
+ async acquire(names) {
214
+ const sorted = Array.from(new Set(names)).sort();
215
+ const releases = [];
216
+ for (const name of sorted) {
217
+ const previous = this.tails.get(name) ?? Promise.resolve();
218
+ let release;
219
+ const next = new Promise((resolve) => {
220
+ release = resolve;
221
+ });
222
+ const tail = previous.then(() => next);
223
+ this.tails.set(name, tail);
224
+ await previous;
225
+ releases.push(() => {
226
+ release();
227
+ if (this.tails.get(name) === tail) {
228
+ this.tails.delete(name);
229
+ }
230
+ });
231
+ }
232
+ return () => {
233
+ for (let index = releases.length - 1; index >= 0; index -= 1) {
234
+ releases[index]?.();
235
+ }
236
+ };
237
+ }
238
+ };
239
+ function actionEndpointNames(action) {
240
+ const names = /* @__PURE__ */ new Set();
241
+ for (const step of action.steps) {
242
+ if (step.requestId !== void 0) continue;
243
+ const endpoint = step.endpoint ?? action.endpoint;
244
+ if (endpoint !== void 0) {
245
+ names.add(endpoint);
246
+ }
247
+ }
248
+ return Array.from(names).sort();
249
+ }
211
250
  var ActionController = class {
212
251
  constructor(dbc, transport, capabilities) {
213
252
  this.dbc = dbc;
@@ -218,6 +257,7 @@ var ActionController = class {
218
257
  transport;
219
258
  capabilities;
220
259
  scheduled = /* @__PURE__ */ new Set();
260
+ endpointLeases = new EndpointLeasePool();
221
261
  async send(signal, opts) {
222
262
  const frame = this.applyMask(
223
263
  this.dbc.encodeSignal(signal.name, opts.value),
@@ -260,23 +300,28 @@ var ActionController = class {
260
300
  this.scheduled.clear();
261
301
  }
262
302
  async run(action, options = {}) {
263
- if (options.wakeFirst === true && action.name !== ECU_WAKE_ACTION) {
264
- const wakeAction = this.resolveEcuWakeAction();
265
- if (wakeAction !== void 0) {
266
- return this.runAfterEcuWake(action, wakeAction);
267
- }
268
- }
303
+ const release = await this.endpointLeases.acquire(actionEndpointNames(action));
269
304
  try {
270
- return await this.runSteps(action);
271
- } catch (error) {
272
- if (!isSilentBusError(error) || action.name === ECU_WAKE_ACTION) {
273
- throw error;
305
+ if (options.wakeFirst === true && action.name !== ECU_WAKE_ACTION) {
306
+ const wakeAction = this.resolveEcuWakeAction();
307
+ if (wakeAction !== void 0) {
308
+ return await this.runAfterEcuWake(action, wakeAction);
309
+ }
274
310
  }
275
- const wakeAction = this.resolveEcuWakeAction();
276
- if (wakeAction === void 0) {
277
- throw error;
311
+ try {
312
+ return await this.runSteps(action);
313
+ } catch (error) {
314
+ if (!isSilentBusError(error) || action.name === ECU_WAKE_ACTION) {
315
+ throw error;
316
+ }
317
+ const wakeAction = this.resolveEcuWakeAction();
318
+ if (wakeAction === void 0) {
319
+ throw error;
320
+ }
321
+ return await this.runAfterEcuWake(action, wakeAction);
278
322
  }
279
- return this.runAfterEcuWake(action, wakeAction);
323
+ } finally {
324
+ release();
280
325
  }
281
326
  }
282
327
  /**
@@ -632,6 +677,27 @@ function applyValueNormalization(value, normalize, sourceUnit) {
632
677
  }
633
678
  return result;
634
679
  }
680
+ function isWithinNumericConstraints(value, constraints) {
681
+ if (constraints === void 0 || typeof value !== "number") {
682
+ return true;
683
+ }
684
+ if (!Number.isFinite(value)) {
685
+ return false;
686
+ }
687
+ if (constraints.min !== void 0 && value < constraints.min) {
688
+ return false;
689
+ }
690
+ if (constraints.max !== void 0 && value > constraints.max) {
691
+ return false;
692
+ }
693
+ if (constraints.step !== void 0 && constraints.step > 0) {
694
+ const steps = (value - (constraints.min ?? 0)) / constraints.step;
695
+ if (Math.abs(steps - Math.round(steps)) > 1e-9) {
696
+ return false;
697
+ }
698
+ }
699
+ return true;
700
+ }
635
701
 
636
702
  // src/controllers/QueryController.ts
637
703
  var DEFAULT_QUERY_SUBSCRIPTION_FREQUENCY_HZ = 1;
@@ -771,6 +837,10 @@ var QueryController = class {
771
837
  return this.capabilities.resolveEndpoint(name);
772
838
  }
773
839
  decodeProfileQuery(query, payload) {
840
+ const value = this.decodeProfileQueryValue(query, payload);
841
+ return isWithinNumericConstraints(value, query.constraints) ? value : void 0;
842
+ }
843
+ decodeProfileQueryValue(query, payload) {
774
844
  const decoder = query.decoder;
775
845
  if (decoder === void 0) {
776
846
  return applyValueNormalization(stripExpectedPrefix(query.expect, payload), query.normalize);
@@ -3548,11 +3618,11 @@ function parseDocument(content, name) {
3548
3618
  }
3549
3619
  function channelBuses(manifest, composed) {
3550
3620
  const names = new Set(Object.keys(composed.channels));
3551
- for (const endpoint of Object.values(composed.endpoints)) {
3552
- names.add(endpoint.channel ?? DEFAULT_CHANNEL);
3553
- }
3554
3621
  for (const dbc of Object.values(manifest.dbc ?? {})) {
3555
- names.add(dbc.channel ?? DEFAULT_CHANNEL);
3622
+ const channel = dbc.channel ?? DEFAULT_CHANNEL;
3623
+ if (composed.channels[channel] !== void 0 || /^can\d+$/u.test(channel)) {
3624
+ names.add(channel);
3625
+ }
3556
3626
  }
3557
3627
  const buses = /* @__PURE__ */ new Map();
3558
3628
  for (const name of names) {
@@ -3643,12 +3713,16 @@ function lowerAddressing(addressing, path) {
3643
3713
  function lowerProperty(path, property, context) {
3644
3714
  const signal = property.read?.signal;
3645
3715
  const query = property.read?.query;
3716
+ const readConstraints = lowerConstraints(property.read?.constraints);
3717
+ const loweredSignal = signal != null ? lowerSignal(path, signal, context) : void 0;
3718
+ const loweredQuery = query != null ? lowerPropertyQuery(path, query, context) : void 0;
3646
3719
  return {
3647
3720
  path,
3648
3721
  ...vssLeaf(path),
3649
3722
  ...property.aliases !== void 0 && property.aliases.length > 0 ? { aliases: property.aliases } : {},
3650
- ...signal != null ? { signal: lowerSignal(path, signal, context) } : {},
3651
- ...query != null ? { query: lowerPropertyQuery(path, query, context) } : {},
3723
+ ...readConstraints !== void 0 ? { readConstraints } : {},
3724
+ ...loweredSignal !== void 0 ? { signal: readConstraints !== void 0 ? { ...loweredSignal, constraints: readConstraints } : loweredSignal } : {},
3725
+ ...loweredQuery !== void 0 ? { query: readConstraints !== void 0 ? { ...loweredQuery, constraints: readConstraints } : loweredQuery } : {},
3652
3726
  ...property.write != null ? { write: lowerWrite(path, property.write, context) } : {}
3653
3727
  };
3654
3728
  }
@@ -3736,31 +3810,45 @@ function lowerQuery(name, query, decoder, context) {
3736
3810
  ...unavailable !== void 0 ? { unavailable } : {}
3737
3811
  };
3738
3812
  }
3813
+ function lowerConstraints(raw) {
3814
+ const constraints = raw;
3815
+ if (constraints == null) {
3816
+ return void 0;
3817
+ }
3818
+ return {
3819
+ ...constraints.min != null ? { min: constraints.min } : {},
3820
+ ...constraints.max != null ? { max: constraints.max } : {},
3821
+ ...constraints.step != null ? { step: constraints.step } : {}
3822
+ };
3823
+ }
3739
3824
  function lowerWrite(path, write, context) {
3740
- const constraints = write.constraints;
3825
+ const constraints = lowerConstraints(write.constraints);
3741
3826
  return {
3742
3827
  ..."cases" in write ? {
3743
3828
  cases: Object.fromEntries(
3744
3829
  Object.entries(write.cases).map(([target, body]) => [target, lowerBody(`${path}=${target}`, body, context)])
3745
3830
  )
3746
3831
  } : { direct: lowerBody(path, write, context) },
3747
- ...constraints != null ? {
3748
- constraints: {
3749
- ...constraints.min != null ? { min: constraints.min } : {},
3750
- ...constraints.max != null ? { max: constraints.max } : {},
3751
- ...constraints.step != null ? { step: constraints.step } : {}
3752
- }
3753
- } : {}
3832
+ ...constraints !== void 0 ? { constraints } : {}
3754
3833
  };
3755
3834
  }
3756
3835
  function lowerBody(name, body, context) {
3757
- const endpoint = resolveEndpoint(name, body.endpoint, context);
3836
+ const callerEndpoint = body.endpoint ?? void 0;
3837
+ if (callerEndpoint !== void 0) {
3838
+ resolveEndpoint(name, callerEndpoint, context);
3839
+ }
3758
3840
  if (body.repeat != null) {
3759
- return { name, endpoint: body.endpoint, steps: [], unavailable: "repeat writes are not supported" };
3841
+ if (callerEndpoint === void 0) {
3842
+ throw new VirtualVehicleError(`${name} requires an endpoint`);
3843
+ }
3844
+ return { name, endpoint: callerEndpoint, steps: [], unavailable: "repeat writes are not supported" };
3845
+ }
3846
+ if (body.steps == null && callerEndpoint === void 0) {
3847
+ throw new VirtualVehicleError(`${name} requires an endpoint`);
3760
3848
  }
3761
3849
  const steps = [];
3762
3850
  let unavailable;
3763
- const expand = (items, depth) => {
3851
+ const expand = (items, activeEndpointName, depth) => {
3764
3852
  for (const step of items) {
3765
3853
  if ("call" in step) {
3766
3854
  const sequence = context.composed.sequences[step.call];
@@ -3770,19 +3858,28 @@ function lowerBody(name, body, context) {
3770
3858
  if (depth >= MAX_CALL_DEPTH) {
3771
3859
  throw new VirtualVehicleError(`${name} nests more than ${MAX_CALL_DEPTH} sequence calls`);
3772
3860
  }
3773
- expand(sequence.steps, depth + 1);
3861
+ const sequenceEndpoint = sequence.endpoint ?? void 0;
3862
+ if (sequenceEndpoint !== void 0) {
3863
+ resolveEndpoint(`sequences.${step.call}`, sequenceEndpoint, context);
3864
+ }
3865
+ expand(sequence.steps, sequenceEndpoint ?? activeEndpointName, depth + 1);
3774
3866
  } else if ("wait_ms" in step) {
3775
3867
  const previous = steps.at(-1);
3776
3868
  if (previous !== void 0) {
3777
3869
  previous.delayMs = (previous.delayMs ?? 0) + step.wait_ms;
3778
3870
  }
3779
3871
  } else {
3872
+ if (activeEndpointName === void 0) {
3873
+ throw new VirtualVehicleError(`${name} has a request with no endpoint`);
3874
+ }
3875
+ const endpoint = resolveEndpoint(name, activeEndpointName, context);
3780
3876
  const request = lowerRequest(step.request, endpoint, name);
3781
3877
  if ("unavailable" in request) {
3782
3878
  unavailable ??= request.unavailable;
3783
3879
  continue;
3784
3880
  }
3785
3881
  steps.push({
3882
+ ...activeEndpointName !== callerEndpoint ? { endpoint: activeEndpointName } : {},
3786
3883
  send: request.send,
3787
3884
  ...request.fields.length > 0 ? { fields: request.fields } : {},
3788
3885
  ...step.expect != null ? { expect: lowerPattern(step.expect, name) } : {}
@@ -3790,10 +3887,14 @@ function lowerBody(name, body, context) {
3790
3887
  }
3791
3888
  }
3792
3889
  };
3793
- expand(body.steps ?? [{ request: body.request, ...body.expect != null ? { expect: body.expect } : {} }], 0);
3890
+ expand(
3891
+ body.steps ?? [{ request: body.request, ...body.expect != null ? { expect: body.expect } : {} }],
3892
+ callerEndpoint,
3893
+ 0
3894
+ );
3794
3895
  return {
3795
3896
  name,
3796
- endpoint: body.endpoint,
3897
+ ...callerEndpoint !== void 0 ? { endpoint: callerEndpoint } : {},
3797
3898
  steps,
3798
3899
  ...unavailable !== void 0 ? { unavailable } : {}
3799
3900
  };
@@ -4134,18 +4235,47 @@ var CapabilityRegistry = class {
4134
4235
  for (const config of buses) {
4135
4236
  this.buses.set(config.bus, config);
4136
4237
  }
4137
- for (const profile of profiles) {
4238
+ for (const [index, profile] of profiles.entries()) {
4239
+ const endpointNames = new Map(profile.endpoints.map((endpoint) => [endpoint.name, `${index}:${endpoint.name}`]));
4240
+ const endpointName = (name) => endpointNames.get(name) ?? name;
4241
+ const scopeAction = (action) => ({
4242
+ ...action,
4243
+ ...action.endpoint !== void 0 ? { endpoint: endpointName(action.endpoint) } : {},
4244
+ steps: action.steps.map((step) => ({
4245
+ ...step,
4246
+ ...step.endpoint !== void 0 ? { endpoint: endpointName(step.endpoint) } : {}
4247
+ }))
4248
+ });
4249
+ const scopeQuery = (query) => ({ ...query, endpoint: endpointName(query.endpoint) });
4138
4250
  for (const endpoint of profile.endpoints) {
4251
+ const name = endpointName(endpoint.name);
4139
4252
  this.endpoints.set(endpoint.name, endpoint);
4253
+ this.endpoints.set(name, { ...endpoint, name });
4140
4254
  }
4141
4255
  for (const property of profile.properties) {
4142
- this.properties.set(property.path, mergeProperty(this.properties.get(property.path), property));
4256
+ const scoped = { ...property };
4257
+ if (property.query !== void 0) {
4258
+ scoped.query = scopeQuery(property.query);
4259
+ }
4260
+ if (property.write !== void 0) {
4261
+ const write = { ...property.write };
4262
+ if (write.direct !== void 0) {
4263
+ write.direct = scopeAction(write.direct);
4264
+ }
4265
+ if (write.cases !== void 0) {
4266
+ write.cases = Object.fromEntries(
4267
+ Object.entries(write.cases).map(([key, action]) => [key, scopeAction(action)])
4268
+ );
4269
+ }
4270
+ scoped.write = write;
4271
+ }
4272
+ this.properties.set(property.path, mergeProperty(this.properties.get(property.path), scoped));
4143
4273
  }
4144
4274
  for (const query of profile.queries) {
4145
- this.queries.set(query.name, query);
4275
+ this.queries.set(query.name, scopeQuery(query));
4146
4276
  }
4147
4277
  for (const action of profile.actions) {
4148
- this.actions.set(action.name, action);
4278
+ this.actions.set(action.name, scopeAction(action));
4149
4279
  }
4150
4280
  }
4151
4281
  for (const endpoint of this.endpoints.values()) {
@@ -4933,7 +5063,13 @@ var VirtualVehicle = class {
4933
5063
  name: monitor.name,
4934
5064
  canId,
4935
5065
  ...bus,
4936
- decode: (frame) => (frame.dlc ?? frame.data.length) < length ? void 0 : decodeField(frame.data, decode)
5066
+ decode: (frame) => {
5067
+ if ((frame.dlc ?? frame.data.length) < length) {
5068
+ return void 0;
5069
+ }
5070
+ const value = decodeField(frame.data, decode);
5071
+ return isWithinNumericConstraints(value, monitor.constraints) ? value : void 0;
5072
+ }
4937
5073
  };
4938
5074
  }
4939
5075
  const signal = this.dbc.resolveMessageSignal(monitor.message, monitor.signal);
@@ -4941,7 +5077,14 @@ var VirtualVehicle = class {
4941
5077
  name: monitor.name,
4942
5078
  canId: signal.canId,
4943
5079
  ...bus,
4944
- decode: (frame) => applyValueNormalization(decodeDbcSignal(frame, signal, monitor.value), monitor.normalize, signal.unit)
5080
+ decode: (frame) => {
5081
+ const value = applyValueNormalization(
5082
+ decodeDbcSignal(frame, signal, monitor.value),
5083
+ monitor.normalize,
5084
+ signal.unit
5085
+ );
5086
+ return isWithinNumericConstraints(value, monitor.constraints) ? value : void 0;
5087
+ }
4945
5088
  };
4946
5089
  }
4947
5090
  };