summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2011-09-28 03:36:14 -0400
committerEric S. Raymond <esr@thyrsus.com>2011-09-28 03:36:14 -0400
commit45b6da360af526817cba737bc3b13ad6e24ee26b (patch)
tree5f3444b98ba8cf73008fdd3ca3a37c138cffd463
parent702896ec598a531fa9263774912d7fe922037180 (diff)
downloadgpsd-45b6da360af526817cba737bc3b13ad6e24ee26b.tar.gz
First step in runtime export dispatch for the client library.
-rw-r--r--gpxlogger.c2
-rw-r--r--libgps.h7
-rw-r--r--libgps_core.c11
-rw-r--r--libgps_dbus.c2
-rw-r--r--libgps_shm.c22
-rw-r--r--libgps_sock.c2
6 files changed, 41 insertions, 5 deletions
diff --git a/gpxlogger.c b/gpxlogger.c
index 63e5be1a..95d716be 100644
--- a/gpxlogger.c
+++ b/gpxlogger.c
@@ -17,8 +17,8 @@
#endif /* S_SPLINT_S */
#include "gps.h"
-#include "libgps.h" /* TEMPORARY */
#include "gpsd_config.h"
+#include "libgps.h" /* TEMPORARY */
#include "gpsdclient.h"
#include "revision.h"
diff --git a/libgps.h b/libgps.h
index f571efa8..17dc73ea 100644
--- a/libgps.h
+++ b/libgps.h
@@ -6,6 +6,13 @@
#ifndef _GPSD_LIBGPS_H_
#define _GPSD_LIBGPS_H_
+/*
+ * first member of each kind of privdata structure must be named
+ * 'export' and must be of this time. It's how we do runtime
+ * dispatch to the different transports.
+ */
+enum export_t {sockets, shm, dbus};
+
extern int gps_sock_open(/*@null@*/const char *, /*@null@*/const char *,
/*@out@*/struct gps_data_t *);
extern int gps_sock_close(struct gps_data_t *);
diff --git a/libgps_core.c b/libgps_core.c
index 2196b0ab..b2e298d9 100644
--- a/libgps_core.c
+++ b/libgps_core.c
@@ -18,6 +18,17 @@
#include "libgps.h"
#include "gps_json.h"
+/*
+ * All privdata structures have export as a first member,
+ * and can have others that the individual method libraries
+ * know about but this one doesn't.
+ */
+struct privdata_t
+{
+ enum export_t export;
+};
+#define PRIVATE(gpsdata) ((struct privdata_t *)gpsdata->privdata)
+
#ifdef LIBGPS_DEBUG
int libgps_debuglevel = 0;
diff --git a/libgps_dbus.c b/libgps_dbus.c
index c948e931..21ea129f 100644
--- a/libgps_dbus.c
+++ b/libgps_dbus.c
@@ -20,6 +20,7 @@
struct privdata_t
{
+ enum export_t export;
void (*handler)(struct gps_data_t *);
};
#define PRIVATE(gpsdata) ((struct privdata_t *)(gpsdata)->privdata)
@@ -124,6 +125,7 @@ int gps_dbus_open(struct gps_data_t *gpsdata)
return 5;
}
+ PRIVATE(gpsdata)->export = dbus;
share_gpsdata = gpsdata;
return 0;
}
diff --git a/libgps_shm.c b/libgps_shm.c
index 4a26c00d..a541a53a 100644
--- a/libgps_shm.c
+++ b/libgps_shm.c
@@ -17,6 +17,7 @@ PERMISSIONS
#include <stddef.h>
#include <string.h>
#include <errno.h>
+#include <stdlib.h>
#include <sys/time.h>
#include <sys/ipc.h>
#include <sys/shm.h>
@@ -26,6 +27,14 @@ PERMISSIONS
#ifdef SHM_EXPORT_ENABLE
+struct privdata_t
+{
+ enum export_t export;
+ void *shmseg;
+};
+#define PRIVATE(gpsdata) ((struct privdata_t *)(gpsdata)->privdata)
+
+
int gps_shm_open(/*@out@*/struct gps_data_t *gpsdata)
/* open a shared-memory connection to the daemon */
{
@@ -39,7 +48,11 @@ int gps_shm_open(/*@out@*/struct gps_data_t *gpsdata)
/* daemon isn't running or failed to create shared segment */
return -1;
}
- gpsdata->privdata = shmat(shmid, 0, 0);
+ gpsdata->privdata = (void *)malloc(sizeof(struct privdata_t));
+ if (gpsdata->privdata == NULL)
+ return -1;
+
+ PRIVATE(gpsdata)->shmseg = shmat(shmid, 0, 0);
if ((int)(long)gpsdata->privdata == -1) {
/* attach failed for sume unknown reason */
return -2;
@@ -49,6 +62,7 @@ int gps_shm_open(/*@out@*/struct gps_data_t *gpsdata)
#else
gpsdata->gps_fd = (void *)(intptr_t)-1;
#endif /* USE_QT */
+ PRIVATE(gpsdata)->export = shm;
return 0;
}
@@ -62,7 +76,7 @@ int gps_shm_read(struct gps_data_t *gpsdata)
{
int before, after;
void *private_save = gpsdata->privdata;
- volatile struct shmexport_t *shared = (struct shmexport_t *)gpsdata->privdata;
+ volatile struct shmexport_t *shared = (struct shmexport_t *)PRIVATE(gpsdata)->shmseg;
struct gps_data_t noclobber;
/*
@@ -106,8 +120,8 @@ int gps_shm_read(struct gps_data_t *gpsdata)
void gps_shm_close(struct gps_data_t *gpsdata)
{
- if (gpsdata->privdata != NULL)
- (void)shmdt((const void *)gpsdata->privdata);
+ if (PRIVATE(gpsdata)->shmseg != NULL)
+ (void)shmdt((const void *)PRIVATE(gpsdata)->shmseg);
}
int gps_shm_mainloop(struct gps_data_t *gpsdata, int timeout UNUSED,
diff --git a/libgps_sock.c b/libgps_sock.c
index eee2b926..0c2347e4 100644
--- a/libgps_sock.c
+++ b/libgps_sock.c
@@ -41,6 +41,7 @@ extern char *strtok_r(char *, const char *, char **);
struct privdata_t
{
+ enum export_t export;
bool newstyle;
/* data buffered from the last read */
ssize_t waiting;
@@ -85,6 +86,7 @@ int gps_sock_open(/*@null@*/const char *host, /*@null@*/const char *port,
gpsdata->privdata = (void *)malloc(sizeof(struct privdata_t));
if (gpsdata->privdata == NULL)
return -1;
+ PRIVATE(gpsdata)->export = sockets;
PRIVATE(gpsdata)->newstyle = false;
PRIVATE(gpsdata)->waiting = 0;