2012-01-25 02:12:57 +08:00
|
|
|
/**
|
|
|
|
* @file phc2sys.c
|
|
|
|
* @brief Utility program to synchronize two clocks via a PPS.
|
|
|
|
* @note Copyright (C) 2012 Richard Cochran <richardcochran@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2013-01-30 01:06:18 +08:00
|
|
|
#include <poll.h>
|
2012-01-25 02:12:57 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2012-12-18 21:00:49 +08:00
|
|
|
#include <inttypes.h>
|
2012-01-25 02:12:57 +08:00
|
|
|
|
|
|
|
#include <linux/pps.h>
|
|
|
|
#include <linux/ptp_clock.h>
|
|
|
|
|
2013-01-30 01:06:18 +08:00
|
|
|
#include "ds.h"
|
|
|
|
#include "fsm.h"
|
2012-01-25 02:12:57 +08:00
|
|
|
#include "missing.h"
|
2013-01-15 00:09:12 +08:00
|
|
|
#include "pi.h"
|
2013-01-30 01:06:18 +08:00
|
|
|
#include "pmc_common.h"
|
|
|
|
#include "print.h"
|
2013-01-15 00:09:12 +08:00
|
|
|
#include "servo.h"
|
2012-09-28 11:42:37 +08:00
|
|
|
#include "sk.h"
|
2012-11-26 20:25:35 +08:00
|
|
|
#include "sysoff.h"
|
2013-01-30 01:06:18 +08:00
|
|
|
#include "tlv.h"
|
2012-12-10 17:28:28 +08:00
|
|
|
#include "version.h"
|
2012-01-25 02:12:57 +08:00
|
|
|
|
|
|
|
#define KP 0.7
|
|
|
|
#define KI 0.3
|
|
|
|
#define NS_PER_SEC 1000000000LL
|
|
|
|
|
|
|
|
#define max_ppb 512000
|
|
|
|
|
2013-01-18 01:31:39 +08:00
|
|
|
#define PHC_PPS_OFFSET_LIMIT 10000000
|
|
|
|
|
2012-01-25 02:12:57 +08:00
|
|
|
static clockid_t clock_open(char *device)
|
|
|
|
{
|
2012-11-01 22:15:35 +08:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (device[0] != '/') {
|
|
|
|
if (!strcasecmp(device, "CLOCK_REALTIME"))
|
|
|
|
return CLOCK_REALTIME;
|
|
|
|
|
|
|
|
fprintf(stderr, "unknown clock %s\n", device);
|
|
|
|
return CLOCK_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(device, O_RDWR);
|
2012-01-25 02:12:57 +08:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "cannot open %s: %m\n", device);
|
|
|
|
return CLOCK_INVALID;
|
|
|
|
}
|
|
|
|
return FD_TO_CLOCKID(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clock_ppb(clockid_t clkid, double ppb)
|
|
|
|
{
|
|
|
|
struct timex tx;
|
|
|
|
memset(&tx, 0, sizeof(tx));
|
|
|
|
tx.modes = ADJ_FREQUENCY;
|
|
|
|
tx.freq = (long) (ppb * 65.536);
|
|
|
|
if (clock_adjtime(clkid, &tx) < 0)
|
|
|
|
fprintf(stderr, "failed to adjust the clock: %m\n");
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:09:14 +08:00
|
|
|
static double clock_ppb_read(clockid_t clkid)
|
|
|
|
{
|
|
|
|
double f = 0.0;
|
|
|
|
struct timex tx;
|
|
|
|
memset(&tx, 0, sizeof(tx));
|
|
|
|
if (clock_adjtime(clkid, &tx) < 0)
|
|
|
|
fprintf(stderr, "failed to read out the clock frequency adjustment: %m\n");
|
|
|
|
else
|
|
|
|
f = tx.freq / 65.536;
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2012-01-25 02:12:57 +08:00
|
|
|
static void clock_step(clockid_t clkid, int64_t ns)
|
|
|
|
{
|
|
|
|
struct timex tx;
|
|
|
|
int sign = 1;
|
|
|
|
if (ns < 0) {
|
|
|
|
sign = -1;
|
|
|
|
ns *= -1;
|
|
|
|
}
|
|
|
|
memset(&tx, 0, sizeof(tx));
|
|
|
|
tx.modes = ADJ_SETOFFSET | ADJ_NANO;
|
|
|
|
tx.time.tv_sec = sign * (ns / NS_PER_SEC);
|
|
|
|
tx.time.tv_usec = sign * (ns % NS_PER_SEC);
|
|
|
|
/*
|
|
|
|
* The value of a timeval is the sum of its fields, but the
|
|
|
|
* field tv_usec must always be non-negative.
|
|
|
|
*/
|
|
|
|
if (tx.time.tv_usec < 0) {
|
|
|
|
tx.time.tv_sec -= 1;
|
|
|
|
tx.time.tv_usec += 1000000000;
|
|
|
|
}
|
|
|
|
if (clock_adjtime(clkid, &tx) < 0)
|
|
|
|
fprintf(stderr, "failed to step clock: %m\n");
|
|
|
|
}
|
|
|
|
|
2012-09-26 02:03:04 +08:00
|
|
|
static int read_phc(clockid_t clkid, clockid_t sysclk, int readings,
|
|
|
|
int64_t *offset, uint64_t *ts)
|
2012-01-25 02:12:57 +08:00
|
|
|
{
|
2012-08-29 21:28:42 +08:00
|
|
|
struct timespec tdst1, tdst2, tsrc;
|
|
|
|
int i;
|
|
|
|
int64_t interval, best_interval = INT64_MAX;
|
2012-01-25 02:12:57 +08:00
|
|
|
|
2012-08-29 21:28:42 +08:00
|
|
|
/* Pick the quickest clkid reading. */
|
2012-08-31 01:44:38 +08:00
|
|
|
for (i = 0; i < readings; i++) {
|
2012-08-29 21:28:42 +08:00
|
|
|
if (clock_gettime(sysclk, &tdst1) ||
|
|
|
|
clock_gettime(clkid, &tsrc) ||
|
|
|
|
clock_gettime(sysclk, &tdst2)) {
|
|
|
|
perror("clock_gettime");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
interval = (tdst2.tv_sec - tdst1.tv_sec) * NS_PER_SEC +
|
|
|
|
tdst2.tv_nsec - tdst1.tv_nsec;
|
|
|
|
|
|
|
|
if (best_interval > interval) {
|
|
|
|
best_interval = interval;
|
|
|
|
*offset = (tdst1.tv_sec - tsrc.tv_sec) * NS_PER_SEC +
|
2012-09-26 02:03:04 +08:00
|
|
|
tdst1.tv_nsec - tsrc.tv_nsec + interval / 2;
|
2012-08-29 21:28:42 +08:00
|
|
|
*ts = tdst2.tv_sec * NS_PER_SEC + tdst2.tv_nsec;
|
|
|
|
}
|
2012-08-30 20:45:50 +08:00
|
|
|
}
|
2012-08-29 21:29:43 +08:00
|
|
|
|
|
|
|
return 1;
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
|
|
|
|
2013-01-15 00:09:12 +08:00
|
|
|
struct clock {
|
|
|
|
clockid_t clkid;
|
|
|
|
struct servo *servo;
|
|
|
|
FILE *log_file;
|
|
|
|
const char *source_label;
|
2012-01-25 02:12:57 +08:00
|
|
|
};
|
|
|
|
|
2013-01-15 00:09:12 +08:00
|
|
|
static void update_clock(struct clock *clock, int64_t offset, uint64_t ts)
|
2012-09-26 14:42:19 +08:00
|
|
|
{
|
2013-01-15 00:09:12 +08:00
|
|
|
enum servo_state state;
|
|
|
|
double ppb;
|
2012-09-26 14:42:19 +08:00
|
|
|
|
2013-01-15 00:09:12 +08:00
|
|
|
ppb = servo_sample(clock->servo, offset, ts, &state);
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case SERVO_UNLOCKED:
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
2013-01-15 00:09:12 +08:00
|
|
|
case SERVO_JUMP:
|
|
|
|
clock_step(clock->clkid, -offset);
|
|
|
|
/* Fall through. */
|
|
|
|
case SERVO_LOCKED:
|
|
|
|
clock_ppb(clock->clkid, -ppb);
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-01-15 00:09:12 +08:00
|
|
|
|
|
|
|
fprintf(clock->log_file, "%s %9" PRId64 " s%d %lld.%09llu adj %.2f\n",
|
|
|
|
clock->source_label, offset, state,
|
|
|
|
ts / NS_PER_SEC, ts % NS_PER_SEC, ppb);
|
|
|
|
fflush(clock->log_file);
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
|
|
|
|
2012-08-29 21:29:43 +08:00
|
|
|
static int read_pps(int fd, int64_t *offset, uint64_t *ts)
|
2012-01-25 02:12:57 +08:00
|
|
|
{
|
|
|
|
struct pps_fdata pfd;
|
2012-08-29 21:29:43 +08:00
|
|
|
|
2012-01-25 02:12:57 +08:00
|
|
|
pfd.timeout.sec = 10;
|
|
|
|
pfd.timeout.nsec = 0;
|
|
|
|
pfd.timeout.flags = ~PPS_TIME_INVALID;
|
|
|
|
if (ioctl(fd, PPS_FETCH, &pfd)) {
|
|
|
|
perror("ioctl PPS_FETCH");
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-29 21:29:43 +08:00
|
|
|
|
|
|
|
*ts = pfd.info.assert_tu.sec * NS_PER_SEC;
|
|
|
|
*ts += pfd.info.assert_tu.nsec;
|
|
|
|
|
|
|
|
*offset = *ts % NS_PER_SEC;
|
|
|
|
if (*offset > NS_PER_SEC / 2)
|
|
|
|
*offset -= NS_PER_SEC;
|
|
|
|
|
|
|
|
return 1;
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
|
|
|
|
2013-01-15 00:09:15 +08:00
|
|
|
static int do_pps_loop(struct clock *clock, char *pps_device,
|
|
|
|
clockid_t src, int n_readings, int sync_offset)
|
2012-09-26 02:00:32 +08:00
|
|
|
{
|
2013-01-18 01:31:39 +08:00
|
|
|
int64_t pps_offset, phc_offset;
|
|
|
|
uint64_t pps_ts, phc_ts;
|
2012-09-26 02:00:32 +08:00
|
|
|
int fd;
|
|
|
|
|
2013-01-15 00:09:12 +08:00
|
|
|
clock->source_label = "pps";
|
|
|
|
|
2012-09-26 02:00:32 +08:00
|
|
|
fd = open(pps_device, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "cannot open '%s': %m\n", pps_device);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-01-15 00:09:15 +08:00
|
|
|
|
2012-09-26 02:00:32 +08:00
|
|
|
while (1) {
|
|
|
|
if (!read_pps(fd, &pps_offset, &pps_ts)) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-18 01:31:39 +08:00
|
|
|
|
|
|
|
/* If a PHC is available, use it to get the whole number
|
|
|
|
of seconds in the offset and PPS for the rest. */
|
|
|
|
if (src != CLOCK_INVALID) {
|
|
|
|
if (!read_phc(src, clock->clkid, n_readings,
|
|
|
|
&phc_offset, &phc_ts))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Convert the time stamp to the PHC time. */
|
|
|
|
phc_ts -= phc_offset;
|
|
|
|
|
|
|
|
/* Check if it is close to the start of the second. */
|
|
|
|
if (phc_ts % NS_PER_SEC > PHC_PPS_OFFSET_LIMIT) {
|
|
|
|
fprintf(stderr, "PPS is not in sync with PHC"
|
|
|
|
" (0.%09lld)\n", phc_ts % NS_PER_SEC);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
phc_ts = phc_ts / NS_PER_SEC * NS_PER_SEC;
|
|
|
|
pps_offset = pps_ts - phc_ts;
|
|
|
|
pps_offset -= sync_offset * NS_PER_SEC;
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:09:12 +08:00
|
|
|
update_clock(clock, pps_offset, pps_ts);
|
2012-09-26 02:00:32 +08:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:09:12 +08:00
|
|
|
static int do_sysoff_loop(struct clock *clock, clockid_t src,
|
|
|
|
int rate, int n_readings, int sync_offset)
|
2012-11-26 20:25:35 +08:00
|
|
|
{
|
|
|
|
uint64_t ts;
|
|
|
|
int64_t offset;
|
|
|
|
int err = 0, fd = CLOCKID_TO_FD(src);
|
2013-01-15 00:09:12 +08:00
|
|
|
|
|
|
|
clock->source_label = "sys";
|
|
|
|
|
2012-11-26 20:25:35 +08:00
|
|
|
while (1) {
|
|
|
|
usleep(1000000 / rate);
|
|
|
|
if (sysoff_measure(fd, n_readings, &offset, &ts)) {
|
|
|
|
err = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset -= sync_offset * NS_PER_SEC;
|
2013-01-15 00:09:12 +08:00
|
|
|
update_clock(clock, offset, ts);
|
2012-11-26 20:25:35 +08:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:09:13 +08:00
|
|
|
static int do_phc_loop(struct clock *clock, clockid_t src,
|
|
|
|
int rate, int n_readings, int sync_offset)
|
|
|
|
{
|
|
|
|
uint64_t ts;
|
|
|
|
int64_t offset;
|
|
|
|
|
|
|
|
clock->source_label = "phc";
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
usleep(1000000 / rate);
|
|
|
|
if (!read_phc(src, clock->clkid, n_readings, &offset, &ts)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
offset -= sync_offset * NS_PER_SEC;
|
|
|
|
update_clock(clock, offset, ts);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-30 01:06:18 +08:00
|
|
|
static int is_msg_mgt(struct ptp_message *msg)
|
|
|
|
{
|
|
|
|
struct TLV *tlv;
|
|
|
|
|
|
|
|
if (msg_type(msg) != MANAGEMENT)
|
|
|
|
return 0;
|
|
|
|
if (management_action(msg) != RESPONSE)
|
|
|
|
return 0;
|
|
|
|
if (msg->tlv_count != 1)
|
|
|
|
return 0;
|
|
|
|
tlv = (struct TLV *) msg->management.suffix;
|
|
|
|
if (tlv->type != TLV_MANAGEMENT)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_mgt_id(struct ptp_message *msg)
|
|
|
|
{
|
|
|
|
return ((struct management_tlv *) msg->management.suffix)->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *get_mgt_data(struct ptp_message *msg)
|
|
|
|
{
|
|
|
|
return ((struct management_tlv *) msg->management.suffix)->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run_pmc(int wait_sync, int *utc_offset)
|
|
|
|
{
|
|
|
|
struct ptp_message *msg;
|
|
|
|
struct pmc *pmc;
|
|
|
|
void *data;
|
|
|
|
#define N_FD 1
|
|
|
|
struct pollfd pollfd[N_FD];
|
|
|
|
#define N_ID 2
|
|
|
|
int cnt, i = 0, ds_done, ds_requested = 0;
|
|
|
|
int ds_ids[N_ID] = {
|
|
|
|
PORT_DATA_SET,
|
|
|
|
TIME_PROPERTIES_DATA_SET
|
|
|
|
};
|
|
|
|
|
|
|
|
pmc = pmc_create(TRANS_UDS, "/tmp/phc2sys", 0, 0, 0);
|
|
|
|
if (!pmc) {
|
|
|
|
fprintf(stderr, "failed to create pmc\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (i < N_ID) {
|
|
|
|
pollfd[0].fd = pmc_get_transport_fd(pmc);
|
|
|
|
pollfd[0].events = POLLIN|POLLPRI;
|
|
|
|
if (!ds_requested)
|
|
|
|
pollfd[0].events |= POLLOUT;
|
|
|
|
|
|
|
|
cnt = poll(pollfd, N_FD, 1000);
|
|
|
|
if (cnt < 0) {
|
|
|
|
fprintf(stderr, "poll failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!cnt) {
|
|
|
|
/* Request the data set again. */
|
|
|
|
ds_requested = 0;
|
|
|
|
fprintf(stderr, "Waiting for ptp4l...\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pollfd[0].revents & POLLOUT) {
|
|
|
|
pmc_send_get_action(pmc, ds_ids[i]);
|
|
|
|
ds_requested = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(pollfd[0].revents & (POLLIN|POLLPRI)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
msg = pmc_recv(pmc);
|
|
|
|
|
|
|
|
if (!msg)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!is_msg_mgt(msg) || get_mgt_id(msg) != ds_ids[i]) {
|
|
|
|
msg_put(msg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = get_mgt_data(msg);
|
|
|
|
ds_done = 0;
|
|
|
|
|
|
|
|
switch (get_mgt_id(msg)) {
|
|
|
|
case PORT_DATA_SET:
|
|
|
|
if (!wait_sync)
|
|
|
|
ds_done = 1;
|
|
|
|
|
|
|
|
switch (((struct portDS *)data)->portState) {
|
|
|
|
case PS_MASTER:
|
|
|
|
case PS_GRAND_MASTER:
|
|
|
|
case PS_SLAVE:
|
|
|
|
ds_done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case TIME_PROPERTIES_DATA_SET:
|
|
|
|
*utc_offset = ((struct timePropertiesDS *)data)->
|
|
|
|
currentUtcOffset;
|
|
|
|
ds_done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ds_done) {
|
|
|
|
/* Proceed with the next data set. */
|
|
|
|
i++;
|
|
|
|
ds_requested = 0;
|
|
|
|
}
|
|
|
|
msg_put(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
pmc_destroy(pmc);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-25 02:12:57 +08:00
|
|
|
static void usage(char *progname)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"\n"
|
|
|
|
"usage: %s [options]\n\n"
|
2012-11-01 22:15:36 +08:00
|
|
|
" -c [dev|name] slave clock (CLOCK_REALTIME)\n"
|
|
|
|
" -d [dev] master PPS device\n"
|
|
|
|
" -s [dev|name] master clock\n"
|
|
|
|
" -i [iface] master clock by network interface\n"
|
|
|
|
" -P [kp] proportional constant (0.7)\n"
|
|
|
|
" -I [ki] integration constant (0.3)\n"
|
|
|
|
" -R [rate] slave clock update rate in HZ (1)\n"
|
|
|
|
" -N [num] number of master clock readings per update (5)\n"
|
2012-11-06 00:13:30 +08:00
|
|
|
" -O [offset] slave-master time offset (0)\n"
|
2013-01-30 01:06:18 +08:00
|
|
|
" -w wait for ptp4l\n"
|
2012-11-01 22:15:36 +08:00
|
|
|
" -h prints this message and exits\n"
|
2012-12-10 17:28:28 +08:00
|
|
|
" -v prints the software version and exits\n"
|
2012-01-25 02:12:57 +08:00
|
|
|
"\n",
|
|
|
|
progname);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2012-09-28 11:42:37 +08:00
|
|
|
char *device = NULL, *progname, *ethdev = NULL;
|
2013-01-15 00:09:12 +08:00
|
|
|
clockid_t src = CLOCK_INVALID;
|
2012-11-06 00:13:30 +08:00
|
|
|
int c, phc_readings = 5, phc_rate = 1, sync_offset = 0;
|
2013-01-30 01:06:18 +08:00
|
|
|
int wait_sync = 0, forced_sync_offset = 0;
|
2013-01-15 00:09:14 +08:00
|
|
|
double ppb;
|
2013-01-15 00:09:12 +08:00
|
|
|
struct clock dst_clock = {
|
|
|
|
.clkid = CLOCK_REALTIME,
|
|
|
|
.log_file = stdout
|
|
|
|
};
|
|
|
|
|
|
|
|
configured_pi_kp = KP;
|
|
|
|
configured_pi_ki = KI;
|
2012-01-25 02:12:57 +08:00
|
|
|
|
|
|
|
/* Process the command line arguments. */
|
|
|
|
progname = strrchr(argv[0], '/');
|
|
|
|
progname = progname ? 1+progname : argv[0];
|
2013-01-30 01:06:18 +08:00
|
|
|
while (EOF != (c = getopt(argc, argv, "c:d:hs:P:I:R:N:O:i:wv"))) {
|
2012-01-25 02:12:57 +08:00
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
2013-01-15 00:09:12 +08:00
|
|
|
dst_clock.clkid = clock_open(optarg);
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
device = optarg;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
src = clock_open(optarg);
|
|
|
|
break;
|
|
|
|
case 'P':
|
2013-01-15 00:09:12 +08:00
|
|
|
configured_pi_kp = atof(optarg);
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
|
|
|
case 'I':
|
2013-01-15 00:09:12 +08:00
|
|
|
configured_pi_ki = atof(optarg);
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
2012-08-31 01:44:38 +08:00
|
|
|
case 'R':
|
|
|
|
phc_rate = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
phc_readings = atoi(optarg);
|
|
|
|
break;
|
2012-11-06 00:13:30 +08:00
|
|
|
case 'O':
|
|
|
|
sync_offset = atoi(optarg);
|
2013-01-30 01:06:18 +08:00
|
|
|
forced_sync_offset = 1;
|
2012-11-06 00:13:30 +08:00
|
|
|
break;
|
2012-09-28 11:42:37 +08:00
|
|
|
case 'i':
|
|
|
|
ethdev = optarg;
|
|
|
|
break;
|
2013-01-30 01:06:18 +08:00
|
|
|
case 'w':
|
|
|
|
wait_sync = 1;
|
|
|
|
break;
|
2012-12-10 17:28:28 +08:00
|
|
|
case 'v':
|
|
|
|
version_show(stdout);
|
|
|
|
return 0;
|
2012-01-25 02:12:57 +08:00
|
|
|
case 'h':
|
|
|
|
usage(progname);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
usage(progname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-28 11:42:37 +08:00
|
|
|
if (src == CLOCK_INVALID && ethdev) {
|
2012-11-14 05:35:04 +08:00
|
|
|
struct sk_ts_info ts_info;
|
2012-09-28 11:42:37 +08:00
|
|
|
char phc_device[16];
|
2012-11-14 05:35:04 +08:00
|
|
|
if (sk_get_ts_info(ethdev, &ts_info) || !ts_info.valid) {
|
2012-09-28 11:42:37 +08:00
|
|
|
fprintf(stderr, "can't autodiscover PHC device\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-12-18 21:00:50 +08:00
|
|
|
if (ts_info.phc_index < 0) {
|
|
|
|
fprintf(stderr, "interface %s doesn't have a PHC\n", ethdev);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-14 05:35:04 +08:00
|
|
|
sprintf(phc_device, "/dev/ptp%d", ts_info.phc_index);
|
2012-09-28 11:42:37 +08:00
|
|
|
src = clock_open(phc_device);
|
|
|
|
}
|
2013-01-15 00:09:12 +08:00
|
|
|
if (!(device || src != CLOCK_INVALID) ||
|
2013-01-18 01:31:40 +08:00
|
|
|
dst_clock.clkid == CLOCK_INVALID ||
|
|
|
|
(device && dst_clock.clkid != CLOCK_REALTIME)) {
|
2012-01-25 02:12:57 +08:00
|
|
|
usage(progname);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-08-29 21:29:43 +08:00
|
|
|
|
2013-01-30 01:06:18 +08:00
|
|
|
print_set_progname(progname);
|
|
|
|
print_set_verbose(1);
|
|
|
|
print_set_syslog(0);
|
|
|
|
|
|
|
|
if (wait_sync) {
|
|
|
|
int ptp_utc_offset;
|
|
|
|
|
|
|
|
run_pmc(wait_sync, &ptp_utc_offset);
|
|
|
|
|
|
|
|
if (!forced_sync_offset) {
|
|
|
|
if (src != CLOCK_REALTIME &&
|
|
|
|
dst_clock.clkid == CLOCK_REALTIME)
|
|
|
|
sync_offset = -ptp_utc_offset;
|
|
|
|
else if (src == CLOCK_REALTIME &&
|
|
|
|
dst_clock.clkid != CLOCK_REALTIME)
|
|
|
|
sync_offset = ptp_utc_offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:09:14 +08:00
|
|
|
ppb = clock_ppb_read(dst_clock.clkid);
|
|
|
|
/* The reading may silently fail and return 0, reset the frequency to
|
|
|
|
make sure ppb is the actual frequency of the clock. */
|
|
|
|
clock_ppb(dst_clock.clkid, ppb);
|
2013-01-15 00:09:12 +08:00
|
|
|
|
2013-01-15 00:09:14 +08:00
|
|
|
dst_clock.servo = servo_create(CLOCK_SERVO_PI, -ppb, max_ppb, 0);
|
2013-01-15 00:09:12 +08:00
|
|
|
|
2012-09-26 02:00:32 +08:00
|
|
|
if (device)
|
2013-01-15 00:09:15 +08:00
|
|
|
return do_pps_loop(&dst_clock, device, src,
|
|
|
|
phc_readings, sync_offset);
|
2012-08-29 21:29:43 +08:00
|
|
|
|
2013-01-15 00:09:12 +08:00
|
|
|
if (dst_clock.clkid == CLOCK_REALTIME &&
|
2012-11-26 20:25:35 +08:00
|
|
|
SYSOFF_SUPPORTED == sysoff_probe(CLOCKID_TO_FD(src), phc_readings))
|
2013-01-15 00:09:12 +08:00
|
|
|
return do_sysoff_loop(&dst_clock, src, phc_rate,
|
|
|
|
phc_readings, sync_offset);
|
|
|
|
|
2013-01-15 00:09:13 +08:00
|
|
|
return do_phc_loop(&dst_clock, src, phc_rate,
|
|
|
|
phc_readings, sync_offset);
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|