Update ao-view to add GPS satellite tracking data
[fw/altos] / ao-view / 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_log_new();
35 }
36
37 static void
38 aoview_parse_string(char *target, int len, char *source)
39 {
40         strncpy(target, source, len-1);
41         target[len-1] = '\0';
42 }
43
44 static void
45 aoview_parse_int(int *target, char *source)
46 {
47         *target = strtol(source, NULL, 0);
48 }
49
50 static void
51 aoview_parse_hex(int *target, char *source)
52 {
53         *target = strtol(source, NULL, 16);
54 }
55
56 static void
57 aoview_parse_pos(double *target, char *source)
58 {
59         int     deg;
60         double  min;
61         char    dir;
62         double  r;
63
64         if (sscanf(source, "%d°%lf'%c", &deg, &min, &dir) != 3) {
65                 *target = 0;
66                 return;
67         }
68         r = deg + min / 60.0;
69         if (dir == 'S' || dir == 'W')
70                 r = -r;
71         *target = r;
72 }
73
74 #define PARSE_MAX_WORDS 256
75
76 gboolean
77 aoview_monitor_parse(const char *input_line)
78 {
79         char *saveptr;
80         char *words[PARSE_MAX_WORDS];
81         int nword;
82         char line_buf[8192], *line;
83         struct aodata   data;
84         int     tracking_pos;
85
86         /* avoid smashing our input parameter */
87         strncpy (line_buf, input_line, sizeof (line_buf)-1);
88         line_buf[sizeof(line_buf) - 1] = '\0';
89         line = line_buf;
90         for (nword = 0; nword < PARSE_MAX_WORDS; nword++) {
91                 words[nword] = strtok_r(line, " \t\n", &saveptr);
92                 line = NULL;
93                 if (words[nword] == NULL)
94                         break;
95         }
96         if (nword < 36)
97                 return FALSE;
98         if (strcmp(words[0], "CALL") != 0)
99                 return FALSE;
100         aoview_parse_string(data.callsign, sizeof (data.callsign), words[1]);
101         aoview_parse_int(&data.serial, words[3]);
102
103         aoview_parse_int(&data.rssi, words[5]);
104         aoview_parse_string(data.state, sizeof (data.state), words[9]);
105         aoview_parse_int(&data.tick, words[10]);
106         aoview_parse_int(&data.accel, words[12]);
107         aoview_parse_int(&data.pres, words[14]);
108         aoview_parse_int(&data.temp, words[16]);
109         aoview_parse_int(&data.batt, words[18]);
110         aoview_parse_int(&data.drogue, words[20]);
111         aoview_parse_int(&data.main, words[22]);
112         aoview_parse_int(&data.flight_accel, words[24]);
113         aoview_parse_int(&data.ground_accel, words[26]);
114         aoview_parse_int(&data.flight_vel, words[28]);
115         aoview_parse_int(&data.flight_pres, words[30]);
116         aoview_parse_int(&data.ground_pres, words[32]);
117         aoview_parse_int(&data.gps.nsat, words[34]);
118         if (strcmp (words[36], "unlocked") == 0) {
119                 data.gps.gps_connected = 1;
120                 data.gps.gps_locked = 0;
121                 data.gps.gps_time.hour = data.gps.gps_time.minute = data.gps.gps_time.second = 0;
122                 data.gps.lat = data.gps.lon = 0;
123                 data.gps.alt = 0;
124                 tracking_pos = 37;
125         } else if (nword >= 40) {
126                 data.gps.gps_locked = 1;
127                 data.gps.gps_connected = 1;
128                 sscanf(words[36], "%d:%d:%d", &data.gps.gps_time.hour, &data.gps.gps_time.minute, &data.gps.gps_time.second);
129                 aoview_parse_pos(&data.gps.lat, words[37]);
130                 aoview_parse_pos(&data.gps.lon, words[38]);
131                 sscanf(words[39], "%dm", &data.gps.alt);
132                 tracking_pos = 46;
133         } else {
134                 data.gps.gps_connected = 0;
135                 data.gps.gps_locked = 0;
136                 data.gps.gps_time.hour = data.gps.gps_time.minute = data.gps.gps_time.second = 0;
137                 data.gps.lat = data.gps.lon = 0;
138                 data.gps.alt = 0;
139         }
140         if (nword >= 46) {
141                 data.gps.gps_extended = 1;
142                 sscanf(words[40], "%lfm/s", &data.gps.ground_speed);
143                 sscanf(words[41], "%d", &data.gps.course);
144                 sscanf(words[42], "%lfm/s", &data.gps.climb_rate);
145                 sscanf(words[43], "%lf", &data.gps.hdop);
146                 sscanf(words[44], "%d", &data.gps.h_error);
147                 sscanf(words[45], "%d", &data.gps.v_error);
148         } else {
149                 data.gps.gps_extended = 0;
150                 data.gps.ground_speed = 0;
151                 data.gps.course = 0;
152                 data.gps.climb_rate = 0;
153                 data.gps.hdop = 0;
154                 data.gps.h_error = 0;
155                 data.gps.v_error = 0;
156         }
157         if (nword >= tracking_pos + 2 && strcmp(words[tracking_pos], "SAT") == 0) {
158                 int     c, n, pos;
159                 aoview_parse_int(&n, words[tracking_pos + 1]);
160                 pos = tracking_pos + 2;
161                 if (nword >= pos + n * 3) {
162                         data.gps_tracking.channels = n;
163                         for (c = 0; c < n; c++) {
164                                 aoview_parse_int(&data.gps_tracking.sats[c].svid,
165                                                  words[pos + 0]);
166                                 aoview_parse_hex(&data.gps_tracking.sats[c].state,
167                                                  words[pos + 1]);
168                                 aoview_parse_int(&data.gps_tracking.sats[c].c_n0,
169                                                  words[pos + 2]);
170                                 pos += 3;
171                         }
172                 } else {
173                         data.gps_tracking.channels = 0;
174                 }
175         }
176         aoview_state_notify(&data);
177         return TRUE;
178 }
179
180 static void
181 aoview_monitor_callback(gpointer user_data,
182                         struct aoview_serial *serial,
183                         gint revents)
184 {
185         int     c;
186
187         if (revents & (G_IO_HUP|G_IO_ERR)) {
188                 aoview_monitor_disconnect();
189                 return;
190         }
191         if (revents & G_IO_IN) {
192                 for (;;) {
193                         c = aoview_serial_getc(serial);
194                         if (c == -1)
195                                 break;
196                         if (c == '\r')
197                                 continue;
198                         if (c == '\n') {
199                                 monitor_line[monitor_pos] = '\0';
200                                 if (monitor_pos) {
201                                         if (aoview_monitor_parse(monitor_line)) {
202                                                 aoview_log_set_serial(aostate.data.serial);
203                                                 if (aoview_log_get_serial())
204                                                         aoview_log_printf ("%s\n", monitor_line);
205                                         }
206                                 }
207                                 monitor_pos = 0;
208                         } else if (monitor_pos < MONITOR_LEN)
209                                 monitor_line[monitor_pos++] = c;
210                 }
211         }
212 }
213
214 gboolean
215 aoview_monitor_connect(char *tty)
216 {
217         aoview_monitor_disconnect();
218         monitor_serial = aoview_serial_open(tty);
219         if (!monitor_serial)
220                 return FALSE;
221         aoview_table_clear();
222         aoview_state_reset();
223         aoview_serial_set_callback(monitor_serial,
224                                    aoview_monitor_callback);
225         return TRUE;
226 }