diff options
author | Eric S. Raymond <esr@thyrsus.com> | 2010-04-14 04:16:04 -0400 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 2010-04-14 04:16:04 -0400 |
commit | daeca67395cf1276a8c33194654b9c7a2b952540 (patch) | |
tree | 76e6e66913781927e620cfbb81603a5a4e017ea8 /net_dgpsip.c | |
parent | 1876159a5a80ae8c6eca24ba128e134edd0fe125 (diff) | |
download | gpsd-daeca67395cf1276a8c33194654b9c7a2b952540.tar.gz |
Reindent the network support. All regression tests pass.
Diffstat (limited to 'net_dgpsip.c')
-rw-r--r-- | net_dgpsip.c | 68 |
1 files changed, 40 insertions, 28 deletions
diff --git a/net_dgpsip.c b/net_dgpsip.c index 9f839bf3..0217c0bb 100644 --- a/net_dgpsip.c +++ b/net_dgpsip.c @@ -7,20 +7,20 @@ #include "gpsd_config.h" #include <sys/types.h> #ifndef S_SPLINT_S - #ifdef HAVE_SYS_SOCKET_H - #include <sys/socket.h> - #else - #define AF_UNSPEC 0 - #endif /* HAVE_SYS_SOCKET_H */ - #include <unistd.h> +#ifdef HAVE_SYS_SOCKET_H +#include <sys/socket.h> +#else +#define AF_UNSPEC 0 +#endif /* HAVE_SYS_SOCKET_H */ +#include <unistd.h> #endif /* S_SPLINT_S */ #include <sys/time.h> #include <stdio.h> #include <math.h> #ifndef S_SPLINT_S - #ifdef HAVE_NETDB_H - #include <netdb.h> - #endif /* HAVE_NETDB_H */ +#ifdef HAVE_NETDB_H +#include <netdb.h> +#endif /* HAVE_NETDB_H */ #endif /* S_SPLINT_S */ #include <string.h> #include <errno.h> @@ -37,30 +37,37 @@ int dgpsip_open(struct gps_context_t *context, const char *dgpsserver) int opts; if ((colon = strchr(dgpsserver, ':')) != NULL) { - dgpsport = colon+1; + dgpsport = colon + 1; *colon = '\0'; } if (!getservbyname(dgpsport, "tcp")) dgpsport = DEFAULT_RTCM_PORT; - context->dsock = netlib_connectsock(AF_UNSPEC, dgpsserver, dgpsport, "tcp"); + context->dsock = + netlib_connectsock(AF_UNSPEC, dgpsserver, dgpsport, "tcp"); if (context->dsock >= 0) { - gpsd_report(LOG_PROG,"connection to DGPS server %s established.\n",dgpsserver); + gpsd_report(LOG_PROG, "connection to DGPS server %s established.\n", + dgpsserver); (void)gethostname(hn, sizeof(hn)); /* greeting required by some RTCM104 servers; others will ignore it */ - (void)snprintf(buf,sizeof(buf), "HELO %s gpsd %s\r\nR\r\n",hn,VERSION); - if (write(context->dsock, buf, strlen(buf)) == (ssize_t)strlen(buf)) + (void)snprintf(buf, sizeof(buf), "HELO %s gpsd %s\r\nR\r\n", hn, + VERSION); + if (write(context->dsock, buf, strlen(buf)) == (ssize_t) strlen(buf)) context->netgnss_service = netgnss_dgpsip; else - gpsd_report(LOG_ERROR, "hello to DGPS server %s failed\n", dgpsserver); + gpsd_report(LOG_ERROR, "hello to DGPS server %s failed\n", + dgpsserver); } else - gpsd_report(LOG_ERROR, "can't connect to DGPS server %s, netlib error %d.\n", dgpsserver, context->dsock); + gpsd_report(LOG_ERROR, + "can't connect to DGPS server %s, netlib error %d.\n", + dgpsserver, context->dsock); opts = fcntl(context->dsock, F_GETFL); if (opts >= 0) (void)fcntl(context->dsock, F_SETFL, opts | O_NONBLOCK); return context->dsock; } + /*@ +branchstate */ void dgpsip_report(struct gps_device_t *session) @@ -74,11 +81,12 @@ void dgpsip_report(struct gps_device_t *session) session->context->sentdgps = true; if (session->context->dsock > -1) { char buf[BUFSIZ]; - (void)snprintf(buf, sizeof(buf), "R %0.8f %0.8f %0.2f\r\n", - session->gpsdata.fix.latitude, - session->gpsdata.fix.longitude, + (void)snprintf(buf, sizeof(buf), "R %0.8f %0.8f %0.2f\r\n", + session->gpsdata.fix.latitude, + session->gpsdata.fix.longitude, session->gpsdata.fix.altitude); - if (write(session->context->dsock, buf, strlen(buf)) == (ssize_t)strlen(buf)) + if (write(session->context->dsock, buf, strlen(buf)) == + (ssize_t) strlen(buf)) gpsd_report(LOG_IO, "=> dgps %s", buf); else gpsd_report(LOG_IO, "write to dgps FAILED"); @@ -89,7 +97,8 @@ void dgpsip_report(struct gps_device_t *session) #define DGPS_THRESHOLD 1600000 /* max. useful dist. from DGPS server (m) */ #define SERVER_SAMPLE 12 /* # of servers within threshold to check */ -struct dgps_server_t { +struct dgps_server_t +{ double lat, lon; char server[257]; double dist; @@ -97,12 +106,11 @@ struct dgps_server_t { static int srvcmp(const void *s, const void *t) { - return (int)(((const struct dgps_server_t *)s)->dist - ((const struct dgps_server_t *)t)->dist); /* fixes: warning: cast discards qualifiers from pointer target type */ + return (int)(((const struct dgps_server_t *)s)->dist - ((const struct dgps_server_t *)t)->dist); /* fixes: warning: cast discards qualifiers from pointer target type */ } void dgpsip_autoconnect(struct gps_context_t *context, - double lat, double lon, - const char *serverlist) + double lat, double lon, const char *serverlist) /* tell the library to talk to the nearest DGPSIP server */ { struct dgps_server_t keep[SERVER_SAMPLE], hold, *sp, *tp; @@ -124,7 +132,8 @@ void dgpsip_autoconnect(struct gps_context_t *context, char *cp = strchr(buf, '#'); if (cp != NULL) *cp = '\0'; - if (sscanf(buf,"%lf %lf %256s",&hold.lat, &hold.lon, hold.server)==3) { + if (sscanf(buf, "%lf %lf %256s", &hold.lat, &hold.lon, hold.server) == + 3) { hold.dist = earth_distance(lat, lon, hold.lat, hold.lon); tp = NULL; /* @@ -134,7 +143,8 @@ void dgpsip_autoconnect(struct gps_context_t *context, * In this way we end up with the closest possible set. */ for (sp = keep; sp < keep + SERVER_SAMPLE; sp++) - if (hold.dist < sp->dist && (tp==NULL || hold.dist > tp->dist)) + if (hold.dist < sp->dist + && (tp == NULL || hold.dist > tp->dist)) tp = sp; if (tp != NULL) memcpy(tp, &hold, sizeof(struct dgps_server_t)); @@ -143,7 +153,8 @@ void dgpsip_autoconnect(struct gps_context_t *context, (void)fclose(sfp); if (keep[0].server[0] == '\0') { - gpsd_report(LOG_ERROR, "no DGPS servers within %dm.\n", (int)(DGPS_THRESHOLD/1000)); + gpsd_report(LOG_ERROR, "no DGPS servers within %dm.\n", + (int)(DGPS_THRESHOLD / 1000)); context->dsock = -2; /* don't try this again */ return; } @@ -153,7 +164,8 @@ void dgpsip_autoconnect(struct gps_context_t *context, qsort((void *)keep, SERVER_SAMPLE, sizeof(struct dgps_server_t), srvcmp); for (sp = keep; sp < keep + SERVER_SAMPLE; sp++) { if (sp->server[0] != '\0') { - gpsd_report(LOG_INF,"%s is %dkm away.\n",sp->server,(int)(sp->dist/1000)); + gpsd_report(LOG_INF, "%s is %dkm away.\n", sp->server, + (int)(sp->dist / 1000)); if (dgpsip_open(context, sp->server) >= 0) break; } |