ba2e9df79d452873f951516595125ccd7d40e49d
[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 }
35
36 static void
37 aoview_parse_string(char *target, int len, char *source)
38 {
39         strncpy(target, source, len-1);
40         target[len-1] = '\0';
41 }
42
43 static void
44 aoview_parse_int(int *target, char *source)
45 {
46         *target = strtol(source, NULL, 0);
47 }
48
49 static void
50 aoview_parse_pos(double *target, char *source)
51 {
52         int     deg;
53         double  min;
54         char    dir;
55         double  r;
56
57         if (sscanf(source, "%d°%lf'%c", &deg, &min, &dir) != 3) {
58                 *target = 0;
59                 return;
60         }
61         r = deg + min / 60.0;
62         if (dir == 'S' || dir == 'W')
63                 r = -r;
64         *target = r;
65 }
66
67 static void
68 aoview_monitor_parse(char *line)
69 {
70         char *saveptr;
71         char *words[64];
72         int nword;
73         struct aostate  state;
74
75         if (aoview_log_get_serial())
76                 aoview_log_printf ("%s\n", line);
77         for (nword = 0; nword < 64; nword++) {
78                 words[nword] = strtok_r(line, " \t\n", &saveptr);
79                 line = NULL;
80                 if (words[nword] == NULL)
81                         break;
82         }
83         if (nword < 26)
84                 return;
85         if (strcmp(words[0], "CALL") != 0)
86                 return;
87         aoview_parse_string(state.callsign, sizeof (state.callsign), words[1]);
88         aoview_parse_int(&state.serial, words[3]);
89         if (!aoview_log_get_serial())
90                 aoview_log_set_serial(state.serial);
91
92         aoview_parse_int(&state.rssi, words[5]);
93         aoview_parse_string(state.state, sizeof (state.state), words[9]);
94         aoview_parse_int(&state.tick, words[10]);
95         aoview_parse_int(&state.accel, words[12]);
96         aoview_parse_int(&state.pres, words[14]);
97         aoview_parse_int(&state.temp, words[16]);
98         aoview_parse_int(&state.batt, words[18]);
99         aoview_parse_int(&state.drogue, words[20]);
100         aoview_parse_int(&state.main, words[22]);
101         aoview_parse_int(&state.nsat, words[24]);
102         if (strcmp (words[26], "unlocked") != 0 && nword >= 29) {
103                 state.locked = 1;
104                 sscanf(words[26], "%d:%d:%d", &state.gps_time.hour, &state.gps_time.minute, &state.gps_time.second);
105                 aoview_parse_pos(&state.lat, words[27]);
106                 aoview_parse_pos(&state.lon, words[28]);
107                 sscanf(words[29], "%dm", &state.alt);
108         } else {
109                 state.locked = 0;
110                 state.gps_time.hour = state.gps_time.minute = state.gps_time.second = 0;
111                 state.lat = state.lon = 0;
112                 state.alt = 0;
113         }
114         aoview_state_notify(&state);
115 }
116
117 static gboolean
118 aoview_monitor_callback(void *user_data)
119 {
120         int     c;
121
122         if (!monitor_serial)
123                 return FALSE;
124
125         for (;;) {
126                 c = aoview_serial_getc(monitor_serial);
127                 if (c == -1)
128                         break;
129                 if (c == '\r')
130                         continue;
131                 if (c == '\n') {
132                         monitor_line[monitor_pos] = '\0';
133                         if (monitor_pos)
134                         aoview_monitor_parse(monitor_line);
135                         monitor_pos = 0;
136                 } else if (monitor_pos < MONITOR_LEN)
137                         monitor_line[monitor_pos++] = c;
138         }
139         return TRUE;
140 }
141
142 void
143 aoview_monitor_connect(char *tty)
144 {
145         aoview_monitor_disconnect();
146         monitor_serial = aoview_serial_open(tty);
147         aoview_serial_set_callback(monitor_serial,
148                                    aoview_monitor_callback,
149                                    monitor_serial,
150                                    NULL);
151 }