Refactor MessageFilter and MessageCallback
parent
28de70aa05
commit
72773d9afa
|
|
@ -9,6 +9,8 @@
|
|||
#include "communication/include/command.h"
|
||||
#include "communication/include/messagedecoder.h"
|
||||
#include "communication/include/packetizer.h"
|
||||
#include "communication/message/include/serialnumbermessage.h"
|
||||
#include "communication/message/filter/include/main51messagefilter.h"
|
||||
|
||||
using namespace icsneo;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@
|
|||
#include "communication/include/command.h"
|
||||
#include "communication/include/network.h"
|
||||
#include "communication/include/packet.h"
|
||||
#include "communication/message/callback/include/messagecallback.h"
|
||||
#include "communication/message/include/serialnumbermessage.h"
|
||||
#include "communication/include/packetizer.h"
|
||||
#include "communication/include/messagedecoder.h"
|
||||
#include <memory>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
#ifndef __CANMESSAGECALLBACK_H_
|
||||
#define __CANMESSAGECALLBACK_H_
|
||||
|
||||
#include "communication/message/callback/include/messagecallback.h"
|
||||
#include "communication/message/include/canmessage.h"
|
||||
#include "communication/message/filter/include/canmessagefilter.h"
|
||||
#include <memory>
|
||||
|
||||
namespace icsneo {
|
||||
|
||||
class CANMessageCallback : public MessageCallback {
|
||||
public:
|
||||
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(CANMessageFilter f, fn_messageCallback cb) : MessageCallback(cb, std::make_shared<CANMessageFilter>(f)) {}
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
#ifndef __MAIN51MESSAGECALLBACK_H_
|
||||
#define __MAIN51MESSAGECALLBACK_H_
|
||||
|
||||
#include "communication/message/callback/include/messagecallback.h"
|
||||
#include "communication/message/include/main51message.h"
|
||||
#include "communication/message/filter/include/main51messagefilter.h"
|
||||
#include <memory>
|
||||
|
||||
namespace icsneo {
|
||||
|
||||
class Main51MessageCallback : public MessageCallback {
|
||||
public:
|
||||
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(Main51MessageFilter f, fn_messageCallback cb) : MessageCallback(cb, std::make_shared<Main51MessageFilter>(f)) {}
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,46 +1,36 @@
|
|||
#ifndef __MESSAGECALLBACK_H_
|
||||
#define __MESSAGECALLBACK_H_
|
||||
|
||||
#include "communication/message/include/message.h"
|
||||
#include "communication/include/messagefilter.h"
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
|
||||
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(MessageFilter f, fn_messageCallback cb) { MessageCallback(cb, 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;
|
||||
};
|
||||
|
||||
class CANMessageCallback : public MessageCallback {
|
||||
public:
|
||||
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(CANMessageFilter f, fn_messageCallback cb) : MessageCallback(cb, std::make_shared<CANMessageFilter>(f)) {}
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#ifndef __MESSAGECALLBACK_H_
|
||||
#define __MESSAGECALLBACK_H_
|
||||
|
||||
#include "communication/message/include/message.h"
|
||||
#include "communication/message/filter/include/messagefilter.h"
|
||||
#include <memory>
|
||||
|
||||
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(MessageFilter f, fn_messageCallback cb) { MessageCallback(cb, 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,38 @@
|
|||
#ifndef __CANMESSAGEFILTER_H_
|
||||
#define __CANMESSAGEFILTER_H_
|
||||
|
||||
#include "communication/message/filter/include/messagefilter.h"
|
||||
#include "communication/include/network.h"
|
||||
#include "communication/message/include/message.h"
|
||||
#include "communication/message/include/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 "communication/message/filter/include/messagefilter.h"
|
||||
#include "communication/include/network.h"
|
||||
#include "communication/include/communication.h"
|
||||
#include "communication/message/include/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
|
||||
|
|
@ -1,72 +1,47 @@
|
|||
#ifndef __MESSAGEFILTER_H_
|
||||
#define __MESSAGEFILTER_H_
|
||||
|
||||
#include "communication/include/network.h"
|
||||
#include "communication/message/include/message.h"
|
||||
#include "communication/message/include/canmessage.h"
|
||||
#include <memory>
|
||||
|
||||
namespace icsneo {
|
||||
|
||||
class MessageFilter {
|
||||
public:
|
||||
MessageFilter() : matchAny(true) {}
|
||||
MessageFilter(Network::Type type) : type(type) {}
|
||||
MessageFilter(Network::NetID netid) : netid(netid) {}
|
||||
virtual ~MessageFilter() {}
|
||||
|
||||
virtual bool match(const std::shared_ptr<Message>& message) const {
|
||||
if(matchAny)
|
||||
return true;
|
||||
if(!matchType(message->network.getType()))
|
||||
return false;
|
||||
if(!matchNetID(message->network.getNetID()))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
bool matchAny = false;
|
||||
|
||||
Network::Type type = Network::Type::Invalid; // Matching a type of invalid will match any
|
||||
bool matchType(Network::Type mtype) const {
|
||||
if(type == Network::Type::Invalid)
|
||||
return true;
|
||||
return type == mtype;
|
||||
}
|
||||
|
||||
Network::NetID netid = Network::NetID::Invalid; // Matching a netid of invalid will match any
|
||||
bool matchNetID(Network::NetID mnetid) const {
|
||||
if(netid == Network::NetID::Invalid)
|
||||
return true;
|
||||
return netid == mnetid;
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#ifndef __MESSAGEFILTER_H_
|
||||
#define __MESSAGEFILTER_H_
|
||||
|
||||
#include "communication/include/network.h"
|
||||
#include "communication/message/include/message.h"
|
||||
#include <memory>
|
||||
|
||||
namespace icsneo {
|
||||
|
||||
class MessageFilter {
|
||||
public:
|
||||
MessageFilter() : matchAny(true) {}
|
||||
MessageFilter(Network::Type type) : type(type) {}
|
||||
MessageFilter(Network::NetID netid) : netid(netid) {}
|
||||
virtual ~MessageFilter() {}
|
||||
|
||||
virtual bool match(const std::shared_ptr<Message>& message) const {
|
||||
if(matchAny)
|
||||
return true;
|
||||
if(!matchType(message->network.getType()))
|
||||
return false;
|
||||
if(!matchNetID(message->network.getNetID()))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
bool matchAny = false;
|
||||
|
||||
Network::Type type = Network::Type::Invalid; // Matching a type of invalid will match any
|
||||
bool matchType(Network::Type mtype) const {
|
||||
if(type == Network::Type::Invalid)
|
||||
return true;
|
||||
return type == mtype;
|
||||
}
|
||||
|
||||
Network::NetID netid = Network::NetID::Invalid; // Matching a netid of invalid will match any
|
||||
bool matchNetID(Network::NetID mnetid) const {
|
||||
if(netid == Network::NetID::Invalid)
|
||||
return true;
|
||||
return netid == mnetid;
|
||||
}
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef __MAIN51MESSAGE_H_
|
||||
#define __MAIN51MESSAGE_H_
|
||||
|
||||
#include "communication/message/include/message.h"
|
||||
#include "communication/include/communication.h"
|
||||
|
||||
namespace icsneo {
|
||||
|
||||
class Main51Message : public Message {
|
||||
public:
|
||||
virtual ~Main51Message() = default;
|
||||
Command command;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -8,7 +8,7 @@ namespace icsneo {
|
|||
|
||||
class Message {
|
||||
public:
|
||||
virtual ~Message() {}
|
||||
virtual ~Message() = default;
|
||||
Network network;
|
||||
std::vector<uint8_t> data;
|
||||
uint64_t timestamp;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef __SERIALNUMBERMESSAGE_H_
|
||||
#define __SERIALNUMBERMESSAGE_H_
|
||||
|
||||
#include "communication/message/include/main51message.h"
|
||||
#include "communication/include/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
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
#include "include/device.h"
|
||||
#include "communication/message/callback/include/messagecallback.h"
|
||||
#include "communication/include/command.h"
|
||||
#include <string.h>
|
||||
#include <iostream>
|
||||
|
|
|
|||
Loading…
Reference in New Issue