From aa25ba1728a55fd69759c08311c54987a1d0e93c Mon Sep 17 00:00:00 2001 From: Paul Hollinsky Date: Tue, 25 Sep 2018 17:47:27 -0400 Subject: [PATCH] Communication uses instantiated Packetizers and Decoders now --- communication/communication.cpp | 8 +- communication/include/communication.h | 4 +- .../include/multichannelcommunication.h | 2 +- communication/multichannelcommunication.cpp | 8 +- device/include/device.h | 158 +++++++++--------- device/neoobd2pro/include/neoobd2pro.h | 5 +- device/neoobd2sim/include/neoobd2sim.h | 5 +- device/neovifire/include/neovifire.h | 5 +- device/neovifire2/include/neovifire2eth.h | 16 +- device/neovifire2/include/neovifire2usb.h | 5 +- device/plasion/include/plasion.h | 5 +- device/radgalaxy/include/radgalaxy.h | 19 ++- device/radstar2/include/radstar2.h | 5 +- device/radsupermoon/include/radsupermoon.h | 8 +- device/valuecan3/include/valuecan3.h | 5 +- device/valuecan4/include/valuecan4.h | 5 +- device/vividcan/include/vividcan.h | 5 +- 17 files changed, 161 insertions(+), 107 deletions(-) diff --git a/communication/communication.cpp b/communication/communication.cpp index 768e980..3846419 100644 --- a/communication/communication.cpp +++ b/communication/communication.cpp @@ -138,15 +138,13 @@ std::shared_ptr Communication::waitForMessageSync(MessageFilter f, std: void Communication::readTask() { std::vector readBytes; - Packetizer packetizer; - MessageDecoder decoder; while(!closing) { readBytes.clear(); if(impl->readWait(readBytes)) { - if(packetizer.input(readBytes)) { - for(auto& packet : packetizer.output()) { - auto msg = decoder.decodePacket(packet); + if(packetizer->input(readBytes)) { + for(auto& packet : packetizer->output()) { + auto msg = decoder->decodePacket(packet); for(auto& cb : messageCallbacks) { // We might have closed while reading or processing if(!closing) { cb.second.callIfMatch(msg); diff --git a/communication/include/communication.h b/communication/include/communication.h index a8d9f3e..431e77e 100644 --- a/communication/include/communication.h +++ b/communication/include/communication.h @@ -18,7 +18,7 @@ class Communication { public: static uint8_t ICSChecksum(const std::vector& data); - Communication(std::shared_ptr com) : impl(com) {} + Communication(std::shared_ptr com, std::shared_ptr p, std::shared_ptr md) : impl(com), packetizer(p), decoder(md) {} virtual ~Communication() { close(); } bool open(); @@ -40,6 +40,8 @@ public: void setAlign16Bit(bool enable) { align16bit = enable; } + std::shared_ptr packetizer; + std::shared_ptr decoder; protected: std::shared_ptr impl; diff --git a/communication/include/multichannelcommunication.h b/communication/include/multichannelcommunication.h index 2224a4a..9a7a9c9 100644 --- a/communication/include/multichannelcommunication.h +++ b/communication/include/multichannelcommunication.h @@ -9,7 +9,7 @@ namespace icsneo { class MultiChannelCommunication : public Communication { public: - MultiChannelCommunication(std::shared_ptr com) : Communication(com) {} + MultiChannelCommunication(std::shared_ptr com, std::shared_ptr p, std::shared_ptr md) : Communication(com, p, md) {} void spawnThreads(); void joinThreads(); bool sendCommand(Command cmd, std::vector arguments); diff --git a/communication/multichannelcommunication.cpp b/communication/multichannelcommunication.cpp index 017e828..2e8a3c6 100644 --- a/communication/multichannelcommunication.cpp +++ b/communication/multichannelcommunication.cpp @@ -32,8 +32,6 @@ void MultiChannelCommunication::readTask() { std::deque usbReadFifo; std::vector readBytes; std::vector payloadBytes; - Packetizer packetizer; - MessageDecoder decoder; while(!closing) { if(readMore) { @@ -108,9 +106,9 @@ void MultiChannelCommunication::readTask() { usbReadFifo.pop_front(); } - if(packetizer.input(payloadBytes)) { - for(auto& packet : packetizer.output()) { - auto msg = decoder.decodePacket(packet); + if(packetizer->input(payloadBytes)) { + for(auto& packet : packetizer->output()) { + auto msg = decoder->decodePacket(packet); for(auto& cb : messageCallbacks) { // We might have closed while reading or processing if(!closing) { cb.second.callIfMatch(msg); diff --git a/device/include/device.h b/device/include/device.h index ad14f39..ceea119 100644 --- a/device/include/device.h +++ b/device/include/device.h @@ -1,79 +1,81 @@ -#ifndef __DEVICE_H__ -#define __DEVICE_H__ - -#include -#include -#include -#include "device/include/neodevice.h" -#include "device/include/idevicesettings.h" -#include "communication/include/communication.h" -#include "third-party/concurrentqueue/concurrentqueue.h" - -namespace icsneo { - -class Device { -public: - Device(neodevice_t neodevice = {}) { - data = neodevice; - data.device = this; - setProductName("undefined"); - } - virtual ~Device() { - disableMessagePolling(); - close(); - } - - static std::string SerialNumToString(uint32_t serial); - static uint32_t SerialStringToNum(const std::string& serial); - static bool SerialStringIsNumeric(const std::string& serial); - - std::string getProductName() const { return data.type; } - uint16_t getProductId() const { return productId; } - std::string getSerial() const { return data.serial; } - uint32_t getSerialNumber() const { return Device::SerialStringToNum(getSerial()); } - const neodevice_t& getNeoDevice() const { return data; } - - virtual bool open(); - virtual bool close(); - virtual bool isOnline() const { return online; } - virtual bool goOnline(); - virtual bool goOffline(); - - // Message polling related functions - void enableMessagePolling(); - bool disableMessagePolling(); - std::vector> getMessages(); - bool getMessages(std::vector>& container, size_t limit = 0); - size_t getCurrentMessageCount() { return pollingContainer.size_approx(); } - size_t getPollingMessageLimit() { return pollingMessageLimit; } - void setPollingMessageLimit(size_t newSize) { - pollingMessageLimit = newSize; - enforcePollingMessageLimit(); - } - - std::shared_ptr settings; - -protected: - uint16_t productId = 0; - bool online = false; - int messagePollingCallbackID = 0; - std::shared_ptr com; - - neodevice_t& getWritableNeoDevice() { return data; } - void setProductName(const std::string& newName) { - #pragma warning( disable : 4996 ) - auto copied = newName.copy(data.type, sizeof(data.type) - 1); - data.type[copied] = '\0'; - } - -private: - neodevice_t data; - - size_t pollingMessageLimit = 20000; - moodycamel::ConcurrentQueue> pollingContainer; - void enforcePollingMessageLimit(); -}; - -}; - +#ifndef __DEVICE_H__ +#define __DEVICE_H__ + +#include +#include +#include +#include "device/include/neodevice.h" +#include "device/include/idevicesettings.h" +#include "communication/include/communication.h" +#include "communication/include/packetizer.h" +#include "communication/include/messagedecoder.h" +#include "third-party/concurrentqueue/concurrentqueue.h" + +namespace icsneo { + +class Device { +public: + Device(neodevice_t neodevice = {}) { + data = neodevice; + data.device = this; + setProductName("undefined"); + } + virtual ~Device() { + disableMessagePolling(); + close(); + } + + static std::string SerialNumToString(uint32_t serial); + static uint32_t SerialStringToNum(const std::string& serial); + static bool SerialStringIsNumeric(const std::string& serial); + + std::string getProductName() const { return data.type; } + uint16_t getProductId() const { return productId; } + std::string getSerial() const { return data.serial; } + uint32_t getSerialNumber() const { return Device::SerialStringToNum(getSerial()); } + const neodevice_t& getNeoDevice() const { return data; } + + virtual bool open(); + virtual bool close(); + virtual bool isOnline() const { return online; } + virtual bool goOnline(); + virtual bool goOffline(); + + // Message polling related functions + void enableMessagePolling(); + bool disableMessagePolling(); + std::vector> getMessages(); + bool getMessages(std::vector>& container, size_t limit = 0); + size_t getCurrentMessageCount() { return pollingContainer.size_approx(); } + size_t getPollingMessageLimit() { return pollingMessageLimit; } + void setPollingMessageLimit(size_t newSize) { + pollingMessageLimit = newSize; + enforcePollingMessageLimit(); + } + + std::shared_ptr settings; + +protected: + uint16_t productId = 0; + bool online = false; + int messagePollingCallbackID = 0; + std::shared_ptr com; + + neodevice_t& getWritableNeoDevice() { return data; } + void setProductName(const std::string& newName) { + #pragma warning( disable : 4996 ) + auto copied = newName.copy(data.type, sizeof(data.type) - 1); + data.type[copied] = '\0'; + } + +private: + neodevice_t data; + + size_t pollingMessageLimit = 20000; + moodycamel::ConcurrentQueue> pollingContainer; + void enforcePollingMessageLimit(); +}; + +}; + #endif \ No newline at end of file diff --git a/device/neoobd2pro/include/neoobd2pro.h b/device/neoobd2pro/include/neoobd2pro.h index 4ada734..f88d824 100644 --- a/device/neoobd2pro/include/neoobd2pro.h +++ b/device/neoobd2pro/include/neoobd2pro.h @@ -12,7 +12,10 @@ public: static constexpr const char* PRODUCT_NAME = "neoOBD2 PRO"; static constexpr const uint16_t PRODUCT_ID = 0x1103; NeoOBD2PRO(neodevice_t neodevice) : Device(neodevice) { - com = std::make_shared(std::make_shared(getWritableNeoDevice())); + auto transport = std::make_shared(getWritableNeoDevice()); + auto packetizer = std::make_shared(); + auto decoder = std::make_shared(); + com = std::make_shared(transport, packetizer, decoder); setProductName(PRODUCT_NAME); productId = PRODUCT_ID; } diff --git a/device/neoobd2sim/include/neoobd2sim.h b/device/neoobd2sim/include/neoobd2sim.h index 46cbddf..22d171c 100644 --- a/device/neoobd2sim/include/neoobd2sim.h +++ b/device/neoobd2sim/include/neoobd2sim.h @@ -12,7 +12,10 @@ public: static constexpr const char* PRODUCT_NAME = "neoOBD2-SIM"; static constexpr const uint16_t PRODUCT_ID = 0x1100; NeoOBD2SIM(neodevice_t neodevice) : Device(neodevice) { - com = std::make_shared(std::make_shared(getWritableNeoDevice())); + auto transport = std::make_shared(getWritableNeoDevice()); + auto packetizer = std::make_shared(); + auto decoder = std::make_shared(); + com = std::make_shared(transport, packetizer, decoder); setProductName(PRODUCT_NAME); productId = PRODUCT_ID; } diff --git a/device/neovifire/include/neovifire.h b/device/neovifire/include/neovifire.h index 5a40402..e6e3d3a 100644 --- a/device/neovifire/include/neovifire.h +++ b/device/neovifire/include/neovifire.h @@ -11,7 +11,10 @@ public: static constexpr const char* PRODUCT_NAME = "neoVI FIRE"; static constexpr const uint16_t PRODUCT_ID = 0x0701; NeoVIFIRE(neodevice_t neodevice) : Device(neodevice) { - com = std::make_shared(std::make_shared(getWritableNeoDevice())); + auto transport = std::make_shared(getWritableNeoDevice()); + auto packetizer = std::make_shared(); + auto decoder = std::make_shared(); + com = std::make_shared(transport, packetizer, decoder); setProductName(PRODUCT_NAME); productId = PRODUCT_ID; } diff --git a/device/neovifire2/include/neovifire2eth.h b/device/neovifire2/include/neovifire2eth.h index b000d96..9636c34 100644 --- a/device/neovifire2/include/neovifire2eth.h +++ b/device/neovifire2/include/neovifire2eth.h @@ -3,6 +3,7 @@ #include "device/neovifire2/include/neovifire2.h" #include "platform/include/pcap.h" +#include namespace icsneo { @@ -10,15 +11,24 @@ class NeoVIFIRE2ETH : public NeoVIFIRE2 { public: static constexpr const uint16_t PRODUCT_ID = 0x0004; NeoVIFIRE2ETH(neodevice_t neodevice) : NeoVIFIRE2(neodevice) { - com = std::make_shared(std::make_shared(getWritableNeoDevice())); + auto transport = std::make_shared(getWritableNeoDevice()); + auto packetizer = std::make_shared(); + auto decoder = std::make_shared(); + com = std::make_shared(transport, packetizer, decoder); productId = PRODUCT_ID; } static std::vector> Find() { std::vector> found; - for(auto neodevice : PCAP::FindByProduct(PRODUCT_ID)) - found.push_back(std::make_shared(neodevice)); + for(auto neodevice : PCAP::FindByProduct(PRODUCT_ID)) { + strncpy(neodevice.serial, SERIAL_FIND_ON_OPEN, sizeof(neodevice.serial)); + neodevice.serial[sizeof(neodevice.serial) - 1] = '\0'; + auto device = std::make_shared(neodevice); + if(!device->open()) // We will get the serial number on open + continue; // If the open failed, we won't display the device as an option to connect to + found.push_back(device); + } return found; } diff --git a/device/neovifire2/include/neovifire2usb.h b/device/neovifire2/include/neovifire2usb.h index 0f8e9a3..e622d0a 100644 --- a/device/neovifire2/include/neovifire2usb.h +++ b/device/neovifire2/include/neovifire2usb.h @@ -10,7 +10,10 @@ class NeoVIFIRE2USB : public NeoVIFIRE2 { public: static constexpr const uint16_t PRODUCT_ID = 0x1000; NeoVIFIRE2USB(neodevice_t neodevice) : NeoVIFIRE2(neodevice) { - com = std::make_shared(std::make_shared(getWritableNeoDevice())); + auto transport = std::make_shared(getWritableNeoDevice()); + auto packetizer = std::make_shared(); + auto decoder = std::make_shared(); + com = std::make_shared(transport, packetizer, decoder); productId = PRODUCT_ID; } diff --git a/device/plasion/include/plasion.h b/device/plasion/include/plasion.h index 9fc3ad0..7cb09c4 100644 --- a/device/plasion/include/plasion.h +++ b/device/plasion/include/plasion.h @@ -10,7 +10,10 @@ namespace icsneo { class Plasion : public Device { public: Plasion(neodevice_t neodevice) : Device(neodevice) { - com = std::make_shared(std::make_shared(getWritableNeoDevice())); + auto transport = std::make_shared(getWritableNeoDevice()); + auto packetizer = std::make_shared(); + auto decoder = std::make_shared(); + com = std::make_shared(transport, packetizer, decoder); } }; diff --git a/device/radgalaxy/include/radgalaxy.h b/device/radgalaxy/include/radgalaxy.h index d9e99ce..44bc510 100644 --- a/device/radgalaxy/include/radgalaxy.h +++ b/device/radgalaxy/include/radgalaxy.h @@ -3,6 +3,8 @@ #include "device/include/device.h" #include "platform/include/pcap.h" +#include "communication/include/packetizer.h" +#include "communication/include/messagedecoder.h" namespace icsneo { @@ -12,7 +14,12 @@ public: static constexpr const char* PRODUCT_NAME = "RADGalaxy"; static constexpr const uint16_t PRODUCT_ID = 0x0003; RADGalaxy(neodevice_t neodevice) : Device(neodevice) { - com = std::make_shared(std::make_shared(getWritableNeoDevice())); + auto transport = std::make_shared(getWritableNeoDevice()); + auto packetizer = std::make_shared(); + packetizer->disableChecksum = true; + packetizer->align16bit = false; + auto decoder = std::make_shared(); + com = std::make_shared(transport, packetizer, decoder); setProductName(PRODUCT_NAME); productId = PRODUCT_ID; } @@ -20,8 +27,14 @@ public: static std::vector> Find() { std::vector> found; - for(auto neodevice : PCAP::FindByProduct(PRODUCT_ID)) - found.push_back(std::make_shared(neodevice)); + for(auto neodevice : PCAP::FindByProduct(PRODUCT_ID)) { + strncpy(neodevice.serial, SERIAL_FIND_ON_OPEN, sizeof(neodevice.serial)); + neodevice.serial[sizeof(neodevice.serial) - 1] = '\0'; + auto device = std::make_shared(neodevice); + if(!device->open()) // We will get the serial number on open + continue; // If the open failed, we won't display the device as an option to connect to + found.push_back(device); + } return found; } diff --git a/device/radstar2/include/radstar2.h b/device/radstar2/include/radstar2.h index d550b5b..0a5efd1 100644 --- a/device/radstar2/include/radstar2.h +++ b/device/radstar2/include/radstar2.h @@ -12,7 +12,10 @@ public: static constexpr const char* PRODUCT_NAME = "RADStar 2"; static constexpr const uint16_t PRODUCT_ID = 0x0005; RADStar2(neodevice_t neodevice) : Device(neodevice) { - com = std::make_shared(std::make_shared(getWritableNeoDevice())); + auto transport = std::make_shared(getWritableNeoDevice()); + auto packetizer = std::make_shared(); + auto decoder = std::make_shared(); + com = std::make_shared(transport, packetizer, decoder); setProductName(PRODUCT_NAME); productId = PRODUCT_ID; } diff --git a/device/radsupermoon/include/radsupermoon.h b/device/radsupermoon/include/radsupermoon.h index dee9d61..9ca97f9 100644 --- a/device/radsupermoon/include/radsupermoon.h +++ b/device/radsupermoon/include/radsupermoon.h @@ -12,8 +12,12 @@ public: static constexpr const char* PRODUCT_NAME = "RADSupermoon"; static constexpr const uint16_t PRODUCT_ID = 0x1201; RADSupermoon(neodevice_t neodevice) : Device(neodevice) { - com = std::make_shared(std::make_shared(getWritableNeoDevice())); - com->setAlign16Bit(false); + auto transport = std::make_shared(getWritableNeoDevice()); + auto packetizer = std::make_shared(); + packetizer->disableChecksum = true; + packetizer->align16bit = false; + auto decoder = std::make_shared(); + com = std::make_shared(transport, packetizer, decoder); setProductName(PRODUCT_NAME); productId = PRODUCT_ID; } diff --git a/device/valuecan3/include/valuecan3.h b/device/valuecan3/include/valuecan3.h index f812238..9108f86 100644 --- a/device/valuecan3/include/valuecan3.h +++ b/device/valuecan3/include/valuecan3.h @@ -11,7 +11,10 @@ public: static constexpr const char* PRODUCT_NAME = "ValueCAN 3"; static constexpr const uint16_t PRODUCT_ID = 0x0601; ValueCAN3(neodevice_t neodevice) : Device(neodevice) { - com = std::make_shared(std::make_shared(getWritableNeoDevice())); + auto transport = std::make_shared(getWritableNeoDevice()); + auto packetizer = std::make_shared(); + auto decoder = std::make_shared(); + com = std::make_shared(transport, packetizer, decoder); setProductName(PRODUCT_NAME); productId = PRODUCT_ID; } diff --git a/device/valuecan4/include/valuecan4.h b/device/valuecan4/include/valuecan4.h index 283e8a2..3638a3e 100644 --- a/device/valuecan4/include/valuecan4.h +++ b/device/valuecan4/include/valuecan4.h @@ -12,7 +12,10 @@ public: static constexpr const char* PRODUCT_NAME = "ValueCAN 4"; static constexpr const uint16_t PRODUCT_ID = 0x1101; ValueCAN4(neodevice_t neodevice) : Device(neodevice) { - com = std::make_shared(std::make_shared(getWritableNeoDevice())); + auto transport = std::make_shared(getWritableNeoDevice()); + auto packetizer = std::make_shared(); + auto decoder = std::make_shared(); + com = std::make_shared(transport, packetizer, decoder); setProductName(PRODUCT_NAME); productId = PRODUCT_ID; } diff --git a/device/vividcan/include/vividcan.h b/device/vividcan/include/vividcan.h index 172048c..ae00100 100644 --- a/device/vividcan/include/vividcan.h +++ b/device/vividcan/include/vividcan.h @@ -12,7 +12,10 @@ public: static constexpr const char* PRODUCT_NAME = "VividCAN"; static constexpr const uint16_t PRODUCT_ID = 0x1102; VividCAN(neodevice_t neodevice) : Device(neodevice) { - com = std::make_shared(std::make_shared(getWritableNeoDevice())); + auto transport = std::make_shared(getWritableNeoDevice()); + auto packetizer = std::make_shared(); + auto decoder = std::make_shared(); + com = std::make_shared(transport, packetizer, decoder); setProductName(PRODUCT_NAME); productId = PRODUCT_ID; }