2025-09-28 23:07:39 -04:00
|
|
|
#include <config.h>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <componentThread.h>
|
2025-09-28 23:26:44 -04:00
|
|
|
#include <callback.h>
|
2025-10-31 08:16:05 -04:00
|
|
|
#include <asynchronousBridge.h>
|
2025-09-28 23:07:39 -04:00
|
|
|
#include <deviceManager/deviceReattacher.h>
|
|
|
|
|
#include <deviceManager/deviceManager.h>
|
|
|
|
|
|
|
|
|
|
namespace smo {
|
|
|
|
|
namespace device {
|
|
|
|
|
|
2025-09-28 23:26:44 -04:00
|
|
|
static void reattachmentCb(AsynchronousLoop& results)
|
|
|
|
|
{
|
|
|
|
|
if (results.nTotal == 0) { return; }
|
|
|
|
|
|
|
|
|
|
std::cout << "DeviceReattacher: Successfully reattached "
|
|
|
|
|
<< results.nSucceeded << " of " << results.nTotal << " devices"
|
|
|
|
|
<< std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-28 23:07:39 -04:00
|
|
|
DeviceReattacher::DeviceReattacher(
|
|
|
|
|
DeviceManager& parent, std::shared_ptr<ComponentThread> ioThread)
|
|
|
|
|
: parent(parent), ioThread(ioThread), shouldContinue(false),
|
|
|
|
|
timer(ioThread->getIoService())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DeviceReattacher::start()
|
|
|
|
|
{
|
2025-09-28 23:35:20 -04:00
|
|
|
shouldContinue.store(true);
|
|
|
|
|
scheduleNextTimeout();
|
2025-09-28 23:07:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DeviceReattacher::stop()
|
|
|
|
|
{
|
2025-09-28 23:35:20 -04:00
|
|
|
shouldContinue.store(false);
|
|
|
|
|
timer.cancel();
|
2025-10-31 08:16:05 -04:00
|
|
|
|
2025-10-31 08:56:38 -04:00
|
|
|
// Set up a timeout bridge using the provided ioThread's io_service
|
|
|
|
|
auto& ioService = ioThread->getIoService();
|
|
|
|
|
boost::asio::deadline_timer timeoutTimer(ioService);
|
|
|
|
|
AsynchronousBridge bridge(ioService);
|
2025-10-31 08:16:05 -04:00
|
|
|
|
|
|
|
|
// Set up the timeout for ~10ms
|
|
|
|
|
timeoutTimer.expires_from_now(boost::posix_time::milliseconds(20));
|
|
|
|
|
timeoutTimer.async_wait(
|
|
|
|
|
[&bridge](const boost::system::error_code& error)
|
|
|
|
|
{
|
|
|
|
|
(void)error;
|
|
|
|
|
|
|
|
|
|
// Always signal complete, whether timeout expired or was cancelled
|
|
|
|
|
bridge.setAsyncOperationComplete();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
bridge.waitForAsyncOperationCompleteOrIoServiceStopped();
|
2025-09-28 23:07:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DeviceReattacher::scheduleNextTimeout()
|
|
|
|
|
{
|
2025-09-28 23:35:20 -04:00
|
|
|
if (!shouldContinue.load()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-09-28 23:07:39 -04:00
|
|
|
|
2025-09-28 23:35:20 -04:00
|
|
|
// Schedule the next timeout using the configured period
|
|
|
|
|
timer.expires_from_now(
|
2025-09-28 23:07:39 -04:00
|
|
|
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:07:39 -04:00
|
|
|
|
2025-09-28 23:35:20 -04:00
|
|
|
if (error)
|
2025-09-28 23:07:39 -04:00
|
|
|
{
|
2025-09-28 23:35:20 -04:00
|
|
|
std::cerr << "DeviceReattacher: Timer error: " << error.message()
|
2025-09-28 23:07:39 -04:00
|
|
|
<< std::endl;
|
2025-09-28 23:35:20 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2025-09-28 23:07:39 -04:00
|
|
|
|
2025-09-28 23:35:20 -04:00
|
|
|
if (!shouldContinue.load()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-09-28 23:07:39 -04:00
|
|
|
|
2025-09-28 23:35:20 -04:00
|
|
|
// Attempt to reattach all unattached devices from the known list
|
|
|
|
|
parent.attachAllUnattachedDevicesFromKnownListReq(
|
2025-09-29 01:59:52 -04:00
|
|
|
{ nullptr, reattachmentCb});
|
2025-09-28 23:26:44 -04:00
|
|
|
|
2025-09-28 23:35:20 -04:00
|
|
|
// Schedule the next timeout
|
|
|
|
|
scheduleNextTimeout();
|
2025-09-28 23:07:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace device
|
|
|
|
|
} // namespace smo
|