AudioManager  7.6.6
Native Application Runtime Environment
CAmWatchdog.cpp
Go to the documentation of this file.
1 
24 #include "CAmWatchdog.h"
25 #include <cassert>
26 #include <cstdlib>
27 #include <stdexcept>
28 #include "audiomanagerconfig.h"
29 #include "CAmDltWrapper.h"
30 #include <systemd/sd-daemon.h>
31 
32 namespace am
33 {
34 
36  TimerCallback(this, &CAmWatchdog::timerCallback), //
37  mpCAmSocketHandler(CAmSocketHandler), //
38  mHandle(0) //
39 {
40  assert(mpCAmSocketHandler);
41 
42 
43  //first retrieve the timeout interval
44 
45  int watchdogTimeout = 0;
46 
47  char* wusec=getenv("WATCHDOG_USEC");
48  if (wusec)
49  watchdogTimeout=atoi(wusec);
50 
51  if (watchdogTimeout > 0)
52  {
53  timespec timeout;
54 
55  //calculate the half cycle as the right interval to trigger the watchdog.
56  timeout.tv_sec = (watchdogTimeout / 2) / 1000000;
57  timeout.tv_nsec = ((watchdogTimeout / 2) % 1000000) * 1000;
58  logInfo("CAmWatchdog::CAmWatchdog setting watchdog timeout:", watchdogTimeout, "us. Notification set to:",
59  (int)timeout.tv_sec, "sec and", (int)timeout.tv_nsec, "ns");
60 
61  //add the timer here
62  if (mpCAmSocketHandler->addTimer(timeout, &TimerCallback, mHandle, NULL))
63  {
64  logError("CAmWatchdog::CAmWatchdog failed to add timer");
65  throw std::runtime_error("CAmWatchdog::CAmWatchdog failed to add timer");
66  }
67  }
68 
69  else
70  {
71  logInfo("CAmWatchdog::CAmWatchdog watchdog timeout was ", watchdogTimeout, " museconds, no watchdog active");
72  }
73 }
74 
76 {
77  //remove the timer again.
78  if (mHandle!=0)
79  mpCAmSocketHandler->removeTimer(mHandle);
80 }
81 
82 void CAmWatchdog::timerCallback(sh_timerHandle_t handle, void* userData)
83 {
84  (void) userData;
85  int error(sd_notify(0, "WATCHDOG=1"));
86  if (error < 0)
87  {
88  logError("CAmWatchdog::timerCallback could not reset watchdog, error ", error);
89  throw std::runtime_error("CAmWatchdog::timerCallback could not reset watchdog");
90  }
91 
92  mpCAmSocketHandler->restartTimer(handle);
93 }
94 
96 {
97  int error(sd_notify(0, "READY=1"));
98  if (error < 0)
99  {
100  logError("CAmWatchdog::startWatchdog could not start watchdog, error ", error);
101  throw std::runtime_error("CAmWatchdog::startWatchdog could not start watchdog");
102  }
103  logInfo("READY=1 was sent to systemd");
104 }
105 
106 }
107 
108 /* namespace am */
A Common-API wrapper class, which loads the common-api runtime and instantiates all necessary objects...
void logInfo(T value, TArgs...args)
logs given values with infolevel with the default context
am_Error_e restartTimer(const sh_timerHandle_t handle)
restarts a timer with the original value
The am::CAmSocketHandler implements a mainloop for the AudioManager.
Implements the watchdog of the AudioManager with the help of systemd.
Definition: CAmWatchdog.h:35
SPDX license identifier: MPL-2.0.
SPDX license identifier: MPL-2.0.
am_Error_e removeTimer(const sh_timerHandle_t handle)
removes a timer from the list of timers
virtual ~CAmWatchdog()
Definition: CAmWatchdog.cpp:75
CAmWatchdog(CAmSocketHandler *CAmSocketHandler)
Definition: CAmWatchdog.cpp:35
void startWatchdog()
starts the watchdog by sending ready to systemD
Definition: CAmWatchdog.cpp:95
void logError(T value, TArgs...args)
logs given values with errorlevel with the default context
TAmShTimerCallBack< CAmWatchdog > TimerCallback
Definition: CAmWatchdog.h:42
void timerCallback(sh_timerHandle_t handle, void *userData)
the watchdog timer callback
Definition: CAmWatchdog.cpp:82
sh_pollHandle_t sh_timerHandle_t
this is a handle for a timer to be used with the SocketHandler
am_Error_e addTimer(const timespec &timeouts, IAmShTimerCallBack *callback, sh_timerHandle_t &handle, void *userData, const bool __attribute__((__unused__)) repeats=false)