pt1588-SH7216 1
IEEE1588v2 Implementation for Renesas SH7216 Demo

C:/Users/mzimmer/IEEE1588/pt1588/branches/pt1588-SH7216/pt1588/ptp-protocol.c File Reference

Main operation of the IEEE 1588 Precision Time Protocol (PTP). More...

#include "ptp-protocol.h"
#include <stdio.h>

Go to the source code of this file.

Functions

void PTPActor (const Event *eventIn)
 Handle input events by calling the corresponding event handling functions.
void PTPInitialize ()
 Use the specifications in 8.2 and the values specified in ptp-config.h to initialize the data sets in PTPState ptp.
void computeMeanPathDelay ()
 Calculate the mean path delay using the Delay_Resp message and the latest Sync (or Follow_Up) message.
void computeOffsetFromMaster ()
 Calculate the offset from master using the difference in timestamps for the egress and ingress sync events and the current value of mean path delay.
void adjustClock ()
 How the clock is adjusted depends on the amount of adjustment required.
void generateAnnounceReceiptTimeout ()
 Create an event of type 'EVENT_ANNOUNCE_RECEIPT_TIMEOUT_EXPIRES' with a timestamp of 'current time + announce interval'.
void generateQualificationTimeout (UInteger8 N)
 Create an event of type 'EVENT_QUALIFICATION_TIMEOUT_EXPIRES' with a timestamp of 'current time + N * announce interval'.
void eventMsgIn (const Event *eventIn)
 Handle PTP messages by calling the corresponding message handling function.
void eventPowerup ()
 Perform state transition as specified in 9.2.5.
void eventInitialize ()
 Perform state transition as specified in 9.2.5.
void eventDesignatedEnabled ()
 Perform state transition as specified in 9.2.5.
void eventDesignatedDisabled ()
 Perform state transition as specified in 9.2.5.
void eventFaultCleared ()
 Perform state transition as specified in 9.2.5.
void eventFaultDetected ()
 Perform state transition as specified in 9.2.5.
void eventStateDecisionEvent ()
 Run the Best master clock (BMC) algorithm as specified in 9.3.
void eventQualificationTimeoutExpires ()
 Perform state transition as specified in 9.2.5.
void eventAnnounceReceiptTimeoutExpires (const Event *eventIn)
 If the event is still relevent, perform state transition as specified in 9.2.5.
void eventSynchronizationFault ()
 Perform state transition as specified in 9.2.5.
void eventMasterClockSelected ()
 Perform state transition as specified in 9.2.5.
void eventPeriodicAnnounce ()
 Create an event to fire PTPActor() at 'announce interval' in the future, and trigger the BMC algorithm.
void eventPeriodicSync ()
 Create an event to fire the PTPActor at 'sync interval' in the future.
void eventDelayReq ()
 Send a Delay_Resp message.
void eventFollowUp ()
 Send a Follow_Up message.
void stateInitializing ()
 Initialize data sets, stop AnnounceReceiptTimeout(9.2.6.11) and transition to LISTENING state.
void stateFaulty ()
 Stop AnnounceReceiptTimeout (9.2.6.11).
void stateDisabled ()
 Stop AnnounceReceiptTimeout (9.2.6.11).
void stateListening ()
 Restart AnnounceReceiptTimeout (9.2.6.11c).
void statePreMaster ()
 Stop AnnounceReceiptTimeout (9.2.6.11) and start QualificationTimeout.
void stateMaster ()
 Stop AnnounceReceiptTimeout (9.2.6.11) and start periodically sending sync messages.
void statePassive ()
 Restart AnnounceReceiptTimeout (9.2.6.11c).
void stateUncalibrated ()
 Restart AnnounceReceiptTimeout (9.2.6.11c).
void stateSlave ()
 Restart AnnounceReceiptTimeout (9.2.6.11c).
void announceMsgIn (UInteger8 *msg)
 Process the Announce message as specified in 9.5.3.
void syncMsgIn (UInteger8 *msg)
 Process the Sync message as specified in 9.5.4.
void followUpMsgIn (UInteger8 *msg)
 Process the Follow_Up message as specified in 9.5.5.
void delayReqMsgIn (UInteger8 *msg)
 Process the Delay_Req message as specified in 9.5.6.
void delayRespMsgIn (UInteger8 *msg)
 Process the Delay_Resp message as specified in 9.5.6.
void managementMsgIn (UInteger8 *msg)
 Process the Management message as specified in 15.
