]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - test/tst-rcv-own-msgs.c
Add kernel version depency for Kernel 3.1.x which extended __rtnl_register().
[socketcan-devel.git] / test / tst-rcv-own-msgs.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * tst-rcv-own-msgs.c
7  *
8  * Copyright (c) 2010 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
63 #define max(a,b) (a > b ? a : b)
64
65 struct rxs {
66         int s;
67         int t;
68 };
69
70 struct rxs test_sockets(int s, int t, canid_t can_id)
71 {
72         fd_set rdfs;
73         struct timeval tv;
74         int m = max(s,t)+1;
75         int have_rx = 1;
76         struct can_frame frame;
77         struct rxs rx;
78         int ret;
79
80         frame.can_id = can_id;
81         frame.can_dlc = 0;
82         if (write(s, &frame, sizeof(frame)) < 0) {
83                 perror("write");
84                 exit(1);
85         }
86
87         rx.s = rx.t = 0;
88
89         while (have_rx) {
90
91                 FD_ZERO(&rdfs);
92                 FD_SET(s, &rdfs);
93                 FD_SET(t, &rdfs);
94                 tv.tv_sec = 0;
95                 tv.tv_usec = 50000; /* 50ms timeout */
96                 have_rx = 0;
97
98                 ret = select(m, &rdfs, NULL, NULL, &tv);
99                 if (ret < 0) {
100                         perror("select");
101                         exit(1);
102                 }
103
104                 if (FD_ISSET(s, &rdfs)) {
105
106                         have_rx = 1;
107                         ret = read(s, &frame, sizeof(struct can_frame));
108                         if (ret < 0) {
109                                 perror("read");
110                                 exit(1);
111                         }
112                         if (frame.can_id != can_id) {
113                                 fprintf(stderr, "received wrong can_id!\n");
114                                 exit(1);
115                         }
116                         rx.s++;
117                 }
118
119                 if (FD_ISSET(t, &rdfs)) {
120
121                         have_rx = 1;
122                         ret = read(t, &frame, sizeof(struct can_frame));
123                         if (ret < 0) {
124                                 perror("read");
125                                 exit(1);
126                         }
127                         if (frame.can_id != can_id) {
128                                 fprintf(stderr, "received wrong can_id!\n");
129                                 exit(1);
130                         }
131                         rx.t++;
132                 }
133         }
134
135         /* timeout */
136
137         return rx;
138 }
139
140 void setopts(int s, int loopback, int recv_own_msgs)
141 {
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));
146
147         printf("check loopback %d recv_own_msgs %d ... ",
148                loopback, recv_own_msgs);
149 }
150
151
152 int main(int argc, char **argv)
153 {
154         int s, t;
155         struct sockaddr_can addr;
156         struct ifreq ifr;
157         struct rxs rx;
158
159         /* check command line options */
160         if (argc != 2) {
161                 fprintf(stderr, "Usage: %s <device>\n", argv[0]);
162                 return 1;
163         }
164
165         if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
166                 perror("socket");
167                 return 1;
168         }
169         if ((t = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
170                 perror("socket");
171                 return 1;
172         }
173
174         strcpy(ifr.ifr_name, argv[1]);
175         if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
176                 perror("SIOCGIFINDEX");
177                 return 1;
178         }
179         addr.can_ifindex = ifr.ifr_ifindex;
180         addr.can_family = AF_CAN;
181
182         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
183                 perror("bind");
184                 return 1;
185         }
186         if (bind(t, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
187                 perror("bind");
188                 return 1;
189         }
190
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)
195                 printf("ok.\n");
196         else {
197                 printf("failure!\n");
198                 return 1;
199         }
200
201         /* check loopback 0 recv_own_msgs 0 */
202         setopts(s, 0, 0);
203         rx = test_sockets(s, t, 0x341);
204         if (rx.s == 0 && rx.t == 0)
205                 printf("ok.\n");
206         else {
207                 printf("failure!\n");
208                 return 1;
209         }
210
211         /* check loopback 0 recv_own_msgs 1 */
212         setopts(s, 0, 1);
213         rx = test_sockets(s, t, 0x342);
214         if (rx.s == 0 && rx.t == 0)
215                 printf("ok.\n");
216         else {
217                 printf("failure!\n");
218                 return 1;
219         }
220
221         /* check loopback 1 recv_own_msgs 0 */
222         setopts(s, 1, 0);
223         rx = test_sockets(s, t, 0x343);
224         if (rx.s == 0 && rx.t == 1)
225                 printf("ok.\n");
226         else {
227                 printf("failure!\n");
228                 return 1;
229         }
230
231         /* check loopback 1 recv_own_msgs 1 */
232         setopts(s, 1, 1);
233         rx = test_sockets(s, t, 0x344);
234         if (rx.s == 1 && rx.t == 1)
235                 printf("ok.\n");
236         else {
237                 printf("failure!\n");
238                 return 1;
239         }
240
241         printf("PF_CAN frame flow test was successful.\n");
242
243         close(s);
244         close(t);
245
246         return 0;
247 }