Device: Refactor ComponentVersions

Also:
- Add setGenericData()
- Add ExtendedResponseFilter
pull/64/merge
Yasser Yassine 2025-04-30 18:49:35 +00:00 committed by Kyle Schwarz
parent b1e875980c
commit 7eeb1b6c38
22 changed files with 90 additions and 56 deletions

View File

@ -31,7 +31,7 @@ std::shared_ptr<ComponentVersionsMessage> ComponentVersionPacket::DecodeToMessag
// Get a reference to the payload to fully validate the length
const auto& response = *reinterpret_cast<const ComponentVersionsResponse*>(bytes.data());
// Expected size is the header, numVersions field, and numVersions ComponentVersion objects.
auto expectedSize = sizeof(ExtendedResponseMessage::ResponseHeader) + 2 + (response.numVersions * sizeof(ComponentVersion));
auto expectedSize = sizeof(ExtendedResponseMessage::ResponseHeader) + 2 + (response.numVersions * sizeof(PackedComponentVersion));
// If the response is malformed (too small), return an empty message.
if(bytes.size() < expectedSize) {
return msg; // Empty
@ -39,7 +39,14 @@ std::shared_ptr<ComponentVersionsMessage> ComponentVersionPacket::DecodeToMessag
// Unpack into the portable class
for(unsigned int i = 0; i < response.numVersions; ++i) {
const auto& packedVersion = response.versions[i];
msg->versions.emplace_back(packedVersion.valid, packedVersion.componentInfo, packedVersion.identifier, packedVersion.dotVersion, packedVersion.commitHash);
msg->versions.emplace_back(
packedVersion.valid,
packedVersion.componentInfo,
packedVersion.identifier,
packedVersion.dotVersion,
packedVersion.commitHash,
packedVersion.expansionSlot
);
}
return msg;
}

View File

