forked from sebi2k1/node-can
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketcan.js
363 lines (301 loc) · 8.68 KB
/
socketcan.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* Copyright Sebastian Haas <[email protected]>. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
//-----------------------------------------------------------------------------
// CAN-Object
var can = require('./build/Release/can');
var buffer = require('buffer');
/**
* @method createRawChannel
* @param channel {string} Channel name (e.g. vcan0)
* @return {RawChannel} a new channel object or exception
* @for exports
*/
exports.createRawChannel = function(channel, timestamps) { return new can.RawChannel(channel, timestamps); }
//-----------------------------------------------------------------------------
/**
* The Signals modules provides an interface to access the values/signals
* encoded in CAN messages.
* @module Signals
*/
var _signals = require('./build/Release/can_signals');
var kcd = require('./parse_kcd');
/**
* The actual signal.
* @class Signal
*/
function Signal(desc)
{
/**
* Symbolic name
* @attribute name
* @final
*/
this.name = desc['name'];
this.spn = desc['spn'];
this.bitOffset = desc['bitOffset'];
this.bitLength = desc['bitLength'];
this.endianess = desc['endianess'];
this.type = desc['type'];
this.intercept = desc['intercept'];
this.slope = desc['slope'];
this.minValue = desc['minValue'];
this.maxValue = desc['maxValue'];
this.unit = desc['unit'];
/**
* Label set for defined states of the signal.
*/
this.labels = desc['labels'];
/**
* this will allow triggering on mux'ed message ids.
*/
this.muxGroup = [ desc['mux'] ];
/**
* Current value
*
* @attribute value
* @final
*/
this.value = null;
this.listeners = [];
}
/**
* Keep track of listeners who want to be notified if this signal changes
* @method onChange
* @param listener JS callback to get notification
* @for Signal
*/
Signal.prototype.onChange = function(listener) {
this.listeners.push(listener);
return listener;
}
/**
* Remove listener from signal onChange
* @method removeListener
* @param listener to be removed
* @for Signal
*/
Signal.prototype.removeListener = function(listener) {
var idx = this.listeners.indexOf(listener);
this.listeners.splice(idx, 1);
}
/**
* Set new value of this signal. Any local registered clients will
* receive a notification. Please note, no CAN message is actually
* send to the bus (@see DatabaseServer::send)
* @method update
* @param newValue {bool|double|integer} New value to set
* @for Signal
*/
Signal.prototype.update = function(newValue) {
// Nothing changed
if (this.value == newValue) {
return;
} else if (newValue > this.maxValue) {
console.error("ERROR : " + this.name + " value= " + newValue
+ " is outof bounds > " + this.maxValue);
} else if (newValue < this.minValue) {
console.error("ERROR : " + this.name + " value= " + newValue
+ " is outof bounds < " + this.minValue);
}
this.value = newValue;
// Update all listeners, that the signal changed
for (f in this.listeners) {
this.listeners[f](this);
}
}
//-----------------------------------------------------------------------------
/**
* Just a container to keep the Signals.
* @class Message
*/
function Message(desc)
{
/**
* CAN identifier
* @attribute id
* @final
*/
this.id = desc.id;
/**
* Extended Frame Format used
* @attribute ext
* @final
*/
this.ext = desc.ext;
/**
* Symbolic name
* @attribute name
* @final
*/
this.name = desc.name;
/**
* Length in bytes of resulting CAN message
*
* @attribute len
* @final
*/
this.len = desc.length;
/**
* This is the time frame that the message gets generated
*
* @attribute interval
* @final
*/
this.interval = desc.interval;
/**
* This is tells us the message is mutliplexed.
*
* @attribute muxed
* @final
*/
this.muxed = desc.muxed;
/**
* Named array of signals within this message. Accessible via index and name.
* @attribute {Signal} signals
* @final
*/
this.signals = [];
for (i in desc['signals']) {
var s = desc['signals'][i];
if (this.signals[s.name] && this.signals[s.name].muxGroup) {
this.signals[s.name].muxGroup.push(s.mux);
} else {
this.signals[s.name] = new Signal(s);
}
}
}
//-----------------------------------------------------------------------------
/**
* A DatabaseService is usually generated once per bus to collect signals
* coded in the CAN messages according a DB description.
* @class DatabaseService
* @constructor DatabaseService
* @param channel RAW channel
* @param db_desc Set of rules to decode/encode signals (@parse_kcd.js)
* @return a new DatabaseService
* @for DatabaseService
*/
function DatabaseService(channel, db_desc) {
this.channel = channel;
/**
* Named array of known messages. Accessible via index and name.
* @attribute {Message} messages
*/
this.messages = [];
for (i in db_desc['messages']) {
var m = db_desc['messages'][i];
var id = m.id | (m.ext ? 1 : 0) << 31;
var nm = new Message(m);
this.messages[id] = nm;
this.messages[m.name] = nm;
}
// Subscribe to any incoming messages
channel.addListener("onMessage", this.onMessage, this);
}
// Callback for incoming messages
DatabaseService.prototype.onMessage = function (msg) {
if (msg == undefined)
return;
if (msg.rtr)
return;
id = msg.id | (msg.ext ? 1 : 0) << 31;
var m = this.messages[id];
if (!m)
{
return;
}
// this is the possible multiplexor for the signals coming in.
var b1mux = _signals.decode_signal(msg.data, 0, 8, true, false);
// Let the C-Portition extract and convert the signal
for (i in m.signals) {
var s = m.signals[i];
if (s.value === undefined)
continue;
// if this is a mux signal and the muxor isnt in my list...
if (m.muxed && s.muxGroup && s.muxGroup.indexOf(b1mux) == -1) {
continue;
}
var val = _signals.decode_signal(msg.data, s.bitOffset, s.bitLength,
s.endianess == 'little', s.type == 'signed');
if (s.slope)
val *= s.slope;
if (s.intercept)
val += s.intercept;
s.update(val);
}
}
/**
* Construct a CAN message and encode all related signals according
* the rules. Finally send the message to the bus.
* @method send
* @param msg_name Name of the message to generate
* @for DatabaseService
*/
DatabaseService.prototype.send = function (msg_name) {
var args = msg_name.split("."); // allow for mux'ed messages sent.
var m = this.messages[args[0]];
var mux = (args.length > 1) ? args[1] : undefined;
if (!m)
throw msg_name + " not defined";
var canmsg = {
id: m.id,
ext: m.ext,
rtr: false,
data : (m.len > 0 && m.len < 8) ? new Buffer(m.len) : new Buffer(8)
};
canmsg.data.fill(0); // should be 0xFF for j1939 message def.
if (mux) {
_signals.encode_signal(canmsg.data, 0, 8, true, false,
parseInt(mux, 16));
}
for (i in m.signals) {
var s = m.signals[i];
if (s.value == undefined)
continue;
if (mux) {
if (s.muxGroup.indexOf(parseInt(mux, 16)) === -1) {
continue;
}
}
var val = s.value;
// Apply factor/intercept and convert to Integer
if (s.intercept)
val -= s.intercept;
if (s.slope)
val /= s.slope;
if (typeof(val) == 'double')
val = parseInt(Math.round(val));
if (m.len == 0) {
return;
}
_signals.encode_signal(canmsg.data, s.bitOffset, s.bitLength,
s.endianess == 'little', s.type == 'signed', val);
}
this.channel.send(canmsg);
}
/**
* @method parseNetworkDescription
* @param file {string} Path to KCD file to parse
* @return DB description to be used in DatabaseService
* @for exports
*/
exports.parseNetworkDescription = kcd.parseKcdFile;
exports.DatabaseService = DatabaseService;