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