summaryrefslogtreecommitdiff
path: root/daemons/gptp/common/avbts_clock.hpp
blob: 22a72464aeaea00ea0a4dde9a0394fa9e140e2e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/******************************************************************************

  Copyright (c) 2009-2012, Intel Corporation
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

   3. Neither the name of the Intel Corporation nor the names of its
      contributors may be used to endorse or promote products derived from
      this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.

******************************************************************************/

#ifndef AVBTS_CLOCK_HPP
#define AVBTS_CLOCK_HPP

#include <stdint.h>
#include <ieee1588.hpp>
#include <common_port.hpp>
#include <avbts_ostimerq.hpp>
#include <avbts_osipc.hpp>

/**@file*/

#define EVENT_TIMER_GRANULARITY 5000000		/*!< Event timer granularity*/

/* These 4 macros are used only when Syntonize mode is enabled */
#define INTEGRAL 0.0003				/*!< PI controller integral factor*/
#define PROPORTIONAL 1.0			/*!< PI controller proportional factor*/
#define UPPER_FREQ_LIMIT  250.0		/*!< Upper frequency limit */
#define LOWER_FREQ_LIMIT -250.0		/*!< Lower frequency limit */

#define UPPER_LIMIT_PPM 250
#define LOWER_LIMIT_PPM -250
#define PPM_OFFSET_TO_RATIO(ppm) ((ppm) / ((FrequencyRatio)US_PER_SEC) + 1)

/* This is the threshold in ns for which frequency adjustments will be made */
#define PHASE_ERROR_THRESHOLD (1000000000)

/* This is the maximum count of phase error, outside of the threshold before
   adjustment is performed */
#define PHASE_ERROR_MAX_COUNT (6)

/* Value returned by calcMasterLocalClockRateDifference() to indicate
   detection of negative time jump in follow_up message */
#define NEGATIVE_TIME_JUMP 0.0

/**
 * @brief Provides the clock quality abstraction.
 * Represents the quality of the clock
 * Defined at IEEE 802.1AS-2011
 * Clause 6.3.3.8
 */
struct ClockQuality {
	unsigned char cq_class;				/*!< Clock Class - Clause 8.6.2.2
										  Denotes the tracebility of the synchronized time
										  distributed by a clock master when it is grandmaster. */
	unsigned char clockAccuracy; 		/*!< Clock Accuracy - clause 8.6.2.3.
										  Indicates the expected time accuracy of
										  a clock master.*/
	int16_t offsetScaledLogVariance;	/*!< ::Offset Scaled log variance - Clause 8.6.2.4.
										  Is the scaled, offset representation
										  of an estimate of the PTP variance. The
										  PTP variance characterizes the
										  precision and frequency stability of the clock
										  master. The PTP variance is the square of
										  PTPDEV (See B.1.3.2). */
};

/**
 * @brief Provides the 1588 clock interface
 */
class IEEE1588Clock {
private:
	ClockIdentity clock_identity;
	ClockQuality clock_quality;
	unsigned char priority1;
	unsigned char priority2;
	bool initializable;
	bool is_boundary_clock;
	bool two_step_clock;
	unsigned char domain_number;
	uint16_t number_ports;
	uint16_t number_foreign_records;
	bool slave_only;
	int16_t current_utc_offset;
	bool leap_59;
	bool leap_61;
	uint16_t epoch_number;
	uint16_t steps_removed;
	int64_t offset_from_master;
	Timestamp one_way_delay;
	PortIdentity parent_identity;
	ClockIdentity grandmaster_clock_identity;
	ClockQuality grandmaster_clock_quality;
	unsigned char grandmaster_priority1;
	unsigned char grandmaster_priority2;
	bool grandmaster_is_boundary_clock;
	uint8_t time_source;

	ClockIdentity LastEBestIdentity;
	bool _syntonize;
	bool _new_syntonization_set_point;
	float _ppm;
	int _phase_error_violation;

	CommonPort *port_list[MAX_PORTS];

	static Timestamp start_time;
	Timestamp last_sync_time;

	bool _master_local_freq_offset_init;
	Timestamp _prev_master_time;
	Timestamp _prev_sync_time;

	bool _local_system_freq_offset_init;
	Timestamp _prev_local_time;
	Timestamp _prev_system_time;

	OS_IPC *ipc;

	OSTimerQueue *timerq;

	bool forceOrdinarySlave;
	FrequencyRatio _master_local_freq_offset;
	FrequencyRatio _local_system_freq_offset;

    /**
     * @brief fup info stores information of the last time
     * the base time has changed. When that happens
     * the information from fup_status is copied over
     * fup_info. The follow-up sendPort method is
     * supposed to get the information from fup_info
     * prior to sending a message out.
     */
    FollowUpTLV *fup_info;

    /**
     * @brief fup status has the instantaneous info
     */
    FollowUpTLV *fup_status;

