GCC Code Coverage Report


Directory: Os/
File: Posix/RawTime.cpp
Date: 2026-09-23 21:11:56
Exec Total Coverage
Lines: 22 44 50.0%
Functions: 4 6 66.7%
Branches: 9 24 37.5%

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 6 PosixRawTime::PosixRawTime(RawTimeSource source) {
16 6 this->m_handle.m_clock_id = static_cast<clockid_t>(source);
17 6 }
18
19 6 PosixRawTime::Status PosixRawTime::now() {
20
1/2
✗ Branch 6 not taken.
✓ Branch 7 taken 6 times.
6 int status = clock_gettime(this->m_handle.m_clock_id, &this->m_handle.m_timespec);
21
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (status != 0) {
22 ✗ return errno_to_rawtime_status(errno);
23 }
24 6 return Status::OP_OK;
25 }
26
27 3 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 3 times.
3 static_cast<const PosixRawTimeHandle*>(const_cast<Os::RawTime&>(other).getHandle());
31 3 FW_ASSERT(other_handle != nullptr);
32 // Times read from different clocks share no epoch, so their difference is meaningless
33
1/2
✗ Branch 6 not taken.
✓ Branch 7 taken 3 times.
3 if (this->m_handle.m_clock_id != other_handle->m_clock_id) {
34 ✗ return Status::INVALID_PARAMS;
35 }
36 3 timespec t1 = this->m_handle.m_timespec;
37 3 timespec t2 = other_handle->m_timespec;
38
39 // Guarantee t1 is the later time to make the calculation below easier
40
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
3 if ((t1.tv_sec < t2.tv_sec) or (t1.tv_sec == t2.tv_sec and t1.tv_nsec < t2.tv_nsec)) {
41 ✗ t1 = other_handle->m_timespec;
42 ✗ t2 = this->m_handle.m_timespec;
43 }
44
45 // Here we have guaranteed that t1 > t2, so there is no underflow
46 3 U32 sec = static_cast<U32>(t1.tv_sec - t2.tv_sec);
47 3 U32 nanosec = 0;
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 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 3 nanosec = static_cast<U32>(t1.tv_nsec - t2.tv_nsec);
53 }
54
55
1/1
✓ Branch 4 taken 3 times.
3 interval.set(sec, nanosec / 1000);
56 3 return Status::OP_OK;
57 }
58
59 ✗ 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 ✗ Fw::SerializeStatus status = buffer.serializeFrom(static_cast<U32>(this->m_handle.m_timespec.tv_sec), mode);
63 ✗ if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
64 ✗ return status;
65 }
66 ✗ return buffer.serializeFrom(static_cast<U32>(this->m_handle.m_timespec.tv_nsec), mode);
67 }
68
69 ✗ 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 ✗ U32 sec = 0;
73 ✗ U32 nsec = 0;
74 ✗ Fw::SerializeStatus status = buffer.deserializeTo(sec, mode);
75 ✗ if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
76 ✗ return status;
77 }
78 ✗ status = buffer.deserializeTo(nsec, mode);
79 ✗ if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
80 ✗ return status;
81 }
82 ✗ this->m_handle.m_timespec = {static_cast<time_t>(sec), static_cast<long>(nsec)};
83 ✗ return Fw::SerializeStatus::FW_SERIALIZE_OK;
84 }
85
86 3 RawTimeHandle* PosixRawTime::getHandle() {
87 3 return &this->m_handle;
88 }
89
90 } // namespace RawTime
91 } // namespace Posix
92 } // namespace Os
93