Files
salmanoff/smocore/deviceManager/deviceReattacher.cpp
T

88 lines
1.8 KiB
C++
Raw Normal View History

#include <config.h>
#include <iostream>
#include <functional>
#include <componentThread.h>
#include <callback.h>
#include <deviceManager/deviceReattacher.h>
#include <deviceManager/deviceManager.h>
namespace smo {
namespace device {
static void reattachmentCb(AsynchronousLoop& results)
{
if (results.nTotal == 0) { return; }
std::cout << "DeviceReattacher: Successfully reattached "
<< results.nSucceeded << " of " << results.nTotal << " devices"
<< std::endl;
}
DeviceReattacher::DeviceReattacher(
DeviceManager& parent, std::shared_ptr<ComponentThread> ioThread)
: parent(parent), ioThread(ioThread), shouldContinue(false),
timer(ioThread->getIoService())
{
}
void DeviceReattacher::start()
{
2025-11-27 22:52:09 -04:00
shouldContinue = true;
2025-09-28 23:35:20 -04:00
scheduleNextTimeout();
}
void DeviceReattacher::stop()
{
2025-11-27 22:52:09 -04:00
{
SpinLock::Guard lock(shouldContinueLock);
shouldContinue = false;
}
2025-11-27 22:52:09 -04:00
timer.cancel();
}
void DeviceReattacher::scheduleNextTimeout()
{
2025-11-27 22:52:09 -04:00
if (!shouldContinue) {
2025-09-28 23:35:20 -04:00
return;
}
2025-09-28 23:35:20 -04:00
// 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) {
2025-09-28 23:35:20 -04:00
return;
}
2025-09-28 23:35:20 -04:00
if (error)
{
2025-09-28 23:35:20 -04:00
std::cerr << "DeviceReattacher: Timer error: " << error.message()
<< std::endl;
2025-09-28 23:35:20 -04:00
return;
}
2025-11-27 22:52:09 -04:00
SpinLock::Guard lock(shouldContinueLock);
if (!shouldContinue) {
2025-09-28 23:35:20 -04:00
return;
}
2025-09-28 23:35:20 -04:00
// Attempt to reattach all unattached devices from the known list
parent.attachAllUnattachedDevicesFromKnownListReq(
{ nullptr, reattachmentCb});
2025-09-28 23:35:20 -04:00
// Schedule the next timeout
scheduleNextTimeout();
}
} // namespace device
} // namespace smo