From a0e1154bfb4c26acac8a80477e81c08a3a49847e Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Thu, 28 Jan 2010 09:14:42 +0100 Subject: [PATCH] Add common refclock driver option parsing --- chrony.texi | 4 ++-- refclock.c | 36 ++++++++++++++++++++++++++++++++++++ refclock.h | 1 + refclock_pps.c | 9 ++------- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/chrony.texi b/chrony.texi index db48bc3..b0235cd 100644 --- a/chrony.texi +++ b/chrony.texi @@ -2229,8 +2229,8 @@ There are currently three drivers implemented: @table @code @item PPS Pulse per second (PPS) API driver. The parameter is a path to the PPS -device. Assert events are used by default. The path can have -:1 appended to use clear events instead. +device. Assert events are used by default. Driver option +@code{:clear} can be appended to the path to use clear events instead. PPS refclock needs another source (NTP or non-PPS refclock) or local directive (@pxref{local directive}) enabled to function. For example: diff --git a/refclock.c b/refclock.c index 0a9473a..ddbc075 100644 --- a/refclock.c +++ b/refclock.c @@ -58,6 +58,7 @@ struct RCL_Instance_Record { RefclockDriver *driver; void *data; char *driver_parameter; + int driver_parameter_length; int driver_poll; int driver_polled; int poll; @@ -169,6 +170,7 @@ RCL_AddRefclock(RefclockParameters *params) inst->data = NULL; inst->driver_parameter = params->driver_parameter; + inst->driver_parameter_length = 0; inst->driver_poll = params->driver_poll; inst->poll = params->poll; inst->missed_samples = 0; @@ -181,6 +183,15 @@ RCL_AddRefclock(RefclockParameters *params) inst->timeout_id = -1; inst->source = NULL; + if (inst->driver_parameter) { + int i; + + inst->driver_parameter_length = strlen(inst->driver_parameter); + for (i = 0; i < inst->driver_parameter_length; i++) + if (inst->driver_parameter[i] == ':') + inst->driver_parameter[i] = '\0'; + } + if (pps_source) { if (inst->pps_rate < 1) inst->pps_rate = 1; @@ -278,6 +289,31 @@ RCL_GetDriverParameter(RCL_Instance instance) return instance->driver_parameter; } +char * +RCL_GetDriverOption(RCL_Instance instance, char *name) +{ + char *s, *e; + int n; + + s = instance->driver_parameter; + e = s + instance->driver_parameter_length; + n = strlen(name); + + while (1) { + s += strlen(s) + 1; + if (s >= e) + break; + if (!strncmp(name, s, n)) { + if (s[n] == '=') + return s + n + 1; + if (s[n] == '\0') + return s + n; + } + } + + return NULL; +} + int RCL_AddSample(RCL_Instance instance, struct timeval *sample_time, double offset, NTP_Leap leap_status) { diff --git a/refclock.h b/refclock.h index f6fb6aa..a9ab0ae 100644 --- a/refclock.h +++ b/refclock.h @@ -64,6 +64,7 @@ extern void RCL_CycleLogFile(void); extern void RCL_SetDriverData(RCL_Instance instance, void *data); extern void *RCL_GetDriverData(RCL_Instance instance); extern char *RCL_GetDriverParameter(RCL_Instance instance); +extern char *RCL_GetDriverOption(RCL_Instance instance, char *name); extern int RCL_AddSample(RCL_Instance instance, struct timeval *sample_time, double offset, NTP_Leap leap_status); extern int RCL_AddPulse(RCL_Instance instance, struct timeval *pulse_time, double second); diff --git a/refclock_pps.c b/refclock_pps.c index b41d194..68d5e6a 100644 --- a/refclock_pps.c +++ b/refclock_pps.c @@ -46,15 +46,10 @@ static int pps_initialise(RCL_Instance instance) { pps_params_t params; struct pps_instance *pps; int fd, edge_clear, mode; - char *path, *s; + char *path; path = RCL_GetDriverParameter(instance); - - edge_clear = 0; - if ((s = strrchr(path, ':')) != NULL) { - *s = '\0'; - edge_clear = atoi(s + 1); - } + edge_clear = RCL_GetDriverOption(instance, "clear") ? 1 : 0; fd = open(path, O_RDWR); if (fd < 0) {