Device: Move network mutex to goOnline

pull/76/merge
Kyle Schwarz 2025-10-29 15:24:03 -04:00
parent 10ffd756a1
commit 7f1668a55b
2 changed files with 27 additions and 33 deletions

View File

@ -459,29 +459,6 @@ APIEvent::Type Device::attemptToBeginCommunication() {
return getCommunicationNotEstablishedError(); return getCommunicationNotEstablishedError();
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(10));
assignedClientId = com->getClientIDSync();
networkMutexCallbackHandle = addMessageCallback(
std::make_shared<MessageCallback>(
[this](std::shared_ptr<Message> message) {
auto netMutexMsg = std::static_pointer_cast<NetworkMutexMessage>(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<MessageFilter>(Message::Type::NetworkMutex)
)
);
auto serial = com->getSerialNumberSync(); auto serial = com->getSerialNumberSync();
int i = 0; int i = 0;
while(!serial) { while(!serial) {
@ -577,6 +554,28 @@ bool Device::goOnline() {
return false; return false;
} }
assignedClientId = com->getClientIDSync();
if(assignedClientId) {
// firmware supports clientid/mutex
networkMutexCallbackHandle = lockAllNetworks(std::numeric_limits<uint32_t>::max(), std::numeric_limits<uint32_t>::max(), NetworkMutexType::Shared, [this](std::shared_ptr<Message> message) {
auto netMutexMsg = std::static_pointer_cast<NetworkMutexMessage>(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 // (re)start the keeponline
keeponline = std::make_unique<Periodic>([this] { return enableNetworkCommunication(true, onlineTimeoutMs); }, std::chrono::milliseconds(onlineTimeoutMs / 4)); keeponline = std::make_unique<Periodic>([this] { return enableNetworkCommunication(true, onlineTimeoutMs); }, std::chrono::milliseconds(onlineTimeoutMs / 4));
@ -590,6 +589,9 @@ bool Device::goOnline() {
bool Device::goOffline() { bool Device::goOffline() {
keeponline.reset(); keeponline.reset();
if(networkMutexCallbackHandle)
removeMessageCallback(*networkMutexCallbackHandle);
forEachExtension([](const std::shared_ptr<DeviceExtension>& ext) { ext->onGoOffline(); return true; }); forEachExtension([](const std::shared_ptr<DeviceExtension>& ext) { ext->onGoOffline(); return true; });
if(isDisconnected()) { if(isDisconnected()) {
@ -3890,10 +3892,6 @@ std::shared_ptr<DiskDetails> Device::getDiskDetails(std::chrono::milliseconds ti
report(APIEvent::Type::NotSupported, APIEvent::Severity::Error); report(APIEvent::Type::NotSupported, APIEvent::Severity::Error);
return std::nullopt; return std::nullopt;
} }
if(!isOnline()) {
report(APIEvent::Type::DeviceCurrentlyOffline, APIEvent::Severity::Error);
return std::nullopt;
}
if(!assignedClientId.has_value()) { if(!assignedClientId.has_value()) {
report(APIEvent::Type::RequiredParameterNull, APIEvent::Severity::Error); report(APIEvent::Type::RequiredParameterNull, APIEvent::Severity::Error);
return std::nullopt; return std::nullopt;
@ -4006,10 +4004,6 @@ std::shared_ptr<NetworkMutexMessage> Device::getNetworkMutexStatus(Network::NetI
report(APIEvent::Type::NotSupported, APIEvent::Severity::Error); report(APIEvent::Type::NotSupported, APIEvent::Severity::Error);
return std::nullopt; return std::nullopt;
} }
if(!isOnline()) {
report(APIEvent::Type::DeviceCurrentlyOffline, APIEvent::Severity::Error);
return std::nullopt;
}
if(!assignedClientId.has_value()) { if(!assignedClientId.has_value()) {
report(APIEvent::Type::RequiredParameterNull, APIEvent::Severity::Error); report(APIEvent::Type::RequiredParameterNull, APIEvent::Severity::Error);
return std::nullopt; return std::nullopt;

View File

@ -13,6 +13,7 @@
#include <optional> #include <optional>
#include <unordered_map> #include <unordered_map>
#include <set> #include <set>
#include <unordered_set>
#include <chrono> #include <chrono>
#include "icsneo/api/eventmanager.h" #include "icsneo/api/eventmanager.h"
#include "icsneo/api/lifetime.h" #include "icsneo/api/lifetime.h"
@ -1139,9 +1140,8 @@ private:
std::unique_ptr<Periodic> keeponline; std::unique_ptr<Periodic> keeponline;
std::optional<uint32_t> assignedClientId; std::optional<uint32_t> assignedClientId;
std::set<icsneo::Network::NetID> lockedNetworks; std::unordered_set<icsneo::Network::NetID> lockedNetworks;
std::optional<int> networkMutexCallbackHandle; std::optional<int> networkMutexCallbackHandle;
}; };
} }