libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
phcclock.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_NETWORK
13#include <cstdint>
14#include <promeki/clockdomain.h>
15#include <promeki/error.h>
16#include <promeki/namespace.h>
17#include <promeki/ntptime.h>
18#include <promeki/result.h>
19#include <promeki/string.h>
20#include <promeki/timestamp.h>
21#include <promeki/uniqueptr.h>
22
23PROMEKI_NAMESPACE_BEGIN
24
79class PhcClock {
80 public:
82 PhcClock() = default;
83
85 PhcClock(PhcClock &&other) noexcept;
86 PhcClock &operator=(PhcClock &&other) noexcept;
87
88 PhcClock(const PhcClock &) = delete;
89 PhcClock &operator=(const PhcClock &) = delete;
90
91 ~PhcClock();
92
100 struct Caps {
101 int maxAdjustmentPpb = 0;
102 int numAlarms = 0;
103 int numExtTs = 0;
104 int numPerOut = 0;
105 int numPins = 0;
106 bool ppsSupported = false;
107 bool crossTimestamping = false;
108 bool adjustPhase = false;
109 };
110
121 struct SysOffset {
122 int64_t sysRealtimeNs = 0;
123 int64_t phcNs = 0;
124 int64_t sysRealtimePostNs = 0;
125 };
126
136 static Result<PhcClock> open(const String &path = String("/dev/ptp0"));
137
139 bool isOpen() const { return _fd >= 0; }
140
142 const String &devicePath() const { return _path; }
143
145 int fileDescriptor() const { return _fd; }
146
148 const Caps &caps() const { return _caps; }
149
158 Result<TimeStamp> now() const;
159
167 Result<NtpTime> ntpNow() const;
168
177 Result<SysOffset> sysOffsetPrecise() const;
178
180 int taiUtcOffsetSeconds() const { return _taiUtcOffsetSec; }
181
183 void setTaiUtcOffsetSeconds(int seconds) { _taiUtcOffsetSec = seconds; }
184
194 Error refreshTaiUtcOffset();
195
211 bool isLocked(int64_t toleranceNs = 1'000'000LL) const;
212
214 void close();
215
238 Error bindAsDomain(const ClockDomain::ID &domain = ClockDomain::Ptp);
239
247 void unbindDomain(const ClockDomain::ID &domain = ClockDomain::Ptp);
248
263 static int systemTaiUtcOffsetSeconds();
264
285 static uint64_t utcNsToTaiNs(int64_t utcNs);
286
287 private:
288 explicit PhcClock(int fd, String path, Caps caps, int taiUtcSec)
289 : _fd(fd), _path(std::move(path)), _caps(caps),
290 _taiUtcOffsetSec(taiUtcSec) {}
291
292 int _fd = -1;
293 String _path;
294 Caps _caps;
295 int _taiUtcOffsetSec = 37;
296};
297
298PROMEKI_NAMESPACE_END
299
300#endif // PROMEKI_ENABLE_NETWORK