Add ao-dumplog to capture flight log from command line
authorKeith Packard <keithp@keithp.com>
Sat, 5 Sep 2009 07:29:26 +0000 (00:29 -0700)
committerKeith Packard <keithp@keithp.com>
Sat, 5 Sep 2009 07:29:26 +0000 (00:29 -0700)
This duplicates the functionality of the flight log stuf in ao-view,
except from the command line where it belongs.

Signed-off-by: Keith Packard <keithp@keithp.com>
ao-tools/Makefile.am
ao-tools/ao-dumplog/Makefile.am [new file with mode: 0644]
ao-tools/ao-dumplog/ao-dumplog.1 [new file with mode: 0644]
ao-tools/ao-dumplog/ao-dumplog.c [new file with mode: 0644]
ao-tools/lib/Makefile.am
ao-tools/lib/cc-usb.c
ao-tools/lib/cc-usb.h
ao-tools/lib/cc.h
configure.ac

index 28e77b08ab7943214f5c0e6a1de36ea8a41ad86b..b61f045f84097cbe11bfb754dd24c33508374d07 100644 (file)
@@ -1 +1 @@
-SUBDIRS=lib ao-rawload ao-dbg ao-bitbang ao-eeprom ao-list ao-load ao-view
+SUBDIRS=lib ao-rawload ao-dbg ao-dumplog ao-bitbang ao-eeprom ao-list ao-load ao-view
diff --git a/ao-tools/ao-dumplog/Makefile.am b/ao-tools/ao-dumplog/Makefile.am
new file mode 100644 (file)
index 0000000..a80cac3
--- /dev/null
@@ -0,0 +1,12 @@
+bin_PROGRAMS=ao-dumplog
+
+AM_CFLAGS=-I$(top_srcdir)/ao-tools/lib $(LIBUSB_CFLAGS) $(GNOME_CFLAGS)
+AO_DUMPLOG_LIBS=$(top_builddir)/ao-tools/lib/libao-tools.a
+
+ao_dumplog_DEPENDENCIES = $(AO_DUMPLOG_LIBS)
+
+ao_dumplog_LDADD=$(AO_DUMPLOG_LIBS) $(LIBUSB_LIBS) $(GNOME_LIBS)
+
+ao_dumplog_SOURCES = ao-dumplog.c
+
+man_MANS = ao-dumplog.1
diff --git a/ao-tools/ao-dumplog/ao-dumplog.1 b/ao-tools/ao-dumplog/ao-dumplog.1
new file mode 100644 (file)
index 0000000..8c2df7c
--- /dev/null
@@ -0,0 +1,68 @@
+.\"
+.\" Copyright © 2009 Keith Packard <keithp@keithp.com>
+.\"
+.\" This program is free software; you can redistribute it and/or modify
+.\" it under the terms of the GNU General Public License as published by
+.\" the Free Software Foundation; either version 2 of the License, or
+.\" (at your option) any later version.
+.\"
+.\" This program is distributed in the hope that it will be useful, but
+.\" WITHOUT ANY WARRANTY; without even the implied warranty of
+.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+.\" General Public License for more details.
+.\"
+.\" You should have received a copy of the GNU General Public License along
+.\" with this program; if not, write to the Free Software Foundation, Inc.,
+.\" 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+.\"
+.\"
+.TH AO-DUMPLOG 1 "ao-dumplog" ""
+.SH NAME
+ao-dumplog \- Store flight log from TeleMetrum device
+.SH SYNOPSIS
+.B "ao-dumplog"
+[\-T \fItty-device\fP]
+[\--tty \fItty-device\fP]
+[\-D \fIaltos-device\fP]
+[\--device \fIaltos-device\fP]
+.SH OPTIONS
+.TP
+\-T tty-device | --tty tty-device
+This selects which tty device ao-dumplog uses to communicate with
+the target device.
+.TP
+\-D AltOS-device | --device AltOS-device
+Search for a connected device. This requires an argument of one of the
+following forms:
+.IP
+TeleMetrum:2
+.br
+TeleMetrum
+.br
+2
+.IP
+Leaving out the product name will cause the tool to select a suitable
+product, leaving out the serial number will cause the tool to match
+one of the available devices.
+.SH DESCRIPTION
+.I ao-dumplog
+downloads the flight log from a connected TeleMetrum device and stores
+it to the configured flight log directory using a name of the form
+.IP
+\fIyyyy\fP-\fImm\fP-\fIdd\fP-serialP-\fIsss\fP-flight-\fIfff\fP.eeprom
+.PP
+\fIyyyy\fP is the current year
+.br
+\fImm\fP is the current month
+.br
+\fIdd\fP is the current day
+.br
+\fIsss\fP is the device serial number
+.br
+\fIfff\fP is a flight sequence number (to make filenames unique)
+.SH USAGE
+.I ao-dumplog
+connects to the specified target device and dumps the stored flight
+log.
+.SH AUTHOR
+Keith Packard
diff --git a/ao-tools/ao-dumplog/ao-dumplog.c b/ao-tools/ao-dumplog/ao-dumplog.c
new file mode 100644 (file)
index 0000000..4bccfd6
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * Copyright © 2009 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <getopt.h>
+#include "cc-usb.h"
+#include "cc.h"
+
+#define NUM_BLOCK      512
+
+static const struct option options[] = {
+       { .name = "tty", .has_arg = 1, .val = 'T' },
+       { .name = "device", .has_arg = 1, .val = 'D' },
+       { 0, 0, 0, 0},
+};
+
+static void usage(char *program)
+{
+       fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>\n", program);
+       exit(1);
+}
+
+int
+main (int argc, char **argv)
+{
+       struct cc_usb   *cc;
+       char            *tty = NULL;
+       char            *device = NULL;
+       int             c;
+       char            line[8192];
+       FILE            *out;
+       char            *filename;
+       int             serial_number;
+       char            cmd;
+       int             tick, a, b;
+
+       while ((c = getopt_long(argc, argv, "T:D:", options, NULL)) != -1) {
+               switch (c) {
+               case 'T':
+                       tty = optarg;
+                       break;
+               case 'D':
+                       device = optarg;
+                       break;
+               default:
+                       usage(argv[0]);
+                       break;
+               }
+       }
+       if (!tty)
+               tty = cc_usbdevs_find_by_arg(device, "TeleMetrum");
+       if (!tty)
+               tty = getenv("ALTOS_TTY");
+       if (!tty)
+               tty="/dev/ttyACM0";
+       cc = cc_usb_open(tty);
+       if (!cc)
+               exit(1);
+       /* send a 'version' command followed by a 'log' command */
+       cc_usb_printf(cc, "v\nl\n");
+       out = NULL;
+       for (;;) {
+               cc_usb_getline(cc, line, sizeof (line));
+               if (!strcmp (line, "end"))
+                       break;
+               if (sscanf(line, "serial-number %u", &serial_number) == 1) {
+                       filename = cc_make_filename(serial_number, "eeprom");
+                       out = fopen (filename, "w");
+                       if (!out) {
+                               perror(filename);
+                       }
+               } else if (sscanf(line, "%c %x %x %x", &cmd, &tick, &a, &b) == 4) {
+                       if (out) {
+                               fprintf(out, "%s\n", line);
+                               if (cmd == 'S' && a == 8) {
+                                       fclose(out);
+                                       out = NULL;
+                               }
+                       }
+               }
+       }
+       if (out)
+               fclose (out);
+       cc_usb_close(cc);
+       exit (0);
+}
index f66ee0a99634a0e663393b549c3e618895f53944..da13ede90dbc2f956b455ebd56506b302a324ced 100644 (file)
@@ -1,6 +1,6 @@
 noinst_LIBRARIES = libao-tools.a
 
 noinst_LIBRARIES = libao-tools.a
 
