]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blobdiff - lib.c
Added new socketoptions to force the isotp protocol to intentionally
[sojka/can-utils.git] / lib.c
diff --git a/lib.c b/lib.c
index c969df45337e155dc82f37fa561f17f9c9e1cc98..0b9148817b1e72d33b45f083d994582cc9a6b57f 100644 (file)
--- a/lib.c
+++ b/lib.c
@@ -114,10 +114,16 @@ int parse_canframe(char *cs, struct can_frame *cf) {
        if (len < 4)
                return 1;
 
-       if (!((cs[3] == CANID_DELIM) || (cs[8] == CANID_DELIM)))
-               return 1;
+       if (cs[3] == CANID_DELIM) { /* 3 digits */
 
-       if (cs[8] == CANID_DELIM) { /* 8 digits */
+               idx = 4;
+               for (i=0; i<3; i++){
+                       if ((tmp = asc2nibble(cs[i])) > 0x0F)
+                               return 1;
+                       cf->can_id |= (tmp << (2-i)*4);
+               }
+
+       } else if (cs[8] == CANID_DELIM) { /* 8 digits */
 
                idx = 9;
                for (i=0; i<8; i++){
@@ -128,15 +134,8 @@ int parse_canframe(char *cs, struct can_frame *cf) {
                if (!(cf->can_id & CAN_ERR_FLAG)) /* 8 digits but no errorframe?  */
                        cf->can_id |= CAN_EFF_FLAG;   /* then it is an extended frame */
 
-       } else { /* 3 digits */
-
-               idx = 4;
-               for (i=0; i<3; i++){
-                       if ((tmp = asc2nibble(cs[i])) > 0x0F)
-                               return 1;
-                       cf->can_id |= (tmp << (2-i)*4);
-               }
-       }
+       } else
+               return 1;
 
        if((cs[idx] == 'R') || (cs[idx] == 'r')){ /* RTR frame */
                cf->can_id |= CAN_RTR_FLAG;
@@ -244,17 +243,34 @@ void sprint_long_canframe(char *buf , struct can_frame *cf, int view) {
 
        if (view & CANLIB_VIEW_BINARY) {
                dlen = 9; /* _10101010 */
-               for (i = 0; i < dlc; i++) {
-                       buf[offset++] = ' ';
-                       for (j = 7; j >= 0; j--)
-                               buf[offset++] = (1<<j & cf->data[i])?'1':'0';
+               if (view & CANLIB_VIEW_SWAP) {
+                       for (i = dlc - 1; i >= 0; i--) {
+                               buf[offset++] = (i == dlc-1)?' ':SWAP_DELIMITER;
+                               for (j = 7; j >= 0; j--)
+                                       buf[offset++] = (1<<j & cf->data[i])?'1':'0';
+                       }
+               } else {
+                       for (i = 0; i < 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 < dlc; i++) {
-                       sprintf(buf+offset, " %02X", cf->data[i]);
-                       offset += dlen;
+               if (view & CANLIB_VIEW_SWAP) {
+                       for (i = dlc - 1; i >= 0; i--) {
+                               sprintf(buf+offset, "%c%02X",
+                                       (i == dlc-1)?' ':SWAP_DELIMITER,
+                                       cf->data[i]);
+                               offset += dlen;
+                       }
+               } else {
+                       for (i = 0; i < dlc; i++) {
+                               sprintf(buf+offset, " %02X", cf->data[i]);
+                               offset += dlen;
+                       }
                }
        }
 
@@ -262,16 +278,27 @@ void sprint_long_canframe(char *buf , struct can_frame *cf, int view) {
                sprintf(buf+offset, "%*s", dlen*(8-dlc)+13, "ERRORFRAME");
        else if (view & CANLIB_VIEW_ASCII) {
                j = dlen*(8-dlc)+4;
-               sprintf(buf+offset, "%*s", j, "'");
-               offset += j;
-
-               for (i = 0; i < dlc; i++)
-                       if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
-                               buf[offset++] = cf->data[i];
-                       else
-                               buf[offset++] = '.';
-
-               sprintf(buf+offset, "'");
+               if (view & CANLIB_VIEW_SWAP) {
+                       sprintf(buf+offset, "%*s", j, "`");
+                       offset += j;
+                       for (i = dlc - 1; i >= 0; i--)
+                               if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
+                                       buf[offset++] = cf->data[i];
+                               else
+                                       buf[offset++] = '.';
+
+                       sprintf(buf+offset, "`");
+               } else {
+                       sprintf(buf+offset, "%*s", j, "'");
+                       offset += j;
+                       for (i = 0; i < dlc; i++)
+                               if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
+                                       buf[offset++] = cf->data[i];
+                               else
+                                       buf[offset++] = '.';
+
+                       sprintf(buf+offset, "'");
+               }
        } 
 }