diff --git a/makefile b/makefile index d09a4a9..3397d3e 100644 --- a/makefile +++ b/makefile @@ -55,10 +55,10 @@ all: $(PRG) ptp4l: $(OBJ) -nsm: config.o filter.o hash.o mave.o mmedian.o msg.o nsm.o print.o raw.o \ +nsm: config.o filter.o hash.o mave.o mmedian.o msg.o nsm.o phc.o print.o raw.o \ rtnl.o sk.o transport.o tlv.o tsproc.o udp.o udp6.o uds.o util.o version.o -pmc: config.o hash.o msg.o pmc.o pmc_common.o print.o raw.o sk.o tlv.o \ +pmc: config.o hash.o msg.o phc.o pmc.o pmc_common.o print.o raw.o sk.o tlv.o \ transport.o udp.o udp6.o uds.o util.o version.o phc2sys: clockadj.o clockcheck.o config.o hash.o linreg.o msg.o ntpshm.o \ @@ -69,14 +69,14 @@ hwstamp_ctl: hwstamp_ctl.o version.o phc_ctl: phc_ctl.o phc.o sk.o util.o clockadj.o sysoff.o print.o version.o -snmp4lptp: config.o hash.o msg.o pmc_common.o print.o raw.o sk.o \ +snmp4lptp: config.o hash.o msg.o phc.o pmc_common.o print.o raw.o sk.o \ snmp4lptp.o tlv.o transport.o udp.o udp6.o uds.o util.o $(CC) $^ $(LDFLAGS) $(LOADLIBES) $(LDLIBS) $(snmplib) -o $@ snmp4lptp.o: snmp4lptp.c $(CC) $(CPPFLAGS) $(CFLAGS) $(snmpflg) -c $< -timemaster: print.o rtnl.o sk.o timemaster.o util.o version.o +timemaster: phc.o print.o rtnl.o sk.o timemaster.o util.o version.o version.o: .version version.sh $(filter-out version.d,$(DEPEND)) diff --git a/phc2sys.c b/phc2sys.c index cc48f18..28c657a 100644 --- a/phc2sys.c +++ b/phc2sys.c @@ -135,40 +135,6 @@ static int run_pmc_port_properties(struct node *node, int timeout, unsigned int port, int *state, int *tstamping, char *iface); -static clockid_t clock_open(char *device, int *phc_index) -{ - struct sk_ts_info ts_info; - char phc_device[19]; - int clkid; - - /* check if device is CLOCK_REALTIME */ - if (!strcasecmp(device, "CLOCK_REALTIME")) - return CLOCK_REALTIME; - - /* check if device is valid phc device */ - clkid = phc_open(device); - if (clkid != CLOCK_INVALID) - return clkid; - - /* check if device is a valid ethernet device */ - if (sk_get_ts_info(device, &ts_info) || !ts_info.valid) { - fprintf(stderr, "unknown clock %s: %m\n", device); - return CLOCK_INVALID; - } - - if (ts_info.phc_index < 0) { - fprintf(stderr, "interface %s does not have a PHC\n", device); - return CLOCK_INVALID; - } - - sprintf(phc_device, "/dev/ptp%d", ts_info.phc_index); - clkid = phc_open(phc_device); - if (clkid == CLOCK_INVALID) - fprintf(stderr, "cannot open %s: %m\n", device); - *phc_index = ts_info.phc_index; - return clkid; -} - static struct servo *servo_add(struct node *node, struct clock *clock) { double ppb; @@ -210,7 +176,7 @@ static struct clock *clock_add(struct node *node, char *device) int phc_index = -1; if (device) { - clkid = clock_open(device, &phc_index); + clkid = posix_clock_open(device, &phc_index); if (clkid == CLOCK_INVALID) return NULL; } @@ -371,7 +337,7 @@ static void clock_reinit(struct node *node, struct clock *clock, int new_state) /* Check if phc index changed */ if (!sk_get_ts_info(clock->device, &ts_info) && clock->phc_index != ts_info.phc_index) { - clkid = clock_open(clock->device, &phc_index); + clkid = posix_clock_open(clock->device, &phc_index); if (clkid == CLOCK_INVALID) return; diff --git a/phc_ctl.c b/phc_ctl.c index b9a9cf4..e55af59 100644 --- a/phc_ctl.c +++ b/phc_ctl.c @@ -110,39 +110,6 @@ static int64_t calculate_offset(struct timespec *ts1, return offset; } -static clockid_t clock_open(char *device) -{ - struct sk_ts_info ts_info; - char phc_device[19]; - int clkid; - - /* check if device is CLOCK_REALTIME */ - if (!strcasecmp(device, "CLOCK_REALTIME")) - return CLOCK_REALTIME; - - /* check if device is valid phc device */ - clkid = phc_open(device); - if (clkid != CLOCK_INVALID) - return clkid; - - /* check if device is a valid ethernet device */ - if (sk_get_ts_info(device, &ts_info) || !ts_info.valid) { - pr_err("unknown clock %s: %m", device); - return CLOCK_INVALID; - } - - if (ts_info.phc_index < 0) { - pr_err("interface %s does not have a PHC", device); - return CLOCK_INVALID; - } - - sprintf(phc_device, "/dev/ptp%d", ts_info.phc_index); - clkid = phc_open(phc_device); - if (clkid == CLOCK_INVALID) - pr_err("cannot open %s for %s: %m", phc_device, device); - return clkid; -} - static void usage(const char *progname) { fprintf(stderr, @@ -503,10 +470,10 @@ static int run_cmds(clockid_t clkid, int cmdc, char *cmdv[]) int main(int argc, char *argv[]) { - const char *progname; + int c, cmdc, junk, print_level = LOG_INFO, result; char **cmdv, *default_cmdv[] = { "caps" }; - int c, result, cmdc; - int print_level = LOG_INFO, verbose = 1, use_syslog = 1; + int use_syslog = 1, verbose = 1; + const char *progname; clockid_t clkid; install_handler(SIGALRM, handle_alarm); @@ -558,7 +525,7 @@ int main(int argc, char *argv[]) cmdc = argc - optind - 1; } - clkid = clock_open(argv[optind]); + clkid = posix_clock_open(argv[optind], &junk); if (clkid == CLOCK_INVALID) return -1; diff --git a/util.c b/util.c index 42c0977..833f1a5 100644 --- a/util.c +++ b/util.c @@ -25,6 +25,7 @@ #include #include "address.h" +#include "phc.h" #include "print.h" #include "sk.h" #include "util.h" @@ -171,6 +172,39 @@ char *portaddr2str(struct PortAddress *addr) return buf; } +clockid_t posix_clock_open(char *device, int *phc_index) +{ + struct sk_ts_info ts_info; + char phc_device[19]; + int clkid; + + /* check if device is CLOCK_REALTIME */ + if (!strcasecmp(device, "CLOCK_REALTIME")) { + return CLOCK_REALTIME; + } + /* check if device is valid phc device */ + clkid = phc_open(device); + if (clkid != CLOCK_INVALID) { + return clkid; + } + /* check if device is a valid ethernet device */ + if (sk_get_ts_info(device, &ts_info) || !ts_info.valid) { + pr_err("unknown clock %s: %m", device); + return CLOCK_INVALID; + } + if (ts_info.phc_index < 0) { + pr_err("interface %s does not have a PHC", device); + return CLOCK_INVALID; + } + snprintf(phc_device, sizeof(phc_device), "/dev/ptp%d", ts_info.phc_index); + clkid = phc_open(phc_device); + if (clkid == CLOCK_INVALID) { + pr_err("cannot open %s for %s: %m", phc_device, device); + } + *phc_index = ts_info.phc_index; + return clkid; +} + int str2addr(enum transport_type type, const char *s, struct address *addr) { unsigned char mac[MAC_LEN]; diff --git a/util.h b/util.h index 39d602e..9d3f227 100644 --- a/util.h +++ b/util.h @@ -103,6 +103,14 @@ char *pid2str(struct PortIdentity *id); char *portaddr2str(struct PortAddress *addr); +/** + * Opens a dynamic posix clock by name. + * @param device The PHC character device or network interface to open. + * @param phc_index Returns the PHC index, if any. + * @return A valid clock ID on success or CLOCK_INVALID otherwise. + */ +clockid_t posix_clock_open(char *device, int *phc_index); + /** * Compare two port identities for equality. *