Stop recording in ao-dumplog after receiving an invalid block
[fw/altos] / ao-tools / ao-dumplog / ao-dumplog.c
index 158a445b48978eab213c8c3ec0cdd282aea2baef..b3a0a25ad9b8729f6d314f1ca9f2506e545335ea 100644 (file)
 static const struct option options[] = {
        { .name = "tty", .has_arg = 1, .val = 'T' },
        { .name = "device", .has_arg = 1, .val = 'D' },
+       { .name = "remote", .has_arg = 1, .val = 'R' },
        { 0, 0, 0, 0},
 };
 
 static void usage(char *program)
 {
-       fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>\n", program);
+       fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>] [-R]\n", program);
        exit(1);
 }
 
@@ -80,8 +81,11 @@ main (int argc, char **argv)
        int             data[8];
        int             done;
        int             column;
+       int             remote = 0;
+       int             any_valid;
+       int             invalid;
 
-       while ((c = getopt_long(argc, argv, "T:D:", options, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "T:D:R", options, NULL)) != -1) {
                switch (c) {
                case 'T':
                        tty = optarg;
@@ -89,13 +93,20 @@ main (int argc, char **argv)
                case 'D':
                        device = optarg;
                        break;
+               case 'R':
+                       remote = 1;
+                       break;
                default:
                        usage(argv[0]);
                        break;
                }
        }
-       if (!tty)
-               tty = cc_usbdevs_find_by_arg(device, "TeleMetrum");
+       if (!tty) {
+               if (remote)
+                       tty = cc_usbdevs_find_by_arg(device, "TeleDongle");
+               else
+                       tty = cc_usbdevs_find_by_arg(device, "TeleMetrum");
+       }
        if (!tty)
                tty = getenv("ALTOS_TTY");
        if (!tty)
@@ -103,6 +114,8 @@ main (int argc, char **argv)
        cc = cc_usb_open(tty);
        if (!cc)
                exit(1);
+       if (remote)
+               cc_usb_open_remote(cc);
        /* send a 'version' command followed by a 'log' command */
        cc_usb_printf(cc, "v\n");
        out = NULL;
@@ -135,6 +148,7 @@ main (int argc, char **argv)
                        column = 0;
                }
                putchar('.'); fflush(stdout); column++;
+               any_valid = 0;
                for (addr = 0; addr < 0x100;) {
                        cc_usb_getline(cc, line, sizeof (line));
                        if (sscanf(line, "00%x %x %x %x %x %x %x %x %x",
@@ -149,6 +163,8 @@ main (int argc, char **argv)
                                if (log_checksum(data) != 0)
                                        fprintf (stderr, "invalid checksum at 0x%x\n",
                                                 block * 256 + received_addr);
+                               else
+                                       any_valid = 1;
 
                                cmd = data[0];
                                tick = data[2] + (data[3] << 8);
@@ -171,6 +187,11 @@ main (int argc, char **argv)
                                addr += 8;
                        }
                }
+               if (!any_valid) {
+                       fclose(out);
+                       out = NULL;
+                       done = 1;
+               }
        }
        if (column)
                putchar('\n');