Initial packaging.
[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, "         -L <mtu>:<tx_dl>:<tx_flags> (link layer options for CAN FD)\n");
76         fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
77         fprintf(stderr, "The pdu data is written on STDOUT in space separated ASCII hex values.\n");
78         fprintf(stderr, "\n");
79 }
80
81 int main(int argc, char **argv)
82 {
83     int s;
84     struct sockaddr_can addr;
85     struct ifreq ifr;
86     static struct can_isotp_options opts;
87     static struct can_isotp_fc_options fcopts;
88     static struct can_isotp_ll_options llopts;
89     int opt, i;
90     extern int optind, opterr, optopt;
91     __u32 force_rx_stmin = 0;
92     int loop = 0;
93
94     unsigned char msg[BUFSIZE];
95     int nbytes;
96
97     addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;
98
99     while ((opt = getopt(argc, argv, "s:d:x:X:p:P:b:m:w:f:lL:?")) != -1) {
100             switch (opt) {
101             case 's':
102                     addr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
103                     if (strlen(optarg) > 7)
104                             addr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
105                     break;
106
107             case 'd':
108                     addr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
109                     if (strlen(optarg) > 7)
110                             addr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
111                     break;
112
113             case 'x':
114                     opts.flags |= CAN_ISOTP_EXTEND_ADDR;
115                     opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
116                     break;
117
118             case 'X':
119                     opts.flags |= CAN_ISOTP_RX_EXT_ADDR;
120                     opts.rx_ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
121                     break;
122
123             case 'p':
124                     opts.flags |= CAN_ISOTP_RX_PADDING;
125                     opts.rxpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
126                     break;
127
128             case 'P':
129                     if (optarg[0] == 'l')
130                             opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
131                     else if (optarg[0] == 'c')
132                             opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
133                     else if (optarg[0] == 'a')
134                             opts.flags |= (CAN_ISOTP_CHK_PAD_LEN | CAN_ISOTP_CHK_PAD_DATA);
135                     else {
136                             printf("unknown padding check option '%c'.\n", optarg[0]);
137                             print_usage(basename(argv[0]));
138                             exit(0);
139                     }
140                     break;
141
142             case 'b':
143                     fcopts.bs = strtoul(optarg, (char **)NULL, 16) & 0xFF;
144                     break;
145
146             case 'm':
147                     fcopts.stmin = strtoul(optarg, (char **)NULL, 16) & 0xFF;
148                     break;
149
150             case 'w':
151                     fcopts.wftmax = strtoul(optarg, (char **)NULL, 16) & 0xFF;
152                     break;
153
154             case 'f':
155                     opts.flags |= CAN_ISOTP_FORCE_RXSTMIN;
156                     force_rx_stmin = strtoul(optarg, (char **)NULL, 10);
157                     break;
158
159             case 'l':
160                     loop = 1;
161                     break;
162
163             case 'L':
164                     if (sscanf(optarg, "%hhu:%hhu:%hhu",
165                                &llopts.mtu,
166                                &llopts.tx_dl,
167                                &llopts.tx_flags) != 3) {
168                             printf("unknown link layer options '%s'.\n", optarg);
169                             print_usage(basename(argv[0]));
170                             exit(0);
171                     }
172                     break;
173
174             case '?':
175                     print_usage(basename(argv[0]));
176                     exit(0);
177                     break;
178
179             default:
180                     fprintf(stderr, "Unknown option %c\n", opt);
181                     print_usage(basename(argv[0]));
182                     exit(1);
183                     break;
184             }
185     }
186
187     if ((argc - optind != 1) ||
188         (addr.can_addr.tp.tx_id == NO_CAN_ID) ||
189         (addr.can_addr.tp.rx_id == NO_CAN_ID)) {
190             print_usage(basename(argv[0]));
191             exit(1);
192     }
193   
194     if ((opts.flags & CAN_ISOTP_RX_EXT_ADDR) && (!(opts.flags & CAN_ISOTP_EXTEND_ADDR))) {
195             print_usage(basename(argv[0]));
196             exit(1);
197     }
198
199     if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
200         perror("socket");
201         exit(1);
202     }
203
204     setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
205     setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts));
206
207     if (llopts.tx_dl) {
208         if (setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_LL_OPTS, &llopts, sizeof(llopts)) < 0) {
209             perror("link layer sockopt");
210             exit(1);
211         }
212     }
213
214     if (opts.flags & CAN_ISOTP_FORCE_RXSTMIN)
215             setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RX_STMIN, &force_rx_stmin, sizeof(force_rx_stmin));
216
217     addr.can_family = AF_CAN;
218     strcpy(ifr.ifr_name, argv[optind]);
219     ioctl(s, SIOCGIFINDEX, &ifr);
220     addr.can_ifindex = ifr.ifr_ifindex;
221
222     if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
223         perror("bind");
224         close(s);
225         exit(1);
226     }
227
228     do {
229             nbytes = read(s, msg, BUFSIZE);
230             if (nbytes > 0 && nbytes < BUFSIZE)
231                     for (i=0; i < nbytes; i++)
232                             printf("%02X ", msg[i]);
233             printf("\n");
234     } while (loop);
235
236     close(s);
237
238     return 0;
239 }