125 offsetFromMaster = t2->secondsField.secondsHigh;
126 offsetFromMaster -= t1->secondsField.secondsHigh;
127 offsetFromMaster <<= 32;
128 offsetFromMaster += t2->secondsField.secondsLow;
129 offsetFromMaster -= t1->secondsField.secondsLow;
140 if ((offsetFromMaster < -30) || (offsetFromMaster > 30)) {
156 && ((offsetFromMaster > 2000000000)
157 || (-offsetFromMaster > 2000000000))) {
163 ptp_meanDelayAtLastAdjust = ptp_meanPathDelay[portIndex] >> 16;
164 offsetFromMaster -= ptp_meanDelayAtLastAdjust;
166 offsetFromMaster = -offsetFromMaster;
169 ptp_lastAdjust = offsetFromMaster;
207 }
else if (A[i] < B[i]) {
228 ptpClock.defaultDS.numberPorts = 1;
229 portIndex = ptpClock.defaultDS.numberPorts;
233 ptp_foreignMaster[portIndex][0].clockIdentity);
235 ptp_announce[portIndex].grandMasterIdentity);
236 ptpClock.portDS[portIndex].portState = PTP_PS_LISTENING;
286 portNumber = ptpClock.defaultDS.numberPorts;
290 &(ptp_announce[iEbest - 1]),
291 &(ptp_announce[portNumber - 1]), iEbest, portNumber);
296 ptp_announce[iEbest - 1u].grandMasterIdentity,
PTP_ZEROSEQ64)
301 portNumber = ptpClock.defaultDS.numberPorts;
303 portIndex = portNumber - 1;
305 ptp_announce[portIndex].grandMasterIdentity,
PTP_ZEROSEQ64) == 0)
311 &(ptp_announce[portIndex]), portNumber);
312 if (ptpClock.defaultDS.clockQuality.clockClass < 128) {
320 else if (portNumber == iEbest)
324 &(ptp_announce[iEbest - 1]),
325 &(ptp_announce[portIndex]), iEbest, portNumber);
347 portIndex = ptpClock.defaultDS.numberPorts;
355 ptpClock.currentDS.stepsRemoved = 0;
356 ptpClock.currentDS.offsetFromMaster.scalatedNanoseconds = 0;
357 ptpClock.currentDS.meanPathDelay.scalatedNanoseconds = 0;
358 ptpClock.parentDS.parentPortIdentity.clockIdentity =
359 ptpClock.defaultDS.clockIdentity;
360 ptpClock.parentDS.parentPortIdentity.portNumber = 0;
361 ptpClock.parentDS.grandMasterIdentity =
362 ptpClock.defaultDS.clockIdentity;
363 ptpClock.parentDS.grandMasterclockQuality.clockAccuracy =
364 ptpClock.defaultDS.clockQuality.clockAccuracy;
365 ptpClock.parentDS.grandMasterclockQuality.clockClass =
366 ptpClock.defaultDS.clockQuality.clockClass;
367 ptpClock.parentDS.grandMasterclockQuality.offsetScaledLogVariance =
368 ptpClock.defaultDS.clockQuality.offsetScaledLogVariance;
369 ptpClock.parentDS.grandMasterPriority1 =
370 ptpClock.defaultDS.priority1;
371 ptpClock.parentDS.grandMasterPriority2 =
372 ptpClock.defaultDS.priority2;
373 ptpClock.timePropertiesDS.currentUtcOffset =
375 ptpClock.timePropertiesDS.currentUtcOffsetValid = PTP_FALSE;
376 ptpClock.timePropertiesDS.leap59 = PTP_FALSE;
377 ptpClock.timePropertiesDS.leap61 = PTP_FALSE;
378 ptpClock.timePropertiesDS.timeTraceable = PTP_FALSE;
379 ptpClock.timePropertiesDS.frequencyTraceable = PTP_FALSE;
380 ptpClock.timePropertiesDS.ptpTimescale = PTP_FALSE;
397 ptpClock.currentDS.stepsRemoved =
398 ptp_announce[portIndex].stepsRemoved + 1;
399 ptpClock.parentDS.parentPortIdentity.clockIdentity =
400 &(ptp_foreignMaster[portIndex][0].clockIdentity);
401 ptpClock.parentDS.parentPortIdentity.portNumber =
403 ptpClock.parentDS.grandMasterIdentity =
404 &(ptp_announce[portIndex].grandMasterIdentity);
405 ptpClock.parentDS.grandMasterclockQuality.clockAccuracy =
406 ptp_announce[portIndex].grandMasterClockQuality.clockAccuracy;
407 ptpClock.parentDS.grandMasterclockQuality.clockClass =
408 ptp_announce[portIndex].grandMasterClockQuality.clockClass;
409 ptpClock.parentDS.grandMasterclockQuality.offsetScaledLogVariance =
410 ptp_announce[portIndex].grandMasterClockQuality.offsetScaledLogVariance;
411 ptpClock.parentDS.grandMasterPriority1 =
412 ptp_announce[portIndex].grandMasterPriority1;
413 ptpClock.parentDS.grandMasterPriority2 =
414 ptp_announce[portIndex].grandMasterPriority1;
415 ptpClock.timePropertiesDS.currentUtcOffset =
416 ptp_announce[portIndex].currentUtcOffset;
417 ptpClock.timePropertiesDS.currentUtcOffsetValid =
418 ptp_announce[portIndex].currentUtcOffsetValid;
419 ptpClock.timePropertiesDS.leap59 =
420 ptp_announce[portIndex].leap59;
421 ptpClock.timePropertiesDS.leap61 =
422 ptp_announce[portIndex].leap61;
423 ptpClock.timePropertiesDS.timeTraceable =
424 ptp_announce[portIndex].timeTraceable;
425 ptpClock.timePropertiesDS.frequencyTraceable =
426 ptp_announce[portIndex].frequencyTraceable;
427 ptpClock.timePropertiesDS.ptpTimescale =
428 ptp_announce[portIndex].ptpTimescale;
429 ptpClock.timePropertiesDS.timeSource =
430 ptp_announce[portIndex].timeSource;
447 i = ptpClock.defaultDS.numberPorts;
454 if (ptpClock.portDS[i].portState == PTP_PS_LISTENING
455 || ptpClock.portDS[i].portState == PTP_PS_UNCALIBRATED
456 || ptpClock.portDS[i].portState == PTP_PS_SLAVE
457 || ptpClock.portDS[i].portState == PTP_PS_PASSIVE) {
458 ptpClock.portDS[i].portState = PTP_PS_PRE_MASTER;
462 if (ptpClock.portDS[i].portState == PTP_PS_LISTENING
463 || ptpClock.portDS[i].portState == PTP_PS_UNCALIBRATED
464 || ptpClock.portDS[i].portState == PTP_PS_PRE_MASTER
465 || ptpClock.portDS[i].portState == PTP_PS_MASTER
466 || ptpClock.portDS[i].portState == PTP_PS_PASSIVE) {
467 ptpClock.portDS[i].portState = PTP_PS_UNCALIBRATED;
472 if (ptpClock.portDS[i].portState == PTP_PS_LISTENING
473 || ptpClock.portDS[i].portState == PTP_PS_UNCALIBRATED
474 || ptpClock.portDS[i].portState == PTP_PS_SLAVE
475 || ptpClock.portDS[i].portState == PTP_PS_PRE_MASTER
476 || ptpClock.portDS[i].portState == PTP_PS_MASTER) {
477 ptpClock.portDS[i].portState = PTP_PS_PASSIVE;
504 *byTopology = PTP_FALSE;
515 if ((i1 == 0) && (i2 == 0))
520 }
else if (i2 == 0) {
527 E2->grandMasterIdentity) == 0) {
529 if (E1->stepsRemoved > (E2->stepsRemoved + 1)) {
532 }
else if ((E1->stepsRemoved + 1) < E2->stepsRemoved) {
537 if (E1->stepsRemoved > E2->stepsRemoved) {
539 *(ptpClock.defaultDS.clockIdentity),
540 ptp_foreignMaster[i1 - 1u][0].clockIdentity)) {
548 *byTopology = PTP_TRUE;
555 }
else if (E1->stepsRemoved < E2->stepsRemoved) {
557 *(ptpClock.defaultDS.clockIdentity),
558 ptp_foreignMaster[i2 - 1u][0].clockIdentity)) {
566 *byTopology = PTP_TRUE;
576 ptp_foreignMaster[i1 - 1u][0].clockIdentity,
577 ptp_foreignMaster[i2 - 1u][0].clockIdentity)) {
580 *byTopology = PTP_TRUE;
584 *byTopology = PTP_TRUE;
593 *byTopology = PTP_TRUE;
595 }
else if (i1 < i2) {
598 *byTopology = PTP_TRUE;
604 if (E1->grandMasterPriority1 > E2->grandMasterPriority1) {
607 }
else if (E1->grandMasterPriority1 < E2->grandMasterPriority1) {
612 if (E1->grandMasterClockQuality.clockClass
613 > E2->grandMasterClockQuality.clockClass) {
616 }
else if (E1->grandMasterClockQuality.clockClass
617 < E2->grandMasterClockQuality.clockClass) {
622 if (E1->grandMasterClockQuality.clockAccuracy
623 > E2->grandMasterClockQuality.clockAccuracy) {
626 }
else if (E1->grandMasterClockQuality.clockAccuracy
627 < E2->grandMasterClockQuality.clockAccuracy) {
632 if (E1->grandMasterClockQuality.offsetScaledLogVariance
633 > E2->grandMasterClockQuality.offsetScaledLogVariance) {
636 }
else if (E1->grandMasterClockQuality.offsetScaledLogVariance
637 < E2->grandMasterClockQuality.offsetScaledLogVariance) {
642 if (E1->grandMasterPriority2 > E2->grandMasterPriority2) {
645 }
else if (E1->grandMasterPriority2 < E2->grandMasterPriority2) {
651 E2->grandMasterIdentity)) {
693 E->grandMasterIdentity) == 0) {
700 if (1 < E->stepsRemoved) {
704 portIndex = portNumber - 1;
722 if (0 != E->stepsRemoved) {
724 ptp_foreignMaster[portIndex][0].clockIdentity)) {
741 ptp_foreignMaster[portIndex][0].clockIdentity)) {
744 *Dbetter = PTP_FALSE;
761 if (D->defaultDS.priority1 > E->grandMasterPriority1) {
762 *Dbetter = PTP_FALSE;
764 }
else if (D->defaultDS.priority1 < E->grandMasterPriority1) {
769 if (D->defaultDS.clockQuality.clockClass
770 > E->grandMasterClockQuality.clockClass) {
771 *Dbetter = PTP_FALSE;
773 }
else if (D->defaultDS.clockQuality.clockClass
774 < E->grandMasterClockQuality.clockClass) {
779 if (D->defaultDS.clockQuality.clockAccuracy
780 > E->grandMasterClockQuality.clockAccuracy) {
781 *Dbetter = PTP_FALSE;
783 }
else if (D->defaultDS.clockQuality.clockAccuracy
784 < E->grandMasterClockQuality.clockAccuracy) {
789 if (D->defaultDS.clockQuality.offsetScaledLogVariance
790 > E->grandMasterClockQuality.offsetScaledLogVariance) {
791 *Dbetter = PTP_FALSE;
793 }
else if (D->defaultDS.clockQuality.offsetScaledLogVariance
794 < E->grandMasterClockQuality.offsetScaledLogVariance) {
799 if (D->defaultDS.priority2 > E->grandMasterPriority2) {
800 *Dbetter = PTP_FALSE;
802 }
else if (D->defaultDS.priority2 < E->grandMasterPriority2) {
808 E->grandMasterIdentity)) {
810 *Dbetter = PTP_FALSE;
836 portIndex = portNumber - 1;
842 && !(~ptp_foreignMaster[portIndex][0].foreignMasterAnnounceMessages)) {
844 ptp_foreignMaster[portIndex][0].foreignMasterAnnounceMessages++;
852 ptp_foreignMaster[portIndex][0].clockIdentity);
853 ptp_foreignMaster[portIndex][0].foreignMasterAnnounceMessages = 0;
857 ptp_announce[portIndex].grandMasterPriority1 =
ptp_rcvUInt8(&(pBuf[47]));
858 ptp_rcvOctets(8, &(pBuf[53]), ptp_announce[portIndex].grandMasterIdentity);
859 ptp_announce[portIndex].grandMasterClockQuality.clockClass =
ptp_rcvUInt8(
861 ptp_announce[portIndex].grandMasterClockQuality.clockAccuracy =
863 ptp_announce[portIndex].grandMasterClockQuality.offsetScaledLogVariance =
865 ptp_announce[portIndex].grandMasterPriority2 =
ptp_rcvUInt8(&(pBuf[52]));
866 ptp_announce[portIndex].stepsRemoved =
ptp_rcvUInt16(&(pBuf[61]));
867 ptp_announce[portIndex].currentUtcOffset =
ptp_rcvUInt16(&(pBuf[44]));
868 ptp_announce[portIndex].currentUtcOffsetValid = (pBuf[6] >> 2) & 0x01;
869 ptp_announce[portIndex].leap59 = (pBuf[6] >> 1) & 0x01;
870 ptp_announce[portIndex].leap61 = pBuf[6] & 0x01;
871 ptp_announce[portIndex].timeTraceable = (pBuf[6] >> 4) & 0x01;
872 ptp_announce[portIndex].frequencyTraceable = (pBuf[6] >> 5) & 0x01;
873 ptp_announce[portIndex].ptpTimescale = (pBuf[6] >> 3) & 0x01;
1045 ((
char *) &r)[0] = pBuf[0];
1046 ((
char *) &r)[1] = pBuf[1];
1061 ((
char *) &r)[0] = pBuf[0];
1062 ((
char *) &r)[1] = pBuf[1];
1063 ((
char *) &r)[2] = pBuf[2];
1064 ((
char *) &r)[3] = pBuf[3];
1079 ((
char *) &r)[0] = pBuf[0];
1080 ((
char *) &r)[1] = pBuf[1];
1081 ((
char *) &r)[2] = pBuf[2];
1082 ((
char *) &r)[3] = pBuf[3];
1083 ((
char *) &r)[4] = pBuf[4];
1084 ((
char *) &r)[5] = pBuf[5];
1085 ((
char *) &r)[6] = 0;
1086 ((
char *) &r)[7] = 0;
1099 ((
char *) &(p->secondsLow))[0] = pBuf[0];
1100 ((
char *) &(p->secondsLow))[1] = pBuf[1];
1101 ((
char *) &(p->secondsLow))[2] = pBuf[2];
1102 ((
char *) &(p->secondsLow))[3] = pBuf[3];
1103 ((
char *) &(p->secondsHigh))[0] = pBuf[4];
1104 ((
char *) &(p->secondsHigh))[1] = pBuf[5];
1118 ((
char *) &r)[0] = pBuf[0];
1119 ((
char *) &r)[1] = pBuf[1];
1120 ((
char *) &r)[2] = pBuf[2];
1121 ((
char *) &r)[3] = pBuf[3];
1122 ((
char *) &r)[4] = pBuf[4];
1123 ((
char *) &r)[5] = pBuf[5];
1124 ((
char *) &r)[6] = pBuf[6];
1125 ((
char *) &r)[7] = pBuf[7];
1140 ((
char *) &r)[0] = pBuf[0];
1141 ((
char *) &r)[1] = pBuf[1];
1142 ((
char *) &r)[2] = pBuf[2];
1143 ((
char *) &r)[3] = pBuf[3];
1144 ((
char *) &r)[4] = pBuf[4];
1145 ((
char *) &r)[5] = pBuf[5];
1146 ((
char *) &r)[6] = pBuf[6];
1147 ((
char *) &r)[7] = pBuf[7];
1180 *dest++ = *source++;
ptp_uinteger64 ptp_rcvUInt64(ptp_octet *pBuf)
Receive unsigned 64 bits integer.
Network functions interface.
uint64_t * nwk_getPIeeeAddress()
Get pointer to the IEEE address.
unsigned char ptp_boolean
ptp_uinteger8 ptp_sndInt64(ptp_integer64 *p)
Send 64 bits integer.
UInt8 nwk_sndInt64(int64_t *p)
Send signed 64 bits integer.
void clock_adjTimeValues(int64_t *adjValue, clock_timeStamp *time)
Adjust time values.
ptp_uinteger8 ptp_startFrame(ptp_uinteger16 destination, ptp_uinteger8 len)
Start Frame.
ptp_uinteger8 ptp_sndUInt32(ptp_uinteger32 *p)
Send unsigned 32 bits integer.
ptp_uinteger8 ptp_rcvUInt8(ptp_octet *pBuf)
Receive unsigned 8 bits integer.
UInt8 nwk_sndUInt64(uint64_t *p)
Send unsigned 64 bits integer.
ptp_uinteger8 ptp_snd(ptp_uinteger8 len, void *pBuf)
Send data.
ptp_uinteger8 ptp_sndUInt16(ptp_uinteger16 v)
Send unsigned 16 bits integer.
UInt8 nwk_getNeighborForeignPort(UInt8 portNumber)
Get neighbor foreign port.
void ptp_rcvOctets(ptp_uinteger8 len, ptp_octet *pBuf, ptp_octet *dest)
Receive octets.
ptp_integer8 ptp_dataSetComparisonDE(ptp_boolean *Dbetter, ptp_clock *D, PTP_announceDS *E, ptp_uinteger8 portNumber)
Data set comparison self clock/announce message.
ptp_uinteger8 ptp_sndUInt8(ptp_uinteger8 v)
Send unsigned 8 bits integer.
ptp_uinteger8 ptp_sndUInt48(ptp_uinteger48 *p)
Send unsigned 48 bits integer.
UInt8 nwk_sndUInt48(uint64_t *p)
Send unsigned 48 bits integer.
unsigned long long ptp_uinteger64
ptp_integer8 ptp_clockIdentityCompare(ptp_clockIdentity A, ptp_clockIdentity B)
Clock identity compare.
Master on clockClass 1 ~ 127 and grandMaster.
void ptp_adjTime(PTP_TimeStamp *t1, PTP_TimeStamp *t2, ptp_uinteger8 portIndex)
Adjust the clock.
Master on clockClass > 128 and grandMaster.
void ptp_copyOctets(ptp_uinteger8 len, ptp_octet *source, ptp_octet *dest)
Copy octets.
void ptp_bmc()
Best master clock.
PTP_announceDS ptp_announce[PTP_NUMBERPORTS]
Last announce message received for each port.
UInt8 nwk_sndUInt32(UInt32 *p)
Send unsigned 32 bits integer.
unsigned long ptp_uinteger32
ptp_clockIdentity * ptp_getClockIdentity()
Get clock identity.
UInt8 nwk_sndUInt16(UInt16 v)
Send unsigned 16 bits integer.
#define NWK_CMDIDPTP
Command id for PTP messages.
When port have no connection, keep listening state.
#define PTP_DESIGNTIME_UTCOFFSET
UInt8 nwk_sndUInt8(UInt8 v)
Send unsigned 8 bits integer.
PTP functions specific to the implementation.
void ptp_state_decision_event()
State decision event.
void ptp_storeForeignMaster(ptp_octet *pBuf, ptp_uinteger8 portNumber)
Store foreign master.
int64_t clock_timeDiff(clock_timeStamp *t1, clock_timeStamp *t2)
Time difference.
void ptp_portUpdateStatus()
Port update status.
clockClass > 128 and is PASSIVE to break a timing loop
ptp_octet ptp_clockIdentity[8]
unsigned char ptp_uinteger8
nwk_devType nwk_getDeviceType()
This function gets the device type.
unsigned long long ptp_uinteger48
void ptp_portsSetListening()
Ports set listening.
ptp_recommendedState ptp_portRecommendedState[PTP_NUMBERPORTS]
Recommended portState set by BMC (Best Master Clock) Algorithm.
ptp_uinteger8 ptp_sndUInt64(ptp_uinteger64 *p)
Send unsigned 64 bits integer.
UInt8 nwk_startFrame(UInt16 destination, UInt16 cmdId, UInt8 radius, UInt8 len)
Start Frame.
ptp_integer64 ptp_timeDiff(PTP_TimeStamp *t1, PTP_TimeStamp *t2)
Time difference.
void ptp_rcvUInt48sc(ptp_octet *pBuf, PTP_SecondsCount *p)
Receive seconds count value.
UInt8 nwk_snd(UInt8 len, void *pBuf)
Send data.
void ptp_portUpdateDS()
Port update data set.
void ptp_getCurrentTime(PTP_TimeStamp *dest)
Get cuttent time.
Master and NOT grandMaster.
PTP constants that should not need to be modified.
ptp_integer64 ptp_rcvInt64(ptp_octet *pBuf)
Receive 64 bits integer.
ptp_uinteger8 ptp_sndUInt48sc(PTP_SecondsCount *p)
Send a seconds count value.
void nwk_endFrame(UInt8 forwardToSerial)
End frame.
ptp_uinteger48 ptp_rcvUInt48(ptp_octet *pBuf)
Receive unsigned 48 bits integer.
PTP data types specifications.
PTP configuration values to compile.
ptp_integer8 ptp_dataSetComparisonEE(ptp_uinteger8 *iBetter, ptp_boolean *byTopology, PTP_announceDS *E1, PTP_announceDS *E2, ptp_uinteger8 i1, ptp_uinteger8 i2)
Data set comparison.
clockClass 1 ~ 127 and NOT grandMaster or is PASSIVE to break a timing loop
UInt16 nwk_getNeighborAddress(UInt8 portNumber)
Get neighbor address.
void clock_setTime(clock_timeStamp *sourceTime)
Set time.
ptp_uinteger16 ptp_rcvUInt16(ptp_octet *pBuf)
Receive unsigned 16 bits integer.
void clock_adjTime(int64_t *adjValue)
Adjust time.
unsigned short ptp_uinteger16
void clock_getTime(clock_timeStamp *destTime)
Get time.
ptp_uinteger32 ptp_rcvUInt32(ptp_octet *pBuf)
Receive unsigned 32 bits integer.
void ptp_endFrame()
End frame.