F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  PlatformIntType status = clock_gettime(CLOCK_REALTIME, &this->m_handle.m_timespec);
17  if (status != 0) {
18  return errno_to_rawtime_status(errno);
19  }
20  return Status::OP_OK;
21 }
22 
24  timespec t1 = this->m_handle.m_timespec;
25  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  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  U32 sec = static_cast<U32>(t1.tv_sec - t2.tv_sec);
35  U32 nanosec = 0;
36  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  nanosec = static_cast<U32>(t1.tv_nsec - t2.tv_nsec);
41  }
42 
43  interval.set(sec, nanosec / 1000);
44  return Status::OP_OK;
45 }
46 
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.serialize(static_cast<U32>(this->m_handle.m_timespec.tv_sec));
52  return status;
53  }
54  return buffer.serialize(static_cast<U32>(this->m_handle.m_timespec.tv_nsec));
55 }
56 
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.deserialize(sec);
64  return status;
65  }
66  status = buffer.deserialize(nsec);
68  return status;
69  }
70  this->m_handle.m_timespec = {sec, nsec};
72 }
73 
75  return &this->m_handle;
76 }
77 
78 } // namespace RawTime
79 } // namespace Posix
80 } // namespace Os
int PlatformIntType
DefaultTypes.hpp provides fallback defaults for the platform types.
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
void set(U32 seconds, U32 useconds)
Status now() override
Get the current time.
Definition: RawTime.cpp:15
Status getTimeInterval(const Os::RawTime &other, Fw::TimeInterval &interval) const override
Calculate the time interval between this and another raw time.
Definition: RawTime.cpp:23
Fw::SerializeStatus deserialize(Fw::SerializeBufferBase &buffer) override
Deserialize the contents of the RawTimeInterface object from a buffer.
Definition: RawTime.cpp:57
Fw::SerializeStatus serialize(Fw::SerializeBufferBase &buffer) const override
Serialize the contents of the RawTimeInterface object into a buffer.
Definition: RawTime.cpp:47
RawTimeHandle * getHandle() override
return the underlying RawTime handle (implementation specific)
Definition: RawTime.cpp:74
static const FwSizeType SERIALIZED_SIZE
Definition: RawTime.hpp:25
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
@ OP_OK
Operation succeeded.
Definition: Os.hpp:26
RawTime::Status errno_to_rawtime_status(PlatformIntType errno_input)
Definition: error.cpp:131