/** \file busload.c * * Load monitor for the SJA1000 * * \author Cristiano Brudna * \date 2003 * * University of Ulm, Germany * * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */ #include #include #include #include #include #include #include #include #include #include #include "can.h" #include "canlib.h" int can, done = 0; struct itimerval interval; struct timeval start; unsigned long long counter = 0; unsigned long long init, end; float averageRate = 0; long long sample = 1; // 1s void sigIntHandler (int a) { done = 1; } unsigned long long timevalToUs (struct timeval time) { return ((unsigned long long)time.tv_sec*1000000 + (unsigned long long)time.tv_usec); } void showBusLoad (int a) { struct timeval now; tputs( tgetstr( "cl", 0 ), 1, &putchar ); fflush( stdout ); printf("\nBusload monitor for CANBus\n\n"); showStatus(can); gettimeofday(&now, NULL); end = timevalToUs(now); if((end - init) == 0) return; averageRate = counter / (float)((end - init)/1000000); printf("busload is %.03f messages/s (Tav = %.06fs) \n\n", averageRate, 1 / (float)counter); printf("sample time is %Lds \n\n", sample); /* reset the counter */ init = timevalToUs(start); counter = 0; setitimer(ITIMER_REAL, &interval, NULL); } void monitor (void) { int ret; canmsg msg; while(!done) { ret = read(can, &msg, sizeof(canmsg)); if(ret > 0) { counter++; } fflush(stdout); } } int main (int argc, char *argv[]) { unsigned long baudrate = B1000; long long acode = -1, amask = -1; char opchr; int par; setupterm( 0, 2, 0 ); while((opchr = getopt(argc,argv, "hb:m:c:s:")) != -1) switch(opchr) { case 'h': printf("\nBus load Monitor for CAN \n\n"); printf("usage: cansend [-b baud] [-m mask] [-c code] [-h] \n\n"); printf("-b baud : set baudrate [1000, 500, 250, 125, 20] kbit/s\n"); printf("-m amask : set a 32-bit acceptance mask\n"); printf("-c acode : set a 32-bit acceptance code\n"); printf("-s sample time : set the period of time is seconds used to calculate/update the bus load\n"); printf("-h : help\n"); printf("\n"); exit(0); case 'b': par = atoi(optarg); switch(par) { case 1000: baudrate = B1000; break; case 500: baudrate = B500; break; case 250: baudrate = B250; break; case 125: baudrate = B125; break; case 20: baudrate = B20; break; default: printf("\nBaud rate not supported!!! Using 1Mbit/s instead\n"); } break; case 'm': sscanf(optarg, "%Lx", (long long *) &amask); /* printf("amask = %Lx\n", amask); */ break; case 'c': sscanf(optarg, "%Lx", (long long *) &acode); /* printf("acode = %Lx\n", acode); */ break; case 's': sscanf(optarg, "%Lx", (long long *) &sample); /* printf("sample = %Lxs \n", sample); */ break; default: break; } can = open("/dev/can", O_RDWR); if(can == -1) { perror("Error opening /dev/can"); exit(0); } ioctl(can, CAN_IOCSBAUD, &baudrate); if(acode != -1) { ioctl(can, CAN_IOCSACODE, &acode); } if(amask != -1) { ioctl(can, CAN_IOCSAMASK, &amask); } gettimeofday(&start, NULL); init = timevalToUs(start); signal(SIGINT, sigIntHandler); siginterrupt(SIGINT, 1); signal(SIGALRM, showBusLoad); siginterrupt(SIGALRM, 1); /* program the timer */ interval.it_value.tv_sec = sample; interval.it_value.tv_usec = 0; setitimer(ITIMER_REAL, &interval, NULL); monitor(); close(can); return 0; }