34 #ifndef AVBTS_CLOCK_HPP
35 #define AVBTS_CLOCK_HPP
45 #define EVENT_TIMER_GRANULARITY 5000000
47 #define INTEGRAL 0.0024
48 #define PROPORTIONAL 1.0
49 #define UPPER_FREQ_LIMIT 250.0
50 #define LOWER_FREQ_LIMIT -250.0
81 unsigned char priority1;
82 unsigned char priority2;
84 bool is_boundary_clock;
86 unsigned char domain_number;
87 uint16_t number_ports;
88 uint16_t number_foreign_records;
90 int16_t current_utc_offset;
93 uint16_t epoch_number;
94 uint16_t steps_removed;
95 int64_t offset_from_master;
100 unsigned char grandmaster_priority1;
101 unsigned char grandmaster_priority2;
102 bool grandmaster_is_boundary_clock;
107 bool _new_syntonization_set_point;
115 bool _master_local_freq_offset_init;
119 bool _local_system_freq_offset_init;
129 bool forceOrdinarySlave;
146 (
bool forceOrdinarySlave,
bool syntonize, uint8_t priority1,
195 return LastEBestIdentity;
204 LastEBestIdentity = id;
214 clock_identity.
set((uint8_t *)
id);
225 clock_identity.
set(addr);
233 return domain_number;
241 return grandmaster_clock_identity;
250 if (
id != grandmaster_clock_identity) {
251 fprintf(stderr,
"New Grandmaster \"%s\" (previous \"%s\")\n",
id.getIdentityString().c_str(), grandmaster_clock_identity.
getIdentityString().c_str());
252 grandmaster_clock_identity = id;
261 return grandmaster_clock_quality;
270 grandmaster_clock_quality = clock_quality;
278 return clock_quality;
286 return grandmaster_priority1;
294 return grandmaster_priority2;
303 grandmaster_priority1 = priority1;
312 grandmaster_priority2 = priority2;
320 return steps_removed;
328 return current_utc_offset;
361 return (number_ports++ % (
MAX_PORTS + 1)) + 1;
372 port_list[index - 1] = port;
384 ports = this->port_list;
385 count = number_ports;
461 ( int64_t master_local_offset,
Timestamp local_time,
463 int64_t local_system_offset,
466 unsigned sync_count,
unsigned pdelay_count,
PortState port_state );
473 return clock_identity;
481 _new_syntonization_set_point =
true;
489 int number_ports, i, j = 0;
494 for( i = 0; i < number_ports; ++i ) {
495 while( ports[j] == NULL ) ++j;
496 if( ports[j]->getTxLock() ==
false ) {
509 int number_ports, i, j = 0;
514 for( i = 0; i < number_ports; ++i ) {
515 while( ports[j] == NULL ) ++j;
516 if( ports[j]->putTxLock() ==
false ) {
537 return timerq_lock->
lock();
545 return timerq_lock->
unlock();
void addEventTimer(IEEE1588Port *target, Event e, unsigned long long time_ns)
Add a new event to the timer queue.
IEEE1588Clock(bool forceOrdinarySlave, bool syntonize, uint8_t priority1, HWTimestamper *timestamper, OSTimerQueueFactory *timerq_factory, OS_IPC *ipc, OSLockFactory *lock_factory)
Instantiates a IEEE 1588 Clock.
Definition: ieee1588.hpp:445
Definition: avbts_message.hpp:434
OSLockResult getTimerQLock()
Gets the timer queue lock.
Definition: avbts_clock.hpp:536
void deleteEventTimerLocked(IEEE1588Port *target, Event e)
Deletes and event from the timer queue using a lock.
uint16_t getCurrentUtcOffset(void)
Gets the currentUtcOffset attribute (IEEE 802.1AS clause 10.3.8.9)
Definition: avbts_clock.hpp:327
bool restoreSerializedState(void *buf, long *count)
Restores the frequencyRatio with the serialized input buffer data.
Definition: avbts_ostimerq.hpp:49
Definition: avbts_port.hpp:76
void newSyntonizationSetPoint()
Sets a flag that will allow syntonization during setMasterOffset calls.
Definition: avbts_clock.hpp:480
bool isBetterThan(PTPMessageAnnounce *msg)
Compares the 1588 Clock to the grandmaster clock.
Definition: avbts_clock.hpp:77
Definition: ieee1588.hpp:111
void setGrandmasterPriority2(unsigned char priority2)
Sets grandmaster's priority2 attribute (IEEE 802.1AS clause 10.5.3.2.4)
Definition: avbts_clock.hpp:311
Event
Definition: ieee1588.hpp:74
void setClockIdentity(LinkLayerAddress *addr)
Set clock id based on the link layer address. Clock id is 8 octets long whereas link layer address is...
Definition: avbts_clock.hpp:224
unsigned char getPriority2(void)
Gets IEEE1588Clock priority2 attribute (IEEE 802.1AS clause 8.6.2.5)
Definition: avbts_clock.hpp:351
Timestamp getTime(void)
Gets the current time from system clock.
ClockQuality getGrandmasterClockQuality(void)
Gets grandmaster clock quality object.
Definition: avbts_clock.hpp:260
PortState
Definition: ptptypes.hpp:48
Definition: avbts_clock.hpp:58
void setMasterOffset(int64_t master_local_offset, Timestamp local_time, FrequencyRatio master_local_freq_offset, int64_t local_system_offset, Timestamp system_time, FrequencyRatio local_system_freq_offset, unsigned sync_count, unsigned pdelay_count, PortState port_state)
Sets the master offset, sintonyze and adjusts the frequency offset.
void setGrandmasterPriority1(unsigned char priority1)
Sets grandmaster's priority1 attribute (IEEE 802.1AS clause 10.5.3.2.2)
Definition: avbts_clock.hpp:302
int getTxLockAll()
Gets all TX locks.
Definition: avbts_clock.hpp:488
void deleteEventTimer(IEEE1588Port *target, Event e)
Deletes an event from the timer queue.
Definition: avbts_ostimerq.hpp:91
bool serializeState(void *buf, long *count)
Updates the frequencyRatio information.
FrequencyRatio calcMasterLocalClockRateDifference(Timestamp master_time, Timestamp sync_time)
Calculates the master to local clock rate difference.
Definition: avbts_osnet.hpp:52
ClockIdentity getLastEBestIdentity(void)
Gets the Last Best clock identity.
Definition: avbts_clock.hpp:194
int putTxLockAll()
Release all TX locks.
Definition: avbts_clock.hpp:508
std::string getIdentityString()
Gets the identity string from the ClockIdentity object.
void getPortList(int &count, IEEE1588Port **&ports)
Gets the current port list instance.
Definition: avbts_clock.hpp:383
OSLockResult
Definition: avbts_oslock.hpp:52
static Timestamp getSystemTime(void)
Gets current system time.
FrequencyRatio calcLocalSystemClockRateDifference(Timestamp local_time, Timestamp system_time)
Calculates the local to system clock rate difference.
uint16_t getMasterStepsRemoved(void)
Gets master steps removed (IEEE 802.1AS clause 10.3.3)
Definition: avbts_clock.hpp:319
unsigned char getDomain(void)
Gets the domain number.
Definition: avbts_clock.hpp:232
OSLock * timerQLock()
Gets a pointer to the timer queue lock object.
Definition: avbts_clock.hpp:552
OSLockResult putTimerQLock()
Releases the timer queue lock.
Definition: avbts_clock.hpp:544
uint8_t getTimeSource(void)
Gets the TimeSource attribute (IEEE 802.1AS-2011 clause 10.3.8.10)
Definition: avbts_clock.hpp:335
virtual OSLockResult unlock()=0
Unlocks a critical section.
Definition: avbts_port.hpp:207
#define MAX_PORTS
Definition: ieee1588.hpp:52
void addEventTimerLocked(IEEE1588Port *target, Event e, unsigned long long time_ns)
Adds an event to the timer queue using a lock.
unsigned char getGrandmasterPriority1(void)
Gets grandmaster priority1 attribute (IEEE 802.1AS clause 10.5.3.2.2)
Definition: avbts_clock.hpp:285
ClockIdentity getGrandmasterClockIdentity(void)
Gets grandmaster clock ID.
Definition: avbts_clock.hpp:240
void setGrandmasterClockIdentity(ClockIdentity id)
Sets a new GM clock ID.
Definition: avbts_clock.hpp:249
Timestamp getPreciseTime(void)
Gets the timestamp from hardware (Deprecated)
uint16_t getNextPortId(void)
Gets nextPortId value.
Definition: avbts_clock.hpp:360
Definition: avbts_oslock.hpp:98
unsigned char getPriority1(void)
Gets IEEE1588Clock priority1 value (IEEE 802.1AS clause 8.6.2.1)
Definition: avbts_clock.hpp:343
void registerPort(IEEE1588Port *port, uint16_t index)
Registers a new IEEE1588 port.
Definition: avbts_clock.hpp:370
unsigned char clockAccuracy
Definition: avbts_clock.hpp:62
Definition: avbts_oslock.hpp:57
Definition: avbts_osipc.hpp:56
ClockQuality getClockQuality(void)
Gets the IEEE 1588 Clock quality.
Definition: avbts_clock.hpp:277
long double FrequencyRatio
Definition: ptptypes.hpp:39
void tick_handler(int sig)
unsigned char getGrandmasterPriority2(void)
Gets grandmaster priotity2 attribute (IEEE 802.1AS clause 10.5.3.2.4)
Definition: avbts_clock.hpp:293
void setLastEBestIdentity(ClockIdentity id)
Sets the last Best clock identity.
Definition: avbts_clock.hpp:203
friend void tick_handler(int sig)
Declares a friend instance of tick_handler method.
virtual OSLockResult lock()=0
Locks a critical section.
int16_t offsetScaledLogVariance
Definition: avbts_clock.hpp:65
unsigned char cq_class
Definition: avbts_clock.hpp:59
void setClockIdentity(char *id)
Sets clock identity by id.
Definition: avbts_clock.hpp:213
ClockIdentity getClockIdentity()
Get the IEEE1588Clock identity value.
Definition: avbts_clock.hpp:472
void setGrandmasterClockQuality(ClockQuality clock_quality)
Sets grandmaster clock quality.
Definition: avbts_clock.hpp:269
Definition: ieee1588.hpp:222
void set(uint8_t *id)
Set the clock id to the object.
Definition: ieee1588.hpp:189