57c432902c81151f366bb887752dd6bf8cba56ff
[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         { .name = "channel", .has_arg = 1, .val = 'C' },
34         { 0, 0, 0, 0},
35 };
36
37 static void usage(char *program)
38 {
39         fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>] [--remote] [--channel <radio-channel>]\n", program);
40         exit(1);
41 }
42
43 static uint8_t
44 log_checksum(int d[8])
45 {
46         uint8_t sum = 0x5a;
47         int     i;
48
49         for (i = 0; i < 8; i++)
50                 sum += (uint8_t) d[i];
51         return -sum;
52 }
53
54 static const char *state_names[] = {
55         "startup",
56         "idle",
57         "pad",
58         "boost",
59         "fast",
60         "coast",
61         "drogue",
62         "main",
63         "landed",
64         "invalid"
65 };
66
67
68 int
69 main (int argc, char **argv)
70 {
71         struct cc_usb   *cc;
72         char            *tty = NULL;
73         char            *device = NULL;
74         int             c;
75         char            line[8192];
76         FILE            *out;
77         char            *filename;
78         int             serial_number = 0;
79         int             channel = 0;
80         int             flight = 0;
81         char            cmd;
82         int             tick, a, b;
83         int             block;
84         int             addr;
85         int             received_addr;
86         int             data[8];
87         int             done;
88         int             column;
89         int             remote = 0;
90         int             any_valid;
91         int             invalid;
92         char            serial_line[8192];
93
94         while ((c = getopt_long(argc, argv, "T:D:R", options, NULL)) != -1) {
95                 switch (c) {
96                 case 'T':
97                         tty = optarg;
98                         break;
99                 case 'D':
100                         device = optarg;
101                         break;
102                 case 'R':
103                         remote = 1;
104                         break;
105                 case 'C':
106                         channel = atoi(optarg);
107                         break;
108                 default:
109                         usage(argv[0]);
110                         break;
111                 }
112         }
113         if (!tty) {
114                 if (remote)
115                         tty = cc_usbdevs_find_by_arg(device, "TeleDongle");
116                 else
117                         tty = cc_usbdevs_find_by_arg(device, "TeleMetrum");
118         }
119         if (!tty)
120                 tty = getenv("ALTOS_TTY");
121         if (!tty)
122                 tty="/dev/ttyACM0";
123         cc = cc_usb_open(tty);
124         if (!cc)
125                 exit(1);
126         if (remote)
127                 cc_usb_open_remote(cc, channel);
128         /* send a 'version' command followed by a 'log' command */
129         cc_usb_printf(cc, "v\n");
130         out = NULL;
131         for (;;) {
132                 cc_usb_getline(cc, line, sizeof (line));
133                 if (sscanf(line, "serial-number %u", &serial_number) == 1)
134                         strcpy(serial_line, line);
135                 if (!strncmp(line, "software-version", 16))
136                         break;
137         }
138         if (!serial_number) {
139                 fprintf(stderr, "no serial number found\n");
140                 cc_usb_close(cc);
141                 exit(1);
142         }
143         printf ("Serial number: %d\n", serial_number);
144         done = 0;
145         column = 0;
146         for (block = 0; !done && block < 511; block++) {
147                 cc_usb_printf(cc, "e %x\n", block);
148                 if (column == 64) {
149                         putchar('\n');
150                         column = 0;
151                 }
152                 putchar('.'); fflush(stdout); column++;
153                 any_valid = 0;
154                 for (addr = 0; addr < 0x100;) {
155                         cc_usb_getline(cc, line, sizeof (line));
156                         if (sscanf(line, "00%x %x %x %x %x %x %x %x %x",
157                                           &received_addr,
158                                           &data[0], &data[1], &data[2], &data[3],
159                                           &data[4], &data[5], &data[6], &data[7]) == 9)
160                         {
161                                 if (received_addr != addr)
162                                         fprintf(stderr, "data out of sync at 0x%x\n",
163                                                 block * 256 + received_addr);
164
165                                 if (log_checksum(data) != 0)
166                                         fprintf (stderr, "invalid checksum at 0x%x\n",
167                                                  block * 256 + received_addr);
168                                 else
169                                         any_valid = 1;
170
171                                 cmd = data[0];
172                                 tick = data[2] + (data[3] << 8);
173                                 a = data[4] + (data[5] << 8);
174                                 b = data[6] + (data[7] << 8);
175                                 if (cmd == 'F') {
176                                         flight = b;
177                                         filename = cc_make_filename(serial_number, flight, "eeprom");
178                                         printf ("Flight:       %d\n", flight);
179                                         printf ("File name:     %s\n", filename);
180                                         out = fopen (filename, "w");
181                                         if (!out) {
182                                                 perror(filename);
183                                                 exit(1);
184                                         }
185                                         fprintf(out, "%s\n", serial_line);
186                                 }
187
188                                 if (cmd == 'S' && a <= 8) {
189                                         if (column) putchar('\n');
190                                         printf("%s\n", state_names[a]);
191                                         column = 0;
192                                 }
193                                 if (out) {
194                                         fprintf(out, "%c %4x %4x %4x\n",
195                                                 cmd, tick, a, b);
196                                         if (cmd == 'S' && a == 8) {
197                                                 fclose(out);
198                                                 out = NULL;
199                                                 done = 1;
200                                         }
201                                 }
202                                 addr += 8;
203                         }
204                 }
205                 if (!any_valid) {
206                         fclose(out);
207                         out = NULL;
208                         done = 1;
209                 }
210         }
211         if (column)
212                 putchar('\n');
213         if (out)
214                 fclose (out);
215         cc_usb_close(cc);
216         exit (0);
217 }