From 93103aa8d4916f476663411f61969fcc6b1a1eff Mon Sep 17 00:00:00 2001 From: Hayodea Hekol Date: Sun, 28 Sep 2025 23:07:39 -0400 Subject: [PATCH] DevMgr: Add DeviceReattacher daemon plumbing This is the plumbing for a periodic polling thread that tries to reattach DA specs that failed to attach in the startup phase. --- CMakeLists.txt | 8 +++ include/config.h.in | 3 + smocore/CMakeLists.txt | 1 + smocore/deviceManager/deviceReattacher.cpp | 68 +++++++++++++++++++ .../include/deviceManager/deviceReattacher.h | 44 ++++++++++++ 5 files changed, 124 insertions(+) create mode 100644 smocore/deviceManager/deviceReattacher.cpp create mode 100644 smocore/include/deviceManager/deviceReattacher.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5554d68..3b5c8b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,14 @@ if(NOT MIND_VOSCILLATOR_PERIOD_MS GREATER 0) endif() math(EXPR MIND_VOSCILLATOR_FREQ_MS "1000 / ${MIND_VOSCILLATOR_PERIOD_MS}") +# Device manager reattacher configuration +set(MRNTT_DEVMGR_REATTACHER_PERIOD_MS 1500 + CACHE STRING "Device manager reattacher period (ms)") +if(NOT MRNTT_DEVMGR_REATTACHER_PERIOD_MS GREATER 0) + message(FATAL_ERROR + "MRNTT_DEVMGR_REATTACHER_PERIOD_MS must be a positive integer > 0") +endif() + # World thread configuration option(WORLD_USE_BODY_THREAD "Use body thread for world component instead of separate world thread" OFF) diff --git a/include/config.h.in b/include/config.h.in index 3e01e3b..72ed66e 100644 --- a/include/config.h.in +++ b/include/config.h.in @@ -9,6 +9,9 @@ #define CONFIG_MIND_VOSCILLATOR_PERIOD_MS @MIND_VOSCILLATOR_PERIOD_MS@ #define CONFIG_MIND_VOSCILLATOR_FREQ_MS @MIND_VOSCILLATOR_FREQ_MS@ +/* Device manager reattacher configuration */ +#define CONFIG_MRNTT_DEVMGR_REATTACHER_PERIOD_MS @MRNTT_DEVMGR_REATTACHER_PERIOD_MS@ + /* World thread configuration */ #cmakedefine WORLD_USE_BODY_THREAD diff --git a/smocore/CMakeLists.txt b/smocore/CMakeLists.txt index b2047c4..b3a9755 100644 --- a/smocore/CMakeLists.txt +++ b/smocore/CMakeLists.txt @@ -25,6 +25,7 @@ add_library(smocore STATIC # DeviceManager deviceManager/deviceManager.cpp + deviceManager/deviceReattacher.cpp deviceManager/deviceAttachmentPipeSpecParser.cpp ${LEX_OUTPUT} ${YACC_OUTPUT} diff --git a/smocore/deviceManager/deviceReattacher.cpp b/smocore/deviceManager/deviceReattacher.cpp new file mode 100644 index 0000000..7c8f970 --- /dev/null +++ b/smocore/deviceManager/deviceReattacher.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include + +namespace smo { +namespace device { + +DeviceReattacher::DeviceReattacher( + DeviceManager& parent, std::shared_ptr ioThread) +: parent(parent), ioThread(ioThread), shouldContinue(false), +timer(ioThread->getIoService()) +{ +} + +void DeviceReattacher::start() +{ + shouldContinue.store(true); + scheduleNextTimeout(); +} + +void DeviceReattacher::stop() +{ + shouldContinue.store(false); + timer.cancel(); +} + +void DeviceReattacher::scheduleNextTimeout() +{ + if (!shouldContinue.load()) { + return; + } + + // Schedule the next timeout using the configured period + timer.expires_from_now( + boost::posix_time::milliseconds( + CONFIG_MRNTT_DEVMGR_REATTACHER_PERIOD_MS)); + + timer.async_wait( + std::bind(&DeviceReattacher::onTimeout, this, std::placeholders::_1)); +} + +void DeviceReattacher::onTimeout(const boost::system::error_code& error) +{ + // Timer was cancelled, which is expected when stopping + if (error == boost::asio::error::operation_aborted) { + return; + } + + if (error) + { + std::cerr << "DeviceReattacher: Timer error: " << error.message() + << std::endl; + return; + } + + if (!shouldContinue.load()) { + return; + } + + // Schedule the next timeout + scheduleNextTimeout(); +} + +} // namespace device +} // namespace smo diff --git a/smocore/include/deviceManager/deviceReattacher.h b/smocore/include/deviceManager/deviceReattacher.h new file mode 100644 index 0000000..c5d2579 --- /dev/null +++ b/smocore/include/deviceManager/deviceReattacher.h @@ -0,0 +1,44 @@ +#ifndef DEVICEREATTACHER_H +#define DEVICEREATTACHER_H + +#include +#include +#include + +namespace smo { + +class ComponentThread; + +namespace device { + +class DeviceManager; + +class DeviceReattacher +{ +public: + DeviceReattacher( + DeviceManager& parent, std::shared_ptr ioThread); + ~DeviceReattacher() = default; + + // Non-copyable + DeviceReattacher(const DeviceReattacher&) = delete; + DeviceReattacher& operator=(const DeviceReattacher&) = delete; + + // Control methods + void start(); + void stop(); + +private: + void scheduleNextTimeout(); + void onTimeout(const boost::system::error_code& error); + + DeviceManager &parent; + std::shared_ptr ioThread; + std::atomic shouldContinue; + boost::asio::deadline_timer timer; +}; + +} // namespace device +} // namespace smo + +#endif // DEVICEREATTACHER_H