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