2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 static struct aoview_serial *monitor_serial;
22 #define MONITOR_LEN 1024
24 static char monitor_line[MONITOR_LEN + 1];
25 static int monitor_pos;
28 aoview_monitor_disconnect(void)
31 aoview_serial_close(monitor_serial);
32 monitor_serial = NULL;
38 aoview_parse_string(char *target, int len, char *source)
40 strncpy(target, source, len-1);
45 aoview_parse_int(int *target, char *source)
47 *target = strtol(source, NULL, 0);
51 aoview_parse_hex(int *target, char *source)
53 *target = strtol(source, NULL, 16);
57 aoview_parse_pos(double *target, char *source)
64 if (sscanf(source, "%d°%lf'%c", °, &min, &dir) != 3) {
69 if (dir == 'S' || dir == 'W')
74 #define PARSE_MAX_WORDS 256
77 aoview_monitor_parse(const char *input_line)
80 char *words[PARSE_MAX_WORDS];
82 char line_buf[8192], *line;
87 /* avoid smashing our input parameter */
88 strncpy (line_buf, input_line, sizeof (line_buf)-1);
89 line_buf[sizeof(line_buf) - 1] = '\0';
91 for (nword = 0; nword < PARSE_MAX_WORDS; nword++) {
92 words[nword] = strtok_r(line, " \t\n", &saveptr);
94 if (words[nword] == NULL)
99 if (strcmp(words[0], "CALL") != 0)
101 aoview_parse_string(data.callsign, sizeof (data.callsign), words[1]);
102 aoview_parse_int(&data.serial, words[3]);
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;
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);
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;
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);
151 data.gps.gps_extended = 0;
152 data.gps.ground_speed = 0;
154 data.gps.climb_rate = 0;
156 data.gps.h_error = 0;
157 data.gps.v_error = 0;
159 if (tracking_pos >= 0 && nword >= tracking_pos + 2 && strcmp(words[tracking_pos], "SAT") == 0) {
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,
168 aoview_parse_hex(&data.gps_tracking.sats[c].state,
170 aoview_parse_int(&data.gps_tracking.sats[c].c_n0,
175 data.gps_tracking.channels = 0;
178 data.gps_tracking.channels = 0;
180 aoview_state_notify(&data);
185 aoview_monitor_callback(gpointer user_data,
186 struct aoview_serial *serial,
191 if (revents & (G_IO_HUP|G_IO_ERR)) {
192 aoview_monitor_disconnect();
195 if (revents & G_IO_IN) {
197 c = aoview_serial_getc(serial);
203 monitor_line[monitor_pos] = '\0';
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);
212 } else if (monitor_pos < MONITOR_LEN)
213 monitor_line[monitor_pos++] = c;
219 aoview_monitor_set_channel(int channel)
222 aoview_serial_printf(monitor_serial, "c r %d\n", channel);
226 aoview_monitor_connect(char *tty)
229 aoview_monitor_disconnect();
230 monitor_serial = aoview_serial_open(tty);
233 aoview_table_clear();
234 aoview_state_reset();
235 channel = aoview_channel_current();
237 aoview_monitor_set_channel(channel);
238 aoview_serial_set_callback(monitor_serial,
239 aoview_monitor_callback);