-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcan.cpp
More file actions
519 lines (411 loc) · 13.6 KB
/
can.cpp
File metadata and controls
519 lines (411 loc) · 13.6 KB
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* Node-type: CAN bus.
*
* Author: Niklas Eiling <niklas.eiling@eonerc.rwth-aachen.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <villas/exceptions.hpp>
#include <villas/node_compat.hpp>
#include <villas/nodes/can.hpp>
#include <villas/sample.hpp>
#include <villas/signal.hpp>
#include <villas/utils.hpp>
using namespace villas;
using namespace villas::node;
using namespace villas::utils;
// Forward declarations
static NodeCompatType p;
int villas::node::can_init(NodeCompat *n) {
auto *c = n->getData<struct can>();
c->interface_name = nullptr;
c->socket = 0;
c->sample_buf = nullptr;
c->sample_buf_num = 0;
c->in = nullptr;
c->out = nullptr;
return 0;
}
int villas::node::can_destroy(NodeCompat *n) {
auto *c = n->getData<struct can>();
if (c->socket != 0)
close(c->socket);
free(c->sample_buf);
if (c->in)
free(c->in);
if (c->out)
free(c->out);
return 0;
}
static int can_parse_signal(json_t *json, SignalList::Ptr node_signals,
struct can_signal *can_signals,
size_t signal_index) {
const char *name = nullptr;
uint64_t can_id = 0;
int can_size = 8;
int can_offset = 0;
int ret = 1;
json_error_t err;
ret = json_unpack_ex(json, &err, 0, "{ s?: s, s?: i, s?: i, s?: i }", "name",
&name, "can_id", &can_id, "can_size", &can_size,
"can_offset", &can_offset);
if (ret)
throw ConfigError(json, err, "node-config-node-can-signals");
if (can_size > 8 || can_size <= 0)
throw ConfigError(json, "node-config-node-can-can-size",
"can_size of {} for signal '{}' is invalid. You must "
"satisfy 0 < can_size <= 8.",
can_size, name);
if (can_offset > 8 || can_offset < 0)
throw ConfigError(json, "node-config-node-can-can-offset",
"can_offset of {} for signal '{}' is invalid. You must "
"satisfy 0 <= can_offset <= 8.",
can_offset, name);
auto sig = node_signals->getByIndex(signal_index);
if ((!name && sig->name.empty()) || (name && sig->name == name)) {
can_signals[signal_index].id = can_id;
can_signals[signal_index].size = can_size;
can_signals[signal_index].offset = can_offset;
ret = 0;
goto out;
} else
throw ConfigError(json, "node-config-node-can-signa;s",
"Signal configuration inconsistency detected: Signal "
"with index {} '{}' does not match can_signal '{}'",
signal_index, sig->name, name);
out:
return ret;
}
int villas::node::can_parse(NodeCompat *n, json_t *json) {
int ret = 1;
auto *c = n->getData<struct can>();
size_t i;
json_t *json_in_signals;
json_t *json_out_signals;
json_t *json_signal;
json_error_t err;
c->in = nullptr;
c->out = nullptr;
ret = json_unpack_ex(json, &err, 0, "{ s: s, s?: { s?: o }, s?: { s?: o } }",
"interface_name", &c->interface_name, "in", "signals",
&json_in_signals, "out", "signals", &json_out_signals);
if (ret)
throw ConfigError(json, err, "node-config-node-can");
c->in = (struct can_signal *)calloc(json_array_size(json_in_signals),
sizeof(struct can_signal));
if (!c->in)
throw MemoryAllocationError();
c->out = (struct can_signal *)calloc(json_array_size(json_out_signals),
sizeof(struct can_signal));
if (!c->out)
throw MemoryAllocationError();
json_array_foreach (json_in_signals, i, json_signal) {
ret = can_parse_signal(json_signal, n->in.signals, c->in, i);
if (ret)
throw RuntimeError("at signal {}.", i);
}
json_array_foreach (json_out_signals, i, json_signal) {
ret = can_parse_signal(json_signal, n->out.signals, c->out, i);
if (ret)
throw RuntimeError("at signal {}.", i);
}
ret = 0;
return ret;
}
char *villas::node::can_print(NodeCompat *n) {
auto *c = n->getData<struct can>();
return strf("interface_name={}", c->interface_name);
}
int villas::node::can_check(NodeCompat *n) {
auto *c = n->getData<struct can>();
if (c->interface_name == nullptr || strlen(c->interface_name) == 0)
throw RuntimeError(
"Empty interface_name. Please specify the name of the CAN interface!");
return 0;
}
int villas::node::can_prepare(NodeCompat *n) {
auto *c = n->getData<struct can>();
c->sample_buf = (union SignalData *)calloc(n->getInputSignals(false)->size(),
sizeof(union SignalData));
return (c->sample_buf != 0 ? 0 : 1);
}
int villas::node::can_start(NodeCompat *n) {
int ret = 1;
struct sockaddr_can addr = {0};
struct ifreq ifr;
auto *c = n->getData<struct can>();
c->start_time = time_now();
c->socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (c->socket < 0)
throw SystemError("Error while opening CAN socket");
strcpy(ifr.ifr_name, c->interface_name);
ret = ioctl(c->socket, SIOCGIFINDEX, &ifr);
if (ret != 0)
throw SystemError("Could not find interface with name '{}'.",
c->interface_name);
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
ret =
bind(c->socket, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr));
if (ret < 0)
throw SystemError("Could not bind to interface with name '{}' ({}).",
c->interface_name, ifr.ifr_ifindex);
return 0;
}
int villas::node::can_stop(NodeCompat *n) {
auto *c = n->getData<struct can>();
if (c->socket != 0) {
close(c->socket);
c->socket = 0;
}
return 0;
}
static int can_convert_to_raw(const union SignalData *sig,
const Signal::Ptr from, void *to, int size) {
if (size <= 0 || size > 8)
throw RuntimeError("Signal size cannot be larger than 8!");
switch (from->type) {
case SignalType::BOOLEAN:
*(uint8_t *)to = sig->b;
return 0;
case SignalType::INTEGER:
switch (size) {
case 1:
*(int8_t *)to = (int8_t)sig->i;
return 0;
case 2:
*(int16_t *)to = (int16_t)sig->i;
return 0;
case 3:
*(int16_t *)to = (int16_t)sig->i;
*((int8_t *)to + 2) = (int8_t)(sig->i >> 16);
return 0;
case 4:
*(int32_t *)to = (int32_t)sig->i;
return 0;
case 8:
*(int64_t *)to = sig->i;
return 0;
default:
goto fail;
}
case SignalType::FLOAT:
switch (size) {
case 4:
assert(sizeof(float) == 4);
*(float *)to = (float)sig->f;
return 0;
case 8:
*(double *)to = sig->f;
return 0;
default:
goto fail;
}
case SignalType::COMPLEX:
if (size != 8)
goto fail;
*(float *)to = sig->z.real();
*((float *)to + 1) = sig->z.imag();
return 0;
default:
goto fail;
}
fail:
throw RuntimeError("Unsupported conversion to {} from raw ({}, {})",
signalTypeToString(from->type), to, size);
return 1;
}
static int can_conv_from_raw(union SignalData *sig, void *from, int size,
Signal::Ptr to) {
if (size <= 0 || size > 8)
throw RuntimeError("Signal size cannot be larger than 8!");
switch (to->type) {
case SignalType::BOOLEAN:
sig->b = (bool)*(uint8_t *)from;
return 0;
case SignalType::INTEGER:
switch (size) {
case 1:
sig->i = (int64_t) * (int8_t *)from;
return 0;
case 2:
sig->i = (int64_t) * (int16_t *)from;
return 0;
case 3:
sig->i = (int64_t) * (int16_t *)from;
sig->i += ((int64_t) * ((int8_t *)(from) + 2)) << 16;
return 0;
case 4:
sig->i = (int64_t) * (int32_t *)from;
return 0;
case 8:
sig->i = *(uint64_t *)from;
return 0;
default:
goto fail;
}
case SignalType::FLOAT:
switch (size) {
case 4:
assert(sizeof(float) == 4);
sig->f = (double)*(float *)from;
return 0;
case 8:
sig->f = *(double *)from;
return 0;
default:
goto fail;
}
case SignalType::COMPLEX:
if (size != 8)
goto fail;
sig->z = std::complex<float>(*(float *)from, *((float *)from + 1));
return 0;
default:
goto fail;
}
fail:
throw RuntimeError("Unsupported conversion from {} to raw ({}, {})",
signalTypeToString(to->type), from, size);
return 1;
}
int villas::node::can_read(NodeCompat *n, struct Sample *const smps[],
unsigned cnt) {
int ret = 0;
int nbytes;
unsigned nread = 0;
struct can_frame frame;
struct timeval tv;
bool found_id = false;
auto *c = n->getData<struct can>();
assert(cnt >= 1 && smps[0]->capacity >= 1);
nbytes = read(c->socket, &frame, sizeof(struct can_frame));
if (nbytes == -1)
throw RuntimeError("CAN read() returned -1. Is the CAN interface up?");
if ((unsigned)nbytes != sizeof(struct can_frame))
throw RuntimeError("CAN read() error. Returned {} bytes but expected {}",
nbytes, sizeof(struct can_frame));
n->logger->debug("Received can message: (id={}, len={}, data={:#x}:{:#x})",
frame.can_id, frame.can_dlc, ((uint32_t *)&frame.data)[0],
((uint32_t *)&frame.data)[1]);
if (ioctl(c->socket, SIOCGSTAMP, &tv) == 0) {
TIMEVAL_TO_TIMESPEC(&tv, &smps[nread]->ts.received);
smps[nread]->flags |= (int)SampleFlags::HAS_TS_RECEIVED;
}
for (size_t i = 0; i < n->getInputSignals(false)->size(); i++) {
if (c->in[i].id == frame.can_id) {
if (can_conv_from_raw(
&c->sample_buf[i], ((uint8_t *)&frame.data) + c->in[i].offset,
c->in[i].size, n->getInputSignals(false)->getByIndex(i)) != 0) {
goto out;
}
c->sample_buf_num++;
found_id = true;
}
}
if (!found_id)
throw RuntimeError("Did not find signal for can id {}", frame.can_id);
n->logger->trace("Received {} signals", c->sample_buf_num);
// Copy signal data to sample only when all signals have been received
if (c->sample_buf_num == n->getInputSignals(false)->size()) {
smps[nread]->length = c->sample_buf_num;
memcpy(smps[nread]->data, c->sample_buf,
c->sample_buf_num * sizeof(union SignalData));
c->sample_buf_num = 0;
smps[nread]->flags |= (int)SampleFlags::HAS_DATA;
ret = 1;
} else {
smps[nread]->length = 0;
ret = 0;
}
out: // Set signals, because other VILLASnode parts expect us to
smps[nread]->signals = n->getInputSignals(false);
return ret;
}
int villas::node::can_write(NodeCompat *n, struct Sample *const smps[],
unsigned cnt) {
int nbytes;
unsigned nwrite;
struct can_frame *frame;
size_t fsize = 0; // number of frames in use
auto *c = n->getData<struct can>();
assert(cnt >= 1 && smps[0]->capacity >= 1);
frame = (struct can_frame *)calloc(sizeof(struct can_frame),
n->getOutputSignals()->size());
if (!frame) {
throw MemoryAllocationError();
}
for (nwrite = 0; nwrite < cnt; nwrite++) {
for (size_t i = 0; i < n->getOutputSignals()->size(); i++) {
if (c->out[i].offset != 0) // frame is shared
continue;
frame[fsize].can_dlc = c->out[i].size;
frame[fsize].can_id = c->out[i].id;
can_convert_to_raw(&smps[nwrite]->data[i],
n->getOutputSignals()->getByIndex(i),
&frame[fsize].data, c->out[i].size);
fsize++;
}
for (size_t i = 0; i < n->getOutputSignals(false)->size(); i++) {
if (c->out[i].offset == 0) { // frame already stored
continue;
}
for (size_t j = 0; j < fsize; j++) {
if (c->out[i].id != frame[j].can_id)
continue;
frame[j].can_dlc += c->out[i].size;
can_convert_to_raw(
&smps[nwrite]->data[i], n->getOutputSignals(false)->getByIndex(i),
(uint8_t *)&frame[j].data + c->out[i].offset, c->out[i].size);
break;
}
}
for (size_t j = 0; j < fsize; j++) {
n->logger->debug("Writing CAN message: (id={}, dlc={}, data={:#x}:{:#x})",
frame[j].can_id, frame[j].can_dlc,
((uint32_t *)&frame[j].data)[0],
((uint32_t *)&frame[j].data)[1]);
if ((nbytes = write(c->socket, &frame[j], sizeof(struct can_frame))) ==
-1)
throw RuntimeError("CAN write() returned -1. Is the CAN interface up?");
if ((unsigned)nbytes != sizeof(struct can_frame))
throw RuntimeError("CAN write() returned {} bytes but expected {}",
nbytes, sizeof(struct can_frame));
}
}
return nwrite;
}
int villas::node::can_poll_fds(NodeCompat *n, int fds[]) {
auto *c = n->getData<struct can>();
fds[0] = c->socket;
return 1; // The number of file descriptors which have been set in fds
}
__attribute__((constructor(110))) static void register_plugin() {
p.name = "can";
p.description = "Receive CAN messages using the socketCAN driver";
p.vectorize = 0;
p.flags = 0;
p.size = sizeof(struct can);
p.init = can_init;
p.destroy = can_destroy;
p.prepare = can_prepare;
p.parse = can_parse;
p.print = can_print;
p.check = can_check;
p.start = can_start;
p.stop = can_stop;
p.read = can_read;
p.write = can_write;
p.poll_fds = can_poll_fds;
static NodeCompatFactory ncp(&p);
}