4 * Test programm that implements a socket server which understands ASCII
5 * messages for simple broadcast manager frame send commands.
7 * < interface command ival_s ival_us can_id can_dlc [data]* >
9 * Only the items 'can_id' and 'data' are given in (ASCII) hexadecimal values.
13 * The commands are 'A'dd, 'U'pdate, 'D'elete and 'S'end.
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 >
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 >
22 * Send the CAN frame 123#42424242 every 20 msecs on vcan1
23 * < vcan1 A 0 20000 123 4 42 42 42 42 >
25 * Update the CAN frame 123#42424242 with 123#112233 - no change of timers
26 * < vcan1 U 0 0 123 3 11 22 33 >
28 * Delete the cyclic send job from above
29 * < vcan1 D 0 0 123 0 >
31 * Send a single CAN frame without cyclic transmission
32 * < can0 S 0 0 123 0 >
34 * When the socket is closed the cyclic transmissions are terminated.
38 * The commands are 'R'eceive setup, 'F'ilter ID Setup and 'X' for delete.
41 * Receive CAN ID 0x123 from vcan1 and check for changes in the first byte
42 * < vcan1 R 0 0 123 1 FF >
44 * Receive CAN ID 0x123 from vcan1 and check for changes in given mask
45 * < vcan1 R 0 0 123 8 FF 00 F8 00 00 00 00 00 >
47 * As above but throttle receive update rate down to 1.5 seconds
48 * < vcan1 R 1 500000 123 8 FF 00 F8 00 00 00 00 00 >
50 * Filter for CAN ID 0x123 from vcan1 without content filtering
51 * < vcan1 F 0 0 123 0 >
53 * Delete receive filter ('R' or 'F') for CAN ID 0x123
54 * < vcan1 X 0 0 123 0 >
56 * CAN messages received by the given filters are send in the format:
57 * < interface can_id can_dlc [data]* >
59 * e.g. when receiving a CAN message from vcan1 with
60 * can_id 0x123 , data length 4 and data 0x11, 0x22, 0x33 and 0x44
62 * < vcan1 123 4 11 22 33 44 >
67 * Andre Naujoks (the socket server stuff)
68 * Oliver Hartkopp (the rest)
70 * Copyright (c) 2002-2009 Volkswagen Group Electronic Research
71 * All rights reserved.
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. Neither the name of Volkswagen nor the names of its contributors
82 * may be used to endorse or promote products derived from this software
83 * without specific prior written permission.
85 * Alternatively, provided that this notice is retained in full, this
86 * software may be distributed under the terms of the GNU General
87 * Public License ("GPL") version 2, in which case the provisions of the
88 * GPL apply INSTEAD OF those given above.
90 * The provided data structures and external interfaces from this code
91 * are not restricted to be used by modules with a GPL compatible license.
93 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
94 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
95 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
96 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
97 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
98 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
99 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
100 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
101 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
102 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
103 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
106 * Send feedback to <linux-can@vger.kernel.org>
117 #include <sys/types.h>
118 #include <sys/wait.h>
119 #include <sys/socket.h>
120 #include <sys/ioctl.h>
123 #include <netinet/in.h>
125 #include <linux/can.h>
126 #include <linux/can/bcm.h>
132 void childdied(int i)
137 int main(int argc, char **argv)
143 struct sockaddr_in saddr, clientaddr;
144 struct sockaddr_can caddr;
145 socklen_t caddrlen = sizeof(caddr);
148 socklen_t sin_size = sizeof(clientaddr);
149 struct sigaction signalaction;
153 char format[FORMATSZ];
157 struct bcm_msg_head msg_head;
158 struct can_frame frame;
161 if (snprintf(format, FORMATSZ, "< %%%ds %%c %%lu %%lu %%x %%hhu "
162 "%%hhx %%hhx %%hhx %%hhx %%hhx %%hhx "
163 "%%hhx %%hhx >", IFNAMSIZ-1) >= FORMATSZ-1)
166 sigemptyset(&sigset);
167 signalaction.sa_handler = &childdied;
168 signalaction.sa_mask = sigset;
169 signalaction.sa_flags = 0;
170 sigaction(SIGCHLD, &signalaction, NULL); /* signal for dying child */
172 if((sl = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
173 perror("inetsocket");
177 saddr.sin_family = AF_INET;
178 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
179 saddr.sin_port = htons(PORT);
181 while(bind(sl,(struct sockaddr*)&saddr, sizeof(saddr)) < 0) {
182 printf(".");fflush(NULL);
186 if (listen(sl,3) != 0) {
192 sa = accept(sl,(struct sockaddr *)&clientaddr, &sin_size);
201 if (errno != EINTR) {
203 * If the cause for the error was NOT the
204 * signal from a dying child => give an error
212 /* open BCM socket */
214 if ((sc = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
219 memset(&caddr, 0, sizeof(caddr));
220 caddr.can_family = PF_CAN;
221 /* can_ifindex is set to 0 (any device) => need for sendto() */
223 if (connect(sc, (struct sockaddr *)&caddr, sizeof(caddr)) < 0) {
231 FD_SET(sc, &readfds);
232 FD_SET(sa, &readfds);
234 select((sc > sa)?sc+1:sa+1, &readfds, NULL, NULL, NULL);
236 if (FD_ISSET(sc, &readfds)) {
238 recvfrom(sc, &msg, sizeof(msg), 0,
239 (struct sockaddr*)&caddr, &caddrlen);
241 ifr.ifr_ifindex = caddr.can_ifindex;
242 ioctl(sc, SIOCGIFNAME, &ifr);
244 sprintf(rxmsg, "< %s %03X %d ", ifr.ifr_name,
245 msg.msg_head.can_id, msg.frame.can_dlc);
247 for ( i = 0; i < msg.frame.can_dlc; i++)
248 sprintf(rxmsg + strlen(rxmsg), "%02X ",
251 /* delimiter '\0' for Adobe(TM) Flash(TM) XML sockets */
252 strcat(rxmsg, ">\0");
254 send(sa, rxmsg, strlen(rxmsg) + 1, 0);
258 if (FD_ISSET(sa, &readfds)) {
263 if (read(sa, buf+idx, 1) < 1)
273 if (idx > MAXLEN-2) {
278 if (buf[idx] != '>') {
286 //printf("read '%s'\n", buf);
288 /* prepare bcm message settings */
289 memset(&msg, 0, sizeof(msg));
290 msg.msg_head.nframes = 1;
292 items = sscanf(buf, format,
295 &msg.msg_head.ival2.tv_sec,
296 &msg.msg_head.ival2.tv_usec,
297 &msg.msg_head.can_id,
310 if (msg.frame.can_dlc > 8)
312 if (items != 6 + msg.frame.can_dlc)
315 msg.frame.can_id = msg.msg_head.can_id;
319 msg.msg_head.opcode = TX_SEND;
322 msg.msg_head.opcode = TX_SETUP;
323 msg.msg_head.flags |= SETTIMER | STARTTIMER;
326 msg.msg_head.opcode = TX_SETUP;
327 msg.msg_head.flags = 0;
330 msg.msg_head.opcode = TX_DELETE;
334 msg.msg_head.opcode = RX_SETUP;
335 msg.msg_head.flags = SETTIMER;
338 msg.msg_head.opcode = RX_SETUP;
339 msg.msg_head.flags = RX_FILTER_ID | SETTIMER;
342 msg.msg_head.opcode = RX_DELETE;
345 printf("unknown command '%c'.\n", cmd);
349 if (!ioctl(sc, SIOCGIFINDEX, &ifr)) {
350 caddr.can_ifindex = ifr.ifr_ifindex;
351 sendto(sc, &msg, sizeof(msg), 0,
352 (struct sockaddr*)&caddr, sizeof(caddr));