Automatically extract flight number for eeprom and telem filenames.
[fw/altos] / ao-tools / ao-dumplog / ao-dumplog.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <getopt.h>
23 #include <string.h>
24 #include "cc-usb.h"
25 #include "cc.h"
26
27 #define NUM_BLOCK       512
28
29 static const struct option options[] = {
30         { .name = "tty", .has_arg = 1, .val = 'T' },
31         { .name = "device", .has_arg = 1, .val = 'D' },
32         { .name = "remote", .has_arg = 1, .val = 'R' },
33         { 0, 0, 0, 0},
34 };
35
36 static void usage(char *program)
37 {
38         fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>] [-R]\n", program);
39         exit(1);
40 }
41
42 static uint8_t
43 log_checksum(int d[8])
44 {
45         uint8_t sum = 0x5a;
46         int     i;
47
48         for (i = 0; i < 8; i++)
49                 sum += (uint8_t) d[i];
50         return -sum;
51 }
52
53 static const char *state_names[] = {
54         "startup",
55         "idle",
56         "pad",
57         "boost",
58         "fast",
59         "coast",
60         "drogue",
61         "main",
62         "landed",
63         "invalid"
64 };
65
66
67 int
68 main (int argc, char **argv)
69 {
70         struct cc_usb   *cc;
71         char            *tty = NULL;
72         char            *device = NULL;
73         int             c;
74         char            line[8192];
75         FILE            *out;
76         char            *filename;
77         int             serial_number = 0;
78         int             flight = 0;
79         char            cmd;
80         int             tick, a, b;
81         int             block;
82         int             addr;
83         int             received_addr;
84         int             data[8];
85         int             done;
86         int             column;
87         int             remote = 0;
88         int             any_valid;
89         int             invalid;
90         char            serial_line[8192];
91
92         while ((c = getopt_long(argc, argv, "T:D:R", options, NULL)) != -1) {
93                 switch (c) {
94                 case 'T':
95                         tty = optarg;
96                         break;
97                 case 'D':
98                         device = optarg;
99                         break;
100                 case 'R':
101                         remote = 1;
102                         break;
103                 default:
104                         usage(argv[0]);
105                         break;
106                 }
107         }
108         if (!tty) {
109                 if (remote)
110                         tty = cc_usbdevs_find_by_arg(device, "TeleDongle");
111                 else
112                         tty = cc_usbdevs_find_by_arg(device, "TeleMetrum");
113         }
114         if (!tty)
115                 tty = getenv("ALTOS_TTY");
116         if (!tty)
117                 tty="/dev/ttyACM0";
118         cc = cc_usb_open(tty);
119         if (!cc)
120                 exit(1);
121         if (remote)
122                 cc_usb_open_remote(cc);
123         /* send a 'version' command followed by a 'log' command */
124         cc_usb_printf(cc, "v\n");
125         out = NULL;
126         for (;;) {
127                 cc_usb_getline(cc, line, sizeof (line));
128                 if (sscanf(line, "serial-number %u", &serial_number) == 1)
129                         strcpy(serial_line, line);
130                 if (!strncmp(line, "software-version", 16))
131                         break;
132         }
133         if (!serial_number) {
134                 fprintf(stderr, "no serial number found\n");
135                 cc_usb_close(cc);
136                 exit(1);
137         }
138         printf ("Serial number: %d\n", serial_number);
139         done = 0;
140         column = 0;
141         for (block = 0; !done && block < 511; block++) {
142                 cc_usb_printf(cc, "e %x\n", block);
143                 if (column == 64) {
144                         putchar('\n');
145                         column = 0;
146                 }
147                 putchar('.'); fflush(stdout); column++;
148                 any_valid = 0;
149                 for (addr = 0; addr < 0x100;) {
150                         cc_usb_getline(cc, line, sizeof (line));
151                         if (sscanf(line, "00%x %x %x %x %x %x %x %x %x",
152                                           &received_addr,
153                                           &data[0], &data[1], &data[2], &data[3],
154                                           &data[4], &data[5], &data[6], &data[7]) == 9)
155                         {
156                                 if (received_addr != addr)
157                                         fprintf(stderr, "data out of sync at 0x%x\n",
158                                                 block * 256 + received_addr);
159
160                                 if (log_checksum(data) != 0)
161                                         fprintf (stderr, "invalid checksum at 0x%x\n",
162                                                  block * 256 + received_addr);
163                                 else
164                                         any_valid = 1;
165
166                                 cmd = data[0];
167                                 tick = data[2] + (data[3] << 8);
168                                 a = data[4] + (data[5] << 8);
169                                 b = data[6] + (data[7] << 8);
170                                 if (cmd == 'F') {
171                                         flight = b;
172                                         filename = cc_make_filename(serial_number, flight, "eeprom");
173                                         printf ("Flight:       %d\n", flight);
174                                         printf ("File name:     %s\n", filename);
175                                         out = fopen (filename, "w");
176                                         if (!out) {
177                                                 perror(filename);
178                                                 exit(1);
179                                         }
180                                         fprintf(out, "%s\n", serial_line);
181                                 }
182
183                                 if (cmd == 'S' && a <= 8) {
184                                         if (column) putchar('\n');
185                                         printf("%s\n", state_names[a]);
186                                         column = 0;
187                                 }
188                                 if (out) {
189                                         fprintf(out, "%c %4x %4x %4x\n",
190                                                 cmd, tick, a, b);
191                                         if (cmd == 'S' && a == 8) {
192                                                 fclose(out);
193                                                 out = NULL;
194                                                 done = 1;
195                                         }
196                                 }
197                                 addr += 8;
198                         }
199                 }
200                 if (!any_valid) {
201                         fclose(out);
202                         out = NULL;
203                         done = 1;
204                 }
205         }
206         if (column)
207                 putchar('\n');
208         if (out)
209                 fclose (out);
210         cc_usb_close(cc);
211         exit (0);
212 }