libptpmgmt  1.3
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;
47  int m_ifIndex, m_ptpIndex;
48  std::string m_ifName;
49  Binary m_mac;
50 
51  bool initPtp(int fd, struct ifreq &m_ifr);
52 
53  public:
54  IfInfo() : m_isInit(false), m_ifIndex(NO_SUCH_IF), m_ptpIndex(NO_SUCH_PTP) {}
60  bool initUsingName(const std::string &ifName);
71  bool isInit() const { return m_isInit; }
76  int ifIndex() const { return m_ifIndex; }
81  const std::string &ifName() const { return m_ifName; }
86  const char *ifName_c() const { return m_ifName.c_str(); }
91  const Binary &mac() const { return m_mac; }
96  const uint8_t *mac_c() const { return m_mac.get(); }
101  size_t mac_size() const { return m_mac.length(); }
109  int ptpIndex() const { return m_ptpIndex; }
110 };
114 struct PtpCaps_t {
115  int64_t max_ppb;
116  int num_alarm;
119  bool pps;
120  int num_pins;
124  int64_t max_phase_adj;
125 };
134 };
138 struct PtpPin_t {
139  unsigned int index;
144  std::string description;
146  unsigned int channel;
147 };
152 const uint8_t PTP_EXTERN_TS_RISING_EDGE = 1 << 1;
157 const uint8_t PTP_EXTERN_TS_FALLING_EDGE = 1 << 2;
162 const uint8_t PTP_EXTERN_TS_STRICT = 1 << 3;
167 const uint8_t PTP_PERIOD_ONE_SHOT = 1 << 0;
172 const uint8_t PTP_PERIOD_WIDTH = 1 << 1;
177 const uint8_t PTP_PERIOD_PHASE = 1 << 2;
178 
182 struct PtpEvent_t {
183  unsigned int index;
185 };
189 struct PtpSample_t {
192 };
200 };
208 };
216 };
221 {
222  protected:
226  clockid_t m_clkId;
227  bool m_isInit;
228  bool m_freq;
234  BaseClock(clockid_t clkId, bool freq) : m_clkId(clkId), m_isInit(true),
235  m_freq(freq) {}
240  BaseClock() : m_clkId(CLOCK_INVALID), m_isInit(false), m_freq(false) {}
242  public:
253  bool setTime(const Timestamp_t &ts) const;
259  bool offsetClock(int64_t offset) const;
270  bool setFreq(float_freq freq) const;
276  bool setPhase(int64_t offset) const;
277 };
281 class SysClock : public BaseClock
282 {
283  public:
284  SysClock();
285 };
296 class PtpClock : public BaseClock
297 {
298  private:
299  int m_fd, m_ptpIndex;
300  std::string m_device;
301  bool init(const char *device, bool readonly);
302 
303  public:
304  PtpClock() : m_fd(-1), m_ptpIndex(NO_SUCH_PTP) {}
305  ~PtpClock();
312  static bool isCharFile(const std::string &file);
319  bool initUsingDevice(const std::string &device, bool readonly = false);
326  bool initUsingIndex(int ptpIndex, bool readonly = false);
331  bool isInit() const { return m_isInit; }
336  clockid_t clkId() const { return m_clkId; }
341  int getFd() const { return m_fd; }
346  int fileno() const { return m_fd; }
352  int ptpIndex() const { return m_ptpIndex; }
357  const std::string &device() const { return m_device; }
362  const char *device_c() const { return m_device.c_str(); }
367  bool setTimeFromSys() const;
372  bool setTimeToSys() const;
378  bool fetchCaps(PtpCaps_t &caps) const;
386  bool readPin(unsigned int index, PtpPin_t &pin) const;
394  bool writePin(PtpPin_t &pin) const;
403  bool ExternTSEbable(unsigned int index, uint8_t flags) const;
410  bool ExternTSDisable(unsigned int index) const;
415  bool MaskClearAll() const;
422  bool MaskEnable(unsigned int index) const;
428  bool readEvent(PtpEvent_t &event) const;
436  bool readEvents(std::vector<PtpEvent_t> &events, size_t max = 0) const;
446  bool setPinPeriod(unsigned int index, PtpPinPeriodDef_t times,
447  uint8_t flags = 0) const;
455  bool setPtpPpsEvent(bool enable) const;
463  bool samplePtpSys(size_t count, std::vector<PtpSample_t> &samples) const;
472  bool extSamplePtpSys(size_t count, std::vector<PtpSampleExt_t> &samples) const;
473 
481 };
482 
483 __PTPMGMT_NAMESPACE_END
484 #else /* __cplusplus */
485 #include "c/ptp.h"
486 #endif /* __cplusplus */
487 
488 #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:221
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:70
const uint8_t * get() const
Definition: bin.h:85
Network interface information.
Definition: ptp.h:44
int ptpIndex() const
Definition: ptp.h:109
bool isInit() const
Definition: ptp.h:71
const uint8_t * mac_c() const
Definition: ptp.h:96
size_t mac_size() const
Definition: ptp.h:101
const Binary & mac() const
Definition: ptp.h:91
bool initUsingName(const std::string &ifName)
int ifIndex() const
Definition: ptp.h:76
const std::string & ifName() const
Definition: ptp.h:81
bool initUsingIndex(int ifIndex)
const char * ifName_c() const
Definition: ptp.h:86
POSIX dynamic clock id generator.
Definition: ptp.h:297
clockid_t clkId() const
Definition: ptp.h:336
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:341
bool ExternTSDisable(unsigned int index) const
bool setTimeFromSys() const
const char * device_c() const
Definition: ptp.h:362
bool writePin(PtpPin_t &pin) const
bool fetchCaps(PtpCaps_t &caps) const
const std::string & device() const
Definition: ptp.h:357
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:352
int fileno() const
Definition: ptp.h:346
bool setPtpPpsEvent(bool enable) const
bool isInit() const
Definition: ptp.h:331
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:282
const uint8_t PTP_PERIOD_ONE_SHOT
Definition: ptp.h:167
const uint8_t PTP_EXTERN_TS_FALLING_EDGE
Definition: ptp.h:157
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:152
const uint8_t PTP_PERIOD_WIDTH
Definition: ptp.h:172
const uint8_t PTP_EXTERN_TS_STRICT
Definition: ptp.h:162
__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:129
@ PTP_PIN_EXTERNAL_TS
Definition: ptp.h:131
@ PTP_PIN_PERIODIC_OUT
Definition: ptp.h:132
@ PTP_PIN_PHY_SYNC
Definition: ptp.h:133
@ PTP_PIN_UNUSED
Definition: ptp.h:130
const uint8_t PTP_PERIOD_PHASE
Definition: ptp.h:177
Definition: ptp.h:114
bool adjust_phase
Definition: ptp.h:123
int num_alarm
Definition: ptp.h:116
int num_periodic_sig
Definition: ptp.h:118
int64_t max_ppb
Definition: ptp.h:115
int num_pins
Definition: ptp.h:120
bool cross_timestamping
Definition: ptp.h:122
int64_t max_phase_adj
Definition: ptp.h:124
int num_external_channels
Definition: ptp.h:117
bool pps
Definition: ptp.h:119
Definition: ptp.h:182
unsigned int index
Definition: ptp.h:183
Timestamp_t time
Definition: ptp.h:184
Definition: ptp.h:212
Timestamp_t width
Definition: ptp.h:214
Timestamp_t phase
Definition: ptp.h:215
Timestamp_t period
Definition: ptp.h:213
Definition: ptp.h:138
PtpPinFunc_e functional
Definition: ptp.h:145
unsigned int index
Definition: ptp.h:139
std::string description
Definition: ptp.h:144
unsigned int channel
Definition: ptp.h:146
Definition: ptp.h:196
Timestamp_t before
Definition: ptp.h:197
Timestamp_t phcClk
Definition: ptp.h:198
Timestamp_t after
Definition: ptp.h:199
Definition: ptp.h:204
Timestamp_t sysClk
Definition: ptp.h:206
Timestamp_t phcClk
Definition: ptp.h:205
Timestamp_t monoClk
Definition: ptp.h:207
Definition: ptp.h:189
Timestamp_t phcClk
Definition: ptp.h:191
Timestamp_t sysClk
Definition: ptp.h:190
Definition: types.h:416