Skip to content

Instantly share code, notes, and snippets.

@matthewgream
Created October 14, 2023 11:34
Show Gist options
  • Save matthewgream/9bbde372d6cc72135f7df29f500cf8ec to your computer and use it in GitHub Desktop.
Save matthewgream/9bbde372d6cc72135f7df29f500cf8ec to your computer and use it in GitHub Desktop.
// ------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------------
#include "common.h"
#include "common_config.h"
#include "common_ioplus.h"
#include "sys/time.h"
// ------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------------
#define DELAY_CYCLE 1000 /* ms */
// ------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------------
uint64_t time_ms_64 (void) {
struct timeval te;
gettimeofday (&te, NULL);
return te.tv_sec * 1000LL + te.tv_usec / 1000;
}
bool is_spaced (uint64_t * const last, const uint64_t curr, const uint64_t mill, const bool nozero) {
if ((curr - *last) < mill)
return false;
bool res = true;
if (*last == 0 && nozero == true)
res = false;
*last = curr;
return res;
}
// ------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------------
int main (const int argc, const char * const argv []) {
int ioplus_stack;
int period_reload, period_startup, period_restart, period_notify;
//
config_t cfg = config_open (argc, argv);
if (!config_get_int (cfg, "ioplus_stack", &ioplus_stack))
app_fatal ("config: no ioplus_stack!");
if (!config_get_int (cfg, "watchdog_startup", &period_startup) ||
!config_get_int (cfg, "watchdog_restart", &period_restart) ||
!config_get_int (cfg, "watchdog_notify", &period_notify) ||
!config_get_int (cfg, "watchdog_period", &period_reload))
app_fatal ("config: no watchdog_startup/restart/period/notify!");
config_close (cfg);
//
app_open ("badtuna-watchdog", "ioplus=%d, period(reload/startup/restart)=%u/%u/%u, notify=%u",
ioplus_stack, period_reload, period_startup, period_restart, period_notify);
//
void * dev_ioplus = ioplus_open (ioplus_stack);
watchdog_reload_delay_set (dev_ioplus, (uint16_t) period_reload);
watchdog_startup_delay_set (dev_ioplus, (uint16_t) period_startup);
watchdog_restart_delay_set (dev_ioplus, (uint32_t) period_restart);
//
const int process_rate_notify = period_notify * 60 * 1000;
const int process_rate_watchdog = (period_reload * 1000) / 4;
while (!app_signalled ()) {
static uint64_t process_last_watchdog = 0, process_last_notify = 0;
const uint64_t process_curr = time_ms_64 ();
if (is_spaced (&process_last_watchdog, process_curr, process_rate_watchdog, false))
watchdog_reload (dev_ioplus);
if (is_spaced (&process_last_notify, process_curr, process_rate_notify, true))
app_info ("notify");
app_delay (DELAY_CYCLE);
}
//
ioplus_close (dev_ioplus);
app_close ();
return (EXIT_SUCCESS);
}
// ------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment