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