]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - test/tst-bcm-dump.c
Whitespace fixes. Indented the code following Linux styleguide to fix
[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, "         -s             (set STARTTIMER flag. Default: off)\n");
54         fprintf(stderr, "\n");
55 }
56
57 int main(int argc, char **argv)
58 {
59         int s;
60         struct sockaddr_can addr;
61         int nbytes;
62         int i;
63         struct ifreq ifr;
64         char *ifname = DEFAULT_IFACE;
65         canid_t canid = DEFAULT_CANID;
66         int opt;
67         struct timeval tv;
68         unsigned long starttimer = 0;
69         unsigned long long timeout = 0;
70         unsigned long long throttle = 0;
71         struct {
72                 struct bcm_msg_head msg_head;
73                 struct can_frame frame;
74         } msg;
75
76         while ((opt = getopt(argc, argv, "i:c:o:t:s")) != -1) {
77                 switch (opt) {
78
79                 case 'i':
80                         ifname = optarg;
81                         break;
82
83                 case 'c':
84                         canid = strtoul(optarg, (char **)NULL, 16);
85                         break;
86
87                 case 'o':
88                         timeout = atoll(optarg);
89                         break;
90
91                 case 't':
92                         throttle = atoll(optarg);
93                         break;
94
95                 case 's':
96                         starttimer = STARTTIMER;
97                         break;
98
99                 case '?':
100                 default:
101                         print_usage(basename(argv[0]));
102                         exit(1);
103                         break;
104                 }
105         }
106
107
108         if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
109                 perror("socket");
110                 return 1;
111         }
112
113         if (strcmp(ifname, "any") == 0)
114                 addr.can_ifindex = 0;
115         else {
116                 strcpy(ifr.ifr_name, ifname);
117                 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
118                         perror("SIOCGIFINDEX");
119                         return 1;
120                 }
121                 addr.can_ifindex = ifr.ifr_ifindex;
122         }
123
124         addr.can_family = PF_CAN;
125
126         if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
127                 perror("connect");
128                 return 1;
129         }
130
131         msg.msg_head.opcode             = RX_SETUP;
132         msg.msg_head.can_id             = canid;
133         msg.msg_head.flags              = SETTIMER|RX_FILTER_ID|starttimer;
134         msg.msg_head.ival1.tv_sec       = timeout / 1000000;
135         msg.msg_head.ival1.tv_usec      = timeout % 1000000;
136         msg.msg_head.ival2.tv_sec       = throttle / 1000000;
137         msg.msg_head.ival2.tv_usec      = throttle % 1000000;
138         msg.msg_head.nframes    = 0;
139
140         gettimeofday(&tv, NULL);
141         printf("[%ld.%06ld] ", tv.tv_sec, tv.tv_usec);
142         printf("Writing RX_SETUP with RX_FILTER_ID for can_id <%03X>\n",
143                msg.msg_head.can_id);
144
145         if (write(s, &msg, sizeof(msg)) < 0)
146                 perror("write");
147
148         while (1) {
149
150                 nbytes = read(s, &msg, sizeof(msg));
151                 if (nbytes < 0) {
152                         perror("read");
153                         return 1;
154                 }
155                 gettimeofday(&tv, NULL);
156                 printf("[%ld.%06ld] ", tv.tv_sec, tv.tv_usec);
157
158                 if (nbytes == sizeof(msg)) {
159
160                         if (ioctl(s, SIOCGSTAMP, &tv) < 0)
161                                 perror("SIOCGSTAMP");
162                         else
163                                 printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
164
165                         if (msg.msg_head.opcode != RX_CHANGED) {
166                                 printf("missing RX_CHANGED.\n");
167                                 return 1;
168                         }
169
170                         printf("RX_CHANGED ");
171
172                         for (i=0; i<8; i++)
173                                 printf("%02X ", msg.frame.data[i]);
174
175                 } else {
176
177                         if (msg.msg_head.opcode != RX_TIMEOUT) {
178                                 printf("missing RX_TIMEOUT.\n");
179                                 return 1;
180                         }
181
182                         printf("RX_TIMEOUT");
183                 }
184
185                 printf("\n");
186                 fflush(stdout);
187         }
188
189         close(s);
190
191         return 0;
192 }