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