4 * Copyright (c) 2008 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * Send feedback to <linux-can@vger.kernel.org>
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <sys/ioctl.h>
55 #include <linux/can.h>
56 #include <linux/can/isotp.h>
58 #define NO_CAN_ID 0xFFFFFFFFU
59 #define BUFSIZE 5000 /* size > 4095 to check socket API internal checks */
61 void print_usage(char *prg)
63 fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
64 fprintf(stderr, "Options: -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
65 fprintf(stderr, " -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
66 fprintf(stderr, " -x <addr>[:<rxaddr>] (extended addressing / opt. separate rxaddr)\n");
67 fprintf(stderr, " -p [tx]:[rx] (set and enable tx/rx padding bytes)\n");
68 fprintf(stderr, " -P <mode> (check rx padding for (l)ength (c)ontent (a)ll)\n");
69 fprintf(stderr, " -t <time ns> (frame transmit time (N_As) in nanosecs)\n");
70 fprintf(stderr, " -f <time ns> (ignore FC and force local tx stmin value in nanosecs)\n");
71 fprintf(stderr, " -D <len> (send a fixed PDU with len bytes - no STDIN data)\n");
72 fprintf(stderr, " -L <mtu>:<tx_dl>:<tx_flags> (link layer options for CAN FD)\n");
73 fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
74 fprintf(stderr, "The pdu data is expected on STDIN in space separated ASCII hex values.\n");
75 fprintf(stderr, "\n");
78 int main(int argc, char **argv)
81 struct sockaddr_can addr;
82 static struct can_isotp_options opts;
83 static struct can_isotp_ll_options llopts;
85 extern int optind, opterr, optopt;
86 __u32 force_tx_stmin = 0;
87 unsigned char buf[BUFSIZE];
92 addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;
94 while ((opt = getopt(argc, argv, "s:d:x:p:P:t:f:D:L:?")) != -1) {
97 addr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
98 if (strlen(optarg) > 7)
99 addr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
103 addr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
104 if (strlen(optarg) > 7)
105 addr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
110 int elements = sscanf(optarg, "%hhx:%hhx",
112 &opts.rx_ext_address);
115 opts.flags |= CAN_ISOTP_EXTEND_ADDR;
116 else if (elements == 2)
117 opts.flags |= (CAN_ISOTP_EXTEND_ADDR | CAN_ISOTP_RX_EXT_ADDR);
119 printf("incorrect extended addr values '%s'.\n", optarg);
120 print_usage(basename(argv[0]));
128 int elements = sscanf(optarg, "%hhx:%hhx",
130 &opts.rxpad_content);
133 opts.flags |= CAN_ISOTP_TX_PADDING;
134 else if (elements == 2)
135 opts.flags |= (CAN_ISOTP_TX_PADDING | CAN_ISOTP_RX_PADDING);
136 else if (sscanf(optarg, ":%hhx", &opts.rxpad_content) == 1)
137 opts.flags |= CAN_ISOTP_RX_PADDING;
139 printf("incorrect padding values '%s'.\n", optarg);
140 print_usage(basename(argv[0]));
147 if (optarg[0] == 'l')
148 opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
149 else if (optarg[0] == 'c')
150 opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
151 else if (optarg[0] == 'a')
152 opts.flags |= (CAN_ISOTP_CHK_PAD_LEN | CAN_ISOTP_CHK_PAD_DATA);
154 printf("unknown padding check option '%c'.\n", optarg[0]);
155 print_usage(basename(argv[0]));
161 opts.frame_txtime = strtoul(optarg, (char **)NULL, 10);
165 opts.flags |= CAN_ISOTP_FORCE_TXSTMIN;
166 force_tx_stmin = strtoul(optarg, (char **)NULL, 10);
170 datalen = strtoul(optarg, (char **)NULL, 10);
171 if (!datalen || datalen >= BUFSIZE) {
172 print_usage(basename(argv[0]));
178 if (sscanf(optarg, "%hhu:%hhu:%hhu",
181 &llopts.tx_flags) != 3) {
182 printf("unknown link layer options '%s'.\n", optarg);
183 print_usage(basename(argv[0]));
189 print_usage(basename(argv[0]));
194 fprintf(stderr, "Unknown option %c\n", opt);
195 print_usage(basename(argv[0]));
201 if ((argc - optind != 1) ||
202 (addr.can_addr.tp.tx_id == NO_CAN_ID) ||
203 (addr.can_addr.tp.rx_id == NO_CAN_ID)) {
204 print_usage(basename(argv[0]));
208 if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
213 setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
216 if (setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_LL_OPTS, &llopts, sizeof(llopts)) < 0) {
217 perror("link layer sockopt");
222 if (opts.flags & CAN_ISOTP_FORCE_TXSTMIN)
223 setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_TX_STMIN, &force_tx_stmin, sizeof(force_tx_stmin));
225 addr.can_family = AF_CAN;
226 addr.can_ifindex = if_nametoindex(argv[optind]);
228 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
235 while (buflen < BUFSIZE && scanf("%hhx", &buf[buflen]) == 1)
238 for (buflen = 0; buflen < datalen; buflen++)
239 buf[buflen] = ((buflen % 0xFF) + 1) & 0xFF;
243 retval = write(s, buf, buflen);
249 if (retval != buflen)
250 fprintf(stderr, "wrote only %d from %d byte\n", retval, buflen);
253 * due to a Kernel internal wait queue the PDU is sent completely
254 * before close() returns.