Communication::Command refactored out of Communication

This commit is contained in:
Paul Hollinsky
2018-09-25 17:35:56 -04:00
parent 6284223650
commit d27b516894
6 changed files with 620 additions and 610 deletions
+16
View File
@@ -0,0 +1,16 @@
#ifndef __COMMAND_H_
#define __COMMAND_H_
namespace icsneo {
enum class Command : uint8_t {
EnableNetworkCommunication = 0x07,
RequestSerialNumber = 0xA1,
SetSettings = 0xA4, // Previously known as RED_CMD_SET_BAUD_REQ, follow up with SaveSettings to write to EEPROM
GetSettings = 0xA5, // Previously known as RED_CMD_READ_BAUD_REQ
SaveSettings = 0xA6
};
}
#endif
+64 -70
View File
@@ -1,71 +1,65 @@
#ifndef __COMMUNICATION_H_
#define __COMMUNICATION_H_
#include "communication/include/icommunication.h"
#include "communication/include/network.h"
#include "communication/include/messagecallback.h"
#include <memory>
#include <vector>
#include <atomic>
#include <thread>
#include <queue>
#include <map>
namespace icsneo {
class Communication {
public:
static uint8_t ICSChecksum(const std::vector<uint8_t>& data);
Communication(std::shared_ptr<ICommunication> com) : impl(com) {}
virtual ~Communication() { close(); }
bool open();
bool close();
virtual void spawnThreads();
virtual void joinThreads();
bool rawWrite(const std::vector<uint8_t>& bytes) { return impl->write(bytes); }
std::vector<uint8_t>& packetWrap(std::vector<uint8_t>& data, bool addChecksum = true);
bool sendPacket(std::vector<uint8_t>& bytes);
enum class Command : uint8_t {
EnableNetworkCommunication = 0x07,
RequestSerialNumber = 0xA1,
SetSettings = 0xA4, // Previously known as RED_CMD_SET_BAUD_REQ, follow up with SaveSettings to write to EEPROM
GetSettings = 0xA5, // Previously known as RED_CMD_READ_BAUD_REQ
SaveSettings = 0xA6
};
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 = {});
bool getSettingsSync(std::vector<uint8_t>& data, std::chrono::milliseconds timeout = std::chrono::milliseconds(50));
bool getSerialNumberSync(std::string& serial, std::chrono::milliseconds timeout = std::chrono::milliseconds(50));
int addMessageCallback(const MessageCallback& cb);
bool removeMessageCallback(int id);
std::shared_ptr<Message> waitForMessageSync(MessageFilter f = MessageFilter(), std::chrono::milliseconds timeout = std::chrono::milliseconds(50));
void setAlign16Bit(bool enable) { align16bit = enable; }
class Packet {
public:
Network network;
std::vector<uint8_t> data;
};
protected:
std::shared_ptr<ICommunication> impl;
static int messageCallbackIDCounter;
std::map<int, MessageCallback> messageCallbacks;
std::atomic<bool> closing{false};
private:
bool isOpen = false;
bool align16bit = true; // Not needed for Gigalog, Galaxy, etc and newer
std::thread readTaskThread;
void readTask();
};
};
#ifndef __COMMUNICATION_H_
#define __COMMUNICATION_H_
#include "communication/include/icommunication.h"
#include "communication/include/command.h"
#include "communication/include/network.h"
#include "communication/include/messagecallback.h"
#include <memory>
#include <vector>
#include <atomic>
#include <thread>
#include <queue>
#include <map>
namespace icsneo {
class Communication {
public:
static uint8_t ICSChecksum(const std::vector<uint8_t>& data);
Communication(std::shared_ptr<ICommunication> com) : impl(com) {}
virtual ~Communication() { close(); }
bool open();
bool close();
virtual void spawnThreads();
virtual void joinThreads();
bool rawWrite(const std::vector<uint8_t>& bytes) { return impl->write(bytes); }
std::vector<uint8_t>& packetWrap(std::vector<uint8_t>& data, bool addChecksum = true);
bool sendPacket(std::vector<uint8_t>& bytes);
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 = {});
bool getSettingsSync(std::vector<uint8_t>& data, std::chrono::milliseconds timeout = std::chrono::milliseconds(50));
bool getSerialNumberSync(std::string& serial, std::chrono::milliseconds timeout = std::chrono::milliseconds(50));
int addMessageCallback(const MessageCallback& cb);
bool removeMessageCallback(int id);
std::shared_ptr<Message> waitForMessageSync(MessageFilter f = MessageFilter(), std::chrono::milliseconds timeout = std::chrono::milliseconds(50));
void setAlign16Bit(bool enable) { align16bit = enable; }
class Packet {
public:
Network network;
std::vector<uint8_t> data;
};
protected:
std::shared_ptr<ICommunication> impl;
static int messageCallbackIDCounter;
std::map<int, MessageCallback> messageCallbacks;
std::atomic<bool> closing{false};
private:
bool isOpen = false;
bool align16bit = true; // Not needed for Gigalog, Galaxy, etc and newer
std::thread readTaskThread;
void readTask();
};
};
#endif
+102 -101
View File
@@ -1,102 +1,103 @@
#ifndef __MULTICHANNELCOMMUNICATION_H_
#define __MULTICHANNELCOMMUNICATION_H_
#include "communication/include/communication.h"
#include "communication/include/icommunication.h"
namespace icsneo {
class MultiChannelCommunication : public Communication {
public:
MultiChannelCommunication(std::shared_ptr<ICommunication> com) : Communication(com) {}
void spawnThreads();
void joinThreads();
bool sendCommand(Communication::Command cmd, std::vector<uint8_t> arguments);
protected:
bool preprocessPacket(std::deque<uint8_t>& usbReadFifo);
private:
enum class CommandType : uint8_t {
PlasmaReadRequest = 0x10, // Status read request to HSC
PlasmaStatusResponse = 0x11, // Status response by HSC
HostPC_to_Vnet1 = 0x20, // Host PC data to Vnet module-1
Vnet1_to_HostPC = 0x21, // Vnet module-1 data to host PC
HostPC_to_Vnet2 = 0x30, // Host PC data to Vnet module-2
Vnet2_to_HostPC = 0x31, // Vnet module-2 data to host PC
HostPC_to_Vnet3 = 0x40, // Host PC data to Vnet module-3
Vnet3_to_HostPC = 0x41, // Vnet module-3 data to host PC
HostPC_to_SDCC1 = 0x50, // Host PC data to write to SDCC-1
HostPC_from_SDCC1 = 0x51, // Host PC wants data read from SDCC-1
SDCC1_to_HostPC = 0x52, // SDCC-1 data to host PC
HostPC_to_SDCC2 = 0x60, // Host PC data to write to SDCC-2
HostPC_from_SDCC2 = 0x61, // Host PC wants data read from SDCC-2
SDCC2_to_HostPC = 0x62, // SDCC-2 data to host PC
PC_to_LSOC = 0x70, // Host PC data to LSOCC
LSOCC_to_PC = 0x71, // LSOCC data to host PC
HostPC_to_Microblaze = 0x80, // Host PC data to microblaze processor
Microblaze_to_HostPC = 0x81 // Microblaze processor data to host PC
};
static bool CommandTypeIsValid(CommandType cmd) {
switch(cmd) {
case CommandType::PlasmaReadRequest:
case CommandType::PlasmaStatusResponse:
case CommandType::HostPC_to_Vnet1:
case CommandType::Vnet1_to_HostPC:
case CommandType::HostPC_to_Vnet2:
case CommandType::Vnet2_to_HostPC:
case CommandType::HostPC_to_Vnet3:
case CommandType::Vnet3_to_HostPC:
case CommandType::HostPC_to_SDCC1:
case CommandType::HostPC_from_SDCC1:
case CommandType::SDCC1_to_HostPC:
case CommandType::HostPC_to_SDCC2:
case CommandType::HostPC_from_SDCC2:
case CommandType::SDCC2_to_HostPC:
case CommandType::PC_to_LSOC:
case CommandType::LSOCC_to_PC:
case CommandType::HostPC_to_Microblaze:
case CommandType::Microblaze_to_HostPC:
return true;
default:
return false;
}
}
static bool CommandTypeHasAddress(CommandType cmd) {
// Check CommandTypeIsValid before this, you will get false on an invalid command
switch(cmd) {
case CommandType::SDCC1_to_HostPC:
case CommandType::SDCC2_to_HostPC:
return true;
default:
return false;
}
}
static uint16_t CommandTypeDefinesLength(CommandType cmd) {
// Check CommandTypeIsValid before this, you will get 0 on an invalid command
switch(cmd) {
case CommandType::PlasmaStatusResponse:
return 2;
default:
return 0; // Length is defined by following bytes in message
}
}
enum class PreprocessState {
SearchForCommand,
ParseAddress,
ParseLength,
GetData
};
PreprocessState state = PreprocessState::SearchForCommand;
uint16_t currentCommandLength;
CommandType currentCommandType;
size_t currentReadIndex = 0;
std::thread mainChannelReadThread;
void readTask();
};
};
#ifndef __MULTICHANNELCOMMUNICATION_H_
#define __MULTICHANNELCOMMUNICATION_H_
#include "communication/include/communication.h"
#include "communication/include/icommunication.h"
#include "communication/include/command.h"
namespace icsneo {
class MultiChannelCommunication : public Communication {
public:
MultiChannelCommunication(std::shared_ptr<ICommunication> com) : Communication(com) {}
void spawnThreads();
void joinThreads();
bool sendCommand(Command cmd, std::vector<uint8_t> arguments);
protected:
bool preprocessPacket(std::deque<uint8_t>& usbReadFifo);
private:
enum class CommandType : uint8_t {
PlasmaReadRequest = 0x10, // Status read request to HSC
PlasmaStatusResponse = 0x11, // Status response by HSC
HostPC_to_Vnet1 = 0x20, // Host PC data to Vnet module-1
Vnet1_to_HostPC = 0x21, // Vnet module-1 data to host PC
HostPC_to_Vnet2 = 0x30, // Host PC data to Vnet module-2
Vnet2_to_HostPC = 0x31, // Vnet module-2 data to host PC
HostPC_to_Vnet3 = 0x40, // Host PC data to Vnet module-3
Vnet3_to_HostPC = 0x41, // Vnet module-3 data to host PC
HostPC_to_SDCC1 = 0x50, // Host PC data to write to SDCC-1
HostPC_from_SDCC1 = 0x51, // Host PC wants data read from SDCC-1
SDCC1_to_HostPC = 0x52, // SDCC-1 data to host PC
HostPC_to_SDCC2 = 0x60, // Host PC data to write to SDCC-2
HostPC_from_SDCC2 = 0x61, // Host PC wants data read from SDCC-2
SDCC2_to_HostPC = 0x62, // SDCC-2 data to host PC
PC_to_LSOC = 0x70, // Host PC data to LSOCC
LSOCC_to_PC = 0x71, // LSOCC data to host PC
HostPC_to_Microblaze = 0x80, // Host PC data to microblaze processor
Microblaze_to_HostPC = 0x81 // Microblaze processor data to host PC
};
static bool CommandTypeIsValid(CommandType cmd) {
switch(cmd) {
case CommandType::PlasmaReadRequest:
case CommandType::PlasmaStatusResponse:
case CommandType::HostPC_to_Vnet1:
case CommandType::Vnet1_to_HostPC:
case CommandType::HostPC_to_Vnet2:
case CommandType::Vnet2_to_HostPC:
case CommandType::HostPC_to_Vnet3:
case CommandType::Vnet3_to_HostPC:
case CommandType::HostPC_to_SDCC1:
case CommandType::HostPC_from_SDCC1:
case CommandType::SDCC1_to_HostPC:
case CommandType::HostPC_to_SDCC2:
case CommandType::HostPC_from_SDCC2:
case CommandType::SDCC2_to_HostPC:
case CommandType::PC_to_LSOC:
case CommandType::LSOCC_to_PC:
case CommandType::HostPC_to_Microblaze:
case CommandType::Microblaze_to_HostPC:
return true;
default:
return false;
}
}
static bool CommandTypeHasAddress(CommandType cmd) {
// Check CommandTypeIsValid before this, you will get false on an invalid command
switch(cmd) {
case CommandType::SDCC1_to_HostPC:
case CommandType::SDCC2_to_HostPC:
return true;
default:
return false;
}
}
static uint16_t CommandTypeDefinesLength(CommandType cmd) {
// Check CommandTypeIsValid before this, you will get 0 on an invalid command
switch(cmd) {
case CommandType::PlasmaStatusResponse:
return 2;
default:
return 0; // Length is defined by following bytes in message
}
}
enum class PreprocessState {
SearchForCommand,
ParseAddress,
ParseLength,
GetData
};
PreprocessState state = PreprocessState::SearchForCommand;
uint16_t currentCommandLength;
CommandType currentCommandType;
size_t currentReadIndex = 0;
std::thread mainChannelReadThread;
void readTask();
};
};
#endif