Dont smash aoview_monitor_parse input buffer
[fw/altos] / aoview / aoview_monitor.c
index 7a04c82f88a653e31d32f189ab9104bda94c0a33..5810be5b91fb006117ac57f048421685a1a86930 100644 (file)
@@ -31,7 +31,6 @@ aoview_monitor_disconnect(void)
                aoview_serial_close(monitor_serial);
                monitor_serial = NULL;
        }
-       aoview_table_clear();
        aoview_log_new();
 }
 
@@ -66,16 +65,20 @@ aoview_parse_pos(double *target, char *source)
        *target = r;
 }
 
-static void
-aoview_monitor_parse(char *line)
+static struct aostate  state;
+
+gboolean
+aoview_monitor_parse(const char *input_line)
 {
        char *saveptr;
        char *words[64];
        int nword;
-       struct aostate  state;
+       char line_buf[8192], *line;
 
-       if (aoview_log_get_serial())
-               aoview_log_printf ("%s\n", line);
+       /* avoid smashing our input parameter */
+       strncpy (line_buf, input_line, sizeof (line_buf)-1);
+       line_buf[sizeof(line_buf) - 1] = '\0';
+       line = line_buf;
        for (nword = 0; nword < 64; nword++) {
                words[nword] = strtok_r(line, " \t\n", &saveptr);
                line = NULL;
@@ -83,13 +86,11 @@ aoview_monitor_parse(char *line)
                        break;
        }
        if (nword < 36)
-               return;
+               return FALSE;
        if (strcmp(words[0], "CALL") != 0)
-               return;
+               return FALSE;
        aoview_parse_string(state.callsign, sizeof (state.callsign), words[1]);
        aoview_parse_int(&state.serial, words[3]);
-       if (!aoview_log_get_serial())
-               aoview_log_set_serial(state.serial);
 
        aoview_parse_int(&state.rssi, words[5]);
        aoview_parse_string(state.state, sizeof (state.state), words[9]);
@@ -106,19 +107,41 @@ aoview_monitor_parse(char *line)
        aoview_parse_int(&state.flight_pres, words[30]);
        aoview_parse_int(&state.ground_pres, words[32]);
        aoview_parse_int(&state.nsat, words[34]);
-       if (strcmp (words[36], "unlocked") != 0 && nword >= 41) {
+       if (strcmp (words[36], "unlocked") != 0 && nword >= 40) {
                state.locked = 1;
                sscanf(words[36], "%d:%d:%d", &state.gps_time.hour, &state.gps_time.minute, &state.gps_time.second);
-               aoview_parse_pos(&state.lat, words[39]);
-               aoview_parse_pos(&state.lon, words[40]);
-               sscanf(words[41], "%dm", &state.alt);
+               aoview_parse_pos(&state.lat, words[37]);
+               aoview_parse_pos(&state.lon, words[38]);
+               sscanf(words[39], "%dm", &state.alt);
        } else {
                state.locked = 0;
                state.gps_time.hour = state.gps_time.minute = state.gps_time.second = 0;
                state.lat = state.lon = 0;
                state.alt = 0;
        }
+       if (nword >= 46) {
+               sscanf(words[40], "%lfm/s", &state.ground_speed);
+               sscanf(words[41], "%d", &state.course);
+               sscanf(words[42], "%lfm/s", &state.climb_rate);
+               sscanf(words[43], "%lf", &state.hdop);
+               sscanf(words[44], "%d", &state.h_error);
+               sscanf(words[45], "%d", &state.v_error);
+       } else {
+               state.ground_speed = 0;
+               state.course = 0;
+               state.climb_rate = 0;
+               state.hdop = 0;
+               state.h_error = 0;
+               state.v_error = 0;
+       }
        aoview_state_notify(&state);
+       return TRUE;
+}
+
+void
+aoview_monitor_reset(void)
+{
+       memset(&state, '\0', sizeof (state));
 }
 
 static void
@@ -141,8 +164,13 @@ aoview_monitor_callback(gpointer user_data,
                                continue;
                        if (c == '\n') {
                                monitor_line[monitor_pos] = '\0';
-                               if (monitor_pos)
-                               aoview_monitor_parse(monitor_line);
+                               if (monitor_pos) {
+                                       if (aoview_monitor_parse(monitor_line)) {
+                                               aoview_log_set_serial(state.serial);
+                                               if (aoview_log_get_serial())
+                                                       aoview_log_printf ("%s\n", monitor_line);
+                                       }
+                               }
                                monitor_pos = 0;
                        } else if (monitor_pos < MONITOR_LEN)
                                monitor_line[monitor_pos++] = c;
@@ -157,6 +185,8 @@ aoview_monitor_connect(char *tty)
        monitor_serial = aoview_serial_open(tty);
        if (!monitor_serial)
                return FALSE;
+       aoview_table_clear();
+       aoview_monitor_reset();
        aoview_serial_set_callback(monitor_serial,
                                   aoview_monitor_callback,
                                   monitor_serial,