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