]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - test/tst-rcv-own-msgs.c
ed96e3bc17a555ed9a0820f2442e90f70a66b21e
[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 void read_sockets(int s, int t)
66 {
67         fd_set rdfs;
68         struct timeval tv;
69         int m = max(s,t)+1;
70         int have_rx = 1;
71         struct can_frame frame;
72         int ret;
73
74         while (have_rx) {
75
76                 FD_ZERO(&rdfs);
77                 FD_SET(s, &rdfs);
78                 FD_SET(t, &rdfs);
79                 tv.tv_sec = 0;
80                 tv.tv_usec = 50000; /* 50ms timeout */
81                 have_rx = 0;
82
83                 if ((ret = select(m, &rdfs, NULL, NULL, &tv)) < 0) {
84                         perror("select");
85                         exit(1);
86                 }
87
88                 if (FD_ISSET(s, &rdfs)) {
89
90                         have_rx = 1;
91                         ret = read(s, &frame, sizeof(struct can_frame));
92                         if (ret < 0) {
93                                 perror("read");
94                                 exit(1);
95                         }
96                         printf (" s : %x\n", frame.can_id);
97                 }
98
99                 if (FD_ISSET(t, &rdfs)) {
100
101                         have_rx = 1;
102                         ret = read(t, &frame, sizeof(struct can_frame));
103                         if (ret < 0) {
104                                 perror("read");
105                                 exit(1);
106                         }
107                         printf (" t : %x\n", frame.can_id);
108                 }
109
110         }
111
112         printf(" timeout\n");
113
114 }
115
116 void write_socket(int s, int val)
117 {
118         static struct can_frame frame;
119
120         frame.can_id = val;
121         if (write(s, &frame, sizeof(frame)) < 0) {
122                 perror("write");
123                 exit(1);
124         }
125 }
126
127
128 void setopts(int s, int loopback, int recv_own_msgs)
129 {
130         setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
131                    &loopback, sizeof(loopback));
132         setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
133                    &recv_own_msgs, sizeof(recv_own_msgs));
134
135         printf("sockopt %c %c\n", (loopback)?'L':'-', (recv_own_msgs)?'R':'-');
136 }
137
138
139 int main(int argc, char **argv)
140 {
141         int s, t;
142         struct sockaddr_can addr;
143         struct ifreq ifr;
144         int ifindex;
145         int i = 0;
146
147
148         if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
149                 perror("socket");
150                 return 1;
151         }
152         if ((t = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
153                 perror("socket");
154                 return 1;
155         }
156
157         strcpy(ifr.ifr_name, "vcan0");
158         ioctl(s, SIOCGIFINDEX, &ifr);
159         ifindex = ifr.ifr_ifindex;
160
161         addr.can_family = AF_CAN;
162         addr.can_ifindex = ifindex;
163
164         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
165                 perror("bind");
166                 return 1;
167         }
168         if (bind(t, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
169                 perror("bind");
170                 return 1;
171         }
172
173
174         /* perform the default settings */
175         printf("sockopt default\n");
176         write_socket(s, i++);
177         read_sockets(s, t);
178
179         setopts(s, 0, 0);
180         write_socket(s, i++);
181         read_sockets(s, t);
182
183         setopts(s, 0, 1);
184         write_socket(s, i++);
185         read_sockets(s, t);
186
187         setopts(s, 1, 0);
188         write_socket(s, i++);
189         read_sockets(s, t);
190
191         setopts(s, 1, 1);
192         write_socket(s, i++);
193         read_sockets(s, t);
194
195         printf("done.\n");
196
197         close(s);
198         close(t);
199
200         return 0;
201 }