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