diff --git a/cmdmon.c b/cmdmon.c index 6a87376..e363317 100644 --- a/cmdmon.c +++ b/cmdmon.c @@ -926,14 +926,16 @@ handle_settime(CMD_Request *rx_message, CMD_Reply *tx_message) long offset_cs; double dfreq_ppm, new_afreq_ppm; UTI_TimevalNetworkToHost(&rx_message->data.settime.ts, &ts); - if (MNL_AcceptTimestamp(&ts, &offset_cs, &dfreq_ppm, &new_afreq_ppm)) { + if (!MNL_IsEnabled()) { + tx_message->status = htons(STT_NOTENABLED); + } else if (MNL_AcceptTimestamp(&ts, &offset_cs, &dfreq_ppm, &new_afreq_ppm)) { tx_message->status = htons(STT_SUCCESS); tx_message->reply = htons(RPY_MANUAL_TIMESTAMP); tx_message->data.manual_timestamp.centiseconds = htonl((int32_t)offset_cs); tx_message->data.manual_timestamp.dfreq_ppm = UTI_FloatHostToNetwork(dfreq_ppm); tx_message->data.manual_timestamp.new_afreq_ppm = UTI_FloatHostToNetwork(new_afreq_ppm); } else { - tx_message->status = htons(STT_NOTENABLED); + tx_message->status = htons(STT_FAILED); } } diff --git a/manual.c b/manual.c index c286ce8..da2ed92 100644 --- a/manual.c +++ b/manual.c @@ -54,6 +54,8 @@ typedef struct { (measured-predicted)) */ } Sample; +#define MIN_SAMPLE_SEPARATION 1.0 + #define MAX_SAMPLES 16 static Sample samples[16]; @@ -174,14 +176,24 @@ int MNL_AcceptTimestamp(struct timeval *ts, long *offset_cs, double *dfreq_ppm, double *new_afreq_ppm) { struct timeval now; - double offset; + double offset, diff; int i; if (enabled) { - - /* Check whether timestamp is within margin of old one */ LCL_ReadCookedTime(&now, NULL); + /* Make sure the provided timestamp is sane and the sample + is not too close to the last one */ + + if (!UTI_IsTimeOffsetSane(ts, 0.0)) + return 0; + + if (n_samples) { + UTI_DiffTimevalsToDouble(&diff, &now, &samples[n_samples - 1].when); + if (diff < MIN_SAMPLE_SEPARATION) + return 0; + } + UTI_DiffTimevalsToDouble(&offset, &now, ts); /* Check if buffer full up */ @@ -258,6 +270,14 @@ MNL_Reset(void) n_samples = 0; } +/* ================================================== */ + +int +MNL_IsEnabled(void) +{ + return enabled; +} + /* ================================================== */ /* Generate report data for the REQ_MANUAL_LIST command/monitoring protocol */ diff --git a/manual.h b/manual.h index bd49b98..bf0c72a 100644 --- a/manual.h +++ b/manual.h @@ -38,6 +38,7 @@ extern int MNL_AcceptTimestamp(struct timeval *ts, long *offset_cs, double *dfre extern void MNL_Enable(void); extern void MNL_Disable(void); extern void MNL_Reset(void); +extern int MNL_IsEnabled(void); extern void MNL_ReportSamples(RPT_ManualSamplesReport *report, int max, int *n); extern int MNL_DeleteSample(int index);