-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanusb.c
665 lines (533 loc) · 15.7 KB
/
canusb.c
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
// #include <asm/termbits.h> /* struct termios2 */
#include <termios.h>
#include <time.h>
#include <ctype.h>
#include <signal.h>
#include <sys/time.h>
#define CANUSB_INJECT_SLEEP_GAP_DEFAULT 200 /* ms */
#define CANUSB_TTY_BAUD_RATE_DEFAULT 2000000
typedef enum {
CANUSB_SPEED_1000000 = 0x01,
CANUSB_SPEED_800000 = 0x02,
CANUSB_SPEED_500000 = 0x03,
CANUSB_SPEED_400000 = 0x04,
CANUSB_SPEED_250000 = 0x05,
CANUSB_SPEED_200000 = 0x06,
CANUSB_SPEED_125000 = 0x07,
CANUSB_SPEED_100000 = 0x08,
CANUSB_SPEED_50000 = 0x09,
CANUSB_SPEED_20000 = 0x0a,
CANUSB_SPEED_10000 = 0x0b,
CANUSB_SPEED_5000 = 0x0c,
} CANUSB_SPEED;
typedef enum {
CANUSB_MODE_NORMAL = 0x00,
CANUSB_MODE_LOOPBACK = 0x01,
CANUSB_MODE_SILENT = 0x02,
CANUSB_MODE_LOOPBACK_SILENT = 0x03,
} CANUSB_MODE;
typedef enum {
CANUSB_FRAME_STANDARD = 0x01,
CANUSB_FRAME_EXTENDED = 0x02,
} CANUSB_FRAME;
typedef enum {
CANUSB_INJECT_PAYLOAD_MODE_RANDOM = 0,
CANUSB_INJECT_PAYLOAD_MODE_INCREMENTAL = 1,
CANUSB_INJECT_PAYLOAD_MODE_FIXED = 2,
} CANUSB_PAYLOAD_MODE;
static int terminate_after = 0;
static int program_running = 1;
static int inject_payload_mode = CANUSB_INJECT_PAYLOAD_MODE_FIXED;
static float inject_sleep_gap = CANUSB_INJECT_SLEEP_GAP_DEFAULT;
static int print_traffic = 0;
static CANUSB_SPEED canusb_int_to_speed(int speed)
{
switch (speed) {
case 1000000:
return CANUSB_SPEED_1000000;
case 800000:
return CANUSB_SPEED_800000;
case 500000:
return CANUSB_SPEED_500000;
case 400000:
return CANUSB_SPEED_400000;
case 250000:
return CANUSB_SPEED_250000;
case 200000:
return CANUSB_SPEED_200000;
case 125000:
return CANUSB_SPEED_125000;
case 100000:
return CANUSB_SPEED_100000;
case 50000:
return CANUSB_SPEED_50000;
case 20000:
return CANUSB_SPEED_20000;
case 10000:
return CANUSB_SPEED_10000;
case 5000:
return CANUSB_SPEED_5000;
default:
return 0;
}
}
static int generate_checksum(const unsigned char *data, int data_len)
{
int i, checksum;
checksum = 0;
for (i = 0; i < data_len; i++) {
checksum += data[i];
}
return checksum & 0xff;
}
static int frame_is_complete(const unsigned char *frame, int frame_len)
{
if (frame_len > 0) {
if (frame[0] != 0xaa) {
/* Need to sync on 0xaa at start of frames, so just skip. */
return 1;
}
}
if (frame_len < 2) {
return 0;
}
if (frame[1] == 0x55) { /* Command frame... */
if (frame_len >= 20) { /* ...always 20 bytes. */
return 1;
} else {
return 0;
}
} else if ((frame[1] >> 4) == 0xc) { /* Data frame... */
if (frame_len >= (frame[1] & 0xf) + 5) { /* ...payload and 5 bytes. */
return 1;
} else {
return 0;
}
}
/* Unhandled frame type. */
return 1;
}
static int frame_send(int tty_fd, const unsigned char *frame, int frame_len)
{
int result, i;
if (print_traffic) {
printf(">>> ");
for (i = 0; i < frame_len; i++) {
printf("%02x ", frame[i]);
}
if (print_traffic > 1) {
printf(" '");
for (i = 4; i < frame_len - 1; i++) {
printf("%c", isalnum(frame[i]) ? frame[i] : '.');
}
printf("'");
}
printf("\n");
}
result = write(tty_fd, frame, frame_len);
if (result == -1) {
fprintf(stderr, "write() failed: %s\n", strerror(errno));
return -1;
}
return frame_len;
}
static int frame_recv(int tty_fd, unsigned char *frame, int frame_len_max)
{
int result, frame_len, checksum;
unsigned char byte;
if (print_traffic)
fprintf(stderr, "<<< ");
frame_len = 0;
while (program_running) {
result = read(tty_fd, &byte, 1);
if (result == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
fprintf(stderr, "read() failed: %s\n", strerror(errno));
return -1;
}
} else if (result > 0) {
if (print_traffic)
fprintf(stderr, "%02x ", byte);
if (frame_len == frame_len_max) {
fprintf(stderr, "frame_recv() failed: Overflow\n");
return -1;
}
frame[frame_len++] = byte;
if (frame_is_complete(frame, frame_len)) {
break;
}
}
usleep(10);
}
if (print_traffic)
fprintf(stderr, "\n");
/* Compare checksum for command frames only. */
if ((frame_len == 20) && (frame[0] == 0xaa) && (frame[1] == 0x55)) {
checksum = generate_checksum(&frame[2], 17);
if (checksum != frame[frame_len - 1]) {
fprintf(stderr, "frame_recv() failed: Checksum incorrect\n");
return -1;
}
}
return frame_len;
}
static int command_settings(int tty_fd, CANUSB_SPEED speed, CANUSB_MODE mode, CANUSB_FRAME frame)
{
int cmd_frame_len;
unsigned char cmd_frame[20];
cmd_frame_len = 0;
cmd_frame[cmd_frame_len++] = 0xaa;
cmd_frame[cmd_frame_len++] = 0x55;
cmd_frame[cmd_frame_len++] = 0x12;
cmd_frame[cmd_frame_len++] = speed;
cmd_frame[cmd_frame_len++] = frame;
cmd_frame[cmd_frame_len++] = 0; /* Filter ID not handled. */
cmd_frame[cmd_frame_len++] = 0; /* Filter ID not handled. */
cmd_frame[cmd_frame_len++] = 0; /* Filter ID not handled. */
cmd_frame[cmd_frame_len++] = 0; /* Filter ID not handled. */
cmd_frame[cmd_frame_len++] = 0; /* Mask ID not handled. */
cmd_frame[cmd_frame_len++] = 0; /* Mask ID not handled. */
cmd_frame[cmd_frame_len++] = 0; /* Mask ID not handled. */
cmd_frame[cmd_frame_len++] = 0; /* Mask ID not handled. */
cmd_frame[cmd_frame_len++] = mode;
cmd_frame[cmd_frame_len++] = 0x01;
cmd_frame[cmd_frame_len++] = 0;
cmd_frame[cmd_frame_len++] = 0;
cmd_frame[cmd_frame_len++] = 0;
cmd_frame[cmd_frame_len++] = 0;
cmd_frame[cmd_frame_len++] = generate_checksum(&cmd_frame[2], 17);
if (frame_send(tty_fd, cmd_frame, cmd_frame_len) < 0) {
return -1;
}
return 0;
}
static int send_data_frame(int tty_fd, CANUSB_FRAME frame, unsigned char id_lsb, unsigned char id_msb, unsigned char data[], int data_length_code)
{
#define MAX_FRAME_SIZE 13
int data_frame_len = 0;
unsigned char data_frame[MAX_FRAME_SIZE] = {0x00};
if (data_length_code < 0 || data_length_code > 8)
{
fprintf(stderr, "Data length code (DLC) must be between 0 and 8!\n");
return -1;
}
/* Byte 0: Packet Start */
data_frame[data_frame_len++] = 0xaa;
/* Byte 1: CAN Bus Data Frame Information */
data_frame[data_frame_len] = 0x00;
data_frame[data_frame_len] |= 0xC0; /* Bit 7 Always 1, Bit 6 Always 1 */
if (frame == CANUSB_FRAME_STANDARD)
data_frame[data_frame_len] &= 0xDF; /* STD frame */
else /* CANUSB_FRAME_EXTENDED */
data_frame[data_frame_len] |= 0x20; /* EXT frame */
data_frame[data_frame_len] &= 0xEF; /* 0=Data */
data_frame[data_frame_len] |= data_length_code; /* DLC=data_len */
data_frame_len++;
/* Byte 2 to 3: ID */
data_frame[data_frame_len++] = id_lsb; /* lsb */
data_frame[data_frame_len++] = id_msb; /* msb */
/* Byte 4 to (4+data_len): Data */
for (int i = 0; i < data_length_code; i++)
data_frame[data_frame_len++] = data[i];
/* Last byte: End of frame */
data_frame[data_frame_len++] = 0x55;
if (frame_send(tty_fd, data_frame, data_frame_len) < 0)
{
fprintf(stderr, "Unable to send frame!\n");
return -1;
}
return 0;
}
static int hex_value(int c)
{
if (c >= 0x30 && c <= 0x39) /* '0' - '9' */
return c - 0x30;
else if (c >= 0x41 && c <= 0x46) /* 'A' - 'F' */
return (c - 0x41) + 10;
else if (c >= 0x61 && c <= 0x66) /* 'a' - 'f' */
return (c - 0x61) + 10;
else
return -1;
}
static int convert_from_hex(const char *hex_string, unsigned char *bin_string, int bin_string_len)
{
int n1, n2, high;
high = -1;
n1 = n2 = 0;
while (hex_string[n1] != '\0') {
if (hex_value(hex_string[n1]) >= 0) {
if (high == -1) {
high = hex_string[n1];
} else {
bin_string[n2] = hex_value(high) * 16 + hex_value(hex_string[n1]);
n2++;
if (n2 >= bin_string_len) {
printf("hex string truncated to %d bytes\n", n2);
break;
}
high = -1;
}
}
n1++;
}
return n2;
}
static int inject_data_frame(int tty_fd, const char *hex_id, const char *hex_data)
{
int data_len;
unsigned char binary_data[8];
unsigned char binary_id_lsb = 0, binary_id_msb = 0;
struct timespec gap_ts;
struct timeval now;
int error = 0;
gap_ts.tv_sec = inject_sleep_gap / 1000;
gap_ts.tv_nsec = (long)(((long long)(inject_sleep_gap * 1000000)) % 1000000000LL);
/* Set seed value for pseudo random numbers. */
gettimeofday(&now, NULL);
srandom(now.tv_usec);
data_len = convert_from_hex(hex_data, binary_data, sizeof(binary_data));
if (data_len == 0) {
fprintf(stderr, "Unable to convert data from hex to binary!\n");
return -1;
}
switch (strlen(hex_id)) {
case 1:
binary_id_lsb = hex_value(hex_id[0]);
break;
case 2:
binary_id_lsb = (hex_value(hex_id[0]) * 16) + hex_value(hex_id[1]);
break;
case 3:
binary_id_msb = hex_value(hex_id[0]);
binary_id_lsb = (hex_value(hex_id[1]) * 16) + hex_value(hex_id[2]);
break;
default:
fprintf(stderr, "Unable to convert ID from hex to binary!\n");
return -1;
}
while (program_running && ! error) {
if (gap_ts.tv_sec || gap_ts.tv_nsec)
nanosleep(&gap_ts, NULL);
if (terminate_after && (--terminate_after == 0))
program_running = 0;
if (inject_payload_mode == CANUSB_INJECT_PAYLOAD_MODE_RANDOM) {
int i;
for (i = 0; i < data_len; i++)
binary_data[i] = random();
} else if (inject_payload_mode == CANUSB_INJECT_PAYLOAD_MODE_INCREMENTAL) {
int i;
for (i = 0; i < data_len; i++)
binary_data[i]++;
}
error = send_data_frame(tty_fd, CANUSB_FRAME_STANDARD, binary_id_lsb, binary_id_msb, binary_data, data_len);
}
return error;
}
static void dump_data_frames(int tty_fd)
{
int i, frame_len;
unsigned char frame[32];
struct timespec ts;
while (program_running) {
frame_len = frame_recv(tty_fd, frame, sizeof(frame));
if (! program_running)
break;
clock_gettime(CLOCK_MONOTONIC, &ts);
printf("%lu.%06lu ", ts.tv_sec, ts.tv_nsec / 1000);
if (frame_len == -1) {
printf("Frame recieve error!\n");
} else {
if ((frame_len >= 6) &&
(frame[0] == 0xaa) &&
((frame[1] >> 4) == 0xc)) {
printf("Frame ID: %02x%02x, Data: ", frame[3], frame[2]);
for (i = frame_len - 2; i > 3; i--) {
printf("%02x ", frame[i]);
}
printf("\n");
} else {
printf("Unknown: ");
for (i = 0; i <= frame_len; i++) {
printf("%02x ", frame[i]);
}
printf("\n");
}
}
if (terminate_after && (--terminate_after == 0))
program_running = 0;
}
}
// static int adapter_init(const char *tty_device, int baudrate)
// {
// int tty_fd, result;
// struct termios2 tio;
// tty_fd = open(tty_device, O_RDWR | O_NOCTTY | O_NONBLOCK);
// if (tty_fd == -1) {
// fprintf(stderr, "open(%s) failed: %s\n", tty_device, strerror(errno));
// return -1;
// }
// result = ioctl(tty_fd, TCGETS2, &tio);
// if (result == -1) {
// fprintf(stderr, "ioctl() failed: %s\n", strerror(errno));
// close(tty_fd);
// return -1;
// }
// tio.c_cflag &= ~CBAUD;
// tio.c_cflag = BOTHER | CS8 | CSTOPB;
// tio.c_iflag = IGNPAR;
// tio.c_oflag = 0;
// tio.c_lflag = 0;
// tio.c_ispeed = baudrate;
// tio.c_ospeed = baudrate;
// result = ioctl(tty_fd, TCSETS2, &tio);
// if (result == -1) {
// fprintf(stderr, "ioctl() failed: %s\n", strerror(errno));
// close(tty_fd);
// return -1;
// }
// return tty_fd;
// }
static int adapter_init(const char *tty_device, int baudrate) {
int tty_fd, result;
struct termios tio;
tty_fd = open(tty_device, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (tty_fd == -1) {
fprintf(stderr, "open(%s) failed: %s\n", tty_device, strerror(errno));
return -1;
}
result = tcgetattr(tty_fd, &tio);
if (result == -1) {
fprintf(stderr, "tcgetattr() failed: %s\n", strerror(errno));
close(tty_fd);
return -1;
}
cfsetispeed(&tio, baudrate);
cfsetospeed(&tio, baudrate);
tio.c_cflag &= ~CSIZE; // Mask the character size bits
tio.c_cflag |= CS8; // Select 8 data bits
tio.c_cflag &= ~CSTOPB; // 1 stop bit
tio.c_iflag = IGNPAR; // Ignore parity errors
tio.c_oflag = 0; // No remapping, no delays
tio.c_lflag = 0; // No signaling chars, no echo, no canonical processing
result = tcsetattr(tty_fd, TCSANOW, &tio);
if (result == -1) {
fprintf(stderr, "tcsetattr() failed: %s\n", strerror(errno));
close(tty_fd);
return -1;
}
return tty_fd;
}
static void display_help(const char *progname)
{
fprintf(stderr, "Usage: %s <options>\n", progname);
fprintf(stderr, "Options:\n"
" -h Display this help and exit.\n"
" -t Print TTY/serial traffic debugging info on stderr.\n"
" -d DEVICE Use TTY DEVICE.\n"
" -s SPEED Set CAN SPEED in bps.\n"
" -b BAUDRATE Set TTY/serial BAUDRATE (default: %d).\n"
" -i ID Inject using ID (specified as hex string).\n"
" -j DATA CAN DATA to inject (specified as hex string).\n"
" -n COUNT Terminate after COUNT frames (default: infinite).\n"
" -g MS Inject sleep gap in MS milliseconds (default: %d ms).\n"
" -m MODE Inject payload MODE (%d = random, %d = incremental, %d = fixed).\n"
"\n",
CANUSB_TTY_BAUD_RATE_DEFAULT,
CANUSB_INJECT_SLEEP_GAP_DEFAULT,
CANUSB_INJECT_PAYLOAD_MODE_RANDOM,
CANUSB_INJECT_PAYLOAD_MODE_INCREMENTAL,
CANUSB_INJECT_PAYLOAD_MODE_FIXED);
}
static void sigterm(int signo)
{
program_running = 0;
}
int main(int argc, char *argv[])
{
int c, tty_fd;
char *tty_device = NULL, *inject_data = NULL, *inject_id = NULL;
CANUSB_SPEED speed = 0;
int baudrate = CANUSB_TTY_BAUD_RATE_DEFAULT;
while ((c = getopt(argc, argv, "htd:s:b:i:j:n:g:m:")) != -1) {
switch (c) {
case 'h':
display_help(argv[0]);
return EXIT_SUCCESS;
case 't':
print_traffic++;
break;
case 'd':
tty_device = optarg;
break;
case 's':
speed = canusb_int_to_speed(atoi(optarg));
break;
case 'b':
baudrate = atoi(optarg);
break;
case 'i':
inject_id = optarg;
break;
case 'j':
inject_data = optarg;
break;
case 'n':
terminate_after = atoi(optarg);
break;
case 'g':
inject_sleep_gap = strtof(optarg, NULL);
break;
case 'm':
inject_payload_mode = atoi(optarg);
break;
case '?':
default:
display_help(argv[0]);
return EXIT_FAILURE;
}
}
signal(SIGTERM, sigterm);
signal(SIGHUP, sigterm);
signal(SIGINT, sigterm);
if (tty_device == NULL) {
fprintf(stderr, "Please specify a TTY!\n");
display_help(argv[0]);
return EXIT_FAILURE;
}
if (speed == 0) {
fprintf(stderr, "Please specify a valid speed!\n");
display_help(argv[0]);
return EXIT_FAILURE;
}
tty_fd = adapter_init(tty_device, baudrate);
if (tty_fd == -1) {
return EXIT_FAILURE;
}
command_settings(tty_fd, speed, CANUSB_MODE_NORMAL, CANUSB_FRAME_STANDARD);
if (inject_data == NULL) {
/* Dumping mode (default). */
dump_data_frames(tty_fd);
} else {
/* Inject mode. */
if (inject_id == NULL) {
fprintf(stderr, "Please specify a ID for injection!\n");
display_help(argv[0]);
return EXIT_FAILURE;
}
if (inject_data_frame(tty_fd, inject_id, inject_data) == -1) {
return EXIT_FAILURE;
} else {
return EXIT_SUCCESS;
}
}
return EXIT_SUCCESS;
}