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