8 * Copyright (c) 2008 Volkswagen Group Electronic Research
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of Volkswagen nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * Alternatively, provided that this notice is retained in full, this
24 * software may be distributed under the terms of the GNU General
25 * Public License ("GPL") version 2, in which case the provisions of the
26 * GPL apply INSTEAD OF those given above.
28 * The provided data structures and external interfaces from this code
29 * are not restricted to be used by modules with a GPL compatible license.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
44 * Send feedback to <linux-can@vger.kernel.org>
55 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <sys/ioctl.h>
59 #include <linux/can.h>
60 #include <linux/can/isotp.h>
62 #define NO_CAN_ID 0xFFFFFFFFU
64 void print_usage(char *prg)
66 fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
67 fprintf(stderr, "Options: -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
68 fprintf(stderr, " -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
69 fprintf(stderr, " -x <addr> (extended addressing mode.)\n");
70 fprintf(stderr, " -p <byte> (set and enable padding byte)\n");
71 fprintf(stderr, " -P <mode> (check padding in SF/CF. (l)ength (c)ontent (a)ll)\n");
72 fprintf(stderr, " -b <bs> (blocksize. 0 = off)\n");
73 fprintf(stderr, " -m <val> (STmin in ms/ns. See spec.)\n");
74 fprintf(stderr, " -f <time ns> (force rx stmin value in nanosecs)\n");
75 fprintf(stderr, " -w <num> (max. wait frame transmissions.)\n");
76 fprintf(stderr, " -l (loop: do not exit after pdu receiption.)\n");
77 fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
78 fprintf(stderr, "The pdu data is written on STDOUT in space separated ASCII hex values.\n");
79 fprintf(stderr, "\n");
82 int main(int argc, char **argv)
85 struct sockaddr_can addr;
87 static struct can_isotp_options opts;
88 static struct can_isotp_fc_options fcopts;
90 extern int optind, opterr, optopt;
91 __u32 force_rx_stmin = 0;
94 unsigned char msg[4096];
97 addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;
99 while ((opt = getopt(argc, argv, "s:d:x:p:P:b:m:w:f:l?")) != -1) {
102 addr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
103 if (strlen(optarg) > 7)
104 addr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
108 addr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
109 if (strlen(optarg) > 7)
110 addr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
114 opts.flags |= CAN_ISOTP_EXTEND_ADDR;
115 opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
119 opts.flags |= CAN_ISOTP_RX_PADDING;
120 opts.rxpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
124 if (optarg[0] == 'l')
125 opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
126 else if (optarg[0] == 'c')
127 opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
128 else if (optarg[0] == 'a')
129 opts.flags |= (CAN_ISOTP_CHK_PAD_DATA | CAN_ISOTP_CHK_PAD_DATA);
131 printf("unknown padding check option '%c'.\n", optarg[0]);
132 print_usage(basename(argv[0]));
138 fcopts.bs = strtoul(optarg, (char **)NULL, 16) & 0xFF;
142 fcopts.stmin = strtoul(optarg, (char **)NULL, 16) & 0xFF;
146 fcopts.wftmax = strtoul(optarg, (char **)NULL, 16) & 0xFF;
150 opts.flags |= CAN_ISOTP_FORCE_RXSTMIN;
151 force_rx_stmin = strtoul(optarg, (char **)NULL, 10);
159 print_usage(basename(argv[0]));
164 fprintf(stderr, "Unknown option %c\n", opt);
165 print_usage(basename(argv[0]));
171 if ((argc - optind != 1) ||
172 (addr.can_addr.tp.tx_id == NO_CAN_ID) ||
173 (addr.can_addr.tp.rx_id == NO_CAN_ID)) {
174 print_usage(basename(argv[0]));
178 if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
183 setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
184 setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts));
186 if (opts.flags & CAN_ISOTP_FORCE_RXSTMIN)
187 setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RX_STMIN, &force_rx_stmin, sizeof(force_rx_stmin));
189 addr.can_family = AF_CAN;
190 strcpy(ifr.ifr_name, argv[optind]);
191 ioctl(s, SIOCGIFINDEX, &ifr);
192 addr.can_ifindex = ifr.ifr_ifindex;
194 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
201 nbytes = read(s, msg, 4096);
202 if (nbytes > 0 && nbytes < 4096)
203 for (i=0; i < nbytes; i++)
204 printf("%02X ", msg[i]);