Automatically extract flight number for eeprom and telem filenames.
authorKeith Packard <keithp@keithp.com>
Sun, 22 Nov 2009 09:10:44 +0000 (01:10 -0800)
committerKeith Packard <keithp@keithp.com>
Sun, 22 Nov 2009 09:10:44 +0000 (01:10 -0800)
Extract flight number from either telemetry or eeprom files and use
that in the resulting filenames. To ensure that files remain unique,
add a new field, -seq-%03d. This is appended only when the sequence
number is non-zero as it shouldn't occur in normal usage.

This also eliminates some duplicate filename creation code in the
library and aoview sources.

Signed-off-by: Keith Packard <keithp@keithp.com>
ao-tools/ao-dumplog/ao-dumplog.c
ao-tools/ao-view/aoview_eeprom.c
ao-tools/ao-view/aoview_file.c
ao-tools/ao-view/aoview_log.c
ao-tools/ao-view/aoview_monitor.c
ao-tools/lib/cc-log.c
ao-tools/lib/cc.h

index b3a0a25ad9b8729f6d314f1ca9f2506e545335ea..440a02b5925d8b473f633a8a08cfd1638c66a159 100644 (file)
@@ -20,6 +20,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <getopt.h>
+#include <string.h>
 #include "cc-usb.h"
 #include "cc.h"
 
@@ -62,6 +63,7 @@ static const char *state_names[] = {
        "invalid"
 };
 
