From 5af55d078b15f613cb615d6cfdf7db3c92436566 Mon Sep 17 00:00:00 2001
From: Viliam Lejcik <viliam.lejcik@kistler.com>
Date: Mon, 16 Jan 2017 18:31:33 +0100
Subject: [PATCH] ptp4l: Make UTC offset configurable.

Currently UTC offset is defined as a constant - CURRENT_UTC_OFFSET, and if
a leap second is added, that constant is no longer valid. Ptp4l was
updated to read the UTC offset from configuration instead.

Signed-off-by: Viliam Lejcik <viliam.lejcik@kistler.com>
---
 clock.c     | 12 +++++++-----
 config.c    |  1 +
 default.cfg |  1 +
 gPTP.cfg    |  1 +
 ptp4l.8     |  4 ++++
 5 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/clock.c b/clock.c
index c716f01..393aa41 100644
--- a/clock.c
+++ b/clock.c
@@ -681,7 +681,7 @@ static void clock_update_slave(struct clock *c)
 	if (!(c->tds.flags & PTP_TIMESCALE)) {
 		pr_warning("foreign master not using PTP timescale");
 	}
-	if (c->tds.currentUtcOffset < CURRENT_UTC_OFFSET) {
+	if (c->tds.currentUtcOffset < config_get_int(c->config, NULL, "utc_offset")) {
 		pr_warning("running in a temporal vortex");
 	}
 }
@@ -689,18 +689,20 @@ static void clock_update_slave(struct clock *c)
 static int clock_utc_correct(struct clock *c, tmv_t ingress)
 {
 	struct timespec offset;
-	int utc_offset, leap, clock_leap;
+	int utc_offset, leap, clock_leap, current_utc_offset;
 	uint64_t ts;
 
 	if (!c->utc_timescale)
 		return 0;
 
+	current_utc_offset = config_get_int(c->config, NULL, "utc_offset");
+
 	if (c->tds.flags & UTC_OFF_VALID && c->tds.flags & TIME_TRACEABLE) {
 		utc_offset = c->tds.currentUtcOffset;
-	} else if (c->tds.currentUtcOffset > CURRENT_UTC_OFFSET) {
+	} else if (c->tds.currentUtcOffset > current_utc_offset) {
 		utc_offset = c->tds.currentUtcOffset;
 	} else {
-		utc_offset = CURRENT_UTC_OFFSET;
+		utc_offset = current_utc_offset;
 	}
 
 	if (c->tds.flags & LEAP_61) {
@@ -1035,7 +1037,7 @@ struct clock *clock_create(enum clock_type type, struct config *config,
 	c->freq_est_interval = config_get_int(config, NULL, "freq_est_interval");
 	c->grand_master_capable = config_get_int(config, NULL, "gmCapable");
 	c->kernel_leap = config_get_int(config, NULL, "kernel_leap");
-	c->utc_offset = CURRENT_UTC_OFFSET;
+	c->utc_offset = config_get_int(c->config, NULL, "utc_offset");
 	c->time_source = config_get_int(config, NULL, "timeSource");
 
 	if (c->free_running) {
diff --git a/config.c b/config.c
index b19f3ad..7bb949d 100644
--- a/config.c
+++ b/config.c
@@ -237,6 +237,7 @@ struct config_item config_tab[] = {
 	GLOB_ITEM_STR("uds_address", "/var/run/ptp4l"),
 	GLOB_ITEM_INT("use_syslog", 1, 0, 1),
 	GLOB_ITEM_STR("userDescription", ""),
+	GLOB_ITEM_INT("utc_offset", CURRENT_UTC_OFFSET, 0, INT_MAX),
 	GLOB_ITEM_INT("verbose", 0, 0, 1),
 };
 
diff --git a/default.cfg b/default.cfg
index 12542c0..ebb263a 100644
--- a/default.cfg
+++ b/default.cfg
@@ -7,6 +7,7 @@ slaveOnly		0
 priority1		128
 priority2		128
 domainNumber		0
+#utc_offset		37
 clockClass		248
 clockAccuracy		0xFE
 offsetScaledLogVariance	0xFFFF
diff --git a/gPTP.cfg b/gPTP.cfg
index 75e996c..142996a 100644
--- a/gPTP.cfg
+++ b/gPTP.cfg
@@ -7,6 +7,7 @@ gmCapable		1
 priority1		248
 priority2		248
 domainNumber		0
+#utc_offset		37
 clockClass		248
 clockAccuracy		0xFE
 offsetScaledLogVariance	0xFFFF
diff --git a/ptp4l.8 b/ptp4l.8
index f53fc6e..2abedf9 100644
--- a/ptp4l.8
+++ b/ptp4l.8
@@ -327,6 +327,10 @@ The default is 0xFFFF.
 The domain attribute of the local clock.
 The default is 0.
 .TP
+.B utc_offset
+The current number of leap seconds.
+The default is 37.
+.TP
 .B free_running
 Don't adjust the local clock if enabled.
 The default is 0 (disabled).
-- 
2.8.1.windows.1

