diff --git a/README.md b/README.md index d1de366..a46d069 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ subsystem (aka SocketCAN): * canplayer : replay CAN logfiles * cansend : send a single frame * cangen : generate (random) CAN traffic -* cansniffer : display CAN data content differences (just 11bit CAN IDs) +* cansniffer : display CAN data content differences ~~(just 11bit CAN IDs)~~ #### CAN access via IP sockets * canlogserver : log CAN frames from a remote/local host diff --git a/cansniffer.c b/cansniffer.c index b95f11b..e59811e 100644 --- a/cansniffer.c +++ b/cansniffer.c @@ -43,6 +43,7 @@ #include #include +#include #include #include #include @@ -60,13 +61,14 @@ #include #include -#include #include "terminal.h" #define SETFNAME "sniffset." #define ANYDEV "any" +#define MAX_SLOTS 2048 + /* flags */ #define ENABLE 1 /* by filter or user */ @@ -102,7 +104,7 @@ struct snif { struct can_frame current; struct can_frame marker; struct can_frame notch; -} sniftab[2048]; +} sniftab[MAX_SLOTS]; extern int optind, opterr, optopt; @@ -110,7 +112,6 @@ extern int optind, opterr, optopt; static int running = 1; static int clearscreen = 1; static int notch; -static int filter_id_only; static long timeout = TIMEOUT; static long hold = HOLD; static long loop = LOOP; @@ -119,54 +120,69 @@ static unsigned char binary_gap; static unsigned char color; static char *interface; -void rx_setup (int fd, int id); -void rx_delete (int fd, int id); -void print_snifline(int id); +void print_snifline(int slot); int handle_keyb(int fd); -int handle_bcm(int fd, long currcms); +int handle_frame(int fd, long currcms); int handle_timeo(int fd, long currcms); +int sniftab_index(canid_t id); void writesettings(char* name); void readsettings(char* name, int sockfd); +int idx = 0; + + +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) { const char manual [] = { "commands that can be entered at runtime:\n" "\n" - "q - quit\n" - "b - toggle binary / HEX-ASCII output\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" - "* - clear notched marked\n" - "rMYNAME - read settings file (filter/notch)\n" - "wMYNAME - write settings file (filter/notch)\n" - "+FILTER - add CAN-IDs to sniff\n" - "-FILTER - remove CAN-IDs to sniff\n" + "q - quit\n" + "b - toggle binary / HEX-ASCII output\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" + "* - clear notched marked\n" + "rMYNAME - read settings file (filter/notch)\n" + "wMYNAME - write settings file (filter/notch)\n" + "+FILTER - add CAN-IDs to sniff\n" + "-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" + "FILTER must be a single CAN-ID:\n"/* "or a CAN-ID/Bitmask:\n"*/ + "+1F5 - add CAN-ID 0x1F5\n" + "-42E - remove CAN-ID 0x42E\n" "\n" - "if (id & filter) == (sniff-id & filter) the action (+/-) is performed,\n" - "which is quite easy when the filter is 000\n" + "29 bit IDs:\n" + "+18FEDF55 - add CAN-ID 0x18FEDF55\n" + "-00000090 - remove CAN-ID 0x00000090\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"*/ +/* "if (id & filter) == (sniff-id & filter) the action (+/-) IS PERFORMED,\N" */ +/* "which is quite easy when the filter is 000\n"*/ "\n" }; fprintf(stderr, "\nUsage: %s [can-interface]\n", prg); - fprintf(stderr, "Options: -m (initial FILTER default 0x00000000)\n"); - fprintf(stderr, " -v (initial FILTER default 0x00000000)\n"); - fprintf(stderr, " -q (quiet - all IDs deactivated)\n"); +// fprintf(stderr, "Options: -m (initial FILTER default 0x00000000)\n"); +// fprintf(stderr, " -v (initial FILTER default 0x00000000)\n"); + fprintf(stderr, "Options: -q (quiet - all slots are deactivated)\n"); fprintf(stderr, " -r (read %sname from file)\n", SETFNAME); fprintf(stderr, " -b (start with binary mode)\n"); fprintf(stderr, " -B (start with binary mode with gap - exceeds 80 chars!)\n"); fprintf(stderr, " -c (color changes)\n"); - fprintf(stderr, " -f (filter on CAN-ID only)\n"); fprintf(stderr, " -t