From d7e47e64e14fac605f15dbeb36f59152b5478f70 Mon Sep 17 00:00:00 2001
From: Viliam Lejcik <viliam.lejcik@kistler.com>
Date: Tue, 17 Jan 2017 18:25:26 +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     | 9 +++++----
 config.c    | 1 +
 default.cfg | 1 +
 gPTP.cfg    | 1 +
 ptp4l.8     | 4 ++++
 5 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/clock.c b/clock.c
index c716f01..a6a1a1a 100644
--- a/clock.c
+++ b/clock.c
@@ -105,6 +105,7 @@ struct clock {
 	int leap_set;
 	int kernel_leap;
 	int utc_offset;  /* grand master role */
+	int current_utc_offset;  /* UTC offset fallback */
 	int time_flags;  /* grand master role */
 	int time_source; /* grand master role */
 	enum servo_state servo_state;
@@ -681,7 +682,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 < c->current_utc_offset) {
 		pr_warning("running in a temporal vortex");
 	}
 }
@@ -697,10 +698,10 @@ static int clock_utc_correct(struct clock *c, tmv_t ingress)
 
 	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 > c->current_utc_offset) {
 		utc_offset = c->tds.currentUtcOffset;
 	} else {
-		utc_offset = CURRENT_UTC_OFFSET;
+		utc_offset = c->current_utc_offset;
 	}
 
 	if (c->tds.flags & LEAP_61) {
@@ -1035,7 +1036,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 = c->current_utc_offset = config_get_int(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..53d5f28 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 offset between TAI and UTC.
+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

