diff --git a/Makefile b/Makefile index 0f678df..8b6719a 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -# Copyright (C) 2017-2019 Fredrik Öhrström +# Copyright (C) 2017-2020 Fredrik Öhrström # # 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 diff --git a/src/main.cc b/src/main.cc index d440e2a..b9a5043 100644 --- a/src/main.cc +++ b/src/main.cc @@ -266,7 +266,7 @@ unique_ptr createWMBusDeviceFrom(Detected *detected, Configuration *confi break; } case DEVICE_UNKNOWN: - warning("No wmbus device found! Exiting!\n"); + verbose("(main) internal error! cannot create an unknown device! exiting!\n"); if (config->daemon) { // If starting as a daemon, wait a bit so that systemd have time to catch up. sleep(1); @@ -408,7 +408,7 @@ void detectAndConfigureWMBusDevices(Configuration *config, SerialCommunicationMa { if (!printed_warning_) { - info("(main) no wmbus devices detected.\n"); + info("(main) no wmbus device detected, waiting for a device to be plugged in.\n"); printed_warning_ = true; } } @@ -522,11 +522,11 @@ bool start(Configuration *config) if (devices_.size() == 0) { - info("(main) no wmbus devices detected.\n"); + info("(main) no wmbus device detected, waiting for a device to be plugged in.\n"); } - // Every 2 seconds, perform the exact same call again and again. - manager_->startRegularCallback("device_detector", + // Every 2 seconds detect any plugged in or removed wmbus devices. + manager_->startRegularCallback("HOT_PLUG_DETECTOR", 2, [&](){ detectAndConfigureWMBusDevices(config, manager_.get(), &devices_); diff --git a/src/serial.cc b/src/serial.cc index 35cb197..8ca4a2f 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -77,6 +77,7 @@ struct SerialCommunicationManagerImp : public SerialCommunicationManager void listenTo(SerialDevice *sd, function cb); void onDisappear(SerialDevice *sd, function cb); + void expectDevicesToWork(); void stop(); void startEventLoop(); void waitForStop(); @@ -711,6 +712,12 @@ void SerialCommunicationManagerImp::onDisappear(SerialDevice *sd, functionon_disappear_ = cb; } +void SerialCommunicationManagerImp::expectDevicesToWork() +{ + debug("(serial) expecting devices to work\n"); + expect_devices_to_work_ = true; +} + void SerialCommunicationManagerImp::stop() { // Notify the main waitForStop thread that we are stopped! diff --git a/src/serial.h b/src/serial.h index dad9636..08d64f1 100644 --- a/src/serial.h +++ b/src/serial.h @@ -78,6 +78,10 @@ struct SerialCommunicationManager virtual void listenTo(SerialDevice *sd, function cb) = 0; // Invoke cb callback when the serial device has disappeared! virtual void onDisappear(SerialDevice *sd, function cb) = 0; + // Normally the communication mananager runs for ever. + // But if you expect configured devices to work, then + // the manager will exit when there are no working devices. + virtual void expectDevicesToWork() = 0; virtual void stop() = 0; virtual void startEventLoop() = 0; virtual void waitForStop() = 0; diff --git a/src/wmbus.cc b/src/wmbus.cc index 9b21276..8dbb43b 100644 --- a/src/wmbus.cc +++ b/src/wmbus.cc @@ -3342,7 +3342,8 @@ bool Telegram::findFormatBytesFromKnownMeterSignatures(vector *format_byt WMBusCommonImplementation::~WMBusCommonImplementation() { - info("(wmbus) deleted %s\n", toString(type())); + manager_->stopRegularCallback(regular_cb_id_); + debug("(wmbus) deleted %s\n", toString(type())); } WMBusCommonImplementation::WMBusCommonImplementation(WMBusDeviceType t, @@ -3358,7 +3359,8 @@ WMBusCommonImplementation::WMBusCommonImplementation(WMBusDeviceType t, // Invoke the check status once per minute. Unless internal testing, then it is every 2 seconds. int default_timer = isInternalTestingEnabled() ? CHECKSTATUS_TIMER_INTERNAL_TESTING : CHECKSTATUS_TIMER; - manager_->startRegularCallback(toString(t), default_timer, call(this,checkStatus)); + string alarm_id = "CHECK_STATUS "+string(toString(t))+":"+serial_->device(); + regular_cb_id_ = manager_->startRegularCallback(alarm_id, default_timer, call(this,checkStatus)); } WMBusDeviceType WMBusCommonImplementation::type() diff --git a/src/wmbus_im871a.cc b/src/wmbus_im871a.cc index f465894..988de97 100644 --- a/src/wmbus_im871a.cc +++ b/src/wmbus_im871a.cc @@ -97,7 +97,7 @@ unique_ptr openIM871A(string device, SerialCommunicationManager *manager, } WMBusIM871A::WMBusIM871A(unique_ptr serial, SerialCommunicationManager *manager) : - WMBusCommonImplementation(DEVICE_IM871A, manager,std::move(serial)) + WMBusCommonImplementation(DEVICE_IM871A, manager, std::move(serial)) { sem_init(&command_wait_, 0, 0); manager_->listenTo(this->serial(),call(this,processSerialData)); diff --git a/src/wmbus_utils.h b/src/wmbus_utils.h index 4dd9dc1..da99945 100644 --- a/src/wmbus_utils.h +++ b/src/wmbus_utils.h @@ -71,6 +71,8 @@ struct WMBusCommonImplementation : public virtual WMBus bool link_modes_configured_ {}; LinkModeSet link_modes_ {}; + int regular_cb_id_; + unique_ptr serial_; };