]> rtime.felk.cvut.cz Git - can-utils.git/blob - canbusload.c
Android.mk: canbusload: fix compilation, make use of libcan
[can-utils.git] / canbusload.c
1 /*
2  * canbusload.c
3  *
4  * Copyright (c) 2002-2008 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of Volkswagen nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * Alternatively, provided that this notice is retained in full, this
20  * software may be distributed under the terms of the GNU General
21  * Public License ("GPL") version 2, in which case the provisions of the
22  * GPL apply INSTEAD OF those given above.
23  *
24  * The provided data structures and external interfaces from this code
25  * are not restricted to be used by modules with a GPL compatible license.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  *
40  * Send feedback to <linux-can@vger.kernel.org>
41  *
42  */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <string.h>
48 #include <signal.h>
49 #include <ctype.h>
50 #include <libgen.h>
51 #include <time.h>
52
53 #include <sys/time.h>
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <sys/ioctl.h>
57 #include <sys/uio.h>
58 #include <net/if.h>
59
60 #include <linux/can.h>
61 #include <linux/can/raw.h>
62
63 #include "terminal.h"
64 #include "canframelen.h"
65
66 #define MAXSOCK 16    /* max. number of CAN interfaces given on the cmdline */
67
68 #define PERCENTRES 5 /* resolution in percent for bargraph */
69 #define NUMBAR (100/PERCENTRES) /* number of bargraph elements */
70
71 extern int optind, opterr, optopt;
72
73 static struct {
74         char devname[IFNAMSIZ+1];
75         unsigned int bitrate;
76         unsigned int recv_frames;
77         unsigned int recv_bits_total;
78         unsigned int recv_bits_payload;
79 } stat[MAXSOCK+1];
80
81 static int  max_devname_len; /* to prevent frazzled device name output */ 
82 static int  max_bitrate_len;
83 static int  currmax;
84 static unsigned char redraw;
85 static unsigned char timestamp;
86 static unsigned char color;
87 static unsigned char bargraph;
88 static enum cfl_mode mode = CFL_WORSTCASE;
89 static char *prg;
90
91 void print_usage(char *prg)
92 {
93         fprintf(stderr, "\nUsage: %s [options] <CAN interface>+\n", prg);
94         fprintf(stderr, "  (use CTRL-C to terminate %s)\n\n", prg);
95         fprintf(stderr, "Options: -t (show current time on the first line)\n");
96         fprintf(stderr, "         -c (colorize lines)\n");
97         fprintf(stderr, "         -b (show bargraph in %d%% resolution)\n", PERCENTRES);
98         fprintf(stderr, "         -r (redraw the terminal - similar to top)\n");
99         fprintf(stderr, "         -i (ignore bitstuffing in bandwidth calculation)\n");
100         fprintf(stderr, "         -e (exact calculation of stuffed bits)\n");
101         fprintf(stderr, "\n");
102         fprintf(stderr, "Up to %d CAN interfaces with mandatory bitrate can be specified on the \n", MAXSOCK);
103         fprintf(stderr, "commandline in the form: <ifname>@<bitrate>\n\n");
104         fprintf(stderr, "The bitrate is mandatory as it is needed to know the CAN bus bitrate to\n");
105         fprintf(stderr, "calcultate the bus load percentage based on the received CAN frames.\n");
106         fprintf(stderr, "Due to the bitstuffing estimation the calculated busload may exceed 100%%.\n");
107         fprintf(stderr, "For each given interface the data is presented in one line which contains:\n\n");
108         fprintf(stderr, "(interface) (received CAN frames) (used bits total) (used bits for payload)\n");
109         fprintf(stderr, "\nExample:\n");
110         fprintf(stderr, "\nuser$> canbusload can0@100000 can1@500000 can2@500000 can3@500000 -r -t -b -c\n\n");
111         fprintf(stderr, "%s 2008-05-27 15:18:49\n", prg);
112         fprintf(stderr, " can0@100000   805   74491  36656  74%% |XXXXXXXXXXXXXX......|\n");
113         fprintf(stderr, " can1@500000   796   75140  37728  15%% |XXX.................|\n");
114         fprintf(stderr, " can2@500000     0       0      0   0%% |....................|\n");
115         fprintf(stderr, " can3@500000    47    4633   2424   0%% |....................|\n");
116         fprintf(stderr, "\n");
117 }
118
119 void sigterm(int signo)
120 {
121         exit(0);
122 }
123
124 void printstats(int signo)
125 {
126         int i, j, percent;
127
128         if (redraw)
129                 printf("%s", CSR_HOME);
130
131         if (timestamp) {
132                 time_t currtime;
133                 struct tm now;
134
135                 if (time(&currtime) == (time_t)-1) {
136                         perror("time");
137                         exit(1);
138                 }
139
140                 localtime_r(&currtime, &now);
141
142                 printf("%s %04d-%02d-%02d %02d:%02d:%02d\n",
143                        prg,
144                        now.tm_year + 1900,
145                        now.tm_mon + 1,
146                        now.tm_mday,
147                        now.tm_hour,
148                        now.tm_min,
149                        now.tm_sec);
150         }
151
152         for (i=0; i<currmax; i++) {
153
154                 if (color) {
155                         if (i%2)
156                                 printf("%s", FGRED);
157                         else
158                                 printf("%s", FGBLUE);
159                 }
160
161                 if (stat[i].bitrate)
162                         percent = (stat[i].recv_bits_total*100)/stat[i].bitrate;
163                 else
164                         percent = 0;
165
166                 printf(" %*s@%-*d %5d %7d %6d %3d%%",
167                        max_devname_len, stat[i].devname,
168                        max_bitrate_len, stat[i].bitrate,
169                        stat[i].recv_frames,
170                        stat[i].recv_bits_total,
171                        stat[i].recv_bits_payload,
172                        percent);
173
174                 if (bargraph) {
175
176                         printf(" |");
177
178                         if (percent > 100)
179                                 percent = 100;
180
181                         for (j=0; j < NUMBAR; j++){
182                                 if (j < percent/PERCENTRES)
183                                         printf("X");
184                                 else
185                                         printf(".");
186                         }
187             
188                         printf("|");
189                 }
190         
191                 if (color)
192                         printf("%s", ATTRESET);
193
194                 printf("\n");
195
196                 stat[i].recv_frames = 0;
197                 stat[i].recv_bits_total = 0;
198                 stat[i].recv_bits_payload = 0;
199         }
200
201         printf("\n");
202         fflush(stdout);
203
204         alarm(1);
205 }
206
207 int main(int argc, char **argv)
208 {
209         fd_set rdfs;
210         int s[MAXSOCK];
211
212         int opt;
213         char *ptr, *nptr;
214         struct sockaddr_can addr;
215         struct can_frame frame;
216         int nbytes, i;
217         struct ifreq ifr;
218         sigset_t sigmask, savesigmask;
219
220         signal(SIGTERM, sigterm);
221         signal(SIGHUP, sigterm);
222         signal(SIGINT, sigterm);
223
224         signal(SIGALRM, printstats);
225
226         prg = basename(argv[0]);
227
228         while ((opt = getopt(argc, argv, "rtbcieh?")) != -1) {
229                 switch (opt) {
230                 case 'r':
231                         redraw = 1;
232                         break;
233
234                 case 't':
235                         timestamp = 1;
236                         break;
237
238                 case 'b':
239                         bargraph = 1;
240                         break;
241
242                 case 'c':
243                         color = 1;
244                         break;
245
246                 case 'i':
247                         mode = CFL_NO_BITSTUFFING;
248                         break;
249
250                 case 'e':
251                         mode = CFL_EXACT;
252                         break;
253
254                 default:
255                         print_usage(prg);
256                         exit(1);
257                         break;
258                 }
259         }
260
261         if (optind == argc) {
262                 print_usage(prg);
263                 exit(0);
264         }
265         
266         currmax = argc - optind; /* find real number of CAN devices */
267
268         if (currmax > MAXSOCK) {
269                 printf("More than %d CAN devices given on commandline!\n", MAXSOCK);
270                 return 1;
271         }
272
273         for (i=0; i < currmax; i++) {
274
275                 ptr = argv[optind+i];
276
277                 nbytes = strlen(ptr);
278                 if (nbytes >= IFNAMSIZ+sizeof("@1000000")+1) {
279                         printf("name of CAN device '%s' is too long!\n", ptr);
280                         return 1;
281                 }
282
283 #ifdef DEBUG
284                 printf("open %d '%s'.\n", i, ptr);
285 #endif
286
287                 s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW);
288                 if (s[i] < 0) {
289                         perror("socket");
290                         return 1;
291                 }
292
293                 nptr = strchr(ptr, '@');
294
295                 if (!nptr) {
296                         print_usage(prg);
297                         return 1;
298                 }
299
300                 nbytes = nptr - ptr;  /* interface name is up the first '@' */
301
302                 if (nbytes >= IFNAMSIZ) {
303                         printf("name of CAN device '%s' is too long!\n", ptr);
304                         return 1;
305                 }
306
307                 strncpy(stat[i].devname, ptr, nbytes);
308                 memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
309                 strncpy(ifr.ifr_name, ptr, nbytes);
310
311                 if (nbytes > max_devname_len)
312                         max_devname_len = nbytes; /* for nice printing */
313
314                 stat[i].bitrate = atoi(nptr+1); /* bitrate is placed behind the '@' */
315
316                 if (!stat[i].bitrate || stat[i].bitrate > 1000000) {
317                         printf("invalid bitrate for CAN device '%s'!\n", ptr);
318                         return 1;
319                 }
320
321                 nbytes = strlen(nptr+1);
322                 if (nbytes > max_bitrate_len)
323                         max_bitrate_len = nbytes; /* for nice printing */
324
325
326 #ifdef DEBUG
327                 printf("using interface name '%s'.\n", ifr.ifr_name);
328 #endif
329
330                 if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
331                         perror("SIOCGIFINDEX");
332                         exit(1);
333                 }
334
335                 addr.can_family = AF_CAN;
336                 addr.can_ifindex = ifr.ifr_ifindex;
337
338                 if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
339                         perror("bind");
340                         return 1;
341                 }
342         }
343
344         alarm(1);
345
346         if (redraw)
347                 printf("%s", CLR_SCREEN);
348
349         while (1) {
350
351                 FD_ZERO(&rdfs);
352                 for (i=0; i<currmax; i++)
353                         FD_SET(s[i], &rdfs);
354
355                 savesigmask = sigmask;
356
357                 if (pselect(s[currmax-1]+1, &rdfs, NULL, NULL, NULL, &sigmask) < 0) {
358                         //perror("pselect");
359                         sigmask = savesigmask;
360                         continue;
361                 }
362
363                 for (i=0; i<currmax; i++) {  /* check all CAN RAW sockets */
364
365                         if (FD_ISSET(s[i], &rdfs)) {
366
367                                 nbytes = read(s[i], &frame, sizeof(struct can_frame));
368
369                                 if (nbytes < 0) {
370                                         perror("read");
371                                         return 1;
372                                 }
373
374                                 if (nbytes < sizeof(struct can_frame)) {
375                                         fprintf(stderr, "read: incomplete CAN frame\n");
376                                         return 1;
377                                 }
378
379                                 stat[i].recv_frames++;
380                                 stat[i].recv_bits_payload += frame.can_dlc*8;
381                                 stat[i].recv_bits_total += can_frame_length((struct canfd_frame*)&frame,
382                                                                             mode, sizeof(frame));
383                         }
384                 }
385         }
386
387         for (i=0; i<currmax; i++)
388                 close(s[i]);
389
390         return 0;
391 }