Compare commits

...

3 Commits

Author SHA1 Message Date
Kyle Schwarz 83ab65b062 Device: Fix NeoVI message guard 2025-10-29 15:37:58 -04:00
Kyle Schwarz 7f1668a55b Device: Move network mutex to goOnline 2025-10-29 15:24:03 -04:00
Max Brombach 10ffd756a1 Device: Remove erroneous API error in VSA buffer overlap check 2025-10-29 12:56:46 -04:00
2 changed files with 36 additions and 43 deletions

View File

@ -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<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();
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<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
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() {
keeponline.reset();
if(networkMutexCallbackHandle)
removeMessageCallback(*networkMutexCallbackHandle);
forEachExtension([](const std::shared_ptr<DeviceExtension>& ext) { ext->onGoOffline(); return true; });
if(isDisconnected()) {
@ -1999,6 +2001,15 @@ void Device::handleInternalMessage(std::shared_ptr<Message> message) {
case Message::Type::RawMessage: {
auto rawMessage = std::static_pointer_cast<RawMessage>(message);
switch(rawMessage->network.getNetID()) {
case Network::NetID::Device: {
// Device is not guaranteed to be a CANMessage, it might be a RawMessage
// if it couldn't be decoded to a CANMessage. We only care about the
// CANMessage decoding right now.
auto canmsg = std::dynamic_pointer_cast<CANMessage>(message);
if(canmsg)
handleNeoVIMessage(std::move(canmsg));
break;
}
case Network::NetID::DeviceStatus:
// Device Status format is unique per device, so the devices need to decode it themselves
handleDeviceStatus(rawMessage);
@ -2008,15 +2019,6 @@ void Device::handleInternalMessage(std::shared_ptr<Message> message) {
}
break;
}
case Message::Type::Frame: {
// Device is not guaranteed to be a CANMessage, it might be a RawMessage
// if it couldn't be decoded to a CANMessage. We only care about the
// CANMessage decoding right now.
auto canmsg = std::dynamic_pointer_cast<CANMessage>(message);
if(canmsg)
handleNeoVIMessage(std::move(canmsg));
break;
}
default: break;
}
forEachExtension([&](const std::shared_ptr<DeviceExtension>& ext) {
@ -3049,7 +3051,6 @@ std::optional<bool> Device::isVSAOverlapped(std::optional<VSAMetadata> optMetada
lastSectorRecord->getTimestamp() > metadata.coreMiniTimestamp;
} else if(lastSectorStatus == VSAParser::RecordParseStatus::NotARecordStart) {
// The vsa record buffer is not full
report(APIEvent::Type::VSAOtherError, APIEvent::Severity::Error);
return false;
}
report(APIEvent::Type::VSABufferFormatError, APIEvent::Severity::Error);
@ -3891,10 +3892,6 @@ std::shared_ptr<DiskDetails> 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;
@ -4007,10 +4004,6 @@ std::shared_ptr<NetworkMutexMessage> 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;

View File

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