8 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
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
44 * Send feedback to <socketcan-users@lists.berlios.de>
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
58 #include <linux/can.h>
59 #include <linux/can/raw.h>
60 #include <linux/can/error.h>
62 int main(int argc, char **argv)
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 */
71 char *ifname = "vcan2";
76 while ((opt = getopt(argc, argv, "i:m:")) != -1) {
82 err_mask = strtoul(optarg, (char **)NULL, 16);
85 fprintf(stderr, "Unknown option %c\n", opt);
91 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
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));
100 setsockopt(s, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &err_mask, sizeof(err_mask));
102 strcpy(ifr.ifr_name, ifname);
103 ioctl(s, SIOCGIFINDEX, &ifr);
104 ifindex = ifr.ifr_ifindex;
106 addr.can_family = AF_CAN;
107 addr.can_ifindex = ifindex;
109 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
116 if ((nbytes = read(s, &frame, sizeof(struct can_frame))) < 0) {
119 } else if (nbytes < sizeof(struct can_frame)) {
120 fprintf(stderr, "read: incomplete CAN frame\n");
123 if (ioctl(s, SIOCGSTAMP, &tv) < 0)
124 perror("SIOCGSTAMP");
126 printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
128 if (frame.can_id & CAN_ERR_BUSOFF)
129 printf("(bus off) ");
131 if (frame.can_id & CAN_ERR_TX_TIMEOUT)
132 printf("(tx timeout) ");
134 if (frame.can_id & CAN_ERR_ACK)
137 if (frame.can_id & CAN_ERR_LOSTARB) {
138 printf("(lost arb)");
140 printf("[%d]", frame.data[0]);
144 if (frame.can_id & CAN_ERR_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]");
157 /* to be continued */