8 * Copyright (c) 2011 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>
59 #include <linux/can.h>
60 #include <linux/can/raw.h>
63 #define TC 18 /* # of testcases */
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};
68 canid_t calc_id(int testcase)
80 canid_t calc_mask(int testcase)
82 canid_t mask = CAN_SFF_MASK;
85 return (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
95 int main(int argc, char **argv)
100 struct sockaddr_can addr;
101 struct can_filter rfilter;
102 struct can_frame frame;
106 int rxbits, rxbitval;
108 int recv_own_msgs = 1;
111 /* check command line options */
113 fprintf(stderr, "Usage: %s <device>\n", argv[0]);
117 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
122 strcpy(ifr.ifr_name, argv[1]);
123 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
124 perror("SIOCGIFINDEX");
127 addr.can_family = AF_CAN;
128 addr.can_ifindex = ifr.ifr_ifindex;
130 setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
131 &recv_own_msgs, sizeof(recv_own_msgs));
133 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
140 for (testcase = 0; testcase < TC; testcase++) {
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));
147 printf("testcase %2d filters : can_id = 0x%08X can_mask = 0x%08X\n",
148 testcase, rfilter.can_id, rfilter.can_mask);
150 printf("testcase %2d sending patterns ... ", testcase);
153 frame.data[0] = testcase;
156 if (write(s, &frame, sizeof(frame)) < 0) {
160 frame.can_id = (ID | CAN_RTR_FLAG);
161 if (write(s, &frame, sizeof(frame)) < 0) {
165 frame.can_id = (ID | CAN_EFF_FLAG);
166 if (write(s, &frame, sizeof(frame)) < 0) {
170 frame.can_id = (ID | CAN_EFF_FLAG | CAN_RTR_FLAG);
171 if (write(s, &frame, sizeof(frame)) < 0) {
188 tv.tv_usec = 50000; /* 50ms timeout */
190 ret = select(s+1, &rdfs, NULL, NULL, &tv);
196 if (FD_ISSET(s, &rdfs)) {
198 ret = read(s, &frame, sizeof(struct can_frame));
203 if ((frame.can_id & CAN_SFF_MASK) != ID) {
204 fprintf(stderr, "received wrong can_id!\n");
207 if (frame.data[0] != testcase) {
208 fprintf(stderr, "received wrong testcase!\n");
212 /* test & calc rxbits */
213 rxbitval = 1 << ((frame.can_id & (CAN_EFF_FLAG|CAN_RTR_FLAG|CAN_ERR_FLAG)) >> 28);
215 /* only receive a rxbitval once */
216 if ((rxbits & rxbitval) == rxbitval) {
217 fprintf(stderr, "received rxbitval %d twice!\n", rxbitval);
223 printf("testcase %2d rx : can_id = 0x%08X rx = %d rxbits = %d\n",
224 testcase, frame.can_id, rx, rxbits);
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]);
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]);
238 printf("testcase %2d ok\n---\n", testcase);