[PATCH] Added run-time selection of the gps device.
Dario Lombardo
dario.lombardo at libero.it
Mon Feb 21 18:30:24 CET 2011
---
src/host/layer23/configure.ac | 2 +-
src/host/layer23/include/osmocom/bb/common/gps.h | 13 ++++-
src/host/layer23/src/common/gps.c | 64 ++++++++++++++++------
src/host/layer23/src/misc/app_cell_log.c | 48 ++++++++++-------
src/host/layer23/src/mobile/vty_interface.c | 20 +++----
5 files changed, 95 insertions(+), 52 deletions(-)
diff --git a/src/host/layer23/configure.ac b/src/host/layer23/configure.ac
index e1c718e..92ec30f 100644
--- a/src/host/layer23/configure.ac
+++ b/src/host/layer23/configure.ac
@@ -15,7 +15,7 @@ AC_PROG_RANLIB
dnl checks for libraries
PKG_CHECK_MODULES(LIBOSMOCORE, libosmocore)
PKG_CHECK_MODULES(LIBOSMOVTY, libosmovty)
-AC_CHECK_LIB(gps, gps_open, CFLAGS+=" -D_USE_GPSD" LDFLAGS+=" -lgps",,)
+AC_CHECK_LIB(gps, gps_open, CFLAGS+=" -D_HAVE_GPSD" LDFLAGS+=" -lgps",,)
dnl checks for header files
AC_HEADER_STDC
diff --git a/src/host/layer23/include/osmocom/bb/common/gps.h b/src/host/layer23/include/osmocom/bb/common/gps.h
index b7500db..8096dd8 100644
--- a/src/host/layer23/include/osmocom/bb/common/gps.h
+++ b/src/host/layer23/include/osmocom/bb/common/gps.h
@@ -19,17 +19,24 @@
*
*/
+enum {
+ GPS_TYPE_UNDEF,
+ GPS_TYPE_GPSD,
+ GPS_TYPE_SERIAL
+};
+
struct osmo_gps {
/* GPS device */
uint8_t enable;
+ uint8_t gps_type;
-#ifdef _USE_GPSD
+#ifdef _HAVE_GPSD
char gpsd_host[32];
char gpsd_port[6];
-#else
+#endif
+
char device[32];
uint32_t baud;
-#endif
/* current data */
uint8_t valid; /* we have a fix */
diff --git a/src/host/layer23/src/common/gps.c b/src/host/layer23/src/common/gps.c
index 55dd239..24d6c51 100644
--- a/src/host/layer23/src/common/gps.c
+++ b/src/host/layer23/src/common/gps.c
@@ -26,8 +26,9 @@
#include <stdlib.h>
#include <errno.h>
#include <time.h>
+#include <stdbool.h>
-#ifdef _USE_GPSD
+#ifdef _HAVE_GPSD
#include <gps.h>
#endif
@@ -39,13 +40,13 @@
struct osmo_gps gps = {
0,
-#ifdef _USE_GPSD
+ GPS_TYPE_UNDEF,
+#ifdef _HAVE_GPSD
"localhost",
"2947",
-#else
+#endif
"/dev/ttyACM0",
0,
-#endif
0,
0,
0,0
@@ -53,11 +54,11 @@ struct osmo_gps gps = {
static struct bsc_fd gps_bfd;
-#ifdef _USE_GPSD
+#ifdef _HAVE_GPSD
static struct gps_data_t* gdata;
-int osmo_gps_cb(struct bsc_fd *bfd, unsigned int what)
+int osmo_gpsd_cb(struct bsc_fd *bfd, unsigned int what)
{
struct tm *tm;
unsigned diff = 0;
@@ -102,13 +103,13 @@ gps_not_ready:
return -1;
}
-int osmo_gps_open(void)
+int osmo_gpsd_open(void)
{
LOGP(DGPS, LOGL_INFO, "Connecting to gpsd at '%s:%s'\n", gps.gpsd_host, gps.gpsd_port);
gps_bfd.data = NULL;
gps_bfd.when = BSC_FD_READ;
- gps_bfd.cb = osmo_gps_cb;
+ gps_bfd.cb = osmo_gpsd_cb;
gdata = gps_open(gps.gpsd_host, gps.gpsd_port);
if (gdata == NULL) {
@@ -129,7 +130,7 @@ int osmo_gps_open(void)
return 0;
}
-void osmo_gps_close(void)
+void osmo_gpsd_close(void)
{
if (gps_bfd.fd <= 0)
return;
@@ -142,11 +143,11 @@ void osmo_gps_close(void)
gps_bfd.fd = -1; /* -1 or 0 indicates: 'close' */
}
-#else
+#endif
static struct termios gps_termios, gps_old_termios;
-static int osmo_gps_line(char *line)
+static int osmo_serialgps_line(char *line)
{
time_t gps_now, host_now;
struct tm *tm;
@@ -241,7 +242,7 @@ static int nmea_checksum(char *line)
return (strtoul(line+1, NULL, 16) == checksum);
}
-int osmo_gps_cb(struct bsc_fd *bfd, unsigned int what)
+int osmo_serialgps_cb(struct bsc_fd *bfd, unsigned int what)
{
char buff[128];
static char line[128];
@@ -265,7 +266,7 @@ int osmo_gps_cb(struct bsc_fd *bfd, unsigned int what)
if (!nmea_checksum(line))
fprintf(stderr, "NMEA checksum error\n");
else
- osmo_gps_line(line);
+ osmo_serialgps_line(line);
continue;
}
line[lpos++] = buff[i++];
@@ -276,7 +277,7 @@ int osmo_gps_cb(struct bsc_fd *bfd, unsigned int what)
return 0;
}
-int osmo_gps_open(void)
+int osmo_serialgps_open(void)
{
int baud = 0;
@@ -287,7 +288,7 @@ int osmo_gps_open(void)
gps_bfd.data = NULL;
gps_bfd.when = BSC_FD_READ;
- gps_bfd.cb = osmo_gps_cb;
+ gps_bfd.cb = osmo_serialgps_cb;
gps_bfd.fd = open(gps.device, O_RDONLY);
if (gps_bfd.fd < 0)
return gps_bfd.fd;
@@ -327,7 +328,7 @@ int osmo_gps_open(void)
return 0;
}
-void osmo_gps_close(void)
+void osmo_serialgps_close(void)
{
if (gps_bfd.fd <= 0)
return;
@@ -343,11 +344,38 @@ void osmo_gps_close(void)
gps_bfd.fd = -1; /* -1 or 0 indicates: 'close' */
}
-#endif
-
void osmo_gps_init(void)
{
memset(&gps_bfd, 0, sizeof(gps_bfd));
}
+int osmo_gps_open(void)
+{
+ switch (gps.gps_type) {
+#ifdef _HAVE_GPSD
+ case GPS_TYPE_GPSD:
+ return osmo_gpsd_open();
+#endif
+ case GPS_TYPE_SERIAL:
+ return osmo_serialgps_open();
+
+ default:
+ return 0;
+ }
+}
+
+void osmo_gps_close(void)
+{
+ switch (gps.gps_type) {
+#ifdef _HAVE_GPSD
+ case GPS_TYPE_GPSD:
+ return osmo_gpsd_close();
+#endif
+ case GPS_TYPE_SERIAL:
+ return osmo_serialgps_close();
+
+ default:
+ return;
+ }
+}
diff --git a/src/host/layer23/src/misc/app_cell_log.c b/src/host/layer23/src/misc/app_cell_log.c
index 7a2c67a..40aac5a 100644
--- a/src/host/layer23/src/misc/app_cell_log.c
+++ b/src/host/layer23/src/misc/app_cell_log.c
@@ -92,13 +92,12 @@ static int l23_getopt_options(struct option **options)
{"logfile", 1, 0, 'l'},
{"rach", 1, 0, 'r'},
{"no-rach", 1, 0, 'n'},
-#ifdef _USE_GPSD
+#ifdef _HAVE_GPSD
{"gpsd-host", 1, 0, 'g'},
- {"gpsd-port", 1, 0, 'p'}
-#else
+ {"gpsd-port", 1, 0, 'p'},
+#endif
{"gps", 1, 0, 'g'},
{"baud", 1, 0, 'b'}
-#endif
};
*options = opts;
@@ -109,15 +108,15 @@ static int l23_cfg_print_help()
{
printf("\nApplication specific\n");
printf(" -l --logfile LOGFILE Logfile for the cell log.\n");
- printf(" -r --rach RACH Nr. of RACH bursts to send.\n");
- printf(" -n --no-rach Send no rach bursts.\n");
-#ifdef _USE_GPSD
+ printf(" -r --rach RACH Nr. of RACH bursts to send.\n");
+ printf(" -n --no-rach Send no rach bursts.\n");
+#ifdef _HAVE_GPSD
printf(" -g --gpsd-host HOST 127.0.0.1. gpsd host.\n");
- printf(" -p --port PORT 2947. gpsd port\n");
-#else
- printf(" -g --gps DEVICE /dev/ttyACM0. GPS device.\n");
- printf(" -b --baud BAUDRAT The baud rate of the GPS device\n");
+ printf(" -p --port PORT 2947. gpsd port\n");
#endif
+ printf(" -f --gps DEVICE /dev/ttyACM0. GPS serial device.\n");
+ printf(" -b --baud BAUDRAT The baud rate of the GPS device\n");
+
return 0;
}
@@ -133,41 +132,52 @@ static int l23_cfg_handle(int c, const char *optarg)
case 'n':
RACH_MAX = 0;
break;
-#ifdef _USE_GPSD
+#ifdef _HAVE_GPSD
case 'g':
snprintf(gps.gpsd_host, ARRAY_SIZE(gps.gpsd_host), "%s", optarg);
/* force string terminator */
gps.gpsd_host[ARRAY_SIZE(gps.gpsd_host) - 1] = '\0';
+ if (gps.gps_type != GPS_TYPE_UNDEF)
+ goto cmd_line_error;
+ gps.gps_type = GPS_TYPE_GPSD;
LOGP(DGPS, LOGL_INFO, "Using gpsd host %s\n", gps.gpsd_host);
break;
case 'p':
snprintf(gps.gpsd_port, ARRAY_SIZE(gps.gpsd_port), "%s", optarg);
/* force string terminator */
gps.gpsd_port[ARRAY_SIZE(gps.gpsd_port) - 1] = '\0';
+ gps.gps_type = GPS_TYPE_GPSD;
LOGP(DGPS, LOGL_INFO, "Using gpsd port %s\n", gps.gpsd_port);
break;
-#else
- case 'g':
+#endif
+ case 'f':
snprintf(gps.device, ARRAY_SIZE(gps.device), "%s", optarg);
/* force string terminator */
gps.device[ARRAY_SIZE(gps.device) - 1] = '\0';
- LOGP(DGPS, LOGL_INFO, "Using GPS device %s\n", gps.device);
+ if (gps.gps_type != GPS_TYPE_UNDEF)
+ goto cmd_line_error;
+ gps.gps_type = GPS_TYPE_SERIAL;
+ LOGP(DGPS, LOGL_INFO, "Using GPS serial device %s\n", gps.device);
break;
case 'b':
gps.baud = atoi(optarg);
+ gps.gps_type = GPS_TYPE_SERIAL;
LOGP(DGPS, LOGL_INFO, "Setting GPS baudrate to %u\n", gps.baud);
break;
-#endif
}
return 0;
+
+cmd_line_error:
+ printf("\nYou can't specify both gpsd and serial gps!!\n\n");
+ exit(1);
}
static struct l23_app_info info = {
.copyright = "Copyright (C) 2010 Andreas Eversberg\n",
-#ifdef _USE_GPSD
- .getopt_string = "l:r:ng:p:",
+#ifdef _HAVE_GPSD
+ .getopt_string = "l:r:nf:b:g:p:",
#else
- .getopt_string = "l:r:ng:b:",
+ .getopt_string = "l:r:nf:b:",
#endif
.cfg_supported = l23_cfg_supported,
.cfg_getopt_opt = l23_getopt_options,
diff --git a/src/host/layer23/src/mobile/vty_interface.c b/src/host/layer23/src/mobile/vty_interface.c
index 7888f01..fadc152 100644
--- a/src/host/layer23/src/mobile/vty_interface.c
+++ b/src/host/layer23/src/mobile/vty_interface.c
@@ -855,7 +855,7 @@ DEFUN(cfg_no_gps_enable, cfg_no_gps_enable_cmd, "no gps enable",
return CMD_SUCCESS;
}
-#ifdef _USE_GPSD
+#ifdef _HAVE_GPSD
DEFUN(cfg_gps_host, cfg_gps_host_cmd, "gps host HOST:PORT",
"GPS receiver\nSelect gpsd host and port\n"
"IP and port (optional) of the host running gpsd")
@@ -872,6 +872,7 @@ DEFUN(cfg_gps_host, cfg_gps_host_cmd, "gps host HOST:PORT",
snprintf(gps.gpsd_port, ARRAY_SIZE(gps.gpsd_port), "2947");
gps.gpsd_port[ARRAY_SIZE(gps.gpsd_port) - 1] = '\0';
}
+ gps.gps_type = GPS_TYPE_GPSD;
if (gps.enable) {
osmo_gps_close();
if (osmo_gps_open()) {
@@ -883,13 +884,15 @@ DEFUN(cfg_gps_host, cfg_gps_host_cmd, "gps host HOST:PORT",
return CMD_SUCCESS;
}
-#else
+#endif
+
DEFUN(cfg_gps_device, cfg_gps_device_cmd, "gps device DEVICE",
"GPS receiver\nSelect serial device\n"
"Full path of serial device including /dev/")
{
strncpy(gps.device, argv[0], sizeof(gps.device));
gps.device[sizeof(gps.device) - 1] = '\0';
+ gps.gps_type = GPS_TYPE_SERIAL;
if (gps.enable) {
osmo_gps_close();
if (osmo_gps_open()) {
@@ -901,9 +904,7 @@ DEFUN(cfg_gps_device, cfg_gps_device_cmd, "gps device DEVICE",
return CMD_SUCCESS;
}
-#endif
-#ifndef _USE_GPSD
DEFUN(cfg_gps_baud, cfg_gps_baud_cmd, "gps baudrate "
"(default|4800|""9600|19200|38400|57600|115200)",
"GPS receiver\nSelect baud rate\nDefault, don't modify\n\n\n\n\n\n")
@@ -924,7 +925,6 @@ DEFUN(cfg_gps_baud, cfg_gps_baud_cmd, "gps baudrate "
return CMD_SUCCESS;
}
-#endif
/* per MS config */
DEFUN(cfg_ms, cfg_ms_cmd, "ms MS_NAME",
@@ -1204,16 +1204,15 @@ static int config_write(struct vty *vty)
{
struct osmocom_ms *ms;
-#ifdef _USE_GPSD
+#ifdef _HAVE_GPSD
vty_out(vty, "gpsd host %s%s", gps.gpsd_host, VTY_NEWLINE);
vty_out(vty, "gpsd port %s%s", gps.gpsd_port, VTY_NEWLINE);
-#else
+#endif
vty_out(vty, "gps device %s%s", gps.device, VTY_NEWLINE);
if (gps.baud)
vty_out(vty, "gps baudrate %d%s", gps.baud, VTY_NEWLINE);
else
vty_out(vty, "gps baudrate default%s", VTY_NEWLINE);
-#endif
vty_out(vty, "%sgps enable%s", (gps.enable) ? "" : "no ", VTY_NEWLINE);
vty_out(vty, "!%s", VTY_NEWLINE);
@@ -2305,12 +2304,11 @@ int ms_vty_init(void)
install_element(ENABLE_NODE, &call_retr_cmd);
install_element(ENABLE_NODE, &call_dtmf_cmd);
-#ifdef _USE_GPSD
+#ifdef _HAVE_GPSD
install_element(CONFIG_NODE, &cfg_gps_host_cmd);
-#else
+#endif
install_element(CONFIG_NODE, &cfg_gps_device_cmd);
install_element(CONFIG_NODE, &cfg_gps_baud_cmd);
-#endif
install_element(CONFIG_NODE, &cfg_gps_enable_cmd);
install_element(CONFIG_NODE, &cfg_no_gps_enable_cmd);
--
1.7.4
--------------070003050405000809030404--
More information about the baseband-devel
mailing list