-AM_CFLAGS=$(WARN_CFLAGS) $(LIBUSB_CFLAGS)
+AM_CFLAGS=$(WARN_CFLAGS) $(LIBUSB_CFLAGS) $(GNOME_CFLAGS)
 
 libao_tools_a_SOURCES = \
        ccdbg-command.c \
 
 libao_tools_a_SOURCES = \
        ccdbg-command.c \
@@ -14,6 +14,7 @@ libao_tools_a_SOURCES = \
        ccdbg-memory.c \
        ccdbg-rom.c \
        ccdbg-state.c \
        ccdbg-memory.c \
        ccdbg-rom.c \
        ccdbg-state.c \
+       cc-log.c \
        cc-usb.c \
        cc-usb.h \
        cc.h \
        cc-usb.c \
        cc-usb.h \
        cc.h \
index 17f059117477994458e3296adfb5d9b2ac1be65b..80d9c04f7ec5271074c730b3bf01c17f2845f610 100644 (file)
 #include "cc-usb.h"
 
 
 #include "cc-usb.h"
 
 
-#define CC_NUM_READ            16
+#define CC_NUM_HEX_READ                64
 /*
  * AltOS has different buffer sizes for in/out packets
  */
 /*
  * AltOS has different buffer sizes for in/out packets
  */
