isotp: added support for separate extended address in rx path
[profile/ivi/can-utils.git] / isotprecv.c
1 /*
2  * isotprecv.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 SF/CF. (l)ength (c)ontent (a)ll)\n");
70         fprintf(stderr, "         -b <bs>      (blocksize. 0 = off)\n");
71         fprintf(stderr, "         -m <val>     (STmin in ms/ns. See spec.)\n");
72         fprintf(stderr, "         -f <time ns> (force rx stmin value in nanosecs)\n");
73         fprintf(stderr, "         -w <num>     (max. wait frame transmissions.)\n");
74         fprintf(stderr, "         -l           (loop: do not exit after pdu receiption.)\n");
75         fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
76         fprintf(stderr, "The pdu data is written on STDOUT in space separated ASCII hex values.\n");
77         fprintf(stderr, "\n");
78 }
79
80 int main(int argc, char **argv)
81 {
82     int s;
83     struct sockaddr_can addr;
84     struct ifreq ifr;
85     static struct can_isotp_options opts;
86     static struct can_isotp_fc_options fcopts;
87     int opt, i;
88     extern int optind, opterr, optopt;
89     __u32 force_rx_stmin = 0;
90     int loop = 0;
91
92     unsigned char msg[BUFSIZE];
93     int nbytes;
94
95     addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;
96
97     while ((opt = getopt(argc, argv, "s:d:x:X:p:P:b:m:w:f:l?")) != -1) {
98             switch (opt) {
99             case 's':
100                     addr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
101                     if (strlen(optarg) > 7)
102                             addr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
103                     break;
104
105             case 'd':
106                     addr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
107                     if (strlen(optarg) > 7)
108                             addr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
109                     break;
110
111             case 'x':
112                     opts.flags |= CAN_ISOTP_EXTEND_ADDR;
113                     opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
114                     break;
115
116             case 'X':
117                     opts.flags |= CAN_ISOTP_RX_EXT_ADDR;
118                     opts.rx_ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
119                     break;
120
121             case 'p':
122                     opts.flags |= CAN_ISOTP_RX_PADDING;
123                     opts.rxpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
124                     break;
125
126             case 'P':
127                     if (optarg[0] == 'l')
128                             opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
129                     else if (optarg[0] == 'c')
130                             opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
131                     else if (optarg[0] == 'a')
132                             opts.flags |= (CAN_ISOTP_CHK_PAD_LEN | CAN_ISOTP_CHK_PAD_DATA);
133                     else {
134                             printf("unknown padding check option '%c'.\n", optarg[0]);
135                             print_usage(basename(argv[0]));
136                             exit(0);
137                     }
138                     break;
139
140             case 'b':
141                     fcopts.bs = strtoul(optarg, (char **)NULL, 16) & 0xFF;
142                     break;
143
144             case 'm':
145                     fcopts.stmin = strtoul(optarg, (char **)NULL, 16) & 0xFF;
146                     break;
147
148             case 'w':
149                     fcopts.wftmax = strtoul(optarg, (char **)NULL, 16) & 0xFF;
150                     break;
151
152             case 'f':
153                     opts.flags |= CAN_ISOTP_FORCE_RXSTMIN;
154                     force_rx_stmin = strtoul(optarg, (char **)NULL, 10);
155                     break;
156
157             case 'l':
158                     loop = 1;
159                     break;
160
161             case '?':
162                     print_usage(basename(argv[0]));
163                     exit(0);
164                     break;
165
166             default:
167                     fprintf(stderr, "Unknown option %c\n", opt);
168                     print_usage(basename(argv[0]));
169                     exit(1);
170                     break;
171             }
172     }
173
174     if ((argc - optind != 1) ||
175         (addr.can_addr.tp.tx_id == NO_CAN_ID) ||
176         (addr.can_addr.tp.rx_id == NO_CAN_ID)) {
177             print_usage(basename(argv[0]));
178             exit(1);
179     }
180   
181     if ((opts.flags & CAN_ISOTP_RX_EXT_ADDR) && (!(opts.flags & CAN_ISOTP_EXTEND_ADDR))) {
182             print_usage(basename(argv[0]));
183             exit(1);
184     }
185
186     if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
187         perror("socket");
188         exit(1);
189     }
190
191     setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
192     setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts));
193
194     if (opts.flags & CAN_ISOTP_FORCE_RXSTMIN)
195             setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RX_STMIN, &force_rx_stmin, sizeof(force_rx_stmin));
196
197     addr.can_family = AF_CAN;
198     strcpy(ifr.ifr_name, argv[optind]);
199     ioctl(s, SIOCGIFINDEX, &ifr);
200     addr.can_ifindex = ifr.ifr_ifindex;
201
202     if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
203         perror("bind");
204         close(s);
205         exit(1);
206     }
207
208     do {
209             nbytes = read(s, msg, BUFSIZE);
210             if (nbytes > 0 && nbytes < BUFSIZE)
211                     for (i=0; i < nbytes; i++)
212                             printf("%02X ", msg[i]);
213             printf("\n");
214     } while (loop);
215
216     close(s);
217
218     return 0;
219 }