]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - test/tst-bcm-dump.c
Fix whitespace issues remarked by checkpatch.pl
[socketcan-devel.git] / test / tst-bcm-dump.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * tst-bcm-dump.c
7  *
8  * Copyright (c) 2008 Oliver Hartkopp
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the version 2 of the GNU General Public License
12  * as published by the Free Software Foundation
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  * Send feedback to <socketcan-users@lists.berlios.de>
24  *
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <time.h>
32 #include <libgen.h>
33
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/ioctl.h>
37 #include <sys/time.h>
38 #include <net/if.h>
39
40 #include <linux/can.h>
41 #include <linux/can/bcm.h>
42
43 #define DEFAULT_IFACE "vcan0"
44 #define DEFAULT_CANID 0x42
45
46 void print_usage(char *prg)
47 {
48         fprintf(stderr, "\nUsage: %s [options]\n", prg);
49         fprintf(stderr, "Options: -i <interface> (CAN interface. Default: '%s')\n", DEFAULT_IFACE);
50         fprintf(stderr, "         -c <can_id>    (used CAN ID. Default: 0x%03X)\n", DEFAULT_CANID);
51         fprintf(stderr, "         -o <timeout>   (Timeout value in nsecs. Default: 0)\n");
52         fprintf(stderr, "         -t <throttle>  (Throttle value in nsecs. Default: 0)\n");
53         fprintf(stderr, "         -q <msgs>      (Quit after receiption of #msgs)\n");
54         fprintf(stderr, "         -s             (set STARTTIMER flag. Default: off)\n");
55         fprintf(stderr, "\n");
56 }
57
58 int main(int argc, char **argv)
59 {
60         int s;
61         struct sockaddr_can addr;
62         int nbytes;
63         int i;
64         struct ifreq ifr;
65         char *ifname = DEFAULT_IFACE;
66         canid_t canid = DEFAULT_CANID;
67         int opt;
68         struct timeval tv;
69         unsigned long starttimer = 0;
70         unsigned long long timeout = 0;
71         unsigned long long throttle = 0;
72         unsigned long msgs = 0;
73         struct {
74                 struct bcm_msg_head msg_head;
75                 struct can_frame frame;
76         } msg;
77
78         while ((opt = getopt(argc, argv, "i:c:o:t:q:s")) != -1) {
79                 switch (opt) {
80
81                 case 'i':
82                         ifname = optarg;
83                         break;
84
85                 case 'c':
86                         canid = strtoul(optarg, (char **)NULL, 16);
87                         break;
88
89                 case 'o':
90                         timeout = strtoull(optarg, (char **)NULL, 10);
91                         break;
92
93                 case 't':
94                         throttle = strtoull(optarg, (char **)NULL, 10);
95                         break;
96
97                 case 'q':
98                         msgs = strtoul(optarg, (char **)NULL, 10);
99                         break;
100
101                 case 's':
102                         starttimer = STARTTIMER;
103                         break;
104
105                 case '?':
106                 default:
107                         print_usage(basename(argv[0]));
108                         exit(1);
109                         break;
110                 }
111         }
112
113
114         if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
115                 perror("socket");
116                 return 1;
117         }
118
119         if (strcmp(ifname, "any") == 0)
120                 addr.can_ifindex = 0;
121         else {
122                 strcpy(ifr.ifr_name, ifname);
123                 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
124                         perror("SIOCGIFINDEX");
125                         return 1;
126                 }
127                 addr.can_ifindex = ifr.ifr_ifindex;
128         }
129
130         addr.can_family = PF_CAN;
131
132         if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
133                 perror("connect");
134                 return 1;
135         }
136
137         msg.msg_head.opcode             = RX_SETUP;
138         msg.msg_head.can_id             = canid;
139         msg.msg_head.flags              = SETTIMER|RX_FILTER_ID|starttimer;
140         msg.msg_head.ival1.tv_sec       = timeout / 1000000;
141         msg.msg_head.ival1.tv_usec      = timeout % 1000000;
142         msg.msg_head.ival2.tv_sec       = throttle / 1000000;
143         msg.msg_head.ival2.tv_usec      = throttle % 1000000;
144         msg.msg_head.nframes    = 0;
145
146         gettimeofday(&tv, NULL);
147         printf("[%ld.%06ld] ", tv.tv_sec, tv.tv_usec);
148         printf("Writing RX_SETUP with RX_FILTER_ID for can_id <%03X>\n",
149                msg.msg_head.can_id);
150
151         if (write(s, &msg, sizeof(msg)) < 0)
152                 perror("write");
153
154         while (1) {
155
156                 nbytes = read(s, &msg, sizeof(msg));
157                 if (nbytes < 0) {
158                         perror("read");
159                         return 1;
160                 }
161                 gettimeofday(&tv, NULL);
162                 printf("[%ld.%06ld] ", tv.tv_sec, tv.tv_usec);
163
164                 if (nbytes == sizeof(msg)) {
165
166                         if (ioctl(s, SIOCGSTAMP, &tv) < 0)
167                                 perror("SIOCGSTAMP");
168                         else
169                                 printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
170
171                         if (msg.msg_head.opcode != RX_CHANGED) {
172                                 printf("missing RX_CHANGED.\n");
173                                 return 1;
174                         }
175
176                         printf("RX_CHANGED ");
177
178                         for (i=0; i < msg.frame.can_dlc; i++)
179                                 printf("%02X ", msg.frame.data[i]);
180
181                 } else {
182
183                         if (msg.msg_head.opcode != RX_TIMEOUT) {
184                                 printf("missing RX_TIMEOUT.\n");
185                                 return 1;
186                         }
187
188                         printf("RX_TIMEOUT");
189                 }
190
191                 printf("\n");
192                 fflush(stdout);
193
194                 if (msgs && !(--msgs))
195                         break;
196         }
197
198         close(s);
199
200         return 0;
201 }