]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blobdiff - cangw.c
Added isotpserver in autotool config file and gitignore
[sojka/can-utils.git] / cangw.c
diff --git a/cangw.c b/cangw.c
index d8757e3c01bcf07b3838ca9fe9cbeeab2981224d..ee063e9c15ff6c6471fa2f54cae8719057418a1e 100644 (file)
--- a/cangw.c
+++ b/cangw.c
@@ -135,21 +135,54 @@ void print_cs_xor(struct cgw_csum_xor *cs_xor)
 {
        printf("-x %d:%d:%d:%02X ",
               cs_xor->from_idx, cs_xor->to_idx,
-              cs_xor->result_idx, cs_xor->prefix_value);
+              cs_xor->result_idx, cs_xor->init_xor_val);
+}
+
+void print_cs_crc8_profile(struct cgw_csum_crc8 *cs_crc8)
+{
+       int i;
+
+       printf("-p %d:", cs_crc8->profile);
+
+       switch (cs_crc8->profile) {
+
+       case  CGW_CRC8PRF_1U8:
+
+               printf("%02X", cs_crc8->profile_data[0]);
+               break;
+
+       case  CGW_CRC8PRF_16U8:
+
+               for (i = 0; i < 16; i++)
+                       printf("%02X", cs_crc8->profile_data[i]);
+               break;
+
+       case  CGW_CRC8PRF_SFFID_XOR:
+               break;
+
+       default:
+               printf("<unknown profile #%d>", cs_crc8->profile);
+       }
+
+       printf(" ");
 }
 
 void print_cs_crc8(struct cgw_csum_crc8 *cs_crc8)
 {
        int i;
 
-       printf("-c %d:%d:%d:",
+       printf("-c %d:%d:%d:%02X:%02X:",
               cs_crc8->from_idx, cs_crc8->to_idx,
-              cs_crc8->result_idx);
+              cs_crc8->result_idx, cs_crc8->init_crc_val,
+              cs_crc8->final_xor_val);
 
        for (i = 0; i < 256; i++)
                printf("%02X", cs_crc8->crctab[i]);
 
        printf(" ");
+
+       if (cs_crc8->profile != CGW_CRC8PRF_UNSPEC)
+               print_cs_crc8_profile(cs_crc8);
 }
 
 void print_usage(char *prg)
@@ -165,6 +198,9 @@ void print_usage(char *prg)
        fprintf(stderr, "           -e (echo sent frames - recommended on vcanx)\n");
        fprintf(stderr, "           -f <filter> (set CAN filter)\n");
        fprintf(stderr, "           -m <mod> (set frame modifications)\n");
+       fprintf(stderr, "           -x <from_idx>:<to_idx>:<result_idx>:<init_xor_val> (XOR checksum)\n");
+       fprintf(stderr, "           -c <from>:<to>:<result>:<init_val>:<xor_val>:<crctab[256]> (CRC8 cs)\n");
+       fprintf(stderr, "           -p <profile>:[<profile_data>] (CRC8 checksum profile & parameters)\n");
        fprintf(stderr, "\nValues are given and expected in hexadecimal values. Leading 0s can be omitted.\n");
        fprintf(stderr, "\n");
        fprintf(stderr, "<filter> is a <value>:<mask> CAN identifier filter\n");
@@ -181,6 +217,11 @@ void print_usage(char *prg)
        fprintf(stderr, "Example:\n");
        fprintf(stderr, "%s -A -s can0 -d vcan3 -e -f 123:C00007FF -m SET:IL:333.4.1122334455667788\n", prg);
        fprintf(stderr, "\n");
+       fprintf(stderr, "Supported CRC 8 profiles:\n");
+       fprintf(stderr, "Profile '%d' (1U8)       - add one additional u8 value\n", CGW_CRC8PRF_1U8);
+       fprintf(stderr, "Profile '%d' (16U8)      - add u8 value from table[16] indexed by (data[1] & 0xF)\n", CGW_CRC8PRF_16U8);
+       fprintf(stderr, "Profile '%d' (SFFID_XOR) - add u8 value (can_id & 0xFF) ^ (can_id >> 8 & 0xFF)\n", CGW_CRC8PRF_SFFID_XOR);
+       fprintf(stderr, "\n");
 }
 
 int b64hex(char *asc, unsigned char *bin, int len)
@@ -194,6 +235,45 @@ int b64hex(char *asc, unsigned char *bin, int len)
        return 0;
 }
 
+int parse_crc8_profile(char *optarg, struct cgw_csum_crc8 *crc8)
+{
+       int ret = 1;
+       char *ptr;
+
+       if (sscanf(optarg, "%hhd:", &crc8->profile) != 1)
+               return ret;
+
+       switch (crc8->profile) {
+
+       case  CGW_CRC8PRF_1U8:
+
+               if (sscanf(optarg, "%hhd:%2hhx", &crc8->profile, &crc8->profile_data[0]) == 2)
+                       ret = 0;
+
+               break;
+
+       case  CGW_CRC8PRF_16U8:
+
+               ptr = strchr(optarg, ':');
+
+               /* check if length contains 16 base64 hex values */
+               if (ptr != NULL &&
+                   strlen(ptr) == strlen(":00112233445566778899AABBCCDDEEFF") &&
+                   b64hex(ptr+1, (unsigned char *)&crc8->profile_data[0], 16) == 0)
+                       ret = 0;
+
+               break;
+
+       case  CGW_CRC8PRF_SFFID_XOR:
+
+               /* no additional parameters needed */
+               ret = 0;
+               break;
+       }
+
+       return ret;
+}
+
 int parse_mod(char *optarg, struct modattr *modmsg)
 {
        char *ptr, *nptr;
@@ -246,18 +326,25 @@ int parse_mod(char *optarg, struct modattr *modmsg)
                ptr++;
        }
 
