From: Oliver Hartkopp Date: Mon, 2 Jun 2008 20:06:37 +0000 (+0000) Subject: Added binary output option to candump.c and sprint_long_canframe() in lib.c . X-Git-Tag: 0.1~189 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8a5c0365e6a0682eca82c0e8cb4d094f43e42f6e;p=profile%2Fivi%2Fcan-utils.git Added binary output option to candump.c and sprint_long_canframe() in lib.c . --- diff --git a/candump.c b/candump.c index f0b50ed..64e6ace 100644 --- a/candump.c +++ b/candump.c @@ -102,6 +102,7 @@ void print_usage(char *prg) fprintf(stderr, " (use CTRL-C to terminate %s)\n\n", prg); fprintf(stderr, "Options: -t (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n"); fprintf(stderr, " -c (increment color mode level)\n"); + fprintf(stderr, " -i (binary output - may exceed 80 chars/line)\n"); fprintf(stderr, " -a (enable additional ASCII output)\n"); fprintf(stderr, " -s (silent mode - 1: animation 2: completely silent)\n"); fprintf(stderr, " -b (bridge mode - send received frames to )\n"); @@ -187,7 +188,7 @@ int main(int argc, char **argv) unsigned char silent = 0; unsigned char silentani = 0; unsigned char color = 0; - unsigned char ascii = 0; + unsigned char view = 0; unsigned char log = 0; unsigned char logfrmt = 0; int opt, ret; @@ -209,7 +210,7 @@ int main(int argc, char **argv) last_tv.tv_sec = 0; last_tv.tv_usec = 0; - while ((opt = getopt(argc, argv, "t:cas:b:B:lLh?")) != -1) { + while ((opt = getopt(argc, argv, "t:cias:b:B:lLh?")) != -1) { switch (opt) { case 't': timestamp = optarg[0]; @@ -225,8 +226,12 @@ int main(int argc, char **argv) color++; break; + case 'i': + view |= CANLIB_VIEW_BINARY; + break; + case 'a': - ascii = 1; + view |= CANLIB_VIEW_ASCII; break; case 's': @@ -544,7 +549,7 @@ int main(int argc, char **argv) printf("%*s", max_devname_len, devname[idx]); printf("%s ", (color==1)?col_off:""); - fprint_long_canframe(stdout, &frame, NULL, ascii); + fprint_long_canframe(stdout, &frame, NULL, view); printf("%s", (color>1)?col_off:""); printf("\n"); diff --git a/lib.c b/lib.c index 9e8a4e4..6d87fae 100644 --- a/lib.c +++ b/lib.c @@ -57,7 +57,7 @@ #define DATA_SEPERATOR '.' #define MAX_CANFRAME "12345678#01.23.45.67.89.AB.CD.EF" -#define MAX_LONG_CANFRAME "12345678 [8] 01 23 45 67 89 AB CD EF '........'" +#define MAX_LONG_CANFRAME "12345678 [8] 10101010 10101010 10101010 10101010 10101010 10101010 10101010 10101010 '........'" static int asc2nibble(char c) { @@ -177,21 +177,21 @@ void sprint_canframe(char *buf , struct can_frame *cf, int sep) { } -void fprint_long_canframe(FILE *stream , struct can_frame *cf, char *eol, int ascii) { +void fprint_long_canframe(FILE *stream , struct can_frame *cf, char *eol, int view) { /* documentation see lib.h */ char buf[sizeof(MAX_LONG_CANFRAME)+1]; /* max length */ - sprint_long_canframe(buf, cf, ascii); + sprint_long_canframe(buf, cf, view); fprintf(stream, "%s", buf); if (eol) fprintf(stream, "%s", eol); } -void sprint_long_canframe(char *buf , struct can_frame *cf, int ascii) { +void sprint_long_canframe(char *buf , struct can_frame *cf, int view) { /* documentation see lib.h */ - int i, offset; + int i, j, dlen, offset; if (cf->can_id & CAN_ERR_FLAG) { sprintf(buf, "%8X ", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG)); @@ -207,26 +207,41 @@ void sprint_long_canframe(char *buf , struct can_frame *cf, int ascii) { sprintf(buf+offset, "[%d]", cf->can_dlc); offset += 3; - if (cf->can_id & CAN_RTR_FLAG) /* there are no ERR frames with RTR */ + if (cf->can_id & CAN_RTR_FLAG) { /* there are no ERR frames with RTR */ sprintf(buf+offset, " remote request"); - else { + return; + } + + if (view & CANLIB_VIEW_BINARY) { + dlen = 9; /* _10101010 */ + for (i = 0; i < cf->can_dlc; i++) { + buf[offset++] = ' '; + for (j = 7; j >= 0; j--) + buf[offset++] = (1<data[i])?'1':'0'; + } + buf[offset] = 0; /* terminate string */ + } else { + dlen = 3; /* _AA */ for (i = 0; i < cf->can_dlc; i++) { sprintf(buf+offset, " %02X", cf->data[i]); - offset += 3; + offset += dlen; } - if (cf->can_id & CAN_ERR_FLAG) - sprintf(buf+offset, "%*s", 3*(8-cf->can_dlc)+13, "ERRORFRAME"); - else if (ascii) { - sprintf(buf+offset, "%*s", 3*(8-cf->can_dlc)+4, "'"); - offset += 3*(8-cf->can_dlc)+4; - - for (i = 0; i < cf->can_dlc; i++) - if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F)) - buf[offset++] = cf->data[i]; - else - buf[offset++] = '.'; - sprintf(buf+offset, "'"); - } } + + if (cf->can_id & CAN_ERR_FLAG) + sprintf(buf+offset, "%*s", dlen*(8-cf->can_dlc)+13, "ERRORFRAME"); + else if (view & CANLIB_VIEW_ASCII) { + j = dlen*(8-cf->can_dlc)+4; + sprintf(buf+offset, "%*s", j, "'"); + offset += j; + + for (i = 0; i < cf->can_dlc; i++) + if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F)) + buf[offset++] = cf->data[i]; + else + buf[offset++] = '.'; + + sprintf(buf+offset, "'"); + } } diff --git a/lib.h b/lib.h index 929ad55..80dbf45 100644 --- a/lib.h +++ b/lib.h @@ -96,8 +96,11 @@ void sprint_canframe(char *buf , struct can_frame *cf, int sep); * */ -void fprint_long_canframe(FILE *stream , struct can_frame *cf, char *eol, int ascii); -void sprint_long_canframe(char *buf , struct can_frame *cf, int ascii); +#define CANLIB_VIEW_ASCII 0x1 +#define CANLIB_VIEW_BINARY 0x2 + +void fprint_long_canframe(FILE *stream , struct can_frame *cf, char *eol, int view); +void sprint_long_canframe(char *buf , struct can_frame *cf, int view); /* * Creates a CAN frame hexadecimal output in user readable format. *