GCC Code Coverage Report


Directory: ./
File: Os/Posix/RawTime.cpp
Date: 2026-10-14 21:15:02
Exec Total Coverage
Lines: 41 44 93.2%
Functions: 6 6 100.0%
Branches: 20 24 83.3%

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 101467 PosixRawTime::PosixRawTime(RawTimeSource source) {
16 101467 this->m_handle.m_clock_id = static_cast<clockid_t>(source);
17 101467 }
18
19 1189 PosixRawTime::Status PosixRawTime::now() {
20
1/2
✗ Branch 6 not taken.
✓ Branch 7 taken 1189 times.
1189 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 1188 times.
1189 if (status != 0) {
22 1 return errno_to_rawtime_status(errno);
23 }
24 1188 return Status::OP_OK;
25 }
26
27 5193 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 5193 times.
5193 static_cast<const PosixRawTimeHandle*>(const_cast<Os::RawTime&>(other).getHandle());
31 5193 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 5189 times.
5193 if (this->m_handle.m_clock_id != other_handle->m_clock_id) {
34 4 return Status::INVALID_PARAMS;
35 }
36 5189 timespec t1 = this->m_handle.m_timespec;
37 5189 timespec t2 = other_handle->m_timespec;
38
39 // Guarantee t1 is the later time to make the calculation below easier
40
6/6
✓ Branch 0 taken 3764 times.
✓ Branch 1 taken 1425 times.
✓ Branch 2 taken 3738 times.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 601 times.
✓ Branch 5 taken 3137 times.
5189 if ((t1.tv_sec < t2.tv_sec) or (t1.tv_sec == t2.tv_sec and t1.tv_nsec < t2.tv_nsec)) {
41 2026 t1 = other_handle->m_timespec;
42 2026 t2 = this->m_handle.m_timespec;
43 }
44
45 // Here we have guaranteed that t1 > t2, so there is no underflow
46 5189 U32 sec = static_cast<U32>(t1.tv_sec - t2.tv_sec);
47 5189 U32 nanosec = 0;
48
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5187 times.
5189 if (t1.tv_nsec < t2.tv_nsec) {
49 2 sec -= 1; // subtract nsec carry to seconds
50 2 nanosec = static_cast<U32>(t1.tv_nsec + (1000000000 - t2.tv_nsec));
51 } else {
52 5187 nanosec = static_cast<U32>(t1.tv_nsec - t2.tv_nsec);
53 }
54
55
1/1
✓ Branch 4 taken 5189 times.
5189 interval.set(sec, nanosec / 1000);
56 5189 return Status::OP_OK;
57 }
58
59 765 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 765 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 765 times.
765 if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
64 ✗ return status;
65 }
66 765 return buffer.serializeFrom(static_cast<U32>(this->m_handle.m_timespec.tv_nsec), mode);
67 }
68
69 730 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 730 U32 sec = 0;
73 730 U32 nsec = 0;
74
1/1
✓ Branch 7 taken 730 times.
730 Fw::SerializeStatus status = buffer.deserializeTo(sec, mode);
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 730 times.
730 if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
76 ✗ return status;
77 }
78
1/1
✓ Branch 7 taken 730 times.
730 status = buffer.deserializeTo(nsec, mode);
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 730 times.
730 if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
80 ✗ return status;
81 }
82 730 this->m_handle.m_timespec = {static_cast<time_t>(sec), static_cast<long>(nsec)};
83 730 return Fw::SerializeStatus::FW_SERIALIZE_OK;
84 }
85
86 5987 RawTimeHandle* PosixRawTime::getHandle() {
87 5987 return &this->m_handle;
88 }
89
90 } // namespace RawTime
91 } // namespace Posix
92 } // namespace Os
93