8 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of Volkswagen nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * Alternatively, provided that this notice is retained in full, this
24 * software may be distributed under the terms of the GNU General
25 * Public License ("GPL") version 2, in which case the provisions of the
26 * GPL apply INSTEAD OF those given above.
28 * The provided data structures and external interfaces from this code
29 * are not restricted to be used by modules with a GPL compatible license.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
44 * Send feedback to <socketcan-users@lists.berlios.de>
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
59 #include <linux/can.h>
60 #include <linux/can/bcm.h>
62 #define U64_DATA(p) (*(unsigned long long*)(p)->data)
63 #define BCM_1FRAME_LEN (sizeof(struct bcm_msg_head) + sizeof(struct can_frame))
64 int main(int argc, char **argv)
67 struct sockaddr_can addr;
71 struct bcm_msg_head msg_head;
72 struct can_frame frame[3];
75 if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
80 addr.can_family = PF_CAN;
81 strcpy(ifr.ifr_name, "vcan2");
82 ioctl(s, SIOCGIFINDEX, &ifr);
83 addr.can_ifindex = ifr.ifr_ifindex;
85 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
90 txmsg.msg_head.opcode = RX_SETUP;
91 txmsg.msg_head.can_id = 0x042;
92 txmsg.msg_head.flags = SETTIMER|RX_FILTER_ID;
93 txmsg.msg_head.ival1.tv_sec = 4;
94 txmsg.msg_head.ival1.tv_usec = 0;
95 txmsg.msg_head.ival2.tv_sec = 2;
96 txmsg.msg_head.ival2.tv_usec = 0;
97 txmsg.msg_head.nframes = 0;
99 printf("<*>Writing RX_SETUP with RX_FILTER_ID for can_id <%03X>\n",
100 txmsg.msg_head.can_id);
102 if (write(s, &txmsg, sizeof(struct bcm_msg_head)) < 0)
105 txmsg.msg_head.opcode = TX_SEND;
106 txmsg.msg_head.nframes = 1;
107 /* obsolete for TX_SEND ... */
109 txmsg.msg_head.can_id = 0x43;
110 txmsg.msg_head.flags = SETTIMER|STARTTIMER|TX_CP_CAN_ID;
111 txmsg.msg_head.count = 0;
112 txmsg.msg_head.ival1.tv_sec = 0;
113 txmsg.msg_head.ival1.tv_usec = 0;
114 txmsg.msg_head.ival2.tv_sec = 0;
115 txmsg.msg_head.ival2.tv_usec = 0;
117 txmsg.frame[0].can_id = 0x43;
118 txmsg.frame[0].can_dlc = 8;
119 U64_DATA(&txmsg.frame[0]) = (__u64) 0xdeadbeefdeadbeefULL;
121 printf("<2>Writing TX_SEND with wrong can_id <%03X>\n",
122 txmsg.frame[0].can_id);
124 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
127 txmsg.msg_head.opcode = TX_SEND;
128 txmsg.msg_head.nframes = 1;
129 txmsg.frame[0].can_id = 0x42;
130 txmsg.frame[0].can_dlc = 8;
131 U64_DATA(&txmsg.frame[0]) = (__u64) 0xdeadbeefdeadbeefULL;
133 printf("<3>Writing TX_SEND with correct can_id <%03X>\n",
134 txmsg.frame[0].can_id);
136 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
139 if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
142 if (rxmsg.msg_head.opcode == RX_CHANGED &&
143 nbytes == BCM_1FRAME_LEN &&
144 rxmsg.msg_head.can_id == 0x42 && rxmsg.frame[0].can_id == 0x42) {
145 printf("<3>Received correct RX_CHANGED message for can_id <%03X> >> OK!\n",
146 rxmsg.frame[0].can_id);
149 /* growing number of nframes => RX_DELETE instead of simple update */
150 txmsg.msg_head.opcode = RX_DELETE;
151 txmsg.msg_head.can_id = 0x042; /* everything we need for RX_DELETE */
153 printf("<*>Writing RX_DELETE for can_id <%03X>\n",
154 txmsg.msg_head.can_id);
156 if (write(s, &txmsg, sizeof(struct bcm_msg_head)) < 0)
159 txmsg.msg_head.opcode = RX_SETUP;
160 txmsg.msg_head.can_id = 0x042;
161 txmsg.msg_head.flags = SETTIMER|RX_CHECK_DLC;
162 txmsg.msg_head.ival1.tv_sec = 4;
163 txmsg.msg_head.ival1.tv_usec = 0;
164 txmsg.msg_head.ival2.tv_sec = 2;
165 txmsg.msg_head.ival2.tv_usec = 0;
166 txmsg.msg_head.nframes = 1;
167 /* txmsg.frame[0].can_dlc = 8; obsolete for RX_SETUP */
168 U64_DATA(&txmsg.frame[0]) = (__u64) 0xFF00000000000000ULL;
170 printf("<*>Writing simple RX_SETUP for can_id <%03X> with msgbits 0x%016llX\n",
171 txmsg.msg_head.can_id, U64_DATA(&txmsg.frame[0]));
173 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
176 txmsg.msg_head.opcode = TX_SEND;
177 txmsg.msg_head.nframes = 1;
178 txmsg.frame[0].can_id = 0x42;
179 txmsg.frame[0].can_dlc = 8;
180 U64_DATA(&txmsg.frame[0]) = (__u64) 0xdeadbeefdeadbeefULL;
182 printf("<5>Writing TX_SEND with correct can_id <%03X>\n",
183 txmsg.frame[0].can_id);
185 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
188 if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
191 if (rxmsg.msg_head.opcode == RX_CHANGED &&
192 nbytes == BCM_1FRAME_LEN &&
193 rxmsg.msg_head.can_id == 0x42 && rxmsg.frame[0].can_id == 0x42) {
194 printf("<5>Received correct RX_CHANGED message for can_id <%03X> >> OK!\n",
195 rxmsg.frame[0].can_id);
198 txmsg.msg_head.opcode = TX_SEND;
199 txmsg.msg_head.nframes = 1;
200 txmsg.frame[0].can_id = 0x42;
201 txmsg.frame[0].can_dlc = 8;
202 U64_DATA(&txmsg.frame[0]) = (__u64) 0xdeadbeefdeadbeefULL;
204 printf("<6>Writing TX_SEND with correct can_id <%03X> ",
205 txmsg.frame[0].can_id);
206 printf("no changed data\n");
208 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
213 txmsg.msg_head.opcode = TX_SEND;
214 txmsg.msg_head.nframes = 1;
215 txmsg.frame[0].can_id = 0x42;
216 txmsg.frame[0].can_dlc = 8;
217 U64_DATA(&txmsg.frame[0]) = (__u64) 0xdeadbeefdeadbeefULL;
219 printf("<7>Writing TX_SEND with correct can_id <%03X> ",
220 txmsg.frame[0].can_id);
221 printf("changed relevant msgbits\n");
223 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
226 if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
229 if (rxmsg.msg_head.opcode == RX_CHANGED &&
230 nbytes == BCM_1FRAME_LEN &&
231 rxmsg.msg_head.can_id == 0x42 && rxmsg.frame[0].can_id == 0x42) {
232 printf("<7>Received correct RX_CHANGED message for can_id <%03X> >> OK!\n",
233 rxmsg.frame[0].can_id);
236 txmsg.msg_head.opcode = TX_SEND;
237 txmsg.msg_head.nframes = 1;
238 txmsg.frame[0].can_id = 0x42;
239 txmsg.frame[0].can_dlc = 8;
240 U64_DATA(&txmsg.frame[0]) = (__u64) 0xdeadbeefdeadbeefULL;
242 printf("<8>Writing TX_SEND with correct can_id <%03X> ",
243 txmsg.frame[0].can_id);
244 printf("changed irrelevant msgbits\n");
246 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
249 txmsg.msg_head.opcode = TX_SEND;
250 txmsg.msg_head.nframes = 1;
251 txmsg.frame[0].can_id = 0x42;
252 txmsg.frame[0].can_dlc = 7;
253 U64_DATA(&txmsg.frame[0]) = (__u64) 0xdeadbeefdeadbeefULL;
255 printf("<9>Writing TX_SEND with correct can_id <%03X> ",
256 txmsg.frame[0].can_id);
257 printf("changed Data Length Code DLC\n");
259 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
262 if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
265 if (rxmsg.msg_head.opcode == RX_CHANGED &&
266 nbytes == BCM_1FRAME_LEN &&
267 rxmsg.msg_head.can_id == 0x42 && rxmsg.frame[0].can_id == 0x42) {
268 printf("<9>Received correct RX_CHANGED message for can_id <%03X> >> OK!\n",
269 rxmsg.frame[0].can_id);
272 /* no problems ;-) but NOW we try MUX messages ... and timeouts */
274 /* growing number of nframes => RX_DELETE instead of simple update */
275 txmsg.msg_head.opcode = RX_DELETE;
276 txmsg.msg_head.can_id = 0x042; /* everything we need for RX_DELETE */
278 printf("<*>Writing RX_DELETE for can_id <%03X>\n",
279 txmsg.msg_head.can_id);
281 if (write(s, &txmsg, sizeof(struct bcm_msg_head)) < 0)
284 txmsg.msg_head.opcode = RX_SETUP;
285 txmsg.msg_head.can_id = 0x042;
286 txmsg.msg_head.flags = SETTIMER|RX_CHECK_DLC;
287 txmsg.msg_head.ival1.tv_sec = 4;
288 txmsg.msg_head.ival1.tv_usec = 0;
289 txmsg.msg_head.ival2.tv_sec = 2;
290 txmsg.msg_head.ival2.tv_usec = 0;
291 txmsg.msg_head.nframes = 3;
292 U64_DATA(&txmsg.frame[0]) = (__u64) 0xFF00000000000000ULL;
293 U64_DATA(&txmsg.frame[1]) = (__u64) 0x01000000000000FFULL;
294 U64_DATA(&txmsg.frame[2]) = (__u64) 0x02000000000000FFULL;
296 printf("<*>Writing multiplex RX_SETUP for can_id <%03X>\n",
297 txmsg.msg_head.can_id);
299 if (write(s, &txmsg, sizeof(txmsg)) < 0)
303 txmsg.msg_head.opcode = TX_SEND;
304 txmsg.msg_head.nframes = 1;
305 txmsg.frame[0].can_id = 0x42;
306 txmsg.frame[0].can_dlc = 8;
307 U64_DATA(&txmsg.frame[0]) = (__u64) 0x4200000000000000ULL;
309 printf("<A>Writing TX_SEND with wrong MUX ID 42\n");
311 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
314 txmsg.msg_head.opcode = TX_SEND;
315 txmsg.msg_head.nframes = 1;
316 txmsg.frame[0].can_id = 0x42;
317 txmsg.frame[0].can_dlc = 8;
318 U64_DATA(&txmsg.frame[0]) = (__u64) 0x0100000000000000ULL;
320 printf("<B>Writing TX_SEND with correct MUX ID 01\n");
322 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
325 if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
328 if (rxmsg.msg_head.opcode == RX_CHANGED &&
329 nbytes == BCM_1FRAME_LEN &&
330 rxmsg.msg_head.can_id == 0x42 && rxmsg.frame[0].can_id == 0x42) {
331 printf("<B>Received correct RX_CHANGED message for can_id <%03X> >> OK!\n",
332 rxmsg.frame[0].can_id);
335 txmsg.msg_head.opcode = TX_SEND;
336 txmsg.msg_head.nframes = 1;
337 txmsg.frame[0].can_id = 0x42;
338 txmsg.frame[0].can_dlc = 8;
339 U64_DATA(&txmsg.frame[0]) = (__u64) 0x0100000000000000ULL;
341 printf("<C>Writing TX_SEND with correct MUX ID 01 but no data change\n");
343 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
346 txmsg.msg_head.opcode = TX_SEND;
347 txmsg.msg_head.nframes = 1;
348 txmsg.frame[0].can_id = 0x42;
349 txmsg.frame[0].can_dlc = 8;
350 U64_DATA(&txmsg.frame[0]) = (__u64) 0x0100001234567800ULL;
352 printf("<D>Writing TX_SEND with correct MUX ID 01 but no relevant data change\n");
354 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
357 txmsg.msg_head.opcode = TX_SEND;
358 txmsg.msg_head.nframes = 1;
359 txmsg.frame[0].can_id = 0x42;
360 txmsg.frame[0].can_dlc = 8;
361 U64_DATA(&txmsg.frame[0]) = (__u64) 0x0100001234567801ULL;
363 printf("<E>Writing TX_SEND with correct MUX ID 01 with relevant data change\n");
365 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
368 if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
371 if (rxmsg.msg_head.opcode == RX_CHANGED &&
372 nbytes == BCM_1FRAME_LEN &&
373 rxmsg.msg_head.can_id == 0x42 && rxmsg.frame[0].can_id == 0x42) {
374 printf("<E>Received correct RX_CHANGED message for can_id <%03X> >> OK!\n",
375 rxmsg.frame[0].can_id);
378 txmsg.msg_head.opcode = TX_SEND;
379 txmsg.msg_head.nframes = 1;
380 txmsg.frame[0].can_id = 0x42;
381 txmsg.frame[0].can_dlc = 8;
382 U64_DATA(&txmsg.frame[0]) = (__u64) 0x0200000000000000ULL;
384 printf("<F>Writing TX_SEND with correct MUX ID 02\n");
386 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
389 if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
392 if (rxmsg.msg_head.opcode == RX_CHANGED &&
393 nbytes == BCM_1FRAME_LEN &&
394 rxmsg.msg_head.can_id == 0x42 && rxmsg.frame[0].can_id == 0x42) {
395 printf("<F>Received correct RX_CHANGED message for can_id <%03X> >> OK!\n",
396 rxmsg.frame[0].can_id);
399 txmsg.msg_head.opcode = TX_SEND;
400 txmsg.msg_head.nframes = 1;
401 txmsg.frame[0].can_id = 0x42;
402 txmsg.frame[0].can_dlc = 8;
403 U64_DATA(&txmsg.frame[0]) = (__u64) 0x0200000000000001ULL;
405 printf("<10>Writing TX_SEND with correct MUX ID 02 with relevant data change\n");
407 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
410 if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
413 if (rxmsg.msg_head.opcode == RX_CHANGED &&
414 nbytes == BCM_1FRAME_LEN &&
415 rxmsg.msg_head.can_id == 0x42 && rxmsg.frame[0].can_id == 0x42) {
416 printf("<10>Received correct RX_CHANGED message for can_id <%03X> >> OK!\n",
417 rxmsg.frame[0].can_id);
420 txmsg.msg_head.opcode = TX_SEND;
421 txmsg.msg_head.nframes = 1;
422 txmsg.frame[0].can_id = 0x42;
423 txmsg.frame[0].can_dlc = 7;
424 U64_DATA(&txmsg.frame[0]) = (__u64) 0x0200000000000001ULL;
426 printf("<11>Writing TX_SEND with correct MUX ID 02 no data change but DLC\n");
428 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
431 if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
434 if (rxmsg.msg_head.opcode == RX_CHANGED &&
435 nbytes == BCM_1FRAME_LEN &&
436 rxmsg.msg_head.can_id == 0x42 && rxmsg.frame[0].can_id == 0x42) {
437 printf("<11>Received correct RX_CHANGED message for can_id <%03X> >> OK!\n",
438 rxmsg.frame[0].can_id);
441 txmsg.msg_head.opcode = TX_SEND;
442 txmsg.msg_head.nframes = 1;
443 txmsg.frame[0].can_id = 0x42;
444 txmsg.frame[0].can_dlc = 7;
445 U64_DATA(&txmsg.frame[0]) = (__u64) 0x0300000000000001ULL;
447 printf("<12>Writing TX_SEND with wrong MUX ID 03\n");
449 if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
452 if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
455 if (rxmsg.msg_head.opcode == RX_TIMEOUT &&
456 nbytes == sizeof(struct bcm_msg_head) &&
457 rxmsg.msg_head.can_id == 0x42) {
458 printf("<-->Received correct RX_TIMEOUT message for can_id <%03X> >> OK!\n",
459 rxmsg.msg_head.can_id);