phc2sys, phc_ctl: Re-factor common code.

The two programs, phc2sys and phc_ctl, use nearly identical subroutines to
open an instance of a dynamic posix clock.  In anticipation of yet a third
program with similar needs, this patch refactors the common code into the
utilities.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Richard Cochran 2019-07-07 18:13:39 -07:00
parent 46b673792a
commit d32758aa09
5 changed files with 52 additions and 77 deletions

View File

@ -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))

View File

@ -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;

View File

@ -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;

34
util.c
View File

@ -25,6 +25,7 @@
#include <string.h>
#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];

8
util.h
View File

@ -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.
*