Documentation ¶
Overview ¶
Package protocol implements a subset of the PTPv2.1 protocol (IEEE 1588-2019).
Implementation is focused on unicast communications over IPv6 and is sufficient to build unicast PTP server or client.
This package also contains basic management client that can be used to exchange Management Packets with ptp server.
Additionally it has helpers to work with NIC hardware and software timestamps.
All references throughout the code relate to the IEEE 1588-2019 Standard.
Index ¶
- Constants
- Variables
- func Bytes(p Packet) ([]byte, error)
- func BytesTo(p BinaryMarshalerTo, buf []byte) (int, error)
- func ConnFd(conn *net.UDPConn) (int, error)
- func EnableHWTimestampsSocket(connFd int, iface string) error
- func EnableSWTimestampsSocket(connFd int) error
- func FromBytes(rawBytes []byte, p Packet) error
- func IPToSockaddr(ip net.IP, port int) unix.Sockaddr
- func ReadPacketWithRXTimestamp(connFd int) ([]byte, unix.Sockaddr, time.Time, error)
- func ReadPacketWithRXTimestampBuf(connFd int, buf, oob []byte) (int, unix.Sockaddr, time.Time, error)
- func ReadTXtimestamp(connFd int) (time.Time, int, error)
- func ReadTXtimestampBuf(connFd int, oob, toob []byte) (time.Time, int, error)
- func RegisterMgmtTLVDecoder(id ManagementID, decoder MgmtTLVDecoderFunc)
- func SockaddrToIP(sa unix.Sockaddr) net.IP
- type AcknowledgeCancelUnicastTransmissionTLV
- type Action
- type Announce
- type AnnounceBody
- type BinaryMarshalerTo
- type CancelUnicastTransmissionTLV
- type ClockIdentity
- type ClockQuality
- type Correction
- type CurrentDataSetTLV
- type DefaultDataSetTLV
- type DelayResp
- type DelayRespBody
- type FollowUp
- type FollowUpBody
- type GrantUnicastTransmissionTLV
- type Header
- type IntFloat
- type LogInterval
- type Management
- type ManagementErrorID
- type ManagementErrorStatusTLV
- type ManagementID
- type ManagementMsgErrorStatus
- type ManagementMsgHead
- type ManagementTLV
- type ManagementTLVHead
- type MessageType
- type MgmtClient
- func (c *MgmtClient) Communicate(packet *Management) (*Management, error)
- func (c *MgmtClient) CurrentDataSet() (*CurrentDataSetTLV, error)
- func (c *MgmtClient) DefaultDataSet() (*DefaultDataSetTLV, error)
- func (c *MgmtClient) ParentDataSet() (*ParentDataSetTLV, error)
- func (c *MgmtClient) PortStatsNP() (*PortStatsNPTLV, error)
- func (c *MgmtClient) SendPacket(packet *Management) error
- func (c *MgmtClient) TimeStatusNP() (*TimeStatusNPTLV, error)
- type MgmtTLVDecoderFunc
- type PDelayReq
- type PDelayReqBody
- type PDelayResp
- type PDelayRespBody
- type PDelayRespFollowUp
- type PDelayRespFollowUpBody
- type PTPText
- type Packet
- type ParentDataSetTLV
- type PortIdentity
- type PortStats
- type PortStatsNPTLV
- type RequestUnicastTransmissionTLV
- type ScaledNS
- type SdoIDAndMsgType
- type Signaling
- type SyncDelayReq
- type SyncDelayReqBody
- type TLV
- type TLVHead
- type TLVType
- type TimeInterval
- type TimeSource
- type TimeStatusNPTLV
- type Timestamp
- type UnicastMsgTypeAndFlags
Constants ¶
const ( MajorVersion uint8 = 2 MinorVersion uint8 = 1 Version uint8 = MinorVersion<<4 | MajorVersion MajorVersionMask uint8 = 0x0f )
what version of PTP protocol we implement
const ( PortEvent = 319 PortGeneral = 320 )
UDP port numbers
The UDP destination port of a PTP event message shall be 319. The UDP destination port of a multicast PTP general message shall be 320. The UDP destination port of a unicast PTP general message that is addressed to a PTP Instance shall be 320. The UDP destination port of a unicast PTP general message that is addressed to a manager shall be the UDP source port value of the PTP message to which this is a response.
const ( // first octet FlagAlternateMaster uint16 = 1 << (8 + 0) FlagTwoStep uint16 = 1 << (8 + 1) FlagUnicast uint16 = 1 << (8 + 2) FlagProfileSpecific1 uint16 = 1 << (8 + 5) FlagProfileSpecific2 uint16 = 1 << (8 + 6) // second octet FlagLeap61 uint16 = 1 << 0 FlagLeap59 uint16 = 1 << 1 FlagCurrentUtcOffsetValid uint16 = 1 << 2 FlagPTPTimescale uint16 = 1 << 3 FlagTimeTraceable uint16 = 1 << 4 FlagFrequencyTraceable uint16 = 1 << 5 FlagSynchronizationUncertain uint16 = 1 << 6 )
flags used in FlagField as per Table 37 Values of flagField
const ( // Control is a socket control message containing TX/RX timestamp // If the read fails we may endup with multiple timestamps in the buffer // which is best to read right away ControlSizeBytes = 128 // ptp packets usually up to 66 bytes PayloadSizeBytes = 128 )
const ( // HWTIMESTAMP is a hardware timestamp HWTIMESTAMP = "hardware" // SWTIMESTAMP is a software timestmap SWTIMESTAMP = "software" )
Variables ¶
var DefaultTargetPortIdentity = PortIdentity{
ClockIdentity: 0xffffffffffffffff,
PortNumber: 0xffff,
}
DefaultTargetPortIdentity is a port identity that means any port
var ErrManagementMsgErrorStatus = errors.New("received MANAGEMENT_ERROR_STATUS_TLV")
ErrManagementMsgErrorStatus is what happens if we expected to get Management TLV in response, but received special ManagementErrorStatusTLV
var ManagementErrorIDToString = map[ManagementErrorID]string{ ErrorResponseTooBig: "RESPONSE_TOO_BIG", ErrorNoSuchID: "NO_SUCH_ID", ErrorWrongLength: "WRONG_LENGTH", ErrorWrongValue: "WRONG_VALUE", ErrorNotSetable: "NOT_SETABLE", ErrorNotSupported: "NOT_SUPPORTED", ErrorUnpopulated: "UNPOPULATED", ErrorGeneralError: "GENERAL_ERROR", }
ManagementErrorIDToString is a map from ManagementErrorID to string
var MessageTypeToString = map[MessageType]string{ MessageSync: "SYNC", MessageDelayReq: "DELAY_REQ", MessagePDelayReq: "PDELAY_REQ", MessagePDelayResp: "PDELAY_RES", MessageFollowUp: "FOLLOW_UP", MessageDelayResp: "DELAY_RESP", MessagePDelayRespFollowUp: "PDELAY_RESP_FOLLOW_UP", MessageAnnounce: "ANNOUNCE", MessageSignaling: "SIGNALING", MessageManagement: "MANAGEMENT", }
MessageTypeToString is a map from MessageType to string
var TLVTypeToString = map[TLVType]string{ TLVManagement: "MANAGEMENT", TLVManagementErrorStatus: "MANAGEMENT_ERROR_STATUS", TLVOrganizationExtension: "ORGANIZATION_EXTENSION", TLVRequestUnicastTransmission: "REQUEST_UNICAST_TRANSMISSION", TLVGrantUnicastTransmission: "GRANT_UNICAST_TRANSMISSION", TLVCancelUnicastTransmission: "CANCEL_UNICAST_TRANSMISSION", TLVAcknowledgeCancelUnicastTransmission: "ACKNOWLEDGE_CANCEL_UNICAST_TRANSMISSION", TLVPathTrace: "PATH_TRACE", TLVAlternateTimeOffsetIndicator: "ALTERNATE_TIME_OFFSET_INDICATOR", }
TLVTypeToString is a map from TLVType to string
var TimeSourceToString = map[TimeSource]string{ TimeSourceAtomicClock: "ATOMIC_CLOCK", TimeSourceGNSS: "GNSS", TimeSourceTerrestrialRadio: "TERRESTRIAL_RADIO", TimeSourceSerialTimeCode: "SERIAL_TIME_CODE", TimeSourcePTP: "PTP", TimeSourceNTP: "NTP", TimeSourceHandSet: "HAND_SET", TimeSourceOther: "OTHER", TimeSourceInternalOscillator: "INTERNAL_OSCILLATOR", }
TimeSourceToString is a map from TimeSource to string
Functions ¶
func Bytes ¶
Bytes converts any packet to []bytes PTP over UDPv6 requires adding extra two bytes that may be modified by the initiator or an intermediate PTP Instance to ensure that the UDP checksum remains uncompromised after any modification of PTP fields. We simply always add them - in worst case they add extra 2 unused bytes when used over UDPv4.
func BytesTo ¶
func BytesTo(p BinaryMarshalerTo, buf []byte) (int, error)
BytesTo marhals packets that support this optimized marshalling into []byte
func EnableHWTimestampsSocket ¶
EnableHWTimestampsSocket enables HW timestamps on the socket
func EnableSWTimestampsSocket ¶
EnableSWTimestampsSocket enables SW timestamps on the socket
func IPToSockaddr ¶
IPToSockaddr converts IP + port into a socket address Somewhat copy from https://github.com/golang/go/blob/16cd770e0668a410a511680b2ac1412e554bd27b/src/net/ipsock_posix.go#L145
func ReadPacketWithRXTimestamp ¶
ReadPacketWithRXTimestamp returns byte packet and HW RX timestamp
func ReadPacketWithRXTimestampBuf ¶
func ReadPacketWithRXTimestampBuf(connFd int, buf, oob []byte) (int, unix.Sockaddr, time.Time, error)
ReadPacketWithRXTimestampBuf writes byte packet into provide buffer buf, and returns number of bytes copied to the buffer, client ip and HW RX timestamp. oob buffer can be reaused after ReadPacketWithRXTimestampBuf call.
func ReadTXtimestamp ¶
ReadTXtimestamp returns HW TX timestamp
func ReadTXtimestampBuf ¶
ReadTXtimestampBuf returns HW TX timestamp, needs to be provided 2 buffers which all can be re-used after ReadTXtimestampBuf finishes.
func RegisterMgmtTLVDecoder ¶
func RegisterMgmtTLVDecoder(id ManagementID, decoder MgmtTLVDecoderFunc)
RegisterMgmtTLVDecoder registers function we'll use to decode particular custom management TLV. IEEE1588-2019 specifies that range C000 – DFFF should be used for implementation-specific identifiers, and E000 – FFFE is to be assigned by alternate PTP Profile.
func SockaddrToIP ¶
SockaddrToIP converts socket address to an IP Somewhat copy from https://github.com/golang/go/blob/658b5e66ecbc41a49e6fb5aa63c5d9c804cf305f/src/net/udpsock_posix.go#L15
Types ¶
type AcknowledgeCancelUnicastTransmissionTLV ¶
type AcknowledgeCancelUnicastTransmissionTLV struct { TLVHead MsgTypeAndFlags UnicastMsgTypeAndFlags // first 4 bits is msg type, then flags R and/or G Reserved uint8 }
AcknowledgeCancelUnicastTransmissionTLV Table 113 ACKNOWLEDGE_CANCEL_UNICAST_TRANSMISSION TLV format
func (*AcknowledgeCancelUnicastTransmissionTLV) MarshalBinaryTo ¶
func (t *AcknowledgeCancelUnicastTransmissionTLV) MarshalBinaryTo(b []byte) (int, error)
func (*AcknowledgeCancelUnicastTransmissionTLV) UnmarshalBinary ¶
func (t *AcknowledgeCancelUnicastTransmissionTLV) UnmarshalBinary(b []byte) error
UnmarshalBinary parses []byte and populates struct fields
type Action ¶
type Action uint8
Action indicate the action to be taken on receipt of the PTP message as defined in Table 57
type Announce ¶
type Announce struct { Header AnnounceBody }
Announce is a full Announce packet
func (*Announce) MarshalBinary ¶
MarshalBinary converts packet to []bytes
type AnnounceBody ¶
type AnnounceBody struct { OriginTimestamp Timestamp CurrentUTCOffset int16 Reserved uint8 GrandmasterPriority1 uint8 GrandmasterClockQuality ClockQuality GrandmasterPriority2 uint8 GrandmasterIdentity ClockIdentity StepsRemoved uint16 TimeSource TimeSource }
AnnounceBody Table 43 Announce message fields
type BinaryMarshalerTo ¶
BinaryMarshalerTo is an interface implemented by an object that can marshal itself into a binary form into provided []byte
type CancelUnicastTransmissionTLV ¶
type CancelUnicastTransmissionTLV struct { TLVHead MsgTypeAndFlags UnicastMsgTypeAndFlags // first 4 bits is msg type, then flags R and/or G Reserved uint8 }
CancelUnicastTransmissionTLV Table 112 CANCEL_UNICAST_TRANSMISSION TLV format
func (*CancelUnicastTransmissionTLV) MarshalBinaryTo ¶
func (t *CancelUnicastTransmissionTLV) MarshalBinaryTo(b []byte) (int, error)
func (*CancelUnicastTransmissionTLV) UnmarshalBinary ¶
func (t *CancelUnicastTransmissionTLV) UnmarshalBinary(b []byte) error
UnmarshalBinary parses []byte and populates struct fields
type ClockIdentity ¶
type ClockIdentity uint64
The ClockIdentity type identifies unique entities within a PTP Network, e.g. a PTP Instance or an entity of a common service.
func NewClockIdentity ¶
func NewClockIdentity(mac net.HardwareAddr) (ClockIdentity, error)
NewClockIdentity creates new ClockIdentity from MAC address
func (ClockIdentity) String ¶
func (c ClockIdentity) String() string
String formats ClockIdentity same way ptp4l pmc client does
type ClockQuality ¶
ClockQuality represents the quality of a clock.
type Correction ¶
type Correction IntFloat
Correction is the value of the correction measured in nanoseconds and multiplied by 2**16. For example, 2.5 ns is represented as 0000 0000 0002 8000 base 16 A value of one in all bits, except the most significant, of the field shall indicate that the correction is too big to be represented.
func NewCorrection ¶
func NewCorrection(ns float64) Correction
NewCorrection returns Correctin built from Nanoseconds
func (Correction) Nanoseconds ¶
func (t Correction) Nanoseconds() float64
Nanoseconds decodes Correction to human-understandable nanoseconds
func (Correction) String ¶
func (t Correction) String() string
func (Correction) TooBig ¶
func (t Correction) TooBig() bool
TooBig means correction is too big to be represented.
type CurrentDataSetTLV ¶
type CurrentDataSetTLV struct { ManagementTLVHead StepsRemoved uint16 OffsetFromMaster TimeInterval MeanPathDelay TimeInterval }
CurrentDataSetTLV Spec Table 84 - CURRENT_DATA_SET management TLV data field
type DefaultDataSetTLV ¶
type DefaultDataSetTLV struct { ManagementTLVHead SoTSC uint8 Reserved0 uint8 NumberPorts uint16 Priority1 uint8 ClockQuality ClockQuality Priority2 uint8 ClockIdentity ClockIdentity DomainNumber uint8 Reserved1 uint8 }
DefaultDataSetTLV Spec Table 69 - DEFAULT_DATA_SET management TLV data field
type DelayResp ¶
type DelayResp struct { Header DelayRespBody }
DelayResp is a full Delay_Resp packet
func (*DelayResp) MarshalBinary ¶
MarshalBinary converts packet to []bytes
func (*DelayResp) UnmarshalBinary ¶
type DelayRespBody ¶
type DelayRespBody struct { ReceiveTimestamp Timestamp RequestingPortIdentity PortIdentity }
DelayRespBody Table 46 Delay_Resp message fields
type FollowUp ¶
type FollowUp struct { Header FollowUpBody }
FollowUp is a full Follow_Up packet
func (*FollowUp) MarshalBinary ¶
MarshalBinary converts packet to []bytes
func (*FollowUp) UnmarshalBinary ¶
type FollowUpBody ¶
type FollowUpBody struct {
PreciseOriginTimestamp Timestamp
}
FollowUpBody Table 45 Follow_Up message fields
type GrantUnicastTransmissionTLV ¶
type GrantUnicastTransmissionTLV struct { TLVHead MsgTypeAndReserved UnicastMsgTypeAndFlags // first 4 bits only, same enums as with normal message type LogInterMessagePeriod LogInterval DurationField uint32 Reserved uint8 Renewal uint8 }
GrantUnicastTransmissionTLV Table 111 GRANT_UNICAST_TRANSMISSION TLV format
func (*GrantUnicastTransmissionTLV) MarshalBinaryTo ¶
func (t *GrantUnicastTransmissionTLV) MarshalBinaryTo(b []byte) (int, error)
func (*GrantUnicastTransmissionTLV) UnmarshalBinary ¶
func (t *GrantUnicastTransmissionTLV) UnmarshalBinary(b []byte) error
UnmarshalBinary parses []byte and populates struct fields
type Header ¶
type Header struct { SdoIDAndMsgType SdoIDAndMsgType // first 4 bits is SdoId, next 4 bytes are msgtype Version uint8 MessageLength uint16 DomainNumber uint8 MinorSdoID uint8 FlagField uint16 CorrectionField Correction MessageTypeSpecific uint32 SourcePortIdentity PortIdentity SequenceID uint16 ControlField uint8 // the use of this field is obsolete according to IEEE, unless it's ipv4 LogMessageInterval LogInterval // see Table 42 Values of logMessageInterval field }
Header Table 35 Common PTP message header
func (*Header) MessageType ¶
func (p *Header) MessageType() MessageType
MessageType returns MessageType
func (*Header) SetSequence ¶
SetSequence populates sequence field
type LogInterval ¶
type LogInterval int8
LogInterval shall be the logarithm, to base 2, of the requested period in seconds. In layman's terms, it's specified as a power of two in seconds.
const MgmtLogMessageInterval LogInterval = 0x7f // as per Table 42 Values of logMessageInterval field
MgmtLogMessageInterval is the default LogInterval value used in Management packets
func NewLogInterval ¶
func NewLogInterval(d time.Duration) (LogInterval, error)
NewLogInterval returns new LogInterval from time.Duration. The values of these logarithmic attributes shall be selected from integers in the range -128 to 127 subject to further limits established in the applicable PTP Profile.
func (LogInterval) Duration ¶
func (i LogInterval) Duration() time.Duration
Duration returns LogInterval as time.Duration
type Management ¶
type Management struct { ManagementMsgHead TLV ManagementTLV }
Management packet, see '15. PTP management messages'
func CurrentDataSetRequest ¶
func CurrentDataSetRequest() *Management
CurrentDataSetRequest prepares request packet for CURRENT_DATA_SET request
func DefaultDataSetRequest ¶
func DefaultDataSetRequest() *Management
DefaultDataSetRequest prepares request packet for DEFAULT_DATA_SET request
func ParentDataSetRequest ¶
func ParentDataSetRequest() *Management
ParentDataSetRequest prepares request packet for PARENT_DATA_SET request
func PortStatsNPRequest ¶
func PortStatsNPRequest() *Management
PortStatsNPRequest prepares request packet for PORT_STATS_NP request
func TimeStatusNPRequest ¶
func TimeStatusNPRequest() *Management
TimeStatusNPRequest prepares request packet for TIME_STATUS_NP request
func (*Management) MarshalBinary ¶
func (p *Management) MarshalBinary() ([]byte, error)
MarshalBinary converts packet to []bytes
func (*Management) MarshalBinaryTo ¶
func (p *Management) MarshalBinaryTo(bytes io.Writer) error
MarshalBinaryTo converts packet to bytes and writes those into provided buffer
func (*Management) UnmarshalBinary ¶
func (p *Management) UnmarshalBinary(rawBytes []byte) error
UnmarshalBinary parses []byte and populates struct fields
type ManagementErrorID ¶
type ManagementErrorID uint16
ManagementErrorID is an enum for possible management errors
const ( ErrorResponseTooBig ManagementErrorID = 0x0001 // The requested operation could not fit in a single response message ErrorNoSuchID ManagementErrorID = 0x0002 // The managementId is not recognized ErrorWrongLength ManagementErrorID = 0x0003 // The managementId was identified but the length of the data was wrong ErrorWrongValue ManagementErrorID = 0x0004 // The managementId and length were correct but one or more values were wrong ErrorNotSetable ManagementErrorID = 0x0005 // Some of the variables in the set command were not updated because they are not configurable ErrorNotSupported ManagementErrorID = 0x0006 // The requested operation is not supported in this PTP Instance ErrorUnpopulated ManagementErrorID = 0x0007 // The targetPortIdentity of the PTP management message refers to an entity that is not present in the PTP Instance at the time of the request // some reserved and provile-specific ranges ErrorGeneralError ManagementErrorID = 0xFFFE //An error occurred that is not covered by other ManagementErrorID values )
Table 109 ManagementErrorID enumeration
func (ManagementErrorID) Error ¶
func (t ManagementErrorID) Error() string
func (ManagementErrorID) String ¶
func (t ManagementErrorID) String() string
type ManagementErrorStatusTLV ¶
type ManagementErrorStatusTLV struct { TLVHead ManagementErrorID ManagementErrorID ManagementID ManagementID Reserved int32 DisplayData PTPText }
ManagementErrorStatusTLV spec Table 108 MANAGEMENT_ERROR_STATUS TLV format
type ManagementID ¶
type ManagementID uint16
ManagementID is type for Management IDs
const ( IDNullPTPManagement ManagementID = 0x0000 IDClockDescription ManagementID = 0x0001 IDUserDescription ManagementID = 0x0002 IDSaveInNonVolatileStorage ManagementID = 0x0003 IDResetNonVolatileStorage ManagementID = 0x0004 IDInitialize ManagementID = 0x0005 IDFaultLog ManagementID = 0x0006 IDFaultLogReset ManagementID = 0x0007 IDDefaultDataSet ManagementID = 0x2000 IDCurrentDataSet ManagementID = 0x2001 IDParentDataSet ManagementID = 0x2002 IDTimePropertiesDataSet ManagementID = 0x2003 IDPortDataSet ManagementID = 0x2004 )
Management IDs we support, from Table 59 managementId values
const ( IDPortStatsNP ManagementID = 0xC005 IDTimeStatusNP ManagementID = 0xC000 )
ptp4l-specific management TLV ids
type ManagementMsgErrorStatus ¶
type ManagementMsgErrorStatus struct { ManagementMsgHead ManagementErrorStatusTLV }
ManagementMsgErrorStatus is header + ManagementErrorStatusTLV
func (*ManagementMsgErrorStatus) MarshalBinary ¶
func (p *ManagementMsgErrorStatus) MarshalBinary() ([]byte, error)
MarshalBinary converts packet to []bytes
func (*ManagementMsgErrorStatus) MarshalBinaryTo ¶
func (p *ManagementMsgErrorStatus) MarshalBinaryTo(bytes io.Writer) error
MarshalBinaryTo converts packet to bytes and writes those into provided buffer
func (*ManagementMsgErrorStatus) UnmarshalBinary ¶
func (p *ManagementMsgErrorStatus) UnmarshalBinary(rawBytes []byte) error
UnmarshalBinary parses []byte and populates struct fields
type ManagementMsgHead ¶
type ManagementMsgHead struct { Header TargetPortIdentity PortIdentity StartingBoundaryHops uint8 BoundaryHops uint8 ActionField Action Reserved uint8 }
ManagementMsgHead Spec Table 56 - Management message fields
func (*ManagementMsgHead) Action ¶
func (p *ManagementMsgHead) Action() Action
Action returns ActionField
type ManagementTLV ¶
type ManagementTLV interface { TLV MgmtID() ManagementID }
ManagementTLV abstracts away any ManagementTLV
type ManagementTLVHead ¶
type ManagementTLVHead struct { TLVHead ManagementID ManagementID }
ManagementTLVHead Spec Table 58 - Management TLV fields
func (*ManagementTLVHead) MgmtID ¶
func (p *ManagementTLVHead) MgmtID() ManagementID
MgmtID returns ManagementID
type MessageType ¶
type MessageType uint8
MessageType is type for Message Types
const ( MessageSync MessageType = 0x0 MessageDelayReq MessageType = 0x1 MessagePDelayReq MessageType = 0x2 MessagePDelayResp MessageType = 0x3 MessageFollowUp MessageType = 0x8 MessageDelayResp MessageType = 0x9 MessagePDelayRespFollowUp MessageType = 0xA MessageAnnounce MessageType = 0xB MessageSignaling MessageType = 0xC MessageManagement MessageType = 0xD )
As per Table 36 Values of messageType field
func ProbeMsgType ¶
func ProbeMsgType(data []byte) (msg MessageType, err error)
ProbeMsgType reads first 8 bits of data and tries to decode it to SdoIDAndMsgType, then return MessageType
func (MessageType) String ¶
func (m MessageType) String() string
type MgmtClient ¶
type MgmtClient struct { Connection io.ReadWriter Sequence uint16 }
MgmtClient talks to ptp server over unix socket
func (*MgmtClient) Communicate ¶
func (c *MgmtClient) Communicate(packet *Management) (*Management, error)
Communicate sends the management the packet, parses response into something usable
func (*MgmtClient) CurrentDataSet ¶
func (c *MgmtClient) CurrentDataSet() (*CurrentDataSetTLV, error)
CurrentDataSet sends CURRENT_DATA_SET request and returns response
func (*MgmtClient) DefaultDataSet ¶
func (c *MgmtClient) DefaultDataSet() (*DefaultDataSetTLV, error)
DefaultDataSet sends DEFAULT_DATA_SET request and returns response
func (*MgmtClient) ParentDataSet ¶
func (c *MgmtClient) ParentDataSet() (*ParentDataSetTLV, error)
ParentDataSet sends PARENT_DATA_SET request and returns response
func (*MgmtClient) PortStatsNP ¶
func (c *MgmtClient) PortStatsNP() (*PortStatsNPTLV, error)
PortStatsNP sends PORT_STATS_NP request and returns response
func (*MgmtClient) SendPacket ¶
func (c *MgmtClient) SendPacket(packet *Management) error
SendPacket sends packet, incrementing sequence counter
func (*MgmtClient) TimeStatusNP ¶
func (c *MgmtClient) TimeStatusNP() (*TimeStatusNPTLV, error)
TimeStatusNP sends TIME_STATUS_NP request and returns response
type MgmtTLVDecoderFunc ¶
type MgmtTLVDecoderFunc func(data []byte) (ManagementTLV, error)
MgmtTLVDecoderFunc is the function we use to decode management TLV from bytes
type PDelayReq ¶
type PDelayReq struct { Header PDelayReqBody }
PDelayReq is a full Pdelay_Req packet
type PDelayReqBody ¶
PDelayReqBody Table 47 Pdelay_Req message fields
type PDelayResp ¶
type PDelayResp struct { Header PDelayRespBody }
PDelayResp is a full Pdelay_Resp packet
type PDelayRespBody ¶
type PDelayRespBody struct { RequestReceiptTimestamp Timestamp RequestingPortIdentity PortIdentity }
PDelayRespBody Table 48 Pdelay_Resp message fields
type PDelayRespFollowUp ¶
type PDelayRespFollowUp struct { Header PDelayRespFollowUpBody }
PDelayRespFollowUp is a full Pdelay_Resp_Follow_Up packet
type PDelayRespFollowUpBody ¶
type PDelayRespFollowUpBody struct { ResponseOriginTimestamp Timestamp RequestingPortIdentity PortIdentity }
PDelayRespFollowUpBody Table 49 Pdelay_Resp_Follow_Up message fields
type PTPText ¶
type PTPText string
PTPText data type is used to represent textual material in PTP messages. TextField is encoded as UTF-8. The most significant byte of the leading text symbol shall be the element of the array with index 0. UTF-8 encoding has variable length, thus LengthField can be larger than number of characters.
type PTPText struct { LengthField uint8 TextField []byte }
func (*PTPText) MarshalBinary ¶
MarshalBinary converts ptptext to []bytes
func (*PTPText) UnmarshalBinary ¶
UnmarshalBinary populates ptptext from bytes
type Packet ¶
type Packet interface { MessageType() MessageType SetSequence(uint16) }
Packet is an iterface to abstract all different packets
func DecodePacket ¶
DecodePacket provides single entry point to try and decode any []bytes to PTPv2 packet. It can be used for easy integration with anything that provides UDP packet payload as bytes. Resulting Packet user can then either switch based on MessageType(), or just with type switch.
type ParentDataSetTLV ¶
type ParentDataSetTLV struct { ManagementTLVHead ParentPortIdentity PortIdentity PS uint8 Reserved uint8 ObservedParentOffsetScaledLogVariance uint16 ObservedParentClockPhaseChangeRate uint32 GrandmasterPriority1 uint8 GrandmasterClockQuality ClockQuality GrandmasterPriority2 uint8 GrandmasterIdentity ClockIdentity }
ParentDataSetTLV Spec Table 85 - PARENT_DATA_SET management TLV data field
type PortIdentity ¶
type PortIdentity struct { ClockIdentity ClockIdentity PortNumber uint16 }
The PortIdentity type identifies a PTP Port or a Link Port
func (PortIdentity) String ¶
func (p PortIdentity) String() string
String formats PortIdentity same way ptp4l pmc client does
type PortStatsNPTLV ¶
type PortStatsNPTLV struct { ManagementTLVHead PortIdentity PortIdentity PortStats PortStats }
PortStatsNPTLV is a ptp4l struct containing port identinity and statistics
type RequestUnicastTransmissionTLV ¶
type RequestUnicastTransmissionTLV struct { TLVHead MsgTypeAndReserved UnicastMsgTypeAndFlags // first 4 bits only, same enums as with normal message type LogInterMessagePeriod LogInterval DurationField uint32 }
RequestUnicastTransmissionTLV Table 110 REQUEST_UNICAST_TRANSMISSION TLV format
func (*RequestUnicastTransmissionTLV) MarshalBinaryTo ¶
func (t *RequestUnicastTransmissionTLV) MarshalBinaryTo(b []byte) (int, error)
func (*RequestUnicastTransmissionTLV) UnmarshalBinary ¶
func (t *RequestUnicastTransmissionTLV) UnmarshalBinary(b []byte) error
UnmarshalBinary parses []byte and populates struct fields
type SdoIDAndMsgType ¶
type SdoIDAndMsgType uint8
SdoIDAndMsgType is a uint8 where first 4 bites contain SdoID and last 4 bits MessageType
func NewSdoIDAndMsgType ¶
func NewSdoIDAndMsgType(msgType MessageType, sdoID uint8) SdoIDAndMsgType
NewSdoIDAndMsgType builds new SdoIDAndMsgType from MessageType and flags
func (SdoIDAndMsgType) MsgType ¶
func (m SdoIDAndMsgType) MsgType() MessageType
MsgType extracts MessageType from SdoIDAndMsgType
type Signaling ¶
type Signaling struct { Header TargetPortIdentity PortIdentity TLVs []TLV }
Signaling packet. As it's of variable size, we cannot just binary.Read/Write it.
func (*Signaling) MarshalBinary ¶
MarshalBinary converts packet to []bytes
func (*Signaling) UnmarshalBinary ¶
UnmarshalBinary parses []byte and populates struct fields
type SyncDelayReq ¶
type SyncDelayReq struct { Header SyncDelayReqBody }
SyncDelayReq is a full Sync/Delay_Req packet
func (*SyncDelayReq) MarshalBinary ¶
func (p *SyncDelayReq) MarshalBinary() ([]byte, error)
MarshalBinary converts packet to []bytes
func (*SyncDelayReq) MarshalBinaryTo ¶
func (p *SyncDelayReq) MarshalBinaryTo(b []byte) (int, error)
func (*SyncDelayReq) UnmarshalBinary ¶
func (p *SyncDelayReq) UnmarshalBinary(b []byte) error
type SyncDelayReqBody ¶
type SyncDelayReqBody struct {
OriginTimestamp Timestamp
}
SyncDelayReqBody Table 44 Sync and Delay_Req message fields
type TLVHead ¶
type TLVHead struct { TLVType TLVType LengthField uint16 // The length of all TLVs shall be an even number of octets }
TLVHead is a common part of all TLVs
type TLVType ¶
type TLVType uint16
TLVType is type for TLV types
const ( TLVManagement TLVType = 0x0001 TLVManagementErrorStatus TLVType = 0x0002 TLVOrganizationExtension TLVType = 0x0003 TLVRequestUnicastTransmission TLVType = 0x0004 TLVGrantUnicastTransmission TLVType = 0x0005 TLVCancelUnicastTransmission TLVType = 0x0006 TLVAcknowledgeCancelUnicastTransmission TLVType = 0x0007 TLVPathTrace TLVType = 0x0008 TLVAlternateTimeOffsetIndicator TLVType = 0x0009 )
As per Table 52 tlvType values
type TimeInterval ¶
type TimeInterval IntFloat
TimeInterval is the time interval expressed in nanoseconds, multiplied by 2**16. Positive or negative time intervals outside the maximum range of this data type shall be encoded as the largest positive and negative values of the data type, respectively. For example, 2.5 ns is expressed as 0000 0000 0002 8000 base 16
func NewTimeInterval ¶
func NewTimeInterval(ns float64) TimeInterval
NewTimeInterval returns TimeInterval built from Nanoseconds
func (TimeInterval) Nanoseconds ¶
func (t TimeInterval) Nanoseconds() float64
Nanoseconds decodes TimeInterval to human-understandable nanoseconds
func (TimeInterval) String ¶
func (t TimeInterval) String() string
type TimeSource ¶
type TimeSource uint8
TimeSource indicates the immediate source of time used by the Grandmaster PTP Instance
const ( TimeSourceAtomicClock TimeSource = 0x10 TimeSourceGNSS TimeSource = 0x20 TimeSourceTerrestrialRadio TimeSource = 0x30 TimeSourceSerialTimeCode TimeSource = 0x39 TimeSourcePTP TimeSource = 0x40 TimeSourceNTP TimeSource = 0x50 TimeSourceHandSet TimeSource = 0x60 TimeSourceOther TimeSource = 0x90 TimeSourceInternalOscillator TimeSource = 0xa0 )
TimeSource values, Table 6 timeSource enumeration
func (TimeSource) String ¶
func (t TimeSource) String() string
type TimeStatusNPTLV ¶
type TimeStatusNPTLV struct { ManagementTLVHead MasterOffsetNS int64 IngressTimeNS int64 // this is PHC time CumulativeScaledRateOffset int32 ScaledLastGmPhaseChange int32 GMTimeBaseIndicator uint16 LastGmPhaseChange ScaledNS GMPresent int32 GMIdentity ClockIdentity }
TimeStatusNPTLV is a ptp4l struct containing actually useful instance metrics
type Timestamp ¶
Timestamp type represents a positive time with respect to the epoch. The secondsField member is the integer portion of the timestamp in units of seconds. The nanosecondsField member is the fractional portion of the timestamp in units of nanoseconds. The nanosecondsField member is always less than 10**9 . For example: +2.000000001 seconds is represented by secondsField = 0000 0000 0002 base 16 and nanosecondsField= 0000 0001 base 16.
func NewTimestamp ¶
NewTimestamp allows to create Timestamp from time.Time
type UnicastMsgTypeAndFlags ¶
type UnicastMsgTypeAndFlags uint8
UnicastMsgTypeAndFlags is a uint8 where first 4 bites contain MessageType and last 4 bits contain some flags
func NewUnicastMsgTypeAndFlags ¶
func NewUnicastMsgTypeAndFlags(msgType MessageType, flags uint8) UnicastMsgTypeAndFlags
NewUnicastMsgTypeAndFlags builds new UnicastMsgTypeAndFlags from MessageType and flags
func (UnicastMsgTypeAndFlags) MsgType ¶
func (m UnicastMsgTypeAndFlags) MsgType() MessageType
MsgType extracts MessageType from UnicastMsgTypeAndFlags