#include <time.h>
#include <getopt.h>
#include <syslog.h>
-#include <pwd.h>
-#include <grp.h>
+#include <signal.h>
#include <sys/stat.h>
#include <libusb-1.0/libusb.h>
#include <rrd.h>
#define TIME_OFFSET 946681200 // Timestamp of 2000-01-01 00:00:00
+static int data_point_counter;
+
static void cooked_point(time_t t, int id, double val, char *unit, int q)
{
- struct tm tm;
- localtime_r(&t, &tm);
-
- char tbuf[64];
- strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", &tm);
-
- if (debug_raw_data)
+ if (debug_raw_data) {
+ struct tm tm;
+ localtime_r(&t, &tm);
+ char tbuf[64];
+ strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", &tm);
printf("== %s id=%d val=%.3f unit=%s q=%d\n", tbuf, id, val, unit, q);
+ }
+ data_point_counter++;
rrd_point(t, id, val);
}
libusb_device *dev = devlist[i];
if (!libusb_get_device_descriptor(dev, &desc)) {
if (desc.idVendor == 0x0451 && desc.idProduct == 0x3211) {
- log_info("Arexx USB receiver found at usb%d.%d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
+ log_info("Arexx data logger found at usb%d.%d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
int err;
if (err = libusb_open(dev, &devh)) {
log_error("libusb_open() failed: error %d", err);
/*** Main ***/
+static volatile sig_atomic_t want_shutdown;
+
+static void sigterm_handler(int sig __attribute__((unused)))
+{
+ want_shutdown = 1;
+}
+
static const struct option long_options[] = {
{ "debug", 0, NULL, 'd' },
{ "log-packets", 0, NULL, 'p' },
use_syslog = 1;
}
+ struct sigaction sa = { .sa_handler = sigterm_handler };
+ sigaction(SIGTERM, &sa, NULL);
+ sigaction(SIGINT, &sa, NULL);
+
+ sigset_t term_sigs;
+ sigemptyset(&term_sigs);
+ sigaddset(&term_sigs, SIGTERM);
+ sigaddset(&term_sigs, SIGINT);
+ sigprocmask(SIG_BLOCK, &term_sigs, NULL);
+
int inited = 0;
- for (;;) {
+ while (!want_shutdown) {
if (!find_device()) {
if (!inited) {
inited = 1;
- log_error("Receiver not connected, waiting until it appears");
+ log_error("Data logger not connected, waiting until it appears");
}
sleep(30);
continue;
}
- inited = 1;
-
- int sync_in = 0;
- for (;;) {
- if (!sync_in) {
- log_info("Synchronizing receiver time");
+ log_info("Listening");
+
+ time_t last_sync = 0;
+ time_t last_show = 0;
+ int want_stats = 0;
+ int want_sleep = 0;
+ data_point_counter = 0;
+ while (!want_shutdown) {
+ time_t now = time(NULL);
+ if (now > last_sync + 900) {
+ log_info("Synchronizing data logger time");
set_clock();
- sync_in = 200;
+ last_sync = now;
}
+ if (want_stats && now > last_show + 300) {
+ log_info("Stats: received %d data points", data_point_counter);
+ data_point_counter = 0;
+ last_show = now;
+ }
+
byte req[64], reply[64];
memset(req, 0, sizeof(req));
req[0] = 3;
err = send_and_receive(req, reply);
if (err < 0)
break;
+ want_sleep = 1;
if (err > 0 && parse_packet(reply))
- continue;
+ want_sleep = 0;
+ want_stats = 1;
+ sigprocmask(SIG_UNBLOCK, &term_sigs, NULL);
sleep(4);
- sync_in--;
+ sigprocmask(SIG_BLOCK, &term_sigs, NULL);
}
- log_info("Disconnecting receiver");
+ log_info("Disconnecting data logger");
release_device();
+ inited = 0;
}
+ log_info("Terminated");
return 0;
}