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];
93 unsigned storage_size;
96 while ((c = getopt_long(argc, argv, "T:D:C:R", options, NULL)) != -1) {
108 channel = atoi(optarg);
117 tty = cc_usbdevs_find_by_arg(device, "TeleDongle");
119 tty = cc_usbdevs_find_by_arg(device, "TeleMetrum");
122 tty = getenv("ALTOS_TTY");
125 cc = cc_usb_open(tty);
129 cc_usb_open_remote(cc, channel);
130 /* send a 'version' command followed by a 'log' command */
131 cc_usb_printf(cc, "v\n");
134 cc_usb_getline(cc, line, sizeof (line));
135 if (sscanf(line, "serial-number %u", &serial_number) == 1)
136 strcpy(serial_line, line);
137 if (!strncmp(line, "software-version", 16))
140 if (!serial_number) {
141 fprintf(stderr, "no serial number found\n");
145 cc_usb_printf(cc, "f\n");
148 cc_usb_getline(cc, line, sizeof(line));
149 if (sscanf(line, "Storage size: %u", &storage_size) == 1)
152 printf ("Serial number: %d Storage size: %u\n", serial_number, storage_size);
154 blocks = storage_size / 256;
159 for (block = 0; !done && block < blocks; block++) {
160 cc_usb_printf(cc, "e %x\n", block);
165 putchar('.'); fflush(stdout); column++;
167 for (addr = 0; addr < 0x100;) {
168 cc_usb_getline(cc, line, sizeof (line));
169 if (sscanf(line, "00%x %x %x %x %x %x %x %x %x",
171 &data[0], &data[1], &data[2], &data[3],
172 &data[4], &data[5], &data[6], &data[7]) == 9)
174 if (received_addr != addr)
175 fprintf(stderr, "data out of sync at 0x%x\n",
176 block * 256 + received_addr);
178 if (log_checksum(data) != 0)
179 fprintf (stderr, "invalid checksum at 0x%x\n",
180 block * 256 + received_addr);
185 tick = data[2] + (data[3] << 8);
186 a = data[4] + (data[5] << 8);
187 b = data[6] + (data[7] << 8);
190 filename = cc_make_filename(serial_number, flight, "eeprom");
191 printf ("Flight: %d\n", flight);
192 printf ("File name: %s\n", filename);
193 out = fopen (filename, "w");
198 fprintf(out, "%s\n", serial_line);
201 if (cmd == 'S' && a <= 8) {
202 if (column) putchar('\n');
203 printf("%s\n", state_names[a]);
207 fprintf(out, "%c %4x %4x %4x\n",
209 if (cmd == 'S' && a == 8) {