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