Share telemetry parsing code in cc library.
authorKeith Packard <keithp@keithp.com>
Sun, 15 Nov 2009 06:24:37 +0000 (22:24 -0800)
committerKeith Packard <keithp@keithp.com>
Sun, 15 Nov 2009 06:24:37 +0000 (22:24 -0800)
ao-view had a private copy of the telemetry parsing code which
included the ability to parse the newer version of that file. Those
changes have been moved to the library version and the private copy removed.

Signed-off-by: Keith Packard <keithp@keithp.com>
ao-tools/ao-view/aoview.h
ao-tools/ao-view/aoview_monitor.c
ao-tools/ao-view/aoview_state.c
ao-tools/lib/cc-telem.c
ao-tools/lib/cc.h

index b6d5bcdf43147581b58f1d2bf48a11562dee0ead..b937df7c51575eee2482e8e2d424c85f3bbbedcb 100644 (file)
 #include <glade/glade.h>
 #include <gconf/gconf-client.h>
 
-struct aogps_time {
-       int hour;
-       int minute;
-       int second;
-};
-
-struct aogps {
-       int     nsat;
-       int     gps_locked;
-       int     gps_connected;
-       struct aogps_time gps_time;
-       double  lat;            /* degrees (+N -S) */
-       double  lon;            /* degrees (+E -W) */
-       int     alt;            /* m */
-
-       int     gps_extended;   /* has extra data */
-       double  ground_speed;   /* m/s */
-       int     course;         /* degrees */
-       double  climb_rate;     /* m/s */
-       double  hdop;           /* unitless? */
-       int     h_error;        /* m */
-       int     v_error;        /* m */
-};
-
-#define SIRF_SAT_STATE_ACQUIRED                        (1 << 0)
-#define SIRF_SAT_STATE_CARRIER_PHASE_VALID     (1 << 1)
-#define SIRF_SAT_BIT_SYNC_COMPLETE             (1 << 2)
-#define SIRF_SAT_SUBFRAME_SYNC_COMPLETE                (1 << 3)
-#define SIRF_SAT_CARRIER_PULLIN_COMPLETE       (1 << 4)
-#define SIRF_SAT_CODE_LOCKED                   (1 << 5)
-#define SIRF_SAT_ACQUISITION_FAILED            (1 << 6)
-#define SIRF_SAT_EPHEMERIS_AVAILABLE           (1 << 7)
-
-struct aogps_sat {
-       int     svid;
-       int     state;
-       int     c_n0;
-};
-
-struct aogps_tracking {
-       int                     channels;
-       struct aogps_sat        sats[12];
-};
-
-struct aodata {
-       char    callsign[16];
-       int     serial;
-       int     rssi;
-       char    state[16];
-       int     tick;
-       int     accel;
-       int     pres;
-       int     temp;
-       int     batt;
-       int     drogue;
-       int     main;
-       int     flight_accel;
-       int     ground_accel;
-       int     flight_vel;
-       int     flight_pres;
-       int     ground_pres;
-       int     accel_plus_g;
-       int     accel_minus_g;
-       struct aogps    gps;
-       struct aogps_tracking   gps_tracking;
-};
-
 struct aostate {
-       struct aodata   data;
+       struct cc_telem data;
 
        /* derived data */
 
-       struct aodata   prev_data;
+       struct cc_telem prev_data;
 
        double          report_time;
 
@@ -135,8 +68,8 @@ struct aostate {
        double  max_acceleration;
        double  max_speed;
 
-       struct aogps    gps;
-       struct aogps_tracking   gps_tracking;
+       struct cc_gps   gps;
+       struct cc_gps_tracking  gps_tracking;
 
        int     gps_valid;
        double  pad_lat;
@@ -201,7 +134,7 @@ void
 aoview_dev_dialog_init(GladeXML *xml);
 
 void
-aoview_state_notify(struct aodata *data);
+aoview_state_notify(struct cc_telem *data);
 
 void
 aoview_state_new(void);
index 6d57f5565212ff4fb13910134d554945a7b94cb4..0f4afb0f943dfd5ca2ef988307771afb78106dc0 100644 (file)
@@ -34,167 +34,14 @@ aoview_monitor_disconnect(void)
        aoview_log_new();
 }
 
-static void
-aoview_parse_string(char *target, int len, char *source)
-{
-       strncpy(target, source, len-1);
-       target[len-1] = '\0';
-}
-
-static void
-aoview_parse_int(int *target, char *source)
-{
-       *target = strtol(source, NULL, 0);
-}
-
-static void
-aoview_parse_hex(int *target, char *source)
-{
-       *target = strtol(source, NULL, 16);
-}
-
-static void
-aoview_parse_pos(double *target, char *source)
-{
-       int     deg;
-       double  min;
-       char    dir;
-       double  r;
-
-       if (sscanf(source, "%d°%lf'%c", &deg, &min, &dir) != 3) {
-               *target = 0;
-               return;
-       }
-       r = deg + min / 60.0;
-       if (dir == 'S' || dir == 'W')
-               r = -r;
-       *target = r;
-}
-
-#define PARSE_MAX_WORDS        256
-
 gboolean
 aoview_monitor_parse(const char *input_line)
 {
-       char *saveptr;
-       char *raw_words[PARSE_MAX_WORDS];
-       char **words;
-       int version = 0;
-       int nword;
-       char line_buf[8192], *line;
-       struct aodata   data;
-       int     tracking_pos;
-       int channel;
+       struct cc_telem telem;
 
-       /* 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 < PARSE_MAX_WORDS; nword++) {
-               raw_words[nword] = strtok_r(line, " \t\n", &saveptr);
-               line = NULL;
-               if (raw_words[nword] == NULL)
-                       break;
-       }
-       if (nword < 36)
-               return FALSE;
-       words = raw_words;
-       if (strcmp(words[0], "VERSION") == 0) {
-               aoview_parse_int(&version, words[1]);
-               words += 2;
-               nword -= 2;
-       }
-       if (strcmp(words[0], "CALL") != 0)
+       if (!cc_telem_parse(input_line, &telem))
                return FALSE;
-       aoview_parse_string(data.callsign, sizeof (data.callsign), words[1]);
-       aoview_parse_int(&data.serial, words[3]);
-
-       aoview_parse_int(&data.rssi, words[5]);
-       aoview_parse_string(data.state, sizeof (data.state), words[9]);
-       aoview_parse_int(&data.tick, words[10]);
-       aoview_parse_int(&data.accel, words[12]);
-       aoview_parse_int(&data.pres, words[14]);
-       aoview_parse_int(&data.temp, words[16]);
-       aoview_parse_int(&data.batt, words[18]);
-       aoview_parse_int(&data.drogue, words[20]);
-       aoview_parse_int(&data.main, words[22]);
-       aoview_parse_int(&data.flight_accel, words[24]);
-       aoview_parse_int(&data.ground_accel, words[26]);
-       aoview_parse_int(&data.flight_vel, words[28]);
-       aoview_parse_int(&data.flight_pres, words[30]);
-       aoview_parse_int(&data.ground_pres, words[32]);
-       if (version >= 1) {
-               aoview_parse_int(&data.accel_plus_g, words[34]);
-               aoview_parse_int(&data.accel_minus_g, words[36]);
-               words += 4;
-               nword -= 4;
-       } else {
-               data.accel_plus_g = data.ground_accel;
-               data.accel_minus_g = data.ground_accel + 530;
-       }
-       aoview_parse_int(&data.gps.nsat, words[34]);
-       if (strcmp (words[36], "unlocked") == 0) {
-               data.gps.gps_connected = 1;
-               data.gps.gps_locked = 0;
-               data.gps.gps_time.hour = data.gps.gps_time.minute = data.gps.gps_time.second = 0;
-               data.gps.lat = data.gps.lon = 0;
-               data.gps.alt = 0;
-               tracking_pos = 37;
-       } else if (nword >= 40) {
-               data.gps.gps_locked = 1;
-               data.gps.gps_connected = 1;
-               sscanf(words[36], "%d:%d:%d", &data.gps.gps_time.hour, &data.gps.gps_time.minute, &data.gps.gps_time.second);
-               aoview_parse_pos(&data.gps.lat, words[37]);
-               aoview_parse_pos(&data.gps.lon, words[38]);
-               sscanf(words[39], "%dm", &data.gps.alt);
-               tracking_pos = 46;
-       } else {
-               data.gps.gps_connected = 0;
-               data.gps.gps_locked = 0;
-               data.gps.gps_time.hour = data.gps.gps_time.minute = data.gps.gps_time.second = 0;
-               data.gps.lat = data.gps.lon = 0;
-               data.gps.alt = 0;
-               tracking_pos = -1;
-       }
-       if (nword >= 46) {
-               data.gps.gps_extended = 1;
-               sscanf(words[40], "%lfm/s", &data.gps.ground_speed);
-               sscanf(words[41], "%d", &data.gps.course);
-               sscanf(words[42], "%lfm/s", &data.gps.climb_rate);
-               sscanf(words[43], "%lf", &data.gps.hdop);
-               sscanf(words[44], "%d", &data.gps.h_error);
-               sscanf(words[45], "%d", &data.gps.v_error);
-       } else {
-               data.gps.gps_extended = 0;
-               data.gps.ground_speed = 0;
-               data.gps.course = 0;
-               data.gps.climb_rate = 0;
-               data.gps.hdop = 0;
-               data.gps.h_error = 0;
-               data.gps.v_error = 0;
-       }
-       if (tracking_pos >= 0 && nword >= tracking_pos + 2 && strcmp(words[tracking_pos], "SAT") == 0) {
-               int     c, n, pos;
-               aoview_parse_int(&n, words[tracking_pos + 1]);
-               pos = tracking_pos + 2;
-               if (nword >= pos + n * 3) {
-                       data.gps_tracking.channels = n;
-                       for (c = 0; c < n; c++) {
-                               aoview_parse_int(&data.gps_tracking.sats[c].svid,
-                                                words[pos + 0]);
-                               aoview_parse_hex(&data.gps_tracking.sats[c].state,
-                                                words[pos + 1]);
-                               aoview_parse_int(&data.gps_tracking.sats[c].c_n0,
-                                                words[pos + 2]);
-                               pos += 3;
-                       }
-               } else {
-                       data.gps_tracking.channels = 0;
-               }
-       } else {
-               data.gps_tracking.channels = 0;
-       }
-       aoview_state_notify(&data);
+       aoview_state_notify(&telem);
        return TRUE;
 }
 
index f8f0168546a7b5b488c370940226d2fa2aabd8b6..a7545c51366e3a4353b04637531047b0dc79bc83 100644 (file)
@@ -99,7 +99,7 @@ aoview_time(void)
  * Fill out the derived data fields
  */
 static void
-aoview_state_derive(struct aodata *data, struct aostate *state)
+aoview_state_derive(struct cc_telem *data, struct aostate *state)
 {
        int     i;
        double  new_height;
@@ -274,7 +274,7 @@ aoview_state_reset(void)
 }
 
 void
-aoview_state_notify(struct aodata *data)
+aoview_state_notify(struct cc_telem *data)
 {
        struct aostate *state = &aostate;
        aoview_state_derive(data, state);
index a6ac0313b03f173c49cf75859e0b05c9f3740f1b..f82ab961ef3e6de9ab31c5a22f10f43020c7c663 100644 (file)
@@ -62,7 +62,9 @@ int
 cc_telem_parse(const char *input_line, struct cc_telem *telem)
 {
        char *saveptr;
-       char *words[PARSE_MAX_WORDS];
+       char *raw_words[PARSE_MAX_WORDS];
+       char **words;
+       int version = 0;
        int nword;
        char line_buf[8192], *line;
        int     tracking_pos;
@@ -72,13 +74,20 @@ cc_telem_parse(const char *input_line, struct cc_telem *telem)
        line_buf[sizeof(line_buf) - 1] = '\0';
        line = line_buf;
        for (nword = 0; nword < PARSE_MAX_WORDS; nword++) {
-               words[nword] = strtok_r(line, " \t\n", &saveptr);
+               raw_words[nword] = strtok_r(line, " \t\n", &saveptr);
                line = NULL;
-               if (words[nword] == NULL)
+               if (raw_words[nword] == NULL)
                        break;
        }
        if (nword < 36)
                return FALSE;
+       words = raw_words;
+       if (strcmp(words[0], "VERSION") == 0) {
+               cc_parse_int(&version, words[1]);
+               words += 2;
+               nword -= 2;
+       }
+
        if (strcmp(words[0], "CALL") != 0)
                return FALSE;
        cc_parse_string(telem->callsign, sizeof (telem->callsign), words[1]);
@@ -98,6 +107,15 @@ cc_telem_parse(const char *input_line, struct cc_telem *telem)
        cc_parse_int(&telem->flight_vel, words[28]);
        cc_parse_int(&telem->flight_pres, words[30]);
        cc_parse_int(&telem->ground_pres, words[32]);
+       if (version >= 1) {
+               cc_parse_int(&telem->accel_plus_g, words[34]);
+               cc_parse_int(&telem->accel_minus_g, words[36]);
+               words += 4;
+               nword -= 4;
+       } else {
+               telem->accel_plus_g = telem->ground_accel;
+               telem->accel_minus_g = telem->ground_accel + 530;
+       }
        cc_parse_int(&telem->gps.nsat, words[34]);
        if (strcmp (words[36], "unlocked") == 0) {
                telem->gps.gps_connected = 1;
index b5f1132fcc1e4d6ac28f2b4b1e696780e93b981c..fd461e5c6239e8ee05415cb6b6ecf837302c89ba 100644 (file)
@@ -252,6 +252,8 @@ struct cc_telem {
        int     flight_vel;
        int     flight_pres;
        int     ground_pres;
+       int     accel_plus_g;
+       int     accel_minus_g;
        struct cc_gps   gps;
        struct cc_gps_tracking  gps_tracking;
 };