void toDataSetFromMsg (ClockDataSet *ds, AnnounceMsg *msg)
 Fill the ClockSetSet ds common data set with pointers to values in the Announce message (9.3.4).
void toDataSetFromLocal (ClockDataSet *ds)
 Fill the ClockSetSet ds common data set with pointers to values in the local data sets (9.3.4).
UInteger8 compareDataSet (const ClockDataSet *a, const ClockDataSet *b)
 Use the data comparison algorithm specified in 9.3.4 to compare two data sets, and return result of comparison.
void updateDataSet (UInteger8 code, const AnnounceMsg *msg)
 Update data sets as specified in 9.3.5.
Boolean comparePortIdentity (const PortIdentity *a, const PortIdentity *b)
 If the PortIdentity.clockIdentity and PortIdentity.portNumber of two PortIdentities are both equal, then the PortIdentities are equal.
Boolean isQualified (const ForeignMasterDS *d)
 Check if an Announce message is qualified as specified in 9.3.2.5.
void fixTimestamps (const TimeInterval *timeInterval)
 Fix the timestamps of when Announce messages were received in the foreignMasterDS records.
void printStatus ()
 Print the state of PTP along with offset from master (in SLAVE state) to LCD display.

Variables

PTPState ptp
 Store state for PTP.
UInteger16 zero = 0
 Zero.

Detailed Description

Main operation of the IEEE 1588 Precision Time Protocol (PTP).

See ptp-protocol.h for more information.

Author:
Michael Zimmer (mzimmer@eecs.berkeley.edu)
Version:
Id:
ptp-protocol.c 51 2011-04-12 01:19:19Z mzimmer

Definition in file ptp-protocol.c.


Function Documentation

void adjustClock ( )

How the clock is adjusted depends on the amount of adjustment required.

Adjust slave clock frequency to synchronize to master clock.

