@emdzej/ediabasx-protocol-uds 0.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/LICENSE +131 -0
- package/README.md +42 -0
- package/dist/__tests__/isoTp.spec.d.ts +2 -0
- package/dist/__tests__/isoTp.spec.d.ts.map +1 -0
- package/dist/__tests__/isoTp.spec.js +45 -0
- package/dist/__tests__/isoTp.spec.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +132 -0
- package/dist/index.js.map +1 -0
- package/package.json +24 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# PolyForm Noncommercial License 1.0.0
|
|
2
|
+
|
|
3
|
+
<https://polyformproject.org/licenses/noncommercial/1.0.0>
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
In order to get any license under these terms, you must agree
|
|
8
|
+
to them as both strict obligations and conditions to all
|
|
9
|
+
your licenses.
|
|
10
|
+
|
|
11
|
+
## Copyright License
|
|
12
|
+
|
|
13
|
+
The licensor grants you a copyright license for the
|
|
14
|
+
software to do everything you might do with the software
|
|
15
|
+
that would otherwise infringe the licensor's copyright
|
|
16
|
+
in it for any permitted purpose. However, you may
|
|
17
|
+
only distribute the software according to [Distribution
|
|
18
|
+
License](#distribution-license) and make changes or new works
|
|
19
|
+
based on the software according to [Changes and New Works
|
|
20
|
+
License](#changes-and-new-works-license).
|
|
21
|
+
|
|
22
|
+
## Distribution License
|
|
23
|
+
|
|
24
|
+
The licensor grants you an additional copyright license
|
|
25
|
+
to distribute copies of the software. Your license
|
|
26
|
+
to distribute covers distributing the software with
|
|
27
|
+
changes and new works permitted by [Changes and New Works
|
|
28
|
+
License](#changes-and-new-works-license).
|
|
29
|
+
|
|
30
|
+
## Notices
|
|
31
|
+
|
|
32
|
+
You must ensure that anyone who gets a copy of any part of
|
|
33
|
+
the software from you also gets a copy of these terms or the
|
|
34
|
+
URL for them above, as well as copies of any plain-text lines
|
|
35
|
+
beginning with `Required Notice:` that the licensor provided
|
|
36
|
+
with the software. For example:
|
|
37
|
+
|
|
38
|
+
> Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
|
|
39
|
+
|
|
40
|
+
## Changes and New Works License
|
|
41
|
+
|
|
42
|
+
The licensor grants you an additional copyright license to
|
|
43
|
+
make changes and new works based on the software for any
|
|
44
|
+
permitted purpose.
|
|
45
|
+
|
|
46
|
+
## Patent License
|
|
47
|
+
|
|
48
|
+
The licensor grants you a patent license for the software that
|
|
49
|
+
covers patent claims the licensor can license, or becomes able
|
|
50
|
+
to license, that you would infringe by using the software.
|
|
51
|
+
|
|
52
|
+
## Noncommercial Purposes
|
|
53
|
+
|
|
54
|
+
Any noncommercial purpose is a permitted purpose.
|
|
55
|
+
|
|
56
|
+
## Personal Uses
|
|
57
|
+
|
|
58
|
+
Personal use for research, experiment, and testing for
|
|
59
|
+
the benefit of public knowledge, personal study, private
|
|
60
|
+
entertainment, hobby projects, amateur pursuits, or religious
|
|
61
|
+
observance, without any anticipated commercial application,
|
|
62
|
+
is use for a permitted purpose.
|
|
63
|
+
|
|
64
|
+
## Noncommercial Organizations
|
|
65
|
+
|
|
66
|
+
Use by any charitable organization, educational institution,
|
|
67
|
+
public research organization, public safety or health
|
|
68
|
+
organization, environmental protection organization,
|
|
69
|
+
or government institution is use for a permitted purpose
|
|
70
|
+
regardless of the source of funding or obligations resulting
|
|
71
|
+
from the funding.
|
|
72
|
+
|
|
73
|
+
## Fair Use
|
|
74
|
+
|
|
75
|
+
You may have "fair use" rights for the software under the
|
|
76
|
+
law. These terms do not limit them.
|
|
77
|
+
|
|
78
|
+
## No Other Rights
|
|
79
|
+
|
|
80
|
+
These terms do not allow you to sublicense or transfer any of
|
|
81
|
+
your licenses to anyone else, or prevent the licensor from
|
|
82
|
+
granting licenses to anyone else. These terms do not imply
|
|
83
|
+
any other licenses.
|
|
84
|
+
|
|
85
|
+
## Patent Defense
|
|
86
|
+
|
|
87
|
+
If you make any written claim that the software infringes or
|
|
88
|
+
contributes to infringement of any patent, your patent license
|
|
89
|
+
for the software granted under these terms ends immediately. If
|
|
90
|
+
your company makes such a claim, your patent license ends
|
|
91
|
+
immediately for work on behalf of your company.
|
|
92
|
+
|
|
93
|
+
## Violations
|
|
94
|
+
|
|
95
|
+
The first time you are notified in writing that you have
|
|
96
|
+
violated any of these terms, or done anything with the software
|
|
97
|
+
not covered by your licenses, your licenses can nonetheless
|
|
98
|
+
continue if you come into full compliance with these terms,
|
|
99
|
+
and take practical steps to correct past violations, within
|
|
100
|
+
32 days of receiving notice. Otherwise, all your licenses
|
|
101
|
+
end immediately.
|
|
102
|
+
|
|
103
|
+
## No Liability
|
|
104
|
+
|
|
105
|
+
***As far as the law allows, the software comes as is, without
|
|
106
|
+
any warranty or condition, and the licensor will not be liable
|
|
107
|
+
to you for any damages arising out of these terms or the use
|
|
108
|
+
or nature of the software, under any kind of legal claim.***
|
|
109
|
+
|
|
110
|
+
## Definitions
|
|
111
|
+
|
|
112
|
+
The **licensor** is the individual or entity offering these
|
|
113
|
+
terms, and the **software** is the software the licensor makes
|
|
114
|
+
available under these terms.
|
|
115
|
+
|
|
116
|
+
**You** refers to the individual or entity agreeing to these
|
|
117
|
+
terms.
|
|
118
|
+
|
|
119
|
+
**Your company** is any legal entity, sole proprietorship,
|
|
120
|
+
or other kind of organization that you work for, plus all
|
|
121
|
+
organizations that have control over, are under the control of,
|
|
122
|
+
or are under common control with that organization. **Control**
|
|
123
|
+
means ownership of substantially all the assets of an entity,
|
|
124
|
+
or the power to direct its management and policies by vote,
|
|
125
|
+
contract, or otherwise. Control can be direct or indirect.
|
|
126
|
+
|
|
127
|
+
**Your licenses** are all the licenses granted to you for the
|
|
128
|
+
software under these terms.
|
|
129
|
+
|
|
130
|
+
**Use** means anything you do with the software requiring one
|
|
131
|
+
of your licenses.
|
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# @emdzej/ediabasx-protocol-uds
|
|
2
|
+
|
|
3
|
+
UDS (ISO 14229) service identifiers, negative-response codes, and **ISO-TP** framing helpers (single / first / consecutive / flow-control frames). Part of [EdiabasX](https://github.com/emdzej/ediabasx).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @emdzej/ediabasx-protocol-uds
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### ISO-TP segmentation
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { segmentIsoTpPayload } from "@emdzej/ediabasx-protocol-uds";
|
|
17
|
+
|
|
18
|
+
// Splits a UDS payload into 8-byte CAN frames per ISO-15765-2.
|
|
19
|
+
const frames = segmentIsoTpPayload(
|
|
20
|
+
new Uint8Array([0x22, 0xf1, 0x90, /* …VIN read… */]),
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
for (const frame of frames) {
|
|
24
|
+
await canTx.send(frame); // your CAN driver
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The `interface-serial` package's K+DCAN ISO-TP path uses this internally for D-CAN ECUs.
|
|
29
|
+
|
|
30
|
+
### Service IDs and NRCs
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { IsoTpFrameTypes } from "@emdzej/ediabasx-protocol-uds";
|
|
34
|
+
|
|
35
|
+
if (frameType === IsoTpFrameTypes.FlowControl) {
|
|
36
|
+
// …
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
|
|
42
|
+
[PolyForm Noncommercial 1.0.0](../../LICENSE).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isoTp.spec.d.ts","sourceRoot":"","sources":["../../src/__tests__/isoTp.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
(0, vitest_1.describe)("segmentIsoTpPayload", () => {
|
|
6
|
+
(0, vitest_1.it)("builds single frames", () => {
|
|
7
|
+
const frames = (0, __1.segmentIsoTpPayload)(Uint8Array.from([0x01, 0x02]));
|
|
8
|
+
(0, vitest_1.expect)(frames).toEqual([Uint8Array.from([0x02, 0x01, 0x02])]);
|
|
9
|
+
});
|
|
10
|
+
(0, vitest_1.it)("builds multi-frame payloads", () => {
|
|
11
|
+
const payload = Uint8Array.from([
|
|
12
|
+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
|
|
13
|
+
]);
|
|
14
|
+
const frames = (0, __1.segmentIsoTpPayload)(payload, { frameSize: 8 });
|
|
15
|
+
(0, vitest_1.expect)(frames).toEqual([
|
|
16
|
+
Uint8Array.from([0x10, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06]),
|
|
17
|
+
Uint8Array.from([0x21, 0x07, 0x08])
|
|
18
|
+
]);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
(0, vitest_1.describe)("parseIsoTpFrame", () => {
|
|
22
|
+
(0, vitest_1.it)("parses single frames", () => {
|
|
23
|
+
const info = (0, __1.parseIsoTpFrame)(Uint8Array.from([0x03, 0x62, 0xf1, 0x90]));
|
|
24
|
+
(0, vitest_1.expect)(info.type).toBe(__1.IsoTpFrameTypes.Single);
|
|
25
|
+
(0, vitest_1.expect)(info.payload).toEqual(Uint8Array.from([0x62, 0xf1, 0x90]));
|
|
26
|
+
(0, vitest_1.expect)(info.totalLength).toBe(3);
|
|
27
|
+
});
|
|
28
|
+
(0, vitest_1.it)("parses consecutive frames", () => {
|
|
29
|
+
const info = (0, __1.parseIsoTpFrame)(Uint8Array.from([0x22, 0xaa, 0xbb]));
|
|
30
|
+
(0, vitest_1.expect)(info.type).toBe(__1.IsoTpFrameTypes.Consecutive);
|
|
31
|
+
(0, vitest_1.expect)(info.sequence).toBe(2);
|
|
32
|
+
(0, vitest_1.expect)(info.payload).toEqual(Uint8Array.from([0xaa, 0xbb]));
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
(0, vitest_1.describe)("decodeIsoTpFrames", () => {
|
|
36
|
+
(0, vitest_1.it)("reassembles multi-frame payloads", () => {
|
|
37
|
+
const frames = [
|
|
38
|
+
Uint8Array.from([0x10, 0x0a, 0x62, 0xf1, 0x90, 0x01, 0x02, 0x03]),
|
|
39
|
+
Uint8Array.from([0x21, 0x04, 0x05, 0x06, 0x07])
|
|
40
|
+
];
|
|
41
|
+
const payload = (0, __1.decodeIsoTpFrames)(frames);
|
|
42
|
+
(0, vitest_1.expect)(payload).toEqual(Uint8Array.from([0x62, 0xf1, 0x90, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]));
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
//# sourceMappingURL=isoTp.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isoTp.spec.js","sourceRoot":"","sources":["../../src/__tests__/isoTp.spec.ts"],"names":[],"mappings":";;AAAA,mCAA8C;AAC9C,0BAKY;AAEZ,IAAA,iBAAQ,EAAC,qBAAqB,EAAE,GAAG,EAAE;IACnC,IAAA,WAAE,EAAC,sBAAsB,EAAE,GAAG,EAAE;QAC9B,MAAM,MAAM,GAAG,IAAA,uBAAmB,EAAC,UAAU,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;QAClE,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAChE,CAAC,CAAC,CAAC;IAEH,IAAA,WAAE,EAAC,6BAA6B,EAAE,GAAG,EAAE;QACrC,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dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const IsoTpFrameTypes: {
|
|
2
|
+
readonly Single: "single";
|
|
3
|
+
readonly First: "first";
|
|
4
|
+
readonly Consecutive: "consecutive";
|
|
5
|
+
readonly FlowControl: "flow-control";
|
|
6
|
+
};
|
|
7
|
+
export type IsoTpFrameType = (typeof IsoTpFrameTypes)[keyof typeof IsoTpFrameTypes];
|
|
8
|
+
export type IsoTpFrameInfo = {
|
|
9
|
+
type: IsoTpFrameType;
|
|
10
|
+
payload: Uint8Array;
|
|
11
|
+
totalLength?: number;
|
|
12
|
+
sequence?: number;
|
|
13
|
+
};
|
|
14
|
+
export type IsoTpSegmentationOptions = {
|
|
15
|
+
readonly frameSize?: number;
|
|
16
|
+
readonly paddingByte?: number;
|
|
17
|
+
};
|
|
18
|
+
export declare function segmentIsoTpPayload(payload: Uint8Array, options?: IsoTpSegmentationOptions): Uint8Array[];
|
|
19
|
+
export declare function parseIsoTpFrame(frame: Uint8Array): IsoTpFrameInfo;
|
|
20
|
+
export declare function decodeIsoTpFrames(frames: readonly Uint8Array[]): Uint8Array;
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe;;;;;CAKlB,CAAC;AAEX,MAAM,MAAM,cAAc,GACxB,CAAC,OAAO,eAAe,CAAC,CAAC,MAAM,OAAO,eAAe,CAAC,CAAC;AAEzD,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,EAAE,cAAc,CAAC;IACrB,OAAO,EAAE,UAAU,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,wBAAwB,GAAG;IACrC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;CAC/B,CAAC;AAIF,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,UAAU,EACnB,OAAO,GAAE,wBAA6B,GACrC,UAAU,EAAE,CAsCd;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,cAAc,CAsCjE;AAED,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,SAAS,UAAU,EAAE,GAAG,UAAU,CA8C3E"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IsoTpFrameTypes = void 0;
|
|
4
|
+
exports.segmentIsoTpPayload = segmentIsoTpPayload;
|
|
5
|
+
exports.parseIsoTpFrame = parseIsoTpFrame;
|
|
6
|
+
exports.decodeIsoTpFrames = decodeIsoTpFrames;
|
|
7
|
+
exports.IsoTpFrameTypes = {
|
|
8
|
+
Single: "single",
|
|
9
|
+
First: "first",
|
|
10
|
+
Consecutive: "consecutive",
|
|
11
|
+
FlowControl: "flow-control"
|
|
12
|
+
};
|
|
13
|
+
const DEFAULT_FRAME_SIZE = 8;
|
|
14
|
+
function segmentIsoTpPayload(payload, options = {}) {
|
|
15
|
+
const frameSize = options.frameSize ?? DEFAULT_FRAME_SIZE;
|
|
16
|
+
if (frameSize < DEFAULT_FRAME_SIZE) {
|
|
17
|
+
throw new Error("ISO-TP frame size must be at least 8 bytes");
|
|
18
|
+
}
|
|
19
|
+
const paddingByte = options.paddingByte;
|
|
20
|
+
const totalLength = payload.length;
|
|
21
|
+
if (totalLength <= 7) {
|
|
22
|
+
const frame = new Uint8Array(1 + totalLength);
|
|
23
|
+
frame[0] = totalLength & 0x0f;
|
|
24
|
+
frame.set(payload, 1);
|
|
25
|
+
return [padFrame(frame, frameSize, paddingByte)];
|
|
26
|
+
}
|
|
27
|
+
const frames = [];
|
|
28
|
+
const firstFrame = new Uint8Array(2 + Math.min(6, totalLength));
|
|
29
|
+
firstFrame[0] = 0x10 | ((totalLength >> 8) & 0x0f);
|
|
30
|
+
firstFrame[1] = totalLength & 0xff;
|
|
31
|
+
firstFrame.set(payload.subarray(0, 6), 2);
|
|
32
|
+
frames.push(padFrame(firstFrame, frameSize, paddingByte));
|
|
33
|
+
let offset = 6;
|
|
34
|
+
let sequence = 1;
|
|
35
|
+
while (offset < totalLength) {
|
|
36
|
+
const chunkLength = Math.min(7, totalLength - offset);
|
|
37
|
+
const frame = new Uint8Array(1 + chunkLength);
|
|
38
|
+
frame[0] = 0x20 | (sequence & 0x0f);
|
|
39
|
+
frame.set(payload.subarray(offset, offset + chunkLength), 1);
|
|
40
|
+
frames.push(padFrame(frame, frameSize, paddingByte));
|
|
41
|
+
offset += chunkLength;
|
|
42
|
+
sequence = (sequence + 1) & 0x0f;
|
|
43
|
+
}
|
|
44
|
+
return frames;
|
|
45
|
+
}
|
|
46
|
+
function parseIsoTpFrame(frame) {
|
|
47
|
+
if (frame.length === 0) {
|
|
48
|
+
throw new Error("ISO-TP frame is empty");
|
|
49
|
+
}
|
|
50
|
+
const pci = frame[0];
|
|
51
|
+
const frameType = (pci & 0xf0) >> 4;
|
|
52
|
+
switch (frameType) {
|
|
53
|
+
case 0x0: {
|
|
54
|
+
const length = pci & 0x0f;
|
|
55
|
+
const payload = frame.subarray(1, 1 + length);
|
|
56
|
+
return { type: exports.IsoTpFrameTypes.Single, payload, totalLength: length };
|
|
57
|
+
}
|
|
58
|
+
case 0x1: {
|
|
59
|
+
if (frame.length < 2) {
|
|
60
|
+
throw new Error("ISO-TP first frame missing length byte");
|
|
61
|
+
}
|
|
62
|
+
const totalLength = ((pci & 0x0f) << 8) | frame[1];
|
|
63
|
+
const payload = frame.subarray(2);
|
|
64
|
+
return { type: exports.IsoTpFrameTypes.First, payload, totalLength };
|
|
65
|
+
}
|
|
66
|
+
case 0x2: {
|
|
67
|
+
const sequence = pci & 0x0f;
|
|
68
|
+
return {
|
|
69
|
+
type: exports.IsoTpFrameTypes.Consecutive,
|
|
70
|
+
payload: frame.subarray(1),
|
|
71
|
+
sequence
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
case 0x3:
|
|
75
|
+
return {
|
|
76
|
+
type: exports.IsoTpFrameTypes.FlowControl,
|
|
77
|
+
payload: frame.subarray(1)
|
|
78
|
+
};
|
|
79
|
+
default:
|
|
80
|
+
throw new Error("Unknown ISO-TP frame type");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function decodeIsoTpFrames(frames) {
|
|
84
|
+
if (frames.length === 0) {
|
|
85
|
+
return new Uint8Array();
|
|
86
|
+
}
|
|
87
|
+
const firstInfo = parseIsoTpFrame(frames[0]);
|
|
88
|
+
if (firstInfo.type === exports.IsoTpFrameTypes.Single) {
|
|
89
|
+
return Uint8Array.from(firstInfo.payload);
|
|
90
|
+
}
|
|
91
|
+
if (firstInfo.type !== exports.IsoTpFrameTypes.First || firstInfo.totalLength === undefined) {
|
|
92
|
+
throw new Error("ISO-TP message must start with single or first frame");
|
|
93
|
+
}
|
|
94
|
+
const totalLength = firstInfo.totalLength;
|
|
95
|
+
const payload = new Uint8Array(totalLength);
|
|
96
|
+
let offset = 0;
|
|
97
|
+
const firstPayload = firstInfo.payload.subarray(0, Math.min(6, totalLength));
|
|
98
|
+
payload.set(firstPayload, 0);
|
|
99
|
+
offset += firstPayload.length;
|
|
100
|
+
let expectedSequence = 1;
|
|
101
|
+
for (let index = 1; index < frames.length && offset < totalLength; index += 1) {
|
|
102
|
+
const info = parseIsoTpFrame(frames[index]);
|
|
103
|
+
if (info.type === exports.IsoTpFrameTypes.FlowControl) {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (info.type !== exports.IsoTpFrameTypes.Consecutive) {
|
|
107
|
+
throw new Error("Unexpected ISO-TP frame type");
|
|
108
|
+
}
|
|
109
|
+
if (info.sequence !== expectedSequence) {
|
|
110
|
+
throw new Error("ISO-TP sequence mismatch");
|
|
111
|
+
}
|
|
112
|
+
const remaining = totalLength - offset;
|
|
113
|
+
const slice = info.payload.subarray(0, Math.min(remaining, info.payload.length));
|
|
114
|
+
payload.set(slice, offset);
|
|
115
|
+
offset += slice.length;
|
|
116
|
+
expectedSequence = (expectedSequence + 1) & 0x0f;
|
|
117
|
+
}
|
|
118
|
+
if (offset < totalLength) {
|
|
119
|
+
throw new Error("Incomplete ISO-TP message");
|
|
120
|
+
}
|
|
121
|
+
return payload;
|
|
122
|
+
}
|
|
123
|
+
function padFrame(frame, frameSize, paddingByte) {
|
|
124
|
+
if (frame.length >= frameSize || paddingByte === undefined) {
|
|
125
|
+
return frame;
|
|
126
|
+
}
|
|
127
|
+
const padded = new Uint8Array(frameSize);
|
|
128
|
+
padded.set(frame);
|
|
129
|
+
padded.fill(paddingByte, frame.length);
|
|
130
|
+
return padded;
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAwBA,kDAyCC;AAED,0CAsCC;AAED,8CA8CC;AAzJY,QAAA,eAAe,GAAG;IAC7B,MAAM,EAAE,QAAQ;IAChB,KAAK,EAAE,OAAO;IACd,WAAW,EAAE,aAAa;IAC1B,WAAW,EAAE,cAAc;CACnB,CAAC;AAiBX,MAAM,kBAAkB,GAAG,CAAC,CAAC;AAE7B,SAAgB,mBAAmB,CACjC,OAAmB,EACnB,UAAoC,EAAE;IAEtC,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,kBAAkB,CAAC;IAC1D,IAAI,SAAS,GAAG,kBAAkB,EAAE,CAAC;QACnC,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;IAChE,CAAC;IAED,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IACxC,MAAM,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC;IAEnC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AM,GAAG,WAAW,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,SAAS,QAAQ,CACf,KAAiB,EACjB,SAAiB,EACjB,WAA+B;IAE/B,IAAI,KAAK,CAAC,MAAM,IAAI,SAAS,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;QAC3D,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,SAAS,CAAC,CAAC;IACzC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAClB,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAEvC,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@emdzej/ediabasx-protocol-uds",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "UDS (ISO 14229) service identifiers, NRCs, and ISO-TP framing helpers.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/emdzej/ediabasx.git"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc -p tsconfig.json",
|
|
19
|
+
"test": "vitest run --passWithNoTests",
|
|
20
|
+
"lint": "eslint .",
|
|
21
|
+
"typecheck": "tsc --noEmit",
|
|
22
|
+
"dev": "tsc -p tsconfig.json -w --preserveWatchOutput"
|
|
23
|
+
}
|
|
24
|
+
}
|