From 1a56e2a1079ba43d1807dae7eba05c8790e85c30 Mon Sep 17 00:00:00 2001 From: Hayodea Hekol Date: Sun, 28 Sep 2025 00:50:05 -0400 Subject: [PATCH] DevMgr: Add DeviceRoles; attachedDevices unrelated to device state now We've decided to add a separate notion of a DeviceRole to track attached device roles now. We no longer use the collection of deviceSpecs to track which roles have been attached. Rather, this list will simply collate all known deviceAttachment specs which are expected to be maintained in an attached state. SMO can periodically scan through these and cross-reference this collection with the collection of attachedDeviceRoles. Then it can re-try to attach those which aren't currently attached at any given moment. This will give resilience against device attachment failures or device resets/malfunctions, at runtime. --- .../deviceAttachmentPipeSpecp.yy | 2 - smocore/deviceManager/deviceManager.cpp | 52 +++++++++++-------- smocore/include/deviceManager/device.h | 9 ++-- smocore/include/deviceManager/deviceManager.h | 8 ++- smocore/include/deviceManager/deviceRole.h | 23 ++++++++ 5 files changed, 62 insertions(+), 32 deletions(-) create mode 100644 smocore/include/deviceManager/deviceRole.h diff --git a/smocore/deviceManager/deviceAttachmentPipeSpecp.yy b/smocore/deviceManager/deviceAttachmentPipeSpecp.yy index 9da1f27..3422eab 100644 --- a/smocore/deviceManager/deviceAttachmentPipeSpecp.yy +++ b/smocore/deviceManager/deviceAttachmentPipeSpecp.yy @@ -87,7 +87,6 @@ interoceptor_spec: *static_cast($3)); spec->sensorType = $1; - smo::device::DeviceManager::interoceptorDeviceSpecs.push_back(spec); smo::device::DeviceManager::deviceAttachmentSpecs.push_back(spec); delete $3; @@ -100,7 +99,6 @@ extrospector_spec: *static_cast($3)); spec->sensorType = $1; - smo::device::DeviceManager::extrospectorDeviceSpecs.push_back(spec); smo::device::DeviceManager::deviceAttachmentSpecs.push_back(spec); delete $3; diff --git a/smocore/deviceManager/deviceManager.cpp b/smocore/deviceManager/deviceManager.cpp index 641fc17..bd741f6 100644 --- a/smocore/deviceManager/deviceManager.cpp +++ b/smocore/deviceManager/deviceManager.cpp @@ -17,25 +17,19 @@ namespace smo { namespace device { -std::vector> - DeviceManager::interoceptorDeviceSpecs; -std::vector> - DeviceManager::extrospectorDeviceSpecs; std::vector> DeviceManager::deviceAttachmentSpecs; std::vector> DeviceManager::devices; +std::vector> + DeviceManager::attachedDeviceRoles; const std::string DeviceManager::stringifyDeviceSpecs(void) { std::ostringstream oss; - for (const auto& spec : DeviceManager::interoceptorDeviceSpecs) { - oss << "Interoceptor " << spec->stringify(); - } - - for (const auto& spec : DeviceManager::extrospectorDeviceSpecs) { - oss << "Extrospector " << spec->stringify(); + for (const auto& spec : DeviceManager::deviceAttachmentSpecs) { + oss << "Device Attachment Spec: " << spec->stringify(); } return oss.str(); @@ -111,14 +105,13 @@ public: devices.push_back(device); } - // Add DeviceAttachmentSpec to device's list - device->deviceAttachmentSpecs.push_back(spec); - - // Add DeviceAttachmentSpec to DeviceManager's list - deviceAttachmentSpecs.push_back(spec); + // Create DeviceRole and add it to both DeviceManager's and Device's collections + auto deviceRole = std::make_shared(spec); + attachedDeviceRoles.push_back(deviceRole); + device->deviceRoles.push_back(deviceRole); // Callback with success - callOriginalCb(true, device, spec); + callOriginalCb(true, deviceRole, spec); } catch (const std::exception& e) { // Attach failed, callback with error callOriginalCb(false, nullptr, spec); @@ -130,13 +123,30 @@ void DeviceManager::newDeviceAttachmentSpecInd( std::shared_ptr spec, Callback callback) { - // Check if a DeviceAttachmentSpec already matches + // First, add the spec to deviceAttachmentSpecs if it's not already there + bool specExists = false; for (const auto& existingSpec : deviceAttachmentSpecs) { - if (!(*existingSpec == *spec)) { continue; } - // Already exists, callback with error - callback.callbackFn(false, nullptr, spec); - return; + if (*existingSpec == *spec) + { + specExists = true; + break; + } + } + + if (!specExists) { + deviceAttachmentSpecs.push_back(spec); + } + + // Check if a DeviceRole w/ this spec already exists in attachedDeviceRoles + for (const auto& existingDeviceRole : attachedDeviceRoles) + { + if (*existingDeviceRole->deviceAttachmentSpec == *spec) + { + // Already attached, callback with success + callback.callbackFn(true, existingDeviceRole, spec); + return; + } } // Create async continuation diff --git a/smocore/include/deviceManager/device.h b/smocore/include/deviceManager/device.h index d21836c..3052721 100644 --- a/smocore/include/deviceManager/device.h +++ b/smocore/include/deviceManager/device.h @@ -6,6 +6,7 @@ #include #include #include +#include namespace smo { namespace device { @@ -19,16 +20,16 @@ public: { std::ostringstream os; os << "Device Identifier: " << deviceIdentifier - << ", Attachment Specs: " << deviceAttachmentSpecs.size() << std::endl; - for (const auto& spec : deviceAttachmentSpecs) { - os << " " << spec->stringify(); + << ", Device Roles: " << deviceRoles.size() << std::endl; + for (const auto& deviceRole : deviceRoles) { + os << " " << deviceRole->deviceAttachmentSpec->stringify(); } return os.str(); } public: std::string deviceIdentifier; - std::vector> deviceAttachmentSpecs; + std::vector> deviceRoles; }; } // namespace device diff --git a/smocore/include/deviceManager/deviceManager.h b/smocore/include/deviceManager/deviceManager.h index 82c5094..f195881 100644 --- a/smocore/include/deviceManager/deviceManager.h +++ b/smocore/include/deviceManager/deviceManager.h @@ -12,6 +12,7 @@ #include #include #include +#include #include namespace smo { @@ -39,7 +40,7 @@ public: // New async function for device attachment typedef std::function device, + bool success, std::shared_ptr deviceRole, std::shared_ptr deviceSpec)> newDeviceAttachmentSpecIndCbFn; void newDeviceAttachmentSpecInd( @@ -75,13 +76,10 @@ private: public: std::string allDapSpecs; - static std::vector> - interoceptorDeviceSpecs; - static std::vector> - extrospectorDeviceSpecs; static std::vector> deviceAttachmentSpecs; static std::vector> devices; + static std::vector> attachedDeviceRoles; private: class NewDeviceAttachmentSpecInd; diff --git a/smocore/include/deviceManager/deviceRole.h b/smocore/include/deviceManager/deviceRole.h new file mode 100644 index 0000000..046fd15 --- /dev/null +++ b/smocore/include/deviceManager/deviceRole.h @@ -0,0 +1,23 @@ +#ifndef DEVICEROLE_H +#define DEVICEROLE_H + +#include +#include + +namespace smo { +namespace device { + +class DeviceRole +{ +public: + DeviceRole(std::shared_ptr spec) + : deviceAttachmentSpec(spec) + {} + + std::shared_ptr deviceAttachmentSpec; +}; + +} // namespace device +} // namespace smo + +#endif // DEVICEROLE_H