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.
This commit is contained in:
2025-09-28 00:50:05 -04:00
parent e6b8d3e85d
commit 1a56e2a107
5 changed files with 62 additions and 32 deletions
@@ -87,7 +87,6 @@ interoceptor_spec:
*static_cast<smo::device::InteroceptorDevAttachmentSpec *>($3)); *static_cast<smo::device::InteroceptorDevAttachmentSpec *>($3));
spec->sensorType = $1; spec->sensorType = $1;
smo::device::DeviceManager::interoceptorDeviceSpecs.push_back(spec);
smo::device::DeviceManager::deviceAttachmentSpecs.push_back(spec); smo::device::DeviceManager::deviceAttachmentSpecs.push_back(spec);
delete $3; delete $3;
@@ -100,7 +99,6 @@ extrospector_spec:
*static_cast<smo::device::ExtrospectorDevAttachmentSpec *>($3)); *static_cast<smo::device::ExtrospectorDevAttachmentSpec *>($3));
spec->sensorType = $1; spec->sensorType = $1;
smo::device::DeviceManager::extrospectorDeviceSpecs.push_back(spec);
smo::device::DeviceManager::deviceAttachmentSpecs.push_back(spec); smo::device::DeviceManager::deviceAttachmentSpecs.push_back(spec);
delete $3; delete $3;
+30 -20
View File
@@ -17,25 +17,19 @@
namespace smo { namespace smo {
namespace device { namespace device {
std::vector<std::shared_ptr<InteroceptorDevAttachmentSpec>>
DeviceManager::interoceptorDeviceSpecs;
std::vector<std::shared_ptr<ExtrospectorDevAttachmentSpec>>
DeviceManager::extrospectorDeviceSpecs;
std::vector<std::shared_ptr<DeviceAttachmentSpec>> std::vector<std::shared_ptr<DeviceAttachmentSpec>>
DeviceManager::deviceAttachmentSpecs; DeviceManager::deviceAttachmentSpecs;
std::vector<std::shared_ptr<Device>> std::vector<std::shared_ptr<Device>>
DeviceManager::devices; DeviceManager::devices;
std::vector<std::shared_ptr<DeviceRole>>
DeviceManager::attachedDeviceRoles;
const std::string DeviceManager::stringifyDeviceSpecs(void) const std::string DeviceManager::stringifyDeviceSpecs(void)
{ {
std::ostringstream oss; std::ostringstream oss;
for (const auto& spec : DeviceManager::interoceptorDeviceSpecs) { for (const auto& spec : DeviceManager::deviceAttachmentSpecs) {
oss << "Interoceptor " << spec->stringify(); oss << "Device Attachment Spec: " << spec->stringify();
}
for (const auto& spec : DeviceManager::extrospectorDeviceSpecs) {
oss << "Extrospector " << spec->stringify();
} }
return oss.str(); return oss.str();
@@ -111,14 +105,13 @@ public:
devices.push_back(device); devices.push_back(device);
} }
// Add DeviceAttachmentSpec to device's list // Create DeviceRole and add it to both DeviceManager's and Device's collections
device->deviceAttachmentSpecs.push_back(spec); auto deviceRole = std::make_shared<DeviceRole>(spec);
attachedDeviceRoles.push_back(deviceRole);
// Add DeviceAttachmentSpec to DeviceManager's list device->deviceRoles.push_back(deviceRole);
deviceAttachmentSpecs.push_back(spec);
// Callback with success // Callback with success
callOriginalCb(true, device, spec); callOriginalCb(true, deviceRole, spec);
} catch (const std::exception& e) { } catch (const std::exception& e) {
// Attach failed, callback with error // Attach failed, callback with error
callOriginalCb(false, nullptr, spec); callOriginalCb(false, nullptr, spec);
@@ -130,14 +123,31 @@ void DeviceManager::newDeviceAttachmentSpecInd(
std::shared_ptr<DeviceAttachmentSpec> spec, std::shared_ptr<DeviceAttachmentSpec> spec,
Callback<newDeviceAttachmentSpecIndCbFn> callback) Callback<newDeviceAttachmentSpecIndCbFn> 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) for (const auto& existingSpec : deviceAttachmentSpecs)
{ {
if (!(*existingSpec == *spec)) { continue; } if (*existingSpec == *spec)
// Already exists, callback with error {
callback.callbackFn(false, nullptr, 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; return;
} }
}
// Create async continuation // Create async continuation
const auto& caller = ComponentThread::getSelf(); const auto& caller = ComponentThread::getSelf();
+5 -4
View File
@@ -6,6 +6,7 @@
#include <memory> #include <memory>
#include <sstream> #include <sstream>
#include <user/deviceAttachmentSpec.h> #include <user/deviceAttachmentSpec.h>
#include <deviceManager/deviceRole.h>
namespace smo { namespace smo {
namespace device { namespace device {
@@ -19,16 +20,16 @@ public:
{ {
std::ostringstream os; std::ostringstream os;
os << "Device Identifier: " << deviceIdentifier os << "Device Identifier: " << deviceIdentifier
<< ", Attachment Specs: " << deviceAttachmentSpecs.size() << std::endl; << ", Device Roles: " << deviceRoles.size() << std::endl;
for (const auto& spec : deviceAttachmentSpecs) { for (const auto& deviceRole : deviceRoles) {
os << " " << spec->stringify(); os << " " << deviceRole->deviceAttachmentSpec->stringify();
} }
return os.str(); return os.str();
} }
public: public:
std::string deviceIdentifier; std::string deviceIdentifier;
std::vector<std::shared_ptr<DeviceAttachmentSpec>> deviceAttachmentSpecs; std::vector<std::shared_ptr<DeviceRole>> deviceRoles;
}; };
} // namespace device } // namespace device
@@ -12,6 +12,7 @@
#include <user/deviceAttachmentSpec.h> #include <user/deviceAttachmentSpec.h>
#include <asynchronousLoop.h> #include <asynchronousLoop.h>
#include <deviceManager/device.h> #include <deviceManager/device.h>
#include <deviceManager/deviceRole.h>
#include <callback.h> #include <callback.h>
namespace smo { namespace smo {
@@ -39,7 +40,7 @@ public:
// New async function for device attachment // New async function for device attachment
typedef std::function<void( typedef std::function<void(
bool success, std::shared_ptr<Device> device, bool success, std::shared_ptr<DeviceRole> deviceRole,
std::shared_ptr<DeviceAttachmentSpec> deviceSpec)> std::shared_ptr<DeviceAttachmentSpec> deviceSpec)>
newDeviceAttachmentSpecIndCbFn; newDeviceAttachmentSpecIndCbFn;
void newDeviceAttachmentSpecInd( void newDeviceAttachmentSpecInd(
@@ -75,13 +76,10 @@ private:
public: public:
std::string allDapSpecs; std::string allDapSpecs;
static std::vector<std::shared_ptr<InteroceptorDevAttachmentSpec>>
interoceptorDeviceSpecs;
static std::vector<std::shared_ptr<ExtrospectorDevAttachmentSpec>>
extrospectorDeviceSpecs;
static std::vector<std::shared_ptr<DeviceAttachmentSpec>> static std::vector<std::shared_ptr<DeviceAttachmentSpec>>
deviceAttachmentSpecs; deviceAttachmentSpecs;
static std::vector<std::shared_ptr<Device>> devices; static std::vector<std::shared_ptr<Device>> devices;
static std::vector<std::shared_ptr<DeviceRole>> attachedDeviceRoles;
private: private:
class NewDeviceAttachmentSpecInd; class NewDeviceAttachmentSpecInd;
@@ -0,0 +1,23 @@
#ifndef DEVICEROLE_H
#define DEVICEROLE_H
#include <memory>
#include <user/deviceAttachmentSpec.h>
namespace smo {
namespace device {
class DeviceRole
{
public:
DeviceRole(std::shared_ptr<DeviceAttachmentSpec> spec)
: deviceAttachmentSpec(spec)
{}
std::shared_ptr<DeviceAttachmentSpec> deviceAttachmentSpec;
};
} // namespace device
} // namespace smo
#endif // DEVICEROLE_H