    OSLock *timerq_lock;

public:
	
    /**
	 * @brief  Add a new event to the timer queue
	 * @param  target EtherPort target
	 * @param  e Event to be added
	 * @param  time_ns Time in nanoseconds
	 */
	void addEventTimer
	( CommonPort *target, Event e,
	  unsigned long long time_ns );

	/**
	 * @brief  Deletes an event from the timer queue
	 * @param  target Target port to remove the event from
	 * @param  e Event to be removed
	 * @return void
	 */
	void deleteEventTimer( CommonPort *target, Event e );

  /**
   * @brief Instantiates a IEEE 1588 Clock
   * @param forceOrdinarySlave Forces it to be an ordinary slave
   * @param syntonize if TRUE, clock will syntonize to the master clock
   * @param priority1 It is used in the execution of BCMA. See IEEE 802.1AS-2011 Clause 10.3
   * @param timerq_factory [in] Provides a factory object for creating timer queues (managing events)
   * @param ipc [in] Inter process communication object
   * @param lock_factory [in] Provides a factory object for creating locking a locking mechanism
   */
  IEEE1588Clock
	  (bool forceOrdinarySlave, bool syntonize, uint8_t priority1,
	   OSTimerQueueFactory * timerq_factory, OS_IPC * ipc,
	   OSLockFactory *lock_factory );

  /*
   * Destroys the IEEE 1588 clock entity
   */
  ~IEEE1588Clock(void);

  /**
   * @brief  Updates the frequencyRatio information
   * @param  buf [out] Stores the serialized clock quality state
   * @param  count [inout] Provides the size of buffer. Its decremented internally
   * @return TRUE in case of success, FALSE when the count should be updated with the right size.
   */
  bool serializeState( void *buf, long *count );

  /**
   * @brief  Restores the frequencyRatio with the serialized input buffer data
   * @param  buf [in] serialized frequencyRatio information
   * @param  count [inout] Size of buffer. It is incremented internally
   * @return TRUE in case of success, FALSE otherwise.
   */
  bool restoreSerializedState( void *buf, long *count );

  /**
   * @brief  Gets the current time from system clock
   * @return System time
   */
  Timestamp getTime(void);

  /**
   * @brief  Gets the timestamp from hardware (Deprecated)
   * @return Hardware timestamp
   */
  Timestamp getPreciseTime(void);

  /**
   * @brief  Compares the 1588 Clock to the grandmaster clock
   * @param  msg [in] PTP announce message
   * @return TRUE if the 1588 clock
   */
  bool isBetterThan(PTPMessageAnnounce * msg);

  /**
   * @brief  Gets the Last Best clock identity
   * @return clock identity
   */
  ClockIdentity getLastEBestIdentity( void ) {
	  return LastEBestIdentity;
  }

  /**
   * @brief  Sets the last Best clock identity
   * @param  id ClockIdentity object to be set
   * @return void
   */
  void setLastEBestIdentity( ClockIdentity id ) {
	  LastEBestIdentity = id;
	  return;
  }

  /**
   * @brief  Sets clock identity by id
   * @param  id [id] Clock identity (as an octet array)
   * @return void
   */
  void setClockIdentity(char *id) {
	  clock_identity.set((uint8_t *) id);
  }

  /**
   * @brief  Set clock id based on the link layer address. Clock id is 8 octets
   * long whereas link layer address is 6 octets long and it is turned into a
   * clock identity as per the 802.1AS standard described in clause 8.5.2.2
   * @param  addr Link layer address
   * @return void
   */
  void setClockIdentity(LinkLayerAddress * addr) {
	  clock_identity.set(addr);
  }

  /**
   * @brief  Gets the domain number
   * @return domain number
   */
  unsigned char getDomain(void) {
	  return domain_number;
  }

  /**
   * @brief  Gets grandmaster clock ID
   * @return GM clock ID
   */
  ClockIdentity getGrandmasterClockIdentity(void) {
	  return grandmaster_clock_identity;
  }

  /**
   * @brief  Sets a new GM clock ID
   * @param  id New id
   * @return void
   */
  void setGrandmasterClockIdentity(ClockIdentity id) {
	  if (id != grandmaster_clock_identity) {
		  GPTP_LOG_STATUS("New Grandmaster \"%s\" (previous \"%s\")", id.getIdentityString().c_str(), grandmaster_clock_identity.getIdentityString().c_str());
		  grandmaster_clock_identity = id;
	  }
  }

  /**
   * @brief  Gets grandmaster clock quality object
   * @return Clock quality
   */
  ClockQuality getGrandmasterClockQuality(void) {
	  return grandmaster_clock_quality;
  }

  /**
   * @brief  Sets grandmaster clock quality
   * @param  clock_quality ClockQuality object to be set
   * @return void
   */
  void setGrandmasterClockQuality( ClockQuality clock_quality ) {
	  grandmaster_clock_quality = clock_quality;
  }

