Message: Create a type system so non-frame data can be represented

This change breaks existing code, hence the version bump, but it's
going to be much less error prone going forward.
This commit is contained in:
Paul Hollinsky
2021-05-22 01:58:36 -04:00
parent 21e93d1f73
commit 21bc4eeff2
48 changed files with 853 additions and 527 deletions
@@ -0,0 +1,25 @@
#ifndef __CANERRORCOUNTMESSAGE_H_
#define __CANERRORCOUNTMESSAGE_H_
#ifdef __cplusplus
#include "icsneo/communication/message/message.h"
namespace icsneo {
class CANErrorCountMessage : public Message {
public:
CANErrorCountMessage(uint8_t tec, uint8_t rec, bool busOffFlag)
: Message(Message::Type::CANErrorCount), transmitErrorCount(tec), receiveErrorCount(rec), busOff(busOffFlag){}
Network network;
uint8_t transmitErrorCount;
uint8_t receiveErrorCount;
bool busOff;
};
}
#endif // __cplusplus
#endif
@@ -7,7 +7,7 @@
namespace icsneo {
class CANMessage : public Message {
class CANMessage : public Frame {
public:
uint32_t arbid;
uint8_t dlcOnWire;
@@ -31,7 +31,7 @@ struct MACAddress {
}
};
class EthernetMessage : public Message {
class EthernetMessage : public Frame {
public:
bool preemptionEnabled = false;
uint8_t preemptionFlags = 0;
@@ -13,8 +13,8 @@ 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) {}
CANMessageFilter() : MessageFilter(Network::Type::CAN), arbid(INVALID_ARBID) { messageType = Message::Type::Frame; }
CANMessageFilter(uint32_t arbid) : MessageFilter(Network::Type::CAN), arbid(arbid) { messageType = Message::Type::Frame; }
bool match(const std::shared_ptr<Message>& message) const {
if(!MessageFilter::match(message))
@@ -14,8 +14,10 @@ namespace icsneo {
class Main51MessageFilter : public MessageFilter {
public:
Main51MessageFilter() : MessageFilter(Network::NetID::Main51), command(INVALID_COMMAND) {}
Main51MessageFilter(Command command) : MessageFilter(Network::NetID::Main51), command(command) {}
Main51MessageFilter() : MessageFilter(Message::Type::Main51), command(INVALID_COMMAND) {}
// Don't filter on Type::Main51 for Command as some Commands have their own type
// We still guarantee it's a Main51Message if it matches because of the dynamic_pointer_cast below
Main51MessageFilter(Command command) : command(command) { includeInternalInAny = true; }
bool match(const std::shared_ptr<Message>& message) const {
if(!MessageFilter::match(message)) {
@@ -12,26 +12,44 @@ namespace icsneo {
class MessageFilter {
public:
MessageFilter() {}
MessageFilter(Network::Type type) : type(type) {}
MessageFilter(Network::NetID netid) : type(Network::GetTypeOfNetID(netid)), netid(netid) {}
virtual ~MessageFilter() {}
MessageFilter(Message::Type type) : includeInternalInAny(neomessagetype_t(type) & 0x8000), messageType(type) {}
MessageFilter(Network::NetID netid) : MessageFilter(Network::GetTypeOfNetID(netid), netid) {}
MessageFilter(Network::Type type, Network::NetID net = Network::NetID::Any) : networkType(type), netid(net) {
// If a Network::Type::Internal is used, we want to also get internal Message::Types
// The NetID we want may be in there
includeInternalInAny = (networkType == Network::Type::Internal);
}
virtual ~MessageFilter() = default;
// 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()))
if(!matchMessageType(message->type))
return false;
if(message->type == Message::Type::Frame) {
Frame& frame = *static_cast<Frame*>(message.get());
if(!matchNetworkType(frame.network.getType()))
return false;
if(!matchNetID(frame.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))
protected:
Message::Type messageType = Message::Type::Invalid; // Used here for "any"
bool matchMessageType(Message::Type mtype) const {
if(messageType == Message::Type::Invalid && ((neomessagetype_t(mtype) & 0x8000) == 0 || includeInternalInAny))
return true;
return type == mtype;
return messageType == mtype;
}
Network::Type networkType = Network::Type::Any;
bool matchNetworkType(Network::Type mtype) const {
if(networkType == Network::Type::Any && (mtype != Network::Type::Internal || includeInternalInAny))
return true;
return networkType == mtype;
}
Network::NetID netid = Network::NetID::Any;
@@ -23,7 +23,7 @@ public:
uint8_t controller, uint16_t bufferId, const std::vector<uint8_t>& data, uint16_t desiredSize);
FlexRayControlMessage(const Packet& packet);
virtual ~FlexRayControlMessage() = default;
bool decoded = false;
uint8_t controller = 0xff; // Controller index, either 0 or 1
FlexRay::Opcode opcode = FlexRay::Opcode::Unknown;
@@ -10,7 +10,7 @@
namespace icsneo {
class FlexRayMessage : public Message {
class FlexRayMessage : public Frame {
public:
uint16_t slotid = 0;
double tsslen = 0;
@@ -8,7 +8,7 @@
namespace icsneo {
class ISO9141Message : public Message {
class ISO9141Message : public Frame {
public:
std::array<uint8_t, 3> header;
bool isInit = false;
@@ -8,9 +8,9 @@
namespace icsneo {
class Main51Message : public Message {
class Main51Message : public RawMessage {
public:
virtual ~Main51Message() = default;
Main51Message() : RawMessage(Message::Type::Main51, Network::NetID::Main51) {}
Command command = Command(0);
bool forceShortFormat = false; // Necessary for EnableNetworkCom and EnableNetworkComEx
};
+50 -1
View File
@@ -1,6 +1,9 @@
#ifndef __MESSAGE_H_
#define __MESSAGE_H_
#include <stdint.h>
typedef uint16_t neomessagetype_t;
#ifdef __cplusplus
#include "icsneo/communication/network.h"
@@ -10,10 +13,42 @@ namespace icsneo {
class Message {
public:
enum class Type : neomessagetype_t {
Frame = 0,
CANErrorCount = 0x100,
// Past 0x8000 are all for internal use only
Invalid = 0x8000,
RawMessage = 0x8001,
ReadSettings = 0x8002,
ResetStatus = 0x8003,
DeviceVersion = 0x8004,
Main51 = 0x8005,
FlexRayControl = 0x8006,
};
Message(Type t) : type(t) {}
virtual ~Message() = default;
const Type type;
uint64_t timestamp = 0;
};
class RawMessage : public Message {
public:
RawMessage(Message::Type type = Message::Type::RawMessage) : Message(type) {}
RawMessage(Message::Type type, Network net) : Message(type), network(net) {}
RawMessage(Network net) : Message(Message::Type::RawMessage), network(net) {}
RawMessage(Network net, std::vector<uint8_t> d) : Message(Message::Type::RawMessage), network(net), data(d) {}
Network network;
std::vector<uint8_t> data;
uint64_t timestamp = 0;
};
class Frame : public RawMessage {
public:
Frame() : RawMessage(Message::Type::Frame) {}
uint16_t description = 0;
bool transmitted = false;
bool error = false;
@@ -23,4 +58,18 @@ public:
#endif // __cplusplus
#ifdef __ICSNEOC_H_
#define ICSNEO_MESSAGE_TYPE_FRAME (0x0)
#define ICSNEO_MESSAGE_TYPE_CAN_ERROR_COUNT (0x100)
#define ICSNEO_MESSAGE_TYPE_INVALID (0x8000)
#define ICSNEO_MESSAGE_TYPE_RAW_MESSAGE (0x8001)
#define ICSNEO_MESSAGE_TYPE_READ_SETTINGS (0x8002)
#define ICSNEO_MESSAGE_TYPE_RESET_STATUS (0x8003)
#define ICSNEO_MESSAGE_TYPE_DEVICE_VERSION (0x8004)
#define ICSNEO_MESSAGE_TYPE_MAIN51 (0x8005)
#define ICSNEO_MESSAGE_TYPE_FLEXRAY_CONTROL (0x8006)
#endif // __ICSNEOC_H_
#endif
@@ -4,6 +4,7 @@
#include <stdint.h>
#include <stddef.h>
#include "icsneo/communication/network.h"
#include "icsneo/communication/message/message.h"
#pragma pack(push, 1)
@@ -18,12 +19,12 @@ typedef union {
uint32_t extendedFrame : 1;
uint32_t remoteFrame : 1;
uint32_t crcError : 1;
uint32_t canErrorPassive : 1;
uint32_t canErrorPassive : 1; // Occupies the same space as headerCRCError
uint32_t incompleteFrame : 1;
uint32_t lostArbitration : 1;
uint32_t undefinedError : 1;
uint32_t canBusOff : 1;
uint32_t canErrorWarning : 1;
uint32_t canBusRecovered : 1;
uint32_t canBusShortedPlus : 1;
uint32_t canBusShortedGround : 1;
uint32_t checksumError : 1;
@@ -101,24 +102,34 @@ typedef union {
#endif
typedef struct {
neomessage_statusbitfield_t status;
uint8_t _reserved1[16];
uint64_t timestamp;
uint64_t timestampReserved;
const uint8_t* data;
size_t length;
uint8_t header[4];
neonetid_t netid;
neonettype_t type;
uint8_t reserved0;
uint16_t description;
uint8_t reserved1[14];
uint64_t _reservedTimestamp;
uint8_t _reserved2[sizeof(size_t) * 2 + 7 + sizeof(neonetid_t) + sizeof(neonettype_t)];
neomessagetype_t messageType;
uint8_t _reserved3[12];
} neomessage_t; // 72 bytes total
// 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;
uint64_t timestampReserved;
uint64_t _reservedTimestamp;
const uint8_t* data;
size_t length;
uint8_t header[4];
neonetid_t netid;
neonettype_t type;
uint8_t _reserved0;
uint16_t description;
neomessagetype_t messageType;
uint8_t _reserved1[12];
} neomessage_frame_t;
typedef struct {
neomessage_statusbitfield_t status;
uint64_t timestamp;
uint64_t _reservedTimestamp;
const uint8_t* data;
size_t length;
uint32_t arbid;
@@ -126,22 +137,38 @@ typedef struct {
neonettype_t type;
uint8_t dlcOnWire;
uint16_t description;
uint8_t reserved[14];
neomessagetype_t messageType;
uint8_t _reserved1[12];
} neomessage_can_t;
typedef struct {
neomessage_statusbitfield_t status;
uint64_t timestamp;
uint64_t timestampReserved;
uint64_t _reservedTimestamp;
size_t _reserved2[2];
uint8_t transmitErrorCount;
uint8_t receiveErrorCount;
uint8_t _reserved3[5];
neonetid_t netid;
neonettype_t type;
neomessagetype_t messageType;
uint8_t _reserved4[12];
} neomessage_can_error_t;
typedef struct {
neomessage_statusbitfield_t status;
uint64_t timestamp;
uint64_t _reservedTimestamp;
const uint8_t* data;
size_t length;
uint8_t preemptionFlags;
uint8_t reservedHeader[3];
uint8_t _reservedHeader[3];
neonetid_t netid;
neonettype_t type;
uint8_t reserved0;
uint8_t _reserved0;
uint16_t description;
uint8_t reserved1[14];
neomessagetype_t messageType;
uint8_t _reserved1[12];
} neomessage_eth_t;
#pragma pack(pop)
@@ -151,7 +178,9 @@ typedef struct {
#include <memory>
static_assert(sizeof(neomessage_t) == (56 + sizeof(void*) + sizeof(size_t)), "neomessage_t size is incorrect! Changing size will break compatibility with existing C API programs.");
static_assert(sizeof(neomessage_frame_t) == sizeof(neomessage_t), "All types of neomessage_t must be the same size! (Base frame is not)");
static_assert(sizeof(neomessage_can_t) == sizeof(neomessage_t), "All types of neomessage_t must be the same size! (CAN is not)");
static_assert(sizeof(neomessage_can_error_t) == sizeof(neomessage_t), "All types of neomessage_t must be the same size! (CAN error is not)");
static_assert(sizeof(neomessage_eth_t) == sizeof(neomessage_t), "All types of neomessage_t must be the same size! (Ethernet is not)");
namespace icsneo {
@@ -8,9 +8,9 @@
namespace icsneo {
class ReadSettingsMessage : public Message {
class ReadSettingsMessage : public RawMessage {
public:
virtual ~ReadSettingsMessage() = default;
ReadSettingsMessage() : RawMessage(Message::Type::ReadSettings, Network::NetID::ReadSettings) {}
enum class Response : uint8_t {
OK = 0,
@@ -11,8 +11,8 @@ namespace icsneo {
class ResetStatusMessage : public Message {
public:
ResetStatusMessage() : Message() {}
virtual ~ResetStatusMessage() = default;
ResetStatusMessage() : Message(Message::Type::ResetStatus) {}
uint16_t mainLoopTime;
uint16_t maxMainLoopTime;
bool justReset;
@@ -11,13 +11,18 @@ namespace icsneo {
class VersionMessage : public Message {
public:
VersionMessage(bool main) : MainChip(main) { network = Network::NetID::Main51; }
enum Chip : uint8_t {
MainChip,
SecondaryChips
};
// If true, the included version is for the main chip
const bool MainChip;
VersionMessage(Chip chip) : Message(Message::Type::DeviceVersion), ForChip(chip) {}
// nullopt here indicates invalid
std::vector< optional<DeviceAppVersion> > Versions;
// What chips the versions are for
const Chip ForChip;
};
}
@@ -13,7 +13,7 @@ namespace icsneo {
typedef uint16_t icscm_bitfield;
struct HardwareCANPacket {
static std::shared_ptr<CANMessage> DecodeToMessage(const std::vector<uint8_t>& bytestream);
static std::shared_ptr<Message> DecodeToMessage(const std::vector<uint8_t>& bytestream);
static bool EncodeFromMessage(const CANMessage& message, std::vector<uint8_t>& bytestream, const device_eventhandler_t& report);
struct {
+3 -3
View File
@@ -121,8 +121,8 @@ public:
int addMessageCallback(const MessageCallback& cb) { return com->addMessageCallback(cb); }
bool removeMessageCallback(int id) { return com->removeMessageCallback(id); }
bool transmit(std::shared_ptr<Message> message);
bool transmit(std::vector<std::shared_ptr<Message>> messages);
bool transmit(std::shared_ptr<Frame> frame);
bool transmit(std::vector<std::shared_ptr<Frame>> frames);
void setWriteBlocks(bool blocks);
@@ -328,7 +328,7 @@ protected:
void handleInternalMessage(std::shared_ptr<Message> message);
virtual void handleDeviceStatus(const std::shared_ptr<Message>&) {}
virtual void handleDeviceStatus(const std::shared_ptr<RawMessage>&) {}
neodevice_t& getWritableNeoDevice() { return data; }
@@ -33,7 +33,7 @@ public:
virtual void handleMessage(const std::shared_ptr<Message>&) {}
// Return true to continue transmitting, success should be written to if false is returned
virtual bool transmitHook(const std::shared_ptr<Message>& message, bool& success) { (void)message; (void)success; return true; }
virtual bool transmitHook(const std::shared_ptr<Frame>& frame, bool& success) { (void)frame; (void)success; return true; }
protected:
Device& device;
@@ -25,7 +25,7 @@ public:
void onGoOffline() override;
void handleMessage(const std::shared_ptr<Message>& message) override;
bool transmitHook(const std::shared_ptr<Message>& message, bool& success) override;
bool transmitHook(const std::shared_ptr<Frame>& frame, bool& success) override;
std::shared_ptr<Controller> getController(uint8_t index) const {
if(index >= controllers.size())
@@ -104,11 +104,12 @@ protected:
// The supported TX networks are the same as the supported RX networks for this device
virtual void setupSupportedTXNetworks(std::vector<Network>& txNetworks) override { setupSupportedRXNetworks(txNetworks); }
void handleDeviceStatus(const std::shared_ptr<Message>& message) override {
if(!message || message->data.size() < sizeof(neovifire2_status_t))
void handleDeviceStatus(const std::shared_ptr<RawMessage>& message) override {
const auto& data = message->data;
if(data.size() < sizeof(neovifire2_status_t))
return;
std::lock_guard<std::mutex> lk(ioMutex);
const neovifire2_status_t* status = reinterpret_cast<const neovifire2_status_t*>(message->data.data());
const neovifire2_status_t* status = reinterpret_cast<const neovifire2_status_t*>(data.data());
backupPowerEnabled = status->backupPowerEnabled;
backupPowerGood = status->backupPowerGood;
ethActivationStatus = status->ethernetActivationLineEnabled;
@@ -24,7 +24,7 @@ public:
if (!fakedev->com->decoder->decode(msg, packet))
continue; // We failed to decode this packet
if(!msg || msg->network.getNetID() != Network::NetID::Main51)
if(!msg || msg->type != Message::Type::Main51)
continue; // Not a message we care about
auto sn = std::dynamic_pointer_cast<SerialNumberMessage>(msg);
if(!sn)
+4 -3
View File
@@ -80,11 +80,12 @@ protected:
return ret;
}
void handleDeviceStatus(const std::shared_ptr<Message>& message) override {
if(!message || message->data.size() < sizeof(fire2vnet_status_t))
void handleDeviceStatus(const std::shared_ptr<RawMessage>& message) override {
const auto& data = message->data;
if(data.size() < sizeof(fire2vnet_status_t))
return;
std::lock_guard<std::mutex> lk(ioMutex);
const fire2vnet_status_t* status = reinterpret_cast<const fire2vnet_status_t*>(message->data.data());
const fire2vnet_status_t* status = reinterpret_cast<const fire2vnet_status_t*>(data.data());
ethActivationStatus = status->ethernetActivationLineEnabled;
}
@@ -30,7 +30,7 @@ public:
if(!fakedev->com->decoder->decode(msg, packet))
continue; // We failed to decode this packet
if(!msg || msg->network.getNetID() != Network::NetID::Main51)
if(!msg || msg->type != Message::Type::Main51)
continue; // Not a message we care about
auto sn = std::dynamic_pointer_cast<SerialNumberMessage>(msg);
if(!sn)
@@ -118,11 +118,12 @@ protected:
// The supported TX networks are the same as the supported RX networks for this device
void setupSupportedTXNetworks(std::vector<Network>& txNetworks) override { setupSupportedRXNetworks(txNetworks); }
void handleDeviceStatus(const std::shared_ptr<Message>& message) override {
if(!message || message->data.size() < sizeof(radgalaxy_status_t))
void handleDeviceStatus(const std::shared_ptr<RawMessage>& message) override {
const auto& data = message->data;
if(data.size() < sizeof(radgalaxy_status_t))
return;
std::lock_guard<std::mutex> lk(ioMutex);
const radgalaxy_status_t* status = reinterpret_cast<const radgalaxy_status_t*>(message->data.data());
const radgalaxy_status_t* status = reinterpret_cast<const radgalaxy_status_t*>(data.data());
ethActivationStatus = status->ethernetActivationLineEnabled;
}
};
@@ -77,11 +77,12 @@ protected:
txNetworks.insert(txNetworks.end(), supportedTxNetworks.begin(), supportedTxNetworks.end());
}
void handleDeviceStatus(const std::shared_ptr<Message>& message) override {
if(!message || message->data.size() < sizeof(radgigalog_status_t))
void handleDeviceStatus(const std::shared_ptr<RawMessage>& message) override {
const auto& data = message->data;
if(data.size() < sizeof(radgigalog_status_t))
return;
std::lock_guard<std::mutex> lk(ioMutex);
const radgigalog_status_t* status = reinterpret_cast<const radgigalog_status_t*>(message->data.data());
const radgigalog_status_t* status = reinterpret_cast<const radgigalog_status_t*>(data.data());
ethActivationStatus = status->ethernetActivationLineEnabled;
}
};
@@ -23,7 +23,7 @@ public:
if (!fakedev->com->decoder->decode(msg, packet))
continue; // We failed to decode this packet
if(!msg || msg->network.getNetID() != Network::NetID::Main51)
if(!msg || msg->type != Message::Type::Main51)
continue; // Not a message we care about
auto sn = std::dynamic_pointer_cast<SerialNumberMessage>(msg);
if(!sn)
@@ -81,11 +81,12 @@ protected:
txNetworks.insert(txNetworks.end(), supportedTxNetworks.begin(), supportedTxNetworks.end());
}
void handleDeviceStatus(const std::shared_ptr<Message>& message) override {
if(!message || message->data.size() < sizeof(radgigastar_status_t))
void handleDeviceStatus(const std::shared_ptr<RawMessage>& message) override {
const auto& data = message->data;
if(data.size() < sizeof(radgigastar_status_t))
return;
std::lock_guard<std::mutex> lk(ioMutex);
const radgigastar_status_t* status = reinterpret_cast<const radgigastar_status_t*>(message->data.data());
const radgigastar_status_t* status = reinterpret_cast<const radgigastar_status_t*>(data.data());
ethActivationStatus = status->ethernetActivationLineEnabled;
}
};
@@ -23,7 +23,7 @@ public:
if (!fakedev->com->decoder->decode(msg, packet))
continue; // We failed to decode this packet
if(!msg || msg->network.getNetID() != Network::NetID::Main51)
if(!msg || msg->type != Message::Type::Main51)
continue; // Not a message we care about
auto sn = std::dynamic_pointer_cast<SerialNumberMessage>(msg);
if(!sn)
@@ -25,7 +25,7 @@ public:
if(!fakedev->com->decoder->decode(msg, packet))
continue; // We failed to decode this packet
if(!msg || msg->network.getNetID() != Network::NetID::Main51)
if(!msg || msg->type != Message::Type::Main51)
continue; // Not a message we care about
auto sn = std::dynamic_pointer_cast<SerialNumberMessage>(msg);
if(!sn)
@@ -73,11 +73,12 @@ protected:
size_t getEthernetActivationLineCount() const override { return 1; }
void handleDeviceStatus(const std::shared_ptr<Message>& message) override {
if(!message || message->data.size() < sizeof(valuecan4_2el_status_t))
void handleDeviceStatus(const std::shared_ptr<RawMessage>& message) override {
const auto& data = message->data;
if(data.size() < sizeof(valuecan4_2el_status_t))
return;
std::lock_guard<std::mutex> lk(ioMutex);
const valuecan4_2el_status_t* status = reinterpret_cast<const valuecan4_2el_status_t*>(message->data.data());
const valuecan4_2el_status_t* status = reinterpret_cast<const valuecan4_2el_status_t*>(data.data());
ethActivationStatus = status->ethernetActivationLineEnabled;
}
};
@@ -25,7 +25,7 @@ public:
if (!fakedev->com->decoder->decode(msg, packet))
continue; // We failed to decode this packet
if(!msg || msg->network.getNetID() != Network::NetID::Main51)
if(!msg || msg->type != Message::Type::Main51)
continue; // Not a message we care about
auto sn = std::dynamic_pointer_cast<SerialNumberMessage>(msg);
if(!sn)
@@ -25,7 +25,7 @@ public:
if (!fakedev->com->decoder->decode(msg, packet))
continue; // We failed to decode this packet
if(!msg || msg->network.getNetID() != Network::NetID::Main51)
if(!msg || msg->type != Message::Type::Main51)
continue; // Not a message we care about
auto sn = std::dynamic_pointer_cast<SerialNumberMessage>(msg);
if(!sn)
+1
View File
@@ -14,6 +14,7 @@
#include "icsneo/communication/message/ethernetmessage.h"
#include "icsneo/communication/message/flexray/flexraymessage.h"
#include "icsneo/communication/message/iso9141message.h"
#include "icsneo/communication/message/canerrorcountmessage.h"
namespace icsneo {