8 * Copyright (c) 2008 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>
63 #define EFF CAN_EFF_FLAG
64 #define RTR CAN_RTR_FLAG
66 canid_t calc_id(int testcase)
78 canid_t calc_mask(int testcase)
83 return (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
93 int main(int argc, char **argv)
97 struct sockaddr_can addr;
98 struct can_filter rfilter;
99 struct can_frame frame;
106 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
110 if ((t = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
115 strcpy(ifr.ifr_name, "vcan0");
116 ioctl(s, SIOCGIFINDEX, &ifr);
117 ifindex = ifr.ifr_ifindex;
119 addr.can_family = AF_CAN;
120 addr.can_ifindex = ifindex;
122 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
126 if (bind(t, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
131 rfilter.can_id = 0xF; /* receive only the filter requests */
132 rfilter.can_mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG;
133 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
135 /* disable default receive filter on the test socket */
136 setsockopt(t, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
144 if ((ret = select(t+1, &rdfs, NULL, NULL, NULL)) < 0) {
149 if (FD_ISSET(s, &rdfs)) {
151 if ((nbytes = read(s, &frame, sizeof(struct can_frame))) < 0) {
156 if (nbytes < sizeof(struct can_frame)) {
157 fprintf(stderr, "read: incomplete CAN frame\n");
161 if ((frame.can_id != 0xF) || (frame.can_dlc != 1)) {
162 fprintf(stderr, "\nWrong request from master!\n");
166 testcase = frame.data[0];
169 rfilter.can_id = calc_id(testcase);
170 rfilter.can_mask = calc_mask(testcase);
171 setsockopt(t, SOL_CAN_RAW, CAN_RAW_FILTER,
172 &rfilter, sizeof(rfilter));
174 printf("testcase %2d : can_id = 0x%08X can_mask = 0x%08X\n",
175 testcase, rfilter.can_id, rfilter.can_mask);
178 frame.can_id = 0xFA; /* filter ack */
180 if (write(s, &frame, sizeof(frame)) < 0) {
189 if (FD_ISSET(t, &rdfs)) {
191 if ((nbytes = read(t, &frame, sizeof(struct can_frame))) < 0) {
196 if (nbytes < sizeof(struct can_frame)) {
197 fprintf(stderr, "read: incomplete CAN frame\n");
201 printf ("%08X\n", frame.can_id);
205 printf("testcase %2d : Filtertest done.\n", testcase);