  /**
   * @brief  Gets the IEEE 1588 Clock quality
   * @return ClockQuality
   */
  ClockQuality getClockQuality(void) {
	  return clock_quality;
  }

  /**
   * @brief  Gets grandmaster priority1 attribute (IEEE 802.1AS-2011 Clause 10.5.3.2.2)
   * @return Grandmaster priority1
   */
  unsigned char getGrandmasterPriority1(void) {
	  return grandmaster_priority1;
  }

  /**
   * @brief  Gets grandmaster priotity2 attribute (IEEE 802.1AS-2011 Clause 10.5.3.2.4)
   * @return Grandmaster priority2
   */
  unsigned char getGrandmasterPriority2(void) {
	  return grandmaster_priority2;
  }

  /**
   * @brief  Sets grandmaster's priority1 attribute (IEEE 802.1AS-2011 Clause 10.5.3.2.2)
   * @param  priority1 value to be set
   * @return void
   */
  void setGrandmasterPriority1( unsigned char priority1 ) {
	  grandmaster_priority1 = priority1;
  }

  /**
   * @brief  Sets grandmaster's priority2 attribute (IEEE 802.1AS-2011 Clause 10.5.3.2.4)
   * @param  priority2 Value to be set
   * @return void
   */
  void setGrandmasterPriority2( unsigned char priority2 ) {
	  grandmaster_priority2 = priority2;
  }

  /**
   * @brief  Gets master steps removed (IEEE 802.1AS-2011 Clause 10.3.3)
   * @return steps removed value
   */
  uint16_t getMasterStepsRemoved(void) {
	  return steps_removed;
  }

  /**
   * @brief  Gets the currentUtcOffset attribute (IEEE 802.1AS-2011 Clause 10.3.8.9)
   * @return currentUtcOffset
   */
  uint16_t getCurrentUtcOffset(void) {
	  return current_utc_offset;
  }

  /**
   * @brief  Gets the TimeSource attribute (IEEE 802.1AS-2011 clause 10.3.8.10)
   * @return TimeSource
   */
  uint8_t getTimeSource(void) {
	  return time_source;
  }

  /**
   * @brief  Gets IEEE1588Clock priority1 value (IEEE 802.1AS-2011 Clause 8.6.2.1)
   * @return Priority1 value
   */
  unsigned char getPriority1(void) {
	  return priority1;
  }

  /**
   * @brief  Gets IEEE1588Clock priority2 attribute (IEEE 802.1AS-2011 Clause 8.6.2.5)
   * @return Priority2 value
   */
  unsigned char getPriority2(void) {
	  return priority2;
  }

  /**
   * @brief  Gets nextPortId value
   * @return The remaining value from the division of current number of ports by
   * (maximum number of ports + 1) + 1
   */
  uint16_t getNextPortId(void) {
	  return (number_ports++ % (MAX_PORTS + 1)) + 1;
  }

  /**
   * @brief  Sets the follow up info internal object. The fup_info object contains
   * the last updated information when the frequency or phase have changed
   * @param  fup Pointer to the FolloUpTLV object.
   * @return void
   */
  void setFUPInfo(FollowUpTLV *fup)
  {
      fup_info = fup;
  }

  /**
   * @brief  Gets the fup_info pointer
   * @return fup_info pointer
   */
  FollowUpTLV *getFUPInfo(void)
  {
      return fup_info;
  }

  /**
   * @brief  Sets the follow up status internal object. The fup_status object contains
   * information about the current frequency/phase aqcuired through the received
   * follow up messages
   * @param  fup Pointer to the FollowUpTLV object
   * @return void
   */
  void setFUPStatus(FollowUpTLV *fup)
  {
      fup_status = fup;
  }

  /**
   * @brief  Gets the fup_status pointer
   * @return fup_status pointer
   */
  FollowUpTLV *getFUPStatus(void)
  {
      return fup_status;
  }

  /**
   * @brief Updates the follow up info internal object with the current clock source time
   * status values. This method should be called whenever the clockSource entity time
   * base changes (IEEE 802.1AS-2011 Clause 9.2)
   * @return void
   */
  void updateFUPInfo(void)
  {
      fup_info->incrementGMTimeBaseIndicator();
      fup_info->setScaledLastGmFreqChange(fup_status->getScaledLastGmFreqChange());
      fup_info->setScaledLastGmPhaseChange(fup_status->getScaledLastGmPhaseChange());
  }

  /**
   * @brief  Registers a new IEEE1588 port
   * @param  port  [in] IEEE1588port instance
   * @param  index Port's index
   * @return void
   */
	void registerPort( CommonPort *port, uint16_t index )
	{
	  if (index < MAX_PORTS) {
		  port_list[index - 1] = port;
	  }
	  ++number_ports;
	}

