forked from sebi2k1/node-can
-
Notifications
You must be signed in to change notification settings - Fork 0
/
can.js
277 lines (229 loc) · 6.88 KB
/
can.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
/* 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.bitOffset = desc['bitOffset'];
this.bitLength = desc['bitLength'];
this.endianess = desc['endianess'];
this.type = desc['type'];
this.offset = desc['offset'];
this.factor = desc['factor'];
this.minValue = desc['minValue'];
this.maxValue = desc['maxValue'];
/**
* Current value
* @attribute value
* @final
*/
this.value = desc['defaultValue'];
if (!this.value)
this.value = 0;
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);
}
/**
* 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;
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 length
* @final
*/
this.length = desc.length;
/**
* 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];
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) {
var m = db_desc[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.rtr)
return;
id = msg.id | (msg.ext ? 1 : 0) << 31;
var m = this.messages[id];
if (!m)
{
console.log("Message ID " + msg.id + " not found");
return;
}
// Let the C-Portition extract and convert the signal
for (i in m.signals) {
var s = m.signals[i];
var val = _signals.decode_signal(msg.data, s.bitOffset, s.bitLength, s.endianess == 'little', s.type == 'signed');
if (s.factor)
val *= s.factor;
if (s.offset)
val += s.offset;
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 m = this.messages[msg_name]
if (!m)
throw msg_name + " not defined";
var canmsg = {
id: m.id,
ext: m.ext,
rtr: false,
data: new Buffer(m.length)
}
for (var i = 0; i < m.length; i++)
canmsg.data[i] = 0;
for (i in m.signals) {
var s = m.signals[i];
var val = s.value;
// Apply factor/offset and convert to Integer
if (s.offset)
val -= s.offset;
if (s.factor)
val /= s.factor;
if (typeof(val) == 'double')
val = parseInt(Math.round(val));
_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;