]> rtime.felk.cvut.cz Git - sojka/can-utils.git/commitdiff
Added binary output option to candump.c and sprint_long_canframe() in lib.c .
authorOliver Hartkopp <socketcan@hartkopp.net>
Mon, 2 Jun 2008 20:06:37 +0000 (20:06 +0000)
committerOliver Hartkopp <socketcan@hartkopp.net>
Mon, 2 Jun 2008 20:06:37 +0000 (20:06 +0000)
candump.c
lib.c
lib.h

index f0b50edb5dc17de36ee6f31c5a3419cfd2f3f70b..64e6ace3b4b99e2950ed09c56dcdc49c657c6bf0 100644 (file)
--- 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 <type>   (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 <level>  (silent mode - 1: animation 2: completely silent)\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 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 9e8a4e42ca4edb0ba9adeb98c00c7082cfed8886..6d87fae5c685bd2999930ce246149c0472fc3090 100644 (file)
--- 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<<j & cf->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 929ad551bdaf1489e3dd5775d5873e81ffd812f5..80dbf457f4bf2626b258b7a47927b884633d0ad7 100644 (file)
--- 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.
  *