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() {
std::vector<uint8_t> 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);

View File

@ -18,7 +18,7 @@ class Communication {
public:
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(); }
bool open();
@ -40,6 +40,8 @@ public:
void setAlign16Bit(bool enable) { align16bit = enable; }
std::shared_ptr<Packetizer> packetizer;
std::shared_ptr<MessageDecoder> decoder;
protected:
std::shared_ptr<ICommunication> impl;

View File

@ -9,7 +9,7 @@ namespace icsneo {
class MultiChannelCommunication : public Communication {
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 joinThreads();
bool sendCommand(Command cmd, std::vector<uint8_t> arguments);

View File

@ -32,8 +32,6 @@ void MultiChannelCommunication::readTask() {
std::deque<uint8_t> usbReadFifo;
std::vector<uint8_t> readBytes;
std::vector<uint8_t> 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);

View File

@ -1,79 +1,81 @@
#ifndef __DEVICE_H__
#define __DEVICE_H__
#include <vector>
#include <memory>
#include <cstring>
#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<std::shared_ptr<Message>> getMessages();
bool getMessages(std::vector<std::shared_ptr<Message>>& 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<IDeviceSettings> settings;
protected:
uint16_t productId = 0;
bool online = false;
int messagePollingCallbackID = 0;
std::shared_ptr<Communication> 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<std::shared_ptr<Message>> pollingContainer;
void enforcePollingMessageLimit();
};
};
#ifndef __DEVICE_H__
#define __DEVICE_H__
#include <vector>
#include <memory>
#include <cstring>
#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<std::shared_ptr<Message>> getMessages();
bool getMessages(std::vector<std::shared_ptr<Message>>& 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<IDeviceSettings> settings;
protected:
uint16_t productId = 0;
bool online = false;
int messagePollingCallbackID = 0;
std::shared_ptr<Communication> 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<std::shared_ptr<Message>> pollingContainer;
void enforcePollingMessageLimit();
};
};
#endif

View File

@ -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<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);
productId = PRODUCT_ID;
}

View File

@ -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<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);
productId = PRODUCT_ID;
}

View File

@ -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<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);
productId = PRODUCT_ID;
}

View File

@ -3,6 +3,7 @@
#include "device/neovifire2/include/neovifire2.h"
#include "platform/include/pcap.h"
#include <memory>
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<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;
}
static std::vector<std::shared_ptr<Device>> Find() {
std::vector<std::shared_ptr<Device>> found;
for(auto neodevice : PCAP::FindByProduct(PRODUCT_ID))
found.push_back(std::make_shared<NeoVIFIRE2ETH>(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<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;
}

View File

@ -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<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;
}

View File

@ -10,7 +10,10 @@ namespace icsneo {
class Plasion : public Device {
public:
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 "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<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);
productId = PRODUCT_ID;
}
@ -20,8 +27,14 @@ public:
static std::vector<std::shared_ptr<Device>> Find() {
std::vector<std::shared_ptr<Device>> found;
for(auto neodevice : PCAP::FindByProduct(PRODUCT_ID))
found.push_back(std::make_shared<RADGalaxy>(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<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;
}

View File

@ -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<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);
productId = PRODUCT_ID;
}

View File

@ -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<Communication>(std::make_shared<FTDI>(getWritableNeoDevice()));
com->setAlign16Bit(false);
auto transport = std::make_shared<FTDI>(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);
productId = PRODUCT_ID;
}

View File

@ -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<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);
productId = PRODUCT_ID;
}

View File

@ -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<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);
productId = PRODUCT_ID;
}

View File

@ -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<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);
productId = PRODUCT_ID;
}