+
 int
 main (int argc, char **argv)
 {
@@ -72,7 +74,8 @@ main (int argc, char **argv)
        char            line[8192];
        FILE            *out;
        char            *filename;
-       int             serial_number;
+       int             serial_number = 0;
+       int             flight = 0;
        char            cmd;
        int             tick, a, b;
        int             block;
@@ -84,6 +87,7 @@ main (int argc, char **argv)
        int             remote = 0;
        int             any_valid;
        int             invalid;
+       char            serial_line[8192];
 
        while ((c = getopt_long(argc, argv, "T:D:R", options, NULL)) != -1) {
                switch (c) {
@@ -121,24 +125,17 @@ main (int argc, char **argv)
        out = NULL;
        for (;;) {
                cc_usb_getline(cc, line, sizeof (line));
-               if (sscanf(line, "serial-number %u", &serial_number) == 1) {
-                       filename = cc_make_filename(serial_number, "eeprom");
-                       out = fopen (filename, "w");
-                       if (!out) {
-                               perror(filename);
-                       }
-                       fprintf (out, "%s\n", line);
-               }
+               if (sscanf(line, "serial-number %u", &serial_number) == 1)
+                       strcpy(serial_line, line);
                if (!strncmp(line, "software-version", 16))
                        break;
        }
-       if (!out) {
+       if (!serial_number) {
                fprintf(stderr, "no serial number found\n");
                cc_usb_close(cc);
                exit(1);
        }
        printf ("Serial number: %d\n", serial_number);
-       printf ("File name:     %s\n", filename);
        done = 0;
        column = 0;
        for (block = 0; !done && block < 511; block++) {
@@ -170,6 +167,19 @@ main (int argc, char **argv)
                                tick = data[2] + (data[3] << 8);
                                a = data[4] + (data[5] << 8);
                                b = data[6] + (data[7] << 8);
+                               if (cmd == 'F') {
+                                       flight = b;
+                                       filename = cc_make_filename(serial_number, flight, "eeprom");
+                                       printf ("Flight:       %d\n", flight);
+                                       printf ("File name:     %s\n", filename);
+                                       out = fopen (filename, "w");
+                                       if (!out) {
+                                               perror(filename);
+                                               exit(1);
+                                       }
+                                       fprintf(out, "%s\n", serial_line);
+                               }
+
                                if (cmd == 'S' && a <= 8) {
                                        if (column) putchar('\n');
                                        printf("%s\n", state_names[a]);
index 34e2deedad12c0aa7bece87e4fc1ec4594daa6f8..447b83a435dd5c89a6ab4367730849a6f2b29e1d 100644 (file)
@@ -66,6 +66,8 @@ aoview_eeprom_parse(struct aoview_serial *serial,
        if (sscanf(line, "serial-number %u", &serial_number) == 1) {
                aoview_file_set_serial(eeprom_file, serial_number);
        } else if (sscanf(line, "%c %x %x %x", &cmd, &tick, &a, &b) == 4) {
+               if (cmd == 'F')
+                       aoview_file_set_flight(eeprom_file, b);
                aoview_file_printf(eeprom_file, "%s\n", line);
                if (cmd == 'S' && a == 8) {
                        aoview_eeprom_done(serial);
index 5288c2f7530cde26903a2f76f88167d20806614d..292887a02a5e6f97a7c9a3db11ea55f112f3c6a5 100644 (file)
@@ -28,6 +28,7 @@ struct aoview_file {
        char    *name;
        int     failed;
        int     serial;
+       int     flight;
        int     sequence;
 };
 
@@ -94,6 +95,7 @@ gboolean
 aoview_file_start(struct aoview_file *file)
 {
        char            base[50];
+       char            seq[20];
        struct tm       tm;
        time_t          now;
        char            *full;
@@ -105,34 +107,17 @@ aoview_file_start(struct aoview_file *file)
        if (file->failed)
                return FALSE;
 
-       now = time(NULL);
-       (void) localtime_r(&now, &tm);
-       aoview_mkdir(aoview_file_dir);
-       for (;;) {
-               snprintf(base, sizeof (base), "%04d-%02d-%02d-serial-%03d-flight-%03d.%s",
-                       tm.tm_year + 1900,
-                       tm.tm_mon + 1,
-                       tm.tm_mday,
-                       file->serial,
-                       file->sequence,
-                       file->ext);
-               full = aoview_fullname(aoview_file_dir, base);
-               r = access(full, F_OK);
-               if (r < 0) {
-                       file->file = fopen(full, "w");
-                       if (!file->file) {
-                               aoview_file_open_failed(full);
-                               free(full);
-                               file->failed = 1;
-                               return FALSE;
-                       } else {
-                               setlinebuf(file->file);
-                               file->name = full;
-                               return TRUE;
-                       }
-               }
+       full = cc_make_filename(file->serial, file->flight, file->ext);
+       file->file = fopen(full, "w");
+       if (!file->file) {
+               aoview_file_open_failed(full);
                free(full);
-               file->sequence++;
+               file->failed = 1;
+               return FALSE;
+       } else {
+               setlinebuf(file->file);
+               file->name = full;
+               return TRUE;
        }
 }
 
@@ -195,6 +180,20 @@ aoview_file_get_serial(struct aoview_file *file)
        return file->serial;
 }
 
+void
+aoview_file_set_flight(struct aoview_file *file, int flight)
+{
+       if (flight != file->flight)
+               aoview_file_finish(file);
+       file->flight = flight;
+}
+
+int
+aoview_file_get_flight(struct aoview_file *file)
+{
+       return file->flight;
+}
+
 void
 aoview_file_init(GladeXML *xml)
 {
index 1b89c28cd602b1253330f91bc74dc964dbee00e6..2880ecb11e288e03aec6b36aab7a118c698d9a1a 100644 (file)
@@ -38,6 +38,18 @@ aoview_log_get_serial(void)
        return aoview_file_get_serial(aoview_log);
 }
 
+void
+aoview_log_set_flight(int flight)
+{
+       aoview_file_set_flight(aoview_log, flight);
+}
+
+int
+aoview_log_get_flight(void)
+{
+       return aoview_file_get_flight(aoview_log);
+}
+
 void
 aoview_log_printf(char *format, ...)
 {
index 3d235e44f5d22d3db797b9c3c0b30fe85c6f0600..4d7e7a9ffc5a50c7c33d6857545e1742fdae9617 100644 (file)
@@ -68,6 +68,7 @@ aoview_monitor_callback(gpointer user_data,
                                if (monitor_pos) {
                                        if (aoview_monitor_parse(monitor_line)) {
                                                aoview_log_set_serial(aostate.data.serial);
+                                               aoview_log_set_flight(aostate.data.flight);
                                                if (aoview_log_get_serial())
                                                        aoview_log_printf ("%s\n", monitor_line);
                                        }
index dd8177f4d7ef33c2e6c230d4af0da54ac15f9404..ed51f87e2ef1c2001932bb94eafedc2282b15c94 100644 (file)
@@ -82,9 +82,10 @@ cc_get_log_dir(void)
 }
 
 char *
-cc_make_filename(int serial, char *ext)
+cc_make_filename(int serial, int flight, char *ext)
 {
        char            base[50];
+       char            seq[20];
        struct tm       tm;
        time_t          now;
        char            *full;
@@ -96,13 +97,19 @@ cc_make_filename(int serial, char *ext)
        cc_mkdir(cc_get_log_dir());
        sequence = 0;
        for (;;) {
-               snprintf(base, sizeof (base), "%04d-%02d-%02d-serial-%03d-flight-%03d.%s",
-                       tm.tm_year + 1900,
-                       tm.tm_mon + 1,
-                       tm.tm_mday,
-                       serial,
-                       sequence,
-                       ext);
+               if (sequence)
+                       snprintf(seq, sizeof(seq), "-seq-%03d", sequence);
+               else
+                       seq[0] = '\0';
+
+               snprintf(base, sizeof (base), "%04d-%02d-%02d-serial-%03d-flight-%03d%s.%s",
+                        tm.tm_year + 1900,
+                        tm.tm_mon + 1,
+                        tm.tm_mday,
+                        serial,
+                        flight,
+                        seq,
+                        ext);
                full = cc_fullname(cc_get_log_dir(), base);
                r = access(full, F_OK);
                if (r < 0)
index 46b16a8e90d45c81b45938c13870f0324e33e6b8..ede46aa0edd6b76a1948b1fcb8770fe3291cd635 100644 (file)
@@ -61,7 +61,7 @@ char *
 cc_get_log_dir(void);
 
 char *
-cc_make_filename(int serial, char *ext);
+cc_make_filename(int serial, int flight, char *ext);
 
 /*
  * For sequential data which are not evenly spaced