Documentation ¶
Overview ¶
Package phc contains code to work with PTP Hardware Clock (PHC). It allows getting PHC time via different APIs (syscall, ioctl).
It also provides means to calculate offset between sys clock and PHC.
Index ¶
- Constants
- func IfaceToPHCDevice(iface string) (string, error)
- func NewPiServo(interval time.Duration, firstStepth time.Duration, stepth time.Duration, ...) (*servo.PiServo, error)
- func OffsetBetweenDevices(deviceA, deviceB *os.File) (time.Duration, error)
- func PPSClockSync(pi ServoController, srcTimestamp time.Time, dstEventTimestamp time.Time, ...) error
- func Time(iface string, method TimeMethod) (time.Time, error)
- type Device
- func (dev *Device) AdjFreq(freqPPB float64) error
- func (dev *Device) ClockID() int32
- func (dev *Device) Fd() uintptr
- func (dev *Device) File() *os.File
- func (dev *Device) FreqPPB() (freqPPB float64, err error)
- func (dev *Device) MaxFreqAdjPPB() (maxFreq float64, err error)
- func (dev *Device) Read(buffer []byte) (int, error)
- func (dev *Device) ReadSysoffExtended() (*PTPSysOffsetExtended, error)
- func (dev *Device) ReadSysoffExtended1() (*PTPSysOffsetExtended, error)
- func (dev *Device) ReadSysoffPrecise() (*PTPSysOffsetPrecise, error)
- func (dev *Device) SetTime(t time.Time) error
- func (dev *Device) Step(step time.Duration) error
- func (dev *Device) Time() (time.Time, error)
- type DeviceController
- type FrequencyGetter
- type PPSPoller
- type PPSSink
- type PPSSource
- type PPSSourceState
- type PTPSysOffsetExtended
- type PTPSysOffsetPrecise
- type PtpClockCaps
- type PtpClockTime
- type PtpExttsEvent
- type PtpExttsRequest
- type PtpPeroutRequest
- type ServoController
- type SysoffResult
- type TimeMethod
- type Timestamper
Constants ¶
const ( DefaultTs2PhcIndex = 3 DefaultTs2PhcSinkIndex = 0 PPSPollMaxAttempts = 20 )
PPS related constants
const DefaultMaxClockFreqPPB = 500000.0
DefaultMaxClockFreqPPB value came from linuxptp project (clockadj.c)
const (
// ExtendedNumProbes is the number of samples we request for IoctlPtpSysOffsetExtended
ExtendedNumProbes = 9
)
Variables ¶
This section is empty.
Functions ¶
func IfaceToPHCDevice ¶
IfaceToPHCDevice returns path to PHC device associated with given network card iface
func NewPiServo ¶
func NewPiServo(interval time.Duration, firstStepth time.Duration, stepth time.Duration, device FrequencyGetter, maxFreq float64) (*servo.PiServo, error)
NewPiServo returns a servo.PiServo object configure for synchronizing the given device. maxFreq 0 is equivalent to no maxFreq
func OffsetBetweenDevices ¶
OffsetBetweenDevices returns estimated difference between two PHC devices
func PPSClockSync ¶
func PPSClockSync(pi ServoController, srcTimestamp time.Time, dstEventTimestamp time.Time, dstDevice DeviceController) error
PPSClockSync adjusts the frequency of the destination device based on the PPS from the ppsSource
Types ¶
type Device ¶
Device represents a PHC device
func (*Device) ClockID ¶
ClockID derives the clock ID from the file descriptor number - see clock_gettime(3), FD_TO_CLOCKID macros
func (*Device) MaxFreqAdjPPB ¶
MaxFreqAdjPPB reads max value for frequency adjustments (in PPB) from ptp device
func (*Device) ReadSysoffExtended ¶
func (dev *Device) ReadSysoffExtended() (*PTPSysOffsetExtended, error)
ReadSysoffExtended reads the precise time from the PHC along with SYS time to measure the call delay. The nsamples parameter is set to ExtendedNumProbes.
func (*Device) ReadSysoffExtended1 ¶
func (dev *Device) ReadSysoffExtended1() (*PTPSysOffsetExtended, error)
ReadSysoffExtended1 reads the precise time from the PHC along with SYS time to measure the call delay. The samples parameter is set to 1.
func (*Device) ReadSysoffPrecise ¶
func (dev *Device) ReadSysoffPrecise() (*PTPSysOffsetPrecise, error)
ReadSysoffPrecise reads the precise time from the PHC along with SYS time to measure the call delay.
type DeviceController ¶
type DeviceController interface { Time() (time.Time, error) File() *os.File AdjFreq(freq float64) error Step(offset time.Duration) error Read(buf []byte) (int, error) Fd() uintptr // contains filtered or unexported methods }
DeviceController defines a subset of functions to interact with a phc device. Enables mocking.
type FrequencyGetter ¶
FrequencyGetter is an interface for getting PHC frequency and max frequency adjustment
type PPSSink ¶
type PPSSink struct { InputPin uint Polarity uint32 PulseWidth uint32 Device DeviceController // contains filtered or unexported fields }
PPSSink represents a device which is a sink of PPS signals
func PPSSinkFromDevice ¶
func PPSSinkFromDevice(targetDevice DeviceController, pinIndex uint) (*PPSSink, error)
PPSSinkFromDevice configures the targetDevice to be a PPS sink and report PPS timestamp events
type PPSSource ¶
type PPSSource struct { PHCDevice DeviceController PPSPinIndex uint // contains filtered or unexported fields }
PPSSource represents a PPS source
func ActivatePPSSource ¶
func ActivatePPSSource(dev DeviceController, pinIndex uint) (*PPSSource, error)
ActivatePPSSource configures the PHC device to be a PPS timestamp source
type PPSSourceState ¶
type PPSSourceState int
PPSSourceState represents the state of a PPS source
const ( // UnknownStatus is the initial state of a PPS source, which means PPS may or may not be configured UnknownStatus PPSSourceState = iota // PPSSet means the underlying device is activated as a PPS source PPSSet )
type PTPSysOffsetExtended ¶
type PTPSysOffsetExtended unix.PtpSysOffsetExtended
PTPSysOffsetExtended wraps unix.PtpSysOffsetExtended to add methods
func (*PTPSysOffsetExtended) BestSample ¶
func (extended *PTPSysOffsetExtended) BestSample() SysoffResult
BestSample finds a sample which took the least time to be read; the logic is loosely based on sysoff_estimate from ptp4l sysoff.c
func (*PTPSysOffsetExtended) Sub ¶
func (extended *PTPSysOffsetExtended) Sub(a *PTPSysOffsetExtended) time.Duration
Sub returns the estimated difference between two PHC SYS_OFFSET_EXTENDED readings
type PTPSysOffsetPrecise ¶
type PTPSysOffsetPrecise unix.PtpSysOffsetPrecise
PTPSysOffsetPrecise wraps unix.PtpSysOffsetPrecise to add methods
func (*PTPSysOffsetPrecise) Sub ¶
func (precise *PTPSysOffsetPrecise) Sub(a *PTPSysOffsetPrecise) time.Duration
Sub returns the estimated difference between two PHC SYS_OFFSET_PRECISE readings
type ServoController ¶
type ServoController interface { Sample(offset int64, localTs uint64) (float64, servo.State) Unlock() }
ServoController abstracts away servo
type SysoffResult ¶
type SysoffResult struct { Offset time.Duration Delay time.Duration SysTime time.Time PHCTime time.Time }
SysoffResult is a result of PHC time measurement with related data
func SysoffEstimateBasic ¶
func SysoffEstimateBasic(ts1, rt, ts2 time.Time) SysoffResult
SysoffEstimateBasic logic based on calculate_offset from ptp4l phc_ctl.c
func SysoffFromPrecise ¶
func SysoffFromPrecise(precise *PTPSysOffsetPrecise) SysoffResult
SysoffFromPrecise returns SysoffResult from *PTPSysOffsetPrecise . Code based on sysoff_precise from ptp4l sysoff.c
func TimeAndOffset ¶
func TimeAndOffset(iface string, method TimeMethod) (SysoffResult, error)
TimeAndOffset returns time we got from network card + offset
func TimeAndOffsetFromDevice ¶
func TimeAndOffsetFromDevice(device string, method TimeMethod) (SysoffResult, error)
TimeAndOffsetFromDevice returns time we got from phc device + offset
type TimeMethod ¶
type TimeMethod string
TimeMethod is method we use to get time
const ( MethodSyscallClockGettime TimeMethod = "syscall_clock_gettime" MethodIoctlSysOffsetExtended TimeMethod = "ioctl_PTP_SYS_OFFSET_EXTENDED" MethodIoctlSysOffsetPrecise TimeMethod = "ioctl_PTP_SYS_OFFSET_PRECISE" )
Methods we support to get time
type Timestamper ¶
Timestamper represents a device that can return a Timestamp