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