8 * Copyright (c) 2010 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 max(a,b) (a > b ? a : b)
70 struct rxs test_sockets(int s, int t, canid_t can_id)
76 struct can_frame frame;
80 frame.can_id = can_id;
82 if (write(s, &frame, sizeof(frame)) < 0) {
95 tv.tv_usec = 50000; /* 50ms timeout */
98 ret = select(m, &rdfs, NULL, NULL, &tv);
104 if (FD_ISSET(s, &rdfs)) {
107 ret = read(s, &frame, sizeof(struct can_frame));
112 if (frame.can_id != can_id) {
113 fprintf(stderr, "received wrong can_id!\n");
119 if (FD_ISSET(t, &rdfs)) {
122 ret = read(t, &frame, sizeof(struct can_frame));
127 if (frame.can_id != can_id) {
128 fprintf(stderr, "received wrong can_id!\n");
140 void setopts(int s, int loopback, int recv_own_msgs)
142 setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
143 &loopback, sizeof(loopback));
144 setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
145 &recv_own_msgs, sizeof(recv_own_msgs));
147 printf("check loopback %d recv_own_msgs %d ... ",
148 loopback, recv_own_msgs);
152 int main(int argc, char **argv)
155 struct sockaddr_can addr;
159 /* check command line options */
161 fprintf(stderr, "Usage: %s <device>\n", argv[0]);
165 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
169 if ((t = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
174 strcpy(ifr.ifr_name, argv[1]);
175 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
176 perror("SIOCGIFINDEX");
179 addr.can_ifindex = ifr.ifr_ifindex;
180 addr.can_family = AF_CAN;
182 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
186 if (bind(t, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
191 printf("Starting PF_CAN frame flow test.\n");
192 printf("checking socket default settings ... ");
193 rx = test_sockets(s, t, 0x340);
194 if (rx.s == 0 && rx.t == 1)
197 printf("failure!\n");
201 /* check loopback 0 recv_own_msgs 0 */
203 rx = test_sockets(s, t, 0x341);
204 if (rx.s == 0 && rx.t == 0)
207 printf("failure!\n");
211 /* check loopback 0 recv_own_msgs 1 */
213 rx = test_sockets(s, t, 0x342);
214 if (rx.s == 0 && rx.t == 0)
217 printf("failure!\n");
221 /* check loopback 1 recv_own_msgs 0 */
223 rx = test_sockets(s, t, 0x343);
224 if (rx.s == 0 && rx.t == 1)
227 printf("failure!\n");
231 /* check loopback 1 recv_own_msgs 1 */
233 rx = test_sockets(s, t, 0x344);
234 if (rx.s == 1 && rx.t == 1)
237 printf("failure!\n");
241 printf("PF_CAN frame flow test was successful.\n");