]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - test/tst-filter-server.c
The commit aabdcb0b553b9c9547b1a506b34d55a764745870 ("can bcm: fix tx_setup
[socketcan-devel.git] / test / tst-filter-server.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * tst-filter-server.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 <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
61 #define ID  0x123
62 #define FIL 0x7FF
63 #define EFF CAN_EFF_FLAG
64 #define RTR CAN_RTR_FLAG
65
66 canid_t calc_id(int testcase)
67 {
68         canid_t id = ID;
69
70         if (testcase & 1)
71                 id |= EFF;
72         if (testcase & 2)
73                 id |= RTR;
74
75         return id;
76 }
77
78 canid_t calc_mask(int testcase)
79 {
80         canid_t mask = FIL;
81
82         if (testcase > 15)
83                 return (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
84
85         if (testcase & 4)
86                 mask |= EFF;
87         if (testcase & 8)
88                 mask |= RTR;
89
90         return mask;
91 }
92
93 int main(int argc, char **argv)
94 {
95         fd_set rdfs;
96         int s, t;
97         struct sockaddr_can addr;
98         struct can_filter rfilter;
99         struct can_frame frame;
100         int testcase = 0;
101         int nbytes, ret;
102         struct ifreq ifr;
103         int ifindex;
104
105
106         if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
107                 perror("socket");
108                 return 1;
109         }
110         if ((t = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
111                 perror("socket");
112                 return 1;
113         }
114
115         strcpy(ifr.ifr_name, "vcan0");
116         ioctl(s, SIOCGIFINDEX, &ifr);
117         ifindex = ifr.ifr_ifindex;
118
119         addr.can_family = AF_CAN;
120         addr.can_ifindex = ifindex;
121
122         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
123                 perror("bind");
124                 return 1;
125         }
126         if (bind(t, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
127                 perror("bind");
128                 return 1;
129         }
130
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));
134
135         /* disable default receive filter on the test socket */
136         setsockopt(t, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
137
138         while (1) {
139                 
140                 FD_ZERO(&rdfs);
141                 FD_SET(s, &rdfs);
142                 FD_SET(t, &rdfs);
143
144                 if ((ret = select(t+1, &rdfs, NULL, NULL, NULL)) < 0) {
145                         perror("select");
146                         break;
147                 }
148
149                 if (FD_ISSET(s, &rdfs)) {
150
151                         if ((nbytes = read(s, &frame, sizeof(struct can_frame))) < 0) {
152                                 perror("read");
153                                 exit(1);
154                         }
155
156                         if (nbytes < sizeof(struct can_frame)) {
157                                 fprintf(stderr, "read: incomplete CAN frame\n");
158                                 exit(1);
159                         }
160
161                         if ((frame.can_id != 0xF) || (frame.can_dlc != 1)) {
162                                 fprintf(stderr, "\nWrong request from master!\n");
163                                 exit(1);
164                         }
165
166                         testcase = frame.data[0];
167
168                         if (testcase < 18) {
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));
173
174                                 printf("testcase %2d : can_id = 0x%08X can_mask = 0x%08X\n",
175                                        testcase, rfilter.can_id, rfilter.can_mask);
176                         }
177
178                         frame.can_id = 0xFA; /* filter ack */
179
180                         if (write(s, &frame, sizeof(frame)) < 0) {
181                                 perror("write");
182                                 exit(1);
183                         }
184
185                         if (testcase > 17)
186                                 break;
187                 }
188
189                 if (FD_ISSET(t, &rdfs)) {
190
191                         if ((nbytes = read(t, &frame, sizeof(struct can_frame))) < 0) {
192                                 perror("read");
193                                 exit(1);
194                         }
195
196                         if (nbytes < sizeof(struct can_frame)) {
197                                 fprintf(stderr, "read: incomplete CAN frame\n");
198                                 exit(1);
199                         }
200                         
201                         printf ("%08X\n", frame.can_id);
202                 }
203         }
204
205         printf("testcase %2d : Filtertest done.\n", testcase);
206
207         close(s);
208         close(t);
209
210         return 0;
211 }