]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - isotprecv.c
can-utils: AOSP build clean up
[sojka/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
60 void print_usage(char *prg)
61 {
62         fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
63         fprintf(stderr, "Options: -s <can_id>  (source can_id. Use 8 digits for extended IDs)\n");
64         fprintf(stderr, "         -d <can_id>  (destination can_id. Use 8 digits for extended IDs)\n");
65         fprintf(stderr, "         -x <addr>    (extended addressing mode.)\n");
66         fprintf(stderr, "         -p <byte>    (set and enable padding byte)\n");
67         fprintf(stderr, "         -P <mode>    (check padding in SF/CF. (l)ength (c)ontent (a)ll)\n");
68         fprintf(stderr, "         -b <bs>      (blocksize. 0 = off)\n");
69         fprintf(stderr, "         -m <val>     (STmin in ms/ns. See spec.)\n");
70         fprintf(stderr, "         -f <time ns> (force rx stmin value in nanosecs)\n");
71         fprintf(stderr, "         -w <num>     (max. wait frame transmissions.)\n");
72         fprintf(stderr, "         -l           (loop: do not exit after pdu receiption.)\n");
73         fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
74         fprintf(stderr, "The pdu data is written on STDOUT 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     static struct can_isotp_fc_options fcopts;
85     int opt, i;
86     extern int optind, opterr, optopt;
87     __u32 force_rx_stmin = 0;
88     int loop = 0;
89
90     unsigned char msg[4096];
91     int nbytes;
92
93     addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;
94
95     while ((opt = getopt(argc, argv, "s:d:x:p:P:b:m:w:f:l?")) != -1) {
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':
110                     opts.flags |= CAN_ISOTP_EXTEND_ADDR;
111                     opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
112                     break;
113
114             case 'p':
115                     opts.flags |= CAN_ISOTP_RX_PADDING;
116                     opts.rxpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
117                     break;
118
119             case 'P':
120                     if (optarg[0] == 'l')
121                             opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
122                     else if (optarg[0] == 'c')
123                             opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
124                     else if (optarg[0] == 'a')
125                             opts.flags |= (CAN_ISOTP_CHK_PAD_DATA | CAN_ISOTP_CHK_PAD_DATA);
126                     else {
127                             printf("unknown padding check option '%c'.\n", optarg[0]);
128                             print_usage(basename(argv[0]));
129                             exit(0);
130                     }
131                     break;
132
133             case 'b':
134                     fcopts.bs = strtoul(optarg, (char **)NULL, 16) & 0xFF;
135                     break;
136
137             case 'm':
138                     fcopts.stmin = strtoul(optarg, (char **)NULL, 16) & 0xFF;
139                     break;
140
141             case 'w':
142                     fcopts.wftmax = strtoul(optarg, (char **)NULL, 16) & 0xFF;
143                     break;
144
145             case 'f':
146                     opts.flags |= CAN_ISOTP_FORCE_RXSTMIN;
147                     force_rx_stmin = strtoul(optarg, (char **)NULL, 10);
148                     break;
149
150             case 'l':
151                     loop = 1;
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 ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
175         perror("socket");
176         exit(1);
177     }
178
179     setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
180     setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts));
181
182     if (opts.flags & CAN_ISOTP_FORCE_RXSTMIN)
183             setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RX_STMIN, &force_rx_stmin, sizeof(force_rx_stmin));
184
185     addr.can_family = AF_CAN;
186     strcpy(ifr.ifr_name, argv[optind]);
187     ioctl(s, SIOCGIFINDEX, &ifr);
188     addr.can_ifindex = ifr.ifr_ifindex;
189
190     if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
191         perror("bind");
192         close(s);
193         exit(1);
194     }
195
196     do {
197             nbytes = read(s, msg, 4096);
198             if (nbytes > 0 && nbytes < 4096)
199                     for (i=0; i < nbytes; i++)
200                             printf("%02X ", msg[i]);
201             printf("\n");
202     } while (loop);
203
204     close(s);
205
206     return 0;
207 }