GCC Code Coverage Report


Directory: ./
File: Posix/RawTime.cpp
Date: 2026-09-03 22:13:09
Exec Total Coverage
Lines: 29 37 78.4%
Functions: 5 5 100.0%
Branches: 12 21 57.1%

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 648 PosixRawTime::Status PosixRawTime::now() {
16 648 int status = clock_gettime(CLOCK_REALTIME, &this->m_handle.m_timespec);
17
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 648 times.
648 if (status != 0) {
18 return errno_to_rawtime_status(errno);
19 }
20 648 return Status::OP_OK;
21 }
22
23 412 PosixRawTime::Status PosixRawTime::getTimeInterval(const Os::RawTime& other, Fw::TimeInterval& interval) const {
24 412 timespec t1 = this->m_handle.m_timespec;
25
1/1
✓ Branch 1 taken 412 times.
412 timespec t2 = static_cast<PosixRawTimeHandle*>(const_cast<Os::RawTime&>(other).getHandle())->m_timespec;
26
27 // Guarantee t1 is the later time to make the calculation below easier
28
3/6
✓ Branch 0 taken 412 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 412 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 412 times.
412 if ((t1.tv_sec < t2.tv_sec) or (t1.tv_sec == t2.tv_sec and t1.tv_nsec < t2.tv_nsec)) {
29 t1 = static_cast<PosixRawTimeHandle*>(const_cast<Os::RawTime&>(other).getHandle())->m_timespec;
30 t2 = this->m_handle.m_timespec;
31 }
32
33 // Here we have guaranteed that t1 > t2, so there is no underflow
34 412 U32 sec = static_cast<U32>(t1.tv_sec - t2.tv_sec);
35 412 U32 nanosec = 0;
36
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 412 times.
412 if (t1.tv_nsec < t2.tv_nsec) {
37 sec -= 1; // subtract nsec carry to seconds
38 nanosec = static_cast<U32>(t1.tv_nsec + (1000000000 - t2.tv_nsec));
39 } else {
40 412 nanosec = static_cast<U32>(t1.tv_nsec - t2.tv_nsec);
41 }
42
43
1/1
✓ Branch 1 taken 412 times.
412 interval.set(sec, nanosec / 1000);
44 412 return Status::OP_OK;
45 }
46
47 413 Fw::SerializeStatus PosixRawTime::serializeTo(Fw::SerialBufferBase& buffer, Fw::Endianness mode) const {
48 static_assert(PosixRawTime::SERIALIZED_SIZE >= 2 * sizeof(U32),
49 "PosixRawTime implementation requires at least 2*sizeof(U32) serialization size");
50 413 Fw::SerializeStatus status = buffer.serializeFrom(static_cast<U32>(this->m_handle.m_timespec.tv_sec), mode);
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 413 times.
413 if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
52 return status;
53 }
54 413 return buffer.serializeFrom(static_cast<U32>(this->m_handle.m_timespec.tv_nsec), mode);
55 }
56
57 388 Fw::SerializeStatus PosixRawTime::deserializeFrom(Fw::SerialBufferBase& buffer, Fw::Endianness mode) {
58 static_assert(PosixRawTime::SERIALIZED_SIZE >= 2 * sizeof(U32),
59 "PosixRawTime implementation requires at least 2*sizeof(U32) serialization size");
60 388 U32 sec = 0;
61 388 U32 nsec = 0;
62
1/1
✓ Branch 1 taken 408 times.
388 Fw::SerializeStatus status = buffer.deserializeTo(sec, mode);
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 408 times.
408 if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
64 return status;
65 }
66
1/1
✓ Branch 1 taken 408 times.
408 status = buffer.deserializeTo(nsec, mode);
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 408 times.
408 if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
68 return status;
69 }
70 408 this->m_handle.m_timespec = {static_cast<time_t>(sec), static_cast<long>(nsec)};
71 408 return Fw::SerializeStatus::FW_SERIALIZE_OK;
72 }
73
74 412 RawTimeHandle* PosixRawTime::getHandle() {
75 412 return &this->m_handle;
76 }
77
78 } // namespace RawTime
79 } // namespace Posix
80 } // namespace Os
81