Blame clock.h

Packit 9c3e7e
/**
Packit 9c3e7e
 * @file clock.h
Packit 9c3e7e
 * @brief Implements a PTP clock.
Packit 9c3e7e
 * @note Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
Packit 9c3e7e
 *
Packit 9c3e7e
 * This program is free software; you can redistribute it and/or modify
Packit 9c3e7e
 * it under the terms of the GNU General Public License as published by
Packit 9c3e7e
 * the Free Software Foundation; either version 2 of the License, or
Packit 9c3e7e
 * (at your option) any later version.
Packit 9c3e7e
 *
Packit 9c3e7e
 * This program is distributed in the hope that it will be useful,
Packit 9c3e7e
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 9c3e7e
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 9c3e7e
 * GNU General Public License for more details.
Packit 9c3e7e
 *
Packit 9c3e7e
 * You should have received a copy of the GNU General Public License along
Packit 9c3e7e
 * with this program; if not, write to the Free Software Foundation, Inc.,
Packit 9c3e7e
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Packit 9c3e7e
 */
Packit 9c3e7e
#ifndef HAVE_CLOCK_H
Packit 9c3e7e
#define HAVE_CLOCK_H
Packit 9c3e7e
Packit 9c3e7e
#include "dm.h"
Packit 9c3e7e
#include "ds.h"
Packit 9c3e7e
#include "config.h"
Packit 9c3e7e
#include "notification.h"
Packit 9c3e7e
#include "servo.h"
Packit 9c3e7e
#include "tlv.h"
Packit 9c3e7e
#include "tmv.h"
Packit 9c3e7e
#include "transport.h"
Packit 9c3e7e
Packit 9c3e7e
struct ptp_message; /*forward declaration*/
Packit 9c3e7e
Packit 9c3e7e
/** Opaque type. */
Packit 9c3e7e
struct clock;
Packit 9c3e7e
Packit 9c3e7e
enum clock_type {
Packit 9c3e7e
	CLOCK_TYPE_ORDINARY   = 0x8000,
Packit 9c3e7e
	CLOCK_TYPE_BOUNDARY   = 0x4000,
Packit 9c3e7e
	CLOCK_TYPE_P2P        = 0x2000,
Packit 9c3e7e
	CLOCK_TYPE_E2E        = 0x1000,
Packit 9c3e7e
	CLOCK_TYPE_MANAGEMENT = 0x0800,
Packit 9c3e7e
};
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtains a reference to the best foreign master of a clock.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   A pointer to the data set of the foreign master,
Packit 9c3e7e
 *           or NULL if none has been yet discovered.
Packit 9c3e7e
 */
