]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - test/tst-filter.c
Added new programm to test the CAN filters in af_can.c .
[socketcan-devel.git] / test / tst-filter.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * tst-filter.c
7  *
8  * Copyright (c) 2011 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 <sys/time.h>
57 #include <net/if.h>
58
59 #include <linux/can.h>
60 #include <linux/can/raw.h>
61
62 #define ID 0x123
63 #define TC 18 /* # of testcases */
64
65 const int rx_res[TC] = {4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1};
66 const int rxbits_res[TC] = {4369, 4369, 4369, 4369, 17, 4352, 17, 4352, 257, 257, 4112, 4112, 1, 256, 16, 4096, 1, 256};
67
68 canid_t calc_id(int testcase)
69 {
70         canid_t id = ID;
71
72         if (testcase & 1)
73                 id |= CAN_EFF_FLAG;
74         if (testcase & 2)
75                 id |= CAN_RTR_FLAG;
76
77         return id;
78 }
79
80 canid_t calc_mask(int testcase)
81 {
82         canid_t mask = CAN_SFF_MASK;
83
84         if (testcase > 15)
85                 return (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
86
87         if (testcase & 4)
88                 mask |= CAN_EFF_FLAG;
89         if (testcase & 8)
90                 mask |= CAN_RTR_FLAG;
91
92         return mask;
93 }
94
95 int main(int argc, char **argv)
96 {
97         fd_set rdfs;
98         struct timeval tv;
99         int s;
100         struct sockaddr_can addr;
101         struct can_filter rfilter;
102         struct can_frame frame;
103         int testcase;
104         int have_rx;
105         int rx;
106         int rxbits, rxbitval;
107         int ret;
108         int recv_own_msgs = 1;
109         struct ifreq ifr;
110
111         /* check command line options */
112         if (argc != 2) {
113                 fprintf(stderr, "Usage: %s <device>\n", argv[0]);
114                 return 1;
115         }
116
117         if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
118                 perror("socket");
119                 return 1;
120         }
121
122         strcpy(ifr.ifr_name, argv[1]);
123         if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
124                 perror("SIOCGIFINDEX");
125                 return 1;
126         }
127         addr.can_family = AF_CAN;
128         addr.can_ifindex = ifr.ifr_ifindex;
129
130         setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
131                    &recv_own_msgs, sizeof(recv_own_msgs));
132
133         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
134                 perror("bind");
135                 return 1;
136         }
137
138         printf("---\n");
139
140         for (testcase = 0; testcase < TC; testcase++) {
141                 
142                 rfilter.can_id   = calc_id(testcase);
143                 rfilter.can_mask = calc_mask(testcase);
144                 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER,
145                            &rfilter, sizeof(rfilter));
146
147                 printf("testcase %2d filters : can_id = 0x%08X can_mask = 0x%08X\n",
148                        testcase, rfilter.can_id, rfilter.can_mask);
149
150                 printf("testcase %2d sending patterns ... ", testcase);
151
152                 frame.can_dlc = 1;
153                 frame.data[0] = testcase;
154
155                 frame.can_id = ID;
156                 if (write(s, &frame, sizeof(frame)) < 0) {
157                         perror("write");
158                         exit(1);
159                 }
160                 frame.can_id = (ID | CAN_RTR_FLAG);
161                 if (write(s, &frame, sizeof(frame)) < 0) {
162                         perror("write");
163                         exit(1);
164                 }
165                 frame.can_id = (ID | CAN_EFF_FLAG);
166                 if (write(s, &frame, sizeof(frame)) < 0) {
167                         perror("write");
168                         exit(1);
169                 }
170                 frame.can_id = (ID | CAN_EFF_FLAG | CAN_RTR_FLAG);
171                 if (write(s, &frame, sizeof(frame)) < 0) {
172                         perror("write");
173                         exit(1);
174                 }
175
176                 printf("ok\n");
177
178                 have_rx = 1;
179                 rx = 0;
180                 rxbits = 0;
181
182                 while (have_rx) {
183
184                         have_rx = 0;
185                         FD_ZERO(&rdfs);
186                         FD_SET(s, &rdfs);
187                         tv.tv_sec = 0;
188                         tv.tv_usec = 50000; /* 50ms timeout */
189
190                         ret = select(s+1, &rdfs, NULL, NULL, &tv);
191                         if (ret < 0) {
192                                 perror("select");
193                                 exit(1);
194                         }
195
196                         if (FD_ISSET(s, &rdfs)) {
197                                 have_rx = 1;
198                                 ret = read(s, &frame, sizeof(struct can_frame));
199                                 if (ret < 0) {
200                                         perror("read");
201                                         exit(1);
202                                 }
203                                 if ((frame.can_id & CAN_SFF_MASK) != ID) {
204                                         fprintf(stderr, "received wrong can_id!\n");
205                                         exit(1);
206                                 }
207                                 if (frame.data[0] != testcase) {
208                                         fprintf(stderr, "received wrong testcase!\n");
209                                         exit(1);
210                                 }
211
212                                 /* test & calc rxbits */
213                                 rxbitval = 1 << ((frame.can_id & (CAN_EFF_FLAG|CAN_RTR_FLAG|CAN_ERR_FLAG)) >> 28);
214
215                                 /* only receive a rxbitval once */
216                                 if ((rxbits & rxbitval) == rxbitval) {
217                                         fprintf(stderr, "received rxbitval %d twice!\n", rxbitval);
218                                         exit(1);
219                                 }
220                                 rxbits |= rxbitval;
221                                 rx++;
222
223                                 printf("testcase %2d rx : can_id = 0x%08X rx = %d rxbits = %d\n",
224                                        testcase, frame.can_id, rx, rxbits);
225                         }
226                 }
227                 /* rx timed out -> check the received results */
228                 if (rx_res[testcase] != rx) {
229                         fprintf(stderr, "wrong rx value in testcase %d : %d (expected %d)\n",
230                                 testcase, rx, rx_res[testcase]);
231                         exit(1);
232                 }
233                 if (rxbits_res[testcase] != rxbits) {
234                         fprintf(stderr, "wrong rxbits value in testcase %d : %d (expected %d)\n",
235                                 testcase, rxbits, rxbits_res[testcase]);
236                         exit(1);
237                 }
238                 printf("testcase %2d ok\n---\n", testcase);
239         }
240
241         close(s);
242
243         return 0;
244 }