]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - test/tst-err.c
Added missing inclusion of linux/types.h
[socketcan-devel.git] / test / tst-err.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * tst-err.c
7  *
8  * Copyright (c) 2002-2007 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 <stdlib.h>
50 #include <unistd.h>
51 #include <string.h>
52
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
56 #include <net/if.h>
57
58 #include <linux/can.h>
59 #include <linux/can/raw.h>
60 #include <linux/can/error.h>
61
62 int main(int argc, char **argv)
63 {
64         int s;
65         struct sockaddr_can addr;
66         struct can_filter rfilter;
67         struct can_frame frame;
68         can_err_mask_t err_mask = CAN_ERR_MASK; /* all */
69         int nbytes;
70         struct ifreq ifr;
71         char *ifname = "vcan2";
72         int ifindex;
73         int opt;
74         struct timeval tv;
75
76         while ((opt = getopt(argc, argv, "i:m:")) != -1) {
77                 switch (opt) {
78                 case 'i':
79                         ifname = optarg;
80                         break;
81                 case 'm':
82                         err_mask = strtoul(optarg, (char **)NULL, 16);
83                         break;
84                 default:
85                         fprintf(stderr, "Unknown option %c\n", opt);
86                         break;
87                 }
88         }
89
90
91         if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
92                 perror("socket");
93                 return 1;
94         }
95
96         rfilter.can_id   = CAN_INV_FILTER; /* no normal CAN frames */
97         rfilter.can_mask = 0; /* all: INV(all) == nothing */
98         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
99
100         setsockopt(s, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &err_mask, sizeof(err_mask));
101
102         strcpy(ifr.ifr_name, ifname);
103         ioctl(s, SIOCGIFINDEX, &ifr);
104         ifindex = ifr.ifr_ifindex;
105
106         addr.can_family = AF_CAN;
107         addr.can_ifindex = ifindex;
108
109         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
110                 perror("bind");
111                 return 1;
112         }
113
114         while (1) {
115
116                 if ((nbytes = read(s, &frame, sizeof(struct can_frame))) < 0) {
117                         perror("read");
118                         return 1;
119                 } else if (nbytes < sizeof(struct can_frame)) {
120                         fprintf(stderr, "read: incomplete CAN frame\n");
121                         return 1;
122                 } else {
123                         if (ioctl(s, SIOCGSTAMP, &tv) < 0)
124                                 perror("SIOCGSTAMP");
125                         else
126                                 printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
127
128                         if (frame.can_id & CAN_ERR_BUSOFF)
129                                 printf("(bus off) ");
130
131                         if (frame.can_id & CAN_ERR_TX_TIMEOUT)
132                                 printf("(tx timeout) ");
133
134                         if (frame.can_id & CAN_ERR_ACK)
135                                 printf("(ack) ");
136
137                         if (frame.can_id & CAN_ERR_LOSTARB) {
138                                 printf("(lost arb)");
139                                 if (frame.data[0])
140                                         printf("[%d]", frame.data[0]);
141                                 printf(" ");
142                         }
143
144                         if (frame.can_id & CAN_ERR_CRTL) {
145                                 printf("(crtl)");
146                                 if (frame.data[1] & CAN_ERR_CRTL_RX_OVERFLOW)
147                                         printf("[RX buffer overflow]");
148                                 if (frame.data[1] & CAN_ERR_CRTL_TX_OVERFLOW)
149                                         printf("[TX buffer overflow]");
150                                 if (frame.data[1] & CAN_ERR_CRTL_RX_WARNING)
151                                         printf("[RX warning]");
152                                 if (frame.data[1] & CAN_ERR_CRTL_TX_WARNING)
153                                         printf("[TX warning]");
154                                 printf(" ");
155                         }
156
157                         /* to be continued */
158
159                         printf("\n");
160                         fflush(stdout);
161                 }
162         }
163
164         close(s);
165
166         return 0;
167 }