aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKing Kévin <kingkevin@cuvoodoo.info>2020-01-09 01:01:38 +0100
committerKing Kévin <kingkevin@cuvoodoo.info>2020-01-09 01:01:38 +0100
commit917f77de95ed36ba5e926c4a1a37f360fbe44d68 (patch)
tree41b237acf530870a8de793e1fc4a10b04c27cfbb
parentc2cbed1ed0b262e24dcb75b63de31f38f8b77f20 (diff)
u2_bt: add watchdog
-rw-r--r--u2_bt.c16
1 files changed, 15 insertions, 1 deletions
diff --git a/u2_bt.c b/u2_bt.c
index 9c5fb16..f291daa 100644
--- a/u2_bt.c
+++ b/u2_bt.c
@@ -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