-#define CC_IN_BUF              256
+#define CC_IN_BUF              65536
 #define CC_OUT_BUF             64
 #define DEFAULT_TTY            "/dev/ttyACM0"
 
 #define CC_OUT_BUF             64
 #define DEFAULT_TTY            "/dev/ttyACM0"
 
-struct cc_read {
+struct cc_hex_read {
        uint8_t *buf;
        int     len;
 };
 
 struct cc_usb {
        uint8_t *buf;
        int     len;
 };
 
 struct cc_usb {
-       int             fd;
-       uint8_t         in_buf[CC_IN_BUF];
-       int             in_count;
-       uint8_t         out_buf[CC_OUT_BUF];
-       int             out_count;
-       struct cc_read  read_buf[CC_NUM_READ];
-       int             read_count;
+       int                     fd;
+       uint8_t                 in_buf[CC_IN_BUF];
+       int                     in_pos;
+       int                     in_count;
+       uint8_t                 out_buf[CC_OUT_BUF];
+       int                     out_count;
+
+       struct cc_hex_read      hex_buf[CC_NUM_HEX_READ];
+       int                     hex_count;
 };
 
 #define NOT_HEX        0xff
 };
 
 #define NOT_HEX        0xff
@@ -72,61 +74,48 @@ cc_hex_nibble(uint8_t c)
  * and write them to the waiting buffer
  */
 static void
  * and write them to the waiting buffer
  */
 static void
-cc_handle_in(struct cc_usb *cc)
+cc_handle_hex_read(struct cc_usb *cc)
 {
        uint8_t h, l;
 {
        uint8_t h, l;
-       int     in_pos;
-       int     read_pos;
+       int     hex_pos;
 
 
-       in_pos = 0;
-       read_pos = 0;
-       while (read_pos < cc->read_count && in_pos < cc->in_count) {
+       hex_pos = 0;
+       while (hex_pos < cc->hex_count && cc->in_pos < cc->in_count) {
                /*
                 * Skip to next hex character
                 */
                /*
                 * Skip to next hex character
                 */
-               while (in_pos < cc->in_count &&
-                      cc_hex_nibble(cc->in_buf[in_pos]) == NOT_HEX)
-                       in_pos++;
+               while (cc->in_pos < cc->in_count &&
+                      cc_hex_nibble(cc->in_buf[cc->in_pos]) == NOT_HEX)
+                       cc->in_pos++;
                /*
                 * Make sure we have two characters left
                 */
                /*
                 * Make sure we have two characters left
                 */
-               if (cc->in_count - in_pos < 2)
+               if (cc->in_count - cc->in_pos < 2)
                        break;
                /*
                 * Parse hex number
                 */
                        break;
                /*
                 * Parse hex number
                 */
-               h = cc_hex_nibble(cc->in_buf[in_pos]);
-               l = cc_hex_nibble(cc->in_buf[in_pos+1]);
+               h = cc_hex_nibble(cc->in_buf[cc->in_pos]);
+               l = cc_hex_nibble(cc->in_buf[cc->in_pos+1]);
                if (h == NOT_HEX || l == NOT_HEX) {
                        fprintf(stderr, "hex read error\n");
                        break;
                }
                if (h == NOT_HEX || l == NOT_HEX) {
                        fprintf(stderr, "hex read error\n");
                        break;
                }
-               in_pos += 2;
+               cc->in_pos += 2;
                /*
                 * Store hex number
                 */
                /*
                 * Store hex number
                 */
-               *cc->read_buf[read_pos].buf++ = (h << 4) | l;
-               if (--cc->read_buf[read_pos].len <= 0)
-                       read_pos++;
-       }
-
-       /* Move remaining bytes to the start of the input buffer */
-       if (in_pos) {
-               memmove(cc->in_buf, cc->in_buf + in_pos,
-                       cc->in_count - in_pos);
-               cc->in_count -= in_pos;
+               *cc->hex_buf[hex_pos].buf++ = (h << 4) | l;
+               if (--cc->hex_buf[hex_pos].len <= 0)
+                       hex_pos++;
        }
 
        }
 
-       /* Move pending reads to the start of the array */
-       if (read_pos) {
-               memmove(cc->read_buf, cc->read_buf + read_pos,
-                       (cc->read_count - read_pos) * sizeof (cc->read_buf[0]));
-               cc->read_count -= read_pos;
+       /* Move pending hex reads to the start of the array */
+       if (hex_pos) {
+               memmove(cc->hex_buf, cc->hex_buf + hex_pos,
+                       (cc->hex_count - hex_pos) * sizeof (cc->hex_buf[0]));
+               cc->hex_count -= hex_pos;
        }
        }
-
-       /* Once we're done reading, flush any pending input */
-       if (cc->read_count == 0)
-               cc->in_count = 0;
 }
 
 static void
 }
 
 static void
