@pamoja/can 0.1.15 → 0.1.16

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.
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Ergonomic facade over the generated CAN binding.
3
+ *
4
+ * CAN is how the moving parts of a machine talk to each other: motor controllers,
5
+ * servos, battery management, and the engines and farm equipment that speak J1939
6
+ * on top of it. This is the identifier and payload layer; the controller hardware
7
+ * handles the wire itself.
8
+ *
9
+ * @packageDocumentation
10
+ */
11
+ import { type CanFrame, Signals } from '@pamoja/native';
12
+ export { type CanFrame, Signals };
13
+ /** The byte a J1939 sender writes for a signal it is not reporting. */
14
+ export declare const NOT_AVAILABLE: number;
15
+ /** The destination address every node on the bus reads. */
16
+ export declare const BROADCAST_ADDRESS: number;
17
+ /** The priorities J1939 publishes, so a caller does not write the number out. */
18
+ export declare const priority: {
19
+ /** Ahead of ordinary traffic, for a message that controls something. */
20
+ readonly control: number;
21
+ /** What ordinary traffic uses. */
22
+ readonly default: number;
23
+ /** Yields to everything else on the bus. */
24
+ readonly lowest: number;
25
+ };
26
+ /**
27
+ * Builds a J1939 payload with every signal marked not available.
28
+ *
29
+ * @returns Eight bytes a controller writes only its own signals into.
30
+ */
31
+ export declare function signals(): Signals;
32
+ /**
33
+ * Reads the eight data bytes of a frame that arrived off the bus.
34
+ *
35
+ * @param data - The frame's payload.
36
+ * @returns The payload, ready for its signals to be read out.
37
+ * @throws If the payload is not exactly eight bytes.
38
+ */
39
+ export declare function signalsFrom(data: Uint8Array): Signals;
40
+ /** The fields J1939 packs into an extended CAN identifier. */
41
+ export interface J1939Message {
42
+ /** The parameter group number, which names what the message carries. */
43
+ pgn: number;
44
+ /** The message priority, 0 (highest) to 7. */
45
+ priority: number;
46
+ /** The source address: the node that sent the message. */
47
+ source: number;
48
+ /** The PDU format byte of the parameter group. */
49
+ pduFormat: number;
50
+ /**
51
+ * The destination address for an addressed (PDU1) message, or `null` for a
52
+ * broadcast (PDU2) one.
53
+ */
54
+ destination: number | null;
55
+ /** Whether the message is a broadcast. */
56
+ broadcast: boolean;
57
+ }
58
+ /**
59
+ * Builds a classic CAN 2.0 frame.
60
+ *
61
+ * @param id - The arbitration identifier, masked to the width `extended` selects.
62
+ * @param data - The payload, at most eight bytes.
63
+ * @param extended - Whether the identifier is a 29-bit extended one.
64
+ * @returns The frame.
65
+ * @throws If the payload is longer than a classic frame carries.
66
+ */
67
+ export declare function frame(id: number, data: Uint8Array, extended?: boolean): CanFrame;
68
+ /**
69
+ * Builds a CAN-FD frame, which carries up to 64 bytes.
70
+ *
71
+ * @param id - The arbitration identifier.
72
+ * @param data - The payload, at one of the discrete CAN-FD lengths: 0 to 8, then
73
+ * 12, 16, 20, 24, 32, 48, or 64 bytes.
74
+ * @param extended - Whether the identifier is a 29-bit extended one.
75
+ * @returns The frame.
76
+ * @throws If the payload length is not one CAN-FD can carry.
77
+ */
78
+ export declare function fdFrame(id: number, data: Uint8Array, extended?: boolean): CanFrame;
79
+ /**
80
+ * Builds a remote transmission request, which asks another node to send.
81
+ *
82
+ * @param id - The arbitration identifier.
83
+ * @param len - The data length being requested, clamped to eight bytes.
84
+ * @param extended - Whether the identifier is a 29-bit extended one.
85
+ * @returns The frame, which carries no payload of its own.
86
+ */
87
+ export declare function remoteFrame(id: number, len: number, extended?: boolean): CanFrame;
88
+ /**
89
+ * Returns the data length code that encodes a payload length.
90
+ *
91
+ * @param len - The payload length in bytes.
92
+ * @returns The code, rounding up to the next length CAN-FD can carry.
93
+ */
94
+ export declare function lenToDlc(len: number): number;
95
+ /**
96
+ * Returns the payload length a data length code encodes.
97
+ *
98
+ * @param dlc - The data length code.
99
+ * @returns The length in bytes.
100
+ */
101
+ export declare function dlcToLen(dlc: number): number;
102
+ /**
103
+ * Decodes the J1939 fields out of an extended CAN identifier.
104
+ *
105
+ * @param id - The identifier as it arrived.
106
+ * @param extended - Whether it is a 29-bit extended identifier.
107
+ * @returns The decoded message, or `null` for a standard identifier, which J1939
108
+ * does not use.
109
+ */
110
+ export declare function decodeJ1939(id: number, extended?: boolean): J1939Message | null;
111
+ /**
112
+ * Composes the extended CAN identifier a set of J1939 fields describes.
113
+ *
114
+ * @param priority - The message priority, 0 (highest) to 7.
115
+ * @param pgn - The parameter group number.
116
+ * @param source - The address of the sending node.
117
+ * @param destination - The destination address, used only for an addressed (PDU1)
118
+ * parameter group and ignored for a broadcast (PDU2) one.
119
+ * @returns The 29-bit identifier.
120
+ */
121
+ export declare function composeJ1939(priority: number, pgn: number, source: number, destination?: number): number;
122
+ /**
123
+ * Composes the identifier of a J1939 broadcast, which every node on the bus reads.
124
+ *
125
+ * Most parameter groups are broadcast, so this is the common case; it saves a
126
+ * caller knowing that a broadcast is addressed to `0xFF`.
127
+ *
128
+ * @param priority - The message priority, 0 (highest) to 7.
129
+ * @param pgn - The parameter group number.
130
+ * @param source - The address of the sending node.
131
+ * @returns The 29-bit identifier.
132
+ */
133
+ export declare function broadcastJ1939(priority: number, pgn: number, source: number): number;
package/dist/index.js ADDED
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ /**
3
+ * Ergonomic facade over the generated CAN binding.
4
+ *
5
+ * CAN is how the moving parts of a machine talk to each other: motor controllers,
6
+ * servos, battery management, and the engines and farm equipment that speak J1939
7
+ * on top of it. This is the identifier and payload layer; the controller hardware
8
+ * handles the wire itself.
9
+ *
10
+ * @packageDocumentation
11
+ */
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.priority = exports.BROADCAST_ADDRESS = exports.NOT_AVAILABLE = exports.Signals = void 0;
14
+ exports.signals = signals;
15
+ exports.signalsFrom = signalsFrom;
16
+ exports.frame = frame;
17
+ exports.fdFrame = fdFrame;
18
+ exports.remoteFrame = remoteFrame;
19
+ exports.lenToDlc = lenToDlc;
20
+ exports.dlcToLen = dlcToLen;
21
+ exports.decodeJ1939 = decodeJ1939;
22
+ exports.composeJ1939 = composeJ1939;
23
+ exports.broadcastJ1939 = broadcastJ1939;
24
+ const native_1 = require("@pamoja/native");
25
+ Object.defineProperty(exports, "Signals", { enumerable: true, get: function () { return native_1.Signals; } });
26
+ /** The byte a J1939 sender writes for a signal it is not reporting. */
27
+ exports.NOT_AVAILABLE = native_1.J1939_NOT_AVAILABLE;
28
+ /** The destination address every node on the bus reads. */
29
+ exports.BROADCAST_ADDRESS = native_1.J1939_BROADCAST_ADDRESS;
30
+ /** The priorities J1939 publishes, so a caller does not write the number out. */
31
+ exports.priority = {
32
+ /** Ahead of ordinary traffic, for a message that controls something. */
33
+ control: native_1.J1939_PRIORITY_CONTROL,
34
+ /** What ordinary traffic uses. */
35
+ default: native_1.J1939_PRIORITY_DEFAULT,
36
+ /** Yields to everything else on the bus. */
37
+ lowest: native_1.J1939_PRIORITY_LOWEST,
38
+ };
39
+ /**
40
+ * Builds a J1939 payload with every signal marked not available.
41
+ *
42
+ * @returns Eight bytes a controller writes only its own signals into.
43
+ */
44
+ function signals() {
45
+ return new native_1.Signals();
46
+ }
47
+ /**
48
+ * Reads the eight data bytes of a frame that arrived off the bus.
49
+ *
50
+ * @param data - The frame's payload.
51
+ * @returns The payload, ready for its signals to be read out.
52
+ * @throws If the payload is not exactly eight bytes.
53
+ */
54
+ function signalsFrom(data) {
55
+ return native_1.Signals.fromBytes(Buffer.from(data));
56
+ }
57
+ /**
58
+ * Builds a classic CAN 2.0 frame.
59
+ *
60
+ * @param id - The arbitration identifier, masked to the width `extended` selects.
61
+ * @param data - The payload, at most eight bytes.
62
+ * @param extended - Whether the identifier is a 29-bit extended one.
63
+ * @returns The frame.
64
+ * @throws If the payload is longer than a classic frame carries.
65
+ */
66
+ function frame(id, data, extended = false) {
67
+ return (0, native_1.canFrame)(id, extended, Buffer.from(data));
68
+ }
69
+ /**
70
+ * Builds a CAN-FD frame, which carries up to 64 bytes.
71
+ *
72
+ * @param id - The arbitration identifier.
73
+ * @param data - The payload, at one of the discrete CAN-FD lengths: 0 to 8, then
74
+ * 12, 16, 20, 24, 32, 48, or 64 bytes.
75
+ * @param extended - Whether the identifier is a 29-bit extended one.
76
+ * @returns The frame.
77
+ * @throws If the payload length is not one CAN-FD can carry.
78
+ */
79
+ function fdFrame(id, data, extended = false) {
80
+ return (0, native_1.canFdFrame)(id, extended, Buffer.from(data));
81
+ }
82
+ /**
83
+ * Builds a remote transmission request, which asks another node to send.
84
+ *
85
+ * @param id - The arbitration identifier.
86
+ * @param len - The data length being requested, clamped to eight bytes.
87
+ * @param extended - Whether the identifier is a 29-bit extended one.
88
+ * @returns The frame, which carries no payload of its own.
89
+ */
90
+ function remoteFrame(id, len, extended = false) {
91
+ return (0, native_1.canRemoteFrame)(id, extended, len);
92
+ }
93
+ /**
94
+ * Returns the data length code that encodes a payload length.
95
+ *
96
+ * @param len - The payload length in bytes.
97
+ * @returns The code, rounding up to the next length CAN-FD can carry.
98
+ */
99
+ function lenToDlc(len) {
100
+ return (0, native_1.canLenToDlc)(len);
101
+ }
102
+ /**
103
+ * Returns the payload length a data length code encodes.
104
+ *
105
+ * @param dlc - The data length code.
106
+ * @returns The length in bytes.
107
+ */
108
+ function dlcToLen(dlc) {
109
+ return (0, native_1.canDlcToLen)(dlc);
110
+ }
111
+ /**
112
+ * Decodes the J1939 fields out of an extended CAN identifier.
113
+ *
114
+ * @param id - The identifier as it arrived.
115
+ * @param extended - Whether it is a 29-bit extended identifier.
116
+ * @returns The decoded message, or `null` for a standard identifier, which J1939
117
+ * does not use.
118
+ */
119
+ function decodeJ1939(id, extended = true) {
120
+ const message = (0, native_1.j1939Decode)(id, extended);
121
+ if (message === null || message === undefined)
122
+ return null;
123
+ // The generated object leaves an absent destination undefined; null says the
124
+ // same thing in the shape the other bindings use.
125
+ return {
126
+ pgn: message.pgn,
127
+ priority: message.priority,
128
+ source: message.source,
129
+ pduFormat: message.pduFormat,
130
+ destination: message.destination ?? null,
131
+ broadcast: message.broadcast,
132
+ };
133
+ }
134
+ /**
135
+ * Composes the extended CAN identifier a set of J1939 fields describes.
136
+ *
137
+ * @param priority - The message priority, 0 (highest) to 7.
138
+ * @param pgn - The parameter group number.
139
+ * @param source - The address of the sending node.
140
+ * @param destination - The destination address, used only for an addressed (PDU1)
141
+ * parameter group and ignored for a broadcast (PDU2) one.
142
+ * @returns The 29-bit identifier.
143
+ */
144
+ function composeJ1939(priority, pgn, source, destination = 0) {
145
+ return (0, native_1.j1939Compose)(priority, pgn, source, destination);
146
+ }
147
+ /**
148
+ * Composes the identifier of a J1939 broadcast, which every node on the bus reads.
149
+ *
150
+ * Most parameter groups are broadcast, so this is the common case; it saves a
151
+ * caller knowing that a broadcast is addressed to `0xFF`.
152
+ *
153
+ * @param priority - The message priority, 0 (highest) to 7.
154
+ * @param pgn - The parameter group number.
155
+ * @param source - The address of the sending node.
156
+ * @returns The 29-bit identifier.
157
+ */
158
+ function broadcastJ1939(priority, pgn, source) {
159
+ return (0, native_1.j1939Broadcast)(priority, pgn, source);
160
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pamoja/can",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "description": "CAN 2.0 and CAN-FD frames with 11- and 29-bit identifiers, plus J1939 decode and compose.",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -34,6 +34,6 @@
34
34
  "node": ">= 16"
35
35
  },
36
36
  "dependencies": {
37
- "@pamoja/native": "0.1.15"
37
+ "@pamoja/native": "0.1.16"
38
38
  }
39
39
  }