[PATCH] Add support for the new gpsd API

Paul Wise pabs3 at bonedaddy.net
Tue Jan 3 08:37:35 UTC 2012


---
 src/host/layer23/src/common/gps.c |   57 +++++++++++++++++++++++++++++++++++++
 1 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/src/host/layer23/src/common/gps.c b/src/host/layer23/src/common/gps.c
index 38aae2c..2ab4023 100644
--- a/src/host/layer23/src/common/gps.c
+++ b/src/host/layer23/src/common/gps.c
@@ -56,7 +56,11 @@ static struct osmo_fd gps_bfd;
 
 #ifdef _HAVE_GPSD
 
+#if GPSD_API_MAJOR_VERSION >= 5
+static struct gps_data_t gdata;
+#else /* GPSD_API_MAJOR_VERSION < 5 */
 static struct gps_data_t* gdata;
+#endif /* GPSD_API_MAJOR_VERSION < 5 */
 
 int osmo_gpsd_cb(struct osmo_fd *bfd, unsigned int what)
 {
@@ -65,6 +69,38 @@ int osmo_gpsd_cb(struct osmo_fd *bfd, unsigned int what)
 
 	g.valid = 0;
 
+#if GPSD_API_MAJOR_VERSION >= 5
+	/* gps is offline */
+	if (gdata.online)
+	    goto gps_not_ready;
+
+	/* gps has no data */
+	if (gps_waiting(&gdata, 500))
+	    goto gps_not_ready;
+
+	/* polling returned an error */
+	if (gps_read(&gdata))
+	    goto gps_not_ready;
+
+	/* data are valid */
+	if (gdata.set & LATLON_SET) {
+		g.valid = 1;
+		g.gmt = gdata.fix.time;
+		tm = localtime(&g.gmt);
+		diff = time(NULL) - g.gmt;
+		g.latitude = gdata.fix.latitude;
+		g.longitude = gdata.fix.longitude;
+
+		LOGP(DGPS, LOGL_INFO, " time=%02d:%02d:%02d %04d-%02d-%02d, "
+			"diff-to-host=%d, latitude=%do%.4f, longitude=%do%.4f\n",
+			tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_year + 1900,
+			tm->tm_mday, tm->tm_mon + 1, diff,
+			(int)g.latitude,
+			(g.latitude - ((int)g.latitude)) * 60.0,
+			(int)g.longitude,
+			(g.longitude - ((int)g.longitude)) * 60.0);
+	}
+#else /* GPSD_API_MAJOR_VERSION < 5 */
 	/* gps is offline */
 	if (gdata->online)
 	    goto gps_not_ready;
@@ -95,6 +131,7 @@ int osmo_gpsd_cb(struct osmo_fd *bfd, unsigned int what)
 			(int)g.longitude,
 			(g.longitude - ((int)g.longitude)) * 60.0);
 	}
+#endif /* GPSD_API_MAJOR_VERSION < 5 */
 
 	return 0;
 
@@ -111,6 +148,20 @@ int osmo_gpsd_open(void)
 	gps_bfd.when = BSC_FD_READ;
 	gps_bfd.cb = osmo_gpsd_cb;
 
+#if GPSD_API_MAJOR_VERSION >= 5
+	if (gps_open(g.gpsd_host, g.gpsd_port, &gdata) == -1) {
+		LOGP(DGPS, LOGL_ERROR, "Can't connect to gpsd\n");
+		return -1;
+	}
+	gps_bfd.fd = gdata.gps_fd;
+	if (gps_bfd.fd < 0)
+		return gps_bfd.fd;
+
+	if (gps_stream(&gdata, WATCH_ENABLE, NULL) == -1) {
+		LOGP(DGPS, LOGL_ERROR, "Error in gps_stream()\n");
+		return -1;
+	}
+#else /* GPSD_API_MAJOR_VERSION < 5 */
 	gdata = gps_open(g.gpsd_host, g.gpsd_port);
 	if (gdata == NULL) {
 		LOGP(DGPS, LOGL_ERROR, "Can't connect to gpsd\n");
@@ -124,6 +175,7 @@ int osmo_gpsd_open(void)
 		LOGP(DGPS, LOGL_ERROR, "Error in gps_stream()\n");
 		return -1;
 	}
+#endif /* GPSD_API_MAJOR_VERSION < 5 */
 
 	osmo_fd_register(&gps_bfd);
 
@@ -139,7 +191,12 @@ void osmo_gpsd_close(void)
 
 	osmo_fd_unregister(&gps_bfd);
 
+#if GPSD_API_MAJOR_VERSION >= 5
+	gps_stream(&gdata, WATCH_DISABLE, NULL);
+	gps_close(&gdata);
+#else /* GPSD_API_MAJOR_VERSION < 5 */
 	gps_close(gdata);
+#endif /* GPSD_API_MAJOR_VERSION < 5 */
 	gps_bfd.fd = -1; /* -1 or 0 indicates: 'close' */
 }
 
-- 
1.7.7.3





More information about the baseband-devel mailing list