2011-11-05 02:20:23 +08:00
|
|
|
/**
|
|
|
|
* @file clock.h
|
2011-11-13 01:44:55 +08:00
|
|
|
* @brief Implements a PTP clock.
|
2011-11-05 02:20:23 +08:00
|
|
|
* @note Copyright (C) 2011 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.
|
|
|
|
*/
|
|
|
|
#ifndef HAVE_CLOCK_H
|
|
|
|
#define HAVE_CLOCK_H
|
|
|
|
|
2012-04-05 18:04:11 +08:00
|
|
|
#include "dm.h"
|
2011-11-05 02:20:23 +08:00
|
|
|
#include "ds.h"
|
2012-08-21 01:56:24 +08:00
|
|
|
#include "config.h"
|
2014-05-07 00:41:46 +08:00
|
|
|
#include "notification.h"
|
2011-12-18 18:02:26 +08:00
|
|
|
#include "servo.h"
|
2012-08-31 22:04:12 +08:00
|
|
|
#include "tlv.h"
|
2012-04-05 20:04:15 +08:00
|
|
|
#include "tmv.h"
|
2011-11-13 00:41:20 +08:00
|
|
|
#include "transport.h"
|
|
|
|
|
2012-07-29 20:31:30 +08:00
|
|
|
struct ptp_message; /*forward declaration*/
|
|
|
|
|
2011-11-05 02:20:23 +08:00
|
|
|
/** Opaque type. */
|
|
|
|
struct clock;
|
|
|
|
|
2015-10-03 16:56:52 +08:00
|
|
|
enum clock_type {
|
|
|
|
CLOCK_TYPE_ORDINARY = 0x8000,
|
|
|
|
CLOCK_TYPE_BOUNDARY = 0x4000,
|
|
|
|
CLOCK_TYPE_P2P = 0x2000,
|
|
|
|
CLOCK_TYPE_E2E = 0x1000,
|
|
|
|
CLOCK_TYPE_MANAGEMENT = 0x0800,
|
|
|
|
};
|
|
|
|
|
2011-11-05 02:20:23 +08:00
|
|
|
/**
|
|
|
|
* Obtains a reference to the best foreign master of a clock.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return A pointer to the data set of the foreign master,
|
|
|
|
* or NULL if none has been yet discovered.
|
|
|
|
*/
|
|
|
|
struct dataset *clock_best_foreign(struct clock *c);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains a reference to the port with the best foreign master.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return A pointer to the port with the best foreign master,
|
|
|
|
* or NULL if none has been yet discovered.
|
|
|
|
*/
|
|
|
|
struct port *clock_best_port(struct clock *c);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain the clockClass attribute from a clock.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return The value of the clock's class.
|
|
|
|
*/
|
|
|
|
UInteger8 clock_class(struct clock *c);
|
|
|
|
|
2015-08-11 02:10:08 +08:00
|
|
|
/**
|
|
|
|
* Obtains a reference to the configuration database.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return A pointer to the configuration, without fail.
|
|
|
|
*/
|
|
|
|
struct config *clock_config(struct clock *c);
|
|
|
|
|
2017-11-24 15:11:07 +08:00
|
|
|
/**
|
|
|
|
* Obtains a reference to the current dataset.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return A pointer to the current dataset, without fail.
|
|
|
|
*/
|
|
|
|
struct currentDS *clock_current_dataset(struct clock *c);
|
|
|
|
|
2017-10-09 22:31:45 +08:00
|
|
|
/**
|
|
|
|
* Obtains the required time stamping mode.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return The value of required time stamping mode, which is a bit mask
|
|
|
|
* of SOF_TIMESTAMPING_ flags.
|
|
|
|
*/
|
|
|
|
int clock_required_modes(struct clock *c);
|
|
|
|
|
2011-11-13 00:41:20 +08:00
|
|
|
/**
|
|
|
|
* Create a clock instance. There can only be one clock in any system,
|
|
|
|
* so subsequent calls will destroy the previous clock instance.
|
|
|
|
*
|
2015-10-18 22:26:49 +08:00
|
|
|
* @param type Specifies which type of clock to create.
|
2015-08-11 02:05:37 +08:00
|
|
|
* @param config Pointer to the configuration database.
|
2015-10-21 16:52:25 +08:00
|
|
|
* @param phc_device PTP hardware clock device to use. Pass NULL for automatic
|
|
|
|
* selection based on the network interface.
|
2012-08-21 01:56:13 +08:00
|
|
|
* @return A pointer to the single global clock instance.
|
2011-11-13 00:41:20 +08:00
|
|
|
*/
|
2015-10-18 22:26:49 +08:00
|
|
|
struct clock *clock_create(enum clock_type type, struct config *config,
|
2015-10-21 16:52:25 +08:00
|
|
|
const char *phc_device);
|
2011-11-13 00:41:20 +08:00
|
|
|
|
2011-11-05 02:20:23 +08:00
|
|
|
/**
|
|
|
|
* Obtains a clock's default data set.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return A pointer to the data set of the clock.
|
|
|
|
*/
|
|
|
|
struct dataset *clock_default_ds(struct clock *c);
|
|
|
|
|
2012-08-28 03:09:59 +08:00
|
|
|
/**
|
|
|
|
* Free all of the resources associated with a clock.
|
|
|
|
* @param c The clock instance.
|
|
|
|
*/
|
|
|
|
void clock_destroy(struct clock *c);
|
|
|
|
|
2011-11-13 15:41:05 +08:00
|
|
|
/**
|
|
|
|
* Obtain the domain number from a clock's default data set.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return The PTP domain number.
|
|
|
|
*/
|
|
|
|
UInteger8 clock_domain_number(struct clock *c);
|
|
|
|
|
2015-11-01 03:44:15 +08:00
|
|
|
/**
|
|
|
|
* Obtains a reference to the first port in the clock's list.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return A pointer to a port, or NULL if no ports are present.
|
|
|
|
*/
|
|
|
|
struct port *clock_first_port(struct clock *c);
|
|
|
|
|
2012-08-31 22:04:12 +08:00
|
|
|
/**
|
|
|
|
* Provide the follow_up info TLV from a slave port.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @param f Pointer to the TLV.
|
|
|
|
*/
|
|
|
|
void clock_follow_up_info(struct clock *c, struct follow_up_info_tlv *f);
|
|
|
|
|
2013-12-02 03:32:05 +08:00
|
|
|
/**
|
|
|
|
* Obtain the gmCapable flag from a clock's default data set.
|
|
|
|
* This function is specific to the 802.1AS standard.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return One if the clock is capable of becoming grand master, zero otherwise.
|
|
|
|
*/
|
|
|
|
int clock_gm_capable(struct clock *c);
|
|
|
|
|
2011-11-13 15:41:05 +08:00
|
|
|
/**
|
|
|
|
* Obtain a clock's identity from its default data set.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return The clock's identity.
|
|
|
|
*/
|
|
|
|
struct ClockIdentity clock_identity(struct clock *c);
|
|
|
|
|
|
|
|
/**
|
2014-08-14 21:56:03 +08:00
|
|
|
* Informs clock that a file descriptor of one of its ports changed. The
|
|
|
|
* clock will rebuild its array of file descriptors to poll.
|
2011-11-13 15:41:05 +08:00
|
|
|
* @param c The clock instance.
|
|
|
|
*/
|
2014-08-14 21:56:03 +08:00
|
|
|
void clock_fda_changed(struct clock *c);
|
2011-11-13 15:41:05 +08:00
|
|
|
|
2017-11-27 11:19:15 +08:00
|
|
|
/**
|
|
|
|
* Obtains the time of the latest synchronization.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return The local time stamp of the last received Sync message.
|
|
|
|
*/
|
|
|
|
tmv_t clock_ingress_time(struct clock *c);
|
|
|
|
|
2012-07-29 20:31:30 +08:00
|
|
|
/**
|
|
|
|
* Manage the clock according to a given message.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @param p The port on which the message arrived.
|
|
|
|
* @param msg A management message.
|
2013-07-08 03:27:32 +08:00
|
|
|
* @return One if the management action caused a change that
|
|
|
|
* implies a state decision event, zero otherwise.
|
2012-07-29 20:31:30 +08:00
|
|
|
*/
|
2013-07-08 03:27:32 +08:00
|
|
|
int clock_manage(struct clock *c, struct port *p, struct ptp_message *msg);
|
2012-07-29 20:31:30 +08:00
|
|
|
|
2014-05-07 00:41:46 +08:00
|
|
|
/**
|
|
|
|
* Send notification about an event to all subscribers.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @param msg The PTP message to send, in network byte order.
|
|
|
|
* @param msglen The length of the message in bytes.
|
|
|
|
* @param event The event that occured.
|
|
|
|
*/
|
|
|
|
void clock_send_notification(struct clock *c, struct ptp_message *msg,
|
|
|
|
int msglen, enum notification event);
|
|
|
|
|
2014-05-07 00:41:49 +08:00
|
|
|
/**
|
|
|
|
* Construct and send notification to subscribers about an event that
|
|
|
|
* occured on the clock.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @param event The identification of the event.
|
|
|
|
*/
|
|
|
|
void clock_notify_event(struct clock *c, enum notification event);
|
|
|
|
|
2011-12-27 20:36:17 +08:00
|
|
|
/**
|
|
|
|
* Obtain a clock's parent data set.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return A pointer to the parent data set of the clock.
|
|
|
|
*/
|
2012-12-01 05:35:26 +08:00
|
|
|
struct parent_ds *clock_parent_ds(struct clock *c);
|
2011-12-27 20:36:17 +08:00
|
|
|
|
2011-11-13 15:41:05 +08:00
|
|
|
/**
|
|
|
|
* Obtain the parent port identity from a clock's parent data set.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return The parent port identity.
|
|
|
|
*/
|
|
|
|
struct PortIdentity clock_parent_identity(struct clock *c);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide a data point to estimate the path delay.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @param req The transmission time of the delay request message.
|
|
|
|
* @param rx The reception time of the delay request message,
|
2015-03-26 23:32:12 +08:00
|
|
|
* as reported in the delay response message, including
|
|
|
|
* correction.
|
2011-11-13 15:41:05 +08:00
|
|
|
*/
|
2015-03-26 23:32:12 +08:00
|
|
|
void clock_path_delay(struct clock *c, tmv_t req, tmv_t rx);
|
2011-11-13 15:41:05 +08:00
|
|
|
|
2012-04-05 20:04:15 +08:00
|
|
|
/**
|
|
|
|
* Provide the estimated peer delay from a slave port.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @param ppd The peer delay as measured on a slave port.
|
2015-03-26 23:32:13 +08:00
|
|
|
* @param req The transmission time of the pdelay request message.
|
|
|
|
* @param rx The reception time of the pdelay request message.
|
2012-08-31 21:49:19 +08:00
|
|
|
* @param nrr The neighbor rate ratio as measured on a slave port.
|
2012-04-05 20:04:15 +08:00
|
|
|
*/
|
2015-03-26 23:32:13 +08:00
|
|
|
void clock_peer_delay(struct clock *c, tmv_t ppd, tmv_t req, tmv_t rx,
|
|
|
|
double nrr);
|
2012-04-05 20:04:15 +08:00
|
|
|
|
2017-07-10 15:39:29 +08:00
|
|
|
/**
|
|
|
|
* Set clock sde
|
|
|
|
* @param c A pointer to a clock instance obtained with clock_create().
|
|
|
|
* @param sde Pass one (1) if need a decision event and zero if not.
|
|
|
|
*/
|
|
|
|
void clock_set_sde(struct clock *c, int sde);
|
|
|
|
|
2011-11-13 00:41:20 +08:00
|
|
|
/**
|
|
|
|
* Poll for events and dispatch them.
|
|
|
|
* @param c A pointer to a clock instance obtained with clock_create().
|
|
|
|
* @return Zero on success, non-zero otherwise.
|
|
|
|
*/
|
|
|
|
int clock_poll(struct clock *c);
|
|
|
|
|
2011-11-13 19:39:49 +08:00
|
|
|
/**
|
|
|
|
* Obtain the slave-only flag from a clock's default data set.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return The value of the clock's slave-only flag.
|
|
|
|
*/
|
|
|
|
int clock_slave_only(struct clock *c);
|
|
|
|
|
2011-12-27 18:28:09 +08:00
|
|
|
/**
|
|
|
|
* Obtain the steps removed field from a clock's current data set.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return The value of the clock's steps removed field.
|
|
|
|
*/
|
|
|
|
UInteger16 clock_steps_removed(struct clock *c);
|
|
|
|
|
2014-12-17 22:24:02 +08:00
|
|
|
/**
|
|
|
|
* Switch to a new PTP Hardware Clock, for use with the "jbod" mode.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @param phc_index The index of the PHC device to use.
|
|
|
|
* @return Zero on success, non-zero otherwise.
|
|
|
|
*/
|
|
|
|
int clock_switch_phc(struct clock *c, int phc_index);
|
|
|
|
|
2011-11-13 15:41:05 +08:00
|
|
|
/**
|
|
|
|
* Provide a data point to synchronize the clock.
|
|
|
|
* @param c The clock instance to synchronize.
|
2015-03-26 23:32:12 +08:00
|
|
|
* @param ingress The ingress time stamp on the sync message.
|
|
|
|
* @param origin The reported transmission time of the sync message,
|
|
|
|
including any corrections.
|
2011-11-13 15:41:05 +08:00
|
|
|
* @param correction1 The correction field of the sync message.
|
|
|
|
* @param correction2 The correction field of the follow up message.
|
|
|
|
* Pass zero in the case of one step operation.
|
2011-12-18 18:02:26 +08:00
|
|
|
* @return The state of the clock's servo.
|
2011-11-13 15:41:05 +08:00
|
|
|
*/
|
2015-03-26 23:32:12 +08:00
|
|
|
enum servo_state clock_synchronize(struct clock *c, tmv_t ingress,
|
|
|
|
tmv_t origin);
|
2011-12-18 18:02:26 +08:00
|
|
|
|
2012-08-22 02:04:45 +08:00
|
|
|
/**
|
|
|
|
* Inform a slaved clock about the master's sync interval.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @param n The logarithm base two of the sync interval.
|
|
|
|
*/
|
|
|
|
void clock_sync_interval(struct clock *c, int n);
|
|
|
|
|
2011-12-27 20:36:17 +08:00
|
|
|
/**
|
|
|
|
* Obtain a clock's time properties data set.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return A pointer to the time properties data set of the clock.
|
|
|
|
*/
|
|
|
|
struct timePropertiesDS *clock_time_properties(struct clock *c);
|
|
|
|
|
2013-04-03 01:25:29 +08:00
|
|
|
/**
|
|
|
|
* Update a clock's time properties data set.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @param tds The new time properties data set for the clock.
|
|
|
|
*/
|
|
|
|
void clock_update_time_properties(struct clock *c, struct timePropertiesDS tds);
|
|
|
|
|
2013-02-04 06:01:51 +08:00
|
|
|
/**
|
|
|
|
* Obtain a clock's description.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return A pointer to the clock_description of the clock.
|
|
|
|
*/
|
|
|
|
struct clock_description *clock_description(struct clock *c);
|
|
|
|
|
2013-02-25 07:56:37 +08:00
|
|
|
/**
|
2015-10-03 17:27:02 +08:00
|
|
|
* Obtain the type of a clock.
|
2013-02-25 07:56:37 +08:00
|
|
|
* @param c The clock instance.
|
2015-10-03 17:27:02 +08:00
|
|
|
* @return One of the @ref clock_type enumeration values.
|
2013-02-25 07:56:37 +08:00
|
|
|
*/
|
2015-10-03 17:27:02 +08:00
|
|
|
enum clock_type clock_type(struct clock *c);
|
2013-02-25 07:56:37 +08:00
|
|
|
|
2013-10-23 00:57:15 +08:00
|
|
|
/**
|
|
|
|
* Perform a sanity check on a time stamp made by a clock.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @param ts The time stamp.
|
|
|
|
*/
|
|
|
|
void clock_check_ts(struct clock *c, struct timespec ts);
|
|
|
|
|
2014-03-14 01:34:19 +08:00
|
|
|
/**
|
|
|
|
* Obtain ratio between master's frequency and current clock frequency.
|
|
|
|
* @param c The clock instance.
|
|
|
|
* @return The rate ratio, 1.0 is returned when not known.
|
|
|
|
*/
|
|
|
|
double clock_rate_ratio(struct clock *c);
|
|
|
|
|
2011-11-05 02:20:23 +08:00
|
|
|
#endif
|