@@ -158,8 +147,8 @@ cc_usb_dbg(int indent, uint8_t *bytes, int len)
  * Flush pending writes, fill pending reads
  */
 
  * Flush pending writes, fill pending reads
  */
 
-int
-cc_usb_sync(struct cc_usb *cc)
+static int
+_cc_usb_sync(struct cc_usb *cc, int wait_for_input)
 {
        int             ret;
        struct pollfd   fds;
 {
        int             ret;
        struct pollfd   fds;
@@ -167,26 +156,33 @@ cc_usb_sync(struct cc_usb *cc)
 
        fds.fd = cc->fd;
        for (;;) {
 
        fds.fd = cc->fd;
        for (;;) {
-               if (cc->read_count || cc->out_count)
+               if (cc->hex_count || cc->out_count)
                        timeout = 5000;
                        timeout = 5000;
+               else if (wait_for_input && cc->in_pos == cc->in_count)
+                       timeout = wait_for_input;
                else
                        timeout = 0;
                fds.events = 0;
                else
                        timeout = 0;
                fds.events = 0;
+               /* Move remaining bytes to the start of the input buffer */
+               if (cc->in_pos) {
+                       memmove(cc->in_buf, cc->in_buf + cc->in_pos,
+                               cc->in_count - cc->in_pos);
+                       cc->in_count -= cc->in_pos;
+                       cc->in_pos = 0;
+               }
                if (cc->in_count < CC_IN_BUF)
                        fds.events |= POLLIN;
                if (cc->out_count)
                        fds.events |= POLLOUT;
                ret = poll(&fds, 1, timeout);
                if (ret == 0) {
                if (cc->in_count < CC_IN_BUF)
                        fds.events |= POLLIN;
                if (cc->out_count)
                        fds.events |= POLLOUT;
                ret = poll(&fds, 1, timeout);
                if (ret == 0) {
-                       if (timeout) {
-                               fprintf(stderr, "USB link timeout\n");
-                               exit(1);
-                       }
+                       if (timeout)
+                               return -1;
                        break;
                }
                if (ret < 0) {
                        perror("poll");
                        break;
                }
                if (ret < 0) {
                        perror("poll");
-                       break;
+                       return -1;
                }
                if (fds.revents & POLLIN) {
                        ret = read(cc->fd, cc->in_buf + cc->in_count,
                }
                if (fds.revents & POLLIN) {
                        ret = read(cc->fd, cc->in_buf + cc->in_count,
@@ -194,7 +190,8 @@ cc_usb_sync(struct cc_usb *cc)
                        if (ret > 0) {
                                cc_usb_dbg(24, cc->in_buf + cc->in_count, ret);
                                cc->in_count += ret;
                        if (ret > 0) {
                                cc_usb_dbg(24, cc->in_buf + cc->in_count, ret);
                                cc->in_count += ret;
-                               cc_handle_in(cc);
+                               if (cc->hex_count)
+                                       cc_handle_hex_read(cc);
                        } else if (ret < 0)
                                perror("read");
                }
                        } else if (ret < 0)
                                perror("read");
                }
@@ -211,6 +208,16 @@ cc_usb_sync(struct cc_usb *cc)
                                perror("write");
                }
        }
                                perror("write");
                }
        }
+       return 0;
+}
+
+void
+cc_usb_sync(struct cc_usb *cc)
+{
+       if (_cc_usb_sync(cc, 0) < 0) {
+               fprintf(stderr, "USB link timeout\n");
+               exit(1);
+       }
 }
 
 void
 }
 
 void
@@ -244,6 +251,38 @@ cc_usb_printf(struct cc_usb *cc, char *format, ...)
        }
 }
 
        }
 }
 
