Skip to content

Commit

Permalink
Merge pull request #461 from marckleinebudde/canbusload
Browse files Browse the repository at this point in the history
canbusload: coding style cleanups + use less screen estate in redraw mode
  • Loading branch information
marckleinebudde authored Sep 27, 2023
2 parents 3490282 + 84919ed commit 1be7e0e
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions canbusload.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,25 @@
#define MAXSOCK 16 /* max. number of CAN interfaces given on the cmdline */

#define PERCENTRES 5 /* resolution in percent for bargraph */
#define NUMBAR (100/PERCENTRES) /* number of bargraph elements */
#define NUMBAR (100 / PERCENTRES) /* number of bargraph elements */

extern int optind, opterr, optopt;

static struct {
char devname[IFNAMSIZ+1];
char devname[IFNAMSIZ + 1];
unsigned int bitrate;
unsigned int dbitrate;
unsigned int recv_frames;
unsigned int recv_bits_total;
unsigned int recv_bits_payload;
unsigned int recv_bits_dbitrate;
} stat[MAXSOCK+1];
} stat[MAXSOCK + 1];

static volatile int running = 1;
static volatile sig_atomic_t signal_num;
static int max_devname_len; /* to prevent frazzled device name output */
static int max_bitrate_len;
static int currmax;
static int max_devname_len; /* to prevent frazzled device name output */
static int max_bitrate_len;
static int currmax;
static unsigned char redraw;
static unsigned char timestamp;
static unsigned char color;
Expand Down Expand Up @@ -180,18 +180,17 @@ void printstats(int signo)
}
}

for (i=0; i<currmax; i++) {

for (i = 0; i < currmax; i++) {
if (color) {
if (i%2)
if (i % 2)
printf("%s", FGRED);
else
printf("%s", FGBLUE);
}

if (stat[i].bitrate)
percent = ((stat[i].recv_bits_total-stat[i].recv_bits_dbitrate) * 100) / stat[i].bitrate
+ (stat[i].recv_bits_dbitrate * 100) / stat[i].dbitrate;
percent = ((stat[i].recv_bits_total - stat[i].recv_bits_dbitrate) * 100) / stat[i].bitrate +
(stat[i].recv_bits_dbitrate * 100) / stat[i].dbitrate;
else
percent = 0;

Expand All @@ -211,28 +210,31 @@ void printstats(int signo)
if (percent > 100)
percent = 100;

for (j=0; j < NUMBAR; j++){
if (j < percent/PERCENTRES)
for (j = 0; j < NUMBAR; j++) {
if (j < percent / PERCENTRES)
printf("X");
else
printf(".");
}

printf("|");
}

if (color)
printf("%s", ATTRESET);

printf("\n");
if (!redraw || (i < currmax - 1))
printf("\n");

stat[i].recv_frames = 0;
stat[i].recv_bits_total = 0;
stat[i].recv_bits_dbitrate = 0;
stat[i].recv_bits_payload = 0;
}

printf("\n");
if (!redraw)
printf("\n");

fflush(stdout);

alarm(1);
Expand Down Expand Up @@ -296,20 +298,19 @@ int main(int argc, char **argv)
print_usage(prg);
exit(0);
}

currmax = argc - optind; /* find real number of CAN devices */

if (currmax > MAXSOCK) {
printf("More than %d CAN devices given on commandline!\n", MAXSOCK);
return 1;
}

for (i=0; i < currmax; i++) {

ptr = argv[optind+i];
for (i = 0; i < currmax; i++) {
ptr = argv[optind + i];

nbytes = strlen(ptr);
if (nbytes >= (int)(IFNAMSIZ+sizeof("@1000000")+1)) {
if (nbytes >= (int)(IFNAMSIZ + sizeof("@1000000") + 1)) {
printf("name of CAN device '%s' is too long!\n", ptr);
return 1;
}
Expand Down Expand Up @@ -357,7 +358,7 @@ int main(int argc, char **argv)
return 1;
}

nbytes = strlen(nptr+1);
nbytes = strlen(nptr + 1);
if (nbytes > max_bitrate_len)
max_bitrate_len = nbytes; /* for nice printing */

Expand Down Expand Up @@ -387,23 +388,21 @@ int main(int argc, char **argv)
printf("%s", CLR_SCREEN);

while (running) {

FD_ZERO(&rdfs);
for (i=0; i<currmax; i++)
for (i = 0; i < currmax; i++)
FD_SET(s[i], &rdfs);

savesigmask = sigmask;

if (pselect(s[currmax-1]+1, &rdfs, NULL, NULL, NULL, &sigmask) < 0) {
if (pselect(s[currmax - 1] + 1, &rdfs, NULL, NULL, NULL, &sigmask) < 0) {
//perror("pselect");
sigmask = savesigmask;
continue;
}

for (i=0; i<currmax; i++) { /* check all CAN RAW sockets */
for (i = 0; i < currmax; i++) { /* check all CAN RAW sockets */

if (FD_ISSET(s[i], &rdfs)) {

nbytes = read(s[i], &frame, sizeof(frame));

if (nbytes < 0) {
Expand All @@ -426,7 +425,7 @@ int main(int argc, char **argv)
}
}

for (i=0; i<currmax; i++)
for (i = 0; i < currmax; i++)
close(s[i]);

if (signal_num)
Expand Down

0 comments on commit 1be7e0e

Please sign in to comment.