Signed-off-by: Miroslav Lichvar <***@redhat.com>
---
phc2sys.8 | 9 ++++++++-
phc2sys.c | 34 +++++++++++++++++++++++++---------
2 files changed, 33 insertions(+), 10 deletions(-)
diff --git a/phc2sys.8 b/phc2sys.8
index f5f72b4..ad8855a 100644
--- a/phc2sys.8
+++ b/phc2sys.8
@@ -30,6 +30,8 @@ phc2sys \- synchronize two clocks
.BI \-u " summary-updates"
] [
.B \-w
+|
+.B \-W
] [
.BI \-n " domain-number"
] [
@@ -125,12 +127,17 @@ statistics. The messages are printed at the LOG_INFO level.
The default is 0 (disabled).
.TP
.B \-w
-Wait until ptp4l is in a synchronized state. If the
+Wait until ptp4l is in a synchronized state (master or slave). If the
.B \-O
option is not used, also keep the offset between the slave and master
times updated according to the currentUtcOffset value obtained from ptp4l and
the direction of the clock synchronization.
.TP
+.B \-W
+Similar to
+.BR \-w ,
+but wait until ptp4l is slave to another clock.
+.TP
.BI \-n " domain-number"
Specify the domain number used by ptp4l. The default is 0.
.TP
diff --git a/phc2sys.c b/phc2sys.c
index dae5999..8942d5a 100644
--- a/phc2sys.c
+++ b/phc2sys.c
@@ -56,6 +56,12 @@
#define PHC_PPS_OFFSET_LIMIT 10000000
#define PMC_UPDATE_INTERVAL (60 * NS_PER_SEC)
+enum wait_state {
+ WS_ANY = 0,
+ WS_SLAVE_OR_MASTER,
+ WS_SLAVE
+};
+
struct clock;
static int update_sync_offset(struct clock *clock, int64_t offset, uint64_t ts);
@@ -349,7 +355,7 @@ static int init_pmc(struct clock *clock, int domain_number)
}
static int run_pmc(struct clock *clock, int timeout,
- int wait_sync, int get_utc_offset)
+ enum wait_state wait_state, int get_utc_offset)
{
struct ptp_message *msg;
void *data;
@@ -365,7 +371,7 @@ static int run_pmc(struct clock *clock, int timeout,
while (clock->pmc_ds_idx < N_ID) {
/* Check if the data set is really needed. */
if ((ds_ids[clock->pmc_ds_idx] == PORT_DATA_SET &&
- !wait_sync) ||
+ !wait_state) ||
(ds_ids[clock->pmc_ds_idx] == TIME_PROPERTIES_DATA_SET &&
!get_utc_offset)) {
clock->pmc_ds_idx++;
@@ -417,8 +423,13 @@ static int run_pmc(struct clock *clock, int timeout,
case PORT_DATA_SET:
switch (((struct portDS *)data)->portState) {
case PS_MASTER:
+ if (wait_state == WS_SLAVE_OR_MASTER)
+ ds_done = 1;
+ break;
case PS_SLAVE:
- ds_done = 1;
+ if (wait_state == WS_SLAVE_OR_MASTER ||
+ wait_state == WS_SLAVE)
+ ds_done = 1;
break;
}
@@ -524,7 +535,8 @@ static void usage(char *progname)
" -N [num] number of master clock readings per update (5)\n"
" -O [offset] slave-master time offset (0)\n"
" -u [num] number of clock updates in summary stats (0)\n"
- " -w wait for ptp4l\n"
+ " -w wait for ptp4l to be slave or master\n"
+ " -W wait for ptp4l to be slave\n"
" -n [num] domain number (0)\n"
" -x apply leap seconds by servo instead of kernel\n"
" -l [num] set the logging level to 'num' (6)\n"
@@ -540,8 +552,9 @@ int main(int argc, char *argv[])
{
char *progname, *ethdev = NULL;
clockid_t src = CLOCK_INVALID;
+ enum wait_state wait_state = WS_ANY;
int c, domain_number = 0, phc_readings = 5, phc_rate = 1, pps_fd = -1;
- int max_ppb, r, wait_sync = 0, forced_sync_offset = 0;
+ int max_ppb, r, forced_sync_offset = 0;
int print_level = LOG_INFO, use_syslog = 1, verbose = 0;
double ppb;
struct clock dst_clock = {
@@ -557,7 +570,7 @@ int main(int argc, char *argv[])
progname = strrchr(argv[0], '/');
progname = progname ? 1+progname : argv[0];
while (EOF != (c = getopt(argc, argv,
- "c:d:hs:P:I:S:R:N:O:i:u:wn:xl:mqv"))) {
+ "c:d:hs:P:I:S:R:N:O:i:u:wWn:xl:mqv"))) {
switch (c) {
case 'c':
dst_clock.clkid = clock_open(optarg);
@@ -600,7 +613,10 @@ int main(int argc, char *argv[])
dst_clock.stats_max_count = atoi(optarg);
break;
case 'w':
- wait_sync = 1;
+ wait_state = WS_SLAVE_OR_MASTER;
+ break;
+ case 'W':
+ wait_state = WS_SLAVE;
break;
case 'n':
domain_number = atoi(optarg);
@@ -667,13 +683,13 @@ int main(int argc, char *argv[])
print_set_syslog(use_syslog);
print_set_level(print_level);
- if (wait_sync) {
+ if (wait_state) {
if (init_pmc(&dst_clock, domain_number))
return -1;
while (1) {
r = run_pmc(&dst_clock, 1000,
- wait_sync, !forced_sync_offset);
+ wait_state, !forced_sync_offset);
if (r < 0)
return -1;
else if (r > 0)
--
1.8.1.4