]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - canbusload.c
Update includes to Linux 3.6 with CAN FD support.
[sojka/can-utils.git] / canbusload.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * canbusload.c
7  *
8  * Copyright (c) 2002-2008 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 <linux-can@vger.kernel.org>
45  *
46  */
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <string.h>
52 #include <signal.h>
53 #include <ctype.h>
54 #include <libgen.h>
55 #include <time.h>
56
57 #include <sys/time.h>
58 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <sys/ioctl.h>
61 #include <sys/uio.h>
62 #include <net/if.h>
63
64 #include <linux/can.h>
65 #include <linux/can/raw.h>
66
67 #include "terminal.h"
68
69 #define MAXSOCK 16    /* max. number of CAN interfaces given on the cmdline */
70
71 #define PERCENTRES 5 /* resolution in percent for bargraph */
72 #define NUMBAR (100/PERCENTRES) /* number of bargraph elements */
73
74 extern int optind, opterr, optopt;
75
76 static struct {
77         char devname[IFNAMSIZ+1];
78         unsigned int bitrate;
79         unsigned int recv_frames;
80         unsigned int recv_bits_total;
81         unsigned int recv_bits_payload;
82 } stat[MAXSOCK+1];
83
84 static int  max_devname_len; /* to prevent frazzled device name output */ 
85 static int  max_bitrate_len;
86 static int  currmax;
87 static unsigned char redraw;
88 static unsigned char timestamp;
89 static unsigned char color;
90 static unsigned char bargraph;
91 static unsigned char ignore_bitstuffing;
92 static char *prg;
93
94 void print_usage(char *prg)
95 {
96         fprintf(stderr, "\nUsage: %s [options] <CAN interface>+\n", prg);
97         fprintf(stderr, "  (use CTRL-C to terminate %s)\n\n", prg);
98         fprintf(stderr, "Options: -t (show current time on the first line)\n");
99         fprintf(stderr, "         -c (colorize lines)\n");
100         fprintf(stderr, "         -b (show bargraph in %d%% resolution)\n", PERCENTRES);
101         fprintf(stderr, "         -r (redraw the terminal - similar to top)\n");
102         fprintf(stderr, "         -i (ignore bitstuffing estimation in bandwith calculation)\n");
103         fprintf(stderr, "\n");
104         fprintf(stderr, "Up to %d CAN interfaces with mandatory bitrate can be specified on the \n", MAXSOCK);
105         fprintf(stderr, "commandline in the form: <ifname>@<bitrate>\n\n");
106         fprintf(stderr, "The bitrate is mandatory as it is needed to know the CAN bus bitrate to\n");
107         fprintf(stderr, "calcultate the bus load percentage based on the received CAN frames.\n");
108         fprintf(stderr, "Due to the bitstuffing estimation the calculated busload may exceed 100%%.\n");
109         fprintf(stderr, "For each given interface the data is presented in one line which contains:\n\n");
110         fprintf(stderr, "(interface) (received CAN frames) (used bits total) (used bits for payload)\n");
111         fprintf(stderr, "\nExample:\n");
112         fprintf(stderr, "\nuser$> canbusload can0@100000 can1@500000 can2@500000 can3@500000 -r -t -b -c\n\n");
113         fprintf(stderr, "%s 2008-05-27 15:18:49\n", prg);
114         fprintf(stderr, " can0@100000   805   74491  36656  74%% |XXXXXXXXXXXXXX......|\n");
115         fprintf(stderr, " can1@500000   796   75140  37728  15%% |XXX.................|\n");
116         fprintf(stderr, " can2@500000     0       0      0   0%% |....................|\n");
117         fprintf(stderr, " can3@500000    47    4633   2424   0%% |....................|\n");
118         fprintf(stderr, "\n");
119 }
120
121 void sigterm(int signo)
122 {
123         exit(0);
124 }
125
126 void printstats(int signo)
127 {
128         int i, j, percent;
129
130         if (redraw)
131                 printf("%s", CSR_HOME);
132
133         if (timestamp) {
134                 time_t currtime;
135                 struct tm now;
136
137                 if (time(&currtime) == (time_t)-1) {
138                         perror("time");
139                         exit(1);
140                 }
141
142                 localtime_r(&currtime, &now);
143
144                 printf("%s %04d-%02d-%02d %02d:%02d:%02d\n",
145                        prg,
146                        now.tm_year + 1900,
147                        now.tm_mon + 1,
148                        now.tm_mday,
149                        now.tm_hour,
150                        now.tm_min,
151                        now.tm_sec);
152         }
153
154         for (i=0; i<currmax; i++) {
155
156                 if (color) {
157                         if (i%2)
158                                 printf("%s", FGRED);
159                         else
160                                 printf("%s", FGBLUE);
161                 }
162
163                 if (stat[i].bitrate)
164                         percent = (stat[i].recv_bits_total*100)/stat[i].bitrate;
165                 else
166                         percent = 0;
167
168                 printf(" %*s@%-*d %5d %7d %6d %3d%%",
169                        max_devname_len, stat[i].devname,
170                        max_bitrate_len, stat[i].bitrate,
171                        stat[i].recv_frames,
172                        stat[i].recv_bits_total,
173                        stat[i].recv_bits_payload,
174                        percent);
175
176                 if (bargraph) {
177
178                         printf(" |");
179
180                         if (percent > 100)
181                                 percent = 100;
182
183                         for (j=0; j < NUMBAR; j++){
184                                 if (j < percent/PERCENTRES)
185                                         printf("X");
186                                 else
187                                         printf(".");
188                         }
189             
190                         printf("|");
191                 }
192         
193                 if (color)
194                         printf("%s", ATTRESET);
195
196                 printf("\n");
197
198                 stat[i].recv_frames = 0;
199                 stat[i].recv_bits_total = 0;
200                 stat[i].recv_bits_payload = 0;
201         }
202
203         printf("\n");
204         fflush(stdout);
205
206         alarm(1);
207 }
208
209 int main(int argc, char **argv)
210 {
211         fd_set rdfs;
212         int s[MAXSOCK];
213
214         int opt;
215         char *ptr, *nptr;
216         struct sockaddr_can addr;
217         struct can_frame frame;
218         int nbytes, i;
219         struct ifreq ifr;
220         sigset_t sigmask, savesigmask;
221
222         signal(SIGTERM, sigterm);
223         signal(SIGHUP, sigterm);
224         signal(SIGINT, sigterm);
225
226         signal(SIGALRM, printstats);
227
228         prg = basename(argv[0]);
229
230         while ((opt = getopt(argc, argv, "rtbcih?")) != -1) {
231                 switch (opt) {
232                 case 'r':
233                         redraw = 1;
234                         break;
235
236                 case 't':
237                         timestamp = 1;
238                         break;
239
240                 case 'b':
241                         bargraph = 1;
242                         break;
243
244                 case 'c':
245                         color = 1;
246                         break;
247
248                 case 'i':
249                         ignore_bitstuffing = 1;
250                         break;
251
252                 default:
253                         print_usage(prg);
254                         exit(1);
255                         break;
256                 }
257         }
258
259         if (optind == argc) {
260                 print_usage(prg);
261                 exit(0);
262         }
263         
264         currmax = argc - optind; /* find real number of CAN devices */
265
266         if (currmax > MAXSOCK) {
267                 printf("More than %d CAN devices given on commandline!\n", MAXSOCK);
268                 return 1;
269         }
270
271         for (i=0; i < currmax; i++) {
272
273                 ptr = argv[optind+i];
274
275                 nbytes = strlen(ptr);
276                 if (nbytes >= IFNAMSIZ+sizeof("@1000000")+1) {
277                         printf("name of CAN device '%s' is too long!\n", ptr);
278                         return 1;
279                 }
280
281 #ifdef DEBUG
282                 printf("open %d '%s'.\n", i, ptr);
283 #endif
284
285                 s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW);
286                 if (s[i] < 0) {
287                         perror("socket");
288                         return 1;
289                 }
290
291                 nptr = strchr(ptr, '@');
292
293                 if (!nptr) {
294                         print_usage(prg);
295                         return 1;
296                 }
297
298                 nbytes = nptr - ptr;  /* interface name is up the first '@' */
299
300                 if (nbytes >= IFNAMSIZ) {
301                         printf("name of CAN device '%s' is too long!\n", ptr);
302                         return 1;
303                 }
304
305                 strncpy(stat[i].devname, ptr, nbytes);
306                 memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
307                 strncpy(ifr.ifr_name, ptr, nbytes);
308
309                 if (nbytes > max_devname_len)
310                         max_devname_len = nbytes; /* for nice printing */
311
312                 stat[i].bitrate = atoi(nptr+1); /* bitrate is placed behind the '@' */
313
314                 if (!stat[i].bitrate || stat[i].bitrate > 1000000) {
315                         printf("invalid bitrate for CAN device '%s'!\n", ptr);
316                         return 1;
317                 }
318
319                 nbytes = strlen(nptr+1);
320                 if (nbytes > max_bitrate_len)
321                         max_bitrate_len = nbytes; /* for nice printing */
322
323
324 #ifdef DEBUG
325                 printf("using interface name '%s'.\n", ifr.ifr_name);
326 #endif
327
328                 if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
329                         perror("SIOCGIFINDEX");
330                         exit(1);
331                 }
332
333                 addr.can_family = AF_CAN;
334                 addr.can_ifindex = ifr.ifr_ifindex;
335
336                 if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
337                         perror("bind");
338                         return 1;
339                 }
340         }
341
342         alarm(1);
343
344         if (redraw)
345                 printf("%s", CLR_SCREEN);
346
347         while (1) {
348
349                 FD_ZERO(&rdfs);
350                 for (i=0; i<currmax; i++)
351                         FD_SET(s[i], &rdfs);
352
353                 savesigmask = sigmask;
354
355                 if (pselect(s[currmax-1]+1, &rdfs, NULL, NULL, NULL, &sigmask) < 0) {
356                         //perror("pselect");
357                         sigmask = savesigmask;
358                         continue;
359                 }
360
361                 for (i=0; i<currmax; i++) {  /* check all CAN RAW sockets */
362
363                         if (FD_ISSET(s[i], &rdfs)) {
364
365                                 nbytes = read(s[i], &frame, sizeof(struct can_frame));
366
367                                 if (nbytes < 0) {
368                                         perror("read");
369                                         return 1;
370                                 }
371
372                                 if (nbytes < sizeof(struct can_frame)) {
373                                         fprintf(stderr, "read: incomplete CAN frame\n");
374                                         return 1;
375                                 }
376
377                                 stat[i].recv_frames++;
378                                 stat[i].recv_bits_payload += frame.can_dlc*8;
379
380                                 /*
381                                  * Following Ken Tindells *worst* case calculation for stuff-bits
382                                  * (see "Guaranteeing Message Latencies on Controller Area Network" 1st ICC'94)
383                                  * the needed bits on the wire can be calculated as:
384                                  *
385                                  * (34 + 8n)/5 + 47 + 8n for SFF frames (11 bit CAN-ID) => (269 + 48n)/5 
386                                  * (54 + 8n)/5 + 67 + 8n for EFF frames (29 bit CAN-ID) => (389 + 48n)/5 
387                                  *
388                                  * while 'n' is the data length code (number of payload bytes)
389                                  *
390                                  */
391
392                                 if (ignore_bitstuffing) {
393                                         /* calculation without bitstuffing */
394                                         if (frame.can_id & CAN_EFF_FLAG)
395                                                 stat[i].recv_bits_total += 67 + frame.can_dlc*8;
396                                         else
397                                                 stat[i].recv_bits_total += 47 + frame.can_dlc*8;
398                                 } else {
399                                         /* needed bits including estimated worst case stuff bits */
400                                         if (frame.can_id & CAN_EFF_FLAG)
401                                                 stat[i].recv_bits_total += (389 + frame.can_dlc*48)/5;
402                                         else
403                                                 stat[i].recv_bits_total += (269 + frame.can_dlc*48)/5;
404                                 }
405                         }
406                 }
407         }
408
409         for (i=0; i<currmax; i++)
410                 close(s[i]);
411
412         return 0;
413 }