F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
LinuxTimerFd.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title LinuxTimerImpl.cpp
3 // \author tim
4 // \brief cpp file for LinuxTimer component implementation class
5 //
6 // \copyright
7 // Copyright 2009-2015, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
13 #include <Fw/Logger/Logger.hpp>
15 #include <Fw/FPrimeBasicTypes.hpp>
16 #include <sys/timerfd.h>
17 #include <unistd.h>
18 #include <cerrno>
19 #include <cstring>
20 
21 namespace Svc {
22 
24  int fd;
25  struct itimerspec itval;
26 
27  /* Create the timer */
28  fd = timerfd_create (CLOCK_MONOTONIC, 0);
29  const FwSizeType interval_secs = interval/1000;
30  FW_ASSERT(static_cast<FwSizeType>(std::numeric_limits<I32>::max()) >= interval_secs,
31  static_cast<FwAssertArgType>(interval));
32  itval.it_interval.tv_sec = static_cast<I32>(interval_secs);
33  itval.it_interval.tv_nsec = static_cast<I32>((interval*1000000)%1000000000);
34  itval.it_value.tv_sec = static_cast<I32>(interval_secs);
35  itval.it_value.tv_nsec = static_cast<I32>((interval*1000000)%1000000000);
36 
37  timerfd_settime (fd, 0, &itval, nullptr);
38 
39  while (true) {
40  unsigned long long missed;
41  int ret = static_cast<int>(read (fd, &missed, sizeof (missed)));
42  if (-1 == ret) {
43  Fw::Logger::log("timer read error: %s\n", strerror(errno));
44  }
45  this->m_mutex.lock();
46  bool quit = this->m_quit;
47  this->m_mutex.unLock();
48  if (quit) {
49  itval.it_interval.tv_sec = 0;
50  itval.it_interval.tv_nsec = 0;
51  itval.it_value.tv_sec = 0;
52  itval.it_value.tv_nsec = 0;
53 
54  timerfd_settime (fd, 0, &itval, nullptr);
55  return;
56  }
57  this->m_rawTime.now();
58  this->CycleOut_out(0,this->m_rawTime);
59  }
60  }
61 
62 } // end namespace Svc
PlatformSizeType FwSizeType
void unLock()
unlock the mutex and assert success
Definition: Mutex.cpp:40
static void log(const char *format,...)
log a formated string with supplied arguments
Definition: Logger.cpp:21
Status now() override
Get the current time.
Definition: RawTime.cpp:36
void CycleOut_out(FwIndexType portNum, Os::RawTime &cycleStart)
Invoke output port CycleOut.
void quit()
Quit timer.
void startTimer(FwSizeType interval)
Start timer.
RateGroupDivider component implementation.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
void lock()
lock the mutex and assert success
Definition: Mutex.cpp:34