8 * Copyright (c) 2002-2008 Volkswagen Group Electronic Research
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
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
44 * Send feedback to <socketcan-users@lists.berlios.de>
58 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <sys/ioctl.h>
64 #include <linux/can.h>
65 #include <linux/can/raw.h>
69 #define MAXSOCK 16 /* max. number of CAN interfaces given on the cmdline */
71 #define PERCENTRES 5 /* resolution in percent for bargraph */
72 #define NUMBAR (100/PERCENTRES) /* number of bargraph elements */
74 extern int optind, opterr, optopt;
77 char devname[IFNAMSIZ+1];
79 unsigned int recv_frames;
80 unsigned int recv_bits_total;
81 unsigned int recv_bits_payload;
84 static int max_devname_len; /* to prevent frazzled device name output */
85 static int max_bitrate_len;
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;
94 void print_usage(char *prg)
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");
121 void sigterm(int signo)
126 void printstats(int signo)
131 printf("%s", CSR_HOME);
137 if (time(&currtime) == (time_t)-1) {
142 localtime_r(&currtime, &now);
144 printf("%s %04d-%02d-%02d %02d:%02d:%02d\n",
154 for (i=0; i<currmax; i++) {
160 printf("%s", FGBLUE);
164 percent = (stat[i].recv_bits_total*100)/stat[i].bitrate;
168 printf(" %*s@%-*d %4d %6d %6d %3d%%",
169 max_devname_len, stat[i].devname,
170 max_bitrate_len, stat[i].bitrate,
172 stat[i].recv_bits_total,
173 stat[i].recv_bits_payload,
183 for (j=0; j < NUMBAR; j++){
184 if (j < percent/PERCENTRES)
194 printf("%s", ATTRESET);
198 stat[i].recv_frames = 0;
199 stat[i].recv_bits_total = 0;
200 stat[i].recv_bits_payload = 0;
208 int main(int argc, char **argv)
215 struct sockaddr_can addr;
216 struct can_frame frame;
219 sigset_t sigmask, savesigmask;
221 signal(SIGTERM, sigterm);
222 signal(SIGHUP, sigterm);
223 signal(SIGINT, sigterm);
225 signal(SIGALRM, printstats);
227 prg = basename(argv[0]);
229 while ((opt = getopt(argc, argv, "rtbcih?")) != -1) {
248 ignore_bitstuffing = 1;
258 if (optind == argc) {
263 currmax = argc - optind; /* find real number of CAN devices */
265 if (currmax > MAXSOCK) {
266 printf("More than %d CAN devices given on commandline!\n", MAXSOCK);
270 for (i=0; i < currmax; i++) {
272 ptr = argv[optind+i];
274 nbytes = strlen(ptr);
275 if (nbytes >= IFNAMSIZ+sizeof("@1000000")+1) {
276 printf("name of CAN device '%s' is too long!\n", ptr);
281 printf("open %d '%s'.\n", i, ptr);
284 s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW);
290 nptr = strchr(ptr, '@');
297 nbytes = nptr - ptr; /* interface name is up the first '@' */
299 if (nbytes >= IFNAMSIZ) {
300 printf("name of CAN device '%s' is too long!\n", ptr);
304 strncpy(stat[i].devname, ptr, nbytes);
305 memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
306 strncpy(ifr.ifr_name, ptr, nbytes);
308 if (nbytes > max_devname_len)
309 max_devname_len = nbytes; /* for nice printing */
311 stat[i].bitrate = atoi(nptr+1); /* bitrate is placed behind the '@' */
313 if (!stat[i].bitrate || stat[i].bitrate > 1000000) {
314 printf("invalid bitrate for CAN device '%s'!\n", ptr);
318 nbytes = strlen(nptr+1);
319 if (nbytes > max_bitrate_len)
320 max_bitrate_len = nbytes; /* for nice printing */
324 printf("using interface name '%s'.\n", ifr.ifr_name);
327 if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
328 perror("SIOCGIFINDEX");
332 addr.can_family = AF_CAN;
333 addr.can_ifindex = ifr.ifr_ifindex;
335 if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
344 printf("%s", CLR_SCREEN);
349 for (i=0; i<currmax; i++)
352 savesigmask = sigmask;
354 if (pselect(s[currmax-1]+1, &rdfs, NULL, NULL, NULL, &sigmask) < 0) {
356 sigmask = savesigmask;
360 for (i=0; i<currmax; i++) { /* check all CAN RAW sockets */
362 if (FD_ISSET(s[i], &rdfs)) {
364 nbytes = read(s[i], &frame, sizeof(struct can_frame));
371 if (nbytes < sizeof(struct can_frame)) {
372 fprintf(stderr, "read: incomplete CAN frame\n");
376 stat[i].recv_frames++;
377 stat[i].recv_bits_payload += frame.can_dlc*8;
380 * Following Ken Tindells *worst* case calculation for stuff-bits
381 * (see "Guaranteeing Message Latencies on Controller Area Network" 1st ICC'94)
382 * the needed bits on the wire can be calculated as:
384 * (34 + 8n)/5 + 47 + 8n for SFF frames (11 bit CAN-ID) => (269 + 48n)/5
385 * (54 + 8n)/5 + 67 + 8n for EFF frames (29 bit CAN-ID) => (389 + 48n)/5
387 * while 'n' is the data length code (number of payload bytes)
391 if (ignore_bitstuffing) {
392 /* calculation without bitstuffing */
393 if (frame.can_id & CAN_EFF_FLAG)
394 stat[i].recv_bits_total += 67 + frame.can_dlc*8;
396 stat[i].recv_bits_total += 47 + frame.can_dlc*8;
398 /* needed bits including estimated worst case stuff bits */
399 if (frame.can_id & CAN_EFF_FLAG)
400 stat[i].recv_bits_total += (389 + frame.can_dlc*48)/5;
402 stat[i].recv_bits_total += (269 + frame.can_dlc*48)/5;
408 for (i=0; i<currmax; i++)