Drivers: Decouple from devices

This allows us to better implement alternative drivers
for devices, such as for device sharing servers or
talking to CoreMini processors within the same device.
This commit is contained in:
Paul Hollinsky
2022-03-27 14:30:31 -04:00
parent 0ff12300f3
commit 781fc2c034
66 changed files with 780 additions and 1566 deletions
@@ -5,15 +5,16 @@
#include "icsneo/device/device.h"
#include "icsneo/device/devicetype.h"
#include "icsneo/platform/ftdi.h"
#include "icsneo/device/tree/neovifire2/neovifire2settings.h"
namespace icsneo {
class NeoVIFIRE2 : public Device {
public:
static constexpr DeviceType::Enum DEVICE_TYPE = DeviceType::FIRE2;
static constexpr const char* SERIAL_START = "CY";
// Serial numbers start with CY
// USB PID is 0x1000, standard driver is FTDI
// Ethernet MAC allocation is 0x04, standard driver is Raw
ICSNEO_FINDABLE_DEVICE(NeoVIFIRE2, DeviceType::FIRE2, "CY");
enum class SKU {
Standard,
@@ -87,8 +88,22 @@ public:
}
protected:
NeoVIFIRE2(neodevice_t neodevice) : Device(neodevice) {
getWritableNeoDevice().type = DEVICE_TYPE;
NeoVIFIRE2(neodevice_t neodevice, const driver_factory_t& makeDriver) : Device(neodevice) {
initialize<NeoVIFIRE2Settings>(makeDriver);
}
void setupSettings(IDeviceSettings& ssettings) override {
if(com->driver->isEthernet()) {
// TODO Check firmware version, old firmwares will reset Ethernet settings on settings send
ssettings.readonly = true;
}
}
bool currentDriverSupportsDFU() const override { return com->driver->isEthernet(); }
void setupPacketizer(Packetizer& packetizer) override {
Device::setupPacketizer(packetizer);
packetizer.align16bit = !com->driver->isEthernet();
}
virtual void setupEncoder(Encoder& encoder) override {
@@ -96,13 +111,13 @@ protected:
encoder.supportCANFD = true;
}
virtual void setupSupportedRXNetworks(std::vector<Network>& rxNetworks) override {
void setupSupportedRXNetworks(std::vector<Network>& rxNetworks) override {
for(auto& netid : GetSupportedNetworks())
rxNetworks.emplace_back(netid);
}
// The supported TX networks are the same as the supported RX networks for this device
virtual void setupSupportedTXNetworks(std::vector<Network>& txNetworks) override { setupSupportedRXNetworks(txNetworks); }
void setupSupportedTXNetworks(std::vector<Network>& txNetworks) override { setupSupportedRXNetworks(txNetworks); }
void handleDeviceStatus(const std::shared_ptr<RawMessage>& message) override {
if(message->data.size() < sizeof(neovifire2_status_t))
@@ -1,71 +0,0 @@
#ifndef __NEOVIFIRE2ETH_H_
#define __NEOVIFIRE2ETH_H_
#ifdef __cplusplus
#include "icsneo/device/tree/neovifire2/neovifire2.h"
#include "icsneo/platform/pcap.h"
#include <memory>
namespace icsneo {
class NeoVIFIRE2ETH : public NeoVIFIRE2 {
public:
static constexpr const uint16_t PRODUCT_ID = 0x0004;
static std::vector<std::shared_ptr<Device>> Find(const std::vector<PCAP::PCAPFoundDevice>& pcapDevices) {
std::vector<std::shared_ptr<Device>> found;
for(auto& foundDev : pcapDevices) {
auto fakedev = std::shared_ptr<NeoVIFIRE2ETH>(new NeoVIFIRE2ETH({}));
for (auto& payload : foundDev.discoveryPackets)
fakedev->com->packetizer->input(payload);
for (auto& packet : fakedev->com->packetizer->output()) {
std::shared_ptr<Message> msg;
if (!fakedev->com->decoder->decode(msg, packet))
continue; // We failed to decode this packet
if(!msg || msg->type != Message::Type::Main51)
continue; // Not a message we care about
auto sn = std::dynamic_pointer_cast<SerialNumberMessage>(msg);
if(!sn)
continue; // Not a serial number message
if(sn->deviceSerial.length() < 2)
continue;
if(sn->deviceSerial.substr(0, 2) != SERIAL_START)
continue; // Not a FIRE 2
auto device = foundDev.device;
device.serial[sn->deviceSerial.copy(device.serial, sizeof(device.serial))] = '\0';
found.push_back(std::make_shared<NeoVIFIRE2ETH>(std::move(device)));
break;
}
}
return found;
}
NeoVIFIRE2ETH(neodevice_t neodevice) : NeoVIFIRE2(neodevice) {
initialize<PCAP, NeoVIFIRE2Settings>();
productId = PRODUCT_ID;
}
bool currentDriverSupportsDFU() const override { return false; }
protected:
void setupSettings(IDeviceSettings& ssettings) override {
// TODO Check firmware version, old firmwares will reset Ethernet settings on settings send
ssettings.readonly = true;
}
void setupPacketizer(Packetizer& packetizer) override {
NeoVIFIRE2::setupPacketizer(packetizer);
packetizer.align16bit = false;
}
};
}
#endif // __cplusplus
#endif
@@ -1,34 +0,0 @@
#ifndef __NEOVIFIRE2USB_H_
#define __NEOVIFIRE2USB_H_
#ifdef __cplusplus
#include "icsneo/device/tree/neovifire2/neovifire2.h"
#include "icsneo/platform/ftdi.h"
namespace icsneo {
class NeoVIFIRE2USB : public NeoVIFIRE2 {
public:
static constexpr const uint16_t PRODUCT_ID = 0x1000;
static std::vector<std::shared_ptr<Device>> Find() {
std::vector<std::shared_ptr<Device>> found;
for(auto neodevice : FTDI::FindByProduct(PRODUCT_ID))
found.emplace_back(new NeoVIFIRE2USB(neodevice)); // Creation of the shared_ptr
return found;
}
private:
NeoVIFIRE2USB(neodevice_t neodevice) : NeoVIFIRE2(neodevice) {
initialize<FTDI, NeoVIFIRE2Settings>();
productId = PRODUCT_ID;
}
};
}
#endif // __cplusplus
#endif