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