From 671771ceb0fde2bd4ec2f5b9e6bc14fc269aecef Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 7 Jun 2024 18:50:14 +0200 Subject: [PATCH] Improve NTP synchronization by rejecting synchronization if the standard deviation of the time offset or round-trip delay is larger than 1 second. This ensures the time cannot go off even in cases where the network connectivity is really bad Signed-off-by: DL6ER --- src/ntp/client.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ntp/client.c b/src/ntp/client.c index 3b331062..b860a90b 100644 --- a/src/ntp/client.c +++ b/src/ntp/client.c @@ -418,6 +418,15 @@ bool ntp_client(const char *server, const bool settime, const bool print) log_info("Average time offset: (%e +/- %e s)", theta_avg, theta_stdev); log_info("Average round-trip delay: (%e +/- %e s)", delta_avg, delta_stdev); + // Reject synchronization if the standard deviation of the time offset + // or round-trip delay is larger than 1 second + if(theta_stdev > 1.0 || delta_stdev > 1.0) + { + log_warn("Standard deviation of time offset is too large, rejecting synchronization"); + free(ntp); + return false; + } + // Compute trimmed mean (average excluding outliers) double theta_trim = 0.0, delta_trim = 0.0; unsigned int trim = 0;