Fix aoview telemetry GPS parsing code to use correct columns
[fw/altos] / aoview / aoview_monitor.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 #include "aoview.h"
19
20 static struct aoview_serial *monitor_serial;
21
22 #define MONITOR_LEN     1024
23
24 static char     monitor_line[MONITOR_LEN + 1];
25 static int      monitor_pos;
26
27 void
28 aoview_monitor_disconnect(void)
29 {
30         if (monitor_serial) {
31                 aoview_serial_close(monitor_serial);
32                 monitor_serial = NULL;
33         }
34         aoview_table_clear();
35         aoview_log_new();
36 }
37
38 static void
39 aoview_parse_string(char *target, int len, char *source)
40 {
41         strncpy(target, source, len-1);
42         target[len-1] = '\0';
43 }
44
45 static void
46 aoview_parse_int(int *target, char *source)
47 {
48         *target = strtol(source, NULL, 0);
49 }
50
51 static void
52 aoview_parse_pos(double *target, char *source)
53 {
54         int     deg;
55         double  min;
56         char    dir;
57         double  r;
58
59         if (sscanf(source, "%d°%lf'%c", &deg, &min, &dir) != 3) {
60                 *target = 0;
61                 return;
62         }
63         r = deg + min / 60.0;
64         if (dir == 'S' || dir == 'W')
65                 r = -r;
66         *target = r;
67 }
68
69 static void
70 aoview_monitor_parse(char *line)
71 {
72         char *saveptr;
73         char *words[64];
74         int nword;
75         struct aostate  state;
76
77         if (aoview_log_get_serial())
78                 aoview_log_printf ("%s\n", line);
79         for (nword = 0; nword < 64; nword++) {
80                 words[nword] = strtok_r(line, " \t\n", &saveptr);
81                 line = NULL;
82                 if (words[nword] == NULL)
83                         break;
84         }
85         if (nword < 36)
86                 return;
87         if (strcmp(words[0], "CALL") != 0)
88                 return;
89         aoview_parse_string(state.callsign, sizeof (state.callsign), words[1]);
90         aoview_parse_int(&state.serial, words[3]);
91         if (!aoview_log_get_serial())
92                 aoview_log_set_serial(state.serial);
93
94         aoview_parse_int(&state.rssi, words[5]);
95         aoview_parse_string(state.state, sizeof (state.state), words[9]);
96         aoview_parse_int(&state.tick, words[10]);
97         aoview_parse_int(&state.accel, words[12]);
98         aoview_parse_int(&state.pres, words[14]);
99         aoview_parse_int(&state.temp, words[16]);
100         aoview_parse_int(&state.batt, words[18]);
101         aoview_parse_int(&state.drogue, words[20]);
102         aoview_parse_int(&state.main, words[22]);
103         aoview_parse_int(&state.flight_accel, words[24]);
104         aoview_parse_int(&state.ground_accel, words[26]);
105         aoview_parse_int(&state.flight_vel, words[28]);
106         aoview_parse_int(&state.flight_pres, words[30]);
107         aoview_parse_int(&state.ground_pres, words[32]);
108         aoview_parse_int(&state.nsat, words[34]);
109         if (strcmp (words[36], "unlocked") != 0 && nword >= 40) {
110                 state.locked = 1;
111                 sscanf(words[36], "%d:%d:%d", &state.gps_time.hour, &state.gps_time.minute, &state.gps_time.second);
112                 aoview_parse_pos(&state.lat, words[37]);
113                 aoview_parse_pos(&state.lon, words[38]);
114                 sscanf(words[39], "%dm", &state.alt);
115         } else {
116                 state.locked = 0;
117                 state.gps_time.hour = state.gps_time.minute = state.gps_time.second = 0;
118                 state.lat = state.lon = 0;
119                 state.alt = 0;
120         }
121         aoview_state_notify(&state);
122 }
123
124 static void
125 aoview_monitor_callback(gpointer user_data,
126                         struct aoview_serial *serial,
127                         gint revents)
128 {
129         int     c;
130
131         if (revents & (G_IO_HUP|G_IO_ERR)) {
132                 aoview_monitor_disconnect();
133                 return;
134         }
135         if (revents & G_IO_IN) {
136                 for (;;) {
137                         c = aoview_serial_getc(serial);
138                         if (c == -1)
139                                 break;
140                         if (c == '\r')
141                                 continue;
142                         if (c == '\n') {
143                                 monitor_line[monitor_pos] = '\0';
144                                 if (monitor_pos)
145                                 aoview_monitor_parse(monitor_line);
146                                 monitor_pos = 0;
147                         } else if (monitor_pos < MONITOR_LEN)
148                                 monitor_line[monitor_pos++] = c;
149                 }
150         }
151 }
152
153 gboolean
154 aoview_monitor_connect(char *tty)
155 {
156         aoview_monitor_disconnect();
157         monitor_serial = aoview_serial_open(tty);
158         if (!monitor_serial)
159                 return FALSE;
160         aoview_serial_set_callback(monitor_serial,
161                                    aoview_monitor_callback,
162                                    monitor_serial,
163                                    NULL);
164         return TRUE;
165 }