F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
RawTime.cpp
Go to the documentation of this file.
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 
16  this->m_handle.m_clock_id = static_cast<clockid_t>(source);
17 }
18 
20  int status = clock_gettime(this->m_handle.m_clock_id, &this->m_handle.m_timespec);
21  if (status != 0) {
22  return errno_to_rawtime_status(errno);
23  }
24  return Status::OP_OK;
25 }
26 
28  // const_cast: RawTimeInterface::getHandle() is non-const; the handle is only read here
29  const PosixRawTimeHandle* other_handle =
30  static_cast<const PosixRawTimeHandle*>(const_cast<Os::RawTime&>(other).getHandle());
31  FW_ASSERT(other_handle != nullptr);
32  // Times read from different clocks share no epoch, so their difference is meaningless
33  if (this->m_handle.m_clock_id != other_handle->m_clock_id) {
34  return Status::INVALID_PARAMS;
35  }
36  timespec t1 = this->m_handle.m_timespec;
37  timespec t2 = other_handle->m_timespec;
38 
39  // Guarantee t1 is the later time to make the calculation below easier
40  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  U32 sec = static_cast<U32>(t1.tv_sec - t2.tv_sec);
47  U32 nanosec = 0;
48  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  nanosec = static_cast<U32>(t1.tv_nsec - t2.tv_nsec);
53  }
54 
55  interval.set(sec, nanosec / 1000);
56  return Status::OP_OK;
57 }
58 
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);
64  return status;
65  }
66  return buffer.serializeFrom(static_cast<U32>(this->m_handle.m_timespec.tv_nsec), mode);
67 }
68 
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);
76  return status;
77  }
78  status = buffer.deserializeTo(nsec, mode);
80  return status;
81  }
82  this->m_handle.m_timespec = {static_cast<time_t>(sec), static_cast<long>(nsec)};
84 }
85 
87  return &this->m_handle;
88 }
89 
90 } // namespace RawTime
91 } // namespace Posix
92 } // namespace Os
Serialization/Deserialization operation was successful.
clockid_t m_clock_id
Clock read by now()
Definition: RawTime.hpp:17
RawTime::Status errno_to_rawtime_status(int errno_input)
Definition: error.cpp:131
Operation succeeded.
Definition: Os.hpp:27
PosixRawTime()=default
constructor using RAWTIME_DEFAULT as the clock source
static const FwSizeType SERIALIZED_SIZE
virtual SerializeStatus serializeFrom(U8 val, Endianness mode=Endianness::BIG)=0
Serialize an 8-bit unsigned integer value.
Status getTimeInterval(const Os::RawTime &other, Fw::TimeInterval &interval) const override
Calculate the time interval between this and another raw time.
Definition: RawTime.cpp:27
void set(U32 seconds, U32 useconds)
SerializeStatus
forward declaration for string
virtual SerializeStatus deserializeTo(U8 &val, Endianness mode=Endianness::BIG)=0
Deserialize an 8-bit unsigned integer value.
Fw::SerializeStatus deserializeFrom(Fw::SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG) override
Deserialize the contents of the RawTimeInterface object from a buffer.
Definition: RawTime.cpp:69
RawTimeSource
Timer source selection for RawTime.
Status now() override
Get the current time.
Definition: RawTime.cpp:19
Fw::SerializeStatus serializeTo(Fw::SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG) const override
Serialize the contents of the RawTimeInterface object into a buffer.
Definition: RawTime.cpp:59
RawTimeHandle * getHandle() override
return the underlying RawTime handle (implementation specific)
Definition: RawTime.cpp:86
Endianness
#define FW_ASSERT(...)
Definition: Assert.hpp:14