If the offset from master is outside the cutoff, the clock is stepped since it would take too long to syncrhonize with frequency adjustment. If it is within the cutoff, a rateAdjFreq is calcuated to attempt to make the frequency of the slave clock equal to that of the master. Even if the frequencies are equal, an offset may still exist (even though it wouldn't change if the frequencies are equal). To account for this, a temporary rateAdjOffset is also calculated to attempt to reduce the offset by the next time adjustClock() is called. There is a PI controller for adjusting rateAdjFreq and a PI controller for adjusting rateAdjOffset. This code can be modified to change the servo algorithm.

Definition at line 268 of file ptp-protocol.c.

void announceMsgIn ( UInteger8 msg)

Process the Announce message as specified in 9.5.3.

React to a received Announce message (9.5.3).

Definition at line 1016 of file ptp-protocol.c.

UInteger8 compareDataSet ( const ClockDataSet a,
const ClockDataSet b 
)

Use the data comparison algorithm specified in 9.3.4 to compare two data sets, and return result of comparison.

Compare two data sets to find which is "better".

Definition at line 1351 of file ptp-protocol.c.

Boolean comparePortIdentity ( const PortIdentity a,
const PortIdentity b 
)

If the PortIdentity.clockIdentity and PortIdentity.portNumber of two PortIdentities are both equal, then the PortIdentities are equal.

Check if two ports are equal.

Definition at line 1505 of file ptp-protocol.c.

void computeMeanPathDelay ( )

Calculate the mean path delay using the Delay_Resp message and the latest Sync (or Follow_Up) message.

Compute CurrentDS.meanPathDelay.

Since clock adjustment is performed after a Sync message and clock adjustment affects the mean path delay calculation (by affecting the timestamps of messages), take those changes into account. This code can be modified to add filtering to deal with the variability of mean path delay.

Definition at line 203 of file ptp-protocol.c.

void computeOffsetFromMaster ( )

Calculate the offset from master using the difference in timestamps for the egress and ingress sync events and the current value of mean path delay.

Compute CurrentDS.offsetFromMaster.

Definition at line 233 of file ptp-protocol.c.

void delayReqMsgIn ( UInteger8 msg)

Process the Delay_Req message as specified in 9.5.6.

React to a received Delay_Req message (9.5.6).

Definition at line 1227 of file ptp-protocol.c.

void delayRespMsgIn ( UInteger8 msg)

Process the Delay_Resp message as specified in 9.5.6.

React to a received Delay_Resp message (9.5.7).

Definition at line 1255 of file ptp-protocol.c.

void eventAnnounceReceiptTimeoutExpires ( const Event eventIn)

If the event is still relevent, perform state transition as specified in 9.2.5.

Handle an EVENT_ANNOUNCE_RECEIPT_TIMEOUT_EXPIRES event (9.2.5, 9.2.6.11).

Definition at line 745 of file ptp-protocol.c.

void eventDelayReq ( )

Send a Delay_Resp message.

Handle an EVENT_DELAY_REQ event.

Definition at line 887 of file ptp-protocol.c.

void eventDesignatedDisabled ( )

Perform state transition as specified in 9.2.5.

Handle an EVENT_DESIGNATED_DISABLED event (9.2.5, 9.2.6.5).

Definition at line 484 of file ptp-protocol.c.

void eventDesignatedEnabled ( )

Perform state transition as specified in 9.2.5.

Handle an EVENT_DESIGNATED_ENABLED event (9.2.5, 9.2.6.4).

Definition at line 474 of file ptp-protocol.c.

void eventFaultCleared ( )

Perform state transition as specified in 9.2.5.

Handle an EVENT_FAULT_CLEARED event (9.2.5, 9.2.6.6).

Definition at line 502 of file ptp-protocol.c.

void eventFaultDetected ( )

Perform state transition as specified in 9.2.5.

Handle an EVENT_FAULT_DETECTED event (9.2.5, 9.2.6.7).

Definition at line 513 of file ptp-protocol.c.

void eventFollowUp ( )

Send a Follow_Up message.

Handle an EVENT_FOLLOW_UP event.

Definition at line 895 of file ptp-protocol.c.

void eventInitialize ( )

Perform state transition as specified in 9.2.5.

Handle an EVENT_INITIALIZE event (9.2.5, 9.2.6.3).

Definition at line 465 of file ptp-protocol.c.

void eventMasterClockSelected ( )

Perform state transition as specified in 9.2.5.

Handle an EVENT_MASTER_CLOCK_SELECTED event (9.2.5, 9.2.6.13).

Definition at line 800 of file ptp-protocol.c.

void eventMsgIn ( const Event eventIn)

Handle PTP messages by calling the corresponding message handling function.

Handle an incoming PTP message.

Definition at line 426 of file ptp-protocol.c.

void eventPeriodicAnnounce ( )

Create an event to fire PTPActor() at 'announce interval' in the future, and trigger the BMC algorithm.

Handle an EVENT_PERIODIC_ANNOUNCE event.

If master, will also send announce message.

Definition at line 815 of file ptp-protocol.c.

void eventPeriodicSync ( )

Create an event to fire the PTPActor at 'sync interval' in the future.

Handle an EVENT_PERIODIC_SYNC event.

If master, also send a sync message (and follow_up in 2 step mode).

Definition at line 843 of file ptp-protocol.c.

void eventPowerup ( )

Perform state transition as specified in 9.2.5.

Handle an EVENT_POWERUP event (9.2.5, 9.2.6.2).

Definition at line 456 of file ptp-protocol.c.

void eventQualificationTimeoutExpires ( )

Perform state transition as specified in 9.2.5.

Handle an EVENT_QUALIFICATION_TIMEOUT_EXPIRES event (9.2.5, 9.2.6.10).

Definition at line 730 of file ptp-protocol.c.

void eventStateDecisionEvent ( )

Run the Best master clock (BMC) algorithm as specified in 9.3.

Handle an EVENT_STATE_DECISION_EVENT event (9.2.5, 9.2.6.8).

Depending on the result of the BMC algorithm, perform state transition as specified in 9.2.5.

Definition at line 534 of file ptp-protocol.c.

void eventSynchronizationFault ( )

Perform state transition as specified in 9.2.5.

Handle an EVENT_SYNCHRONIZATION_FAULT event (9.2.5, 9.2.6.12).

Definition at line 787 of file ptp-protocol.c.

void fixTimestamps ( const TimeInterval timeInterval)

Fix the timestamps of when Announce messages were received in the foreignMasterDS records.

Fix timestamps when clock is stepped.

Definition at line 1550 of file ptp-protocol.c.

void followUpMsgIn ( UInteger8 msg)

Process the Follow_Up message as specified in 9.5.5.

React to a received Follow_Up message (9.5.5).

Definition at line 1180 of file ptp-protocol.c.

void generateAnnounceReceiptTimeout ( )

Create an event of type 'EVENT_ANNOUNCE_RECEIPT_TIMEOUT_EXPIRES' with a timestamp of 'current time + announce interval'.

Create an event to fire PTPActor() at a time 'announce interval' in the future.

Call addFutureEvent() with this event to have this event fire PTPActor() when current time reaches that of the timestamp.

Definition at line 373 of file ptp-protocol.c.

void generateQualificationTimeout ( UInteger8  N)

Create an event of type 'EVENT_QUALIFICATION_TIMEOUT_EXPIRES' with a timestamp of 'current time + N * announce interval'.

Create an event to fire PTPActor() at a time 'N * announce interval' in the future.

Call addFutureEvent() with this event to have this event fire PTPActor() when current time reaches that of the timestamp.

Definition at line 407 of file ptp-protocol.c.

Boolean isQualified ( const ForeignMasterDS d)

Check if an Announce message is qualified as specified in 9.3.2.5.

Check qualification of Announce message (9.3.2.5).

Definition at line 1516 of file ptp-protocol.c.

void managementMsgIn ( UInteger8 msg)

Process the Management message as specified in 15.

React to a recieved Management message (15).

Definition at line 1299 of file ptp-protocol.c.

void printStatus ( )

Print the state of PTP along with offset from master (in SLAVE state) to LCD display.

Print status to LCD screen.

Definition at line 1570 of file ptp-protocol.c.

void PTPActor ( const Event eventIn)

Handle input events by calling the corresponding event handling functions.

The PTPActor() function is responsible for implementing the IEEE 1588 PTP.

Definition at line 40 of file ptp-protocol.c.

void PTPInitialize ( )

Use the specifications in 8.2 and the values specified in ptp-config.h to initialize the data sets in PTPState ptp.

Initialize the data sets in the PTPState ptp as specified in 8.2.

Definition at line 104 of file ptp-protocol.c.

void stateDisabled ( )

Stop AnnounceReceiptTimeout (9.2.6.11).

Perform actions required when entering the DISABLED state.

Definition at line 936 of file ptp-protocol.c.

void stateFaulty ( )

Stop AnnounceReceiptTimeout (9.2.6.11).

Perform actions required when entering the FAULTY state.

Definition at line 926 of file ptp-protocol.c.

void stateInitializing ( )

Initialize data sets, stop AnnounceReceiptTimeout(9.2.6.11) and transition to LISTENING state.

Perform actions required when entering the INITIALIZING state.

Definition at line 908 of file ptp-protocol.c.

void stateListening ( )

Restart AnnounceReceiptTimeout (9.2.6.11c).

Perform actions required when entering the LISTENING state.

Definition at line 946 of file ptp-protocol.c.

void stateMaster ( )

Stop AnnounceReceiptTimeout (9.2.6.11) and start periodically sending sync messages.

Perform actions required when entering the MASTER state.

Definition at line 971 of file ptp-protocol.c.

void statePassive ( )

Restart AnnounceReceiptTimeout (9.2.6.11c).

Perform actions required when entering the PASSIVE state.

Definition at line 984 of file ptp-protocol.c.

void statePreMaster ( )

Stop AnnounceReceiptTimeout (9.2.6.11) and start QualificationTimeout.

Perform actions required when entering the PRE_MASTER state.

Definition at line 956 of file ptp-protocol.c.

void stateSlave ( )

Restart AnnounceReceiptTimeout (9.2.6.11c).

Perform actions required when entering the SLAVE state.

Definition at line 1006 of file ptp-protocol.c.

void stateUncalibrated ( )

Restart AnnounceReceiptTimeout (9.2.6.11c).

Perform actions required when entering the UNCALIBRATED state.

Definition at line 994 of file ptp-protocol.c.

void syncMsgIn ( UInteger8 msg)

Process the Sync message as specified in 9.5.4.

React to a received Sync message (9.5.4).

Definition at line 1112 of file ptp-protocol.c.

void toDataSetFromLocal ( ClockDataSet ds)

Fill the ClockSetSet ds common data set with pointers to values in the local data sets (9.3.4).

Convert local clock information to common data structure for comparison (9.3.4).

Definition at line 1331 of file ptp-protocol.c.

void toDataSetFromMsg ( ClockDataSet ds,
AnnounceMsg msg 
)

Fill the ClockSetSet ds common data set with pointers to values in the Announce message (9.3.4).

Convert message information to common data structure for comparison (9.3.4).

Definition at line 1312 of file ptp-protocol.c.

void updateDataSet ( UInteger8  code,
const AnnounceMsg msg 
)

Update data sets as specified in 9.3.5.

Update the data sets according to state decision code (9.3.5).

Definition at line 1426 of file ptp-protocol.c.


Variable Documentation

Store state for PTP.

Definition at line 34 of file ptp-protocol.c.

Zero.

Definition at line 36 of file ptp-protocol.c.

 All Data Structures Files Functions Variables Typedefs Enumerator Defines