#include #include #include #include #include #include #include #include namespace smo { namespace device { DeviceReattacher::DeviceReattacher( DeviceManager& parent, std::shared_ptr ioThread) : parent(parent), ioThread(ioThread), daemonTimer(ioThread->getIoContext()) { /** EXPLANATION: * deviceReattacherCDaemon is a dynamic posting non-viral coroutine: start() * passes ExplicitPostTarget{ioThread->getIoContext()} so the daemon body * always runs on ioThread. daemonTimer is reused each loop iteration. */ } sscl::co::DynamicNonViralPostingInvoker DeviceReattacher::deviceReattacherCDaemon( [[maybe_unused]] sscl::co::ExplicitPostTarget postTarget, [[maybe_unused]] std::exception_ptr &exceptionPtr, [[maybe_unused]] std::function callback, sscl::SyncCancelerForAsyncWork &canceler) { boost::asio::io_context &timerIoContext = sscl::ComponentThread::getSelf()->getIoContext(); const auto periodMs = boost::posix_time::milliseconds( CONFIG_MRNTT_DEVMGR_REATTACHER_PERIOD_MS); while (!canceler.isCancellationRequested()) { const bool expiredNormally = co_await adapters::boostAsio::getDeadlineTimerAReqAwaiter( timerIoContext, daemonTimer, periodMs); if (!expiredNormally) { break; } co_await parent.attachAllUnattachedDevicesFromKnownListCReq(); } co_return; } void DeviceReattacher::start() { taskNursery.openAdmission(); taskNursery.launch( [this](sscl::co::NonViralTaskNursery::Slot::Lease &lease) { return deviceReattacherCDaemon( sscl::co::ExplicitPostTarget{ioThread->getIoContext()}, lease.getExceptionStorage(), lease.getCallerLambda(), lease.getSyncCanceler()); }, [](std::exception_ptr &exceptionPtr) { sscl::co::NonViralCompletion nvc(exceptionPtr); if (nvc.hasException()) { try { nvc.checkAndRethrowException(); } catch (const std::exception &e) { std::cerr << "DeviceReattacher: " << e.what() << std::endl; } } }); } void DeviceReattacher::stop() { daemonTimer.cancel(); taskNursery.requestCancelOnAll(); taskNursery.closeAdmission(); taskNursery.syncAwaitAllSettlements(ioThread->getIoContext()); } } // namespace device } // namespace smo