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;
93 void print_usage(char *prg)
95 fprintf(stderr, "\nUsage: %s [options] <CAN interface>+\n", prg);
96 fprintf(stderr, " (use CTRL-C to terminate %s)\n\n", prg);
97 fprintf(stderr, "Options: -t (show current time on the first line)\n");
98 fprintf(stderr, " -c (colorize lines)\n");
99 fprintf(stderr, " -b (show bargraph in %d%% resolution)\n", PERCENTRES);
100 fprintf(stderr, " -r (redraw the terminal - similar to top)\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, "For each given interface the data is presented in one line which contains:\n\n");
107 fprintf(stderr, "(interface) (received CAN frames) (used bits total) (used bits for payload)\n");
108 fprintf(stderr, "\nExample:\n");
109 fprintf(stderr, "\nuser$> canbusload can0@100000 can1@500000 can2@500000 can3@500000 -r -t -b -c\n\n");
110 fprintf(stderr, "%s 2008-05-27 15:18:49\n", prg);
111 fprintf(stderr, " can0@100000 805 74491 36656 74%% |XXXXXXXXXXXXXX......|\n");
112 fprintf(stderr, " can1@500000 796 75140 37728 15%% |XXX.................|\n");
113 fprintf(stderr, " can2@500000 0 0 0 0%% |....................|\n");
114 fprintf(stderr, " can3@500000 47 4633 2424 0%% |....................|\n");
115 fprintf(stderr, "\n");
118 void sigterm(int signo)
123 void printstats(int signo)
128 printf("%s", CSR_HOME);
134 if (time(&currtime) == (time_t)-1) {
139 localtime_r(&currtime, &now);
141 printf("%s %04d-%02d-%02d %02d:%02d:%02d\n",
151 for (i=0; i<currmax; i++) {
157 printf("%s", FGBLUE);
161 percent = (stat[i].recv_bits_total*100)/stat[i].bitrate;
165 printf(" %*s@%-*d %4d %6d %6d %3d%%",
166 max_devname_len, stat[i].devname,
167 max_bitrate_len, stat[i].bitrate,
169 stat[i].recv_bits_total,
170 stat[i].recv_bits_payload,
177 for (j=0; j < NUMBAR; j++){
178 if (j < percent/PERCENTRES)
188 printf("%s", ATTRESET);
192 stat[i].recv_frames = 0;
193 stat[i].recv_bits_total = 0;
194 stat[i].recv_bits_payload = 0;
202 int main(int argc, char **argv)
209 struct sockaddr_can addr;
210 struct can_frame frame;
213 sigset_t sigmask, savesigmask;
215 signal(SIGTERM, sigterm);
216 signal(SIGHUP, sigterm);
217 signal(SIGINT, sigterm);
219 signal(SIGALRM, printstats);
221 prg = basename(argv[0]);
223 while ((opt = getopt(argc, argv, "rtbch?")) != -1) {
248 if (optind == argc) {
253 currmax = argc - optind; /* find real number of CAN devices */
255 if (currmax > MAXSOCK) {
256 printf("More than %d CAN devices given on commandline!\n", MAXSOCK);
260 for (i=0; i < currmax; i++) {
262 ptr = argv[optind+i];
264 nbytes = strlen(ptr);
265 if (nbytes >= IFNAMSIZ+sizeof("@1000000")+1) {
266 printf("name of CAN device '%s' is too long!\n", ptr);
271 printf("open %d '%s'.\n", i, ptr);
274 s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW);
280 nptr = strchr(ptr, '@');
287 nbytes = nptr - ptr; /* interface name is up the first '@' */
289 if (nbytes >= IFNAMSIZ) {
290 printf("name of CAN device '%s' is too long!\n", ptr);
294 strncpy(stat[i].devname, ptr, nbytes);
295 memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
296 strncpy(ifr.ifr_name, ptr, nbytes);
298 if (nbytes > max_devname_len)
299 max_devname_len = nbytes; /* for nice printing */
301 stat[i].bitrate = atoi(nptr+1); /* bitrate is placed behind the '@' */
303 if (!stat[i].bitrate || stat[i].bitrate > 1000000) {
304 printf("invalid bitrate for CAN device '%s'!\n", ptr);
308 nbytes = strlen(nptr+1);
309 if (nbytes > max_bitrate_len)
310 max_bitrate_len = nbytes; /* for nice printing */
314 printf("using interface name '%s'.\n", ifr.ifr_name);
317 if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
318 perror("SIOCGIFINDEX");
322 addr.can_family = AF_CAN;
323 addr.can_ifindex = ifr.ifr_ifindex;
325 if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
334 printf("%s", CLR_SCREEN);
339 for (i=0; i<currmax; i++)
342 savesigmask = sigmask;
344 if (pselect(s[currmax-1]+1, &rdfs, NULL, NULL, NULL, &sigmask) < 0) {
346 sigmask = savesigmask;
350 for (i=0; i<currmax; i++) { /* check all CAN RAW sockets */
352 if (FD_ISSET(s[i], &rdfs)) {
354 nbytes = read(s[i], &frame, sizeof(struct can_frame));
361 if (nbytes < sizeof(struct can_frame)) {
362 fprintf(stderr, "read: incomplete CAN frame\n");
366 stat[i].recv_frames++;
367 stat[i].recv_bits_payload += frame.can_dlc*8;
368 stat[i].recv_bits_total += frame.can_dlc*8;
369 if (frame.can_id & CAN_EFF_FLAG)
370 stat[i].recv_bits_total += 67;
372 stat[i].recv_bits_total += 47;
377 for (i=0; i<currmax; i++)