forked from linux-can/can-utils
-
Notifications
You must be signed in to change notification settings - Fork 5
/
cangen.c
1090 lines (923 loc) · 27.5 KB
/
cangen.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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */
/*
* cangen.c - CAN frames generator
*
* Copyright (c) 2022 Pengutronix,
* Marc Kleine-Budde <[email protected]>
* Copyright (c) 2002-2007 Volkswagen Group Electronic Research
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Volkswagen nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* Alternatively, provided that this notice is retained in full, this
* software may be distributed under the terms of the GNU General
* Public License ("GPL") version 2, in which case the provisions of the
* GPL apply INSTEAD OF those given above.
*
* The provided data structures and external interfaces from this code
* are not restricted to be used by modules with a GPL compatible license.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* Send feedback to <[email protected]>
*
*/
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <libgen.h>
#include <limits.h>
#include <poll.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <linux/net_tstamp.h>
#include "lib.h"
#define DEFAULT_GAP 200 /* ms */
#define DEFAULT_BURST_COUNT 1
#define DEFAULT_SO_MARK_VAL 1
#define MODE_RANDOM 0
#define MODE_INCREMENT 1
#define MODE_FIX 2
#define MODE_RANDOM_EVEN 3
#define MODE_RANDOM_ODD 4
#define MODE_RANDOM_FIX 5
#define NIBBLE_H 1
#define NIBBLE_L 2
#define CHAR_RANDOM 'x'
extern int optind, opterr, optopt;
static volatile int running = 1;
static volatile sig_atomic_t signal_num;
static unsigned long long enobufs_count;
static bool ignore_enobufs;
static bool use_so_txtime;
static int clockid = CLOCK_TAI;
static int clock_nanosleep_flags;
static struct timespec ts, ts_gap;
static int so_mark_val = DEFAULT_SO_MARK_VAL;
#define NSEC_PER_SEC 1000000000LL
static struct timespec timespec_normalise(struct timespec ts)
{
while (ts.tv_nsec >= NSEC_PER_SEC) {
++(ts.tv_sec);
ts.tv_nsec -= NSEC_PER_SEC;
}
while (ts.tv_nsec <= -NSEC_PER_SEC) {
--(ts.tv_sec);
ts.tv_nsec += NSEC_PER_SEC;
}
if (ts.tv_nsec < 0) {
/*
* Negative nanoseconds isn't valid according to
* POSIX. Decrement tv_sec and roll tv_nsec over.
*/
--(ts.tv_sec);
ts.tv_nsec = (NSEC_PER_SEC + ts.tv_nsec);
}
return ts;
}
static struct timespec timespec_add(struct timespec ts1, struct timespec ts2)
{
/*
* Normalize inputs to prevent tv_nsec rollover if
* whole-second values are packed in it.
*/
ts1 = timespec_normalise(ts1);
ts2 = timespec_normalise(ts2);
ts1.tv_sec += ts2.tv_sec;
ts1.tv_nsec += ts2.tv_nsec;
return timespec_normalise(ts1);
}
struct timespec double_to_timespec(double s)
{
struct timespec ts = {
.tv_sec = s,
.tv_nsec = (s - (long)(s)) * NSEC_PER_SEC,
};
return timespec_normalise(ts);
}
static struct timespec ns_to_timespec(int64_t ns)
{
struct timespec ts = {
.tv_sec = ns / NSEC_PER_SEC,
.tv_nsec = ns % NSEC_PER_SEC,
};
return timespec_normalise(ts);
}
static void print_usage(char *prg)
{
fprintf(stderr, "%s - CAN frames generator.\n\n", prg);
fprintf(stderr, "Usage: %s [options] <CAN interface>\n", prg);
fprintf(stderr, "Options:\n");
fprintf(stderr, " -g <ms> (gap in milli seconds - default: %d ms)\n", DEFAULT_GAP);
fprintf(stderr, " -a (use absolute time for gap)\n");
fprintf(stderr, " -t (use SO_TXTIME)\n");
fprintf(stderr, " --start <ns> (start time (UTC nanoseconds))\n");
fprintf(stderr, " --mark <id> (set SO_MARK to <id>, default %u)\n", DEFAULT_SO_MARK_VAL);
fprintf(stderr, " -e (generate extended frame mode (EFF) CAN frames)\n");
fprintf(stderr, " -f (generate CAN FD CAN frames)\n");
fprintf(stderr, " -b (generate CAN FD CAN frames with bitrate switch (BRS))\n");
fprintf(stderr, " -E (generate CAN FD CAN frames with error state (ESI))\n");
fprintf(stderr, " -X (generate CAN XL CAN frames)\n");
fprintf(stderr, " -R (generate RTR frames)\n");
fprintf(stderr, " -8 (allow DLC values greater then 8 for Classic CAN frames)\n");
fprintf(stderr, " -m (mix -e -f -b -E -R -X frames)\n");
fprintf(stderr, " -I <mode> (CAN ID generation mode - see below)\n");
fprintf(stderr, " -L <mode> (CAN data length code (dlc) generation mode - see below)\n");
fprintf(stderr, " -D <mode> (CAN data (payload) generation mode - see below)\n");
fprintf(stderr, " -F <mode> (CAN XL Flags generation mode - see below, no e/o mode)\n");
fprintf(stderr, " -S <mode> (CAN XL SDT generation mode - see below, no e/o mode)\n");
fprintf(stderr, " -A <mode> (CAN XL AF generation mode - see below, no e/o mode)\n");
fprintf(stderr, " -V <mode> (CAN XL VCID generation mode - see below, no e/o mode)\n");
fprintf(stderr, " -p <timeout> (poll on -ENOBUFS to write frames with <timeout> ms)\n");
fprintf(stderr, " -n <count> (terminate after <count> CAN frames - default infinite)\n");
fprintf(stderr, " -i (ignore -ENOBUFS return values on write() syscalls)\n");
fprintf(stderr, " -x (disable local loopback of generated CAN frames)\n");
fprintf(stderr, " -c <count> (number of messages to send in burst, default %u)\n", DEFAULT_BURST_COUNT);
fprintf(stderr, " -v (increment verbose level for printing sent CAN frames)\n\n");
fprintf(stderr, "Generation modes:\n");
fprintf(stderr, " 'r' => random values (default)\n");
fprintf(stderr, " 'e' => random values, even ID\n");
fprintf(stderr, " 'o' => random values, odd ID\n");
fprintf(stderr, " 'i' => increment values\n");
fprintf(stderr, " <value> => fixed value (in hexadecimal for -I and -D)\n");
fprintf(stderr, " => nibbles written as '%c' are randomized (only -D)\n\n", CHAR_RANDOM);
fprintf(stderr, "The gap value (in milliseconds) may have decimal places, e.g. '-g 4.73'\n");
fprintf(stderr, "When incrementing the CAN data the data length code minimum is set to 1.\n");
fprintf(stderr, "CAN IDs and data content are given and expected in hexadecimal values.\n\n");
fprintf(stderr, "Examples:\n");
fprintf(stderr, "%s vcan0 -g 4 -I 42A -L 1 -D i -v -v\n", prg);
fprintf(stderr, "\t(fixed CAN ID and length, inc. data)\n");
fprintf(stderr, "%s vcan0 -e -L i -v -v -v\n", prg);
fprintf(stderr, "\t(generate EFF frames, incr. length)\n");
fprintf(stderr, "%s vcan0 -D 11223344DEADBEEF -L 8\n", prg);
fprintf(stderr, "\t(fixed CAN data payload and length)\n");
fprintf(stderr, "%s vcan0 -D 11%c%c3344DEADBEEF -L 8\n", prg, CHAR_RANDOM, CHAR_RANDOM);
fprintf(stderr, "\t(fixed CAN data payload where 2. byte is randomized, fixed length)\n");
fprintf(stderr, "%s vcan0 -I 555 -D CCCCCCCCCCCCCCCC -L 8 -g 3.75\n", prg);
fprintf(stderr, "\t(generate a fix busload without bit-stuffing effects)\n");
fprintf(stderr, "%s vcan0 -g 0 -i -x\n", prg);
fprintf(stderr, "\t(full load test ignoring -ENOBUFS)\n");
fprintf(stderr, "%s vcan0 -g 0 -p 10 -x\n", prg);
fprintf(stderr, "\t(full load test with polling, 10ms timeout)\n");
fprintf(stderr, "%s vcan0\n", prg);
fprintf(stderr, "\t(my favourite default :)\n\n");
}
static void sigterm(int signo)
{
running = 0;
signal_num = signo;
}
static int setsockopt_txtime(int fd)
{
const struct sock_txtime so_txtime_val = {
.clockid = clockid,
.flags = SOF_TXTIME_REPORT_ERRORS,
};
struct sock_txtime so_txtime_val_read;
int so_mark_val_read;
socklen_t vallen;
int ret;
/* SO_TXTIME */
ret = setsockopt(fd, SOL_SOCKET, SO_TXTIME,
&so_txtime_val, sizeof(so_txtime_val));
if (ret) {
int err = errno;
perror("setsockopt() SO_TXTIME");
if (err == EPERM)
fprintf(stderr, "Run with CAP_NET_ADMIN or as root.\n");
return -err;
};
vallen = sizeof(so_txtime_val_read);
ret = getsockopt(fd, SOL_SOCKET, SO_TXTIME,
&so_txtime_val_read, &vallen);
if (ret) {
perror("getsockopt() SO_TXTIME");
return -errno;
};
if (vallen != sizeof(so_txtime_val) ||
memcmp(&so_txtime_val, &so_txtime_val_read, vallen)) {
perror("getsockopt() SO_TXTIME: mismatch");
return -EINVAL;
}
/* SO_MARK */
ret = setsockopt(fd, SOL_SOCKET, SO_MARK, &so_mark_val, sizeof(so_mark_val));
if (ret) {
int err = errno;
perror("setsockopt() SO_MARK");
if (err == EPERM)
fprintf(stderr, "Run with CAP_NET_ADMIN or as root.\n");
return -err;
};
vallen = sizeof(so_mark_val_read);
ret = getsockopt(fd, SOL_SOCKET, SO_MARK,
&so_mark_val_read, &vallen);
if (ret) {
perror("getsockopt() SO_MARK");
return -errno;
};
if (vallen != sizeof(so_mark_val) ||
memcmp(&so_mark_val, &so_mark_val_read, vallen)) {
perror("getsockopt() SO_MARK: mismatch");
return -EINVAL;
}
return 0;
}
static int do_send_one(int fd, cu_t *cu, size_t len, int timeout)
{
uint8_t control[CMSG_SPACE(sizeof(uint64_t))] = { 0 };
struct iovec iov = {
.iov_base = cu,
};
struct msghdr msg = {
.msg_iov = &iov,
.msg_iovlen = 1,
};
ssize_t nbytes;
int ret;
/* CAN XL frames need real frame length for sending */
if (len == CANXL_MTU)
len = CANXL_HDR_SIZE + cu->xl.len;
iov.iov_len = len;
if (use_so_txtime) {
struct cmsghdr *cm;
uint64_t tdeliver;
msg.msg_control = control;
msg.msg_controllen = sizeof(control);
tdeliver = ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
ts = timespec_add(ts, ts_gap);
cm = CMSG_FIRSTHDR(&msg);
cm->cmsg_level = SOL_SOCKET;
cm->cmsg_type = SCM_TXTIME;
cm->cmsg_len = CMSG_LEN(sizeof(tdeliver));
memcpy(CMSG_DATA(cm), &tdeliver, sizeof(tdeliver));
}
resend:
nbytes = sendmsg(fd, &msg, 0);
if (nbytes < 0) {
ret = -errno;
if (ret != -ENOBUFS) {
perror("write");
return ret;
}
if (!ignore_enobufs && !timeout) {
perror("write");
return ret;
}
if (timeout) {
struct pollfd fds = {
.fd = fd,
.events = POLLOUT,
};
/* wait for the write socket (with timeout) */
ret = poll(&fds, 1, timeout);
if (ret == 0 || (ret == -1 && errno != EINTR)) {
ret = -errno;
perror("poll");
return ret;
}
goto resend;
} else {
enobufs_count++;
}
} else if (nbytes < (ssize_t)len) {
fprintf(stderr, "write: incomplete CAN frame\n");
return -EINVAL;
}
return 0;
}
static int setup_time(void)
{
int ret;
if (use_so_txtime) {
/* start time is defined */
if (ts.tv_sec || ts.tv_nsec)
return 0;
/* start time is now .... */
ret = clock_gettime(clockid, &ts);
if (ret) {
perror("clock_gettime");
return ret;
}
/* ... + gap */
ts = timespec_add(ts, ts_gap);
return 0;
}
if (ts.tv_sec || ts.tv_nsec) {
ret = clock_nanosleep(clockid, TIMER_ABSTIME, &ts, NULL);
if (ret != 0 && ret != EINTR) {
perror("clock_nanosleep");
return ret;
}
} else if (clock_nanosleep_flags == TIMER_ABSTIME) {
ret = clock_gettime(clockid, &ts);
if (ret)
perror("clock_gettime");
return ret;
}
if (clock_nanosleep_flags != TIMER_ABSTIME)
ts = ts_gap;
return 0;
}
enum {
OPT_MARK = UCHAR_MAX + 1,
OPT_START = UCHAR_MAX + 2,
};
/*
* Search for CHAR_RANDOM in dataoptarg, save its position, replace it with 0.
* Return 1 if at least one CHAR_RANDOM found.
*/
static int parse_dataoptarg(char *dataoptarg, unsigned char *rand_position)
{
int mode_format_selected = MODE_FIX;
int arglen = strlen(dataoptarg);
int i;
/* Mark nibbles with * as fuzzable */
for (i = 0; i < CANFD_MAX_DLEN && i < arglen / 2; i++) {
if (optarg[2 * i] == CHAR_RANDOM) {
optarg[2 * i] = '0';
rand_position[i] += NIBBLE_H;
mode_format_selected = MODE_RANDOM_FIX;
}
if (optarg[2 * i + 1] == CHAR_RANDOM) {
optarg[2 * i + 1] = '0';
rand_position[i] += NIBBLE_L;
mode_format_selected = MODE_RANDOM_FIX;
}
}
return mode_format_selected;
}
int main(int argc, char **argv)
{
double gap = DEFAULT_GAP;
unsigned long burst_count = DEFAULT_BURST_COUNT;
unsigned long polltimeout = 0;
unsigned char extended = 0;
unsigned char canfd = 0;
unsigned char canxl = 0;
unsigned char brs = 0;
unsigned char esi = 0;
unsigned char mix = 0;
unsigned char id_mode = MODE_RANDOM;
unsigned char data_mode = MODE_RANDOM;
unsigned char dlc_mode = MODE_RANDOM;
__u8 xl_flags = 0;
__u8 xl_sdt = 0;
__u32 xl_af = 0;
__u8 xl_vcid = 0;
unsigned char xl_flags_mode = MODE_RANDOM;
unsigned char xl_sdt_mode = MODE_RANDOM;
unsigned char xl_af_mode = MODE_RANDOM;
unsigned char xl_vcid_mode = MODE_RANDOM;
unsigned char loopback_disable = 0;
unsigned char verbose = 0;
unsigned char rtr_frame = 0;
unsigned char len8_dlc = 0;
unsigned char view = 0;
int count = 0;
unsigned long burst_sent_count = 0;
int mtu, maxdlen;
uint64_t incdata = 0;
__u8 *data; /* base pointer for CC/FD or XL data */
int incdlc = 0;
unsigned long rnd;
unsigned char fixdata[CANFD_MAX_DLEN];
unsigned char rand_position[CANFD_MAX_DLEN] = { 0 };
int opt;
int s; /* socket */
struct sockaddr_can addr = { 0 };
struct can_raw_vcid_options vcid_opts = {
.flags = CAN_RAW_XL_VCID_TX_PASS,
};
static cu_t cu;
int i;
struct ifreq ifr = { 0 };
const int enable_canfx = 1;
struct timeval now;
int ret;
/* set seed value for pseudo random numbers */
gettimeofday(&now, NULL);
srandom(now.tv_usec);
signal(SIGTERM, sigterm);
signal(SIGHUP, sigterm);
signal(SIGINT, sigterm);
const struct option long_options[] = {
{ "mark", required_argument, 0, OPT_MARK, },
{ "start", required_argument, 0, OPT_START, },
{ 0, 0, 0, 0 },
};
while ((opt = getopt_long(argc, argv, "g:atefbEXR8mI:L:D:F:S:A:V:p:n:ixc:vh?", long_options, NULL)) != -1) {
switch (opt) {
case 'g':
gap = strtod(optarg, NULL);
break;
case 'a':
clock_nanosleep_flags = TIMER_ABSTIME;
break;
case 't':
clock_nanosleep_flags = TIMER_ABSTIME;
use_so_txtime = true;
break;
case OPT_START: {
int64_t start_time_ns;
start_time_ns = strtoll(optarg, NULL, 0);
ts = ns_to_timespec(start_time_ns);
break;
}
case OPT_MARK:
so_mark_val = strtoul(optarg, NULL, 0);
break;
case 'e':
extended = 1;
view |= CANLIB_VIEW_INDENT_SFF;
break;
case 'f':
canfd = 1;
break;
case 'b':
brs = 1; /* bitrate switch implies CAN FD */
canfd = 1;
break;
case 'E':
esi = 1; /* error state indicator implies CAN FD */
canfd = 1;
break;
case 'X':
canxl = 1;
break;
case 'R':
rtr_frame = 1;
break;
case '8':
len8_dlc = 1;
view |= CANLIB_VIEW_LEN8_DLC;
break;
case 'm':
mix = 1;
canfd = 1; /* to switch the socket into CAN FD mode */
view |= CANLIB_VIEW_INDENT_SFF;
break;
case 'I':
if (optarg[0] == 'r') {
id_mode = MODE_RANDOM;
} else if (optarg[0] == 'i') {
id_mode = MODE_INCREMENT;
} else if (optarg[0] == 'e') {
id_mode = MODE_RANDOM_EVEN;
} else if (optarg[0] == 'o') {
id_mode = MODE_RANDOM_ODD;
} else {
id_mode = MODE_FIX;
cu.fd.can_id = strtoul(optarg, NULL, 16);
}
break;
case 'L':
if (optarg[0] == 'r') {
dlc_mode = MODE_RANDOM;
} else if (optarg[0] == 'i') {
dlc_mode = MODE_INCREMENT;
} else {
dlc_mode = MODE_FIX;
cu.fd.len = atoi(optarg) & 0xFF; /* is cut to 8 / 64 later */
}
break;
case 'D':
if (optarg[0] == 'r') {
data_mode = MODE_RANDOM;
} else if (optarg[0] == 'i') {
data_mode = MODE_INCREMENT;
} else {
data_mode = parse_dataoptarg(optarg, rand_position);
if (hexstring2data(optarg, fixdata, CANFD_MAX_DLEN)) {
printf("wrong fix data definition\n");
return 1;
}
}
break;
case 'F':
if (optarg[0] == 'r') {
xl_flags_mode = MODE_RANDOM;
} else if (optarg[0] == 'i') {
xl_flags_mode = MODE_INCREMENT;
} else {
xl_flags_mode = MODE_FIX;
if (sscanf(optarg, "%hhx", &xl_flags) != 1) {
printf("Bad xl_flags definition '%s'.\n", optarg);
exit(1);
}
}
break;
case 'S':
if (optarg[0] == 'r') {
xl_sdt_mode = MODE_RANDOM;
} else if (optarg[0] == 'i') {
xl_sdt_mode = MODE_INCREMENT;
} else {
xl_sdt_mode = MODE_FIX;
if (sscanf(optarg, "%hhx", &xl_sdt) != 1) {
printf("Bad xl_sdt definition '%s'.\n", optarg);
exit(1);
}
}
break;
case 'A':
if (optarg[0] == 'r') {
xl_af_mode = MODE_RANDOM;
} else if (optarg[0] == 'i') {
xl_af_mode = MODE_INCREMENT;
} else {
xl_af_mode = MODE_FIX;
xl_af = strtoul(optarg, NULL, 16);
}
break;
case 'V':
if (optarg[0] == 'r') {
xl_vcid_mode = MODE_RANDOM;
} else if (optarg[0] == 'i') {
xl_vcid_mode = MODE_INCREMENT;
} else {
xl_vcid_mode = MODE_FIX;
if (sscanf(optarg, "%hhx", &xl_vcid) != 1) {
printf("Bad xl_vcid definition '%s'.\n", optarg);
exit(1);
}
}
break;
case 'p':
polltimeout = strtoul(optarg, NULL, 10);
break;
case 'n':
count = atoi(optarg);
if (count < 1) {
print_usage(basename(argv[0]));
return 1;
}
break;
case 'i':
ignore_enobufs = true;
break;
case 'x':
loopback_disable = 1;
break;
case 'c':
burst_count = strtoul(optarg, NULL, 10);
break;
case 'v':
verbose++;
break;
case '?':
case 'h':
default:
print_usage(basename(argv[0]));
return 1;
}
}
if (optind == argc) {
print_usage(basename(argv[0]));
return 1;
}
if (verbose > 2)
view |= CANLIB_VIEW_ASCII;
ts_gap = double_to_timespec(gap / 1000);
/* recognize obviously missing commandline option */
if (id_mode == MODE_FIX && cu.fd.can_id > 0x7FF && !extended) {
printf("The given CAN-ID is greater than 0x7FF and the '-e' option is not set.\n");
return 1;
}
if (strlen(argv[optind]) >= IFNAMSIZ) {
printf("Name of CAN device '%s' is too long!\n\n", argv[optind]);
return 1;
}
s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (s < 0) {
perror("socket");
return 1;
}
addr.can_family = AF_CAN;
strcpy(ifr.ifr_name, argv[optind]);
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
perror("SIOCGIFINDEX");
return 1;
}
addr.can_ifindex = ifr.ifr_ifindex;
/*
* disable default receive filter on this RAW socket
* This is obsolete as we do not read from the socket at all, but for
* this reason we can remove the receive list in the Kernel to save a
* little (really a very little!) CPU usage.
*/
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
if (loopback_disable) {
const int loopback = 0;
setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
&loopback, sizeof(loopback));
}
if (canfd || canxl) {
/* check if the frame fits into the CAN netdevice */
if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
perror("SIOCGIFMTU");
return 1;
}
if (canfd) {
/* ensure discrete CAN FD length values 0..8, 12, 16, 20, 24, 32, 64 */
cu.fd.len = can_fd_dlc2len(can_fd_len2dlc(cu.fd.len));
} else {
/* limit fixed CAN XL data length to 64 */
if (cu.fd.len > CANFD_MAX_DLEN)
cu.fd.len = CANFD_MAX_DLEN;
}
if (canxl && (ifr.ifr_mtu < (int)CANXL_MIN_MTU)) {
printf("CAN interface not CAN XL capable - sorry.\n");
return 1;
}
if (canfd && (ifr.ifr_mtu < (int)CANFD_MTU)) {
printf("CAN interface not CAN FD capable - sorry.\n");
return 1;
}
if (ifr.ifr_mtu == (int)CANFD_MTU) {
/* interface is ok - try to switch the socket into CAN FD mode */
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES,
&enable_canfx, sizeof(enable_canfx))){
printf("error when enabling CAN FD support\n");
return 1;
}
}
if (ifr.ifr_mtu >= (int)CANXL_MIN_MTU) {
/* interface is ok - try to switch the socket into CAN XL mode */
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_XL_FRAMES,
&enable_canfx, sizeof(enable_canfx))){
printf("error when enabling CAN XL support\n");
return 1;
}
/* try to enable the CAN XL VCID pass through mode */
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_XL_VCID_OPTS,
&vcid_opts, sizeof(vcid_opts))) {
printf("error when enabling CAN XL VCID pass through\n");
return 1;
}
}
} else {
/* sanitize Classical CAN 2.0 frame length */
if (len8_dlc) {
if (cu.cc.len > CAN_MAX_RAW_DLC)
cu.cc.len = CAN_MAX_RAW_DLC;
if (cu.cc.len > CAN_MAX_DLEN)
cu.cc.len8_dlc = cu.cc.len;
}
if (cu.cc.len > CAN_MAX_DLEN)
cu.cc.len = CAN_MAX_DLEN;
}
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("bind");
return 1;
}
if (use_so_txtime) {
ret = setsockopt_txtime(s);
if (ret)
return 1;
}
ret = setup_time();
if (ret)
return 1;
while (running) {
/* clear values but preserve cu.fd.len */
cu.fd.flags = 0;
cu.fd.__res0 = 0;
cu.fd.__res1 = 0;
if (count && (--count == 0))
running = 0;
if (canxl) {
mtu = CANXL_MTU;
maxdlen = CANFD_MAX_DLEN; /* generate up to 64 byte */
extended = 0; /* prio has only 11 bit ID content */
data = cu.xl.data; /* fill CAN XL data */
} else if (canfd) {
mtu = CANFD_MTU;
maxdlen = CANFD_MAX_DLEN;
data = cu.fd.data; /* fill CAN CC/FD data */
cu.fd.flags = CANFD_FDF;
if (brs)
cu.fd.flags |= CANFD_BRS;
if (esi)
cu.fd.flags |= CANFD_ESI;
} else {
mtu = CAN_MTU;
maxdlen = CAN_MAX_DLEN;
data = cu.cc.data; /* fill CAN CC/FD data */
}
if (id_mode == MODE_RANDOM)
cu.fd.can_id = random();
else if (id_mode == MODE_RANDOM_EVEN)
cu.fd.can_id = random() & ~0x1;
else if (id_mode == MODE_RANDOM_ODD)
cu.fd.can_id = random() | 0x1;
if (extended) {
cu.fd.can_id &= CAN_EFF_MASK;
cu.fd.can_id |= CAN_EFF_FLAG;
} else {
cu.fd.can_id &= CAN_SFF_MASK;
}
if (rtr_frame && !canfd && !canxl)
cu.fd.can_id |= CAN_RTR_FLAG;
if (dlc_mode == MODE_RANDOM) {
if (canxl)
cu.fd.len = CANXL_MIN_DLEN + (random() & 0x3F);
else if (canfd)
cu.fd.len = can_fd_dlc2len(random() & 0xF);
else {
cu.cc.len = random() & 0xF;
if (cu.cc.len > CAN_MAX_DLEN) {
/* generate Classic CAN len8 DLCs? */
if (len8_dlc)
cu.cc.len8_dlc = cu.cc.len;
cu.cc.len = 8; /* for about 50% of the frames */
} else {
cu.cc.len8_dlc = 0;
}
}
}
if (data_mode == MODE_INCREMENT && !cu.cc.len)
cu.cc.len = 1; /* min dlc value for incr. data */
if (data_mode == MODE_RANDOM) {
rnd = random();
memcpy(&data[0], &rnd, 4);
rnd = random();
memcpy(&data[4], &rnd, 4);
/* omit extra random number generation for CAN FD */
if ((canfd || canxl) && cu.fd.len > 8) {
memcpy(&data[8], &data[0], 8);
memcpy(&data[16], &data[0], 16);
memcpy(&data[32], &data[0], 32);
}
}
if (data_mode == MODE_RANDOM_FIX) {
int i;
memcpy(data, fixdata, CANFD_MAX_DLEN);
for (i = 0; i < cu.fd.len; i++) {
if (rand_position[i] == (NIBBLE_H | NIBBLE_L)) {
data[i] = random();
} else if (rand_position[i] == NIBBLE_H) {
data[i] = (data[i] & 0x0f) | (random() & 0xf0);
} else if (rand_position[i] == NIBBLE_L) {
data[i] = (data[i] & 0xf0) | (random() & 0x0f);
}
}
}
if (data_mode == MODE_FIX)
memcpy(data, fixdata, CANFD_MAX_DLEN);
/* set unused payload data to zero like the CAN driver does it on rx */
if (cu.fd.len < maxdlen)
memset(&data[cu.fd.len], 0, maxdlen - cu.fd.len);
if (!use_so_txtime &&
(ts.tv_sec || ts.tv_nsec) &&
burst_sent_count >= burst_count) {
if (clock_nanosleep_flags == TIMER_ABSTIME)
ts = timespec_add(ts, ts_gap);
ret = clock_nanosleep(clockid, clock_nanosleep_flags, &ts, NULL);
if (ret != 0 && ret != EINTR) {
perror("clock_nanosleep");
return 1;
}
}
if (canxl) {
/* convert some CAN FD frame content into a CAN XL frame */
if (cu.fd.len < CANXL_MIN_DLEN) {
cu.fd.len = CANXL_MIN_DLEN;
data[0] = 0xCC; /* default filler */
}
cu.xl.len = cu.fd.len;
rnd = random();
if (xl_flags_mode == MODE_RANDOM) {
cu.xl.flags = rnd & CANXL_SEC;
} else if (xl_flags_mode == MODE_FIX) {
cu.xl.flags = xl_flags;
} else if (xl_flags_mode == MODE_INCREMENT) {
xl_flags ^= CANXL_SEC;
cu.xl.flags = (xl_flags & CANXL_SEC);
}
/* mark CAN XL frame */
cu.xl.flags |= CANXL_XLF;
if (xl_sdt_mode == MODE_RANDOM) {
cu.xl.sdt = rnd & 0xFF;
} else if (xl_sdt_mode == MODE_FIX) {
cu.xl.sdt = xl_sdt;
} else if (xl_sdt_mode == MODE_INCREMENT) {
xl_sdt++;
cu.xl.sdt = xl_sdt;
}
if (xl_af_mode == MODE_RANDOM) {
cu.xl.af = rnd;
} else if (xl_af_mode == MODE_FIX) {
cu.xl.af = xl_af;
} else if (xl_af_mode == MODE_INCREMENT) {
xl_af++;
cu.xl.af = xl_af;
}
if (xl_vcid_mode == MODE_RANDOM) {
cu.xl.prio |= rnd & CANXL_VCID_MASK;