DevMgr: implement removeDeviceAttachmentSpecReq
This reverts all state changes made by newDeviceAttachmentSpecInd.
This commit is contained in:
@@ -95,6 +95,104 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class DeviceManager::RemoveDeviceAttachmentSpecReq
|
||||
: public PostedAsynchronousContinuation<removeDeviceAttachmentSpecReqCbFn>
|
||||
{
|
||||
public:
|
||||
RemoveDeviceAttachmentSpecReq(
|
||||
const std::shared_ptr<DeviceAttachmentSpec> &s,
|
||||
const std::shared_ptr<ComponentThread> &caller,
|
||||
Callback<removeDeviceAttachmentSpecReqCbFn> cb)
|
||||
: PostedAsynchronousContinuation<removeDeviceAttachmentSpecReqCbFn>(
|
||||
caller, cb),
|
||||
spec(s)
|
||||
{}
|
||||
|
||||
public:
|
||||
void removeDeviceAttachmentSpecReq1_posted(
|
||||
[[maybe_unused]] std::shared_ptr<RemoveDeviceAttachmentSpecReq> context
|
||||
)
|
||||
{
|
||||
// Call detachSenseDeviceReq first - only clean up metadata if this succeeds
|
||||
DeviceManager::getInstance().detachSenseDeviceReq(
|
||||
spec,
|
||||
{context, std::bind(
|
||||
&RemoveDeviceAttachmentSpecReq::removeDeviceAttachmentSpecReq2,
|
||||
context.get(), context,
|
||||
std::placeholders::_1, std::placeholders::_2)});
|
||||
}
|
||||
|
||||
void removeDeviceAttachmentSpecReq2(
|
||||
[[maybe_unused]] std::shared_ptr<RemoveDeviceAttachmentSpecReq> context,
|
||||
bool success,
|
||||
std::shared_ptr<DeviceAttachmentSpec> deviceSpec
|
||||
)
|
||||
{
|
||||
if (!success)
|
||||
{
|
||||
// Detach failed, callback with failure (metadata remains intact)
|
||||
callOriginalCb(false, deviceSpec);
|
||||
return;
|
||||
}
|
||||
|
||||
// Detach succeeded, now find and clean up metadata
|
||||
try {
|
||||
// Find the DeviceRole in attachedDeviceRoles
|
||||
auto deviceRoleIt = std::find_if(
|
||||
attachedDeviceRoles.begin(),
|
||||
attachedDeviceRoles.end(),
|
||||
[&spec = spec](const std::shared_ptr<DeviceRole> &role) {
|
||||
return *role->deviceAttachmentSpec == *spec;
|
||||
}
|
||||
);
|
||||
|
||||
if (deviceRoleIt == attachedDeviceRoles.end())
|
||||
{
|
||||
// DeviceRole not found, callback with failure
|
||||
callOriginalCb(false, deviceSpec);
|
||||
return;
|
||||
}
|
||||
|
||||
auto deviceRole = *deviceRoleIt;
|
||||
auto& device = deviceRole->parentDevice;
|
||||
|
||||
// Remove DeviceRole from DeviceManager's collection
|
||||
attachedDeviceRoles.erase(deviceRoleIt);
|
||||
|
||||
// Remove DeviceRole from Device's collection
|
||||
auto deviceRoleIt2 = std::find(
|
||||
device.deviceRoles.begin(),
|
||||
device.deviceRoles.end(),
|
||||
deviceRole);
|
||||
if (deviceRoleIt2 != device.deviceRoles.end())
|
||||
{
|
||||
device.deviceRoles.erase(deviceRoleIt2);
|
||||
}
|
||||
|
||||
// Remove DeviceAttachmentSpec from deviceAttachmentSpecs collection
|
||||
auto specIt = std::find_if(
|
||||
deviceAttachmentSpecs.begin(),
|
||||
deviceAttachmentSpecs.end(),
|
||||
[&spec = spec](const std::shared_ptr<DeviceAttachmentSpec> &existingSpec) {
|
||||
return *existingSpec == *spec;
|
||||
});
|
||||
if (specIt != deviceAttachmentSpecs.end())
|
||||
{
|
||||
deviceAttachmentSpecs.erase(specIt);
|
||||
}
|
||||
|
||||
// Callback with success
|
||||
callOriginalCb(true, deviceSpec);
|
||||
} catch (const std::exception& e) {
|
||||
// Cleanup failed, callback with error
|
||||
callOriginalCb(false, deviceSpec);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
std::shared_ptr<DeviceAttachmentSpec> spec;
|
||||
};
|
||||
|
||||
void DeviceManager::newDeviceAttachmentSpecInd(
|
||||
const DeviceAttachmentSpec &spec,
|
||||
Callback<newDeviceAttachmentSpecIndCbFn> callback)
|
||||
@@ -177,6 +275,39 @@ void DeviceManager::newDeviceAttachmentSpecInd(
|
||||
continuation.get(), continuation));
|
||||
}
|
||||
|
||||
void DeviceManager::removeDeviceAttachmentSpecReq(
|
||||
const DeviceAttachmentSpec &spec,
|
||||
Callback<removeDeviceAttachmentSpecReqCbFn> callback)
|
||||
{
|
||||
// Find the shared_ptr to the spec in the collection
|
||||
std::shared_ptr<DeviceAttachmentSpec> specPtr = nullptr;
|
||||
for (const auto& existingSpec : deviceAttachmentSpecs)
|
||||
{
|
||||
if (*existingSpec == spec)
|
||||
{
|
||||
specPtr = existingSpec;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!specPtr)
|
||||
{
|
||||
// Spec not found, callback with failure
|
||||
callback.callbackFn(false, nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create async continuation
|
||||
const auto& caller = ComponentThread::getSelf();
|
||||
auto continuation = std::make_shared<RemoveDeviceAttachmentSpecReq>(
|
||||
specPtr, caller, callback);
|
||||
|
||||
mrntt::mrntt.thread->getIoService().post(
|
||||
std::bind(
|
||||
&RemoveDeviceAttachmentSpecReq::removeDeviceAttachmentSpecReq1_posted,
|
||||
continuation.get(), continuation));
|
||||
}
|
||||
|
||||
class DeviceManager::AttachSenseDeviceReq
|
||||
: public PostedAsynchronousContinuation<attachSenseDeviceReqCbFn>
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user