altosuilib: Get the Eeprom download progress bar working again
[fw/altos] / src / test / ao_gps_test.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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #define AO_GPS_TEST
19 #include "ao_host.h"
20 #include <termios.h>
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
27 #define AO_GPS_NUM_SAT_SHIFT    (0)
28
29 #define AO_GPS_NEW_DATA         1
30 #define AO_GPS_NEW_TRACKING     2
31
32 #define AO_GPS_VALID            (1 << 4)
33 #define AO_GPS_RUNNING          (1 << 5)
34 #define AO_GPS_DATE_VALID       (1 << 6)
35 #define AO_GPS_COURSE_VALID     (1 << 7)
36
37 struct ao_gps_orig {
38         uint8_t                 year;
39         uint8_t                 month;
40         uint8_t                 day;
41         uint8_t                 hour;
42         uint8_t                 minute;
43         uint8_t                 second;
44         uint8_t                 flags;
45         int32_t                 latitude;       /* degrees * 10⁷ */
46         int32_t                 longitude;      /* degrees * 10⁷ */
47         int16_t                 altitude;       /* m */
48         uint16_t                ground_speed;   /* cm/s */
49         uint8_t                 course;         /* degrees / 2 */
50         uint8_t                 hdop;           /* * 5 */
51         int16_t                 climb_rate;     /* cm/s */
52         uint16_t                h_error;        /* m */
53         uint16_t                v_error;        /* m */
54 };
55
56 #define AO_TELEMETRY_LOCATION_ALTITUDE(l)       ((l)->altitude)
57 #define AO_TELEMETRY_LOCATION_SET_ALTITUDE(l,a) ((l)->altitude = (a))
58
59 #define SIRF_SAT_STATE_ACQUIRED                 (1 << 0)
60 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID      (1 << 1)
61 #define SIRF_SAT_BIT_SYNC_COMPLETE              (1 << 2)
62 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE         (1 << 3)
63 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE        (1 << 4)
64 #define SIRF_SAT_CODE_LOCKED                    (1 << 5)
65 #define SIRF_SAT_ACQUISITION_FAILED             (1 << 6)
66 #define SIRF_SAT_EPHEMERIS_AVAILABLE            (1 << 7)
67
68 struct ao_gps_sat_orig {
69         uint8_t         svid;
70         uint8_t         c_n_1;
71 };
72
73 #define AO_MAX_GPS_TRACKING     12
74
75 struct ao_gps_tracking_orig {
76         uint8_t                 channels;
77         struct ao_gps_sat_orig  sats[AO_MAX_GPS_TRACKING];
78 };
79
80 #define ao_telemetry_location ao_gps_orig
81 #define ao_telemetry_satellite ao_gps_tracking_orig
82 #define ao_telemetry_satellite_info ao_gps_sat_orig
83
84 void
85 ao_mutex_get(uint8_t *mutex)
86 {
87 }
88
89 void
90 ao_mutex_put(uint8_t *mutex)
91 {
92 }
93
94 static int
95 ao_gps_fd;
96
97 #if 0
98 static void
99 ao_dbg_char(char c)
100 {
101         char    line[128];
102         line[0] = '\0';
103         if (c < ' ') {
104                 if (c == '\n')
105                         sprintf (line, "\n");
106                 else
107                         sprintf (line, "\\%02x", ((int) c) & 0xff);
108         } else {
109                 sprintf (line, "%c", c);
110         }
111         write(1, line, strlen(line));
112 }
113 #endif
114
115 #define QUEUE_LEN       4096
116
117 static char     input_queue[QUEUE_LEN];
118 int             input_head, input_tail;
119
120 #include <sys/time.h>
121
122 int
123 get_millis(void)
124 {
125         struct timeval  tv;
126         gettimeofday(&tv, NULL);
127         return tv.tv_sec * 1000 + tv.tv_usec / 1000;
128 }
129
130 static void
131 check_sirf_message(char *from, uint8_t *msg, int len)
132 {
133         uint16_t        encoded_len, encoded_cksum;
134         uint16_t        cksum;
135         uint8_t         id;
136         int             i;
137
138         if (msg[0] != 0xa0 || msg[1] != 0xa2) {
139                 printf ("bad header\n");
140                 return;
141         }
142         if (len < 7) {
143                 printf("short\n");
144                 return;
145         }
146         if (msg[len-1] != 0xb3 || msg[len-2] != 0xb0) {
147                 printf ("bad trailer\n");
148                 return;
149         }
150         encoded_len = (msg[2] << 8) | msg[3];
151         id = msg[4];
152 /*      printf ("%9d: %3d\n", get_millis(), id); */
153         if (encoded_len != len - 8) {
154                 if (id != 52)
155                         printf ("length mismatch (got %d, wanted %d)\n",
156                                 len - 8, encoded_len);
157                 return;
158         }
159         encoded_cksum = (msg[len - 4] << 8) | msg[len-3];
160         cksum = 0;
161         for (i = 4; i < len - 4; i++)
162                 cksum = (cksum + msg[i]) & 0x7fff;
163         if (encoded_cksum != cksum) {
164                 printf ("cksum mismatch (got %04x wanted %04x)\n",
165                         cksum, encoded_cksum);
166                 return;
167         }
168         id = msg[4];
169         switch (id) {
170         case 41:{
171                 int     off = 4;
172
173                 uint8_t         id;
174                 uint16_t        nav_valid;
175                 uint16_t        nav_type;
176                 uint16_t        week;
177                 uint32_t        tow;
178                 uint16_t        year;
179                 uint8_t         month;
180                 uint8_t         day;
181                 uint8_t         hour;
182                 uint8_t         minute;
183                 uint16_t        second;
184                 uint32_t        sat_list;
185                 int32_t         lat;
186                 int32_t         lon;
187                 int32_t         alt_ell;
188                 int32_t         alt_msl;
189                 int8_t          datum;
190                 uint16_t        sog;
191                 uint16_t        cog;
192                 int16_t         mag_var;
193                 int16_t         climb_rate;
194                 int16_t         heading_rate;
195                 uint32_t        h_error;
196                 uint32_t        v_error;
197                 uint32_t        t_error;
198                 uint16_t        h_v_error;
199
200 #define get_u8(u)       u = (msg[off]); off+= 1
201 #define get_u16(u)      u = (msg[off] << 8) | (msg[off + 1]); off+= 2
202 #define get_u32(u)      u = (msg[off] << 24) | (msg[off + 1] << 16) | (msg[off+2] << 8) | (msg[off+3]); off+= 4
203
204                 get_u8(id);
205                 get_u16(nav_valid);
206                 get_u16(nav_type);
207                 get_u16(week);
208                 get_u32(tow);
209                 get_u16(year);
210                 get_u8(month);
211                 get_u8(day);
212                 get_u8(hour);
213                 get_u8(minute);
214                 get_u16(second);
215                 get_u32(sat_list);
216                 get_u32(lat);
217                 get_u32(lon);
218                 get_u32(alt_ell);
219                 get_u32(alt_msl);
220                 get_u8(datum);
221                 get_u16(sog);
222                 get_u16(cog);
223                 get_u16(mag_var);
224                 get_u16(climb_rate);
225                 get_u16(heading_rate);
226                 get_u32(h_error);
227                 get_u32(v_error);
228                 get_u32(t_error);
229                 get_u16(h_v_error);
230
231
232                 (void) mag_var;
233                 (void) id;
234                 printf ("Geodetic Navigation Data (41):\n");
235                 printf ("\tNav valid %04x\n", nav_valid);
236                 printf ("\tNav type %04x\n", nav_type);
237                 printf ("\tWeek %5d", week);
238                 printf (" TOW %9d", tow);
239                 printf (" %4d-%2d-%2d %02d:%02d:%07.4f\n",
240                         year, month, day,
241                         hour, minute, second / 1000.0);
242                 printf ("\tsats: %08x\n", sat_list);
243                 printf ("\tlat: %g", lat / 1.0e7);
244                 printf (" lon: %g", lon / 1.0e7);
245                 printf (" alt_ell: %g", alt_ell / 100.0);
246                 printf (" alt_msll: %g", alt_msl / 100.0);
247                 printf (" datum: %d\n", datum);
248                 printf ("\tground speed: %g", sog / 100.0);
249                 printf (" course: %g", cog / 100.0);
250                 printf (" climb: %g", climb_rate / 100.0);
251                 printf (" heading rate: %g\n", heading_rate / 100.0);
252                 printf ("\th error: %g", h_error / 100.0);
253                 printf (" v error: %g", v_error / 100.0);
254                 printf (" t error: %g", t_error / 100.0);
255                 printf (" h vel error: %g\n", h_v_error / 100.0);
256                 break;
257         }
258         case 4: {
259                 int off = 4;
260                 uint8_t         id;
261                 int16_t         gps_week;
262                 uint32_t        gps_tow;
263                 uint8_t         channels;
264                 int             j, k;
265
266                 get_u8(id);
267                 get_u16(gps_week);
268                 get_u32(gps_tow);
269                 get_u8(channels);
270
271                 (void) id;
272                 printf ("Measured Tracker Data (4):\n");
273                 printf ("GPS week: %d\n", gps_week);
274                 printf ("GPS time of week: %d\n", gps_tow);
275                 printf ("channels: %d\n", channels);
276                 for (j = 0; j < 12; j++) {
277                         uint8_t svid, azimuth, elevation;
278                         uint16_t state;
279                         uint8_t c_n[10];
280                         get_u8(svid);
281                         get_u8(azimuth);
282                         get_u8(elevation);
283                         get_u16(state);
284                         for (k = 0; k < 10; k++) {
285                                 get_u8(c_n[k]);
286                         }
287                         printf ("Sat %3d:", svid);
288                         printf (" aziumuth: %6.1f", azimuth * 1.5);
289                         printf (" elevation: %6.1f", elevation * 0.5);
290                         printf (" state: 0x%02x", state);
291                         printf (" c_n:");
292                         for (k = 0; k < 10; k++)
293                                 printf(" %3d", c_n[k]);
294                         if (state & SIRF_SAT_STATE_ACQUIRED)
295                                 printf(" acq,");
296                         if (state & SIRF_SAT_STATE_CARRIER_PHASE_VALID)
297                                 printf(" car,");
298                         if (state & SIRF_SAT_BIT_SYNC_COMPLETE)
299                                 printf(" bit,");
300                         if (state & SIRF_SAT_SUBFRAME_SYNC_COMPLETE)
301                                 printf(" sub,");
302                         if (state & SIRF_SAT_CARRIER_PULLIN_COMPLETE)
303                                 printf(" pullin,");
304                         if (state & SIRF_SAT_CODE_LOCKED)
305                                 printf(" code,");
306                         if (state & SIRF_SAT_ACQUISITION_FAILED)
307                                 printf(" fail,");
308                         if (state & SIRF_SAT_EPHEMERIS_AVAILABLE)
309                                 printf(" ephem,");
310                         printf ("\n");
311                 }
312                 break;
313         }
314         default:
315                 return;
316                 printf ("%s %4d:", from, encoded_len);
317                 for (i = 4; i < len - 4; i++) {
318                         if (((i - 4) & 0xf) == 0)
319                                 printf("\n   ");
320                         printf (" %3d", msg[i]);
321                 }
322                 printf ("\n");
323         }
324 }
325
326 static uint8_t  sirf_message[4096];
327 static int      sirf_message_len;
328 static uint8_t  sirf_in_message[4096];
329 static int      sirf_in_len;
330
331 char
332 ao_serial1_getchar(void)
333 {
334         char    c;
335         uint8_t uc;
336
337         while (input_head == input_tail) {
338                 for (;;) {
339                         input_tail = read(ao_gps_fd, input_queue, QUEUE_LEN);
340                         if (input_tail < 0) {
341                                 if (errno == EINTR || errno == EAGAIN)
342                                         continue;
343                                 perror ("getchar");
344                                 exit (1);
345                         }
346                         input_head = 0;
347                         break;
348                 }
349         }
350         c = input_queue[input_head];
351         input_head = (input_head + 1) % QUEUE_LEN;
352         uc = c;
353         if (sirf_in_len || uc == 0xa0) {
354                 if (sirf_in_len < 4096)
355                         sirf_in_message[sirf_in_len++] = uc;
356                 if (uc == 0xb3) {
357                         check_sirf_message("recv", sirf_in_message, sirf_in_len);
358                         sirf_in_len = 0;
359                 }
360         }
361         return c;
362 }
363
364
365 void
366 ao_serial1_putchar(char c)
367 {
368         int     i;
369         uint8_t uc = (uint8_t) c;
370
371         if (sirf_message_len || uc == 0xa0) {
372                 if (sirf_message_len < 4096)
373                         sirf_message[sirf_message_len++] = uc;
374                 if (uc == 0xb3) {
375                         check_sirf_message("send", sirf_message, sirf_message_len);
376                         sirf_message_len = 0;
377                 }
378         }
379         for (;;) {
380                 i = write(ao_gps_fd, &c, 1);
381                 if (i == 1) {
382                         if ((uint8_t) c == 0xb3 || c == '\r') {
383 /*                              static const struct timespec delay = {
384                                         .tv_sec = 0,
385                                         .tv_nsec = 100 * 1000 * 1000
386                                 };
387 */
388                                 tcdrain(ao_gps_fd);
389 //                              nanosleep(&delay, NULL);
390                         }
391                         break;
392                 }
393                 if (i < 0 && (errno == EINTR || errno == EAGAIN))
394                         continue;
395                 perror("putchar");
396                 exit(1);
397         }
398 }
399
400 #define AO_SERIAL_SPEED_4800    0
401 #define AO_SERIAL_SPEED_57600   1
402 #define AO_SERIAL_SPEED_115200  2
403
404 static void
405 ao_serial1_set_speed(uint8_t speed)
406 {
407         int     fd = ao_gps_fd;
408         struct termios  termios;
409
410         tcdrain(fd);
411         tcgetattr(fd, &termios);
412         switch (speed) {
413         case AO_SERIAL_SPEED_4800:
414                 cfsetspeed(&termios, B4800);
415                 break;
416         case AO_SERIAL_SPEED_57600:
417                 cfsetspeed(&termios, B57600);
418                 break;
419         case AO_SERIAL_SPEED_115200:
420                 cfsetspeed(&termios, B115200);
421                 break;
422         }
423         tcsetattr(fd, TCSAFLUSH, &termios);
424         tcflush(fd, TCIFLUSH);
425 }
426
427 #define ao_time() 0
428
429 #include "ao_gps_print.c"
430 #include "ao_gps_sirf.c"
431
432 void
433 ao_dump_state(void *wchan)
434 {
435         int     i;
436
437         if (wchan != &ao_gps_new)
438                 return;
439
440         if (ao_gps_new & AO_GPS_NEW_DATA) {
441                 ao_gps_print(&ao_gps_data);
442                 putchar('\n');
443         }
444         if (ao_gps_new & AO_GPS_NEW_TRACKING) {
445                 ao_gps_tracking_print(&ao_gps_tracking_data);
446                 putchar('\n');
447         }
448         return;
449         printf ("%02d:%02d:%02d",
450                 ao_gps_data.hour, ao_gps_data.minute,
451                 ao_gps_data.second);
452         printf (" nsat %d %svalid",
453                 ao_gps_data.flags & AO_GPS_NUM_SAT_MASK,
454                 ao_gps_data.flags & AO_GPS_VALID ? "" : "not ");
455         printf (" lat %g lon %g alt %d",
456                 ao_gps_data.latitude / 1.0e7,
457                 ao_gps_data.longitude / 1.0e7,
458                 ao_gps_data.altitude);
459         printf (" speed %g climb %g course %d",
460                 ao_gps_data.ground_speed / 100.0,
461                 ao_gps_data.climb_rate / 100.0,
462                 ao_gps_data.course * 2);
463         printf (" hdop %g h_error %d v_error %d",
464                 ao_gps_data.hdop / 5.0,
465                 ao_gps_data.h_error, ao_gps_data.v_error);
466         printf("\n");
467         printf ("\t");
468         for (i = 0; i < 12; i++)
469                 printf (" %2d(%02d)",
470                         ao_gps_tracking_data.sats[i].svid,
471                         ao_gps_tracking_data.sats[i].c_n_1);
472         printf ("\n");
473 }
474
475 int
476 ao_gps_open(const char *tty)
477 {
478         struct termios  termios;
479         int fd;
480
481         fd = open (tty, O_RDWR);
482         if (fd < 0)
483                 return -1;
484
485         tcgetattr(fd, &termios);
486         cfmakeraw(&termios);
487         cfsetspeed(&termios, B4800);
488         tcsetattr(fd, TCSAFLUSH, &termios);
489
490         tcdrain(fd);
491         tcflush(fd, TCIFLUSH);
492         return fd;
493 }
494
495 #include <getopt.h>
496
497 static const struct option options[] = {
498         { .name = "tty", .has_arg = 1, .val = 'T' },
499         { 0, 0, 0, 0},
500 };
501
502 static void usage(char *program)
503 {
504         fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
505         exit(1);
506 }
507
508 int
509 main (int argc, char **argv)
510 {
511         char    *tty = "/dev/ttyUSB0";
512         int     c;
513
514         while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
515                 switch (c) {
516                 case 'T':
517                         tty = optarg;
518                         break;
519                 default:
520                         usage(argv[0]);
521                         break;
522                 }
523         }
524         ao_gps_fd = ao_gps_open(tty);
525         if (ao_gps_fd < 0) {
526                 perror (tty);
527                 exit (1);
528         }
529         ao_gps_setup();
530         ao_gps();
531         return 0;
532 }