From 9c8fe6a94ed2591c4fbc6e284072511450ee6324 Mon Sep 17 00:00:00 2001 From: Richard Cochran Date: Sun, 18 Oct 2015 16:26:49 +0200 Subject: [PATCH] clock: specify type at creation time. Store the type in the clock object explicitly. Signed-off-by: Richard Cochran --- clock.c | 20 +++++++++++++++----- clock.h | 4 +++- ptp4l.c | 3 ++- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/clock.c b/clock.c index d18cab0..17c9687 100644 --- a/clock.c +++ b/clock.c @@ -74,6 +74,7 @@ struct clock_subscriber { }; struct clock { + enum clock_type type; struct config *config; clockid_t clkid; struct servo *servo; @@ -799,7 +800,8 @@ static void clock_remove_port(struct clock *c, struct port *p) port_close(p); } -struct clock *clock_create(struct config *config, int phc_index) +struct clock *clock_create(enum clock_type type, struct config *config, + int phc_index) { enum timestamp_type timestamping = config_get_int(config, NULL, "time_stamping"); @@ -821,6 +823,17 @@ struct clock *clock_create(struct config *config, int phc_index) if (c->nports) clock_destroy(c); + switch (type) { + case CLOCK_TYPE_ORDINARY: + case CLOCK_TYPE_BOUNDARY: + c->type = type; + break; + case CLOCK_TYPE_P2P: + case CLOCK_TYPE_E2E: + case CLOCK_TYPE_MANAGEMENT: + return NULL; + } + /* Initialize the defaultDS. */ c->dds.clockQuality.clockClass = config_get_int(config, NULL, "clockClass"); @@ -1666,10 +1679,7 @@ struct clock_description *clock_description(struct clock *c) enum clock_type clock_type(struct clock *c) { - if (c->nports > 1) { - return CLOCK_TYPE_BOUNDARY; - } - return CLOCK_TYPE_ORDINARY; + return c->type; } void clock_check_ts(struct clock *c, struct timespec ts) diff --git a/clock.h b/clock.h index ec38146..d4a57e9 100644 --- a/clock.h +++ b/clock.h @@ -76,12 +76,14 @@ struct config *clock_config(struct clock *c); * Create a clock instance. There can only be one clock in any system, * so subsequent calls will destroy the previous clock instance. * + * @param type Specifies which type of clock to create. * @param config Pointer to the configuration database. * @param phc_index PTP hardware clock device to use. * Pass -1 to select CLOCK_REALTIME. * @return A pointer to the single global clock instance. */ -struct clock *clock_create(struct config *config, int phc_index); +struct clock *clock_create(enum clock_type type, struct config *config, + int phc_index); /** * Obtains a clock's default data set. diff --git a/ptp4l.c b/ptp4l.c index d48324b..c3694b0 100644 --- a/ptp4l.c +++ b/ptp4l.c @@ -222,7 +222,8 @@ int main(int argc, char *argv[]) pr_info("selected /dev/ptp%d as PTP clock", phc_index); } - clock = clock_create(cfg, phc_index); + clock = clock_create(cfg->n_interfaces > 1 ? CLOCK_TYPE_BOUNDARY : + CLOCK_TYPE_ORDINARY, cfg, phc_index); if (!clock) { fprintf(stderr, "failed to create a clock\n"); goto out;