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 <socketcan-users@lists.berlios.de>
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. Use 'any' for all addresses)\n");
70 fprintf(stderr, " -p <byte> (set and enable padding byte)\n");
71 fprintf(stderr, " -P <mode> (check padding in FC. (l)ength (c)ontent (a)ll)\n");
72 fprintf(stderr, " -t <time ns> (transmit time in nanosecs)\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;
83 static struct can_isotp_options opts;
85 extern int optind, opterr, optopt;
86 unsigned char buf[4096];
89 addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;
91 while ((opt = getopt(argc, argv, "s:d:x:p:P:t:?")) != -1) {
94 addr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
95 if (strlen(optarg) > 7)
96 addr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
100 addr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
101 if (strlen(optarg) > 7)
102 addr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
106 opts.flags |= CAN_ISOTP_EXTEND_ADDR;
107 opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
111 opts.flags |= CAN_ISOTP_TX_PADDING;
112 opts.txpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
116 if (optarg[0] == 'l')
117 opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
118 else if (optarg[0] == 'c')
119 opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
120 else if (optarg[0] == 'a')
121 opts.flags |= (CAN_ISOTP_CHK_PAD_DATA | CAN_ISOTP_CHK_PAD_DATA);
123 printf("unknown padding check option '%c'.\n", optarg[0]);
124 print_usage(basename(argv[0]));
130 opts.frame_txtime = strtoul(optarg, (char **)NULL, 10);
134 print_usage(basename(argv[0]));
139 fprintf(stderr, "Unknown option %c\n", opt);
140 print_usage(basename(argv[0]));
146 if ((argc - optind != 1) ||
147 (addr.can_addr.tp.tx_id == NO_CAN_ID) ||
148 (addr.can_addr.tp.rx_id == NO_CAN_ID)) {
149 print_usage(basename(argv[0]));
153 if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
158 setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
160 addr.can_family = AF_CAN;
161 strcpy(ifr.ifr_name, argv[optind]);
162 ioctl(s, SIOCGIFINDEX, &ifr);
163 addr.can_ifindex = ifr.ifr_ifindex;
165 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
171 while (buflen < 4096 && scanf("%hhx", &buf[buflen]) == 1)
174 write(s, buf, buflen);
177 * due to a Kernel internal wait queue the PDU is sent completely
178 * before close() returns.