2025-09-14 22:17:19 -04:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <asynchronousContinuation.h>
|
|
|
|
|
#include <asynchronousLoop.h>
|
2025-09-27 18:30:09 -04:00
|
|
|
#include <callback.h>
|
2025-09-14 22:17:19 -04:00
|
|
|
#include <component.h>
|
|
|
|
|
#include <componentThread.h>
|
2025-09-29 01:07:32 -04:00
|
|
|
#include <deviceManager/deviceManager.h>
|
2025-09-14 22:17:19 -04:00
|
|
|
#include <mindManager/mindManager.h>
|
|
|
|
|
#include <marionette/marionette.h>
|
|
|
|
|
|
|
|
|
|
namespace smo {
|
|
|
|
|
namespace mrntt {
|
|
|
|
|
|
|
|
|
|
class MarionetteComponent::MrnttLifetimeMgmtOp
|
2025-09-17 16:32:20 -04:00
|
|
|
: public PostedAsynchronousContinuation<mrnttLifetimeMgmtOpCbFn>
|
2025-09-14 22:17:19 -04:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
MrnttLifetimeMgmtOp(
|
|
|
|
|
MarionetteComponent &parent, const std::shared_ptr<ComponentThread> &caller,
|
2025-09-27 18:30:09 -04:00
|
|
|
Callback<mrnttLifetimeMgmtOpCbFn> callback)
|
2025-09-17 16:32:20 -04:00
|
|
|
: PostedAsynchronousContinuation<mrnttLifetimeMgmtOpCbFn>(
|
|
|
|
|
caller, callback),
|
|
|
|
|
parent(parent)
|
2025-09-14 22:17:19 -04:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
MarionetteComponent &parent;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
void initializeReq1_posted(
|
|
|
|
|
[[maybe_unused]] std::shared_ptr<MrnttLifetimeMgmtOp> context
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
auto self = ComponentThread::getSelf();
|
|
|
|
|
if (self->id != ComponentThread::MRNTT)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error(std::string(__func__)
|
|
|
|
|
+ ": Must be executed on Marionette thread");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
smo::mind::globalMind = std::make_shared<Mind>();
|
2025-09-27 18:30:09 -04:00
|
|
|
smo::mind::globalMind->initializeReq({context, std::bind(
|
|
|
|
|
&MrnttLifetimeMgmtOp::initializeReq2,
|
|
|
|
|
this, context, std::placeholders::_1)});
|
2025-09-14 22:17:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void initializeReq2(
|
|
|
|
|
std::shared_ptr<MrnttLifetimeMgmtOp> context,
|
|
|
|
|
bool success
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
if (!success)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << __func__ << ": Failed to initialize globalMind"
|
|
|
|
|
<< std::endl;
|
2025-09-17 16:32:20 -04:00
|
|
|
context->callOriginalCb(false);
|
2025-09-14 22:17:19 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 01:07:32 -04:00
|
|
|
device::DeviceManager::getInstance().initializeDeviceReattacher();
|
2025-09-17 16:32:20 -04:00
|
|
|
context->callOriginalCb(success);
|
2025-09-14 22:17:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void finalizeReq1_posted(
|
|
|
|
|
[[maybe_unused]] std::shared_ptr<MrnttLifetimeMgmtOp> context
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
auto self = ComponentThread::getSelf();
|
|
|
|
|
if (self->id != ComponentThread::MRNTT)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error(std::string(__func__)
|
|
|
|
|
+ ": Must be executed on Marionette thread");
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 01:07:32 -04:00
|
|
|
device::DeviceManager::getInstance().finalizeDeviceReattacher();
|
|
|
|
|
|
2025-09-27 18:30:09 -04:00
|
|
|
smo::mind::globalMind->finalizeReq({context, std::bind(
|
|
|
|
|
&MrnttLifetimeMgmtOp::finalizeReq2,
|
|
|
|
|
this, context, std::placeholders::_1)});
|
2025-09-14 22:17:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void finalizeReq2(
|
|
|
|
|
std::shared_ptr<MrnttLifetimeMgmtOp> context,
|
|
|
|
|
bool success
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
if (!success)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << __func__ << ": globalMind finalization failed"
|
|
|
|
|
<< std::endl;
|
2025-09-17 16:32:20 -04:00
|
|
|
context->callOriginalCb(false);
|
2025-09-14 22:17:19 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 16:32:20 -04:00
|
|
|
context->callOriginalCb(success);
|
2025-09-14 22:17:19 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-27 18:30:09 -04:00
|
|
|
void MarionetteComponent::initializeReq(
|
|
|
|
|
Callback<mrnttLifetimeMgmtOpCbFn> callback)
|
2025-09-14 22:17:19 -04:00
|
|
|
{
|
|
|
|
|
auto mrntt = ComponentThread::getSelf();
|
|
|
|
|
|
|
|
|
|
if (mrntt->id != ComponentThread::MRNTT)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error(std::string(__func__)
|
|
|
|
|
+ ": Must be executed on Marionette thread");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto request = std::make_shared<MrnttLifetimeMgmtOp>(
|
|
|
|
|
*this, mrntt, callback);
|
|
|
|
|
|
|
|
|
|
mrntt->getIoService().post(
|
|
|
|
|
std::bind(
|
|
|
|
|
&MrnttLifetimeMgmtOp::initializeReq1_posted,
|
|
|
|
|
request.get(), request));
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-27 18:30:09 -04:00
|
|
|
void MarionetteComponent::finalizeReq(
|
|
|
|
|
Callback<mrnttLifetimeMgmtOpCbFn> callback)
|
2025-09-14 22:17:19 -04:00
|
|
|
{
|
|
|
|
|
auto mrntt = ComponentThread::getSelf();
|
|
|
|
|
|
|
|
|
|
if (mrntt->id != ComponentThread::MRNTT)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error(std::string(__func__)
|
|
|
|
|
+ ": Must be executed on Marionette thread");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto request = std::make_shared<MrnttLifetimeMgmtOp>(
|
|
|
|
|
*this, mrntt, callback);
|
|
|
|
|
|
|
|
|
|
mrntt->getIoService().post(
|
|
|
|
|
std::bind(
|
|
|
|
|
&MrnttLifetimeMgmtOp::finalizeReq1_posted,
|
|
|
|
|
request.get(), request));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace mrntt
|
|
|
|
|
} // namespace smo
|