1 /*
2  * tst-filter.c
3  *
4  * Copyright (c) 2011 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of Volkswagen nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * Alternatively, provided that this notice is retained in full, this
20  * software may be distributed under the terms of the GNU General
21  * Public License ("GPL") version 2, in which case the provisions of the
22  * GPL apply INSTEAD OF those given above.
23  *
24  * The provided data structures and external interfaces from this code
25  * are not restricted to be used by modules with a GPL compatible license.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  *
40  * Send feedback to <socketcan-users@lists.berlios.de>
41  *
42  */
43 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <string.h>
48 
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <sys/ioctl.h>
52 #include <sys/time.h>
53 #include <net/if.h>
54 #include "config.h"
55 #include "tst_res_flags.h"
56 
57 #ifdef HAVE_LINUX_CAN_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,
67 			     257, 4112, 4112, 1, 256, 16, 4096, 1, 256 };
68 
calc_id(int testcase)69 canid_t calc_id(int testcase)
70 {
71 	canid_t id = ID;
72 
73 	if (testcase & 1)
74 		id |= CAN_EFF_FLAG;
75 	if (testcase & 2)
76 		id |= CAN_RTR_FLAG;
77 
78 	return id;
79 }
80 
calc_mask(int testcase)81 canid_t calc_mask(int testcase)
82 {
83 	canid_t mask = CAN_SFF_MASK;
84 
85 	if (testcase > 15)
86 		return CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG;
87 
88 	if (testcase & 4)
89 		mask |= CAN_EFF_FLAG;
90 	if (testcase & 8)
91 		mask |= CAN_RTR_FLAG;
92 
93 	return mask;
94 }
95 
main(int argc,char ** argv)96 int main(int argc, char **argv)
97 {
98 	fd_set rdfs;
99 	struct timeval tv;
100 	int s;
101 	struct sockaddr_can addr;
102 	struct can_filter rfilter;
103 	struct can_frame frame;
104 	int testcase;
105 	int have_rx;
106 	int rx;
107 	int rxbits, rxbitval;
108 	int ret;
109 	int recv_own_msgs = 1;
110 	struct ifreq ifr;
111 
112 	/* check command line options */
113 	if (argc != 2) {
114 		fprintf(stderr, "Usage: %s <device>\n", argv[0]);
115 		return TFAIL;
116 	}
117 
118 	s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
119 	if (s < 0) {
120 		perror("socket");
121 		return TFAIL;
122 	}
123 
124 	strcpy(ifr.ifr_name, argv[1]);
125 	if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
126 		perror("SIOCGIFINDEX");
127 		return TFAIL;
128 	}
129 	addr.can_family = AF_CAN;
130 	addr.can_ifindex = ifr.ifr_ifindex;
131 
132 	setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
133 		   &recv_own_msgs, sizeof(recv_own_msgs));
134 
135 	if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
136 		perror("bind");
137 		return TFAIL;
138 	}
139 
140 	printf("---\n");
141 
142 	for (testcase = 0; testcase < TC; testcase++) {
143 
144 		rfilter.can_id = calc_id(testcase);
145 		rfilter.can_mask = calc_mask(testcase);
146 		setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER,
147 			   &rfilter, sizeof(rfilter));
148 
149 		printf("testcase %2d filters : can_id = 0x%08X can_mask = "
150 		       "0x%08X\n", testcase, rfilter.can_id, rfilter.can_mask);
151 
152 		printf("testcase %2d sending patterns ... ", testcase);
153 
154 		frame.can_dlc = 1;
155 		frame.data[0] = testcase;
156 
157 		frame.can_id = ID;
158 		if (write(s, &frame, sizeof(frame)) < 0) {
159 			perror("write");
160 			exit(1);
161 		}
162 		frame.can_id = (ID | CAN_RTR_FLAG);
163 		if (write(s, &frame, sizeof(frame)) < 0) {
164 			perror("write");
165 			exit(1);
166 		}
167 		frame.can_id = (ID | CAN_EFF_FLAG);
168 		if (write(s, &frame, sizeof(frame)) < 0) {
169 			perror("write");
170 			exit(1);
171 		}
172 		frame.can_id = (ID | CAN_EFF_FLAG | CAN_RTR_FLAG);
173 		if (write(s, &frame, sizeof(frame)) < 0) {
174 			perror("write");
175 			exit(1);
176 		}
177 
178 		printf("ok\n");
179 
180 		have_rx = 1;
181 		rx = 0;
182 		rxbits = 0;
183 
184 		while (have_rx) {
185 
186 			have_rx = 0;
187 			FD_ZERO(&rdfs);
188 			FD_SET(s, &rdfs);
189 			tv.tv_sec = 0;
190 			tv.tv_usec = 50000;	/* 50ms timeout */
191 
192 			ret = select(s + 1, &rdfs, NULL, NULL, &tv);
193 			if (ret < 0) {
194 				perror("select");
195 				exit(1);
196 			}
197 
198 			if (FD_ISSET(s, &rdfs)) {
199 				have_rx = 1;
200 				ret = read(s, &frame, sizeof(struct can_frame));
201 				if (ret < 0) {
202 					perror("read");
203 					exit(1);
204 				}
205 				if ((frame.can_id & CAN_SFF_MASK) != ID) {
206 					fprintf(stderr,
207 						"received wrong can_id!\n");
208 					exit(1);
209 				}
210 				if (frame.data[0] != testcase) {
211 					fprintf(stderr,
212 						"received wrong testcase!\n");
213 					exit(1);
214 				}
215 
216 				/* test & calc rxbits */
217 				rxbitval = 1 << ((frame.can_id &
218 						 (CAN_EFF_FLAG | CAN_RTR_FLAG |
219 						  CAN_ERR_FLAG)) >> 28);
220 
221 				/* only receive a rxbitval once */
222 				if ((rxbits & rxbitval) == rxbitval) {
223 					fprintf(stderr,
224 						"received rxbitval %d twice!\n",
225 						rxbitval);
226 					exit(1);
227 				}
228 				rxbits |= rxbitval;
229 				rx++;
230 
231 				printf("testcase %2d rx : can_id = 0x%08X rx = "
232 				       "%d rxbits = %d\n", testcase,
233 				       frame.can_id, rx, rxbits);
234 			}
235 		}
236 		/* rx timed out -> check the received results */
237 		if (rx_res[testcase] != rx) {
238 			fprintf(stderr,
239 				"wrong rx value in testcase %d : %d (expected "
240 				"%d)\n", testcase, rx, rx_res[testcase]);
241 			exit(1);
242 		}
243 		if (rxbits_res[testcase] != rxbits) {
244 			fprintf(stderr,
245 				"wrong rxbits value in testcase %d : %d "
246 				"(expected %d)\n", testcase, rxbits,
247 				rxbits_res[testcase]);
248 			exit(1);
249 		}
250 		printf("testcase %2d ok\n---\n", testcase);
251 	}
252 
253 	close(s);
254 
255 	return TPASS;
256 }
257 
258 #else
259 
main(void)260 int main(void)
261 {
262 	printf("The linux/can.h was missing upon compilation.\n");
263 	return TCONF;
264 }
265 
266 #endif /* HAVE_LINUX_CAN_H */
267