-       if ((sscanf(++ptr, "%lx.%hhd.%16s",
-                   (long unsigned int *)&modmsg->cf.can_id,
-                   (unsigned char *)&modmsg->cf.can_dlc, hexdata) != 3) ||
-           (modmsg->cf.can_dlc > 8))
+       if (sscanf(++ptr, "%lx.%hhx.%16s",
+                  (long unsigned int *)&modmsg->cf.can_id,
+                  (unsigned char *)&modmsg->cf.can_dlc, hexdata) != 3)
                return 5;
 
-       if (strlen(hexdata) != 16)
+       /* 4-bit masks can have values from 0 to 0xF */ 
+       if (modmsg->cf.can_dlc > 0xF)
                return 6;
 
-       if (b64hex(hexdata, &modmsg->cf.data[0], 8))
+       /* but when setting CAN_DLC the value has to be limited to 8 */
+       if (modmsg->instruction == CGW_MOD_SET && modmsg->cf.can_dlc > 8)
                return 7;
 
+       if (strlen(hexdata) != 16)
+               return 8;
+
+       if (b64hex(hexdata, &modmsg->cf.data[0], 8))
+               return 9;
+
        return 0; /* ok */
 }
 
@@ -461,8 +548,10 @@ int main(int argc, char **argv)
        int i;
 
        memset(&req, 0, sizeof(req));
+       memset(&cs_xor, 0, sizeof(cs_xor));
+       memset(&cs_crc8, 0, sizeof(cs_crc8));
 
-       while ((opt = getopt(argc, argv, "ADFLs:d:tef:c:x:m:?")) != -1) {
+       while ((opt = getopt(argc, argv, "ADFLs:d:tef:c:p:x:m:?")) != -1) {
                switch (opt) {
 
                case 'A':
@@ -515,7 +604,7 @@ int main(int argc, char **argv)
                case 'x':
                        if (sscanf(optarg, "%hhd:%hhd:%hhd:%hhx",
                                   &cs_xor.from_idx, &cs_xor.to_idx,
-                                  &cs_xor.result_idx, &cs_xor.prefix_value) == 4) {
+                                  &cs_xor.result_idx, &cs_xor.init_xor_val) == 4) {
                                have_cs_xor = 1;
                        } else {
                                printf("Bad XOR checksum definition '%s'.\n", optarg);
@@ -524,9 +613,10 @@ int main(int argc, char **argv)
                        break;
 
                case 'c':
-                       if ((sscanf(optarg, "%hhd:%hhd:%hhd:%512s",
+                       if ((sscanf(optarg, "%hhd:%hhd:%hhd:%hhx:%hhx:%512s",
                                    &cs_crc8.from_idx, &cs_crc8.to_idx,
-                                   &cs_crc8.result_idx, crc8tab) == 4) &&
+                                   &cs_crc8.result_idx, &cs_crc8.init_crc_val,
+                                   &cs_crc8.final_xor_val, crc8tab) == 6) &&
                            (strlen(crc8tab) == 512) &&
                            (b64hex(crc8tab, (unsigned char *)&cs_crc8.crctab, 256) == 0)) {
                                have_cs_crc8 = 1;
@@ -536,6 +626,13 @@ int main(int argc, char **argv)
                        }
                        break;
 
+               case 'p':
+                       if (parse_crc8_profile(optarg, &cs_crc8)) {
+                               printf("Bad CRC8 profile definition '%s'.\n", optarg);
+                               exit(1);
+                       }
+                       break;
+
                case 'm':
                        /* may be triggered by each of the CGW_MOD_FUNCS functions */
                        if ((modidx < CGW_MOD_FUNCS) && (err = parse_mod(optarg, &modmsg[modidx++]))) {
@@ -616,12 +713,12 @@ int main(int argc, char **argv)
        if (have_filter)
                addattr_l(&req.nh, sizeof(req), CGW_FILTER, &filter, sizeof(filter));
 
-       if (have_cs_xor)
-               addattr_l(&req.nh, sizeof(req), CGW_CS_XOR, &cs_xor, sizeof(cs_xor));
-
        if (have_cs_crc8)
                addattr_l(&req.nh, sizeof(req), CGW_CS_CRC8, &cs_crc8, sizeof(cs_crc8));
 
+       if (have_cs_xor)
+               addattr_l(&req.nh, sizeof(req), CGW_CS_XOR, &cs_xor, sizeof(cs_xor));
+
        /*
         * a better example code
         * modmsg.modtype = CGW_MOD_ID;
@@ -645,7 +742,7 @@ int main(int argc, char **argv)
        }
 
        /* clean netlink receive buffer */
-       bzero(rxbuf, sizeof(rxbuf));
+       memset(rxbuf, 0x0, sizeof(rxbuf));
 
        if (cmd != LIST) {