Communication uses instantiated Packetizers and Decoders now

pull/4/head
Paul Hollinsky 2018-09-25 17:47:27 -04:00
parent 585abe7cbb
commit aa25ba1728
17 changed files with 161 additions and 107 deletions

View File

@ -138,15 +138,13 @@ std::shared_ptr<Message> Communication::waitForMessageSync(MessageFilter f, std:
void Communication::readTask() { void Communication::readTask() {
std::vector<uint8_t> readBytes; std::vector<uint8_t> readBytes;
Packetizer packetizer;
MessageDecoder decoder;
while(!closing) { while(!closing) {
readBytes.clear(); readBytes.clear();
if(impl->readWait(readBytes)) { if(impl->readWait(readBytes)) {
if(packetizer.input(readBytes)) { if(packetizer->input(readBytes)) {
for(auto& packet : packetizer.output()) { for(auto& packet : packetizer->output()) {
auto msg = decoder.decodePacket(packet); auto msg = decoder->decodePacket(packet);
for(auto& cb : messageCallbacks) { // We might have closed while reading or processing for(auto& cb : messageCallbacks) { // We might have closed while reading or processing
if(!closing) { if(!closing) {
cb.second.callIfMatch(msg); cb.second.callIfMatch(msg);

View File

@ -18,7 +18,7 @@ class Communication {
public: public:
static uint8_t ICSChecksum(const std::vector<uint8_t>& data); static uint8_t ICSChecksum(const std::vector<uint8_t>& data);
Communication(std::shared_ptr<ICommunication> com) : impl(com) {} Communication(std::shared_ptr<ICommunication> com, std::shared_ptr<Packetizer> p, std::shared_ptr<MessageDecoder> md) : impl(com), packetizer(p), decoder(md) {}
virtual ~Communication() { close(); } virtual ~Communication() { close(); }
bool open(); bool open();
@ -40,6 +40,8 @@ public:
void setAlign16Bit(bool enable) { align16bit = enable; } void setAlign16Bit(bool enable) { align16bit = enable; }
std::shared_ptr<Packetizer> packetizer;
std::shared_ptr<MessageDecoder> decoder;
protected: protected:
std::shared_ptr<ICommunication> impl; std::shared_ptr<ICommunication> impl;

View File

@ -9,7 +9,7 @@ namespace icsneo {
class MultiChannelCommunication : public Communication { class MultiChannelCommunication : public Communication {
public: public:
MultiChannelCommunication(std::shared_ptr<ICommunication> com) : Communication(com) {} MultiChannelCommunication(std::shared_ptr<ICommunication> com, std::shared_ptr<Packetizer> p, std::shared_ptr<MessageDecoder> md) : Communication(com, p, md) {}
void spawnThreads(); void spawnThreads();
void joinThreads(); void joinThreads();
bool sendCommand(Command cmd, std::vector<uint8_t> arguments); bool sendCommand(Command cmd, std::vector<uint8_t> arguments);

View File

@ -32,8 +32,6 @@ void MultiChannelCommunication::readTask() {
std::deque<uint8_t> usbReadFifo; std::deque<uint8_t> usbReadFifo;
std::vector<uint8_t> readBytes; std::vector<uint8_t> readBytes;
std::vector<uint8_t> payloadBytes; std::vector<uint8_t> payloadBytes;
Packetizer packetizer;
MessageDecoder decoder;
while(!closing) { while(!closing) {
if(readMore) { if(readMore) {
@ -108,9 +106,9 @@ void MultiChannelCommunication::readTask() {
usbReadFifo.pop_front(); usbReadFifo.pop_front();
} }
if(packetizer.input(payloadBytes)) { if(packetizer->input(payloadBytes)) {
for(auto& packet : packetizer.output()) { for(auto& packet : packetizer->output()) {
auto msg = decoder.decodePacket(packet); auto msg = decoder->decodePacket(packet);
for(auto& cb : messageCallbacks) { // We might have closed while reading or processing for(auto& cb : messageCallbacks) { // We might have closed while reading or processing
if(!closing) { if(!closing) {
cb.second.callIfMatch(msg); cb.second.callIfMatch(msg);

View File

@ -1,79 +1,81 @@
#ifndef __DEVICE_H__ #ifndef __DEVICE_H__
#define __DEVICE_H__ #define __DEVICE_H__
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <cstring> #include <cstring>
#include "device/include/neodevice.h" #include "device/include/neodevice.h"
#include "device/include/idevicesettings.h" #include "device/include/idevicesettings.h"
#include "communication/include/communication.h" #include "communication/include/communication.h"
#include "third-party/concurrentqueue/concurrentqueue.h" #include "communication/include/packetizer.h"
#include "communication/include/messagedecoder.h"
namespace icsneo { #include "third-party/concurrentqueue/concurrentqueue.h"
class Device { namespace icsneo {
public:
Device(neodevice_t neodevice = {}) { class Device {
data = neodevice; public:
data.device = this; Device(neodevice_t neodevice = {}) {
setProductName("undefined"); data = neodevice;
} data.device = this;
virtual ~Device() { setProductName("undefined");
disableMessagePolling(); }
close(); 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); static std::string SerialNumToString(uint32_t serial);
static uint32_t SerialStringToNum(const std::string& serial);
std::string getProductName() const { return data.type; } static bool SerialStringIsNumeric(const std::string& serial);
uint16_t getProductId() const { return productId; }
std::string getSerial() const { return data.serial; } std::string getProductName() const { return data.type; }
uint32_t getSerialNumber() const { return Device::SerialStringToNum(getSerial()); } uint16_t getProductId() const { return productId; }
const neodevice_t& getNeoDevice() const { return data; } std::string getSerial() const { return data.serial; }
uint32_t getSerialNumber() const { return Device::SerialStringToNum(getSerial()); }
virtual bool open(); const neodevice_t& getNeoDevice() const { return data; }
virtual bool close();
virtual bool isOnline() const { return online; } virtual bool open();
virtual bool goOnline(); virtual bool close();
virtual bool goOffline(); virtual bool isOnline() const { return online; }
virtual bool goOnline();
// Message polling related functions virtual bool goOffline();
void enableMessagePolling();
bool disableMessagePolling(); // Message polling related functions
std::vector<std::shared_ptr<Message>> getMessages(); void enableMessagePolling();
bool getMessages(std::vector<std::shared_ptr<Message>>& container, size_t limit = 0); bool disableMessagePolling();
size_t getCurrentMessageCount() { return pollingContainer.size_approx(); } std::vector<std::shared_ptr<Message>> getMessages();
size_t getPollingMessageLimit() { return pollingMessageLimit; } bool getMessages(std::vector<std::shared_ptr<Message>>& container, size_t limit = 0);
void setPollingMessageLimit(size_t newSize) { size_t getCurrentMessageCount() { return pollingContainer.size_approx(); }
pollingMessageLimit = newSize; size_t getPollingMessageLimit() { return pollingMessageLimit; }
enforcePollingMessageLimit(); void setPollingMessageLimit(size_t newSize) {
} pollingMessageLimit = newSize;
enforcePollingMessageLimit();
std::shared_ptr<IDeviceSettings> settings; }
protected: std::shared_ptr<IDeviceSettings> settings;
uint16_t productId = 0;
bool online = false; protected:
int messagePollingCallbackID = 0; uint16_t productId = 0;
std::shared_ptr<Communication> com; bool online = false;
int messagePollingCallbackID = 0;
neodevice_t& getWritableNeoDevice() { return data; } std::shared_ptr<Communication> com;
void setProductName(const std::string& newName) {
#pragma warning( disable : 4996 ) neodevice_t& getWritableNeoDevice() { return data; }
auto copied = newName.copy(data.type, sizeof(data.type) - 1); void setProductName(const std::string& newName) {
data.type[copied] = '\0'; #pragma warning( disable : 4996 )
} auto copied = newName.copy(data.type, sizeof(data.type) - 1);
data.type[copied] = '\0';
private: }
neodevice_t data;
private:
size_t pollingMessageLimit = 20000; neodevice_t data;
moodycamel::ConcurrentQueue<std::shared_ptr<Message>> pollingContainer;
void enforcePollingMessageLimit(); size_t pollingMessageLimit = 20000;
}; moodycamel::ConcurrentQueue<std::shared_ptr<Message>> pollingContainer;
void enforcePollingMessageLimit();
}; };
};
#endif #endif

View File

@ -12,7 +12,10 @@ public:
static constexpr const char* PRODUCT_NAME = "neoOBD2 PRO"; static constexpr const char* PRODUCT_NAME = "neoOBD2 PRO";
static constexpr const uint16_t PRODUCT_ID = 0x1103; static constexpr const uint16_t PRODUCT_ID = 0x1103;
NeoOBD2PRO(neodevice_t neodevice) : Device(neodevice) { NeoOBD2PRO(neodevice_t neodevice) : Device(neodevice) {
com = std::make_shared<Communication>(std::make_shared<STM32>(getWritableNeoDevice())); auto transport = std::make_shared<STM32>(getWritableNeoDevice());
auto packetizer = std::make_shared<Packetizer>();
auto decoder = std::make_shared<MessageDecoder>();
com = std::make_shared<Communication>(transport, packetizer, decoder);
setProductName(PRODUCT_NAME); setProductName(PRODUCT_NAME);
productId = PRODUCT_ID; productId = PRODUCT_ID;
} }

View File

@ -12,7 +12,10 @@ public:
static constexpr const char* PRODUCT_NAME = "neoOBD2-SIM"; static constexpr const char* PRODUCT_NAME = "neoOBD2-SIM";
static constexpr const uint16_t PRODUCT_ID = 0x1100; static constexpr const uint16_t PRODUCT_ID = 0x1100;
NeoOBD2SIM(neodevice_t neodevice) : Device(neodevice) { NeoOBD2SIM(neodevice_t neodevice) : Device(neodevice) {
com = std::make_shared<Communication>(std::make_shared<STM32>(getWritableNeoDevice())); auto transport = std::make_shared<STM32>(getWritableNeoDevice());
auto packetizer = std::make_shared<Packetizer>();
auto decoder = std::make_shared<MessageDecoder>();
com = std::make_shared<Communication>(transport, packetizer, decoder);
setProductName(PRODUCT_NAME); setProductName(PRODUCT_NAME);
productId = PRODUCT_ID; productId = PRODUCT_ID;
} }

View File

@ -11,7 +11,10 @@ public:
static constexpr const char* PRODUCT_NAME = "neoVI FIRE"; static constexpr const char* PRODUCT_NAME = "neoVI FIRE";
static constexpr const uint16_t PRODUCT_ID = 0x0701; static constexpr const uint16_t PRODUCT_ID = 0x0701;
NeoVIFIRE(neodevice_t neodevice) : Device(neodevice) { NeoVIFIRE(neodevice_t neodevice) : Device(neodevice) {
com = std::make_shared<Communication>(std::make_shared<FTDI>(getWritableNeoDevice())); auto transport = std::make_shared<FTDI>(getWritableNeoDevice());
auto packetizer = std::make_shared<Packetizer>();
auto decoder = std::make_shared<MessageDecoder>();
com = std::make_shared<Communication>(transport, packetizer, decoder);
setProductName(PRODUCT_NAME); setProductName(PRODUCT_NAME);
productId = PRODUCT_ID; productId = PRODUCT_ID;
} }

View File

@ -3,6 +3,7 @@
#include "device/neovifire2/include/neovifire2.h" #include "device/neovifire2/include/neovifire2.h"
#include "platform/include/pcap.h" #include "platform/include/pcap.h"
#include <memory>
namespace icsneo { namespace icsneo {
@ -10,15 +11,24 @@ class NeoVIFIRE2ETH : public NeoVIFIRE2 {
public: public:
static constexpr const uint16_t PRODUCT_ID = 0x0004; static constexpr const uint16_t PRODUCT_ID = 0x0004;
NeoVIFIRE2ETH(neodevice_t neodevice) : NeoVIFIRE2(neodevice) { NeoVIFIRE2ETH(neodevice_t neodevice) : NeoVIFIRE2(neodevice) {
com = std::make_shared<Communication>(std::make_shared<PCAP>(getWritableNeoDevice())); auto transport = std::make_shared<PCAP>(getWritableNeoDevice());
auto packetizer = std::make_shared<Packetizer>();
auto decoder = std::make_shared<MessageDecoder>();
com = std::make_shared<Communication>(transport, packetizer, decoder);
productId = PRODUCT_ID; productId = PRODUCT_ID;
} }
static std::vector<std::shared_ptr<Device>> Find() { static std::vector<std::shared_ptr<Device>> Find() {
std::vector<std::shared_ptr<Device>> found; std::vector<std::shared_ptr<Device>> found;
for(auto neodevice : PCAP::FindByProduct(PRODUCT_ID)) for(auto neodevice : PCAP::FindByProduct(PRODUCT_ID)) {
found.push_back(std::make_shared<NeoVIFIRE2ETH>(neodevice)); strncpy(neodevice.serial, SERIAL_FIND_ON_OPEN, sizeof(neodevice.serial));
neodevice.serial[sizeof(neodevice.serial) - 1] = '\0';
auto device = std::make_shared<NeoVIFIRE2ETH>(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; return found;
} }

View File

@ -10,7 +10,10 @@ class NeoVIFIRE2USB : public NeoVIFIRE2 {
public: public:
static constexpr const uint16_t PRODUCT_ID = 0x1000; static constexpr const uint16_t PRODUCT_ID = 0x1000;
NeoVIFIRE2USB(neodevice_t neodevice) : NeoVIFIRE2(neodevice) { NeoVIFIRE2USB(neodevice_t neodevice) : NeoVIFIRE2(neodevice) {
com = std::make_shared<Communication>(std::make_shared<FTDI>(getWritableNeoDevice())); auto transport = std::make_shared<FTDI>(getWritableNeoDevice());
auto packetizer = std::make_shared<Packetizer>();
auto decoder = std::make_shared<MessageDecoder>();
com = std::make_shared<Communication>(transport, packetizer, decoder);
productId = PRODUCT_ID; productId = PRODUCT_ID;
} }

View File

@ -10,7 +10,10 @@ namespace icsneo {
class Plasion : public Device { class Plasion : public Device {
public: public:
Plasion(neodevice_t neodevice) : Device(neodevice) { Plasion(neodevice_t neodevice) : Device(neodevice) {
com = std::make_shared<MultiChannelCommunication>(std::make_shared<FTDI>(getWritableNeoDevice())); auto transport = std::make_shared<FTDI>(getWritableNeoDevice());
auto packetizer = std::make_shared<Packetizer>();
auto decoder = std::make_shared<MessageDecoder>();
com = std::make_shared<MultiChannelCommunication>(transport, packetizer, decoder);
} }
}; };

View File

@ -3,6 +3,8 @@
#include "device/include/device.h" #include "device/include/device.h"
#include "platform/include/pcap.h" #include "platform/include/pcap.h"
#include "communication/include/packetizer.h"
#include "communication/include/messagedecoder.h"
namespace icsneo { namespace icsneo {
@ -12,7 +14,12 @@ public:
static constexpr const char* PRODUCT_NAME = "RADGalaxy"; static constexpr const char* PRODUCT_NAME = "RADGalaxy";
static constexpr const uint16_t PRODUCT_ID = 0x0003; static constexpr const uint16_t PRODUCT_ID = 0x0003;
RADGalaxy(neodevice_t neodevice) : Device(neodevice) { RADGalaxy(neodevice_t neodevice) : Device(neodevice) {
com = std::make_shared<Communication>(std::make_shared<PCAP>(getWritableNeoDevice())); auto transport = std::make_shared<PCAP>(getWritableNeoDevice());
auto packetizer = std::make_shared<Packetizer>();
packetizer->disableChecksum = true;
packetizer->align16bit = false;
auto decoder = std::make_shared<MessageDecoder>();
com = std::make_shared<Communication>(transport, packetizer, decoder);
setProductName(PRODUCT_NAME); setProductName(PRODUCT_NAME);
productId = PRODUCT_ID; productId = PRODUCT_ID;
} }
@ -20,8 +27,14 @@ public:
static std::vector<std::shared_ptr<Device>> Find() { static std::vector<std::shared_ptr<Device>> Find() {
std::vector<std::shared_ptr<Device>> found; std::vector<std::shared_ptr<Device>> found;
for(auto neodevice : PCAP::FindByProduct(PRODUCT_ID)) for(auto neodevice : PCAP::FindByProduct(PRODUCT_ID)) {
found.push_back(std::make_shared<RADGalaxy>(neodevice)); strncpy(neodevice.serial, SERIAL_FIND_ON_OPEN, sizeof(neodevice.serial));
neodevice.serial[sizeof(neodevice.serial) - 1] = '\0';
auto device = std::make_shared<RADGalaxy>(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; return found;
} }

View File

@ -12,7 +12,10 @@ public:
static constexpr const char* PRODUCT_NAME = "RADStar 2"; static constexpr const char* PRODUCT_NAME = "RADStar 2";
static constexpr const uint16_t PRODUCT_ID = 0x0005; static constexpr const uint16_t PRODUCT_ID = 0x0005;
RADStar2(neodevice_t neodevice) : Device(neodevice) { RADStar2(neodevice_t neodevice) : Device(neodevice) {
com = std::make_shared<Communication>(std::make_shared<FTDI>(getWritableNeoDevice())); auto transport = std::make_shared<FTDI>(getWritableNeoDevice());
auto packetizer = std::make_shared<Packetizer>();
auto decoder = std::make_shared<MessageDecoder>();
com = std::make_shared<Communication>(transport, packetizer, decoder);
setProductName(PRODUCT_NAME); setProductName(PRODUCT_NAME);
productId = PRODUCT_ID; productId = PRODUCT_ID;
} }

View File

@ -12,8 +12,12 @@ public:
static constexpr const char* PRODUCT_NAME = "RADSupermoon"; static constexpr const char* PRODUCT_NAME = "RADSupermoon";
static constexpr const uint16_t PRODUCT_ID = 0x1201; static constexpr const uint16_t PRODUCT_ID = 0x1201;
RADSupermoon(neodevice_t neodevice) : Device(neodevice) { RADSupermoon(neodevice_t neodevice) : Device(neodevice) {
com = std::make_shared<Communication>(std::make_shared<FTDI>(getWritableNeoDevice())); auto transport = std::make_shared<FTDI>(getWritableNeoDevice());
com->setAlign16Bit(false); auto packetizer = std::make_shared<Packetizer>();
packetizer->disableChecksum = true;
packetizer->align16bit = false;
auto decoder = std::make_shared<MessageDecoder>();
com = std::make_shared<Communication>(transport, packetizer, decoder);
setProductName(PRODUCT_NAME); setProductName(PRODUCT_NAME);
productId = PRODUCT_ID; productId = PRODUCT_ID;
} }

View File

@ -11,7 +11,10 @@ public:
static constexpr const char* PRODUCT_NAME = "ValueCAN 3"; static constexpr const char* PRODUCT_NAME = "ValueCAN 3";
static constexpr const uint16_t PRODUCT_ID = 0x0601; static constexpr const uint16_t PRODUCT_ID = 0x0601;
ValueCAN3(neodevice_t neodevice) : Device(neodevice) { ValueCAN3(neodevice_t neodevice) : Device(neodevice) {
com = std::make_shared<Communication>(std::make_shared<FTDI>(getWritableNeoDevice())); auto transport = std::make_shared<FTDI>(getWritableNeoDevice());
auto packetizer = std::make_shared<Packetizer>();
auto decoder = std::make_shared<MessageDecoder>();
com = std::make_shared<Communication>(transport, packetizer, decoder);
setProductName(PRODUCT_NAME); setProductName(PRODUCT_NAME);
productId = PRODUCT_ID; productId = PRODUCT_ID;
} }

View File

@ -12,7 +12,10 @@ public:
static constexpr const char* PRODUCT_NAME = "ValueCAN 4"; static constexpr const char* PRODUCT_NAME = "ValueCAN 4";
static constexpr const uint16_t PRODUCT_ID = 0x1101; static constexpr const uint16_t PRODUCT_ID = 0x1101;
ValueCAN4(neodevice_t neodevice) : Device(neodevice) { ValueCAN4(neodevice_t neodevice) : Device(neodevice) {
com = std::make_shared<Communication>(std::make_shared<STM32>(getWritableNeoDevice())); auto transport = std::make_shared<STM32>(getWritableNeoDevice());
auto packetizer = std::make_shared<Packetizer>();
auto decoder = std::make_shared<MessageDecoder>();
com = std::make_shared<Communication>(transport, packetizer, decoder);
setProductName(PRODUCT_NAME); setProductName(PRODUCT_NAME);
productId = PRODUCT_ID; productId = PRODUCT_ID;
} }

View File

@ -12,7 +12,10 @@ public:
static constexpr const char* PRODUCT_NAME = "VividCAN"; static constexpr const char* PRODUCT_NAME = "VividCAN";
static constexpr const uint16_t PRODUCT_ID = 0x1102; static constexpr const uint16_t PRODUCT_ID = 0x1102;
VividCAN(neodevice_t neodevice) : Device(neodevice) { VividCAN(neodevice_t neodevice) : Device(neodevice) {
com = std::make_shared<Communication>(std::make_shared<STM32>(getWritableNeoDevice())); auto transport = std::make_shared<STM32>(getWritableNeoDevice());
auto packetizer = std::make_shared<Packetizer>();
auto decoder = std::make_shared<MessageDecoder>();
com = std::make_shared<Communication>(transport, packetizer, decoder);
setProductName(PRODUCT_NAME); setProductName(PRODUCT_NAME);
productId = PRODUCT_ID; productId = PRODUCT_ID;
} }