diff --git a/device/devicefinder.cpp b/device/devicefinder.cpp index 7262866..5aa19e3 100644 --- a/device/devicefinder.cpp +++ b/device/devicefinder.cpp @@ -94,8 +94,12 @@ static std::vector supportedDevices = { ValueCAN4_2::DEVICE_TYPE, #endif - #ifdef __VALUECAN4_2EL_H_ - ValueCAN4_2EL::DEVICE_TYPE, + #ifdef __VALUECAN4_2EL_ETH_H_ + ValueCAN4_2EL_ETH::DEVICE_TYPE, + #endif + + #ifdef __VALUECAN4_2EL_USB_H_ + ValueCAN4_2EL_USB::DEVICE_TYPE, #endif #ifdef __VALUECAN4_4_H_ @@ -204,8 +208,12 @@ std::vector> DeviceFinder::FindAll() { findResults.push_back(ValueCAN4_2::Find()); #endif - #ifdef __VALUECAN4_2EL_H_ - findResults.push_back(ValueCAN4_2EL::Find()); + #ifdef __VALUECAN4_2EL_ETH_H_ + findResults.push_back(ValueCAN4_2EL_ETH::Find(pcapDevices)); + #endif + + #ifdef __VALUECAN4_2EL_USB_H_ + findResults.push_back(ValueCAN4_2EL_USB::Find()); #endif #ifdef __VALUECAN4_4_H_ diff --git a/include/icsneo/device/tree/valuecan4/valuecan4-1.h b/include/icsneo/device/tree/valuecan4/valuecan4-1.h index 3810ff5..5572b13 100644 --- a/include/icsneo/device/tree/valuecan4/valuecan4-1.h +++ b/include/icsneo/device/tree/valuecan4/valuecan4-1.h @@ -5,6 +5,7 @@ #include "icsneo/device/tree/valuecan4/valuecan4.h" #include "icsneo/device/tree/valuecan4/settings/valuecan4-1settings.h" +#include "icsneo/platform/stm32.h" #include namespace icsneo { @@ -13,11 +14,13 @@ class ValueCAN4_1 : public ValueCAN4 { public: // Serial numbers start with V1 for 4-1 static constexpr DeviceType::Enum DEVICE_TYPE = DeviceType::VCAN4_1; + static constexpr const char* SERIAL_START = "V1"; + static std::vector> Find() { std::vector> found; - for(auto neodevice : STM32::FindByProduct(PRODUCT_ID)) { - if(std::string(neodevice.serial).substr(0, 2) == "V1") + for(auto neodevice : STM32::FindByProduct(USB_PRODUCT_ID)) { + if(std::string(neodevice.serial).substr(0, 2) == SERIAL_START) found.emplace_back(new ValueCAN4_1(neodevice)); } @@ -49,6 +52,7 @@ private: ValueCAN4_1(neodevice_t neodevice) : ValueCAN4(neodevice) { initialize(); getWritableNeoDevice().type = DEVICE_TYPE; + productId = USB_PRODUCT_ID; } }; diff --git a/include/icsneo/device/tree/valuecan4/valuecan4-2.h b/include/icsneo/device/tree/valuecan4/valuecan4-2.h index 68e424b..e996cec 100644 --- a/include/icsneo/device/tree/valuecan4/valuecan4-2.h +++ b/include/icsneo/device/tree/valuecan4/valuecan4-2.h @@ -5,6 +5,7 @@ #include "icsneo/device/tree/valuecan4/valuecan4.h" #include "icsneo/device/tree/valuecan4/settings/valuecan4-2settings.h" +#include "icsneo/platform/stm32.h" #include namespace icsneo { @@ -13,11 +14,13 @@ class ValueCAN4_2 : public ValueCAN4 { public: // Serial numbers start with V2 for 4-2 static constexpr DeviceType::Enum DEVICE_TYPE = DeviceType::VCAN4_2; + static constexpr const char* SERIAL_START = "V2"; + static std::vector> Find() { std::vector> found; - for(auto neodevice : STM32::FindByProduct(PRODUCT_ID)) { - if(std::string(neodevice.serial).substr(0, 2) == "V2") + for(auto neodevice : STM32::FindByProduct(USB_PRODUCT_ID)) { + if(std::string(neodevice.serial).substr(0, 2) == SERIAL_START) found.emplace_back(new ValueCAN4_2(neodevice)); } @@ -45,6 +48,7 @@ private: ValueCAN4_2(neodevice_t neodevice) : ValueCAN4(neodevice) { initialize(); getWritableNeoDevice().type = DEVICE_TYPE; + productId = USB_PRODUCT_ID; } }; diff --git a/include/icsneo/device/tree/valuecan4/valuecan4-2el.h b/include/icsneo/device/tree/valuecan4/valuecan4-2el.h index e0b6f86..126636f 100644 --- a/include/icsneo/device/tree/valuecan4/valuecan4-2el.h +++ b/include/icsneo/device/tree/valuecan4/valuecan4-2el.h @@ -13,39 +13,10 @@ class ValueCAN4_2EL : public ValueCAN4 { public: // Serial numbers start with VE for 4-2EL static constexpr DeviceType::Enum DEVICE_TYPE = DeviceType::VCAN4_2EL; - static std::vector> Find() { - std::vector> found; - - for(auto neodevice : STM32::FindByProduct(PRODUCT_ID)) { - if(std::string(neodevice.serial).substr(0, 2) == "VE") - found.emplace_back(new ValueCAN4_2EL(neodevice)); - } - - return found; - } - - static const std::vector& GetSupportedNetworks() { - static std::vector supportedNetworks = { - Network::NetID::HSCAN, - Network::NetID::HSCAN2, - - Network::NetID::Ethernet - }; - return supportedNetworks; - } + static constexpr const char* SERIAL_START = "VE"; protected: - virtual void setupSupportedRXNetworks(std::vector& 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& txNetworks) override { setupSupportedRXNetworks(txNetworks); } - -private: ValueCAN4_2EL(neodevice_t neodevice) : ValueCAN4(neodevice) { - initialize(); getWritableNeoDevice().type = DEVICE_TYPE; } }; diff --git a/include/icsneo/device/tree/valuecan4/valuecan4-2eleth.h b/include/icsneo/device/tree/valuecan4/valuecan4-2eleth.h new file mode 100644 index 0000000..d08bf5d --- /dev/null +++ b/include/icsneo/device/tree/valuecan4/valuecan4-2eleth.h @@ -0,0 +1,79 @@ +#ifndef __VALUECAN4_2EL_ETH_H_ +#define __VALUECAN4_2EL_ETH_H_ + +#ifdef __cplusplus + +#include "icsneo/device/tree/valuecan4/valuecan4-2el.h" +#include "icsneo/platform/pcap.h" +#include + +namespace icsneo { + +class ValueCAN4_2EL_ETH : public ValueCAN4_2EL { +public: + static constexpr const uint16_t ETH_PRODUCT_ID = 0x000B; + + static std::vector> Find(const std::vector& pcapDevices) { + std::vector> found; + + for(auto& foundDev : pcapDevices) { + auto fakedev = std::shared_ptr(new ValueCAN4_2EL_ETH({})); + for (auto& payload : foundDev.discoveryPackets) + fakedev->com->packetizer->input(payload); + for (auto& packet : fakedev->com->packetizer->output()) { + std::shared_ptr msg; + if (!fakedev->com->decoder->decode(msg, packet)) + continue; // We failed to decode this packet + + if(!msg || msg->network.getNetID() != Network::NetID::Main51) + continue; // Not a message we care about + auto sn = std::dynamic_pointer_cast(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 ValueCAN4-2EL + + auto device = foundDev.device; + device.serial[sn->deviceSerial.copy(device.serial, sizeof(device.serial))] = '\0'; + found.push_back(std::shared_ptr(new ValueCAN4_2EL_ETH(std::move(device)))); + break; + } + } + + return found; + } + + static const std::vector& GetSupportedNetworks() { + static std::vector supportedNetworks = { + Network::NetID::HSCAN, + Network::NetID::HSCAN2, + + // No Network::NetID::Ethernet, since we're communicating over it instead + }; + return supportedNetworks; + } + +protected: + virtual void setupSupportedRXNetworks(std::vector& 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& txNetworks) override { setupSupportedRXNetworks(txNetworks); } + +private: + ValueCAN4_2EL_ETH(neodevice_t neodevice) : ValueCAN4_2EL(neodevice) { + initialize(); + productId = ETH_PRODUCT_ID; + } +}; + +} + +#endif // __cplusplus + +#endif \ No newline at end of file diff --git a/include/icsneo/device/tree/valuecan4/valuecan4-2elusb.h b/include/icsneo/device/tree/valuecan4/valuecan4-2elusb.h new file mode 100644 index 0000000..c8f171b --- /dev/null +++ b/include/icsneo/device/tree/valuecan4/valuecan4-2elusb.h @@ -0,0 +1,55 @@ +#ifndef __VALUECAN4_2EL_USB_H_ +#define __VALUECAN4_2EL_USB_H_ + +#ifdef __cplusplus + +#include "icsneo/device/tree/valuecan4/valuecan4-2el.h" +#include "icsneo/platform/stm32.h" +#include + +namespace icsneo { + +class ValueCAN4_2EL_USB : public ValueCAN4_2EL { +public: + static std::vector> Find() { + std::vector> found; + + for(auto neodevice : STM32::FindByProduct(USB_PRODUCT_ID)) { + if(std::string(neodevice.serial).substr(0, 2) == SERIAL_START) + found.emplace_back(new ValueCAN4_2EL_USB(neodevice)); + } + + return found; + } + + static const std::vector& GetSupportedNetworks() { + static std::vector supportedNetworks = { + Network::NetID::HSCAN, + Network::NetID::HSCAN2, + + Network::NetID::Ethernet + }; + return supportedNetworks; + } + +protected: + virtual void setupSupportedRXNetworks(std::vector& 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& txNetworks) override { setupSupportedRXNetworks(txNetworks); } + +private: + ValueCAN4_2EL_USB(neodevice_t neodevice) : ValueCAN4_2EL(neodevice) { + initialize(); + productId = USB_PRODUCT_ID; + } +}; + +} + +#endif // __cplusplus + +#endif \ No newline at end of file diff --git a/include/icsneo/device/tree/valuecan4/valuecan4-4.h b/include/icsneo/device/tree/valuecan4/valuecan4-4.h index d396bc9..7c9d37d 100644 --- a/include/icsneo/device/tree/valuecan4/valuecan4-4.h +++ b/include/icsneo/device/tree/valuecan4/valuecan4-4.h @@ -5,6 +5,7 @@ #include "icsneo/device/tree/valuecan4/valuecan4.h" #include "icsneo/device/tree/valuecan4/settings/valuecan4-4settings.h" +#include "icsneo/platform/stm32.h" #include namespace icsneo { @@ -13,11 +14,13 @@ class ValueCAN4_4 : public ValueCAN4 { public: // Serial numbers start with V4 for 4-4 static constexpr DeviceType::Enum DEVICE_TYPE = DeviceType::VCAN4_4; + static constexpr const char* SERIAL_START = "V4"; + static std::vector> Find() { std::vector> found; - for(auto neodevice : STM32::FindByProduct(PRODUCT_ID)) { - if(std::string(neodevice.serial).substr(0, 2) == "V4") + for(auto neodevice : STM32::FindByProduct(USB_PRODUCT_ID)) { + if(std::string(neodevice.serial).substr(0, 2) == SERIAL_START) found.emplace_back(new ValueCAN4_4(neodevice)); } @@ -47,6 +50,7 @@ private: ValueCAN4_4(neodevice_t neodevice) : ValueCAN4(neodevice) { initialize(); getWritableNeoDevice().type = DEVICE_TYPE; + productId = USB_PRODUCT_ID; } }; diff --git a/include/icsneo/device/tree/valuecan4/valuecan4.h b/include/icsneo/device/tree/valuecan4/valuecan4.h index 5a27031..d7abe10 100644 --- a/include/icsneo/device/tree/valuecan4/valuecan4.h +++ b/include/icsneo/device/tree/valuecan4/valuecan4.h @@ -5,13 +5,13 @@ #include "icsneo/device/device.h" #include "icsneo/device/devicetype.h" -#include "icsneo/platform/stm32.h" namespace icsneo { class ValueCAN4 : public Device { public: - static constexpr const uint16_t PRODUCT_ID = 0x1101; + // All ValueCAN 4 devices share a USB PID + static constexpr const uint16_t USB_PRODUCT_ID = 0x1101; protected: virtual void setupEncoder(Encoder& encoder) override { @@ -19,9 +19,7 @@ protected: encoder.supportCANFD = true; } - ValueCAN4(neodevice_t neodevice) : Device(neodevice) { - productId = PRODUCT_ID; - } + ValueCAN4(neodevice_t neodevice) : Device(neodevice) {} }; } diff --git a/include/icsneo/platform/posix/devices.h b/include/icsneo/platform/posix/devices.h index 9891414..a4f3629 100644 --- a/include/icsneo/platform/posix/devices.h +++ b/include/icsneo/platform/posix/devices.h @@ -23,7 +23,8 @@ #include "icsneo/device/tree/valuecan3/valuecan3.h" #include "icsneo/device/tree/valuecan4/valuecan4-1.h" #include "icsneo/device/tree/valuecan4/valuecan4-2.h" -#include "icsneo/device/tree/valuecan4/valuecan4-2el.h" +#include "icsneo/device/tree/valuecan4/valuecan4-2eleth.h" +#include "icsneo/device/tree/valuecan4/valuecan4-2elusb.h" #include "icsneo/device/tree/valuecan4/valuecan4-4.h" #include "icsneo/device/tree/vividcan/vividcan.h" diff --git a/include/icsneo/platform/windows/devices.h b/include/icsneo/platform/windows/devices.h index 4a0bf2a..d1b408b 100644 --- a/include/icsneo/platform/windows/devices.h +++ b/include/icsneo/platform/windows/devices.h @@ -23,7 +23,8 @@ #include "icsneo/device/tree/valuecan3/valuecan3.h" #include "icsneo/device/tree/valuecan4/valuecan4-1.h" #include "icsneo/device/tree/valuecan4/valuecan4-2.h" -#include "icsneo/device/tree/valuecan4/valuecan4-2el.h" +#include "icsneo/device/tree/valuecan4/valuecan4-2eleth.h" +#include "icsneo/device/tree/valuecan4/valuecan4-2elusb.h" #include "icsneo/device/tree/valuecan4/valuecan4-4.h" #include "icsneo/device/tree/vividcan/vividcan.h"