  /**
   * @brief  Gets the current port list instance
   * @param  count [out] Number of ports
   * @param  ports [out] Pointer to the port list
   * @return
   */
  void getPortList(int &count, CommonPort ** &ports) {
	  ports = this->port_list;
	  count = number_ports;
	  return;
  }

  /**
   * @brief  Gets current system time
   * @return Instance of a Timestamp object
   */
  static Timestamp getSystemTime(void);

  /**
   * @brief  Adds an event to the timer queue using a lock
   * @param  target EtherPort target
   * @param  e Event to be added
   * @param  time_ns event time in nanoseconds
   * @return void
   */
  void addEventTimerLocked
  ( CommonPort *target, Event e, unsigned long long time_ns );

  /**
   * @brief  Deletes and event from the timer queue using a lock
   * @param  target Target port to remove the event from
   * @param  e Event to be deleted
   * @return
   */
  void deleteEventTimerLocked( CommonPort *target, Event e );

  /**
   * @brief  Calculates the master to local clock rate difference
   * @param  master_time Master time
   * @param  sync_time Local time
   * @return The offset in ppt (parts per trillion)
   */
  FrequencyRatio calcMasterLocalClockRateDifference
	  ( Timestamp master_time, Timestamp sync_time );

  /**
   * @brief  Calculates the local to system clock rate difference
   * @param  local_time Local time
   * @param  system_time System time
   * @return The offset in ppt (parts per trillion)
   */
  FrequencyRatio calcLocalSystemClockRateDifference
	  ( Timestamp local_time, Timestamp system_time );

  /**
   * @brief  Sets the master offset, sintonyze and adjusts the frequency offset
   * @param  master_local_offset Master to local phase offset
   * @param  local_time Local time
   * @param  master_local_freq_offset Master to local frequency offset
   * @param  local_system_offset Local time to system time phase offset
   * @param  system_time System time
   * @param  local_system_freq_offset Local to system frequency offset
   * @param  sync_count Sync messages count
   * @param  pdelay_count PDelay messages count
   * @param  port_state PortState instance
   * @param  asCapable asCapable flag
   */
  void setMasterOffset
  ( CommonPort *port, 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, bool asCapable );

	/**
	 * @brief  Get local:system frequency ratio
	 * @return clock ratio
	 */
	FrequencyRatio getLocalSystemFreqOffset()
	{
		return _local_system_freq_offset;
	}

  /**
   * @brief  Get the IEEE1588Clock identity value
   * @return clock identity
   */
  ClockIdentity getClockIdentity() {
	  return clock_identity;
  }

  /**
   * @brief  Sets a flag that will allow syntonization during setMasterOffset calls
   * @return void
   */
  void newSyntonizationSetPoint() {
	  _new_syntonization_set_point = true;
  }

  /**
   * @brief  Restart PDelays on all ports
   * @return void
   */
  void restartPDelayAll() {
	  int number_ports, i, j = 0;
	  CommonPort **ports;

	  getPortList( number_ports, ports );

	  for( i = 0; i < number_ports; ++i ) {
		  while( ports[j] == NULL ) ++j;
		  ports[j]->restartPDelay();
	  }
  }

  /**
   * @brief  Gets all TX locks
   * @return void
   */
  int getTxLockAll() {
	  int number_ports, i, j = 0;
	  CommonPort **ports;

	  getPortList( number_ports, ports );

	  for( i = 0; i < number_ports; ++i ) {
		  while( ports[j] == NULL ) ++j;
		  if( ports[j]->getTxLock() == false ) {
			  return false;
		  }
	  }

	  return true;
  }

  /**
   * @brief  Release all TX locks
   * @return void
   */
  int putTxLockAll() {
	  int number_ports, i, j = 0;
	  CommonPort **ports;

	  getPortList( number_ports, ports );

	  for( i = 0; i < number_ports; ++i ) {
		  while( ports[j] == NULL ) ++j;
		  if( ports[j]->putTxLock() == false ) {
			  return false;
		  }
	  }

	  return true;
  }


  /**
   * @brief  Declares a friend instance of tick_handler method
   * @param  sig Signal
   * @return void
   */
  friend void tick_handler(int sig);

  /**
   * @brief  Gets the timer queue lock
   * @return OSLockResult structure
   */
  OSLockResult getTimerQLock() {
	  return timerq_lock->lock();
  }

  /**
   * @brief  Releases the timer queue lock
   * @return OSLockResult structure
   */
  OSLockResult putTimerQLock() {
	  return timerq_lock->unlock();
  }

  /**
   * @brief  Gets a pointer to the timer queue lock object
   * @return OSLock instance
   */
  OSLock *timerQLock() {
	  return timerq_lock;
  }
};

void tick_handler(int sig);

#endif