@ -134,6 +134,18 @@ std::pair<std::vector<std::shared_ptr<Message>>, bool> Device::getMessages() {
return std::make_pair(ret, retBool);
}
std::shared_ptr<DeviceExtension> Device::getExtension(const std::string& name) const {
std::shared_ptr<DeviceExtension> ret;
std::lock_guard<std::mutex> lk(extensionsLock);
for(auto& ext : extensions) {
if((ext->getName() == name)) {
ret = ext;
break;
}
}
return ret;
}
bool Device::getMessages(std::vector<std::shared_ptr<Message>>& container, size_t limit, std::chrono::milliseconds timeout) {
if(!isOpen()) {
report(APIEvent::Type::DeviceCurrentlyClosed, APIEvent::Severity::Error);
@ -180,6 +192,14 @@ void Device::enforcePollingMessageLimit() {
}
}
bool Device::refreshComponentVersions() {
if(auto compVersions = com->getComponentVersionsSync()) {
componentVersions = std::move(*compVersions);
return true;
}
return false;
}
bool Device::open(OpenFlags flags, OpenStatusHandler handler) {
if(!com) {
report(APIEvent::Type::Unknown, APIEvent::Severity::Error);
@ -199,6 +219,7 @@ bool Device::open(OpenFlags flags, OpenStatusHandler handler) {
tryAgain = true;
return true;
});
if(!tryAgain) {
com->close();
report(attemptErr, APIEvent::Severity::Error);
@ -222,15 +243,7 @@ bool Device::open(OpenFlags flags, OpenStatusHandler handler) {
if(block) // Extensions say no
return false;
// Get component versions again *after* the extension "onDeviceOpen" hooks (e.g. device reflashes)
if(supportsComponentVersions()) {
if(auto compVersions = com->getComponentVersionsSync())
componentVersions = std::move(*compVersions);
else
// It's possible the device is on older firmware so don't return false here
report(APIEvent::Type::NoDeviceResponse, APIEvent::Severity::EventWarning);
}
refreshComponentVersions();
if(!settings->disabled) {
// Since we will not fail the open if a settings read fails,
// downgrade any errors to warnings. Otherwise the error will
@ -336,13 +349,14 @@ APIEvent::Type Device::attemptToBeginCommunication() {
return getCommunicationNotEstablishedError();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
auto serial = com->getSerialNumberSync();
auto serial = com->getSerialNumberSync(std::chrono::milliseconds(200));
int i = 0;
while(!serial) {
serial = com->getSerialNumberSync();
serial = com->getSerialNumberSync(std::chrono::milliseconds(200));
if(i++ > 5)
break;
}
if(!serial) // "Communication could not be established with the device. Perhaps it is not powered with 12 volts?"
return getCommunicationNotEstablishedError();
@ -356,7 +370,6 @@ APIEvent::Type Device::attemptToBeginCommunication() {
else
versions = std::move(*maybeVersions);
// Get component versions before the extension "onDeviceOpen" hooks so that we can properly check verisons
if(supportsComponentVersions()) {
if(auto compVersions = com->getComponentVersionsSync())

View File

@ -7,8 +7,8 @@
namespace icsneo {
class ComponentVersion {
public:
ComponentVersion(uint8_t valid, uint8_t componentInfo, uint32_t identifier, uint32_t dotVersion, uint32_t commitHash) :
valid(valid), componentInfo(componentInfo), identifier(identifier), dotVersion(dotVersion), commitHash(commitHash) {}
ComponentVersion(uint8_t valid, uint8_t componentInfo, uint32_t identifier, uint32_t dotVersion, uint32_t commitHash, uint8_t expansionSlot) :
valid(valid), componentInfo(componentInfo), identifier(identifier), dotVersion(dotVersion), commitHash(commitHash), expansionSlot(expansionSlot) {}
static ComponentVersion FromAppVersion(uint32_t identifier, uint8_t appMajor, uint8_t appMinor) {
uint32_t dotVersion = (appMajor << 24) | (appMinor << 16);
@ -19,6 +19,7 @@ public:
static_cast<uint8_t>(0u),
identifier,
dotVersion,
static_cast<uint8_t>(0u),
static_cast<uint8_t>(0u)
);
}
@ -28,6 +29,7 @@ public:
const uint32_t identifier;
const uint32_t dotVersion;
const uint32_t commitHash;
const uint8_t expansionSlot;
};
class ComponentVersionsMessage : public Message {

View File

@ -0,0 +1,39 @@
#ifndef __EXTENDEDRESPONSEFILTER_H_
#define __EXTENDEDRESPONSEFILTER_H_
#ifdef __cplusplus
#include "icsneo/communication/message/filter/messagefilter.h"
#include "icsneo/communication/network.h"
#include "icsneo/communication/communication.h"
#include "icsneo/communication/command.h"
#include "icsneo/communication/message/extendedresponsemessage.h"
#include <memory>
#include <iostream>
namespace icsneo {
class ExtendedResponseFilter : public MessageFilter {
public:
ExtendedResponseFilter(icsneo::ExtendedResponse resp) : MessageFilter(Message::Type::ExtendedResponse), response(resp) {}
bool match(const std::shared_ptr<Message>& message) const override {
if(!MessageFilter::match(message)) {
return false;
}
const auto respMsg = std::static_pointer_cast<ExtendedResponseMessage>(message);
return respMsg && matchResponse(respMsg->response);
}
private:
icsneo::ExtendedResponse response;
bool matchResponse(icsneo::ExtendedResponse resp) const {
return response == resp;
}
};
}
#endif // __cplusplus
#endif

View File

@ -570,6 +570,7 @@ public:
NODISCARD("If the Lifetime is not held, disconnects will be immediately unsuppressed")
Lifetime suppressDisconnects();
bool refreshComponentVersions();
/**
* For use by extensions only. A more stable API will be provided in the future.
*/
@ -727,7 +728,9 @@ public:
virtual bool isOnlineSupported() const { return true; }
virtual bool supportsComponentVersions() const { return false; }
virtual bool supportsComponentVersions() const {
return !getComponentVersions().empty();
}
virtual bool supportsTC10() const { return false; }
@ -743,6 +746,8 @@ public:
/* MACsec support */
virtual bool writeMACsecConfig(const MACsecMessage& message, uint16_t binaryIndex);
std::shared_ptr<DeviceExtension> getExtension(const std::string& name) const;
protected:
bool online = false;
int messagePollingCallbackID = 0;
@ -824,12 +829,6 @@ protected:
virtual void setupExtensions() {}
// Hook for devices such as FIRE which need to inject traffic before RequestSerialNumber
// Return false to bail
virtual bool afterCommunicationOpen() { return true; }
virtual bool requiresVehiclePower() const { return true; }
template<typename Extension>
std::shared_ptr<Extension> getExtension() const {
std::shared_ptr<Extension> ret;
@ -841,6 +840,12 @@ protected:
return ret;
}
// END Initialization Functions
// Hook for devices such as FIRE which need to inject traffic before RequestSerialNumber
// Return false to bail
virtual bool afterCommunicationOpen() { return true; }
virtual bool requiresVehiclePower() const { return true; }
void handleInternalMessage(std::shared_ptr<Message> message);

View File

@ -28,6 +28,7 @@ public:
virtual void onGoOffline() {}
virtual void onDeviceClose() {}
virtual void setGenericData(void*) {}
virtual bool providesFirmware() const { return false; }
virtual void handleMessage(const std::shared_ptr<Message>&) {}

View File

@ -26,8 +26,6 @@ public:
return supportedNetworks;
}
bool supportsComponentVersions() const override { return true; }
protected:
EtherBADGE(neodevice_t neodevice, const driver_factory_t& makeDriver) : Device(neodevice) {
initialize<EtherBADGESettings>(makeDriver);

View File

@ -69,7 +69,6 @@ protected:
return 0;
}
bool supportsComponentVersions() const override { return true; }
bool supportsGPTP() const override { return true; }
};

View File

@ -87,8 +87,6 @@ public:
};
}
bool supportsComponentVersions() const override { return true; }
protected:
NeoVIFIRE2(neodevice_t neodevice, const driver_factory_t& makeDriver) : Device(neodevice) {
initialize<NeoVIFIRE2Settings, Disk::NeoMemoryDiskDriver, Disk::NeoMemoryDiskDriver>(makeDriver);

View File

@ -49,9 +49,6 @@ public:
};
return supportedNetworks;
}
bool supportsComponentVersions() const override { return true; }
protected:
NeoVIFIRE3(neodevice_t neodevice, const driver_factory_t& makeDriver) : Device(neodevice) {
initialize<NeoVIFIRE3Settings, Disk::ExtExtractorDiskReadDriver, Disk::NeoMemoryDiskDriver>(makeDriver);

View File

@ -52,8 +52,6 @@ public:
return supportedNetworks;
}
bool supportsComponentVersions() const override { return true; }
protected:
NeoVIFIRE3FlexRay(neodevice_t neodevice, const driver_factory_t& makeDriver) : Device(neodevice) {
initialize<NeoVIFIRE3FlexRaySettings, Disk::ExtExtractorDiskReadDriver, Disk::NeoMemoryDiskDriver>(makeDriver);

View File

@ -35,7 +35,6 @@ public:
return supportedNetworks;
}
bool supportsComponentVersions() const override { return true; }
bool supportsGPTP() const override { return true; }
protected:

View File

@ -15,8 +15,6 @@ public:
// USB PID is 0x0901, standard driver is FTDI
ICSNEO_FINDABLE_DEVICE_BY_PID(NeoVIION, DeviceType::ION, 0x0901);
bool supportsComponentVersions() const override { return true; }
private:
NeoVIION(neodevice_t neodevice, const driver_factory_t& makeDriver) : Plasion(neodevice) {
initialize<NullSettings, Disk::PlasionDiskReadDriver, Disk::NeoMemoryDiskDriver>(makeDriver);

View File

@ -25,8 +25,6 @@ public:
return supportedNetworks;
}
bool supportsComponentVersions() const override { return true; }
protected:
RADEpsilon(neodevice_t neodevice, const driver_factory_t& makeDriver) : Device(neodevice) {
initialize<RADEpsilonSettings, Disk::NeoMemoryDiskDriver, Disk::NeoMemoryDiskDriver>(makeDriver);

View File

@ -29,8 +29,6 @@ public:
bool getEthPhyRegControlSupported() const override { return true; }
bool supportsComponentVersions() const override { return true; }
protected:
RADJupiter(neodevice_t neodevice, const driver_factory_t& makeDriver) : Device(neodevice) {
initialize<RADJupiterSettings>(makeDriver);

View File

@ -16,8 +16,6 @@ public:
uint8_t getPhyAddrOrPort() const override { return 1; }
bool supportsComponentVersions() const override { return true; }
bool supportsTC10() const override { return true; }
protected:

View File

@ -28,8 +28,6 @@ public:
bool isOnlineSupported() const override { return false; }
bool supportsComponentVersions() const override { return true; }
bool supportsTC10() const override { return true; }
protected:

View File

@ -25,8 +25,6 @@ public:
bool getEthPhyRegControlSupported() const override { return true; }
bool supportsComponentVersions() const override { return true; }
protected:
RADMoonDuo(neodevice_t neodevice, const driver_factory_t& makeDriver) : Device(neodevice) {
initialize<RADMoonDuoSettings>(makeDriver);

View File

@ -34,8 +34,6 @@ public:
bool getEthPhyRegControlSupported() const override { return true; }
bool supportsComponentVersions() const override { return true; }
protected:
RADPluto(neodevice_t neodevice, const driver_factory_t& makeDriver) : Device(neodevice) {
initialize<RADPlutoSettings>(makeDriver);

View File

@ -22,8 +22,6 @@ public:
return supportedNetworks;
}
bool supportsComponentVersions() const override { return true; }
private:
ValueCAN3(neodevice_t neodevice, const driver_factory_t& makeDriver) : Device(neodevice) {
initialize<ValueCAN3Settings>(makeDriver);

View File

@ -11,9 +11,6 @@ namespace icsneo {
class ValueCAN4 : public Device {
public:
// All ValueCAN 4 devices share a USB PID of 0x1101
bool supportsComponentVersions() const override { return true; }
protected:
using Device::Device;

View File

@ -28,9 +28,6 @@ public:
}
bool isOnlineSupported() const override { return false; }
bool supportsComponentVersions() const override { return true; }
protected:
VividCAN(neodevice_t neodevice, const driver_factory_t& makeDriver) : Device(neodevice) {
initialize<VividCANSettings>(makeDriver);