u2_bt: add watchdog

This commit is contained in:
King Kévin 2020-01-09 01:01:38 +01:00
parent c2cbed1ed0
commit 917f77de95
1 changed files with 15 additions and 1 deletions

16
u2_bt.c
View File

@ -1,4 +1,4 @@
/* Copyright 2019 King Kévin <kingkevin@cuvoodoo.info>
/* Copyright 2019-2020 King Kévin <kingkevin@cuvoodoo.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -31,6 +31,7 @@
#include <stdarg.h>
#include <unistd.h>
#include <assert.h>
#include <signal.h>
#include <dbus/dbus.h>
#include <sys/select.h>
#include <sys/time.h>
@ -72,6 +73,15 @@ static void die(const char* format, ...)
exit(1);
}
/* watchdog called after alarm timeout
* @param[in] sig alarm signal
*/
static void watchdog(int sig)
{
(void)sig; // argument not used
die("exiting due to inactivity\n");
}
/* get all D-Bus object paths with matching interface
* @param[in] connection D-Bus connection
* @param[in] bus_name bus to get objects from
@ -411,6 +421,9 @@ int main(int argc, char* argv[])
}
}
signal(SIGALRM, watchdog); // setup watchdog
alarm(30); // start watchdog (in case connecting to the U2 gets stuck)
// UDP socket to send data to influxdb
int influxdb_fd = 0;
struct addrinfo* res = 0;
@ -794,6 +807,7 @@ int main(int argc, char* argv[])
printf_debug("energy: %.04f Wh\n", fvalue);
printf_debug("group: %u\n", packet[18] + 1);
} else if (0xfe == packet[0] && packet[1] < 6 && packet[18] < 6 && 0x00 == packet[19]) { // measurement packet (use group limit and last byte to somehow detect transmission errors)
alarm(10); // restart watchdog
u2_values.messages |= (1 << packet[1]);
if (u2_values.group != (packet[18] + 1)) { // new group
u2_values.messages = 0; // remember not all message has been received