GCC Code Coverage Report


Directory: Os/Posix/
File: RawTime.cpp
Date: 2026-10-14 21:17:27
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 2164 PosixRawTime::PosixRawTime(RawTimeSource source) {
16 2164 this->m_handle.m_clock_id = static_cast<clockid_t>(source);
17 2164 }
18
19 794 PosixRawTime::Status PosixRawTime::now() {
20
1/2
✗ Branch 6 not taken.
✓ Branch 7 taken 794 times.
794 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 793 times.
794 if (status != 0) {
22 1 return errno_to_rawtime_status(errno);
23 }
24 793 return Status::OP_OK;
25 }
26
27 4944 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 4944 times.
4944 static_cast<const PosixRawTimeHandle*>(const_cast<Os::RawTime&>(other).getHandle());
31 4944 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 4940 times.
4944 if (this->m_handle.m_clock_id != other_handle->m_clock_id) {
34 4 return Status::INVALID_PARAMS;
35 }
36 4940 timespec t1 = this->m_handle.m_timespec;
37 4940 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 3571 times.
✓ Branch 1 taken 1369 times.
✓ Branch 2 taken 3571 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 563 times.
✓ Branch 5 taken 3008 times.
4940 if ((t1.tv_sec < t2.tv_sec) or (t1.tv_sec == t2.tv_sec and t1.tv_nsec < t2.tv_nsec)) {
41 1932 t1 = other_handle->m_timespec;
42 1932 t2 = this->m_handle.m_timespec;
43 }
44
45 // Here we have guaranteed that t1 > t2, so there is no underflow
46 4940 U32 sec = static_cast<U32>(t1.tv_sec - t2.tv_sec);
47 4940 U32 nanosec = 0;
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4940 times.
4940 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 4940 nanosec = static_cast<U32>(t1.tv_nsec - t2.tv_nsec);
53 }
54
55
1/1
✓ Branch 4 taken 4940 times.
4940 interval.set(sec, nanosec / 1000);
56 4940 return Status::OP_OK;
57 }
58
59 749 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 749 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 749 times.
749 if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
64 ✗ return status;
65 }
66 749 return buffer.serializeFrom(static_cast<U32>(this->m_handle.m_timespec.tv_nsec), mode);
67 }
68
69 749 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 749 U32 sec = 0;
73 749 U32 nsec = 0;
74
1/1
✓ Branch 7 taken 749 times.
749 Fw::SerializeStatus status = buffer.deserializeTo(sec, mode);
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 749 times.
749 if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
76 ✗ return status;
77 }
78
1/1
✓ Branch 7 taken 749 times.
749 status = buffer.deserializeTo(nsec, mode);
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 749 times.
749 if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
80 ✗ return status;
81 }
82 749 this->m_handle.m_timespec = {static_cast<time_t>(sec), static_cast<long>(nsec)};
83 749 return Fw::SerializeStatus::FW_SERIALIZE_OK;
84 }
85
86 5738 RawTimeHandle* PosixRawTime::getHandle() {
87 5738 return &this->m_handle;
88 }
89
90 } // namespace RawTime
91 } // namespace Posix
92 } // namespace Os
93