]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blobdiff - tc/em_canid.c
em_canid: Minor fixes.
[lisovros/iproute2_canprio.git] / tc / em_canid.c
index 20ec90070eb509598c96cd4c254df089f882bf14..052dca39f04433786a2c68390d5b6b0d0481c899 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * em_canid.c  Ematch rule to match CAN frames according to their CAN IDs
+ * em_canid.c  Ematch rule to match CAN frames according to their CAN identifiers
  *
- *              This program is free software; you can distribute it and/or
- *              modify it under the terms of the GNU General Public License
- *              as published by the Free Software Foundation; either version
- *              2 of the License, or (at your option) any later version.
+ *             This program is free software; you can distribute it and/or
+ *             modify it under the terms of the GNU General Public License
+ *             as published by the Free Software Foundation; either version
+ *             2 of the License, or (at your option) any later version.
  *
  * Idea:       Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
  * Copyright:  (c) 2011 Czech Technical University in Prague
@@ -29,7 +29,7 @@
 #include <inttypes.h>
 #include "m_ematch.h"
 
-#define EM_CAN_RULES_MAX 400 /* Main reason for this number is Nelink
+#define EM_CANID_RULES_MAX 400 /* Main reason for this number is Nelink
        message size limit equal to Single memory page size. When dump()
        is invoked, there are even some ematch related headers sent from
        kernel to userspace together with em_canid configuration --
@@ -38,8 +38,8 @@
 extern struct ematch_util canid_ematch_util;
 struct rules {
        struct can_filter *rules_raw;
-       int rules_capacity;
-       int rules_cnt;
+       int rules_capacity;     /* Size of array allocated for rules_raw */
+       int rules_cnt;          /* Actual number of rules stored in rules_raw */
 };
 
 static void canid_print_usage(FILE *fd)
@@ -66,9 +66,9 @@ static int canid_parse_rule(struct rules *rules, struct bstr *a, int iseff)
                }
        }
 
-       /* Stretch rules array up to EM_CAN_RULES_MAX if necessary */
+       /* Stretch rules array up to EM_CANID_RULES_MAX if necessary */
        if (rules->rules_cnt == rules->rules_capacity) {
-               if (rules->rules_capacity <= EM_CAN_RULES_MAX/2) {
+               if (rules->rules_capacity <= EM_CANID_RULES_MAX/2) {
                        rules->rules_capacity *= 2;
                        rules->rules_raw = realloc(rules->rules_raw,
                                sizeof(struct can_filter) * rules->rules_capacity);
@@ -90,17 +90,16 @@ static int canid_parse_rule(struct rules *rules, struct bstr *a, int iseff)
 static int canid_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr,
                          struct bstr *args)
 {
-       struct bstr *a;
        int iseff = 0;
        int ret = 0;
        struct rules rules = {
-               .rules_capacity = 25, /* Denominator of EM_CAN_RULES_MAX
+               .rules_capacity = 25, /* Denominator of EM_CANID_RULES_MAX
                        Will be multiplied by 2 to calculate the size for realloc() */
                .rules_cnt = 0
        };
 
 #define PARSE_ERR(CARG, FMT, ARGS...) \
-       em_parse_error(EINVAL, args, CARG, &canid_ematch_util, FMT ,##ARGS)
+       em_parse_error(EINVAL, args, CARG, &canid_ematch_util, FMT##ARGS)
 
        if (args == NULL)
                return PARSE_ERR(args, "canid: missing arguments");
@@ -129,7 +128,7 @@ static int canid_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr,
                        ret = PARSE_ERR(args, "canid: Improperly formed CAN ID & mask\n");
                        goto exit;
                } else if (ret == -2) {
-                       fprintf(stderr, "canid: Too many rules on input\n");
+                       ret = PARSE_ERR(args, "canid: Too many arguments on input\n");
                        goto exit;
                }
        } while ((args = bstr_next(args)) != NULL);
@@ -137,8 +136,8 @@ static int canid_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr,
        addraw_l(n, MAX_MSG, hdr, sizeof(*hdr));
        addraw_l(n, MAX_MSG, rules.rules_raw,
                sizeof(struct can_filter) * rules.rules_cnt);
-#undef PARSE_ERR
 
+#undef PARSE_ERR
 exit:
        free(rules.rules_raw);
        return ret;
@@ -151,20 +150,13 @@ static int canid_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
        int rules_count;
        int i;
 
-#if 0
-       if (data_len < sizeof(*conf)) {
-               fprintf(stderr, "U32 header size mismatch\n");
-               return -1;
-       }
-#endif
-
        rules_count = data_len / sizeof(struct can_filter);
 
        for (i = 0; i < rules_count; i++) {
                struct can_filter *pcfltr = &conf[i];
 
                if (pcfltr->can_id & CAN_EFF_FLAG) {
-                       if (pcfltr->can_mask == (CAN_EFF_FLAG|CAN_EFF_MASK))
+                       if (pcfltr->can_mask == (CAN_EFF_FLAG | CAN_EFF_MASK))
                                fprintf(fd, "eff 0x%"PRIX32,
                                                pcfltr->can_id & CAN_EFF_MASK);
                        else
@@ -172,7 +164,7 @@ static int canid_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
                                                pcfltr->can_id & CAN_EFF_MASK,
                                                pcfltr->can_mask & CAN_EFF_MASK);
                } else {
-                       if (pcfltr->can_mask == (CAN_EFF_FLAG|CAN_SFF_MASK))
+                       if (pcfltr->can_mask == (CAN_EFF_FLAG | CAN_SFF_MASK))
                                fprintf(fd, "sff 0x%"PRIX32,
                                                pcfltr->can_id & CAN_SFF_MASK);
                        else