Refactor for a central include directory

This commit is contained in:
Paul Hollinsky
2018-10-22 11:52:34 -04:00
parent 12451def11
commit 8e6b0d0b0e
127 changed files with 263 additions and 265 deletions
+17
View File
@@ -0,0 +1,17 @@
#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,
SetDefaultSettings = 0xA8 // Follow up with SaveSettings to write to EEPROM
};
}
#endif
@@ -0,0 +1,69 @@
#ifndef __COMMUNICATION_H_
#define __COMMUNICATION_H_
#include "icsneo/communication/icommunication.h"
#include "icsneo/communication/command.h"
#include "icsneo/communication/network.h"
#include "icsneo/communication/packet.h"
#include "icsneo/communication/message/callback/messagecallback.h"
#include "icsneo/communication/message/serialnumbermessage.h"
#include "icsneo/communication/packetizer.h"
#include "icsneo/communication/encoder.h"
#include "icsneo/communication/decoder.h"
#include <memory>
#include <vector>
#include <atomic>
#include <thread>
#include <queue>
#include <map>
namespace icsneo {
class Communication {
public:
Communication(
std::unique_ptr<ICommunication> com,
std::shared_ptr<Packetizer> p,
std::unique_ptr<Encoder> e,
std::unique_ptr<Decoder> md) : packetizer(p), encoder(std::move(e)), decoder(std::move(md)), impl(std::move(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); }
virtual 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));
std::shared_ptr<SerialNumberMessage> getSerialNumberSync(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)) {
return waitForMessageSync(std::make_shared<MessageFilter>(f), timeout);
}
std::shared_ptr<Message> waitForMessageSync(std::shared_ptr<MessageFilter> f, std::chrono::milliseconds timeout = std::chrono::milliseconds(50));
std::shared_ptr<Packetizer> packetizer; // Ownership is shared with the encoder
std::unique_ptr<Encoder> encoder;
std::unique_ptr<Decoder> decoder;
protected:
std::unique_ptr<ICommunication> impl;
static int messageCallbackIDCounter;
std::map<int, MessageCallback> messageCallbacks;
std::atomic<bool> closing{false};
private:
bool isOpen = false;
std::thread readTaskThread;
void readTask();
};
}
#endif
+97
View File
@@ -0,0 +1,97 @@
#ifndef __DECODER_H_
#define __DECODER_H_
#include "icsneo/communication/message/message.h"
#include "icsneo/communication/message/canmessage.h"
#include "icsneo/communication/packet.h"
#include "icsneo/communication/network.h"
#include <queue>
#include <vector>
#include <memory>
#pragma pack(push, 1)
namespace icsneo {
class Decoder {
public:
static uint64_t GetUInt64FromLEBytes(uint8_t* bytes);
bool decode(std::shared_ptr<Message>& result, const std::shared_ptr<Packet>& packet);
private:
typedef uint16_t icscm_bitfield;
struct HardwareCANPacket {
struct {
icscm_bitfield IDE : 1;
icscm_bitfield SRR : 1;
icscm_bitfield SID : 11;
icscm_bitfield EDL : 1;
icscm_bitfield BRS : 1;
icscm_bitfield ESI : 1;
} header;
struct {
icscm_bitfield EID : 12;
icscm_bitfield TXMSG : 1;
icscm_bitfield TXAborted : 1;
icscm_bitfield TXLostArb : 1;
icscm_bitfield TXError : 1;
} eid;
struct {
icscm_bitfield DLC : 4;
icscm_bitfield RB0 : 1;
icscm_bitfield IVRIF : 1;
icscm_bitfield HVEnable : 1;// must be cleared before passing into CAN driver
icscm_bitfield ExtendedNetworkIndexBit : 1;//DO NOT CLOBBER THIS
icscm_bitfield RB1 : 1;
icscm_bitfield RTR : 1;
icscm_bitfield EID2 : 6;
} dlc;
unsigned char data[8];
uint16_t stats;
struct {
uint64_t TS : 60;
uint64_t : 3; // Reserved for future status bits
uint64_t IsExtended : 1;
} timestamp;
};
union CoreMiniStatusBits_t {
struct {
unsigned just_reset : 1;
unsigned com_enabled : 1;
unsigned cm_is_running : 1;
unsigned cm_checksum_failed : 1;
unsigned cm_license_failed : 1;
unsigned cm_version_mismatch : 1;
unsigned cm_boot_off : 1;
unsigned hardware_failure : 1;//to check SRAM failure (for now)
unsigned isPassiveConnect : 1;///< Always zero. Set to one when neoVI connection is passive,i.e. no async traffic
unsigned usbComEnabled : 1;///< Set to one when USB Host PC has enabled communication.
unsigned linuxComEnabled : 1;///< Set to one when Android (Linux) has enabled communication.
unsigned cm_too_big : 1;
unsigned hidUsbState : 1;
unsigned fpgaUsbState : 1;
unsigned reserved : 2;
};
uint32_t dword;
};
struct HardwareResetStatusPacket {
uint16_t main_loop_time_25ns;
uint16_t max_main_loop_time_25ns;
CoreMiniStatusBits_t status;
uint8_t histo[6];//!< Can hold histogram performance data.
uint16_t spi1Kbps;//!< Spi1's kbps throughput.
uint16_t initBits;//!< Bitfield with init states of drivers, 1 is succes, 0 is fail.
uint16_t cpuMipsH;
uint16_t cpuMipsL;
uint16_t busVoltage;
uint16_t deviceTemperature;
};
};
}
#pragma pack(pop)
#endif
+34
View File
@@ -0,0 +1,34 @@
#ifndef __ENCODER_H_
#define __ENCODER_H_
#include "icsneo/communication/message/message.h"
#include "icsneo/communication/message/canmessage.h"
#include "icsneo/communication/packet.h"
#include "icsneo/communication/command.h"
#include "icsneo/communication/network.h"
#include "icsneo/communication/packetizer.h"
#include <queue>
#include <vector>
#include <memory>
#pragma pack(push, 1)
namespace icsneo {
class Encoder {
public:
Encoder(std::shared_ptr<Packetizer> packetizerInstance) : packetizer(packetizerInstance) {}
bool encode(std::vector<uint8_t>& result, const std::shared_ptr<Message>& message);
bool encode(std::vector<uint8_t>& result, Command cmd, bool boolean) { return encode(result, cmd, std::vector<uint8_t>({ (uint8_t)boolean })); }
bool encode(std::vector<uint8_t>& result, Command cmd, std::vector<uint8_t> arguments = {});
bool supportCANFD = false;
private:
std::shared_ptr<Packetizer> packetizer;
};
}
#pragma pack(pop)
#endif
@@ -0,0 +1,43 @@
#ifndef __ICOMMUNICATION_H_
#define __ICOMMUNICATION_H_
#include <vector>
#include <chrono>
#include <atomic>
#include <thread>
#include "icsneo/third-party/concurrentqueue/blockingconcurrentqueue.h"
namespace icsneo {
class ICommunication {
public:
virtual ~ICommunication() {}
virtual bool open() = 0;
virtual bool isOpen() = 0;
virtual bool close() = 0;
virtual bool read(std::vector<uint8_t>& bytes, size_t limit = 0);
virtual bool readWait(std::vector<uint8_t>& bytes, std::chrono::milliseconds timeout = std::chrono::milliseconds(100), size_t limit = 0);
virtual bool write(const std::vector<uint8_t>& bytes);
protected:
class WriteOperation {
public:
WriteOperation() {}
WriteOperation(std::vector<uint8_t> b) { bytes = b; }
std::vector<uint8_t> bytes;
};
enum IOTaskState {
LAUNCH,
WAIT
};
virtual void readTask() = 0;
virtual void writeTask() = 0;
moodycamel::BlockingConcurrentQueue<uint8_t> readQueue;
moodycamel::BlockingConcurrentQueue<WriteOperation> writeQueue;
std::thread readThread, writeThread;
std::atomic<bool> closing{false};
};
}
#endif
@@ -0,0 +1,23 @@
#ifndef __CANMESSAGECALLBACK_H_
#define __CANMESSAGECALLBACK_H_
#include "icsneo/communication/message/callback/messagecallback.h"
#include "icsneo/communication/message/canmessage.h"
#include "icsneo/communication/message/filter/canmessagefilter.h"
#include <memory>
namespace icsneo {
class CANMessageCallback : public MessageCallback {
public:
CANMessageCallback(fn_messageCallback cb, std::shared_ptr<CANMessageFilter> f) : MessageCallback(cb, f) {}
CANMessageCallback(fn_messageCallback cb, CANMessageFilter f = CANMessageFilter()) : MessageCallback(cb, std::make_shared<CANMessageFilter>(f)) {}
// Allow the filter to be placed first if the user wants (maybe in the case of a lambda)
CANMessageCallback(std::shared_ptr<CANMessageFilter> f, fn_messageCallback cb) : MessageCallback(cb, f) {}
CANMessageCallback(CANMessageFilter f, fn_messageCallback cb) : MessageCallback(cb, std::make_shared<CANMessageFilter>(f)) {}
};
};
#endif
@@ -0,0 +1,23 @@
#ifndef __MAIN51MESSAGECALLBACK_H_
#define __MAIN51MESSAGECALLBACK_H_
#include "icsneo/communication/message/callback/messagecallback.h"
#include "icsneo/communication/message/main51message.h"
#include "icsneo/communication/message/filter/main51messagefilter.h"
#include <memory>
namespace icsneo {
class Main51MessageCallback : public MessageCallback {
public:
Main51MessageCallback(fn_messageCallback cb, std::shared_ptr<Main51MessageFilter> f) : MessageCallback(cb, f) {}
Main51MessageCallback(fn_messageCallback cb, Main51MessageFilter f = Main51MessageFilter()) : MessageCallback(cb, std::make_shared<Main51MessageFilter>(f)) {}
// Allow the filter to be placed first if the user wants (maybe in the case of a lambda)
Main51MessageCallback(std::shared_ptr<Main51MessageFilter> f, fn_messageCallback cb) : MessageCallback(cb, f) {}
Main51MessageCallback(Main51MessageFilter f, fn_messageCallback cb) : MessageCallback(cb, std::make_shared<Main51MessageFilter>(f)) {}
};
};
#endif
@@ -0,0 +1,38 @@
#ifndef __MESSAGECALLBACK_H_
#define __MESSAGECALLBACK_H_
#include "icsneo/communication/message/message.h"
#include "icsneo/communication/message/filter/messagefilter.h"
#include <memory>
#include <functional>
namespace icsneo {
class MessageCallback {
public:
typedef std::function< void( std::shared_ptr<Message> ) > fn_messageCallback;
MessageCallback(fn_messageCallback cb, std::shared_ptr<MessageFilter> f) : callback(cb), filter(f) {}
MessageCallback(fn_messageCallback cb, MessageFilter f = MessageFilter()) : callback(cb), filter(std::make_shared<MessageFilter>(f)) {}
// Allow the filter to be placed first if the user wants (maybe in the case of a lambda)
MessageCallback(std::shared_ptr<MessageFilter> f, fn_messageCallback cb) : callback(cb), filter(f) {}
MessageCallback(MessageFilter f, fn_messageCallback cb) : callback(cb), filter(std::make_shared<MessageFilter>(f)) {}
virtual bool callIfMatch(const std::shared_ptr<Message>& message) const {
bool ret = filter->match(message);
if(ret)
callback(message);
return ret;
}
const MessageFilter& getFilter() const { return *filter; }
const fn_messageCallback& getCallback() const { return callback; }
protected:
fn_messageCallback callback;
std::shared_ptr<MessageFilter> filter;
};
}
#endif
@@ -0,0 +1,20 @@
#ifndef __CANMESSAGE_H_
#define __CANMESSAGE_H_
#include "icsneo/communication/message/message.h"
namespace icsneo {
class CANMessage : public Message {
public:
uint32_t arbid;
uint8_t dlcOnWire;
bool isRemote = false;
bool isExtended = false;
bool isCANFD = false;
bool baudrateSwitch = false; // CAN FD only
};
}
#endif
@@ -0,0 +1,38 @@
#ifndef __CANMESSAGEFILTER_H_
#define __CANMESSAGEFILTER_H_
#include "icsneo/communication/message/filter/messagefilter.h"
#include "icsneo/communication/network.h"
#include "icsneo/communication/message/message.h"
#include "icsneo/communication/message/canmessage.h"
#include <memory>
namespace icsneo {
class CANMessageFilter : public MessageFilter {
public:
CANMessageFilter() : MessageFilter(Network::Type::CAN), arbid(INVALID_ARBID) {}
CANMessageFilter(uint32_t arbid) : MessageFilter(Network::Type::CAN), arbid(arbid) {}
bool match(const std::shared_ptr<Message>& message) const {
if(!MessageFilter::match(message))
return false;
const auto canMessage = std::dynamic_pointer_cast<CANMessage>(message);
if(canMessage == nullptr || !matchArbID(canMessage->arbid))
return false;
return true;
}
private:
static constexpr uint32_t INVALID_ARBID = 0xffffffff;
uint32_t arbid;
bool matchArbID(uint32_t marbid) const {
if(arbid == INVALID_ARBID)
return true;
return arbid == marbid;
}
};
};
#endif
@@ -0,0 +1,46 @@
#ifndef __MAIN51MESSAGEFILTER_H_
#define __MAIN51MESSAGEFILTER_H_
#include "icsneo/communication/message/filter/messagefilter.h"
#include "icsneo/communication/network.h"
#include "icsneo/communication/communication.h"
#include "icsneo/communication/message/main51message.h"
#include <memory>
#include <iostream>
namespace icsneo {
class Main51MessageFilter : public MessageFilter {
public:
Main51MessageFilter() : MessageFilter(Network::NetID::Main51), command(INVALID_COMMAND) {}
Main51MessageFilter(Command command) : MessageFilter(Network::NetID::Main51), command(command) {}
bool match(const std::shared_ptr<Message>& message) const {
if(!MessageFilter::match(message)) {
//std::cout << "message filter did not match base for " << message->network << std::endl;
return false;
}
const auto main51Message = std::dynamic_pointer_cast<Main51Message>(message);
if(!main51Message)
std::cout << "could not upcast " << message->network << std::endl;
if(main51Message == nullptr || !matchCommand(main51Message->command)) {
if(main51Message)
std::cout << "Could not match command " << (int)(command) << " to " << (int)(main51Message->command) << std::endl;
return false;
}
return true;
}
private:
static constexpr Command INVALID_COMMAND = (Command)0xff;
Command command;
bool matchCommand(Command mcommand) const {
if(command == INVALID_COMMAND)
return true;
return command == mcommand;
}
};
}
#endif
@@ -0,0 +1,45 @@
#ifndef __MESSAGEFILTER_H_
#define __MESSAGEFILTER_H_
#include "icsneo/communication/network.h"
#include "icsneo/communication/message/message.h"
#include <memory>
namespace icsneo {
class MessageFilter {
public:
MessageFilter() {}
MessageFilter(Network::Type type) : type(type) {}
MessageFilter(Network::NetID netid) : netid(netid) {}
virtual ~MessageFilter() {}
// When getting "all" types of messages, include the ones marked as "internal only"
bool includeInternalInAny = false;
virtual bool match(const std::shared_ptr<Message>& message) const {
if(!matchType(message->network.getType()))
return false;
if(!matchNetID(message->network.getNetID()))
return false;
return true;
}
private:
Network::Type type = Network::Type::Any;
bool matchType(Network::Type mtype) const {
if(type == Network::Type::Any && (mtype != Network::Type::Internal || includeInternalInAny))
return true;
return type == mtype;
}
Network::NetID netid = Network::NetID::Any;
bool matchNetID(Network::NetID mnetid) const {
if(netid == Network::NetID::Any)
return true;
return netid == mnetid;
}
};
}
#endif
@@ -0,0 +1,17 @@
#ifndef __MAIN51MESSAGE_H_
#define __MAIN51MESSAGE_H_
#include "icsneo/communication/message/message.h"
#include "icsneo/communication/communication.h"
namespace icsneo {
class Main51Message : public Message {
public:
virtual ~Main51Message() = default;
Command command;
};
}
#endif
@@ -0,0 +1,19 @@
#ifndef __MESSAGE_H_
#define __MESSAGE_H_
#include "icsneo/communication/network.h"
#include <vector>
namespace icsneo {
class Message {
public:
virtual ~Message() = default;
Network network;
std::vector<uint8_t> data;
uint64_t timestamp;
};
}
#endif
@@ -0,0 +1,135 @@
#ifndef __NEOMESSAGE_H_
#define __NEOMESSAGE_H_
#include <stdint.h>
#include <stddef.h>
#pragma pack(push, 1)
typedef union {
struct {
uint32_t globalError : 1;
uint32_t transmitMessage : 1;
uint32_t extendedFrame : 1;
uint32_t remoteFrame : 1;
uint32_t crcError : 1;
uint32_t canErrorPassive : 1;
uint32_t incompleteFrame : 1;
uint32_t lostArbitration : 1;
uint32_t undefinedError : 1;
uint32_t canBusOff : 1;
uint32_t canErrorWarning : 1;
uint32_t canBusShortedPlus : 1;
uint32_t canBusShortedGround : 1;
uint32_t checksumError : 1;
uint32_t badMessageBitTimeError : 1;
uint32_t ifrData : 1;
uint32_t hardwareCommError : 1;
uint32_t expectedLengthError : 1;
uint32_t incomingNoMatch : 1;
uint32_t statusBreak : 1;
uint32_t avsiRecOverflow : 1;
uint32_t testTrigger : 1;
uint32_t audioComment : 1;
uint32_t gpsData : 1;
uint32_t analogDigitalInput : 1;
uint32_t textComment : 1;
uint32_t networkMessageType : 1;
uint32_t vsiTXUnderrun : 1;
uint32_t vsiIFRCRCBit : 1;
uint32_t initMessage : 1;
//uint32_t highSpeedMessage : 1; // Occupies the same space as flexraySecondStartupFrame
uint32_t flexraySecondStartupFrame : 1;
uint32_t extended : 1;
// ~~~ End of bitfield 1 ~~~
uint32_t hasValue : 1;
uint32_t valueIsBoolean : 1;
uint32_t highVoltage : 1;
uint32_t longMessage : 1;
uint32_t : 12;
uint32_t globalChange : 1;
uint32_t errorFrame : 1;
uint32_t : 2;
uint32_t endOfLongMessage : 1;
uint32_t linErrorRXBreakNotZero : 1;
uint32_t linErrorRXBreakTooShort : 1;
uint32_t linErrorRXSyncNot55 : 1;
uint32_t linErrorRXDataGreaterEight : 1;
uint32_t linErrorTXRXMismatch : 1;
uint32_t linErrorMessageIDParity : 1;
//isoFrameError
uint32_t linSyncFrameError : 1;
//isoOverflowError
uint32_t linIDFrameError : 1;
//isoParityError
uint32_t linSlaveByteError : 1;
uint32_t rxTimeoutError : 1;
uint32_t linNoSlaveData : 1;
// mostPacketData
// mostStatus
// mostLowLevel
// mostControlData
// mostMHPUserData
// mostMHPControlData
// mostI2SDump
// mostTooShort
// most50
// most150
// mostChangedParameter
// ethernetCRCError
// ethernetFrameTooShort
// ethernetFCSAvailable
// ~~~ End of bitfield 2 ~~~
//uint32_t linJustBreakSync : 1;
//uint32_t linSlaveDataTooShort : 1;
//uint32_t linOnlyUpdateSlaveTableOnce : 1;
uint32_t canfdESI : 1;
uint32_t canfdIDE : 1;
uint32_t canfdRTR : 1;
uint32_t canfdFDF : 1;
uint32_t canfdBRS : 1;
};
uint32_t statusBitfield[4];
} neomessage_statusbitfield_t;
typedef struct {
neomessage_statusbitfield_t status;
uint64_t timestamp;
const uint8_t* data;
size_t length;
uint8_t header[4];
uint16_t netid;
uint8_t type;
uint8_t reserved[9];
} neomessage_t;
// Any time you add another neomessage_*_t type, make sure to add it to the static_asserts below!
typedef struct {
neomessage_statusbitfield_t status;
uint64_t timestamp;
const uint8_t* data;
size_t length;
uint32_t arbid;
uint16_t netid;
uint8_t type;
uint8_t dlcOnWire;
char reserved[8];
} neomessage_can_t;
#pragma pack(pop)
#ifdef __cplusplus
#include "icsneo/communication/message/message.h"
#include <memory>
static_assert(sizeof(neomessage_can_t) == sizeof(neomessage_t), "All types of neomessage_t must be the same size!");
namespace icsneo {
neomessage_t CreateNeoMessage(const std::shared_ptr<Message> message);
std::shared_ptr<Message> CreateMessageFromNeoMessage(const neomessage_t* neomessage);
}
#endif
#endif
@@ -0,0 +1,35 @@
#ifndef __RESETSTATUSMESSAGE_H_
#define __RESETSTATUSMESSAGE_H_
#include "icsneo/communication/message/main51message.h"
#include "icsneo/communication/command.h"
#include <string>
namespace icsneo {
class ResetStatusMessage : public Message {
public:
ResetStatusMessage() : Message() {}
virtual ~ResetStatusMessage() = default;
uint16_t mainLoopTime;
uint16_t maxMainLoopTime;
bool justReset;
bool comEnabled;
bool cmRunning;
bool cmChecksumFailed;
bool cmLicenseFailed;
bool cmVersionMismatch;
bool cmBootOff;
bool hardwareFailure;
bool usbComEnabled;
bool linuxComEnabled;
bool cmTooBig;
bool hidUsbState;
bool fpgaUsbState;
uint16_t busVoltage;
uint16_t deviceTemperature;
};
}
#endif
@@ -0,0 +1,24 @@
#ifndef __SERIALNUMBERMESSAGE_H_
#define __SERIALNUMBERMESSAGE_H_
#include "icsneo/communication/message/main51message.h"
#include "icsneo/communication/command.h"
#include <string>
namespace icsneo {
// The response for Command::RequestSerialNumber
class SerialNumberMessage : public Main51Message {
public:
SerialNumberMessage() : Main51Message() { command = Command::RequestSerialNumber; }
virtual ~SerialNumberMessage() = default;
std::string deviceSerial;
uint8_t macAddress[6]; // This might be all zeros even if `hasMacAddress` is true
bool hasMacAddress = false; // The message might not actually be long enough to contain a MAC address, in which case we mark this
uint8_t pcbSerial[16];
bool hasPCBSerial = false;
};
}
#endif
@@ -0,0 +1,108 @@
#ifndef __MULTICHANNELCOMMUNICATION_H_
#define __MULTICHANNELCOMMUNICATION_H_
#include "icsneo/communication/communication.h"
#include "icsneo/communication/icommunication.h"
#include "icsneo/communication/command.h"
#include "icsneo/communication/encoder.h"
namespace icsneo {
class MultiChannelCommunication : public Communication {
public:
MultiChannelCommunication(
std::unique_ptr<ICommunication> com,
std::shared_ptr<Packetizer> p,
std::unique_ptr<Encoder> e,
std::unique_ptr<Decoder> md) : Communication(std::move(com), p, std::move(e), std::move(md)) {}
void spawnThreads() override;
void joinThreads() override;
bool sendPacket(std::vector<uint8_t>& bytes) override;
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
+415
View File
@@ -0,0 +1,415 @@
#ifndef __NETWORKID_H_
#define __NETWORKID_H_
#include <cstdint>
#include <ostream>
namespace icsneo {
class Network {
public:
enum class NetID : uint16_t {
Device = 0,
HSCAN = 1,
MSCAN = 2,
SWCAN = 3,
LSFTCAN = 4,
FordSCP = 5,
J1708 = 6,
Aux = 7,
J1850VPW = 8,
ISO = 9,
ISOPIC = 10,
Main51 = 11,
RED = 12,
SCI = 13,
ISO2 = 14,
ISO14230 = 15,
LIN = 16,
OP_Ethernet1 = 17,
OP_Ethernet2 = 18,
OP_Ethernet3 = 19,
// START Device Command Returns
// When we send a command, the device returns on one of these, depending on command
RED_EXT_MEMORYREAD = 20,
RED_INT_MEMORYREAD = 21,
RED_DFLASH_READ = 22,
RED_SDCARD_READ = 23,
CAN_ERRBITS = 24,
RED_DFLASH_WRITE_DONE = 25,
RED_WAVE_CAN1_LOGICAL = 26,
RED_WAVE_CAN2_LOGICAL = 27,
RED_WAVE_LIN1_LOGICAL = 28,
RED_WAVE_LIN2_LOGICAL = 29,
RED_WAVE_LIN1_ANALOG = 30,
RED_WAVE_LIN2_ANALOG = 31,
RED_WAVE_MISC_ANALOG = 32,
RED_WAVE_MISCDIO2_LOGICAL = 33,
RED_NETWORK_COM_ENABLE_EX = 34,
RED_NEOVI_NETWORK = 35,
RED_READ_BAUD_SETTINGS = 36,
RED_OLDFORMAT = 37,
RED_SCOPE_CAPTURE = 38,
RED_HARDWARE_EXCEP = 39,
RED_GET_RTC = 40,
// END Device Command Returns
ISO3 = 41,
HSCAN2 = 42,
HSCAN3 = 44,
OP_Ethernet4 = 45,
OP_Ethernet5 = 46,
ISO4 = 47,
LIN2 = 48,
LIN3 = 49,
LIN4 = 50,
// MOST = 51, Old and unused
RED_App_Error = 52,
CGI = 53,
Reset_Status = 54,
FB_Status = 55,
App_Signal_Status = 56,
Read_Datalink_Cm_Tx_Msg = 57,
Read_Datalink_Cm_Rx_Msg = 58,
Logging_Overflow = 59,
Read_Settings_Ex = 60,
HSCAN4 = 61,
HSCAN5 = 62,
RS232 = 63,
UART = 64,
UART2 = 65,
UART3 = 66,
UART4 = 67,
SWCAN2 = 68,
Ethernet_DAQ = 69,
Data_To_Host = 70,
TextAPI_To_Host = 71,
OP_Ethernet6 = 73,
Red_VBat = 74,
OP_Ethernet7 = 75,
OP_Ethernet8 = 76,
OP_Ethernet9 = 77,
OP_Ethernet10 = 78,
OP_Ethernet11 = 79,
FlexRay1a = 80,
FlexRay1b = 81,
FlexRay2a = 82,
FlexRay2b = 83,
LIN5 = 84,
FlexRay = 85,
FlexRay2 = 86,
OP_Ethernet12 = 87,
MOST25 = 90,
MOST50 = 91,
MOST150 = 92,
Ethernet = 93,
GMFSA = 94,
TCP = 95,
HSCAN6 = 96,
HSCAN7 = 97,
LIN6 = 98,
LSFTCAN2 = 99,
HW_COM_Latency_Test = 512,
Device_Status = 513,
Any = 0xfffe, // Never actually set as type, but used as flag for filtering
Invalid = 0xffff
};
enum class Type {
Invalid,
Internal, // Used for statuses that don't actually need to be transferred to the client application
CAN,
LIN,
FlexRay,
MOST,
Ethernet,
Other,
Any // Never actually set as type, but used as flag for filtering
};
static const char* GetTypeString(Type type) {
switch(type) {
case Type::CAN:
return "CAN";
case Type::LIN:
return "LIN";
case Type::FlexRay:
return "FlexRay";
case Type::MOST:
return "MOST";
case Type::Other:
return "Other";
case Type::Internal:
return "Internal";
case Type::Invalid:
default:
return "Invalid Type";
}
}
static Type GetTypeOfNetID(NetID netid) {
switch(netid) {
case NetID::HSCAN:
case NetID::MSCAN:
case NetID::SWCAN:
case NetID::LSFTCAN:
case NetID::HSCAN2:
case NetID::HSCAN3:
case NetID::HSCAN4:
case NetID::HSCAN5:
case NetID::SWCAN2:
case NetID::HSCAN6:
case NetID::HSCAN7:
case NetID::LSFTCAN2:
return Type::CAN;
case NetID::LIN:
case NetID::LIN2:
case NetID::LIN3:
case NetID::LIN4:
case NetID::LIN5:
case NetID::LIN6:
return Type::LIN;
case NetID::FlexRay:
case NetID::FlexRay1a:
case NetID::FlexRay1b:
case NetID::FlexRay2:
case NetID::FlexRay2a:
case NetID::FlexRay2b:
return Type::FlexRay;
case NetID::MOST25:
case NetID::MOST50:
case NetID::MOST150:
return Type::MOST;
case NetID::RED:
case NetID::Reset_Status:
case NetID::Device_Status:
return Type::Internal;
case NetID::Invalid:
case NetID::Any:
return Type::Invalid;
default:
return Type::Other;
}
}
static const char* GetNetIDString(NetID netid) {
switch(netid) {
case NetID::Device:
return "Device";
case NetID::HSCAN:
return "HSCAN";
case NetID::MSCAN:
return "MSCAN";
case NetID::SWCAN:
return "SWCAN";
case NetID::LSFTCAN:
return "LSFTCAN";
case NetID::FordSCP:
return "FordSCP";
case NetID::J1708:
return "J1708";
case NetID::Aux:
return "Aux";
case NetID::J1850VPW:
return "J1850 VPW";
case NetID::ISO:
return "ISO";
case NetID::ISOPIC:
return "ISOPIC";
case NetID::Main51:
return "Main51";
case NetID::RED:
return "RED";
case NetID::SCI:
return "SCI";
case NetID::ISO2:
return "ISO 2";
case NetID::ISO14230:
return "ISO 14230";
case NetID::LIN:
return "LIN";
case NetID::OP_Ethernet1:
return "Ethernet 1";
case NetID::OP_Ethernet2:
return "Ethernet 2";
case NetID::OP_Ethernet3:
return "Ethernet 3";
case NetID::RED_EXT_MEMORYREAD:
return "RED_EXT_MEMORYREAD";
case NetID::RED_INT_MEMORYREAD:
return "RED_INT_MEMORYREAD";
case NetID::RED_DFLASH_READ:
return "RED_DFLASH_READ";
case NetID::RED_SDCARD_READ:
return "RED_SDCARD_READ";
case NetID::CAN_ERRBITS:
return "CAN_ERRBITS";
case NetID::RED_DFLASH_WRITE_DONE:
return "RED_DFLASH_WRITE_DONE";
case NetID::RED_WAVE_CAN1_LOGICAL:
return "RED_WAVE_CAN1_LOGICAL";
case NetID::RED_WAVE_CAN2_LOGICAL:
return "RED_WAVE_CAN2_LOGICAL";
case NetID::RED_WAVE_LIN1_LOGICAL:
return "RED_WAVE_LIN1_LOGICAL";
case NetID::RED_WAVE_LIN2_LOGICAL:
return "RED_WAVE_LIN2_LOGICAL";
case NetID::RED_WAVE_LIN1_ANALOG:
return "RED_WAVE_LIN1_ANALOG";
case NetID::RED_WAVE_LIN2_ANALOG:
return "RED_WAVE_LIN2_ANALOG";
case NetID::RED_WAVE_MISC_ANALOG:
return "RED_WAVE_MISC_ANALOG";
case NetID::RED_WAVE_MISCDIO2_LOGICAL:
return "RED_WAVE_MISCDIO2_LOGICAL";
case NetID::RED_NETWORK_COM_ENABLE_EX:
return "RED_NETWORK_COM_ENABLE_EX";
case NetID::RED_NEOVI_NETWORK:
return "RED_NEOVI_NETWORK";
case NetID::RED_READ_BAUD_SETTINGS:
return "RED_READ_BAUD_SETTINGS";
case NetID::RED_OLDFORMAT:
return "RED_OLDFORMAT";
case NetID::RED_SCOPE_CAPTURE:
return "RED_SCOPE_CAPTURE";
case NetID::RED_HARDWARE_EXCEP:
return "RED_HARDWARE_EXCEP";
case NetID::RED_GET_RTC:
return "RED_GET_RTC";
case NetID::ISO3:
return "ISO 3";
case NetID::HSCAN2:
return "HSCAN 2";
case NetID::HSCAN3:
return "HSCAN 3";
case NetID::OP_Ethernet4:
return "Ethernet 4";
case NetID::OP_Ethernet5:
return "Ethernet 5";
case NetID::ISO4:
return "ISO 4";
case NetID::LIN2:
return "LIN 2";
case NetID::LIN3:
return "LIN 3";
case NetID::LIN4:
return "LIN 4";
case NetID::RED_App_Error:
return "App Error";
case NetID::CGI:
return "CGI";
case NetID::Reset_Status:
return "Reset Status";
case NetID::FB_Status:
return "FB Status";
case NetID::App_Signal_Status:
return "App Signal Status";
case NetID::Read_Datalink_Cm_Tx_Msg:
return "Read Datalink Cm Tx Msg";
case NetID::Read_Datalink_Cm_Rx_Msg:
return "Read Datalink Cm Rx Msg";
case NetID::Logging_Overflow:
return "Logging Overflow";
case NetID::Read_Settings_Ex:
return "Read Settings Ex";
case NetID::HSCAN4:
return "HSCAN 4";
case NetID::HSCAN5:
return "HSCAN 5";
case NetID::RS232:
return "RS232";
case NetID::UART:
return "UART";
case NetID::UART2:
return "UART 2";
case NetID::UART3:
return "UART 3";
case NetID::UART4:
return "UART 4";
case NetID::SWCAN2:
return "SWCAN 2";
case NetID::Ethernet_DAQ:
return "Ethernet DAQ";
case NetID::Data_To_Host:
return "Data To Host";
case NetID::TextAPI_To_Host:
return "TextAPI To Host";
case NetID::OP_Ethernet6:
return "Ethernet 6";
case NetID::Red_VBat:
return "Red VBat";
case NetID::OP_Ethernet7:
return "Ethernet 7";
case NetID::OP_Ethernet8:
return "Ethernet 8";
case NetID::OP_Ethernet9:
return "Ethernet 9";
case NetID::OP_Ethernet10:
return "Ethernet 10";
case NetID::OP_Ethernet11:
return "Ethernet 11";
case NetID::FlexRay1a:
return "FlexRay 1a";
case NetID::FlexRay1b:
return "FlexRay 1b";
case NetID::FlexRay2a:
return "FlexRay 2a";
case NetID::FlexRay2b:
return "FlexRay 2b";
case NetID::LIN5:
return "LIN 5";
case NetID::FlexRay:
return "FlexRay";
case NetID::FlexRay2:
return "FlexRay 2";
case NetID::OP_Ethernet12:
return "Ethernet 12";
case NetID::MOST25:
return "MOST25";
case NetID::MOST50:
return "MOST50";
case NetID::MOST150:
return "MOST150";
case NetID::Ethernet:
return "Ethernet";
case NetID::GMFSA:
return "GMFSA";
case NetID::TCP:
return "TCP";
case NetID::HSCAN6:
return "HSCAN 6";
case NetID::HSCAN7:
return "HSCAN 7";
case NetID::LIN6:
return "LIN 6";
case NetID::LSFTCAN2:
return "LSFTCAN 2";
case NetID::HW_COM_Latency_Test:
return "HW COM Latency Test";
case NetID::Device_Status:
return "Device Status";
case NetID::Invalid:
default:
return "Invalid Network";
}
}
Network() { setValue(NetID::Invalid); }
Network(uint16_t netid) { setValue((NetID)netid); }
Network(NetID netid) { setValue(netid); }
NetID getNetID() const { return value; }
Type getType() const { return type; }
friend std::ostream& operator<<(std::ostream& os, const Network& network) {
os << GetNetIDString(network.getNetID());
return os;
}
private:
NetID value; // Always use setValue so that value and type stay in sync
Type type;
void setValue(NetID id) {
value = id;
type = GetTypeOfNetID(value);
}
};
}
#endif
+18
View File
@@ -0,0 +1,18 @@
#ifndef __PACKET_H_
#define __PACKET_H_
#include "icsneo/communication/network.h"
#include <vector>
#include <stdint.h>
namespace icsneo {
class Packet {
public:
Network network;
std::vector<uint8_t> data;
};
}
#endif
+44
View File
@@ -0,0 +1,44 @@
#ifndef __PACKETIZER_H_
#define __PACKETIZER_H_
#include "icsneo/communication/packet.h"
#include <queue>
#include <vector>
#include <memory>
namespace icsneo {
class Packetizer {
public:
static uint8_t ICSChecksum(const std::vector<uint8_t>& data);
std::vector<uint8_t>& packetWrap(std::vector<uint8_t>& data, bool shortFormat);
bool input(const std::vector<uint8_t>& bytes);
std::vector<std::shared_ptr<Packet>> output();
bool disableChecksum = false; // Even for short packets
bool align16bit = true; // Not needed for Gigalog, Galaxy, etc and newer
private:
enum class ReadState {
SearchForHeader,
ParseHeader,
ParseLongStylePacketHeader,
GetData
};
ReadState state = ReadState::SearchForHeader;
int currentIndex = 0;
int packetLength = 0;
int headerSize = 0;
bool checksum = false;
bool gotGoodPackets = false; // Tracks whether we've ever gotten a good packet
Packet packet;
std::deque<uint8_t> bytes;
std::vector<std::shared_ptr<Packet>> processedPackets;
};
}
#endif