]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - test/tst-bcm-throttle.c
Added missing inclusion of linux/types.h
[socketcan-devel.git] / test / tst-bcm-throttle.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * tst-bcm-throttle.c
7  *
8  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
22  *
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.
27  *
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.
30  *
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
42  * DAMAGE.
43  *
44  * Send feedback to <socketcan-users@lists.berlios.de>
45  *
46  */
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <string.h>
52
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
56 #include <sys/uio.h>
57 #include <net/if.h>
58
59 #include <linux/can.h>
60 #include <linux/can/bcm.h>
61
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)
65 {
66         int s,nbytes;
67         struct sockaddr_can addr;
68         struct ifreq ifr;
69
70         struct {
71                 struct bcm_msg_head msg_head;
72                 struct can_frame frame[3];
73         } txmsg, rxmsg;
74
75         if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
76                 perror("socket");
77                 return 1;
78         }
79
80         addr.can_family = PF_CAN;
81         strcpy(ifr.ifr_name, "vcan2");
82         ioctl(s, SIOCGIFINDEX, &ifr);
83         addr.can_ifindex = ifr.ifr_ifindex;
84
85         if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
86                 perror("connect");
87                 return 1;
88         }
89
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;
98
99         printf("<*>Writing RX_SETUP with RX_FILTER_ID for can_id <%03X>\n",
100                txmsg.msg_head.can_id);
101
102         if (write(s, &txmsg, sizeof(struct bcm_msg_head)) < 0)
103                 perror("write");
104
105         txmsg.msg_head.opcode  = TX_SEND;
106         txmsg.msg_head.nframes = 1;
107         /* obsolete for TX_SEND ... */
108 #if 0
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;
116 #endif
117         txmsg.frame[0].can_id    = 0x43;
118         txmsg.frame[0].can_dlc   = 8;
119         U64_DATA(&txmsg.frame[0]) = (__u64) 0xdeadbeefdeadbeefULL;
120
121         printf("<2>Writing TX_SEND with wrong can_id <%03X>\n",
122                txmsg.frame[0].can_id);
123
124         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
125                 perror("write");
126
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;
132
133         printf("<3>Writing TX_SEND with correct can_id <%03X>\n",
134                txmsg.frame[0].can_id);
135
136         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
137                 perror("write");
138
139         if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
140                 perror("read");
141
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);
147         }
148
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 */
152
153         printf("<*>Writing RX_DELETE for can_id <%03X>\n",
154                txmsg.msg_head.can_id);
155
156         if (write(s, &txmsg, sizeof(struct bcm_msg_head)) < 0)
157                 perror("write");
158
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;
169
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]));
172
173         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
174                 perror("write");
175
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;
181
182         printf("<5>Writing TX_SEND with correct can_id <%03X>\n",
183                txmsg.frame[0].can_id);
184
185         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
186                 perror("write");
187
188         if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
189                 perror("read");
190
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);
196         }
197
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;
203
204         printf("<6>Writing TX_SEND with correct can_id <%03X> ",
205                txmsg.frame[0].can_id);
206         printf("no changed data\n");
207
208         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
209                 perror("write");
210
211         /* no change here */
212
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;
218
219         printf("<7>Writing TX_SEND with correct can_id <%03X> ",
220                txmsg.frame[0].can_id);
221         printf("changed relevant msgbits\n");
222
223         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
224                 perror("write");
225
226         if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
227                 perror("read");
228
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);
234         }
235
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;
241
242         printf("<8>Writing TX_SEND with correct can_id <%03X> ",
243                txmsg.frame[0].can_id);
244         printf("changed irrelevant msgbits\n");
245
246         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
247                 perror("write");
248
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;
254
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");
258
259         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
260                 perror("write");
261
262         if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
263                 perror("read");
264
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);
270         }
271
272         /* no problems ;-) but NOW we try MUX messages ... and timeouts */
273
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 */
277
278         printf("<*>Writing RX_DELETE for can_id <%03X>\n",
279                txmsg.msg_head.can_id);
280
281         if (write(s, &txmsg, sizeof(struct bcm_msg_head)) < 0)
282                 perror("write");
283
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;
295
296         printf("<*>Writing multiplex RX_SETUP for can_id <%03X>\n",
297                txmsg.msg_head.can_id);
298
299         if (write(s, &txmsg, sizeof(txmsg)) < 0)
300                 perror("write");
301
302
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;
308
309         printf("<A>Writing TX_SEND with wrong MUX ID 42\n");
310
311         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
312                 perror("write");
313
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;
319
320         printf("<B>Writing TX_SEND with correct MUX ID 01\n");
321
322         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
323                 perror("write");
324
325         if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
326                 perror("read");
327
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);
333         }
334
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;
340
341         printf("<C>Writing TX_SEND with correct MUX ID 01 but no data change\n");
342
343         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
344                 perror("write");
345
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;
351
352         printf("<D>Writing TX_SEND with correct MUX ID 01 but no relevant data change\n");
353
354         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
355                 perror("write");
356
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;
362
363         printf("<E>Writing TX_SEND with correct MUX ID 01 with relevant data change\n");
364
365         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
366                 perror("write");
367
368         if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
369                 perror("read");
370
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);
376         }
377
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;
383
384         printf("<F>Writing TX_SEND with correct MUX ID 02\n");
385
386         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
387                 perror("write");
388
389         if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
390                 perror("read");
391
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);
397         }
398
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;
404
405         printf("<10>Writing TX_SEND with correct MUX ID 02 with relevant data change\n");
406
407         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
408                 perror("write");
409
410         if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
411                 perror("read");
412
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);
418         }
419
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;
425
426         printf("<11>Writing TX_SEND with correct MUX ID 02 no data change but DLC\n");
427
428         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
429                 perror("write");
430
431         if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
432                 perror("read");
433
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);
439         }
440
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;
446
447         printf("<12>Writing TX_SEND with wrong MUX ID 03\n");
448
449         if (write(s, &txmsg, BCM_1FRAME_LEN) < 0)
450                 perror("write");
451
452         if ((nbytes = read(s, &rxmsg, sizeof(rxmsg))) < 0)
453                 perror("read");
454
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);
460         }
461
462         close(s);
463
464         return 0;
465 }