diff --git a/device/device.cpp b/device/device.cpp index 2d8c6b3..adb3dad 100644 --- a/device/device.cpp +++ b/device/device.cpp @@ -459,29 +459,6 @@ APIEvent::Type Device::attemptToBeginCommunication() { return getCommunicationNotEstablishedError(); std::this_thread::sleep_for(std::chrono::milliseconds(10)); - assignedClientId = com->getClientIDSync(); - networkMutexCallbackHandle = addMessageCallback( - std::make_shared( - [this](std::shared_ptr message) { - auto netMutexMsg = std::static_pointer_cast(message); - if(netMutexMsg->networks.size() && netMutexMsg->event.has_value()) { - switch(*netMutexMsg->event) { - case NetworkMutexEvent::Acquired: - lockedNetworks.emplace(*netMutexMsg->networks.begin()); - break; - case NetworkMutexEvent::Released: { - auto it = lockedNetworks.find(*netMutexMsg->networks.begin()); - if (it != lockedNetworks.end()) - lockedNetworks.erase(it); - break; - } - } - } - }, - std::make_shared(Message::Type::NetworkMutex) - ) - ); - auto serial = com->getSerialNumberSync(); int i = 0; while(!serial) { @@ -577,6 +554,28 @@ bool Device::goOnline() { return false; } + assignedClientId = com->getClientIDSync(); + if(assignedClientId) { + // firmware supports clientid/mutex + networkMutexCallbackHandle = lockAllNetworks(std::numeric_limits::max(), std::numeric_limits::max(), NetworkMutexType::Shared, [this](std::shared_ptr message) { + auto netMutexMsg = std::static_pointer_cast(message); + if(netMutexMsg->networks.size() && netMutexMsg->event.has_value()) { + switch(*netMutexMsg->event) { + case NetworkMutexEvent::Acquired: + lockedNetworks.emplace(*netMutexMsg->networks.begin()); + break; + case NetworkMutexEvent::Released: { + auto it = lockedNetworks.find(*netMutexMsg->networks.begin()); + if (it != lockedNetworks.end()) + lockedNetworks.erase(it); + break; + } + } + } + } + ); + } + // (re)start the keeponline keeponline = std::make_unique([this] { return enableNetworkCommunication(true, onlineTimeoutMs); }, std::chrono::milliseconds(onlineTimeoutMs / 4)); @@ -590,6 +589,9 @@ bool Device::goOnline() { bool Device::goOffline() { keeponline.reset(); + if(networkMutexCallbackHandle) + removeMessageCallback(*networkMutexCallbackHandle); + forEachExtension([](const std::shared_ptr& ext) { ext->onGoOffline(); return true; }); if(isDisconnected()) { @@ -3890,10 +3892,6 @@ std::shared_ptr Device::getDiskDetails(std::chrono::milliseconds ti report(APIEvent::Type::NotSupported, APIEvent::Severity::Error); return std::nullopt; } - if(!isOnline()) { - report(APIEvent::Type::DeviceCurrentlyOffline, APIEvent::Severity::Error); - return std::nullopt; - } if(!assignedClientId.has_value()) { report(APIEvent::Type::RequiredParameterNull, APIEvent::Severity::Error); return std::nullopt; @@ -4006,10 +4004,6 @@ std::shared_ptr Device::getNetworkMutexStatus(Network::NetI report(APIEvent::Type::NotSupported, APIEvent::Severity::Error); return std::nullopt; } - if(!isOnline()) { - report(APIEvent::Type::DeviceCurrentlyOffline, APIEvent::Severity::Error); - return std::nullopt; - } if(!assignedClientId.has_value()) { report(APIEvent::Type::RequiredParameterNull, APIEvent::Severity::Error); return std::nullopt; diff --git a/include/icsneo/device/device.h b/include/icsneo/device/device.h index 0bef99e..e2fc89f 100644 --- a/include/icsneo/device/device.h +++ b/include/icsneo/device/device.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include "icsneo/api/eventmanager.h" #include "icsneo/api/lifetime.h" @@ -1139,9 +1140,8 @@ private: std::unique_ptr keeponline; std::optional assignedClientId; - std::set lockedNetworks; + std::unordered_set lockedNetworks; std::optional networkMutexCallbackHandle; - }; }