libptpmgmt  1.4
libptpmgmt library that provides the functionality of linuxptp pmc
ptp.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-3.0-or-later
2  SPDX-FileCopyrightText: Copyright © 2021 Erez Geva <ErezGeva2@gmail.com> */
3 
15 #ifndef __PTPMGMT_IF_H
16 #define __PTPMGMT_IF_H
17 
18 #ifdef __cplusplus
19 #include <vector>
20 #include <ctime>
21 #include "types.h"
22 #ifdef __linux__
23 #include <linux/ethtool.h>
24 #endif
25 
26 __PTPMGMT_NAMESPACE_BEGIN
27 
28 /* The macro might be used, we prefer C++ const */
29 #ifndef CLOCK_INVALID
31 const clockid_t CLOCK_INVALID = -1;
32 #endif
34 const int NO_SUCH_IF = -1;
36 const int NO_SUCH_PTP = -1;
38 typedef long double float_freq;
39 
43 class IfInfo
44 {
45  private:
46  bool m_isInit = false;
47  int m_ifIndex = NO_SUCH_IF, m_ptpIndex = NO_SUCH_PTP;
48  std::string m_ifName;
49  Binary m_mac;
50 
51  bool initPtp(int fd, struct ifreq &m_ifr);
52 
53  public:
59  bool initUsingName(const std::string &ifName);
70  bool isInit() const { return m_isInit; }
75  int ifIndex() const { return m_ifIndex; }
80  const std::string &ifName() const { return m_ifName; }
85  const char *ifName_c() const { return m_ifName.c_str(); }
90  const Binary &mac() const { return m_mac; }
95  const uint8_t *mac_c() const { return m_mac.get(); }
100  size_t mac_size() const { return m_mac.length(); }
108  int ptpIndex() const { return m_ptpIndex; }
109 };
113 struct PtpCaps_t {
114  int64_t max_ppb;
115  int num_alarm;
118  bool pps;
119  int num_pins;
123  int64_t max_phase_adj;
124 };
133 };
137 struct PtpPin_t {
138  unsigned int index;
143  std::string description;
145  unsigned int channel;
146 };
151 const uint8_t PTP_EXTERN_TS_RISING_EDGE = 1 << 1;
156 const uint8_t PTP_EXTERN_TS_FALLING_EDGE = 1 << 2;
161 const uint8_t PTP_EXTERN_TS_STRICT = 1 << 3;
166 const uint8_t PTP_PERIOD_ONE_SHOT = 1 << 0;
171 const uint8_t PTP_PERIOD_WIDTH = 1 << 1;
176 const uint8_t PTP_PERIOD_PHASE = 1 << 2;
177 
181 struct PtpEvent_t {
182  unsigned int index;
184 };
188 struct PtpSample_t {
191 };
199 };
207 };
215 };
220 {
221  protected:
225  clockid_t m_clkId;
226  bool m_isInit;
227  bool m_freq;
233  BaseClock(clockid_t clkId, bool freq) : m_clkId(clkId), m_isInit(true),
234  m_freq(freq) {}
239  BaseClock() : m_clkId(CLOCK_INVALID), m_isInit(false), m_freq(false) {}
241  public:
252  bool setTime(const Timestamp_t &ts) const;
258  bool offsetClock(int64_t offset) const;
269  bool setFreq(float_freq freq) const;
275  bool setPhase(int64_t offset) const;
276 };
280 class SysClock : public BaseClock
281 {
282  public:
283  SysClock();
284 };
295 class PtpClock : public BaseClock
296 {
297  private:
298  int m_fd, m_ptpIndex;
299  std::string m_device;
300  bool init(const char *device, bool readonly);
301 
302  public:
303  PtpClock() : m_fd(-1), m_ptpIndex(NO_SUCH_PTP) {}
304  ~PtpClock();
311  static bool isCharFile(const std::string &file);
318  bool initUsingDevice(const std::string &device, bool readonly = false);
325  bool initUsingIndex(int ptpIndex, bool readonly = false);
330  bool isInit() const { return m_isInit; }
335  clockid_t clkId() const { return m_clkId; }
340  int getFd() const { return m_fd; }
345  int fileno() const { return m_fd; }
351  int ptpIndex() const { return m_ptpIndex; }
356  const std::string &device() const { return m_device; }
361  const char *device_c() const { return m_device.c_str(); }
366  bool setTimeFromSys() const;
371  bool setTimeToSys() const;
377  bool fetchCaps(PtpCaps_t &caps) const;
385  bool readPin(unsigned int index, PtpPin_t &pin) const;
393  bool writePin(PtpPin_t &pin) const;
402  bool ExternTSEbable(unsigned int index, uint8_t flags) const;
409  bool ExternTSDisable(unsigned int index) const;
414  bool MaskClearAll() const;
421  bool MaskEnable(unsigned int index) const;
427  bool readEvent(PtpEvent_t &event) const;
435  bool readEvents(std::vector<PtpEvent_t> &events, size_t max = 0) const;
445  bool setPinPeriod(unsigned int index, PtpPinPeriodDef_t times,
446  uint8_t flags = 0) const;
454  bool setPtpPpsEvent(bool enable) const;
462  bool samplePtpSys(size_t count, std::vector<PtpSample_t> &samples) const;
471  bool extSamplePtpSys(size_t count, std::vector<PtpSampleExt_t> &samples) const;
472 
480 };
481 
482 __PTPMGMT_NAMESPACE_END
483 #else /* __cplusplus */
484 #include "c/ptp.h"
485 #endif /* __cplusplus */
486 
487 #endif /* __PTPMGMT_IF_H */
C interface to network and PHC classes.
Types, enumerators, and structers used by C interface to PTP management messages.
Definition: ptp.h:220
Timestamp_t getTime() const
bool setPhase(int64_t offset) const
bool setFreq(float_freq freq) const
float_freq getFreq() const
bool offsetClock(int64_t offset) const
bool setTime(const Timestamp_t &ts) const
Hold octets.
Definition: bin.h:28
size_t length() const
Definition: bin.h:69
const uint8_t * get() const
Definition: bin.h:84
Network interface information.
Definition: ptp.h:44
int ptpIndex() const
Definition: ptp.h:108
bool isInit() const
Definition: ptp.h:70
const uint8_t * mac_c() const
Definition: ptp.h:95
size_t mac_size() const
Definition: ptp.h:100
const Binary & mac() const
Definition: ptp.h:90
bool initUsingName(const std::string &ifName)
int ifIndex() const
Definition: ptp.h:75
const std::string & ifName() const
Definition: ptp.h:80
bool initUsingIndex(int ifIndex)
const char * ifName_c() const
Definition: ptp.h:85
POSIX dynamic clock id generator.
Definition: ptp.h:296
clockid_t clkId() const
Definition: ptp.h:335
bool MaskClearAll() const
bool initUsingDevice(const std::string &device, bool readonly=false)
static bool isCharFile(const std::string &file)
int getFd() const
Definition: ptp.h:340
bool ExternTSDisable(unsigned int index) const
bool setTimeFromSys() const
const char * device_c() const
Definition: ptp.h:361
bool writePin(PtpPin_t &pin) const
bool fetchCaps(PtpCaps_t &caps) const
const std::string & device() const
Definition: ptp.h:356
bool initUsingIndex(int ptpIndex, bool readonly=false)
bool setTimeToSys() const
bool readEvents(std::vector< PtpEvent_t > &events, size_t max=0) const
bool ExternTSEbable(unsigned int index, uint8_t flags) const
bool readEvent(PtpEvent_t &event) const
bool preciseSamplePtpSys(PtpSamplePrecise_t &sample) const
bool MaskEnable(unsigned int index) const
bool setPinPeriod(unsigned int index, PtpPinPeriodDef_t times, uint8_t flags=0) const
int ptpIndex() const
Definition: ptp.h:351
int fileno() const
Definition: ptp.h:345
bool setPtpPpsEvent(bool enable) const
bool isInit() const
Definition: ptp.h:330
bool samplePtpSys(size_t count, std::vector< PtpSample_t > &samples) const
bool extSamplePtpSys(size_t count, std::vector< PtpSampleExt_t > &samples) const
bool readPin(unsigned int index, PtpPin_t &pin) const
Definition: ptp.h:281
const uint8_t PTP_PERIOD_ONE_SHOT
Definition: ptp.h:166
const uint8_t PTP_EXTERN_TS_FALLING_EDGE
Definition: ptp.h:156
long double float_freq
Definition: ptp.h:38
const int NO_SUCH_IF
Definition: ptp.h:34
const uint8_t PTP_EXTERN_TS_RISING_EDGE
Definition: ptp.h:151
const uint8_t PTP_PERIOD_WIDTH
Definition: ptp.h:171
const uint8_t PTP_EXTERN_TS_STRICT
Definition: ptp.h:161
__PTPMGMT_NAMESPACE_BEGIN const clockid_t CLOCK_INVALID
Definition: ptp.h:31
const int NO_SUCH_PTP
Definition: ptp.h:36
PtpPinFunc_e
Definition: ptp.h:128
@ PTP_PIN_EXTERNAL_TS
Definition: ptp.h:130
@ PTP_PIN_PERIODIC_OUT
Definition: ptp.h:131
@ PTP_PIN_PHY_SYNC
Definition: ptp.h:132
@ PTP_PIN_UNUSED
Definition: ptp.h:129
const uint8_t PTP_PERIOD_PHASE
Definition: ptp.h:176
Definition: ptp.h:113
bool adjust_phase
Definition: ptp.h:122
int num_alarm
Definition: ptp.h:115
int num_periodic_sig
Definition: ptp.h:117
int64_t max_ppb
Definition: ptp.h:114
int num_pins
Definition: ptp.h:119
bool cross_timestamping
Definition: ptp.h:121
int64_t max_phase_adj
Definition: ptp.h:123
int num_external_channels
Definition: ptp.h:116
bool pps
Definition: ptp.h:118
Definition: ptp.h:181
unsigned int index
Definition: ptp.h:182
Timestamp_t time
Definition: ptp.h:183
Definition: ptp.h:211
Timestamp_t width
Definition: ptp.h:213
Timestamp_t phase
Definition: ptp.h:214
Timestamp_t period
Definition: ptp.h:212
Definition: ptp.h:137
PtpPinFunc_e functional
Definition: ptp.h:144
unsigned int index
Definition: ptp.h:138
std::string description
Definition: ptp.h:143
unsigned int channel
Definition: ptp.h:145
Definition: ptp.h:195
Timestamp_t before
Definition: ptp.h:196
Timestamp_t phcClk
Definition: ptp.h:197
Timestamp_t after
Definition: ptp.h:198
Definition: ptp.h:203
Timestamp_t sysClk
Definition: ptp.h:205
Timestamp_t phcClk
Definition: ptp.h:204
Timestamp_t monoClk
Definition: ptp.h:206
Definition: ptp.h:188
Timestamp_t phcClk
Definition: ptp.h:190
Timestamp_t sysClk
Definition: ptp.h:189
Definition: types.h:416