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