]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - test/tst-bcm-server.c
Added BCM receive functions 'R', 'F' and 'X'.
[socketcan-devel.git] / test / tst-bcm-server.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * tst-bcm-server.c
7  *
8  * Test programm that implements a socket server which understands ASCII
9  * messages for simple broadcast manager frame send commands.
10  *
11  * < interface command ival_s ival_us can_id can_dlc [data]* >
12  *
13  * ## TX path:
14  *
15  * The commands are 'A'dd, 'U'pdate, 'D'elete and 'S'end.
16  * e.g.
17  *
18  * Send the CAN frame 123#1122334455667788 every second on vcan1
19  * < vcan1 A 1 0 123 8 11 22 33 44 55 66 77 88 >
20  *
21  * Send the CAN frame 123#1122334455667788 every 10 usecs on vcan1
22  * < vcan1 A 0 10 123 8 11 22 33 44 55 66 77 88 >
23  *
24  * Send the CAN frame 123#42424242 every 20 msecs on vcan1
25  * < vcan1 A 0 20000 123 4 42 42 42 42 >
26  *
27  * Update the CAN frame 123#42424242 with 123#112233 - no change of timers
28  * < vcan1 U 0 0 123 3 11 22 33 >
29  *
30  * Delete the cyclic send job from above
31  * < vcan1 D 0 0 123 0 >
32  *
33  * Send a single CAN frame without cyclic transmission
34  * < can0 S 0 0 123 0 >
35  *
36  * When the socket is closed the cyclic transmissions are terminated.
37  *
38  * ## RX path:
39  *
40  * The commands are 'R'eceive setup, 'F'ilter ID Setup and 'X' for delete.
41  * e.g.
42  *
43  * Receive CAN ID 0x123 from vcan1 and check for changes in the first byte
44  * < vcan1 R 0 0 123 1 FF >
45  *
46  * Receive CAN ID 0x123 from vcan1 and check for changes in given mask
47  * < vcan1 R 0 0 123 8 FF 00 F8 00 00 00 00 00 >
48  *
49  * As above but throttle receive update rate down to 1.5 seconds
50  * < vcan1 R 1 500000 123 8 FF 00 F8 00 00 00 00 00 >
51  *
52  * Filter for CAN ID 0x123 from vcan1 without content filtering
53  * < vcan1 F 0 0 123 0 >
54  *
55  * Delete receive filter ('R' or 'F') for CAN ID 0x123
56  * < vcan1 X 0 0 123 0 >
57  *
58  * ##
59  *
60  * Authors:
61  * Andre Naujoks (the socket server stuff)
62  * Oliver Hartkopp (the rest)
63  *
64  * Copyright (c) 2002-2009 Volkswagen Group Electronic Research
65  * All rights reserved.
66  *
67  * Redistribution and use in source and binary forms, with or without
68  * modification, are permitted provided that the following conditions
69  * are met:
70  * 1. Redistributions of source code must retain the above copyright
71  *    notice, this list of conditions and the following disclaimer.
72  * 2. Redistributions in binary form must reproduce the above copyright
73  *    notice, this list of conditions and the following disclaimer in the
74  *    documentation and/or other materials provided with the distribution.
75  * 3. Neither the name of Volkswagen nor the names of its contributors
76  *    may be used to endorse or promote products derived from this software
77  *    without specific prior written permission.
78  *
79  * Alternatively, provided that this notice is retained in full, this
80  * software may be distributed under the terms of the GNU General
81  * Public License ("GPL") version 2, in which case the provisions of the
82  * GPL apply INSTEAD OF those given above.
83  *
84  * The provided data structures and external interfaces from this code
85  * are not restricted to be used by modules with a GPL compatible license.
86  *
87  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
88  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
89  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
90  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
91  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
92  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
93  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
94  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
95  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
96  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
97  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
98  * DAMAGE.
99  *
100  * Send feedback to <socketcan-users@lists.berlios.de>
101  *
102  */
103
104 #include <stdio.h>
105 #include <stdlib.h>
106 #include <unistd.h>
107 #include <string.h>
108 #include <errno.h>
109
110 #include <sys/types.h>
111 #include <sys/socket.h>
112 #include <sys/ioctl.h>
113 #include <sys/uio.h>
114 #include <net/if.h>
115 #include <netinet/in.h>
116
117 #include <linux/can.h>
118 #include <linux/can/bcm.h>
119
120 #define MAXLEN 100
121 #define PORT 28600
122
123 int main(int argc, char **argv)
124 {
125
126         int sl, sa, sc;
127         int i, ret;
128         int idx = 0;
129         struct sockaddr_in  saddr, clientaddr;
130         struct sockaddr_can caddr;
131         socklen_t caddrlen = sizeof(caddr);
132         struct ifreq ifr;
133         fd_set readfds;
134         socklen_t sin_size = sizeof(clientaddr);
135
136         char buf[MAXLEN];
137         char rxmsg[50];
138
139         struct {
140                 struct bcm_msg_head msg_head;
141                 struct can_frame frame;
142         } msg;
143
144         if((sl = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
145                 perror("inetsocket");
146                 exit(1);
147         }
148
149         saddr.sin_family = AF_INET;
150         saddr.sin_addr.s_addr = htonl(INADDR_ANY);
151         saddr.sin_port = htons(PORT);
152
153         while(bind(sl,(struct sockaddr*)&saddr, sizeof(saddr)) < 0) {
154                 printf(".");fflush(NULL);
155                 usleep(100000);
156         }
157
158         if (listen(sl,3) != 0) {
159                 perror("listen");
160                 exit(1);
161         }
162
163         while (1) { 
164                 sa = accept(sl,(struct sockaddr *)&clientaddr, &sin_size);
165                 if (sa > 0 ){
166
167                         if (fork())
168                                 close(sa);
169                         else
170                                 break;
171                 }
172                 else {
173                         if (errno != EINTR) {
174                                 /*
175                                  * If the cause for the error was NOT the
176                                  * signal from a dying child => give an error
177                                  */
178                                 perror("accept");
179                                 exit(1);
180                         }
181                 }
182         }
183
184         /* open BCM socket */
185
186         if ((sc = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
187                 perror("bcmsocket");
188                 return 1;
189         }
190
191         memset(&caddr, 0, sizeof(caddr));
192         caddr.can_family = PF_CAN;
193         /* can_ifindex is set to 0 (any device) => need for sendto() */
194
195         if (connect(sc, (struct sockaddr *)&caddr, sizeof(caddr)) < 0) {
196                 perror("connect");
197                 return 1;
198         }
199
200         while (1) {
201
202                 FD_ZERO(&readfds);
203                 FD_SET(sc, &readfds);
204                 FD_SET(sa, &readfds);
205
206                 ret = select((sc > sa)?sc+1:sa+1, &readfds, NULL, NULL, NULL);
207
208                 if (FD_ISSET(sc, &readfds)) {
209
210                         ret = recvfrom(sc, &msg, sizeof(msg), 0,
211                                        (struct sockaddr*)&caddr, &caddrlen);
212
213                         ifr.ifr_ifindex = caddr.can_ifindex;
214                         ioctl(sc, SIOCGIFNAME, &ifr);
215
216                         sprintf(rxmsg, "< %s %03X %d ", ifr.ifr_name,
217                                 msg.msg_head.can_id, msg.frame.can_dlc);
218
219                         for ( i = 0; i < msg.frame.can_dlc; i++)
220                                 sprintf(rxmsg + strlen(rxmsg), "%02X ",
221                                         msg.frame.data[i]);
222
223                         /* delimiter '\0' for Adobe(TM) Flash(TM) XML sockets */
224                         strcat(rxmsg, ">\0");
225
226                         send(sa, rxmsg, strlen(rxmsg) + 1, 0);
227                 }
228
229
230                 if (FD_ISSET(sa, &readfds)) {
231
232                         char cmd;
233                         int items;
234
235                         if (read(sa, buf+idx, 1) != 1)
236                                 continue;
237
238                         if (!idx) {
239                                 if (buf[0] == '<')
240                                         idx = 1;
241
242                                 continue;
243                         }
244
245                         if (idx > MAXLEN-2) {
246                                 idx = 0;
247                                 continue;
248                         }
249
250                         if (buf[idx] != '>') {
251                                 idx++;
252                                 continue;
253                         }
254
255                         buf[idx+1] = 0;
256                         idx = 0;
257
258                         //printf("read '%s'\n", buf);
259
260                         /* prepare bcm message settings */
261                         memset(&msg, 0, sizeof(msg));
262                         msg.msg_head.nframes = 1;
263
264                         items = sscanf(buf, "< %6s %c %lu %lu %x %hhu "
265                                        "%hhx %hhx %hhx %hhx %hhx %hhx "
266                                        "%hhx %hhx >",
267                                        ifr.ifr_name,
268                                        &cmd, 
269                                        &msg.msg_head.ival2.tv_sec,
270                                        &msg.msg_head.ival2.tv_usec,
271                                        &msg.msg_head.can_id,
272                                        &msg.frame.can_dlc,
273                                        &msg.frame.data[0],
274                                        &msg.frame.data[1],
275                                        &msg.frame.data[2],
276                                        &msg.frame.data[3],
277                                        &msg.frame.data[4],
278                                        &msg.frame.data[5],
279                                        &msg.frame.data[6],
280                                        &msg.frame.data[7]);
281
282                         if (items < 6)
283                                 break;
284                         if (msg.frame.can_dlc > 8)
285                                 break;
286                         if (items != 6 + msg.frame.can_dlc)
287                                 break;
288
289                         msg.frame.can_id = msg.msg_head.can_id;
290
291                         switch (cmd) {
292                         case 'S':
293                                 msg.msg_head.opcode = TX_SEND;
294                                 break;
295                         case 'A':
296                                 msg.msg_head.opcode = TX_SETUP;
297                                 msg.msg_head.flags |= SETTIMER | STARTTIMER;
298                                 break;
299                         case 'U':
300                                 msg.msg_head.opcode = TX_SETUP;
301                                 msg.msg_head.flags  = 0;
302                                 break;
303                         case 'D':
304                                 msg.msg_head.opcode = TX_DELETE;
305                                 break;
306
307                         case 'R':
308                                 msg.msg_head.opcode = RX_SETUP;
309                                 msg.msg_head.flags  = SETTIMER;
310                                 break;
311                         case 'F':
312                                 msg.msg_head.opcode = RX_SETUP;
313                                 msg.msg_head.flags  = RX_FILTER_ID | SETTIMER;
314                                 break;
315                         case 'X':
316                                 msg.msg_head.opcode = RX_DELETE;
317                                 break;
318                         default:
319                                 printf("unknown command '%c'.\n", cmd);
320                                 exit(1);
321                         }
322
323                         if (!ioctl(sc, SIOCGIFINDEX, &ifr)) {
324                                 caddr.can_ifindex = ifr.ifr_ifindex;
325                                 sendto(sc, &msg, sizeof(msg), 0,
326                                        (struct sockaddr*)&caddr, sizeof(caddr));
327                         }
328                 }
329         }
330
331         close(sc);
332         close(sa);
333
334         return 0;
335 }