gPTP Documentation
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
avbts_clock.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 
3  Copyright (c) 2009-2012, Intel Corporation
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8 
9  1. Redistributions of source code must retain the above copyright notice,
10  this list of conditions and the following disclaimer.
11 
12  2. Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15 
16  3. Neither the name of the Intel Corporation nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  POSSIBILITY OF SUCH DAMAGE.
31 
32 ******************************************************************************/
33 
34 #ifndef AVBTS_CLOCK_HPP
35 #define AVBTS_CLOCK_HPP
36 
37 #include <stdint.h>
38 #include <ieee1588.hpp>
39 #include <avbts_port.hpp>
40 #include <avbts_ostimerq.hpp>
41 #include <avbts_osipc.hpp>
42 
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
58 struct ClockQuality {
59  unsigned char cq_class;
62  unsigned char clockAccuracy;
72 };
73 
78 private:
79  ClockIdentity clock_identity;
80  ClockQuality clock_quality;
81  unsigned char priority1;
82  unsigned char priority2;
83  bool initializable;
84  bool is_boundary_clock;
85  bool two_step_clock;
86  unsigned char domain_number;
87  uint16_t number_ports;
88  uint16_t number_foreign_records;
89  bool slave_only;
90  int16_t current_utc_offset;
91  bool leap_59;
92  bool leap_61;
93  uint16_t epoch_number;
94  uint16_t steps_removed;
95  int64_t offset_from_master;
96  Timestamp one_way_delay;
97  PortIdentity parent_identity;
98  ClockIdentity grandmaster_clock_identity;
99  ClockQuality grandmaster_clock_quality;
100  unsigned char grandmaster_priority1;
101  unsigned char grandmaster_priority2;
102  bool grandmaster_is_boundary_clock;
103  uint8_t time_source;
104 
105  ClockIdentity LastEBestIdentity;
106  bool _syntonize;
107  bool _new_syntonization_set_point;
108  float _ppm;
109 
110  IEEE1588Port *port_list[MAX_PORTS];
111 
112  static Timestamp start_time;
113  Timestamp last_sync_time;
114 
115  bool _master_local_freq_offset_init;
116  Timestamp _prev_master_time;
117  Timestamp _prev_sync_time;
118 
119  bool _local_system_freq_offset_init;
120  Timestamp _prev_local_time;
121  Timestamp _prev_system_time;
122 
123  HWTimestamper *_timestamper;
124 
125  OS_IPC *ipc;
126 
127  OSTimerQueue *timerq;
128 
129  bool forceOrdinarySlave;
130  FrequencyRatio _master_local_freq_offset;
131  FrequencyRatio _local_system_freq_offset;
132 
133  OSLock *timerq_lock;
134 public:
146  (bool forceOrdinarySlave, bool syntonize, uint8_t priority1,
147  HWTimestamper *timestamper, OSTimerQueueFactory * timerq_factory,
148  OS_IPC * ipc, OSLockFactory *lock_factory );
149 
150  /*
151  * Destroys the IEEE 1588 clock entity
152  */
153  ~IEEE1588Clock(void);
154 
161  bool serializeState( void *buf, long *count );
162 
169  bool restoreSerializedState( void *buf, long *count );
170 
175  Timestamp getTime(void);
176 
182 
188  bool isBetterThan(PTPMessageAnnounce * msg);
189 
195  return LastEBestIdentity;
196  }
197 
204  LastEBestIdentity = id;
205  return;
206  }
207 
213  void setClockIdentity(char *id) {
214  clock_identity.set((uint8_t *) id);
215  }
216 
225  clock_identity.set(addr);
226  }
227 
232  unsigned char getDomain(void) {
233  return domain_number;
234  }
235 
241  return grandmaster_clock_identity;
242  }
243 
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;
253  }
254  }
255 
261  return grandmaster_clock_quality;
262  }
263 
269  void setGrandmasterClockQuality( ClockQuality clock_quality ) {
270  grandmaster_clock_quality = clock_quality;
271  }
272 
278  return clock_quality;
279  }
280 
285  unsigned char getGrandmasterPriority1(void) {
286  return grandmaster_priority1;
287  }
288 
293  unsigned char getGrandmasterPriority2(void) {
294  return grandmaster_priority2;
295  }
296 
302  void setGrandmasterPriority1( unsigned char priority1 ) {
303  grandmaster_priority1 = priority1;
304  }
305 
311  void setGrandmasterPriority2( unsigned char priority2 ) {
312  grandmaster_priority2 = priority2;
313  }
314 
319  uint16_t getMasterStepsRemoved(void) {
320  return steps_removed;
321  }
322 
327  uint16_t getCurrentUtcOffset(void) {
328  return current_utc_offset;
329  }
330 
335  uint8_t getTimeSource(void) {
336  return time_source;
337  }
338 
343  unsigned char getPriority1(void) {
344  return priority1;
345  }
346 
351  unsigned char getPriority2(void) {
352  return priority2;
353  }
354 
360  uint16_t getNextPortId(void) {
361  return (number_ports++ % (MAX_PORTS + 1)) + 1;
362  }
363 
370  void registerPort(IEEE1588Port * port, uint16_t index) {
371  if (index < MAX_PORTS) {
372  port_list[index - 1] = port;
373  }
374  ++number_ports;
375  }
376 
383  void getPortList(int &count, IEEE1588Port ** &ports) {
384  ports = this->port_list;
385  count = number_ports;
386  return;
387  }
388 
393  static Timestamp getSystemTime(void);
394 
401  void addEventTimer
402  ( IEEE1588Port * target, Event e, unsigned long long time_ns );
403 
410  void deleteEventTimer(IEEE1588Port * target, Event e);
411 
420  ( IEEE1588Port * target, Event e, unsigned long long time_ns );
421 
428  void deleteEventTimerLocked(IEEE1588Port * target, Event e);
429 
437  ( Timestamp master_time, Timestamp sync_time );
438 
446  ( Timestamp local_time, Timestamp system_time );
447 
460  void setMasterOffset
461  ( int64_t master_local_offset, Timestamp local_time,
462  FrequencyRatio master_local_freq_offset,
463  int64_t local_system_offset,
464  Timestamp system_time,
465  FrequencyRatio local_system_freq_offset,
466  unsigned sync_count, unsigned pdelay_count, PortState port_state );
467 
473  return clock_identity;
474  }
475 
481  _new_syntonization_set_point = true;
482  }
483 
488  int getTxLockAll() {
489  int number_ports, i, j = 0;
490  IEEE1588Port **ports;
491 
492  getPortList( number_ports, ports );
493 
494  for( i = 0; i < number_ports; ++i ) {
495  while( ports[j] == NULL ) ++j;
496  if( ports[j]->getTxLock() == false ) {
497  return false;
498  }
499  }
500 
501  return true;
502  }
503 
508  int putTxLockAll() {
509  int number_ports, i, j = 0;
510  IEEE1588Port **ports;
511 
512  getPortList( number_ports, ports );
513 
514  for( i = 0; i < number_ports; ++i ) {
515  while( ports[j] == NULL ) ++j;
516  if( ports[j]->putTxLock() == false ) {
517  return false;
518  }
519  }
520 
521  return true;
522  }
523 
524 
530  friend void tick_handler(int sig);
531 
537  return timerq_lock->lock();
538  }
539 
545  return timerq_lock->unlock();
546  }
547 
553  return timerq_lock;
554  }
555 };
556 
557 void tick_handler(int sig);
558 
559 #endif
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