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