diff --git a/CMakeLists.txt b/CMakeLists.txt index 7088c51..a4e8484 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ set(PROGRAMS_CANLIB cansequence log2asc log2long + slcanpty ) set(PROGRAMS_J1939 @@ -58,7 +59,6 @@ set(PROGRAMS isotptun slcan_attach slcand - slcanpty ) add_executable(can-calc-bit-timing diff --git a/Makefile b/Makefile index 970a4cb..529343d 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,7 @@ MAKEFLAGS := -k CFLAGS := -O2 -Wall -Wno-parentheses CPPFLAGS += \ + -I. \ -Iinclude \ -DAF_CAN=PF_CAN \ -DPF_CAN=29 \ @@ -119,6 +120,7 @@ canplayer.o: lib.h cansend.o: lib.h log2asc.o: lib.h log2long.o: lib.h +slcanpty.o: lib.h j1939acd.o: libj1939.h j1939cat.o: libj1939.h j1939spy.o: libj1939.h @@ -135,6 +137,7 @@ cansend: cansend.o lib.o cansequence: cansequence.o lib.o log2asc: log2asc.o lib.o log2long: log2long.o lib.o +slcanpty: slcanpty.o lib.o j1939acd: j1939acd.o libj1939.o j1939cat: j1939cat.o libj1939.o j1939spy: j1939spy.o libj1939.o diff --git a/canbusload.c b/canbusload.c index e4dfc02..47b62fd 100644 --- a/canbusload.c +++ b/canbusload.c @@ -61,6 +61,7 @@ #include #include +#include "lib.h" #include "terminal.h" #include "canframelen.h" @@ -310,9 +311,7 @@ int main(int argc, char **argv) return 1; } -#ifdef DEBUG - printf("open %d '%s'.\n", i, ptr); -#endif + pr_debug("open %d '%s'.\n", i, ptr); s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW); if (s[i] < 0) { @@ -358,10 +357,8 @@ int main(int argc, char **argv) if (nbytes > max_bitrate_len) max_bitrate_len = nbytes; /* for nice printing */ + pr_debug("using interface name '%s'.\n", ifr.ifr_name); -#ifdef DEBUG - printf("using interface name '%s'.\n", ifr.ifr_name); -#endif /* try to switch the socket into CAN FD mode */ const int canfd_on = 1; setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)); diff --git a/candump.c b/candump.c index 680166f..ff646e7 100644 --- a/candump.c +++ b/candump.c @@ -63,6 +63,7 @@ #include #include +#include #include "terminal.h" #include "lib.h" @@ -72,10 +73,6 @@ #define SO_TIMESTAMPING 37 #endif -/* from #include - since Linux 2.6.30 */ -#define SOF_TIMESTAMPING_SOFTWARE (1 << 4) -#define SOF_TIMESTAMPING_RX_SOFTWARE (1 << 3) -#define SOF_TIMESTAMPING_RAW_HARDWARE (1 << 6) #define TIMESTAMPSZ 50 /* string 'absolute with date' requires max 49 bytes */ #define MAXSOCK 16 /* max. number of CAN interfaces given on the cmdline */ @@ -108,6 +105,7 @@ struct if_info { /* bundled information per open socket */ }; static struct if_info sock_info[MAXSOCK]; +static char *progname; static char devname[MAXIFNAMES][IFNAMSIZ+1]; static int dindex[MAXIFNAMES]; static int max_devname_len; /* to prevent frazzled device name output */ @@ -121,11 +119,11 @@ extern int optind, opterr, optopt; static volatile int running = 1; -static void print_usage(char *prg) +static void print_usage(void) { - fprintf(stderr, "%s - dump CAN bus traffic.\n", prg); - fprintf(stderr, "\nUsage: %s [options] +\n", prg); - fprintf(stderr, " (use CTRL-C to terminate %s)\n\n", prg); + fprintf(stderr, "%s - dump CAN bus traffic.\n", progname); + fprintf(stderr, "\nUsage: %s [options] +\n", progname); + fprintf(stderr, " (use CTRL-C to terminate %s)\n\n", progname); fprintf(stderr, "Options:\n"); fprintf(stderr, " -t (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n"); fprintf(stderr, " -H (read hardware timestamps instead of system timestamps)\n"); @@ -159,12 +157,12 @@ static void print_usage(char *prg) fprintf(stderr, "Without any given filter all data frames are received ('0:0' default filter).\n"); fprintf(stderr, "\nUse interface name '%s' to receive from all CAN interfaces.\n", ANYDEV); fprintf(stderr, "\nExamples:\n"); - fprintf(stderr, "%s -c -c -ta can0,123:7FF,400:700,#000000FF can2,400~7F0 can3 can8\n\n", prg); - fprintf(stderr, "%s -l any,0~0,#FFFFFFFF\n (log only error frames but no(!) data frames)\n", prg); - fprintf(stderr, "%s -l any,0:0,#FFFFFFFF\n (log error frames and also all data frames)\n", prg); - fprintf(stderr, "%s vcan2,12345678:DFFFFFFF\n (match only for extended CAN ID 12345678)\n", prg); - fprintf(stderr, "%s vcan2,123:7FF\n (matches CAN ID 123 - including EFF and RTR frames)\n", prg); - fprintf(stderr, "%s vcan2,123:C00007FF\n (matches CAN ID 123 - only SFF and non-RTR frames)\n", prg); + fprintf(stderr, "%s -c -c -ta can0,123:7FF,400:700,#000000FF can2,400~7F0 can3 can8\n\n", progname); + fprintf(stderr, "%s -l any,0~0,#FFFFFFFF\n (log only error frames but no(!) data frames)\n", progname); + fprintf(stderr, "%s -l any,0:0,#FFFFFFFF\n (log error frames and also all data frames)\n", progname); + fprintf(stderr, "%s vcan2,12345678:DFFFFFFF\n (match only for extended CAN ID 12345678)\n", progname); + fprintf(stderr, "%s vcan2,123:7FF\n (matches CAN ID 123 - including EFF and RTR frames)\n", progname); + fprintf(stderr, "%s vcan2,123:C00007FF\n (matches CAN ID 123 - only SFF and non-RTR frames)\n", progname); fprintf(stderr, "\n"); } @@ -216,9 +214,7 @@ static int idx2dindex(int ifidx, int socket) strcpy(devname[i], ifr.ifr_name); -#ifdef DEBUG - printf("new index %d (%s)\n", i, devname[i]); -#endif + pr_debug("new index %d (%s)\n", i, devname[i]); return i; } @@ -327,6 +323,8 @@ int main(int argc, char **argv) last_tv.tv_sec = 0; last_tv.tv_usec = 0; + progname = basename(argv[0]); + while ((opt = getopt(argc, argv, "t:HciaSs:lDdxLf:n:r:he8T:?")) != -1) { switch (opt) { case 't': @@ -335,7 +333,7 @@ int main(int argc, char **argv) if ((timestamp != 'a') && (timestamp != 'A') && (timestamp != 'd') && (timestamp != 'z')) { fprintf(stderr, "%s: unknown timestamp mode '%c' - ignored\n", - basename(argv[0]), optarg[0]); + progname, optarg[0]); timestamp = 0; } if ((logtimestamp != 'a') && (logtimestamp != 'z')) { @@ -374,7 +372,7 @@ int main(int argc, char **argv) case 's': silent = atoi(optarg); if (silent > SILENT_ON) { - print_usage(basename(argv[0])); + print_usage(); exit(1); } break; @@ -407,7 +405,7 @@ int main(int argc, char **argv) case 'n': count = atoi(optarg); if (count < 1) { - print_usage(basename(argv[0])); + print_usage(); exit(1); } break; @@ -415,7 +413,7 @@ int main(int argc, char **argv) case 'r': rcvbuf_size = atoi(optarg); if (rcvbuf_size < 1) { - print_usage(basename(argv[0])); + print_usage(); exit(1); } break; @@ -424,19 +422,19 @@ int main(int argc, char **argv) errno = 0; timeout_ms = strtol(optarg, NULL, 0); if (errno != 0) { - print_usage(basename(argv[0])); + print_usage(); exit(1); } break; default: - print_usage(basename(argv[0])); + print_usage(); exit(1); break; } } if (optind == argc) { - print_usage(basename(argv[0])); + print_usage(); exit(0); } @@ -477,9 +475,7 @@ int main(int argc, char **argv) ptr = argv[optind+i]; nptr = strchr(ptr, ','); -#ifdef DEBUG - printf("open %d '%s'.\n", i, ptr); -#endif + pr_debug("open %d '%s'.\n", i, ptr); obj->s = socket(PF_CAN, SOCK_RAW, CAN_RAW); if (obj->s < 0) { @@ -513,9 +509,7 @@ int main(int argc, char **argv) memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name)); strncpy(ifr.ifr_name, ptr, nbytes); -#ifdef DEBUG - printf("using interface name '%s'.\n", ifr.ifr_name); -#endif + pr_debug("using interface name '%s'.\n", ifr.ifr_name); if (strcmp(ANYDEV, ifr.ifr_name) != 0) { if (ioctl(obj->s, SIOCGIFINDEX, &ifr) < 0) { @@ -605,9 +599,7 @@ int main(int argc, char **argv) /* try SO_RCVBUFFORCE first, if we run with CAP_NET_ADMIN */ if (setsockopt(obj->s, SOL_SOCKET, SO_RCVBUFFORCE, &rcvbuf_size, sizeof(rcvbuf_size)) < 0) { -#ifdef DEBUG - printf("SO_RCVBUFFORCE failed so try SO_RCVBUF ...\n"); -#endif + pr_debug("SO_RCVBUFFORCE failed so try SO_RCVBUF ...\n"); if (setsockopt(obj->s, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, sizeof(rcvbuf_size)) < 0) { perror("setsockopt SO_RCVBUF"); diff --git a/canlogserver.c b/canlogserver.c index 6425ca4..51d548f 100644 --- a/canlogserver.c +++ b/canlogserver.c @@ -145,9 +145,7 @@ int idx2dindex(int ifidx, int socket) strcpy(devname[i], ifr.ifr_name); -#ifdef DEBUG - printf("new index %d (%s)\n", i, devname[i]); -#endif + pr_debug("new index %d (%s)\n", i, devname[i]); return i; } @@ -310,11 +308,9 @@ int main(int argc, char **argv) for (i=0; idata[6] || cf->data[7]) { + if (cf->can_id & CAN_ERR_CNT || cf->data[6] || cf->data[7]) { n += snprintf(buf + n, len - n, "%s", sep); n += snprintf(buf + n, len - n, "error-counter-tx-rx{{%d}{%d}}", cf->data[6], cf->data[7]); diff --git a/lib.h b/lib.h index a4d3ce5..6cc58a2 100644 --- a/lib.h +++ b/lib.h @@ -47,6 +47,13 @@ #include +#ifdef DEBUG +#define pr_debug(fmt, args...) printf(fmt, ##args) +#else +__attribute__((format (printf, 1, 2))) +static inline int pr_debug(const char* fmt, ...) {return 0;} +#endif + /* buffer sizes for CAN frame string representations */ #define CL_ID (sizeof("12345678##1")) diff --git a/mcp251xfd/mcp251xfd-dev-coredump.c b/mcp251xfd/mcp251xfd-dev-coredump.c index 5874d24..680c734 100644 --- a/mcp251xfd/mcp251xfd-dev-coredump.c +++ b/mcp251xfd/mcp251xfd-dev-coredump.c @@ -17,18 +17,10 @@ #include +#include "lib.h" #include "mcp251xfd.h" #include "mcp251xfd-dump-userspace.h" -#define pr_err(fmt, args...) fprintf(stderr, fmt, ##args) -#define pr_no(fmt, args...) while (0) { fprintf(stdout, fmt, ##args); } - -#ifdef DEBUG -#define pr_debug(fmt, args...) pr_err(fmt, ##args) -#else -#define pr_debug(fmt, args...) pr_no(fmt, ##args) -#endif - struct mcp251xfd_dump_iter { const void *start; diff --git a/slcanpty.c b/slcanpty.c index e6f1efe..fa97cd6 100644 --- a/slcanpty.c +++ b/slcanpty.c @@ -43,27 +43,12 @@ #include #include +#include "lib.h" + /* maximum rx buffer len: extended CAN frame with timestamp */ #define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1) #define DEVICE_NAME_PTMX "/dev/ptmx" -#define DEBUG - -static int asc2nibble(char c) -{ - - if ((c >= '0') && (c <= '9')) - return c - '0'; - - if ((c >= 'A') && (c <= 'F')) - return c - 'A' + 10; - - if ((c >= 'a') && (c <= 'f')) - return c - 'a' + 10; - - return 16; /* error */ -} - /* read data from pty, send CAN frames to CAN socket and answer commands */ int pty2can(int pty, int socket, struct can_filter *fi, int *is_open, int *tstamp) @@ -119,14 +104,12 @@ rx_restart: cmd = buf[0]; buf[nbytes] = 0; -#ifdef DEBUG for (tmp = 0; tmp < nbytes; tmp++) if (buf[tmp] == '\r') - putchar('@'); + pr_debug("@"); else - putchar(buf[tmp]); - printf("\n"); -#endif + pr_debug("%c", buf[tmp]); + pr_debug("\n"); /* check for filter configuration commands */ if (cmd == 'm' || cmd == 'M') {