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