+int
+cc_usb_getchar(struct cc_usb *cc)
+{
+       while (cc->in_pos == cc->in_count) {
+               if (_cc_usb_sync(cc, 5000) < 0) {
+                       fprintf(stderr, "USB link timeout\n");
+                       exit(1);
+               }
+       }
+       return cc->in_buf[cc->in_pos++];
+}
+
+void
+cc_usb_getline(struct cc_usb *cc, char *line, int max)
+{
+       int     c;
+
+       while ((c = cc_usb_getchar(cc)) != '\n') {
+               switch (c) {
+               case '\r':
+                       break;
+               default:
+                       if (max > 1) {
+                               *line++ = c;
+                               max--;
+                       }
+                       break;
+               }
+       }
+       *line++ = '\0';
+}
+
 int
 cc_usb_send_bytes(struct cc_usb *cc, uint8_t *bytes, int len)
 {
 int
 cc_usb_send_bytes(struct cc_usb *cc, uint8_t *bytes, int len)
 {
@@ -266,12 +305,18 @@ cc_usb_send_bytes(struct cc_usb *cc, uint8_t *bytes, int len)
 void
 cc_queue_read(struct cc_usb *cc, uint8_t *buf, int len)
 {
 void
 cc_queue_read(struct cc_usb *cc, uint8_t *buf, int len)
 {
-       struct cc_read  *read_buf;
-       while (cc->read_count >= CC_NUM_READ)
+       struct cc_hex_read      *hex_buf;
+
+       /* At the start of a command sequence, flush any pending input */
+       if (cc->hex_count == 0) {
                cc_usb_sync(cc);
                cc_usb_sync(cc);
-       read_buf = &cc->read_buf[cc->read_count++];
-       read_buf->buf = buf;
-       read_buf->len = len;
+               cc->in_count = 0;
+       }
+       while (cc->hex_count >= CC_NUM_HEX_READ)
+               cc_usb_sync(cc);
+       hex_buf = &cc->hex_buf[cc->hex_count++];
+       hex_buf->buf = buf;
+       hex_buf->len = len;
 }
 
 int
 }
 
 int
@@ -351,9 +396,10 @@ cc_usb_open(char *tty)
        cfmakeraw(&termios);
        tcsetattr(cc->fd, TCSAFLUSH, &termios);
        cc_usb_printf(cc, "E 0\nm 0\n");
        cfmakeraw(&termios);
        tcsetattr(cc->fd, TCSAFLUSH, &termios);
        cc_usb_printf(cc, "E 0\nm 0\n");
-       cc_usb_sync(cc);
-       sleep(1);
-       cc_usb_sync(cc);
+       do {
+               cc->in_count = cc->in_pos = 0;
+               _cc_usb_sync(cc, 100);
+       } while (cc->in_count > 0);
        return cc;
 }
 
        return cc;
 }
 
index 9baabd9598fbb9356f43ee33332fe3deed917414..7b6be350f02e6badefe61b5142fdd660f51e8a06 100644 (file)
@@ -47,12 +47,18 @@ cc_usb_debug_mode(struct cc_usb *cc);
 int
 cc_usb_reset(struct cc_usb *cc);
 
 int
 cc_usb_reset(struct cc_usb *cc);
 
-int
+void
 cc_usb_sync(struct cc_usb *cc);
 
 void
 cc_queue_read(struct cc_usb *cc, uint8_t *buf, int len);
 
 cc_usb_sync(struct cc_usb *cc);
 
 void
 cc_queue_read(struct cc_usb *cc, uint8_t *buf, int len);
 
+int
+cc_usb_getchar(struct cc_usb *cc);
+
+void
+cc_usb_getline(struct cc_usb *cc, char *line, int max);
+
 void
 cc_usb_printf(struct cc_usb *cc, char *format, ...);
 
 void
 cc_usb_printf(struct cc_usb *cc, char *format, ...);
 
index 0933f2725a9fe79ee1db50bc3e2803ed16a84943..f92a29f7932a0acf8c7c5fcd4a6ce718174a92d0 100644 (file)
@@ -51,4 +51,13 @@ cc_usbdevs_scan(void);
 char *
 cc_usbdevs_find_by_arg(char *arg, char *default_product);
 
 char *
 cc_usbdevs_find_by_arg(char *arg, char *default_product);
 
+void
+cc_set_log_dir(char *dir);
+
+char *
+cc_get_log_dir(void);
+
+char *
+cc_make_filename(int serial, char *ext);
+
 #endif /* _CC_H_ */
 #endif /* _CC_H_ */
index 564028572a2ab0ec6ff070a65555d9c32b7fd55d..73a33ac3d8cdacbbf85993295e1a32d369f988f8 100644 (file)
@@ -77,6 +77,7 @@ ao-tools/Makefile
 ao-tools/lib/Makefile
 ao-tools/ao-rawload/Makefile
 ao-tools/ao-dbg/Makefile
 ao-tools/lib/Makefile
 ao-tools/ao-rawload/Makefile
 ao-tools/ao-dbg/Makefile
+ao-tools/ao-dumplog/Makefile
 ao-tools/ao-bitbang/Makefile
 ao-tools/ao-eeprom/Makefile
 ao-tools/ao-list/Makefile
 ao-tools/ao-bitbang/Makefile
 ao-tools/ao-eeprom/Makefile
 ao-tools/ao-list/Makefile