GCC Code Coverage Report


Directory: Os/Posix/
File: RawTime.cpp
Date: 2026-09-03 21:16:08
Exec Total Coverage
Lines: 31 37 83.8%
Functions: 5 5 100.0%
Branches: 16 23 69.6%

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 724 PosixRawTime::Status PosixRawTime::now() {
16
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 724 times.
724 int status = clock_gettime(CLOCK_REALTIME, &this->m_handle.m_timespec);
17
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 724 times.
724 if (status != 0) {
18 return errno_to_rawtime_status(errno);
19 }
20 724 return Status::OP_OK;
21 }
22
23 5026 PosixRawTime::Status PosixRawTime::getTimeInterval(const Os::RawTime& other, Fw::TimeInterval& interval) const {
24 5026 timespec t1 = this->m_handle.m_timespec;
25
1/1
✓ Branch 5 taken 5026 times.
5026 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
5/6
✓ Branch 0 taken 3581 times.
✓ Branch 1 taken 1445 times.
✓ Branch 2 taken 3581 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 613 times.
✓ Branch 5 taken 2968 times.
5026 if ((t1.tv_sec < t2.tv_sec) or (t1.tv_sec == t2.tv_sec and t1.tv_nsec < t2.tv_nsec)) {
29
1/1
✓ Branch 4 taken 2058 times.
2058 t1 = static_cast<PosixRawTimeHandle*>(const_cast<Os::RawTime&>(other).getHandle())->m_timespec;
30 2058 t2 = this->m_handle.m_timespec;
31 }
32
33 // Here we have guaranteed that t1 > t2, so there is no underflow
34 5026 U32 sec = static_cast<U32>(t1.tv_sec - t2.tv_sec);
35 5026 U32 nanosec = 0;
36
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5026 times.
5026 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 5026 nanosec = static_cast<U32>(t1.tv_nsec - t2.tv_nsec);
41 }
42
43
1/1
✓ Branch 4 taken 5026 times.
5026 interval.set(sec, nanosec / 1000);
44 10052 return Status::OP_OK;
45 }
46
47 701 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 701 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 701 times.
701 if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
52 return status;
53 }
54 701 return buffer.serializeFrom(static_cast<U32>(this->m_handle.m_timespec.tv_nsec), mode);
55 }
56
57 701 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 701 U32 sec = 0;
61 701 U32 nsec = 0;
62
1/1
✓ Branch 7 taken 701 times.
701 Fw::SerializeStatus status = buffer.deserializeTo(sec, mode);
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 701 times.
701 if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
64 return status;
65 }
66
1/1
✓ Branch 7 taken 701 times.
701 status = buffer.deserializeTo(nsec, mode);
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 701 times.
701 if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
68 return status;
69 }
70 701 this->m_handle.m_timespec = {static_cast<time_t>(sec), static_cast<long>(nsec)};
71 701 return Fw::SerializeStatus::FW_SERIALIZE_OK;
72 }
73
74 7808 RawTimeHandle* PosixRawTime::getHandle() {
75 7808 return &this->m_handle;
76 }
77
78 } // namespace RawTime
79 } // namespace Posix
80 } // namespace Os
81