From badba486df0153f716a345692faa1b022376a5e9 Mon Sep 17 00:00:00 2001 From: Rostislav Lisovy Date: Thu, 14 Jun 2012 16:21:38 +0200 Subject: [PATCH] em_canid: Dump function --- tc/em_canid.c | 79 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 21 deletions(-) diff --git a/tc/em_canid.c b/tc/em_canid.c index 83af577..80d6f7b 100644 --- a/tc/em_canid.c +++ b/tc/em_canid.c @@ -1,12 +1,18 @@ /* - * em_canid.c + * em_canid.c Ematch rule to match CAN frames according to their CAN IDs * - * 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. * - * Authors: Rostislav Lisovy + * Idea: Oliver Hartkopp + * Copyright: (c) 2011 Czech Technical University in Prague + * (c) 2011 Volkswagen Group Research + * Authors: Michal Sojka + * Pavel Pisa + * Rostislav Lisovy + * Funded by: Volkswagen Group Research */ #include @@ -45,8 +51,9 @@ static int canid_parse_rule(struct can_filter *rule, struct bstr *a, int iseff) if (sscanf(a->data, "%"SCNx32 ":" "%"SCNx32, &can_id, &can_mask) != 2) { if (sscanf(a->data, "%"SCNx32, &can_id) != 1) { return -1; - } else - can_mask = CAN_SFF_MASK; + } else { + can_mask = (iseff) ? CAN_EFF_MASK : CAN_SFF_MASK; + } } rule->can_id = can_id | ((iseff) ? CAN_EFF_FLAG : 0); rule->can_mask = can_mask | ((iseff) ? CAN_EFF_FLAG : 0); @@ -69,22 +76,22 @@ static int canid_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr, em_parse_error(EINVAL, args, CARG, &canid_ematch_util, FMT ,##ARGS) if (args == NULL) - return PARSE_ERR(args, "can: missing arguments"); + return PARSE_ERR(args, "canid: missing arguments"); if (!bstrcmp(args, "sff")) iseff = 0; else if (!bstrcmp(args, "eff")) iseff = 1; else - return PARSE_ERR(args, "can: invalid key"); + return PARSE_ERR(args, "canid: invalid key"); a = bstr_next(args); if (a == NULL) - return PARSE_ERR(a, "can: missing key"); + return PARSE_ERR(a, "canid: missing key"); ret = canid_parse_rule(&rules_raw[0], a, iseff); if (ret == -1) - return PARSE_ERR(a, "can: Improperly formed CAN ID & mask\n"); + return PARSE_ERR(a, "canid: Improperly formed CAN ID & mask\n"); for (i = 1; i < (sizeof(rules_raw)/sizeof(struct can_filter)); i++) { /* There is no another keyword to parse */ @@ -93,18 +100,25 @@ static int canid_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr, break; } + if (!bstrcmp(a, "sff")) + iseff = 0; + else if (!bstrcmp(a, "eff")) + iseff = 1; + else + return PARSE_ERR(args, "canid: invalid key"); + a = bstr_next(a); if (a == NULL) - return PARSE_ERR(a, "can: missing argument"); + return PARSE_ERR(a, "canid: missing argument"); ret = canid_parse_rule(&rules_raw[i], a, iseff); if (ret == -1) - return PARSE_ERR(a, "can: Improperly formed CAN ID & mask\n"); + return PARSE_ERR(a, "canid: Improperly formed CAN ID & mask\n"); } // FIX THIS !@#$% //if (a->next) - // return PARSE_ERR(a->next, "can: unexpected trailer"); + // return PARSE_ERR(a->next, "canid: unexpected trailer"); addraw_l(n, MAX_MSG, hdr, sizeof(*hdr)); addraw_l(n, MAX_MSG, &rules_raw, sizeof(struct can_filter) * i); // FIXME is size ok @@ -117,20 +131,43 @@ static int canid_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data, int data_len) { struct can_filter *conf = data; /* Array with rules, fixed size EM_CAN_RULES_SIZE */ + int rules_count; + int i; #if 0 if (data_len < sizeof(*conf)) { fprintf(stderr, "U32 header size mismatch\n"); return -1; } - - fprintf(fd, "%08x/%08x at %s%d", - (unsigned int) ntohl(u_key->val), - (unsigned int) ntohl(u_key->mask), - u_key->offmask ? "nexthdr+" : "", - u_key->off); #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)) + fprintf(fd, "effid 0x%"PRIX32, + pcfltr->can_id & CAN_EFF_MASK); + else + fprintf(fd, "effid 0x%"PRIX32":0x%"PRIX32, + pcfltr->can_id & CAN_EFF_MASK, + pcfltr->can_mask & CAN_EFF_MASK); + } else { + if (pcfltr->can_mask == CAN_SFF_MASK) + fprintf(fd, "sffid 0x%"PRIX32, + pcfltr->can_id); + else + fprintf(fd, "sffid 0x%"PRIX32":0x%"PRIX32, + pcfltr->can_id, + pcfltr->can_mask); + } + + if ((i + 1) < rules_count) + fprintf(fd, " "); + } + return 0; } -- 2.39.2