Skip to content

Commit 06f0df5

Browse files
committed
Use sw kernel timestamps as fallback to hw timestamps
1 parent 3482162 commit 06f0df5

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

score/TimeSlave/code/gptp/platform/linux/raw_socket.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@ bool RawSocket::Open(const std::string& iface)
134134
<< "RawSocket::Open: Failed to set BPF filter (errno=" << errno << ")";
135135
}
136136

137+
// Enable kernel layer software timestamps as a baseline, so we get at least some timestamp
138+
// even if hw timestamping isn't available or fails to configure. The HW timestamp will come
139+
// in the same control message but different field, so the caller can use it if present and
140+
// fall back to SW timestamp if not.
141+
const int enable = 1;
142+
if (sys_->setsockopt_call(fd, SOL_SOCKET, SO_TIMESTAMPNS, &enable, sizeof(enable)) < 0)
143+
{
144+
score::mw::log::LogWarn(kTimeSlaveAppContext)
145+
<< "RawSocket::Open: Failed to enable SO_TIMESTAMPNS (errno=" << errno << ")";
146+
}
147+
137148
fd_.store(fd, std::memory_order_release);
138149
iface_ = iface;
139150
return true;
@@ -213,7 +224,14 @@ int RawSocket::Recv(std::uint8_t* buf, std::size_t buf_len, ::timespec& hwts, in
213224
continue;
214225
const auto* ts = reinterpret_cast<const ::timespec*>(CMSG_DATA(cm));
215226
if (ts[2].tv_sec != 0 || ts[2].tv_nsec != 0)
227+
{
216228
hwts = ts[2];
229+
break; // Prefer raw HW timestamp if available
230+
}
231+
}
232+
else if (cm->cmsg_type == SO_TIMESTAMPNS)
233+
{
234+
memcpy(&hwts, CMSG_DATA(cm), sizeof(hwts));
217235
}
218236
}
219237
return len;

0 commit comments

Comments
 (0)