diff --git a/cansniffer.c b/cansniffer.c index b388235..cb009c1 100644 --- a/cansniffer.c +++ b/cansniffer.c @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */ /* - * cansniffer.c + * cansniffer.c - volatile CAN content visualizer * * Copyright (c) 2002-2007 Volkswagen Group Electronic Research * All rights reserved. @@ -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,31 +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 int filter_id_only; 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); +void print_snifline(int slot); int handle_keyb(int fd); -int handle_bcm(int fd, long currcms); -int handle_timeo(int fd, long currcms); +int handle_frame(int fd, long currcms); +int handle_timeo(long currcms); void writesettings(char* name); -void 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) { @@ -136,42 +163,62 @@ 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" + " - force a clear screen\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" + " a - enable 'a'll SFF CAN-IDs to sniff\n" + " n - enable 'n'one SFF CAN-IDs to sniff\n" + " A - enable 'A'll EFF CAN-IDs to sniff\n" + " N - enable 'N'one EFF CAN-IDs to sniff\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" + "\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" }; + fprintf(stderr, "%s - volatile CAN content visualizer.\n", prg); fprintf(stderr, "\nUsage: %s [can-interface]\n", prg); fprintf(stderr, "Options:\n"); - fprintf(stderr, " -m (initial FILTER default 0x00000000)\n"); - fprintf(stderr, " -v (initial FILTER default 0x00000000)\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, " -f (filter on CAN-ID only)\n"); fprintf(stderr, " -t