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