Add ability to dump eeprom data over radio link.
[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
86         while ((c = getopt_long(argc, argv, "T:D:R", options, NULL)) != -1) {
87                 switch (c) {
88                 case 'T':
89                         tty = optarg;
90                         break;
91                 case 'D':
92                         device = optarg;
93                         break;
94                 case 'R':
95                         remote = 1;
96                         break;
97                 default:
98                         usage(argv[0]);
99                         break;
100                 }
101         }
102         if (!tty) {
103                 if (remote)
104                         tty = cc_usbdevs_find_by_arg(device, "TeleDongle");
105                 else
106                         tty = cc_usbdevs_find_by_arg(device, "TeleMetrum");
107         }
108         if (!tty)
109                 tty = getenv("ALTOS_TTY");
110         if (!tty)
111                 tty="/dev/ttyACM0";
112         cc = cc_usb_open(tty);
113         if (!cc)
114                 exit(1);
115         if (remote)
116                 cc_usb_open_remote(cc);
117         /* send a 'version' command followed by a 'log' command */
118         cc_usb_printf(cc, "v\n");
119         out = NULL;
120         for (;;) {
121                 cc_usb_getline(cc, line, sizeof (line));
122                 if (sscanf(line, "serial-number %u", &serial_number) == 1) {
123                         filename = cc_make_filename(serial_number, "eeprom");
124                         out = fopen (filename, "w");
125                         if (!out) {
126                                 perror(filename);
127                         }
128                         fprintf (out, "%s\n", line);
129                 }
130                 if (!strncmp(line, "software-version", 16))
131                         break;
132         }
133         if (!out) {
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         printf ("File name:     %s\n", filename);
140         done = 0;
141         column = 0;
142         for (block = 0; !done && block < 511; block++) {
143                 cc_usb_printf(cc, "e %x\n", block);
144                 if (column == 64) {
145                         putchar('\n');
146                         column = 0;
147                 }
148                 putchar('.'); fflush(stdout); column++;
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
164                                 cmd = data[0];
165                                 tick = data[2] + (data[3] << 8);
166                                 a = data[4] + (data[5] << 8);
167                                 b = data[6] + (data[7] << 8);
168                                 if (cmd == 'S' && a <= 8) {
169                                         if (column) putchar('\n');
170                                         printf("%s\n", state_names[a]);
171                                         column = 0;
172                                 }
173                                 if (out) {
174                                         fprintf(out, "%c %4x %4x %4x\n",
175                                                 cmd, tick, a, b);
176                                         if (cmd == 'S' && a == 8) {
177                                                 fclose(out);
178                                                 out = NULL;
179                                                 done = 1;
180                                         }
181                                 }
182                                 addr += 8;
183                         }
184                 }
185         }
186         if (column)
187                 putchar('\n');
188         if (out)
189                 fclose (out);
190         cc_usb_close(cc);
191         exit (0);
192 }