]> rtime.felk.cvut.cz Git - can-utils.git/blob - isotpsend.c
7b48ec266045a2d6d06dd6591d08b84ab3cc94f8
[can-utils.git] / isotpsend.c
1 /*
2  * isotpsend.c
3  *
4  * Copyright (c) 2008 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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.
23  *
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.
26  *
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
38  * DAMAGE.
39  *
40  * Send feedback to <linux-can@vger.kernel.org>
41  *
42  */
43
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <libgen.h>
49
50 #include <net/if.h>
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <sys/ioctl.h>
54
55 #include <linux/can.h>
56 #include <linux/can/isotp.h>
57
58 #define NO_CAN_ID 0xFFFFFFFFU
59 #define BUFSIZE 5000 /* size > 4095 to check socket API internal checks */
60
61 void print_usage(char *prg)
62 {
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>    (extended addressing mode)\n");
67         fprintf(stderr, "         -X <addr>    (extended addressing mode - rx addr)\n");
68         fprintf(stderr, "         -p <byte>    (set and enable padding byte)\n");
69         fprintf(stderr, "         -P <mode>    (check padding in FC. (l)ength (c)ontent (a)ll)\n");
70         fprintf(stderr, "         -t <time ns> (frame transmit time (N_As) in nanosecs)\n");
71         fprintf(stderr, "         -f <time ns> (ignore FC and force local tx stmin value in nanosecs)\n");
72         fprintf(stderr, "         -D <len>     (send a fixed PDU with len bytes - no STDIN data)\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");
76 }
77
78 int main(int argc, char **argv)
79 {
80     int s;
81     struct sockaddr_can addr;
82     struct ifreq ifr;
83     static struct can_isotp_options opts;
84     int opt;
85     extern int optind, opterr, optopt;
86     __u32 force_tx_stmin = 0;
87     unsigned char buf[BUFSIZE];
88     int buflen = 0;
89     int datalen = 0;
90     int retval = 0;
91
92     addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;
93
94     while ((opt = getopt(argc, argv, "s:d:x:X:p:P:t:f:D:?")) != -1) {
95             switch (opt) {
96             case 's':
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;
100                     break;
101
102             case 'd':
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;
106                     break;
107
108             case 'x':
109                     opts.flags |= CAN_ISOTP_EXTEND_ADDR;
110                     opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
111                     break;
112
113             case 'X':
114                     opts.flags |= CAN_ISOTP_RX_EXT_ADDR;
115                     opts.rx_ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
116                     break;
117
118             case 'p':
119                     opts.flags |= CAN_ISOTP_TX_PADDING;
120                     opts.txpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
121                     break;
122
123             case 'P':
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_LEN | CAN_ISOTP_CHK_PAD_DATA);
130                     else {
131                             printf("unknown padding check option '%c'.\n", optarg[0]);
132                             print_usage(basename(argv[0]));
133                             exit(0);
134                     }
135                     break;
136
137             case 't':
138                     opts.frame_txtime = strtoul(optarg, (char **)NULL, 10);
139                     break;
140
141             case 'f':
142                     opts.flags |= CAN_ISOTP_FORCE_TXSTMIN;
143                     force_tx_stmin = strtoul(optarg, (char **)NULL, 10);
144                     break;
145
146             case 'D':
147                     datalen = strtoul(optarg, (char **)NULL, 10);
148                     if (!datalen || datalen >= BUFSIZE) {
149                             print_usage(basename(argv[0]));
150                             exit(0);
151                     }
152                     break;
153
154             case '?':
155                     print_usage(basename(argv[0]));
156                     exit(0);
157                     break;
158
159             default:
160                     fprintf(stderr, "Unknown option %c\n", opt);
161                     print_usage(basename(argv[0]));
162                     exit(1);
163                     break;
164             }
165     }
166
167     if ((argc - optind != 1) ||
168         (addr.can_addr.tp.tx_id == NO_CAN_ID) ||
169         (addr.can_addr.tp.rx_id == NO_CAN_ID)) {
170             print_usage(basename(argv[0]));
171             exit(1);
172     }
173   
174     if ((opts.flags & CAN_ISOTP_RX_EXT_ADDR) && (!(opts.flags & CAN_ISOTP_EXTEND_ADDR))) {
175             print_usage(basename(argv[0]));
176             exit(1);
177     }
178
179     if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
180         perror("socket");
181         exit(1);
182     }
183
184     setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
185
186     if (opts.flags & CAN_ISOTP_FORCE_TXSTMIN)
187             setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_TX_STMIN, &force_tx_stmin, sizeof(force_tx_stmin));
188
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;
193
194     if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
195         perror("bind");
196         close(s);
197         exit(1);
198     }
199
200     if (!datalen) {
201             while (buflen < BUFSIZE && scanf("%hhx", &buf[buflen]) == 1)
202                     buflen++;
203     } else {
204             for (buflen = 0; buflen < datalen; buflen++)
205                     buf[buflen] = ((buflen % 0xFF) + 1) & 0xFF;
206     }
207
208
209     retval = write(s, buf, buflen);
210     if (retval < 0) {
211             perror("write");
212             return retval;
213     }
214
215     if (retval != buflen)
216             fprintf(stderr, "wrote only %d from %d byte\n", retval, buflen);
217
218     /* 
219      * due to a Kernel internal wait queue the PDU is sent completely
220      * before close() returns.
221      */
222     close(s);
223
224     return 0;
225 }