8 * Test programm that implements a socket server which understands ASCII
9 * messages for simple broadcast manager frame send commands.
11 * < interface command ival_s ival_us can_id can_dlc [data]* >
13 * The commands are 'A'dd, '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 * Delete the cyclic send job from above
26 * < vcan1 D 0 0 123 0 >
28 * Send a single CAN frame without cyclic transmission
29 * < can0 S 0 0 123 0 >
31 * When the socket is closed the cyclic transmissions are terminated.
34 * Andre Naujoks (the socket server stuff)
35 * Oliver Hartkopp (the rest)
37 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
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.
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.
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.
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
73 * Send feedback to <socketcan-users@lists.berlios.de>
83 #include <sys/types.h>
84 #include <sys/socket.h>
85 #include <sys/ioctl.h>
88 #include <netinet/in.h>
90 #include <linux/can.h>
91 #include <linux/can/bcm.h>
93 void readmsg(int sock, char *buf, int maxlen) {
97 while (read(sock, buf+ptr, 1) == 1) {
100 if (*(buf+ptr) == '>') {
104 if (++ptr > maxlen-2)
108 if (*(buf+ptr) == '<')
116 int main(int argc, char **argv)
120 struct sockaddr_in saddr, clientaddr;
121 struct sockaddr_can caddr;
123 socklen_t sin_size = sizeof(clientaddr);
128 struct bcm_msg_head msg_head;
129 struct can_frame frame;
132 if((sl = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
133 perror("inetsocket");
137 saddr.sin_family = AF_INET;
138 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
139 saddr.sin_port = htons(28600);
141 while(bind(sl,(struct sockaddr*)&saddr, sizeof(saddr)) < 0) {
142 printf(".");fflush(NULL);
146 if (listen(sl,3) != 0) {
152 sa = accept(sl,(struct sockaddr *)&clientaddr, &sin_size);
161 if (errno != EINTR) {
163 * If the cause for the error was NOT the signal from
164 * a dying child, than give an error
172 /* open BCM socket */
174 if ((sc = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
179 caddr.can_family = PF_CAN;
180 caddr.can_ifindex = 0; /* any device => need for sendto() */
182 if (connect(sc, (struct sockaddr *)&caddr, sizeof(caddr)) < 0) {
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;
198 readmsg(sa, buf, sizeof(buf));
200 // printf("read '%s'\n", buf);
202 items = sscanf(buf, "< %6s %c %lu %lu %x %hhu "
203 "%hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx >",
206 &msg.msg_head.ival2.tv_sec,
207 &msg.msg_head.ival2.tv_usec,
208 &msg.msg_head.can_id,
221 if (msg.frame.can_dlc > 8)
223 if (items != 6 + msg.frame.can_dlc)
226 msg.frame.can_id = msg.msg_head.can_id;
230 msg.msg_head.opcode = TX_SEND;
233 msg.msg_head.opcode = TX_SETUP;
234 msg.msg_head.flags |= SETTIMER|STARTTIMER;
237 msg.msg_head.opcode = TX_DELETE;
241 printf("unknown command '%c'.\n", cmd);
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));