]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - test/tst-bcm-server.c
Fix data sizes for write() because the BCM looks on correct length now.
[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  * The commands are 'A'dd, 'D'elete and 'S'end.
14  * e.g.
15  *
16  * Send the CAN frame 123#1122334455667788 every second on vcan1
17  * < vcan1 A 1 0 123 8 11 22 33 44 55 66 77 88 >
18  *
19  * Send the CAN frame 123#1122334455667788 every 10 usecs on vcan1
20  * < vcan1 A 0 10 123 8 11 22 33 44 55 66 77 88 >
21  *
22  * Send the CAN frame 123#42424242 every 20 msecs on vcan1
23  * < vcan1 A 0 20000 123 4 42 42 42 42 >
24  *
25  * Delete the cyclic send job from above
26  * < vcan1 D 0 0 123 0 >
27  *
28  * Send a single CAN frame without cyclic transmission
29  * < can0 S 0 0 123 0 >
30  *
31  * When the socket is closed the cyclic transmissions are terminated.
32  *
33  * Authors:
34  * Andre Naujoks (the socket server stuff)
35  * Oliver Hartkopp (the rest)
36  *
37  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
38  * All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. Neither the name of Volkswagen nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * Alternatively, provided that this notice is retained in full, this
53  * software may be distributed under the terms of the GNU General
54  * Public License ("GPL") version 2, in which case the provisions of the
55  * GPL apply INSTEAD OF those given above.
56  *
57  * The provided data structures and external interfaces from this code
58  * are not restricted to be used by modules with a GPL compatible license.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
61  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
62  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
63  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
64  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
65  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
66  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
67  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
68  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
69  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
70  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
71  * DAMAGE.
72  *
73  * Send feedback to <socketcan-users@lists.berlios.de>
74  *
75  */
76
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <unistd.h>
80 #include <string.h>
81 #include <errno.h>
82
83 #include <sys/types.h>
84 #include <sys/socket.h>
85 #include <sys/ioctl.h>
86 #include <sys/uio.h>
87 #include <net/if.h>
88 #include <netinet/in.h>
89
90 #include <linux/can.h>
91 #include <linux/can/bcm.h>
92
93 void readmsg(int sock, char *buf, int maxlen) {
94
95         int ptr = 0;
96
97         while (read(sock, buf+ptr, 1) == 1) {
98
99                 if (ptr) {
100                         if (*(buf+ptr) == '>') {
101                                 *(buf+ptr+1) = 0;
102                                 return;
103                         }
104                         if (++ptr > maxlen-2)
105                                 ptr = 0;
106                 }
107                 else
108                         if (*(buf+ptr) == '<')
109                                 ptr++;
110         }
111
112         *buf = 0;
113 }
114
115
116 int main(int argc, char **argv)
117 {
118
119         int sl, sa, sc;
120         struct sockaddr_in  saddr, clientaddr;
121         struct sockaddr_can caddr;
122         struct ifreq ifr;
123         socklen_t sin_size = sizeof(clientaddr);
124
125         char buf[100];
126
127         struct {
128                 struct bcm_msg_head msg_head;
129                 struct can_frame frame;
130         } msg;
131
132         if((sl = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
133                 perror("inetsocket");
134                 exit(1);
135         }
136
137         saddr.sin_family = AF_INET;
138         saddr.sin_addr.s_addr = htonl(INADDR_ANY);
139         saddr.sin_port = htons(28600);
140
141         while(bind(sl,(struct sockaddr*)&saddr, sizeof(saddr)) < 0) {
142                 printf(".");fflush(NULL);
143                 usleep(100000);
144         }
145
146         if (listen(sl,3) != 0) {
147                 perror("listen");
148                 exit(1);
149         }
150
151         while (1) { 
152                 sa = accept(sl,(struct sockaddr *)&clientaddr, &sin_size);
153                 if (sa > 0 ){
154
155                         if (fork())
156                                 close(sa);
157                         else
158                                 break;
159                 }
160                 else {
161                         if (errno != EINTR) {
162                                 /*
163                                  * If the cause for the error was NOT the signal from
164                                  * a dying child, than give an error
165                                  */
166                                 perror("accept");
167                                 exit(1);
168                         }
169                 }
170         }
171
172         /* open BCM socket */
173
174         if ((sc = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
175                 perror("bcmsocket");
176                 return 1;
177         }
178
179         caddr.can_family = PF_CAN;
180         caddr.can_ifindex = 0; /* any device => need for sendto() */
181
182         if (connect(sc, (struct sockaddr *)&caddr, sizeof(caddr)) < 0) {
183                 perror("connect");
184                 return 1;
185         }
186
187         /* prepare stable settings */
188         msg.msg_head.nframes       = 1;
189         msg.msg_head.count         = 0;
190         msg.msg_head.ival1.tv_sec  = 0;
191         msg.msg_head.ival1.tv_usec = 0;
192
193         while (1) {
194
195                 char cmd;
196                 int items;
197
198                 readmsg(sa, buf, sizeof(buf));
199
200                 // printf("read '%s'\n", buf);
201
202                 items = sscanf(buf, "< %6s %c %lu %lu %x %hhu "
203                                "%hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx >",
204                                ifr.ifr_name,
205                                &cmd, 
206                                &msg.msg_head.ival2.tv_sec,
207                                &msg.msg_head.ival2.tv_usec,
208                                &msg.msg_head.can_id,
209                                &msg.frame.can_dlc,
210                                &msg.frame.data[0],
211                                &msg.frame.data[1],
212                                &msg.frame.data[2],
213                                &msg.frame.data[3],
214                                &msg.frame.data[4],
215                                &msg.frame.data[5],
216                                &msg.frame.data[6],
217                                &msg.frame.data[7]);
218
219                 if (items < 6)
220                         break;
221                 if (msg.frame.can_dlc > 8)
222                         break;
223                 if (items != 6 + msg.frame.can_dlc)
224                         break;
225
226                 msg.frame.can_id = msg.msg_head.can_id;
227
228                 switch (cmd) {
229                 case 'S':
230                         msg.msg_head.opcode = TX_SEND;
231                         break;
232                 case 'A':
233                         msg.msg_head.opcode = TX_SETUP;
234                         msg.msg_head.flags |= SETTIMER|STARTTIMER;
235                         break;
236                 case 'D':
237                         msg.msg_head.opcode = TX_DELETE;
238                         break;
239
240                 default:
241                         printf("unknown command '%c'.\n", cmd);
242                         exit(1);
243                 }
244
245                 if (!ioctl(sc, SIOCGIFINDEX, &ifr)) {
246                         caddr.can_ifindex = ifr.ifr_ifindex;
247                         sendto(sc, &msg, sizeof(msg), 0,
248                                (struct sockaddr*)&caddr, sizeof(caddr));
249                 }
250
251         }
252
253         close(sc);
254         close(sa);
255
256         return 0;
257 }