Added binary output option to candump.c and sprint_long_canframe() in lib.c .

pull/7/head
Oliver Hartkopp 2008-06-02 20:06:37 +00:00
parent 789a021bb9
commit 8a5c0365e6
3 changed files with 50 additions and 27 deletions

View File

@ -102,6 +102,7 @@ void print_usage(char *prg)
fprintf(stderr, " (use CTRL-C to terminate %s)\n\n", prg); fprintf(stderr, " (use CTRL-C to terminate %s)\n\n", prg);
fprintf(stderr, "Options: -t <type> (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n"); fprintf(stderr, "Options: -t <type> (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
fprintf(stderr, " -c (increment color mode level)\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, " -a (enable additional ASCII output)\n");
fprintf(stderr, " -s <level> (silent mode - 1: animation 2: completely silent)\n"); fprintf(stderr, " -s <level> (silent mode - 1: animation 2: completely silent)\n");
fprintf(stderr, " -b <can> (bridge mode - send received frames to <can>)\n"); fprintf(stderr, " -b <can> (bridge mode - send received frames to <can>)\n");
@ -187,7 +188,7 @@ int main(int argc, char **argv)
unsigned char silent = 0; unsigned char silent = 0;
unsigned char silentani = 0; unsigned char silentani = 0;
unsigned char color = 0; unsigned char color = 0;
unsigned char ascii = 0; unsigned char view = 0;
unsigned char log = 0; unsigned char log = 0;
unsigned char logfrmt = 0; unsigned char logfrmt = 0;
int opt, ret; int opt, ret;
@ -209,7 +210,7 @@ int main(int argc, char **argv)
last_tv.tv_sec = 0; last_tv.tv_sec = 0;
last_tv.tv_usec = 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) { switch (opt) {
case 't': case 't':
timestamp = optarg[0]; timestamp = optarg[0];
@ -225,8 +226,12 @@ int main(int argc, char **argv)
color++; color++;
break; break;
case 'i':
view |= CANLIB_VIEW_BINARY;
break;
case 'a': case 'a':
ascii = 1; view |= CANLIB_VIEW_ASCII;
break; break;
case 's': case 's':
@ -544,7 +549,7 @@ int main(int argc, char **argv)
printf("%*s", max_devname_len, devname[idx]); printf("%*s", max_devname_len, devname[idx]);
printf("%s ", (color==1)?col_off:""); 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("%s", (color>1)?col_off:"");
printf("\n"); printf("\n");

41
lib.c
View File

@ -57,7 +57,7 @@
#define DATA_SEPERATOR '.' #define DATA_SEPERATOR '.'
#define MAX_CANFRAME "12345678#01.23.45.67.89.AB.CD.EF" #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) { 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 */ /* documentation see lib.h */
char buf[sizeof(MAX_LONG_CANFRAME)+1]; /* max length */ 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); fprintf(stream, "%s", buf);
if (eol) if (eol)
fprintf(stream, "%s", 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 */ /* documentation see lib.h */
int i, offset; int i, j, dlen, offset;
if (cf->can_id & CAN_ERR_FLAG) { if (cf->can_id & CAN_ERR_FLAG) {
sprintf(buf, "%8X ", cf->can_id & (CAN_ERR_MASK|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); sprintf(buf+offset, "[%d]", cf->can_dlc);
offset += 3; 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"); 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<<j & cf->data[i])?'1':'0';
}
buf[offset] = 0; /* terminate string */
} else {
dlen = 3; /* _AA */
for (i = 0; i < cf->can_dlc; i++) { for (i = 0; i < cf->can_dlc; i++) {
sprintf(buf+offset, " %02X", cf->data[i]); sprintf(buf+offset, " %02X", cf->data[i]);
offset += 3; offset += dlen;
} }
}
if (cf->can_id & CAN_ERR_FLAG) if (cf->can_id & CAN_ERR_FLAG)
sprintf(buf+offset, "%*s", 3*(8-cf->can_dlc)+13, "ERRORFRAME"); sprintf(buf+offset, "%*s", dlen*(8-cf->can_dlc)+13, "ERRORFRAME");
else if (ascii) { else if (view & CANLIB_VIEW_ASCII) {
sprintf(buf+offset, "%*s", 3*(8-cf->can_dlc)+4, "'"); j = dlen*(8-cf->can_dlc)+4;
offset += 3*(8-cf->can_dlc)+4; sprintf(buf+offset, "%*s", j, "'");
offset += j;
for (i = 0; i < cf->can_dlc; i++) for (i = 0; i < cf->can_dlc; i++)
if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F)) if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
buf[offset++] = cf->data[i]; buf[offset++] = cf->data[i];
else else
buf[offset++] = '.'; buf[offset++] = '.';
sprintf(buf+offset, "'"); sprintf(buf+offset, "'");
} }
} }
}

7
lib.h
View File

@ -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); #define CANLIB_VIEW_ASCII 0x1
void sprint_long_canframe(char *buf , struct can_frame *cf, int ascii); #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. * Creates a CAN frame hexadecimal output in user readable format.
* *