Pass accel calibration over telemetry stream. Telemetry data format change.
[fw/altos] / ao-tools / 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 *raw_words[PARSE_MAX_WORDS];
81         char **words;
82         int version = 0;
83         int nword;
84         char line_buf[8192], *line;
85         struct aodata   data;
86         int     tracking_pos;
87         int channel;
88
89         /* avoid smashing our input parameter */
90         strncpy (line_buf, input_line, sizeof (line_buf)-1);
91         line_buf[sizeof(line_buf) - 1] = '\0';
92         line = line_buf;
93         for (nword = 0; nword < PARSE_MAX_WORDS; nword++) {
94                 raw_words[nword] = strtok_r(line, " \t\n", &saveptr);
95                 line = NULL;
96                 if (raw_words[nword] == NULL)
97                         break;
98         }
99         if (nword < 36)
100                 return FALSE;
101         words = raw_words;
102         if (strcmp(words[0], "VERSION") == 0) {
103                 aoview_parse_int(&version, words[1]);
104                 words += 2;
105                 nword -= 2;
106         }
107         if (strcmp(words[0], "CALL") != 0)
108                 return FALSE;
109         aoview_parse_string(data.callsign, sizeof (data.callsign), words[1]);
110         aoview_parse_int(&data.serial, words[3]);
111
112         aoview_parse_int(&data.rssi, words[5]);
113         aoview_parse_string(data.state, sizeof (data.state), words[9]);
114         aoview_parse_int(&data.tick, words[10]);
115         aoview_parse_int(&data.accel, words[12]);
116         aoview_parse_int(&data.pres, words[14]);
117         aoview_parse_int(&data.temp, words[16]);
118         aoview_parse_int(&data.batt, words[18]);
119         aoview_parse_int(&data.drogue, words[20]);
120         aoview_parse_int(&data.main, words[22]);
121         aoview_parse_int(&data.flight_accel, words[24]);
122         aoview_parse_int(&data.ground_accel, words[26]);
123         aoview_parse_int(&data.flight_vel, words[28]);
124         aoview_parse_int(&data.flight_pres, words[30]);
125         aoview_parse_int(&data.ground_pres, words[32]);
126         if (version >= 1) {
127                 aoview_parse_int(&data.accel_plus_g, words[34]);
128                 aoview_parse_int(&data.accel_minus_g, words[36]);
129                 words += 4;
130                 nword -= 4;
131         } else {
132                 data.accel_plus_g = data.ground_accel;
133                 data.accel_minus_g = data.ground_accel + 530;
134         }
135         aoview_parse_int(&data.gps.nsat, words[34]);
136         if (strcmp (words[36], "unlocked") == 0) {
137                 data.gps.gps_connected = 1;
138                 data.gps.gps_locked = 0;
139                 data.gps.gps_time.hour = data.gps.gps_time.minute = data.gps.gps_time.second = 0;
140                 data.gps.lat = data.gps.lon = 0;
141                 data.gps.alt = 0;
142                 tracking_pos = 37;
143         } else if (nword >= 40) {
144                 data.gps.gps_locked = 1;
145                 data.gps.gps_connected = 1;
146                 sscanf(words[36], "%d:%d:%d", &data.gps.gps_time.hour, &data.gps.gps_time.minute, &data.gps.gps_time.second);
147                 aoview_parse_pos(&data.gps.lat, words[37]);
148                 aoview_parse_pos(&data.gps.lon, words[38]);
149                 sscanf(words[39], "%dm", &data.gps.alt);
150                 tracking_pos = 46;
151         } else {
152                 data.gps.gps_connected = 0;
153                 data.gps.gps_locked = 0;
154                 data.gps.gps_time.hour = data.gps.gps_time.minute = data.gps.gps_time.second = 0;
155                 data.gps.lat = data.gps.lon = 0;
156                 data.gps.alt = 0;
157                 tracking_pos = -1;
158         }
159         if (nword >= 46) {
160                 data.gps.gps_extended = 1;
161                 sscanf(words[40], "%lfm/s", &data.gps.ground_speed);
162                 sscanf(words[41], "%d", &data.gps.course);
163                 sscanf(words[42], "%lfm/s", &data.gps.climb_rate);
164                 sscanf(words[43], "%lf", &data.gps.hdop);
165                 sscanf(words[44], "%d", &data.gps.h_error);
166                 sscanf(words[45], "%d", &data.gps.v_error);
167         } else {
168                 data.gps.gps_extended = 0;
169                 data.gps.ground_speed = 0;
170                 data.gps.course = 0;
171                 data.gps.climb_rate = 0;
172                 data.gps.hdop = 0;
173                 data.gps.h_error = 0;
174                 data.gps.v_error = 0;
175         }
176         if (tracking_pos >= 0 && nword >= tracking_pos + 2 && strcmp(words[tracking_pos], "SAT") == 0) {
177                 int     c, n, pos;
178                 aoview_parse_int(&n, words[tracking_pos + 1]);
179                 pos = tracking_pos + 2;
180                 if (nword >= pos + n * 3) {
181                         data.gps_tracking.channels = n;
182                         for (c = 0; c < n; c++) {
183                                 aoview_parse_int(&data.gps_tracking.sats[c].svid,
184                                                  words[pos + 0]);
185                                 aoview_parse_hex(&data.gps_tracking.sats[c].state,
186                                                  words[pos + 1]);
187                                 aoview_parse_int(&data.gps_tracking.sats[c].c_n0,
188                                                  words[pos + 2]);
189                                 pos += 3;
190                         }
191                 } else {
192                         data.gps_tracking.channels = 0;
193                 }
194         } else {
195                 data.gps_tracking.channels = 0;
196         }
197         aoview_state_notify(&data);
198         return TRUE;
199 }
200
201 static void
202 aoview_monitor_callback(gpointer user_data,
203                         struct aoview_serial *serial,
204                         gint revents)
205 {
206         int     c;
207
208         if (revents & (G_IO_HUP|G_IO_ERR)) {
209                 aoview_monitor_disconnect();
210                 return;
211         }
212         if (revents & G_IO_IN) {
213                 for (;;) {
214                         c = aoview_serial_getc(serial);
215                         if (c == -1)
216                                 break;
217                         if (c == '\r')
218                                 continue;
219                         if (c == '\n') {
220                                 monitor_line[monitor_pos] = '\0';
221                                 if (monitor_pos) {
222                                         if (aoview_monitor_parse(monitor_line)) {
223                                                 aoview_log_set_serial(aostate.data.serial);
224                                                 if (aoview_log_get_serial())
225                                                         aoview_log_printf ("%s\n", monitor_line);
226                                         }
227                                 }
228                                 monitor_pos = 0;
229                         } else if (monitor_pos < MONITOR_LEN)
230                                 monitor_line[monitor_pos++] = c;
231                 }
232         }
233 }
234
235 void
236 aoview_monitor_set_channel(int channel)
237 {
238         if (monitor_serial)
239                 aoview_serial_printf(monitor_serial, "c r %d\n", channel);
240 }
241
242 gboolean
243 aoview_monitor_connect(char *tty)
244 {
245         int     channel;
246         aoview_monitor_disconnect();
247         monitor_serial = aoview_serial_open(tty);
248         if (!monitor_serial)
249                 return FALSE;
250         aoview_table_clear();
251         aoview_state_reset();
252         channel = aoview_channel_current();
253         if (channel >= 0)
254                 aoview_monitor_set_channel(channel);
255         aoview_serial_set_callback(monitor_serial,
256                                    aoview_monitor_callback);
257         return TRUE;
258 }