gpsd-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gpsd-dev] [PATCH 5/6] Add a wind data structure


From: chris
Subject: [gpsd-dev] [PATCH 5/6] Add a wind data structure
Date: Sat, 21 Apr 2012 10:34:21 +0100

From: Christian Gagneraud <address@hidden>

---
 gps.h         |   23 +++++++++++++++++++++--
 gps_json.h    |    1 +
 gpsd.c        |    2 +-
 gpsd_json.c   |   44 ++++++++++++++++++++++++++++++++++++++++++++
 libgps_json.c |   36 ++++++++++++++++++++++++++++++++++++
 5 files changed, 103 insertions(+), 3 deletions(-)

diff --git a/gps.h b/gps.h
index d4813f5..47d4fe9 100644
--- a/gps.h
+++ b/gps.h
@@ -1526,6 +1526,23 @@ struct attitude_t {
     char yaw_st;
 };
 
+struct wind_t {
+    int speed_type;
+#define WIND_SPEED_UNKNOWN 0
+#define WIND_SPEED_VESSEL  1 /* Speed is relative to the (moving) vessel */
+#define WIND_SPEED_WATER   2 /* Speed is relative to the (moving) water */
+#define WIND_SPEED_GROUND  3 /* Speed is relative to the (fixed) ground */
+    double speed;            /* Meter per second */
+    int dir_type;
+#define WIND_DIR_UNKNOWN   0
+#define WIND_DIR_NORTH     1 /* dir is relative to true north, dir_mag to mag 
north */
+#define WIND_DIR_HEADING   2 /* dir is relative to the vessel's heading, 
dir_mag not used */
+#define WIND_DIR_VESSEL    3 /* dir is relative to the vessel's 
bow/centerline, dir_mag not used */
+#define WIND_DIR_SENSOR    4 /* dir is relative to the sensor, dir_mag not 
used */
+    double dir;              /* Degree */
+    double dir_mag;          /* Degree */
+};
+
 struct dop_t {
     /* Dilution of precision factors */
     double xdop, ydop, pdop, hdop, vdop, tdop, gdop;
@@ -1642,7 +1659,8 @@ struct gps_data_t {
 #define POLICY_SET     (1llu<<29)
 #define LOGMESSAGE_SET (1llu<<30)
 #define ERROR_SET      (1llu<<31)
-#define SET_HIGH_BIT   31
+#define WIND_SET       (1llu<<32)
+#define SET_HIGH_BIT   32
     timestamp_t online;                /* NZ if GPS is on line, 0 if not.
                                 *
                                 * Note: gpsd clears this time when sentences
@@ -1693,7 +1711,7 @@ struct gps_data_t {
     char tag[MAXTAGLEN+1];     /* tag of last sentence processed */
 
     /* pack things never reported together to reduce structure size */ 
-#define UNION_SET      
(RTCM2_SET|RTCM3_SET|SUBFRAME_SET|AIS_SET|ATTITUDE_SET|VERSION_SET|DEVICELIST_SET|LOGMESSAGE_SET|ERROR_SET|GST_SET|VERSION_SET)
+#define UNION_SET      
(RTCM2_SET|RTCM3_SET|SUBFRAME_SET|AIS_SET|ATTITUDE_SET|VERSION_SET|DEVICELIST_SET|LOGMESSAGE_SET|ERROR_SET|GST_SET|VERSION_SET|WIND_SET)
     union {
        /* unusual forms of sensor data that might come up the pipe */ 
        struct rtcm2_t  rtcm2;
@@ -1701,6 +1719,7 @@ struct gps_data_t {
        struct subframe_t subframe;
        struct ais_t ais;
        struct attitude_t attitude;
+        struct wind_t wind;
        struct rawdata_t raw;
        struct gst_t gst;
        /* "artificial" structures for various protocol responses */
diff --git a/gps_json.h b/gps_json.h
index 1da124a..e3d13c4 100644
--- a/gps_json.h
+++ b/gps_json.h
@@ -23,6 +23,7 @@ void json_tpv_dump(const struct gps_device_t *,
 void json_noise_dump(const struct gps_data_t *, /address@hidden@*/char *, 
size_t);
 void json_sky_dump(const struct gps_data_t *, /address@hidden@*/char *, 
size_t);
 void json_att_dump(const struct gps_data_t *, /address@hidden@*/char *, 
size_t);
+void json_wind_dump(const struct gps_data_t *, /address@hidden@*/char *, 
size_t);
 void json_subframe_dump(const struct gps_data_t *, /address@hidden@*/ char 
buf[], size_t);
 void json_device_dump(const struct gps_device_t *, /address@hidden@*/char *, 
size_t);
 void json_watch_dump(const struct policy_t *, /address@hidden@*/char *, 
size_t);
diff --git a/gpsd.c b/gpsd.c
index 93264f0..8faa025 100644
--- a/gpsd.c
+++ b/gpsd.c
@@ -1613,7 +1613,7 @@ static void consume_packets(struct gps_device_t *device)
 
 #ifdef SHM_EXPORT_ENABLE
        if ((changed & (REPORT_IS|GST_SET|SATELLITE_SET|SUBFRAME_SET|
-                       ATTITUDE_SET|RTCM2_SET|RTCM3_SET|AIS_SET)) != 0)
+                       ATTITUDE_SET|RTCM2_SET|RTCM3_SET|AIS_SET|WIND_SET)) != 
0)
            shm_update(&context, &device->gpsdata);
 #endif /* SHM_EXPORT_ENABLE */
 
diff --git a/gpsd_json.c b/gpsd_json.c
index f1ecdff..a565837 100644
--- a/gpsd_json.c
+++ b/gpsd_json.c
@@ -2953,6 +2953,46 @@ void json_att_dump(const struct gps_data_t *gpsdata,
 }
 #endif /* COMPASS_ENABLE */
 
+/* TODO: #ifdef WIND_ENABLE? */
+void json_wind_dump(const struct gps_data_t *gpsdata,
+                  /address@hidden@*/ char *reply, size_t replylen)
+/* dump the contents of a wind_t structure as JSON */
+{
+    assert(replylen > 2);
+    (void)strlcpy(reply, "{\"class\":\"WIND\",", replylen);
+    (void)snprintf(reply + strlen(reply),
+                  replylen - strlen(reply),
+                  "\"tag\":\"%s\",",
+                  gpsdata->tag[0] != '\0' ? gpsdata->tag : "-");
+    (void)snprintf(reply + strlen(reply),
+                  replylen - strlen(reply),
+                  "\"device\":\"%s\",", gpsdata->dev.path);
+    if (isnan(gpsdata->wind.speed) == 0) {
+       (void)snprintf(reply + strlen(reply),
+                      replylen - strlen(reply),
+                      "\"speed\":%.1f,", gpsdata->wind.speed);
+       (void)snprintf(reply + strlen(reply),
+                      replylen - strlen(reply),
+                      "\"speed_type\":%d,", gpsdata->wind.speed_type);
+    }
+    if (isnan(gpsdata->wind.dir) == 0) {
+       (void)snprintf(reply + strlen(reply),
+                      replylen - strlen(reply),
+                      "\"dir\":%.1f,", gpsdata->wind.dir);
+       if (gpsdata->wind.dir_type == WIND_DIR_NORTH) {
+           (void)snprintf(reply + strlen(reply),
+                          replylen - strlen(reply),
+                          "\"dir_mag\":\"%.1f\",", gpsdata->wind.dir_mag);
+       }
+       (void)snprintf(reply + strlen(reply),
+                      replylen - strlen(reply),
+                      "\"dir_type\":%d,", gpsdata->wind.dir_type);
+    }
+    if (reply[strlen(reply) - 1] == ',')
+       reply[strlen(reply) - 1] = '\0';        /* trim trailing comma */
+    (void)strlcat(reply, "}\r\n", replylen);
+}
+
 void json_data_report(const gps_mask_t changed,
                 const struct gps_device_t *session,
                 const struct policy_t *policy,
@@ -2984,6 +3024,10 @@ void json_data_report(const gps_mask_t changed,
     }
 #endif /* COMPASS_ENABLE */
 
+    if ((changed & WIND_SET) != 0) {
+       json_wind_dump(datap, buf+strlen(buf), buflen-strlen(buf));
+    }
+
 #ifdef RTCM104V2_ENABLE
     if ((changed & RTCM2_SET) != 0) {
        json_rtcm2_dump(&datap->rtcm2, datap->dev.path,
diff --git a/libgps_json.c b/libgps_json.c
index 1ea7698..e447ec6 100644
--- a/libgps_json.c
+++ b/libgps_json.c
@@ -256,6 +256,35 @@ static int json_att_read(const char *buf, struct 
gps_data_t *gpsdata,
     return json_read_object(buf, json_attrs_1, endptr);
 }
 
+static int json_wind_read(const char *buf, struct gps_data_t *gpsdata,
+                        /address@hidden@*/ const char **endptr)
+{
+    /*@ -fullinitblock @*/
+    const struct json_attr_t json_attrs_1[] = {
+       /* *INDENT-OFF* */
+       {"class",    t_check,     .dflt.check = "WIND"},
+       {"device",   t_string,    .addr.string = gpsdata->dev.path,
+                                    .len = sizeof(gpsdata->dev.path)},
+       {"tag",      t_string,    .addr.string = gpsdata->tag,
+                                    .len = sizeof(gpsdata->tag)},
+       {"speed_type", t_integer, .addr.integer = &gpsdata->wind.speed_type,
+                                    .dflt.integer = WIND_SPEED_UNKNOWN},
+       {"speed",    t_real,      .addr.real = &gpsdata->wind.speed,
+                                    .dflt.real = NAN},
+       {"dir_type", t_integer,   .addr.integer = &gpsdata->wind.dir_type,
+                                    .dflt.integer = WIND_DIR_UNKNOWN},
+       {"dir",      t_real,     .addr.real = &gpsdata->wind.dir,
+                                    .dflt.real = NAN},
+       {"dir_mag",  t_real,     .addr.real = &gpsdata->wind.dir_mag,
+                                    .dflt.real = NAN},
+       {NULL},
+       /* *INDENT-ON* */
+    };
+    /*@ +fullinitblock @*/
+
+    return json_read_object(buf, json_attrs_1, endptr);
+}
+
 static int json_devicelist_read(const char *buf, struct gps_data_t *gpsdata,
                                /address@hidden@*/ const char **endptr)
 {
@@ -421,6 +450,13 @@ int libgps_json_unpack(const char *buf,
            gpsdata->set |= ATTITUDE_SET;
        }
        return status;
+    } else if (STARTSWITH(classtag, "\"class\":\"WIND\"")) {
+       status = json_wind_read(buf, gpsdata, end);
+       if (status == 0) {
+           gpsdata->set &= ~UNION_SET;
+           gpsdata->set |= WIND_SET;
+       }
+       return status;
     } else if (STARTSWITH(classtag, "\"class\":\"DEVICES\"")) {
        status = json_devicelist_read(buf, gpsdata, end);
        if (status == 0) {
-- 
1.7.0.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]