ao-tools: Fix warnings in ao-tools
[fw/altos] / ao-tools / ao-cal-freq / ao-cal-freq.c
1 /*
2  * Copyright © 2014 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 <err.h>
20 #include <fcntl.h>
21 #include <gelf.h>
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <sysexits.h>
26 #include <unistd.h>
27 #include <getopt.h>
28 #include <string.h>
29 #include <stdbool.h>
30 #include <termios.h>
31 #include <math.h>
32 #include "ao-elf.h"
33 #include "ccdbg.h"
34 #include "cc-usb.h"
35 #include "cc.h"
36 #include "ao-verbose.h"
37
38 static const struct option options[] = {
39         { .name = "tty", .has_arg = 1, .val = 'T' },
40         { .name = "device", .has_arg = 1, .val = 'D' },
41         { .name = "verbose", .has_arg = 0, .val = 'v' },
42         { .name = "output", .has_arg = 1, .val = 'o' },
43         { .name = "nosave", .has_arg = 0, .val = 'n' },
44         { 0, 0, 0, 0},
45 };
46
47 static void usage(char *program)
48 {
49         fprintf(stderr, "usage: %s [--verbose] [--nosave] [--device=<device>] [-tty=<tty>] [--output=<cal-value-file>]\n", program);
50         exit(1);
51 }
52
53 static char **
54 tok(char *line) {
55         char    **strs = malloc (sizeof (char *)), *str;
56         int     n = 0;
57
58         while ((str = strtok(line, " \t"))) {
59                 line = NULL;
60                 strs = realloc(strs, (n + 2) * sizeof (char *));
61                 strs[n] = strdup(str);
62                 n++;
63         }
64         strs[n] = '\0';
65         return strs;
66 }
67
68 struct flash {
69         struct flash    *next;
70         char            line[512];
71         char            **strs;
72 };
73
74 static struct flash *
75 flash(struct cc_usb *usb)
76 {
77         struct flash    *head = NULL, **tail = &head;
78         cc_usb_printf(usb, "c s\nv\n");
79         for (;;) {
80                 char    line[512];
81                 struct flash    *b;
82
83                 cc_usb_getline(usb, line, sizeof (line));
84                 b = malloc (sizeof (struct flash));
85                 strcpy(b->line, line);
86                 b->strs = tok(line);
87                 b->next = NULL;
88                 *tail = b;
89                 tail = &b->next;
90                 if (strstr(line, "software-version"))
91                         break;
92         }
93         return head;
94 }
95
96 static char **
97 find_flash(struct flash *b, char *word0) {
98         for (;b; b = b->next) {
99                 if (strstr(b->line, word0))
100                         return b->strs;
101         }
102         return NULL;
103 }
104
105 static int
106 do_save(struct cc_usb *usb)
107 {
108         int ret = 0;
109
110         printf("Saving calibration to device\n");
111         cc_usb_printf(usb, "c w\nv\n");
112         for (;;) {
113                 char    line[512];
114
115                 cc_usb_getline(usb, line, sizeof (line));
116                 if (strstr(line, "Nothing to save"))
117                         ret = 1;
118                 if (strstr(line, "Saved"))
119                         ret = 1;
120                 if (strstr(line, "software-version"))
121                         break;
122         }
123         if (!ret) {
124                 printf("Calibration save failed\n");
125         }
126         return ret;
127 }
128
129 static int
130 do_output(char *output, int cur_cal)
131 {
132         printf ("Saving calibration value to file \"%s\"\n", output);
133
134         FILE    *out = fopen(output, "w");
135         int     ret = 1;
136
137         if (!out) {
138                 perror(output);
139                 return 0;
140         }
141
142         if (fprintf(out, "%d\n", cur_cal) < 0) {
143                 perror("fprintf");
144                 ret = 0;
145         }
146         if (fflush(out) != 0) {
147                 perror("fflush");
148                 ret = 0;
149         }
150         if (fclose(out) != 0) {
151                 perror("fclose");
152                 ret = 0;
153         }
154         return ret;
155 }
156
157 static int
158 do_cal(char *tty, int save, char *output)
159 {
160         struct cc_usb *usb = NULL;
161         struct flash    *b;
162         char    line[1024];
163         double  measured_freq;
164         char    **cur_freq_words;
165         char    **cur_cal_words;
166         char    *line_end;
167         int     cur_freq;
168         int     cur_cal;
169         int     new_cal;
170         int     ret = 1;
171         int     changed = 0;
172
173         for(;;) {
174                 usb = cc_usb_open(tty);
175
176                 if (!usb) {
177                         fprintf(stderr, "failed to open device\n");
178                         ret = 0;
179                         break;
180                 }
181
182                 cc_usb_printf(usb, "E 0\n");
183
184                 b = flash(usb);
185
186                 cur_cal_words = find_flash(b, "Radio cal:");
187                 cur_freq_words = find_flash(b, "Frequency:");
188
189                 if (!cur_cal_words || !cur_freq_words) {
190                         fprintf(stderr, "no response\n");
191                         ret = 0;
192                         break;
193                 }
194
195                 cur_cal = atoi(cur_cal_words[2]);
196                 cur_freq = atoi(cur_freq_words[1]);
197
198                 printf ("Current radio calibration %d\n", cur_cal);
199                 printf ("Current radio frequency: %7.3f\n", cur_freq / 1000.0);
200
201                 cc_usb_sync(usb);
202                 cc_usb_printf(usb, "C 1\n");
203                 cc_usb_sync(usb);
204
205                 printf("Generating RF carrier. Please enter measured frequency [enter for done]: ");
206                 fflush(stdout);
207                 fgets(line, sizeof (line) - 1, stdin);
208                 cc_usb_printf(usb, "C 0\n");
209                 cc_usb_sync(usb);
210
211                 measured_freq = strtod(line, &line_end);
212                 if (line_end == line)
213                         break;
214
215                 new_cal = floor ((((double) cur_freq / 1000.0) / measured_freq) * cur_cal + 0.5);
216
217                 if (new_cal == cur_cal) {
218                         printf("Calibration value %d unchanged\n", cur_cal);
219                 } else {
220                         printf ("Setting cal value %d\n", new_cal);
221
222                         cc_usb_printf (usb, "c f %d\n", new_cal);
223                         changed = 1;
224                         cc_usb_sync(usb);
225                 }
226                 cc_usb_close(usb);
227         }
228         if (usb) {
229                 if (ret && save) {
230                         if (changed) {
231                                 if (!do_save(usb))
232                                         ret = 0;
233                         } else {
234                                 printf("Calibration unchanged, not saving\n");
235                         }
236                 }
237                 if (ret && output) {
238                         if (!do_output(output, cur_cal))
239                                 ret = 0;
240                 }
241                 cc_usb_close(usb);
242         }
243         return ret;
244 }
245
246 int
247 main (int argc, char **argv)
248 {
249         char                    *device = NULL;
250         int                     c;
251         char                    *tty = NULL;
252         int                     verbose = 0;
253         int                     save = 1;
254         int                     ret = 0;
255         char                    *output = NULL;
256
257         while ((c = getopt_long(argc, argv, "vnT:D:o:", options, NULL)) != -1) {
258                 switch (c) {
259                 case 'T':
260                         tty = optarg;
261                         break;
262                 case 'D':
263                         device = optarg;
264                         break;
265                 case 'v':
266                         verbose++;
267                         break;
268                 case 'n':
269                         save = 0;
270                         break;
271                 case 'o':
272                         output = optarg;
273                         break;
274                 default:
275                         usage(argv[0]);
276                         break;
277                 }
278         }
279
280         ao_verbose = verbose;
281         if (verbose)
282                 ccdbg_add_debug(CC_DEBUG_BITBANG);
283
284         if (!tty)
285                 tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
286         if (!tty)
287                 tty = cc_usbdevs_find_by_arg(device, "TeleMega");
288         if (!tty)
289                 tty = getenv("ALTOS_TTY");
290         if (!tty)
291                 tty="/dev/ttyACM0";
292
293         if (!do_cal(tty, save, output))
294                 ret = 1;
295         return ret;
296 }