]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - test/tst-err.c
43ba07e441d09144448fba6c1c81fc25b77c6975
[socketcan-devel.git] / test / tst-err.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * tst-err.c
7  *
8  * Copyright (c) 2002-2006 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, the following disclaimer and
16  *    the referenced file 'COPYING'.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of Volkswagen nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * Alternatively, provided that this notice is retained in full, this
25  * software may be distributed under the terms of the GNU General
26  * Public License ("GPL") version 2 as distributed in the 'COPYING'
27  * file from the main directory of the linux kernel source.
28  *
29  * The provided data structures and external interfaces from this code
30  * are not restricted to be used by modules with a GPL compatible license.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
43  * DAMAGE.
44  *
45  * Send feedback to <socketcan-users@lists.berlios.de>
46  *
47  */
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 #include <string.h>
53
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <sys/ioctl.h>
57
58 #include "af_can.h"
59 #include "raw.h"
60 #include "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 }