GCC Code Coverage Report


Directory: Os/Posix/
File: RawTime.cpp
Date: 2026-09-23 21:12:59
Exec Total Coverage
Lines: 39 44 88.6%
Functions: 6 6 100.0%
Branches: 18 24 75.0%

Line Branch Exec Source
1 // ======================================================================
2 // \title Os/Posix/RawTime.cpp
3 // \brief Posix implementation for Os::RawTime
4 // ======================================================================
5 #include <Fw/Logger/Logger.hpp>
6 #include <Fw/Types/Assert.hpp>
7 #include <Os/Posix/RawTime.hpp>
8 #include <Os/Posix/error.hpp>
9 #include <cerrno>
10
11 namespace Os {
12 namespace Posix {
13 namespace RawTime {
14
15 2249 PosixRawTime::PosixRawTime(RawTimeSource source) {
16 2249 this->m_handle.m_clock_id = static_cast<clockid_t>(source);
17 2249 }
18
19 729 PosixRawTime::Status PosixRawTime::now() {
20
1/2
✗ Branch 6 not taken.
✓ Branch 7 taken 729 times.
729 int status = clock_gettime(this->m_handle.m_clock_id, &this->m_handle.m_timespec);
21
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 728 times.
729 if (status != 0) {
22 1 return errno_to_rawtime_status(errno);
23 }
24 728 return Status::OP_OK;
25 }
26
27 5089 PosixRawTime::Status PosixRawTime::getTimeInterval(const Os::RawTime& other, Fw::TimeInterval& interval) const {
28 // const_cast: RawTimeInterface::getHandle() is non-const; the handle is only read here
29 const PosixRawTimeHandle* other_handle =
30
1/1
✓ Branch 4 taken 5089 times.
5089 static_cast<const PosixRawTimeHandle*>(const_cast<Os::RawTime&>(other).getHandle());
31 5089 FW_ASSERT(other_handle != nullptr);
32 // Times read from different clocks share no epoch, so their difference is meaningless
33
2/2
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 5085 times.
5089 if (this->m_handle.m_clock_id != other_handle->m_clock_id) {
34 4 return Status::INVALID_PARAMS;
35 }
36 5085 timespec t1 = this->m_handle.m_timespec;
37 5085 timespec t2 = other_handle->m_timespec;
38
39 // Guarantee t1 is the later time to make the calculation below easier
40
5/6
✓ Branch 0 taken 3615 times.
✓ Branch 1 taken 1470 times.
✓ Branch 2 taken 3615 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 588 times.
✓ Branch 5 taken 3027 times.
5085 if ((t1.tv_sec < t2.tv_sec) or (t1.tv_sec == t2.tv_sec and t1.tv_nsec < t2.tv_nsec)) {
41 2058 t1 = other_handle->m_timespec;
42 2058 t2 = this->m_handle.m_timespec;
43 }
44
45 // Here we have guaranteed that t1 > t2, so there is no underflow
46 5085 U32 sec = static_cast<U32>(t1.tv_sec - t2.tv_sec);
47 5085 U32 nanosec = 0;
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5085 times.
5085 if (t1.tv_nsec < t2.tv_nsec) {
49 ✗ sec -= 1; // subtract nsec carry to seconds
50 ✗ nanosec = static_cast<U32>(t1.tv_nsec + (1000000000 - t2.tv_nsec));
51 } else {
52 5085 nanosec = static_cast<U32>(t1.tv_nsec - t2.tv_nsec);
53 }
54
55
1/1
✓ Branch 4 taken 5085 times.
5085 interval.set(sec, nanosec / 1000);
56 5085 return Status::OP_OK;
57 }
58
59 733 Fw::SerializeStatus PosixRawTime::serializeTo(Fw::SerialBufferBase& buffer, Fw::Endianness mode) const {
60 static_assert(PosixRawTime::SERIALIZED_SIZE >= 2 * sizeof(U32),
61 "PosixRawTime implementation requires at least 2*sizeof(U32) serialization size");
62 733 Fw::SerializeStatus status = buffer.serializeFrom(static_cast<U32>(this->m_handle.m_timespec.tv_sec), mode);
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 733 times.
733 if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
64 ✗ return status;
65 }
66 733 return buffer.serializeFrom(static_cast<U32>(this->m_handle.m_timespec.tv_nsec), mode);
67 }
68
69 733 Fw::SerializeStatus PosixRawTime::deserializeFrom(Fw::SerialBufferBase& buffer, Fw::Endianness mode) {
70 static_assert(PosixRawTime::SERIALIZED_SIZE >= 2 * sizeof(U32),
71 "PosixRawTime implementation requires at least 2*sizeof(U32) serialization size");
72 733 U32 sec = 0;
73 733 U32 nsec = 0;
74
1/1
✓ Branch 7 taken 733 times.
733 Fw::SerializeStatus status = buffer.deserializeTo(sec, mode);
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 733 times.
733 if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
76 ✗ return status;
77 }
78
1/1
✓ Branch 7 taken 733 times.
733 status = buffer.deserializeTo(nsec, mode);
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 733 times.
733 if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
80 ✗ return status;
81 }
82 733 this->m_handle.m_timespec = {static_cast<time_t>(sec), static_cast<long>(nsec)};
83 733 return Fw::SerializeStatus::FW_SERIALIZE_OK;
84 }
85
86 5818 RawTimeHandle* PosixRawTime::getHandle() {
87 5818 return &this->m_handle;
88 }
89
90 } // namespace RawTime
91 } // namespace Posix
92 } // namespace Os
93