From 57b98c216a7c93f7378c9978d93d2dc99f7d7a71 Mon Sep 17 00:00:00 2001 From: Anders Selhammer Date: Fri, 14 Sep 2018 11:14:58 +0200 Subject: [PATCH] Add possibility to set clockIdentity Currently the clockIdentity is generated from the mac address of the first interface/port in config file. This patch add the possibility to set it in config file. The reason is if the stack is restarted with a different set of ports, it may be circumstances when clockIdentity needs to be equal as before restart even if the port setup is different. Signed-off-by: Anders Selhammer --- clock.c | 16 +++++++++++++--- config.c | 1 + ptp4l.8 | 9 +++++++++ util.c | 15 +++++++++++++++ util.h | 9 +++++++++ 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/clock.c b/clock.c index 47592ac..0a000b9 100644 --- a/clock.c +++ b/clock.c @@ -988,9 +988,19 @@ struct clock *clock_create(enum clock_type type, struct config *config, pr_info("selected /dev/ptp%d as PTP clock", phc_index); } - if (generate_clock_identity(&c->dds.clockIdentity, iface->name)) { - pr_err("failed to generate a clock identity"); - return NULL; + if (strcmp(config_get_string(config, NULL, "clockIdentity"), + "000000.0000.000000") == 0) { + if (generate_clock_identity(&c->dds.clockIdentity, + iface->name)) { + pr_err("failed to generate a clock identity"); + return NULL; + } + } else { + if (str2cid(config_get_string(config, NULL, "clockIdentity"), + &c->dds.clockIdentity)) { + pr_err("failed to set clock identity"); + return NULL; + } } /* Configure the UDS. */ diff --git a/config.c b/config.c index 3455b5a..ac082bf 100644 --- a/config.c +++ b/config.c @@ -202,6 +202,7 @@ struct config_item config_tab[] = { GLOB_ITEM_INT("check_fup_sync", 0, 0, 1), GLOB_ITEM_INT("clockAccuracy", 0xfe, 0, UINT8_MAX), GLOB_ITEM_INT("clockClass", 248, 0, UINT8_MAX), + GLOB_ITEM_STR("clockIdentity", "000000.0000.000000"), GLOB_ITEM_ENU("clock_servo", CLOCK_SERVO_PI, clock_servo_enu), GLOB_ITEM_ENU("clock_type", CLOCK_TYPE_ORDINARY, clock_type_enu), GLOB_ITEM_ENU("dataset_comparison", DS_CMP_IEEE1588, dataset_comp_enu), diff --git a/ptp4l.8 b/ptp4l.8 index 90454fa..d1751df 100644 --- a/ptp4l.8 +++ b/ptp4l.8 @@ -395,6 +395,15 @@ The clockAccuracy attribute of the local clock. It is used in the best master selection algorithm. The default is 0xFE. .TP +.B clockIdentity +The clockIdentity attribute of the local clock. +The clockIdentity is an 8-octet array and should in this configuration be +written in textual form, see default. It should be unique since it is used to +identify the specific clock. +If default is used or if not set at all, the clockIdentity will be automtically +generated. +The default is "000000.0000.000000" +.TP .B offsetScaledLogVariance The offsetScaledLogVariance attribute of the local clock. It characterizes the stability of the clock. diff --git a/util.c b/util.c index 73fb37e..c617510 100644 --- a/util.c +++ b/util.c @@ -218,6 +218,21 @@ int str2mac(const char *s, unsigned char mac[MAC_LEN]) return 0; } +int str2cid(const char *s, struct ClockIdentity *result) +{ + struct ClockIdentity cid; + unsigned char *ptr = cid.id; + int c; + c = sscanf(s, " %02hhx%02hhx%02hhx.%02hhx%02hhx.%02hhx%02hhx%02hhx", + &ptr[0], &ptr[1], &ptr[2], &ptr[3], + &ptr[4], &ptr[5], &ptr[6], &ptr[7]); + if (c == 8) { + *result = cid; + return 0; + } + return -1; +} + int str2pid(const char *s, struct PortIdentity *result) { struct PortIdentity pid; diff --git a/util.h b/util.h index 1ab1b3f..39d602e 100644 --- a/util.h +++ b/util.h @@ -133,6 +133,15 @@ int str2addr(enum transport_type type, const char *s, struct address *addr); */ int str2mac(const char *s, unsigned char mac[MAC_LEN]); +/** + * Scan a string containing a clock identity and convert it into binary form. + * + * @param s String in human readable form. + * @param result Pointer to a buffer to hold the result. + * @return Zero on success, or -1 if the string is incorrectly formatted. + */ +int str2cid(const char *s, struct ClockIdentity *result); + /** * Scan a string containing a port identity and convert it into binary form. *