]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - isotprecv.c
Initialize packet counters when printing each routing entry.
[sojka/can-utils.git] / isotprecv.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * isotprecv.c
7  *
8  * Copyright (c) 2008 Volkswagen Group Electronic Research
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of Volkswagen nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * Alternatively, provided that this notice is retained in full, this
24  * software may be distributed under the terms of the GNU General
25  * Public License ("GPL") version 2, in which case the provisions of the
26  * GPL apply INSTEAD OF those given above.
27  *
28  * The provided data structures and external interfaces from this code
29  * are not restricted to be used by modules with a GPL compatible license.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
42  * DAMAGE.
43  *
44  * Send feedback to <socketcan-users@lists.berlios.de>
45  *
46  */
47
48 #include <stdio.h>
49 #include <unistd.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <libgen.h>
53
54 #include <net/if.h>
55 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <sys/ioctl.h>
58
59 #include <linux/can.h>
60 #include <linux/can/isotp.h>
61
62 #define NO_CAN_ID 0xFFFFFFFFU
63
64 void print_usage(char *prg)
65 {
66         fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
67         fprintf(stderr, "Options: -s <can_id>  (source can_id. Use 8 digits for extended IDs)\n");
68         fprintf(stderr, "         -d <can_id>  (destination can_id. Use 8 digits for extended IDs)\n");
69         fprintf(stderr, "         -x <addr>    (extended addressing mode.)\n");
70         fprintf(stderr, "         -p <byte>    (set and enable padding byte)\n");
71         fprintf(stderr, "         -P <mode>    (check padding in SF/CF. (l)ength (c)ontent (a)ll)\n");
72         fprintf(stderr, "         -b <bs>      (blocksize. 0 = off)\n");
73         fprintf(stderr, "         -m <val>     (STmin in ms/ns. See spec.)\n");
74         fprintf(stderr, "         -w <num>     (max. wait frame transmissions.)\n");
75         fprintf(stderr, "         -l           (loop: do not exit after pdu receiption.)\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     int opt, i;
89     extern int optind, opterr, optopt;
90     int loop = 0;
91
92     unsigned char msg[4096];
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:p:P:b:m:w: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 'p':
117                     opts.flags |= CAN_ISOTP_RX_PADDING;
118                     opts.rxpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
119                     break;
120
121             case 'P':
122                     if (optarg[0] == 'l')
123                             opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
124                     else if (optarg[0] == 'c')
125                             opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
126                     else if (optarg[0] == 'a')
127                             opts.flags |= (CAN_ISOTP_CHK_PAD_DATA | CAN_ISOTP_CHK_PAD_DATA);
128                     else {
129                             printf("unknown padding check option '%c'.\n", optarg[0]);
130                             print_usage(basename(argv[0]));
131                             exit(0);
132                     }
133                     break;
134
135             case 'b':
136                     fcopts.bs = strtoul(optarg, (char **)NULL, 16) & 0xFF;
137                     break;
138
139             case 'm':
140                     fcopts.stmin = strtoul(optarg, (char **)NULL, 16) & 0xFF;
141                     break;
142
143             case 'w':
144                     fcopts.wftmax = strtoul(optarg, (char **)NULL, 16) & 0xFF;
145                     break;
146
147             case 'l':
148                     loop = 1;
149                     break;
150
151             case '?':
152                     print_usage(basename(argv[0]));
153                     exit(0);
154                     break;
155
156             default:
157                     fprintf(stderr, "Unknown option %c\n", opt);
158                     print_usage(basename(argv[0]));
159                     exit(1);
160                     break;
161             }
162     }
163
164     if ((argc - optind != 1) ||
165         (addr.can_addr.tp.tx_id == NO_CAN_ID) ||
166         (addr.can_addr.tp.rx_id == NO_CAN_ID)) {
167             print_usage(basename(argv[0]));
168             exit(1);
169     }
170   
171     if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
172         perror("socket");
173         exit(1);
174     }
175
176     setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
177     setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts));
178
179     addr.can_family = AF_CAN;
180     strcpy(ifr.ifr_name, argv[optind]);
181     ioctl(s, SIOCGIFINDEX, &ifr);
182     addr.can_ifindex = ifr.ifr_ifindex;
183
184     if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
185         perror("bind");
186         close(s);
187         exit(1);
188     }
189
190     do {
191             nbytes = read(s, msg, 4096);
192             if (nbytes > 0 && nbytes < 4096)
193                     for (i=0; i < nbytes; i++)
194                             printf("%02X ", msg[i]);
195             printf("\n");
196     } while (loop);
197
198     close(s);
199
200     return 0;
201 }