]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blobdiff - canplayer.c
can-utils: canfdtest, a full duplex test to find out-of-order messages
[sojka/can-utils.git] / canplayer.c
index f1d6d7ec2cb278e1d54bd3ed7908eaf49d6550b5..efce089fb9421c321bb6ba30d4c3c3bd42a6e85d 100644 (file)
 
 #include "lib.h"
 
-#define DEFAULT_GAP 1 /* ms */
-#define DEFAULT_LOOPS 1 /* only one replay */
-#define CHANNELS 20   /* anyone using more than 20 CAN interfaces at a time? */
-#define BUFSZ 100     /* for one line in the logfile */
+#define DEFAULT_GAP    1       /* ms */
+#define DEFAULT_LOOPS  1       /* only one replay */
+#define CHANNELS       20      /* anyone using more than 20 CAN interfaces at a time? */
+#define BUFSZ          400     /* for one line in the logfile */
+#define STDOUTIDX      65536   /* interface index for printing on stdout - bigger than max uint16 */
 
 struct assignment {
        char txif[IFNAMSIZ];
@@ -96,8 +97,12 @@ void print_usage(char *prg)
                "<write-if>=<log-if>\n");
        fprintf(stderr, "e.g. vcan2=can0 ( send frames received from can0 on "
                "vcan2 )\n");
+       fprintf(stderr, "extra hook: stdout=can0 ( print logfile line marked with can0 on "
+               "stdout )\n");
        fprintf(stderr, "No assignments => send frames to the interface(s) they "
                "had been received from.\n\n");
+       fprintf(stderr, "Lines in the logfile not beginning with '(' (start of "
+               "timestamp) are ignored.\n\n");
 }
 
 /* copied from /usr/src/linux/include/linux/time.h ...
@@ -208,13 +213,16 @@ int add_assignment(char *mode, int socket, char *txname, char *rxname,
        }
        strcpy(asgn[i].rxif, rxname);
 
-       strcpy(ifr.ifr_name, txname);
-       if (ioctl(socket, SIOCGIFINDEX, &ifr) < 0) {
-               perror("SIOCGIFINDEX");
-               fprintf(stderr, "write-if interface name '%s' is wrong!\n", txname);
-               return 1;
-       }
-       asgn[i].txifidx = ifr.ifr_ifindex;
+       if (strcmp(txname, "stdout")) {
+               strcpy(ifr.ifr_name, txname);
+               if (ioctl(socket, SIOCGIFINDEX, &ifr) < 0) {
+                       perror("SIOCGIFINDEX");
+                       fprintf(stderr, "write-if interface name '%s' is wrong!\n", txname);
+                       return 1;
+               }
+               asgn[i].txifidx = ifr.ifr_ifindex;
+       } else
+               asgn[i].txifidx = STDOUTIDX;
 
        if (verbose > 1) /* use -v -v to see this */
                printf("added %s assignment: log-if=%s write-if=%s write-if-idx=%d\n",
@@ -241,8 +249,9 @@ int main(int argc, char **argv)
        int assignments; /* assignments defined on the commandline */
        int txidx;       /* sendto() interface index */
        int eof, nbytes, i, j;
+       char *fret;
 
-       while ((opt = getopt(argc, argv, "I:l:tg:s:xv")) != -1) {
+       while ((opt = getopt(argc, argv, "I:l:tg:s:xv?")) != -1) {
                switch (opt) {
                case 'I':
                        infile = fopen(optarg, "r");
@@ -286,6 +295,7 @@ int main(int argc, char **argv)
                        verbose++;
                        break;
 
+               case '?':
                default:
                        print_usage(basename(argv[0]));
                        return 1;
@@ -346,7 +356,7 @@ int main(int argc, char **argv)
                                if (buf[j] == '=')
                                        break;
                        }
-                       if (buf[j] != '=') {
+                       if ((j == BUFSZ) || (buf[j] != '=')) {
                                fprintf(stderr, "'=' missing in assignment!\n");
                                print_usage(basename(argv[0]));
                                return 1;
@@ -365,14 +375,24 @@ int main(int argc, char **argv)
                if (verbose > 1) /* use -v -v to see this */
                        printf (">>>>>>>>> start reading file. remaining loops = %d\n", loops);
 
-               if (!fgets(buf, BUFSZ-1, infile)) /* read first frame from logfile */
+               /* read first non-comment frame from logfile */
+               while ((fret = fgets(buf, BUFSZ-1, infile)) != NULL && buf[0] != '(') {
+                       if (strlen(buf) >= BUFSZ-2) {
+                               fprintf(stderr, "comment line too long for input buffer\n");
+                               return 1;
+                       }
+               }
+
+               if (!fret)
                        goto out; /* nothing to read */
 
                eof = 0;
 
                if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
-                          device, ascframe) != 4)
+                          device, ascframe) != 4) {
+                       fprintf(stderr, "incorrect line format in logfile\n");
                        return 1;
+               }
 
                if (use_timestamps) { /* throttle sending due to logfile timestamps */
 
@@ -403,7 +423,11 @@ int main(int argc, char **argv)
                                        txidx = get_txidx(device);
                                }
 
-                               if (txidx) { /* only send to valid CAN devices */
+                               if (txidx == STDOUTIDX) /* hook to print logfile lines on stdout */
+
+                                       printf("%s", buf); /* print the line AS-IS without extra \n */
+
+                               else if (txidx > 0) { /* only send to valid CAN devices */
 
                                        if (parse_canframe(ascframe, &frame)) {
                                                fprintf(stderr, "wrong CAN frame format: '%s'!", ascframe);
@@ -427,15 +451,24 @@ int main(int argc, char **argv)
                                        }
                                }
 
-                               /* read next frame from logfile */
-                               if (!fgets(buf, BUFSZ-1, infile)) {
+                               /* read next non-comment frame from logfile */
+                               while ((fret = fgets(buf, BUFSZ-1, infile)) != NULL && buf[0] != '(') {
+                                       if (strlen(buf) >= BUFSZ-2) {
+                                               fprintf(stderr, "comment line too long for input buffer\n");
+                                               return 1;
+                                       }
+                               }
+
+                               if (!fret) {
                                        eof = 1; /* this file is completely processed */
                                        break;
                                }
 
                                if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
-                                          device, ascframe) != 4)
+                                          device, ascframe) != 4) {
+                                       fprintf(stderr, "incorrect line format in logfile\n");
                                        return 1;
+                               }
 
                                if (use_timestamps) {
                                        gettimeofday(&today_tv, NULL);