1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20140227 |
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
|
|
1.1.1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20141123 |
59 |
#define BUFSIZE 5000 /* size > 4095 to check socket API internal checks */ |
1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20140227 |
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"); |
|
1.1.1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20141123 |
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"); |
|
1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20140227 |
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"); |
|
1.1.1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20141123 |
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"); |
|
1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20140227 |
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; |
|
1.1.1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20141123 |
84 |
static struct can_isotp_ll_options llopts; |
1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20140227 |
85 |
int opt; |
86 |
extern int optind, opterr, optopt; |
|
87 |
__u32 force_tx_stmin = 0; |
|
1.1.1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20141123 |
88 |
unsigned char buf[BUFSIZE]; |
1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20140227 |
89 |
int buflen = 0; |
1.1.1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20141123 |
90 |
int datalen = 0; |
91 |
int retval = 0; |
|
1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20140227 |
92 |
|
93 |
addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID; |
|
94 |
||
1.1.1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20141123 |
95 |
while ((opt = getopt(argc, argv, "s:d:x:p:P:t:f:D:L:?")) != -1) { |
1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20140227 |
96 |
switch (opt) { |
97 |
case 's': |
|
98 |
addr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16); |
|
99 |
if (strlen(optarg) > 7) |
|
100 |
addr.can_addr.tp.tx_id |= CAN_EFF_FLAG; |
|
101 |
break; |
|
102 |
||
103 |
case 'd': |
|
104 |
addr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16); |
|
105 |
if (strlen(optarg) > 7) |
|
106 |
addr.can_addr.tp.rx_id |= CAN_EFF_FLAG; |
|
107 |
break; |
|
108 |
||
109 |
case 'x': |
|
1.1.1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20141123 |
110 |
{
|
111 |
int elements = sscanf(optarg, "%hhx:%hhx", |
|
112 |
&opts.ext_address, |
|
113 |
&opts.rx_ext_address); |
|
114 |
||
115 |
if (elements == 1) |
|
116 |
opts.flags |= CAN_ISOTP_EXTEND_ADDR; |
|
117 |
else if (elements == 2) |
|
118 |
opts.flags |= (CAN_ISOTP_EXTEND_ADDR | CAN_ISOTP_RX_EXT_ADDR); |
|
119 |
else { |
|
120 |
printf("incorrect extended addr values '%s'.\n", optarg); |
|
121 |
print_usage(basename(argv[0])); |
|
122 |
exit(0); |
|
123 |
}
|
|
1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20140227 |
124 |
break; |
1.1.1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20141123 |
125 |
}
|
1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20140227 |
126 |
|
127 |
case 'p': |
|
1.1.1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20141123 |
128 |
{
|
129 |
int elements = sscanf(optarg, "%hhx:%hhx", |
|
130 |
&opts.txpad_content, |
|
131 |
&opts.rxpad_content); |
|
132 |
||
133 |
if (elements == 1) |
|
134 |
opts.flags |= CAN_ISOTP_TX_PADDING; |
|
135 |
else if (elements == 2) |
|
136 |
opts.flags |= (CAN_ISOTP_TX_PADDING | CAN_ISOTP_RX_PADDING); |
|
137 |
else if (sscanf(optarg, ":%hhx", &opts.rxpad_content) == 1) |
|
138 |
opts.flags |= CAN_ISOTP_RX_PADDING; |
|
139 |
else { |
|
140 |
printf("incorrect padding values '%s'.\n", optarg); |
|
141 |
print_usage(basename(argv[0])); |
|
142 |
exit(0); |
|
143 |
}
|
|
1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20140227 |
144 |
break; |
1.1.1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20141123 |
145 |
}
|
1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20140227 |
146 |
|
147 |
case 'P': |
|
148 |
if (optarg[0] == 'l') |
|
149 |
opts.flags |= CAN_ISOTP_CHK_PAD_LEN; |
|
150 |
else if (optarg[0] == 'c') |
|
151 |
opts.flags |= CAN_ISOTP_CHK_PAD_DATA; |
|
152 |
else if (optarg[0] == 'a') |
|
1.1.1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20141123 |
153 |
opts.flags |= (CAN_ISOTP_CHK_PAD_LEN | CAN_ISOTP_CHK_PAD_DATA); |
1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20140227 |
154 |
else { |
155 |
printf("unknown padding check option '%c'.\n", optarg[0]); |
|
156 |
print_usage(basename(argv[0])); |
|
157 |
exit(0); |
|
158 |
}
|
|
159 |
break; |
|
160 |
||
161 |
case 't': |
|
162 |
opts.frame_txtime = strtoul(optarg, (char **)NULL, 10); |
|
163 |
break; |
|
164 |
||
165 |
case 'f': |
|
166 |
opts.flags |= CAN_ISOTP_FORCE_TXSTMIN; |
|
167 |
force_tx_stmin = strtoul(optarg, (char **)NULL, 10); |
|
168 |
break; |
|
169 |
||
1.1.1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20141123 |
170 |
case 'D': |
171 |
datalen = strtoul(optarg, (char **)NULL, 10); |
|
172 |
if (!datalen || datalen >= BUFSIZE) { |
|
173 |
print_usage(basename(argv[0])); |
|
174 |
exit(0); |
|
175 |
}
|
|
176 |
break; |
|
177 |
||
178 |
case 'L': |
|
179 |
if (sscanf(optarg, "%hhu:%hhu:%hhu", |
|
180 |
&llopts.mtu, |
|
181 |
&llopts.tx_dl, |
|
182 |
&llopts.tx_flags) != 3) { |
|
183 |
printf("unknown link layer options '%s'.\n", optarg); |
|
184 |
print_usage(basename(argv[0])); |
|
185 |
exit(0); |
|
186 |
}
|
|
187 |
break; |
|
188 |
||
1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20140227 |
189 |
case '?': |
190 |
print_usage(basename(argv[0])); |
|
191 |
exit(0); |
|
192 |
break; |
|
193 |
||
194 |
default: |
|
195 |
fprintf(stderr, "Unknown option %c\n", opt); |
|
196 |
print_usage(basename(argv[0])); |
|
197 |
exit(1); |
|
198 |
break; |
|
199 |
}
|
|
200 |
}
|
|
201 |
||
202 |
if ((argc - optind != 1) || |
|
203 |
(addr.can_addr.tp.tx_id == NO_CAN_ID) || |
|
204 |
(addr.can_addr.tp.rx_id == NO_CAN_ID)) { |
|
205 |
print_usage(basename(argv[0])); |
|
206 |
exit(1); |
|
207 |
}
|
|
208 |
||
209 |
if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) { |
|
210 |
perror("socket"); |
|
211 |
exit(1); |
|
212 |
}
|
|
213 |
||
214 |
setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts)); |
|
215 |
||
1.1.1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20141123 |
216 |
if (llopts.tx_dl) { |
217 |
if (setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_LL_OPTS, &llopts, sizeof(llopts)) < 0) { |
|
218 |
perror("link layer sockopt"); |
|
219 |
exit(1); |
|
220 |
}
|
|
221 |
}
|
|
222 |
||
1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20140227 |
223 |
if (opts.flags & CAN_ISOTP_FORCE_TXSTMIN) |
224 |
setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_TX_STMIN, &force_tx_stmin, sizeof(force_tx_stmin)); |
|
225 |
||
226 |
addr.can_family = AF_CAN; |
|
227 |
strcpy(ifr.ifr_name, argv[optind]); |
|
228 |
ioctl(s, SIOCGIFINDEX, &ifr); |
|
229 |
addr.can_ifindex = ifr.ifr_ifindex; |
|
230 |
||
231 |
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
|
232 |
perror("bind"); |
|
233 |
close(s); |
|
234 |
exit(1); |
|
235 |
}
|
|
236 |
||
1.1.1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20141123 |
237 |
if (!datalen) { |
238 |
while (buflen < BUFSIZE && scanf("%hhx", &buf[buflen]) == 1) |
|
239 |
buflen++; |
|
240 |
} else { |
|
241 |
for (buflen = 0; buflen < datalen; buflen++) |
|
242 |
buf[buflen] = ((buflen % 0xFF) + 1) & 0xFF; |
|
243 |
}
|
|
244 |
||
245 |
||
246 |
retval = write(s, buf, buflen); |
|
247 |
if (retval < 0) { |
|
248 |
perror("write"); |
|
249 |
return retval; |
|
250 |
}
|
|
251 |
||
252 |
if (retval != buflen) |
|
253 |
fprintf(stderr, "wrote only %d from %d byte\n", retval, buflen); |
|
1
by Alexander GQ Gerasiov
Import upstream version 0.0+git20140227 |
254 |
|
255 |
/*
|
|
256 |
* due to a Kernel internal wait queue the PDU is sent completely
|
|
257 |
* before close() returns.
|
|
258 |
*/
|
|
259 |
close(s); |
|
260 |
||
261 |
return 0; |
|
262 |
}
|