8 * Implements a socket server which understands ASCII HEX
9 * messages for simple TCP/IP <-> ISO 15765-2 bridging.
11 * General message format: <[data]+>
13 * e.g. for an eight bytes PDU
17 * Valid ISO 15625-2 PDUs have a length from 1-4095 bytes.
20 * Andre Naujoks (the socket server stuff)
21 * Oliver Hartkopp (the rest)
23 * Copyright (c) 2002-2010 Volkswagen Group Electronic Research
24 * All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. Neither the name of Volkswagen nor the names of its contributors
35 * may be used to endorse or promote products derived from this software
36 * without specific prior written permission.
38 * Alternatively, provided that this notice is retained in full, this
39 * software may be distributed under the terms of the GNU General
40 * Public License ("GPL") version 2, in which case the provisions of the
41 * GPL apply INSTEAD OF those given above.
43 * The provided data structures and external interfaces from this code
44 * are not restricted to be used by modules with a GPL compatible license.
46 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
47 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
48 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
49 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
50 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
52 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
56 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
59 * Send feedback to <socketcan-users@lists.berlios.de>
71 #include <sys/types.h>
73 #include <sys/socket.h>
74 #include <sys/ioctl.h>
77 #include <netinet/in.h>
79 #include <linux/can.h>
80 #include <linux/can/isotp.h>
82 #define NO_CAN_ID 0xFFFFFFFFU
84 int b64hex(char *asc, unsigned char *bin, int len)
88 for (i = 0; i < len; i++) {
89 if (!sscanf(asc+(i*2), "%2hhx", bin+i))
100 void print_usage(char *prg)
102 fprintf(stderr, "\nUsage: %s -l <port> -s <can_id> -d <can_id> [options] <CAN interface>\n", prg);
103 fprintf(stderr, "Options: (* = mandatory)\n");
104 fprintf(stderr, "\n");
105 fprintf(stderr, "ip adressing:\n");
106 fprintf(stderr, " * -l <port> (local port for the server)\n");
107 fprintf(stderr, "\n");
108 fprintf(stderr, "isotp adressing:\n");
109 fprintf(stderr, " * -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
110 fprintf(stderr, " * -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
111 fprintf(stderr, " -x <addr> (extended addressing mode)\n");
112 fprintf(stderr, "\n");
113 fprintf(stderr, "padding:\n");
114 fprintf(stderr, " -p <byte> (set and enable tx padding byte)\n");
115 fprintf(stderr, " -r <byte> (set and enable rx padding byte)\n");
116 fprintf(stderr, " -P <mode> (check padding in SF/CF. (l)ength (c)ontent (a)ll)\n");
117 fprintf(stderr, "\n");
118 fprintf(stderr, "rx path: (config, which is sent to the sender / data source)\n");
119 fprintf(stderr, " -b <bs> (blocksize. 0 = off)\n");
120 fprintf(stderr, " -m <val> (STmin in ms/ns. See spec.)\n");
121 fprintf(stderr, " -w <num> (max. wait frame transmissions)\n");
122 fprintf(stderr, "\n");
123 fprintf(stderr, "tx path: (config, which changes local tx settings)\n");
124 fprintf(stderr, " -t <time ns> (transmit time in nanosecs)\n");
125 fprintf(stderr, "\n");
126 fprintf(stderr, "All values except for '-l' and '-t' are expected in hexadecimal values.\n");
127 fprintf(stderr, "\n");
130 int main(int argc, char **argv)
132 extern int optind, opterr, optopt;
135 int sl, sa, sc; /* (L)isten, (A)ccept, (C)AN sockets */
136 struct sockaddr_in saddr, clientaddr;
137 struct sockaddr_can caddr;
138 static struct can_isotp_options opts;
139 static struct can_isotp_fc_options fcopts;
141 socklen_t sin_size = sizeof(clientaddr);
142 socklen_t caddrlen = sizeof(caddr);
144 struct sigaction signalaction;
155 int idx = 0; /* index in txmsg[] */
157 unsigned char msg[4096]; /* isotp socket message buffer (4095 + test_for_too_long_byte)*/
158 char rxmsg[8194]; /* isotp->tcp ASCII message buffer (4095*2 + < > \n null) */
159 char txmsg[8193]; /* tcp->isotp ASCII message buffer (4095*2 + < > null) */
161 /* mark missing mandatory commandline options as missing */
162 caddr.can_addr.tp.tx_id = caddr.can_addr.tp.rx_id = NO_CAN_ID;
164 while ((opt = getopt(argc, argv, "l:s:d:x:p:r:P:b:m:w:t:v?")) != -1) {
167 local_port = strtoul(optarg, (char **)NULL, 10);
171 caddr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
172 if (strlen(optarg) > 7)
173 caddr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
177 caddr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
178 if (strlen(optarg) > 7)
179 caddr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
183 opts.flags |= CAN_ISOTP_EXTEND_ADDR;
184 opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
188 opts.flags |= CAN_ISOTP_TX_PADDING;
189 opts.txpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
193 opts.flags |= CAN_ISOTP_RX_PADDING;
194 opts.rxpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
198 if (optarg[0] == 'l')
199 opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
200 else if (optarg[0] == 'c')
201 opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
202 else if (optarg[0] == 'a')
203 opts.flags |= (CAN_ISOTP_CHK_PAD_DATA | CAN_ISOTP_CHK_PAD_DATA);
205 printf("unknown padding check option '%c'.\n", optarg[0]);
206 print_usage(basename(argv[0]));
212 fcopts.bs = strtoul(optarg, (char **)NULL, 16) & 0xFF;
216 fcopts.stmin = strtoul(optarg, (char **)NULL, 16) & 0xFF;
220 fcopts.wftmax = strtoul(optarg, (char **)NULL, 16) & 0xFF;
224 opts.frame_txtime = strtoul(optarg, (char **)NULL, 10);
232 print_usage(basename(argv[0]));
237 fprintf(stderr, "Unknown option %c\n", opt);
238 print_usage(basename(argv[0]));
244 if ((argc - optind != 1) || (local_port == 0) ||
245 (caddr.can_addr.tp.tx_id == NO_CAN_ID) ||
246 (caddr.can_addr.tp.rx_id == NO_CAN_ID)) {
247 print_usage(basename(argv[0]));
251 sigemptyset(&sigset);
252 signalaction.sa_handler = &childdied;
253 signalaction.sa_mask = sigset;
254 signalaction.sa_flags = 0;
255 sigaction(SIGCHLD, &signalaction, NULL); /* signal for dying child */
257 if((sl = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
258 perror("inetsocket");
262 saddr.sin_family = AF_INET;
263 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
264 saddr.sin_port = htons(local_port);
266 while(bind(sl,(struct sockaddr*)&saddr, sizeof(saddr)) < 0) {
272 if (listen(sl, 3) != 0) {
278 sa = accept(sl,(struct sockaddr *)&clientaddr, &sin_size);
287 if (errno != EINTR) {
289 * If the cause for the error was NOT the
290 * signal from a dying child => give an error
298 if ((sc = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
303 setsockopt(sc, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
304 setsockopt(sc, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts));
306 caddr.can_family = AF_CAN;
307 strcpy(ifr.ifr_name, argv[optind]);
308 if (ioctl(sc, SIOCGIFINDEX, &ifr) < 0) {
309 perror("SIOCGIFINDEX");
312 caddr.can_ifindex = ifr.ifr_ifindex;
314 if (bind(sc, (struct sockaddr *)&caddr, caddrlen) < 0) {
322 FD_SET(sc, &readfds);
323 FD_SET(sa, &readfds);
325 ret = select((sc > sa)?sc+1:sa+1, &readfds, NULL, NULL, NULL);
327 if (FD_ISSET(sc, &readfds)) {
330 nbytes = read(sc, &msg, 4096);
332 if (nbytes < 1 || nbytes > 4095) {
333 perror("read from isotp socket");
339 for ( i = 0; i < nbytes; i++)
340 sprintf(rxmsg + 1 + 2*i, "%02X", msg[i]);
342 /* finalize string for sending */
343 strcat(rxmsg, ">\n");
346 printf("CAN>TCP %s", rxmsg);
348 send(sa, rxmsg, strlen(rxmsg), 0);
352 if (FD_ISSET(sa, &readfds)) {
354 if (read(sa, txmsg+idx, 1) < 1) {
355 perror("read from tcp/ip socket");
366 /* max len is 4095*2 + '<' + '>' = 8192. The buffer index starts with 0 */
372 if (txmsg[idx] != '>') {
380 /* must be an even number of bytes and at least one data byte <XX> */
381 if (strlen(txmsg) < 4 || strlen(txmsg) % 2)
385 printf("TCP>CAN %s\n", txmsg);
387 nbytes = (strlen(txmsg)-2)/2;
388 if (b64hex(txmsg+1, msg, nbytes) == 0)
389 send(sc, msg, nbytes, 0);