summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Tatarinov <kukabu@gmail.com>2013-11-06 11:43:36 +0400
committerEric S. Raymond <esr@thyrsus.com>2013-11-06 08:08:07 -0500
commita4ecaa3e867880890e25a63f7a2472a4b9c85beb (patch)
tree633ab03e8acea5e5514282994491e2fd1a559fe8
parentd93ba6e008cb6a7f75adf3d4c08876cf96fe95b0 (diff)
downloadgpsd-a4ecaa3e867880890e25a63f7a2472a4b9c85beb.tar.gz
ntpshm_pps() is gone.
Signed-off-by: Eric S. Raymond <esr@thyrsus.com>
-rw-r--r--gpsd.c24
-rw-r--r--gpsd.h-tail2
-rw-r--r--ntpshm.c130
3 files changed, 43 insertions, 113 deletions
diff --git a/gpsd.c b/gpsd.c
index bbee5989..18fa87df 100644
--- a/gpsd.c
+++ b/gpsd.c
@@ -1530,15 +1530,29 @@ static void all_reports(struct gps_device_t *device, gps_mask_t changed)
} else if (!device->ship_to_ntpd) {
//gpsd_report(context.debug, LOG_PROG, "NTP: No precision time report\n");
} else {
- double offset;
- //gpsd_report(context.debug, LOG_PROG, "NTP: Got one\n");
+ double fix_time, integral, fractional;
+ struct timedrift_t td;
+
+#ifdef HAVE_CLOCK_GETTIME
+ (void)clock_gettime(CLOCK_REALTIME, &td.clock);
+#else
+ struct timeval clock_tv;
+ (void)gettimeofday(&clock_tv, NULL);
+ TVTOTS(&td.clock, &clock_tv);
+#endif /* HAVE_CLOCK_GETTIME */
+ fix_time = device->newdata.time;
/* assume zero when there's no offset method */
if (device->device_type == NULL
|| device->device_type->time_offset == NULL)
- offset = 0.0;
+ fix_time += 0.0;
else
- offset = device->device_type->time_offset(device);
- (void)ntpshm_put(device, device->newdata.time, offset);
+ fix_time += device->device_type->time_offset(device);
+ /* it's ugly but timestamp_t is double */
+ fractional = modf(fix_time, &integral);
+ td.real.tv_sec = (time_t)integral;
+ td.real.tv_nsec = (long)(fractional * 1e+9);
+ /* Any NMEA will be about -1 or -2. Garmin GPS-18/USB is around -6 or -7. */
+ (void)ntpshm_put(device, device->shmIndex, &td, -1);
device->last_fixtime = device->newdata.time;
}
#endif /* NTPSHM_ENABLE */
diff --git a/gpsd.h-tail b/gpsd.h-tail
index e0bfa472..d22f2e63 100644
--- a/gpsd.h-tail
+++ b/gpsd.h-tail
@@ -824,7 +824,7 @@ extern unsigned int ais_binary_encode(struct ais_t *ais, /*@out@*/unsigned char
extern void ntpshm_context_init(struct gps_context_t *);
extern void ntpshm_session_init(struct gps_device_t *);
-extern int ntpshm_put(struct gps_device_t *, double, double);
+extern int ntpshm_put(struct gps_device_t *, int, struct timedrift_t *, int);
extern void ntpshm_link_deactivate(struct gps_device_t *);
extern void ntpshm_link_activate(struct gps_device_t *);
diff --git a/ntpshm.c b/ntpshm.c
index 8cebdb05..8c6250b3 100644
--- a/ntpshm.c
+++ b/ntpshm.c
@@ -221,30 +221,16 @@ void ntpshm_session_init(struct gps_device_t *session)
#endif /* PPS_ENABLE */
}
-int ntpshm_put(struct gps_device_t *session, double fixtime, double fudge)
+int ntpshm_put(struct gps_device_t *session, int shmIndex, struct timedrift_t *td, int precision)
/* put a received fix time into shared memory for NTP */
{
/* shmTime is volatile to try to prevent C compiler from reordering
* writes, or optimizing some 'dead code'. but CPU cache may still
- *write out of order since we do not use memory barriers, yet */
+ * write out of order since we do not use memory barriers, yet */
volatile struct shmTime *shmTime = NULL;
- struct timeval tv;
- double seconds, microseconds;
-
- // gpsd_report(session->context->debug, LOG_PROG, "NTP: doing ntpshm_put(,%g, %g)\n", fixtime, fudge);
- if (session->shmIndex < 0 ||
- (shmTime = session->context->shmTime[session->shmIndex]) == NULL) {
- gpsd_report(session->context->debug, LOG_RAW,
- "NTPD missing shm\n");
- return 0;
- }
- (void)gettimeofday(&tv, NULL);
- fixtime += fudge;
- microseconds = 1000000.0 * modf(fixtime, &seconds);
- if (shmTime->clockTimeStampSec == (time_t) seconds) {
- gpsd_report(session->context->debug, LOG_RAW,
- "NTPD ntpshm_put: skipping duplicate second\n");
+ if (shmIndex < 0 || (shmTime = session->context->shmTime[shmIndex]) == NULL) {
+ gpsd_report(session->context->debug, LOG_RAW, "NTPD missing shm\n");
return 0;
}
@@ -262,110 +248,39 @@ int ntpshm_put(struct gps_device_t *session, double fixtime, double fudge)
* clear valid
*
*/
+
shmTime->valid = 0;
shmTime->count++;
-
- /*
- * We need a memory barrier here to prevent write reordering by
- * the compiler or CPU cache
- */
+ /* We need a memory barrier here to prevent write reordering by
+ * the compiler or CPU cache */
barrier();
- shmTime->clockTimeStampSec = (time_t) seconds;
- shmTime->clockTimeStampUSec = (int)microseconds;
- shmTime->clockTimeStampNSec = (unsigned)(microseconds*1000);
- shmTime->receiveTimeStampSec = (time_t) tv.tv_sec;
- shmTime->receiveTimeStampUSec = (int)tv.tv_usec;
- shmTime->receiveTimeStampNSec = (unsigned int)(tv.tv_usec*1000);
+ /*@-type@*/ /* splint is confused about struct timespec */
+ shmTime->clockTimeStampSec = (time_t)td->real.tv_sec;
+ shmTime->clockTimeStampUSec = (int)(td->real.tv_nsec/1000);
+ shmTime->clockTimeStampNSec = (unsigned)td->real.tv_nsec;
+ shmTime->receiveTimeStampSec = (time_t)td->clock.tv_sec;
+ shmTime->receiveTimeStampUSec = (int)(td->clock.tv_nsec/1000);
+ shmTime->receiveTimeStampNSec = (unsigned)td->clock.tv_nsec;
+ /*@+type@*/
shmTime->leap = session->context->leap_notify;
- /* setting the precision here does not seem to help anything, too
- * hard to calculate properly anyway. Let ntpd figure it out.
- * Any NMEA will be about -1 or -2.
- * Garmin GPS-18/USB is around -6 or -7.
- */
+ shmTime->precision = precision;
barrier();
-
shmTime->count++;
shmTime->valid = 1;
+ /*@-type@*/ /* splint is confused about struct timespec */
gpsd_report(session->context->debug, LOG_RAW,
- "NTPD ntpshm_put: Clock: %lu.%06lu @ %lu.%06lu, fudge: %0.3f\n",
- (unsigned long)seconds, (unsigned long)microseconds,
- (unsigned long)tv.tv_sec, (unsigned long)tv.tv_usec, fudge);
-
- return 1;
-}
-
-#ifdef PPS_ENABLE
-/*@unused@*//* splint is confused here */
-/* put NTP shared memory info based on received PPS pulse
- *
- * good news is that kernel PPS gives us nSec resolution
- * bad news is that ntpshm only has uSec resolution
- *
- * actual_ts is the actual time we think the PPS happened
- * clock_ts is the time we saw the pulse
- */
-static int ntpshm_pps(struct gps_device_t *session,
- struct timedrift_t *td, double offset)
-{
- volatile struct shmTime *shmTime = NULL, *shmTimeP = NULL;
- /* precision is an expensive float oeration, shold be optional */
- int precision;
-
- if (0 > session->shmIndex || 0 > session->shmIndexPPS ||
- (shmTime = session->context->shmTime[session->shmIndex]) == NULL ||
- (shmTimeP = session->context->shmTime[session->shmIndexPPS]) == NULL)
- return 0;
-
- /* new ntpd interrace can use uSec andnSec */
- /* do NOT use TSTOTV() since that may round up and we need seconds to
- * be the same for uSec and nSec. */
-
- /* we use the shmTime mode 1 protocol
- *
- * ntpd does this:
- *
- * reads valid.
- * IFF valid is 1
- * reads count
- * reads values
- * reads count
- * IFF count unchanged
- * use values
- * clear valid
- *
- */
- shmTimeP->valid = 0;
- shmTimeP->count++;
- /*@-type@*//* splint is confused about struct timespec */
- shmTimeP->clockTimeStampSec = (time_t)td->real.tv_sec;
- shmTimeP->clockTimeStampUSec = (int)(td->real.tv_nsec/1000);
- shmTimeP->clockTimeStampNSec = (unsigned)td->real.tv_nsec;
- shmTimeP->receiveTimeStampSec = (time_t)td->clock.tv_sec;
- shmTimeP->receiveTimeStampUSec = (int)(td->clock.tv_nsec/1000);
- shmTimeP->receiveTimeStampNSec = (unsigned)td->clock.tv_nsec;
- /*@+type@*/
- shmTimeP->leap = session->context->leap_notify;
- /* precision is a placebo, ntpd does not really use it
- * real world accuracy is around 16uS, thus -16 precision */
- shmTimeP->precision = -16;
- shmTimeP->count++;
- shmTimeP->valid = 1;
-
- /* precision is expensive to compute, make compile optional */
- precision = offset != 0 ? (int)(ceil(log(offset) / M_LN2)) : -20;
- /*@-type@*//* splint is confused about struct timespec */
- gpsd_report(session->context->debug, LOG_RAW,
- "PPS ntpshm_pps %lu.%09lu @ %lu.%09lu, preci %d\n",
+ "PPS ntpshm_put %lu.%09lu @ %lu.%09lu\n",
(unsigned long)td->real.tv_sec,
(unsigned long)td->real.tv_nsec,
(unsigned long)td->clock.tv_sec,
- (unsigned long)td->clock.tv_nsec,
- precision);
+ (unsigned long)td->clock.tv_nsec);
/*@+type@*/
+
return 1;
}
+#ifdef PPS_ENABLE
#define SOCK_MAGIC 0x534f434b
struct sock_sample {
struct timeval tv;
@@ -466,7 +381,8 @@ static /*@observer@*/ char *report_hook(struct gps_device_t *session,
log1 = "accepted chrony sock";
chrony_send(session, td, edge_offset);
}
- (void)ntpshm_pps(session, td, edge_offset);
+ /* ntpd sets -20 for PPS refclocks, thus -20 precision */
+ (void)ntpshm_put(session, session->shmIndexPPS, td, -20);
return log1;
}