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_pos(double *target, char *source)
58 if (sscanf(source, "%d°%lf'%c", °, &min, &dir) != 3) {
63 if (dir == 'S' || dir == 'W')
69 aoview_monitor_parse(const char *input_line)
74 char line_buf[8192], *line;
77 /* avoid smashing our input parameter */
78 strncpy (line_buf, input_line, sizeof (line_buf)-1);
79 line_buf[sizeof(line_buf) - 1] = '\0';
81 for (nword = 0; nword < 64; nword++) {
82 words[nword] = strtok_r(line, " \t\n", &saveptr);
84 if (words[nword] == NULL)
89 if (strcmp(words[0], "CALL") != 0)
91 aoview_parse_string(data.callsign, sizeof (data.callsign), words[1]);
92 aoview_parse_int(&data.serial, words[3]);
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.gps.nsat, words[34]);
109 if (strcmp (words[36], "unlocked") == 0) {
110 data.gps.gps_connected = 1;
111 data.gps.gps_locked = 0;
112 data.gps.gps_time.hour = data.gps.gps_time.minute = data.gps.gps_time.second = 0;
113 data.gps.lat = data.gps.lon = 0;
115 } else if (nword >= 40) {
116 data.gps.gps_locked = 1;
117 data.gps.gps_connected = 1;
118 sscanf(words[36], "%d:%d:%d", &data.gps.gps_time.hour, &data.gps.gps_time.minute, &data.gps.gps_time.second);
119 aoview_parse_pos(&data.gps.lat, words[37]);
120 aoview_parse_pos(&data.gps.lon, words[38]);
121 sscanf(words[39], "%dm", &data.gps.alt);
123 data.gps.gps_connected = 0;
124 data.gps.gps_locked = 0;
125 data.gps.gps_time.hour = data.gps.gps_time.minute = data.gps.gps_time.second = 0;
126 data.gps.lat = data.gps.lon = 0;
130 data.gps.gps_extended = 1;
131 sscanf(words[40], "%lfm/s", &data.gps.ground_speed);
132 sscanf(words[41], "%d", &data.gps.course);
133 sscanf(words[42], "%lfm/s", &data.gps.climb_rate);
134 sscanf(words[43], "%lf", &data.gps.hdop);
135 sscanf(words[44], "%d", &data.gps.h_error);
136 sscanf(words[45], "%d", &data.gps.v_error);
138 data.gps.gps_extended = 0;
139 data.gps.ground_speed = 0;
141 data.gps.climb_rate = 0;
143 data.gps.h_error = 0;
144 data.gps.v_error = 0;
146 aoview_state_notify(&data);
151 aoview_monitor_callback(gpointer user_data,
152 struct aoview_serial *serial,
157 if (revents & (G_IO_HUP|G_IO_ERR)) {
158 aoview_monitor_disconnect();
161 if (revents & G_IO_IN) {
163 c = aoview_serial_getc(serial);
169 monitor_line[monitor_pos] = '\0';
171 if (aoview_monitor_parse(monitor_line)) {
172 aoview_log_set_serial(aostate.data.serial);
173 if (aoview_log_get_serial())
174 aoview_log_printf ("%s\n", monitor_line);
178 } else if (monitor_pos < MONITOR_LEN)
179 monitor_line[monitor_pos++] = c;
185 aoview_monitor_connect(char *tty)
187 aoview_monitor_disconnect();
188 monitor_serial = aoview_serial_open(tty);
191 aoview_table_clear();
192 aoview_state_reset();
193 aoview_serial_set_callback(monitor_serial,
194 aoview_monitor_callback);