2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
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 = 0, .val = 'R' },
33 { .name = "channel", .has_arg = 1, .val = 'C' },
37 static void usage(char *program)
39 fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>] [--remote] [--channel <radio-channel>]\n", program);
44 log_checksum(int d[8])
49 for (i = 0; i < 8; i++)
50 sum += (uint8_t) d[i];
54 static const char *state_names[] = {
69 main (int argc, char **argv)
78 int serial_number = 0;
92 char serial_line[8192];
94 while ((c = getopt_long(argc, argv, "T:D:C:R", options, NULL)) != -1) {
106 channel = atoi(optarg);
115 tty = cc_usbdevs_find_by_arg(device, "TeleDongle");
117 tty = cc_usbdevs_find_by_arg(device, "TeleMetrum");
120 tty = getenv("ALTOS_TTY");
123 cc = cc_usb_open(tty);
127 cc_usb_open_remote(cc, channel);
128 /* send a 'version' command followed by a 'log' command */
129 cc_usb_printf(cc, "v\n");
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))
138 if (!serial_number) {
139 fprintf(stderr, "no serial number found\n");
143 printf ("Serial number: %d\n", serial_number);
146 for (block = 0; !done && block < 511; block++) {
147 cc_usb_printf(cc, "e %x\n", block);
152 putchar('.'); fflush(stdout); column++;
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",
158 &data[0], &data[1], &data[2], &data[3],
159 &data[4], &data[5], &data[6], &data[7]) == 9)
161 if (received_addr != addr)
162 fprintf(stderr, "data out of sync at 0x%x\n",
163 block * 256 + received_addr);
165 if (log_checksum(data) != 0)
166 fprintf (stderr, "invalid checksum at 0x%x\n",
167 block * 256 + received_addr);
172 tick = data[2] + (data[3] << 8);
173 a = data[4] + (data[5] << 8);
174 b = data[6] + (data[7] << 8);
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");
185 fprintf(out, "%s\n", serial_line);
188 if (cmd == 'S' && a <= 8) {
189 if (column) putchar('\n');
190 printf("%s\n", state_names[a]);
194 fprintf(out, "%c %4x %4x %4x\n",
196 if (cmd == 'S' && a == 8) {