Packit 9c3e7e
struct dataset *clock_best_foreign(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtains a reference to the port with the best foreign master.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   A pointer to the port with the best foreign master,
Packit 9c3e7e
 *           or NULL if none has been yet discovered.
Packit 9c3e7e
 */
Packit 9c3e7e
struct port *clock_best_port(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtain the clockClass attribute from a clock.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   The value of the clock's class.
Packit 9c3e7e
 */
Packit 9c3e7e
UInteger8 clock_class(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtains a reference to the configuration database.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   A pointer to the configuration, without fail.
Packit 9c3e7e
 */
Packit 9c3e7e
struct config *clock_config(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtains a reference to the current dataset.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   A pointer to the current dataset, without fail.
Packit 9c3e7e
 */
Packit 9c3e7e
struct currentDS *clock_current_dataset(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtains the clock's data set comparison function.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   A pointer to the data set comparison function, without fail.
Packit 9c3e7e
 */
Packit 9c3e7e
int (*clock_dscmp(struct clock *c))(struct dataset *a, struct dataset *b);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtains the required time stamping mode.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   The value of required time stamping mode, which is a bit mask
Packit 9c3e7e
 *           of SOF_TIMESTAMPING_ flags.
Packit 9c3e7e
 */
Packit 9c3e7e
int clock_required_modes(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Create a clock instance. There can only be one clock in any system,
Packit 9c3e7e
 * so subsequent calls will destroy the previous clock instance.
Packit 9c3e7e
 *
Packit 9c3e7e
 * @param type         Specifies which type of clock to create.
Packit 9c3e7e
 * @param config       Pointer to the configuration database.
Packit 9c3e7e
 * @param phc_device   PTP hardware clock device to use. Pass NULL for automatic
Packit 9c3e7e
 *                     selection based on the network interface.
Packit 9c3e7e
 * @return             A pointer to the single global clock instance.
Packit 9c3e7e
 */
Packit 9c3e7e
struct clock *clock_create(enum clock_type type, struct config *config,
Packit 9c3e7e
			   const char *phc_device);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtains a clock's default data set.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   A pointer to the data set of the clock.
Packit 9c3e7e
 */
Packit 9c3e7e
struct dataset *clock_default_ds(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Free all of the resources associated with a clock.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 */
Packit 9c3e7e
void clock_destroy(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtain the domain number from a clock's default data set.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   The PTP domain number.
Packit 9c3e7e
 */
Packit 9c3e7e
UInteger8 clock_domain_number(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtains a reference to the first port in the clock's list.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   A pointer to a port, or NULL if no ports are present.
Packit 9c3e7e
 */
Packit 9c3e7e
struct port *clock_first_port(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Provide the follow_up info TLV from a slave port.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @param f  Pointer to the TLV.
Packit 9c3e7e
 */
Packit 9c3e7e
void clock_follow_up_info(struct clock *c, struct follow_up_info_tlv *f);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Determine if a clock is free running or not.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   One if the clock is free running or zero otherwise.
Packit 9c3e7e
 */
Packit 9c3e7e
int clock_free_running(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtain the gmCapable flag from a clock's default data set.
Packit 9c3e7e
 * This function is specific to the 802.1AS standard.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return One if the clock is capable of becoming grand master, zero otherwise.
Packit 9c3e7e
 */
Packit 9c3e7e
int clock_gm_capable(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtain a clock's identity from its default data set.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   The clock's identity.
Packit 9c3e7e
 */
Packit 9c3e7e
struct ClockIdentity clock_identity(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Informs clock that a file descriptor of one of its ports changed. The
Packit 9c3e7e
 * clock will rebuild its array of file descriptors to poll.
Packit 9c3e7e
 * @param c    The clock instance.
Packit 9c3e7e
 */
Packit 9c3e7e
void clock_fda_changed(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtains the time of the latest synchronization.
Packit 9c3e7e
 * @param c    The clock instance.
Packit 9c3e7e
 * @return     The local time stamp of the last received Sync message.
Packit 9c3e7e
 */
Packit 9c3e7e
tmv_t clock_ingress_time(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Manage the clock according to a given message.
Packit 9c3e7e
 * @param c    The clock instance.
Packit 9c3e7e
 * @param p    The port on which the message arrived.
Packit 9c3e7e
 * @param msg  A management message.
Packit 9c3e7e
 * @return     One if the management action caused a change that
Packit 9c3e7e
 *             implies a state decision event, zero otherwise.
Packit 9c3e7e
 */
Packit 9c3e7e
int clock_manage(struct clock *c, struct port *p, struct ptp_message *msg);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Send notification about an event to all subscribers.
Packit 9c3e7e
 * @param c      The clock instance.
Packit 9c3e7e
 * @param msg    The PTP message to send, in network byte order.
Packit 9c3e7e
 * @param event  The event that occured.
Packit 9c3e7e
 */
Packit 9c3e7e
void clock_send_notification(struct clock *c, struct ptp_message *msg,
Packit 9c3e7e
			     enum notification event);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Construct and send notification to subscribers about an event that
Packit 9c3e7e
 * occured on the clock.
Packit 9c3e7e
 * @param c      The clock instance.
Packit 9c3e7e
 * @param event  The identification of the event.
Packit 9c3e7e
 */
Packit 9c3e7e
void clock_notify_event(struct clock *c, enum notification event);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtain a clock's parent data set.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   A pointer to the parent data set of the clock.
Packit 9c3e7e
 */
Packit 9c3e7e
struct parent_ds *clock_parent_ds(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtain the parent port identity from a clock's parent data set.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   The parent port identity.
Packit 9c3e7e
 */
Packit 9c3e7e
struct PortIdentity clock_parent_identity(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Provide a data point to estimate the path delay.
Packit 9c3e7e
 * @param c           The clock instance.
Packit 9c3e7e
 * @param req         The transmission time of the delay request message.
Packit 9c3e7e
 * @param rx          The reception time of the delay request message,
Packit 9c3e7e
 *                    as reported in the delay response message, including
Packit 9c3e7e
 *                    correction.
Packit 9c3e7e
 */
Packit 9c3e7e
void clock_path_delay(struct clock *c, tmv_t req, tmv_t rx);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Provide the estimated peer delay from a slave port.
Packit 9c3e7e
 * @param c           The clock instance.
Packit 9c3e7e
 * @param ppd         The peer delay as measured on a slave port.
Packit 9c3e7e
 * @param req         The transmission time of the pdelay request message.
Packit 9c3e7e
 * @param rx          The reception time of the pdelay request message.
Packit 9c3e7e
 * @param nrr         The neighbor rate ratio as measured on a slave port.
Packit 9c3e7e
 */
Packit 9c3e7e
void clock_peer_delay(struct clock *c, tmv_t ppd, tmv_t req, tmv_t rx,
Packit 9c3e7e
		      double nrr);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Set clock sde
Packit 9c3e7e
 * @param c     A pointer to a clock instance obtained with clock_create().
Packit 9c3e7e
 * @param sde   Pass one (1) if need a decision event and zero if not.
Packit 9c3e7e
 */
Packit 9c3e7e
void clock_set_sde(struct clock *c, int sde);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Poll for events and dispatch them.
Packit 9c3e7e
 * @param c A pointer to a clock instance obtained with clock_create().
Packit 9c3e7e
 * @return  Zero on success, non-zero otherwise.
Packit 9c3e7e
 */
Packit 9c3e7e
int clock_poll(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtain the slave-only flag from a clock's default data set.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   The value of the clock's slave-only flag.
Packit 9c3e7e
 */
Packit 9c3e7e
int clock_slave_only(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtain the steps removed field from a clock's current data set.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   The value of the clock's steps removed field.
Packit 9c3e7e
 */
Packit 9c3e7e
UInteger16 clock_steps_removed(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Switch to a new PTP Hardware Clock, for use with the "jbod" mode.
Packit 9c3e7e
 * @param c          The clock instance.
Packit 9c3e7e
 * @param phc_index  The index of the PHC device to use.
Packit 9c3e7e
 * @return           Zero on success, non-zero otherwise.
Packit 9c3e7e
 */
Packit 9c3e7e
int clock_switch_phc(struct clock *c, int phc_index);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Provide a data point to synchronize the clock.
Packit 9c3e7e
 * @param c            The clock instance to synchronize.
Packit 9c3e7e
 * @param ingress      The ingress time stamp on the sync message.
Packit 9c3e7e
 * @param origin       The reported transmission time of the sync message,
Packit 9c3e7e
                       including any corrections.
Packit 9c3e7e
 * @param correction1  The correction field of the sync message.
Packit 9c3e7e
 * @param correction2  The correction field of the follow up message.
Packit 9c3e7e
 *                     Pass zero in the case of one step operation.
Packit 9c3e7e
 * @return             The state of the clock's servo.
Packit 9c3e7e
 */
Packit 9c3e7e
enum servo_state clock_synchronize(struct clock *c, tmv_t ingress,
Packit 9c3e7e
				   tmv_t origin);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Inform a slaved clock about the master's sync interval.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @param n  The logarithm base two of the sync interval.
Packit 9c3e7e
 */
Packit 9c3e7e
void clock_sync_interval(struct clock *c, int n);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtain a clock's time properties data set.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   A pointer to the time properties data set of the clock.
Packit 9c3e7e
 */
Packit 9c3e7e
struct timePropertiesDS *clock_time_properties(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Update a clock's time properties data set.
Packit 9c3e7e
 * @param c   The clock instance.
Packit 9c3e7e
 * @param tds The new time properties data set for the clock.
Packit 9c3e7e
 */
Packit 9c3e7e
void clock_update_time_properties(struct clock *c, struct timePropertiesDS tds);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtain a clock's description.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   A pointer to the clock_description of the clock.
Packit 9c3e7e
 */
Packit 9c3e7e
struct clock_description *clock_description(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtain the type of a clock.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   One of the @ref clock_type enumeration values.
Packit 9c3e7e
 */
Packit 9c3e7e
enum clock_type clock_type(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Perform a sanity check on a time stamp made by a clock.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @param ts The time stamp.
Packit 9c3e7e
 */
Packit 9c3e7e
void clock_check_ts(struct clock *c, uint64_t ts);
Packit 9c3e7e
Packit 9c3e7e
/**
Packit 9c3e7e
 * Obtain ratio between master's frequency and current clock frequency.
Packit 9c3e7e
 * @param c  The clock instance.
Packit 9c3e7e
 * @return   The rate ratio, 1.0 is returned when not known.
Packit 9c3e7e
 */
Packit 9c3e7e
double clock_rate_ratio(struct clock *c);
Packit 9c3e7e
Packit 9c3e7e
#endif