Refactor ICommunication/Transport => Driver

pull/25/head
Paul Hollinsky 2020-03-09 14:09:27 -04:00
parent d8798acaa7
commit 38e24d7641
17 changed files with 47 additions and 47 deletions

View File

@ -111,7 +111,7 @@ set(COMMON_SRC
communication/packetizer.cpp communication/packetizer.cpp
communication/multichannelcommunication.cpp communication/multichannelcommunication.cpp
communication/communication.cpp communication/communication.cpp
communication/icommunication.cpp communication/driver.cpp
device/extensions/flexray/extension.cpp device/extensions/flexray/extension.cpp
device/extensions/flexray/controller.cpp device/extensions/flexray/controller.cpp
device/idevicesettings.cpp device/idevicesettings.cpp

View File

@ -24,7 +24,7 @@ bool Communication::open() {
} }
spawnThreads(); spawnThreads();
return impl->open(); return driver->open();
} }
void Communication::spawnThreads() { void Communication::spawnThreads() {
@ -46,11 +46,11 @@ bool Communication::close() {
joinThreads(); joinThreads();
return impl->close(); return driver->close();
} }
bool Communication::isOpen() { bool Communication::isOpen() {
return impl->isOpen(); return driver->isOpen();
} }
bool Communication::sendPacket(std::vector<uint8_t>& bytes) { bool Communication::sendPacket(std::vector<uint8_t>& bytes) {
@ -174,7 +174,7 @@ void Communication::readTask() {
while(!closing) { while(!closing) {
readBytes.clear(); readBytes.clear();
if(impl->readWait(readBytes)) { if(driver->readWait(readBytes)) {
if(packetizer->input(readBytes)) { if(packetizer->input(readBytes)) {
for(const auto& packet : packetizer->output()) { for(const auto& packet : packetizer->output()) {
std::shared_ptr<Message> msg; std::shared_ptr<Message> msg;

View File

@ -1,8 +1,8 @@
#include "icsneo/communication/icommunication.h" #include "icsneo/communication/driver.h"
using namespace icsneo; using namespace icsneo;
bool ICommunication::read(std::vector<uint8_t>& bytes, size_t limit) { bool Driver::read(std::vector<uint8_t>& bytes, size_t limit) {
// A limit of zero indicates no limit // A limit of zero indicates no limit
if(limit == 0) if(limit == 0)
limit = (size_t)-1; limit = (size_t)-1;
@ -21,7 +21,7 @@ bool ICommunication::read(std::vector<uint8_t>& bytes, size_t limit) {
return true; return true;
} }
bool ICommunication::readWait(std::vector<uint8_t>& bytes, std::chrono::milliseconds timeout, size_t limit) { bool Driver::readWait(std::vector<uint8_t>& bytes, std::chrono::milliseconds timeout, size_t limit) {
// A limit of zero indicates no limit // A limit of zero indicates no limit
if(limit == 0) if(limit == 0)
limit = (size_t)-1; limit = (size_t)-1;
@ -38,7 +38,7 @@ bool ICommunication::readWait(std::vector<uint8_t>& bytes, std::chrono::millisec
return actuallyRead > 0; return actuallyRead > 0;
} }
bool ICommunication::write(const std::vector<uint8_t>& bytes) { bool Driver::write(const std::vector<uint8_t>& bytes) {
if(!isOpen()) { if(!isOpen()) {
report(APIEvent::Type::DeviceCurrentlyClosed, APIEvent::Severity::Error); report(APIEvent::Type::DeviceCurrentlyClosed, APIEvent::Severity::Error);
return false; return false;

View File

@ -41,7 +41,7 @@ void MultiChannelCommunication::hidReadTask() {
while(!closing) { while(!closing) {
if(readMore) { if(readMore) {
readBytes.clear(); readBytes.clear();
if(impl->readWait(readBytes)) { if(driver->readWait(readBytes)) {
readMore = false; readMore = false;
usbReadFifo.insert(usbReadFifo.end(), std::make_move_iterator(readBytes.begin()), std::make_move_iterator(readBytes.end())); usbReadFifo.insert(usbReadFifo.end(), std::make_move_iterator(readBytes.begin()), std::make_move_iterator(readBytes.end()));
} }

View File

@ -1,7 +1,7 @@
#ifndef __COMMUNICATION_H_ #ifndef __COMMUNICATION_H_
#define __COMMUNICATION_H_ #define __COMMUNICATION_H_
#include "icsneo/communication/icommunication.h" #include "icsneo/communication/driver.h"
#include "icsneo/communication/command.h" #include "icsneo/communication/command.h"
#include "icsneo/communication/network.h" #include "icsneo/communication/network.h"
#include "icsneo/communication/packet.h" #include "icsneo/communication/packet.h"
@ -24,10 +24,10 @@ class Communication {
public: public:
Communication( Communication(
device_eventhandler_t report, device_eventhandler_t report,
std::unique_ptr<ICommunication>&& com, std::unique_ptr<Driver>&& driver,
std::function<std::unique_ptr<Packetizer>()> makeConfiguredPacketizer, std::function<std::unique_ptr<Packetizer>()> makeConfiguredPacketizer,
std::unique_ptr<Encoder>&& e, std::unique_ptr<Encoder>&& e,
std::unique_ptr<Decoder>&& md) : makeConfiguredPacketizer(makeConfiguredPacketizer), encoder(std::move(e)), decoder(std::move(md)), report(report), impl(std::move(com)) { std::unique_ptr<Decoder>&& md) : makeConfiguredPacketizer(makeConfiguredPacketizer), encoder(std::move(e)), decoder(std::move(md)), report(report), driver(std::move(driver)) {
packetizer = makeConfiguredPacketizer(); packetizer = makeConfiguredPacketizer();
} }
virtual ~Communication() { close(); } virtual ~Communication() { close(); }
@ -37,10 +37,10 @@ public:
bool isOpen(); bool isOpen();
virtual void spawnThreads(); virtual void spawnThreads();
virtual void joinThreads(); virtual void joinThreads();
bool rawWrite(const std::vector<uint8_t>& bytes) { return impl->write(bytes); } bool rawWrite(const std::vector<uint8_t>& bytes) { return driver->write(bytes); }
virtual bool sendPacket(std::vector<uint8_t>& bytes); virtual bool sendPacket(std::vector<uint8_t>& bytes);
void setWriteBlocks(bool blocks) { impl->writeBlocks = blocks; } void setWriteBlocks(bool blocks) { driver->writeBlocks = blocks; }
virtual bool sendCommand(Command cmd, bool boolean) { return sendCommand(cmd, std::vector<uint8_t>({ (uint8_t)boolean })); } virtual bool sendCommand(Command cmd, bool boolean) { return sendCommand(cmd, std::vector<uint8_t>({ (uint8_t)boolean })); }
virtual bool sendCommand(Command cmd, std::vector<uint8_t> arguments = {}); virtual bool sendCommand(Command cmd, std::vector<uint8_t> arguments = {});
@ -68,7 +68,7 @@ public:
device_eventhandler_t report; device_eventhandler_t report;
protected: protected:
std::unique_ptr<ICommunication> impl; std::unique_ptr<Driver> driver;
static int messageCallbackIDCounter; static int messageCallbackIDCounter;
std::mutex messageCallbacksLock; std::mutex messageCallbacksLock;
std::map<int, MessageCallback> messageCallbacks; std::map<int, MessageCallback> messageCallbacks;

View File

@ -1,5 +1,5 @@
#ifndef __ICOMMUNICATION_H_ #ifndef __DRIVER_H_
#define __ICOMMUNICATION_H_ #define __DRIVER_H_
#include <vector> #include <vector>
#include <chrono> #include <chrono>
@ -12,10 +12,10 @@
namespace icsneo { namespace icsneo {
class ICommunication { class Driver {
public: public:
ICommunication(const device_eventhandler_t& handler) : report(handler) {} Driver(const device_eventhandler_t& handler) : report(handler) {}
virtual ~ICommunication() {} virtual ~Driver() {}
virtual bool open() = 0; virtual bool open() = 0;
virtual bool isOpen() = 0; virtual bool isOpen() = 0;
virtual bool close() = 0; virtual bool close() = 0;

View File

@ -2,7 +2,7 @@
#define __MULTICHANNELCOMMUNICATION_H_ #define __MULTICHANNELCOMMUNICATION_H_
#include "icsneo/communication/communication.h" #include "icsneo/communication/communication.h"
#include "icsneo/communication/icommunication.h" #include "icsneo/communication/driver.h"
#include "icsneo/communication/command.h" #include "icsneo/communication/command.h"
#include "icsneo/communication/encoder.h" #include "icsneo/communication/encoder.h"
#include "icsneo/third-party/readerwriterqueue/readerwriterqueue.h" #include "icsneo/third-party/readerwriterqueue/readerwriterqueue.h"
@ -13,7 +13,7 @@ class MultiChannelCommunication : public Communication {
public: public:
MultiChannelCommunication( MultiChannelCommunication(
device_eventhandler_t err, device_eventhandler_t err,
std::unique_ptr<ICommunication> com, std::unique_ptr<Driver> com,
std::function<std::unique_ptr<Packetizer>()> makeConfiguredPacketizer, std::function<std::unique_ptr<Packetizer>()> makeConfiguredPacketizer,
std::unique_ptr<Encoder> e, std::unique_ptr<Encoder> e,
std::unique_ptr<Decoder> md) : Communication(err, std::move(com), makeConfiguredPacketizer, std::move(e), std::move(md)) {} std::unique_ptr<Decoder> md) : Communication(err, std::move(com), makeConfiguredPacketizer, std::move(e), std::move(md)) {}

View File

@ -106,16 +106,16 @@ protected:
data.device = this; data.device = this;
} }
template<typename Transport, typename Settings = NullSettings> template<typename Driver, typename Settings = NullSettings>
void initialize() { void initialize() {
report = makeEventHandler(); report = makeEventHandler();
auto transport = makeTransport<Transport>(); auto driver = makeDriver<Driver>();
setupTransport(*transport); setupDriver(*driver);
auto encoder = makeEncoder(); auto encoder = makeEncoder();
setupEncoder(*encoder); setupEncoder(*encoder);
auto decoder = makeDecoder(); auto decoder = makeDecoder();
setupDecoder(*decoder); setupDecoder(*decoder);
com = makeCommunication(std::move(transport), std::bind(&Device::makeConfiguredPacketizer, this), std::move(encoder), std::move(decoder)); com = makeCommunication(std::move(driver), std::bind(&Device::makeConfiguredPacketizer, this), std::move(encoder), std::move(decoder));
setupCommunication(*com); setupCommunication(*com);
settings = makeSettings<Settings>(com); settings = makeSettings<Settings>(com);
setupSettings(*settings); setupSettings(*settings);
@ -130,9 +130,9 @@ protected:
}; };
} }
template<typename Transport> template<typename Driver>
std::unique_ptr<ICommunication> makeTransport() { return std::unique_ptr<ICommunication>(new Transport(report, getWritableNeoDevice())); } std::unique_ptr<Driver> makeDriver() { return std::unique_ptr<Driver>(new Driver(report, getWritableNeoDevice())); }
virtual void setupTransport(ICommunication&) {} virtual void setupDriver(Driver&) {}
virtual std::unique_ptr<Packetizer> makePacketizer() { return std::unique_ptr<Packetizer>(new Packetizer(report)); } virtual std::unique_ptr<Packetizer> makePacketizer() { return std::unique_ptr<Packetizer>(new Packetizer(report)); }
virtual void setupPacketizer(Packetizer&) {} virtual void setupPacketizer(Packetizer&) {}
@ -149,7 +149,7 @@ protected:
virtual void setupDecoder(Decoder&) {} virtual void setupDecoder(Decoder&) {}
virtual std::shared_ptr<Communication> makeCommunication( virtual std::shared_ptr<Communication> makeCommunication(
std::unique_ptr<ICommunication> t, std::unique_ptr<Driver> t,
std::function<std::unique_ptr<Packetizer>()> makeConfiguredPacketizer, std::function<std::unique_ptr<Packetizer>()> makeConfiguredPacketizer,
std::unique_ptr<Encoder> e, std::unique_ptr<Encoder> e,
std::unique_ptr<Decoder> d) { return std::make_shared<Communication>(report, std::move(t), makeConfiguredPacketizer, std::move(e), std::move(d)); } std::unique_ptr<Decoder> d) { return std::make_shared<Communication>(report, std::move(t), makeConfiguredPacketizer, std::move(e), std::move(d)); }

View File

@ -11,7 +11,7 @@ namespace icsneo {
class Plasion : public Device { class Plasion : public Device {
protected: protected:
virtual std::shared_ptr<Communication> makeCommunication( virtual std::shared_ptr<Communication> makeCommunication(
std::unique_ptr<ICommunication> transport, std::unique_ptr<Driver> transport,
std::function<std::unique_ptr<Packetizer>()> makeConfiguredPacketizer, std::function<std::unique_ptr<Packetizer>()> makeConfiguredPacketizer,
std::unique_ptr<Encoder> encoder, std::unique_ptr<Encoder> encoder,
std::unique_ptr<Decoder> decoder std::unique_ptr<Decoder> decoder

View File

@ -6,13 +6,13 @@
#include <string> #include <string>
#include <ftdi.h> #include <ftdi.h>
#include "icsneo/device/neodevice.h" #include "icsneo/device/neodevice.h"
#include "icsneo/communication/icommunication.h" #include "icsneo/communication/driver.h"
#include "icsneo/third-party/concurrentqueue/blockingconcurrentqueue.h" #include "icsneo/third-party/concurrentqueue/blockingconcurrentqueue.h"
#include "icsneo/api/eventmanager.h" #include "icsneo/api/eventmanager.h"
namespace icsneo { namespace icsneo {
class FTDI : public ICommunication { class FTDI : public Driver {
public: public:
static std::vector<neodevice_t> FindByProduct(int product); static std::vector<neodevice_t> FindByProduct(int product);

View File

@ -2,14 +2,14 @@
#define __PCAP_POSIX_H_ #define __PCAP_POSIX_H_
#include "icsneo/device/neodevice.h" #include "icsneo/device/neodevice.h"
#include "icsneo/communication/icommunication.h" #include "icsneo/communication/driver.h"
#include "icsneo/api/eventmanager.h" #include "icsneo/api/eventmanager.h"
#include <string> #include <string>
#include <pcap.h> #include <pcap.h>
namespace icsneo { namespace icsneo {
class PCAP : public ICommunication { class PCAP : public Driver {
public: public:
class PCAPFoundDevice { class PCAPFoundDevice {
public: public:

View File

@ -1,7 +1,7 @@
#ifndef __STM32_POSIX_H_ #ifndef __STM32_POSIX_H_
#define __STM32_POSIX_H_ #define __STM32_POSIX_H_
#include "icsneo/communication/icommunication.h" #include "icsneo/communication/driver.h"
#include "icsneo/device/neodevice.h" #include "icsneo/device/neodevice.h"
#include "icsneo/api/eventmanager.h" #include "icsneo/api/eventmanager.h"
#include <chrono> #include <chrono>
@ -9,7 +9,7 @@
namespace icsneo { namespace icsneo {
class STM32 : public ICommunication { class STM32 : public Driver {
public: public:
/* /*
* Note: This is a driver for all devices which use CDC_ACM * Note: This is a driver for all devices which use CDC_ACM
@ -21,7 +21,7 @@ public:
* in stm32linux.cpp and stm32darwin.cpp respectively * in stm32linux.cpp and stm32darwin.cpp respectively
* Other POSIX systems (BSDs, QNX, etc) will need bespoke code written in the future * Other POSIX systems (BSDs, QNX, etc) will need bespoke code written in the future
*/ */
STM32(const device_eventhandler_t& err, neodevice_t& forDevice) : ICommunication(err), device(forDevice) {} STM32(const device_eventhandler_t& err, neodevice_t& forDevice) : Driver(err), device(forDevice) {}
static std::vector<neodevice_t> FindByProduct(int product); static std::vector<neodevice_t> FindByProduct(int product);
bool open(); bool open();

View File

@ -3,13 +3,13 @@
#include "icsneo/platform/windows/internal/pcapdll.h" #include "icsneo/platform/windows/internal/pcapdll.h"
#include "icsneo/device/neodevice.h" #include "icsneo/device/neodevice.h"
#include "icsneo/communication/icommunication.h" #include "icsneo/communication/driver.h"
#include "icsneo/api/eventmanager.h" #include "icsneo/api/eventmanager.h"
#include <string> #include <string>
namespace icsneo { namespace icsneo {
class PCAP : public ICommunication { class PCAP : public Driver {
public: public:
class PCAPFoundDevice { class PCAPFoundDevice {
public: public:

View File

@ -8,19 +8,19 @@
#include <chrono> #include <chrono>
#include <Windows.h> #include <Windows.h>
#include "icsneo/device/neodevice.h" #include "icsneo/device/neodevice.h"
#include "icsneo/communication/icommunication.h" #include "icsneo/communication/driver.h"
#include "icsneo/api/eventmanager.h" #include "icsneo/api/eventmanager.h"
namespace icsneo { namespace icsneo {
// Virtual COM Port Communication // Virtual COM Port Communication
class VCP : public ICommunication { class VCP : public Driver {
public: public:
static std::vector<neodevice_t> FindByProduct(int product, std::vector<std::wstring> driverName); static std::vector<neodevice_t> FindByProduct(int product, std::vector<std::wstring> driverName);
static bool IsHandleValid(neodevice_handle_t handle); static bool IsHandleValid(neodevice_handle_t handle);
typedef void(*fn_boolCallback)(bool success); typedef void(*fn_boolCallback)(bool success);
VCP(const device_eventhandler_t& err, neodevice_t& forDevice) : ICommunication(err), device(forDevice) { VCP(const device_eventhandler_t& err, neodevice_t& forDevice) : Driver(err), device(forDevice) {
overlappedRead.hEvent = INVALID_HANDLE_VALUE; overlappedRead.hEvent = INVALID_HANDLE_VALUE;
overlappedWrite.hEvent = INVALID_HANDLE_VALUE; overlappedWrite.hEvent = INVALID_HANDLE_VALUE;
overlappedWait.hEvent = INVALID_HANDLE_VALUE; overlappedWait.hEvent = INVALID_HANDLE_VALUE;

View File

@ -39,7 +39,7 @@ std::vector<neodevice_t> FTDI::FindByProduct(int product) {
return found; return found;
} }
FTDI::FTDI(const device_eventhandler_t& err, neodevice_t& forDevice) : ICommunication(err), device(forDevice) { FTDI::FTDI(const device_eventhandler_t& err, neodevice_t& forDevice) : Driver(err), device(forDevice) {
openable = strlen(forDevice.serial) > 0 && device.handle >= 0 && device.handle < (neodevice_handle_t)handles.size(); openable = strlen(forDevice.serial) > 0 && device.handle >= 0 && device.handle < (neodevice_handle_t)handles.size();
} }

View File

@ -173,7 +173,7 @@ bool PCAP::IsHandleValid(neodevice_handle_t handle) {
return (netifIndex < knownInterfaces.size()); return (netifIndex < knownInterfaces.size());
} }
PCAP::PCAP(device_eventhandler_t err, neodevice_t& forDevice) : ICommunication(err), device(forDevice) { PCAP::PCAP(device_eventhandler_t err, neodevice_t& forDevice) : Driver(err), device(forDevice) {
if(IsHandleValid(device.handle)) { if(IsHandleValid(device.handle)) {
interface = knownInterfaces[(device.handle >> 24) & 0xFF]; interface = knownInterfaces[(device.handle >> 24) & 0xFF];
interface.fp = nullptr; // We're going to open our own connection to the interface. This should already be nullptr but just in case. interface.fp = nullptr; // We're going to open our own connection to the interface. This should already be nullptr but just in case.

View File

@ -177,7 +177,7 @@ bool PCAP::IsHandleValid(neodevice_handle_t handle) {
return (netifIndex < knownInterfaces.size()); return (netifIndex < knownInterfaces.size());
} }
PCAP::PCAP(const device_eventhandler_t& err, neodevice_t& forDevice) : ICommunication(err), device(forDevice) { PCAP::PCAP(const device_eventhandler_t& err, neodevice_t& forDevice) : Driver(err), device(forDevice) {
if(IsHandleValid(device.handle)) { if(IsHandleValid(device.handle)) {
interface = knownInterfaces[(device.handle >> 24) & 0xFF]; interface = knownInterfaces[(device.handle >> 24) & 0xFF];
interface.fp = nullptr; // We're going to open our own connection to the interface. This should already be nullptr but just in case. interface.fp = nullptr; // We're going to open our own connection to the interface. This should already be nullptr but just in case.