From bd6ef44321323f6dbcf49016a606febbae015875 Mon Sep 17 00:00:00 2001 From: Oliver Hartkopp Date: Wed, 29 Apr 2020 09:48:52 +0200 Subject: [PATCH] cansniffer: support EFF frames with 29 bit CAN IDs Convert cansniffer from CAN_BCM to CAN_RAW sockets and handle the content filter in user space. Conversion CAN_RAW including slot concept and sorting contributed by coryjfowler. Functional changes visible to users: - New format for settings file - New command '8' to switch in a compact binary view for 80 columns view - New head line layout - The gap time between CAN frames is displayed in milli seconds (ms) - The interactive filters support 29 bit CAN IDs Signed-off-by: Oliver Hartkopp --- cansniffer.c | 418 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 259 insertions(+), 159 deletions(-) diff --git a/cansniffer.c b/cansniffer.c index fe0fd93..41259fb 100644 --- a/cansniffer.c +++ b/cansniffer.c @@ -44,6 +44,8 @@ #include #include +#include +#include #include #include #include @@ -61,13 +63,14 @@ #include #include -#include +#include #include #include "terminal.h" #define SETFNAME "sniffset." #define ANYDEV "any" +#define MAX_SLOTS 2048 /* flags */ @@ -92,9 +95,10 @@ #define ATTCOLOR ATTBOLD FGRED -#define STARTLINESTR "XX delta ID data ... " +#define LDL " | " /* long delimiter */ +#define SDL "|" /* short delimiter for binary on 80 chars terminal */ -struct snif { +static struct snif { int flags; long hold; long timeout; @@ -104,30 +108,54 @@ struct snif { struct can_frame current; struct can_frame marker; struct can_frame notch; -} sniftab[2048]; - +} sniftab[MAX_SLOTS]; extern int optind, opterr, optopt; +static int idx; static int running = 1; static int clearscreen = 1; +static int print_eff; static int notch; static long timeout = TIMEOUT; static long hold = HOLD; static long loop = LOOP; static unsigned char binary; +static unsigned char binary8; static unsigned char binary_gap; static unsigned char color; static char *interface; +static char *vdl = LDL; /* variable delimiter */ +static char *ldl = LDL; /* long delimiter */ -void rx_setup (int fd, int id); -void rx_delete (int fd, int id); void print_snifline(int id); int handle_keyb(int fd); int handle_frame(int fd, long currcms); -int handle_timeo(int fd, long currcms); +int handle_timeo(long currcms); void writesettings(char* name); -int readsettings(char* name, int sockfd); +int readsettings(char* name); +int sniftab_index(canid_t id); + +void switchvdl(char *delim) +{ + /* reduce delimiter size for EFF IDs in binary display of up + to 8 data bytes payload to fit into 80 chars per line */ + if (binary8) + vdl = delim; +} + +int comp (const void * elem1, const void * elem2) +{ + unsigned long f = ((struct snif*)elem1)->current.can_id; + unsigned long s = ((struct snif*)elem2)->current.can_id; + + if (f > s) + return 1; + if (f < s) + return -1; + + return 0; +} void print_usage(char *prg) { @@ -135,6 +163,7 @@ void print_usage(char *prg) "commands that can be entered at runtime:\n" " q - quit\n" " b - toggle binary / HEX-ASCII output\n" + " 8 - toggle binary / HEX-ASCII output (small for EFF on 80 chars)\n" " B - toggle binary with gap / HEX-ASCII output (exceeds 80 chars!)\n" " c - toggle color mode\n" " # - notch currently marked/changed bits (can be used repeatedly)\n" @@ -145,16 +174,29 @@ void print_usage(char *prg) " -FILTER - remove CAN-IDs to sniff\n" "\n" "FILTER can be a single CAN-ID or a CAN-ID/Bitmask:\n" - " +1F5 - add CAN-ID 0x1F5\n" - " -42E - remove CAN-ID 0x42E\n" - " -42E7FF - remove CAN-ID 0x42E (using Bitmask)\n" - " -500700 - remove CAN-IDs 0x500 - 0x5FF\n" - " +400600 - add CAN-IDs 0x400 - 0x5FF\n" - " +000000 - add all CAN-IDs\n" - " -000000 - remove all CAN-IDs\n" + "\n" + " single SFF 11 bit IDs:\n" + " +1F5 - add SFF CAN-ID 0x1F5\n" + " -42E - remove SFF CAN-ID 0x42E\n" + "\n" + " single EFF 29 bit IDs:\n" + " +18FEDF55 - add EFF CAN-ID 0x18FEDF55\n" + " -00000090 - remove EFF CAN-ID 0x00000090\n" + "\n" + " CAN-ID/Bitmask SFF:\n" + " -42E7FF - remove SFF CAN-ID 0x42E (using Bitmask)\n" + " -500700 - remove SFF CAN-IDs 0x500 - 0x5FF\n" + " +400600 - add SFF CAN-IDs 0x400 - 0x5FF\n" + " +000000 - add all SFF CAN-IDs\n" + " -000000 - remove all SFF CAN-IDs\n" + "\n" + " CAN-ID/Bitmask EFF:\n" + " -0000000000000000 - remove all EFF CAN-IDs\n" + " +12345678000000FF - add EFF CAN IDs xxxxxx78\n" + " +0000000000000000 - add all EFF CAN-IDs\n" "\n" "if (id & filter) == (sniff-id & filter) the action (+/-) is performed,\n" - "which is quite easy when the filter is 000\n" + "which is quite easy when the filter is 000 resp. 00000000 for EFF.\n" "\n" }; @@ -162,12 +204,15 @@ void print_usage(char *prg) fprintf(stderr, "Options:\n"); fprintf(stderr, " -q (quiet - all IDs deactivated)\n"); fprintf(stderr, " -r (read %sname from file)\n", SETFNAME); + fprintf(stderr, " -e (fix extended frame format output - no auto detect)\n"); fprintf(stderr, " -b (start with binary mode)\n"); + fprintf(stderr, " -8 (start with binary mode - for EFF on 80 chars)\n"); fprintf(stderr, " -B (start with binary mode with gap - exceeds 80 chars!)\n"); fprintf(stderr, " -c (color changes)\n"); fprintf(stderr, " -t