Index: sources/canbus/messagehandler.h =================================================================== diff -u -r4c53c7c62df7562a6c0915f0c5af7ea0ed884ca3 -r68fe14b0d2f0e79d03c95cc0dfff5b47948b6d5b --- sources/canbus/messagehandler.h (.../messagehandler.h) (revision 4c53c7c62df7562a6c0915f0c5af7ea0ed884ca3) +++ sources/canbus/messagehandler.h (.../messagehandler.h) (revision 68fe14b0d2f0e79d03c95cc0dfff5b47948b6d5b) @@ -25,6 +25,7 @@ // Project #include "main.h" #include "guiactions.h" +#include "messagepartitioner.h" // Define #define _MessageHandler MessageHandler::I() @@ -39,93 +40,15 @@ #define CAN_FRAME_LENGTH 8 #define MAX_PAYLOAD_IN_HEADER_FRAME 3 -/*! - * - * \brief The MessageHandler class - * \details This class is handling the can message by building and striping it. - * - * // -- CAN PAYLOAD STRUCTURE -- - * #0 #1 #2 #3 #4 #5 #6 #7 - * +---+---+---+---+---+---+---+---+--.......--+ - * | A5| MsgId |Len| Data |CRC|..padding..| - * +---+---+---+---+---+---+---+---+--.......--+ - * - * Header Frame: - * 1 - CRC is last after payload - * Ex1 - If Len=0 then CRC is #4 - * Ex2 - If Len=1 then CRC is #5 and payload is #4 - * 2 - If CRC is not the last by in the frame - * then there is padding 0x00 to make the frame 8 byte fix - * - * Tail Frame: - * 3 - Partial frames only have Payload and CRC ( and padded) - * - */ class MessageHandler : public QObject { Q_OBJECT // friends friend class ::unittests; - const QHash payloadLen { - {GuiActionType::PowerOff, 1}, - {GuiActionType::Check_In, 0} - }; + MessagePartitioner _partitioner; - // constants - enum Payload_Data : quint8 { - ePayload_None = 0x00, - ePayload_Sync = 0xA5, - }; - - enum Frame_Data : quint8 { - eLenCanFrame = 8, - eLenMaxHeaderData = 3, - eLenMaxData = 255, - - eLenSyncByte = 1, - eLenActionId = 2, - eLenLength = 1, - - }; - - enum Can_Id : quint16 { - // Broadcasts - //// Alarm - eChlid_HD_Alarm = 0x001, ///< HD alarm broadcast - eChlid_DG_Alarm = 0x002, ///< DG alarm broadcast - eChlid_UI_Alarm = 0x004, ///< UI alarm broadcast [Out] - //// Sync - eChlid_HD_Sync = 0x040, ///< HD sync broadcast - eChlid_DG_Sync = 0x080, ///< DG sync broadcast - eChlid_UI_Sync = 0x200, ///< UI sync broadcast [Out] - - // UI not listening - eChlid_HD_DG = 0x008, ///< HD => DG - eChlid_DG_HD = 0x010, ///< DG => HD - - // UI is listening - eChlid_HD = 0x020, ///< HD => UI - eChlid_UI = 0x100, ///< UI => HD [Out] - - // UI lessens occasionally - eChlid_DG_UI = eChlid_DG_Alarm, ///< No direct channel has been defined between DG&UI, May be required for logging - eChlid_UI_DG = eChlid_UI_Sync , ///< No direct channel has been defined between DG&UI, May be required for logging - - }; - - struct Message { // Private Data Class - quint8 length; - GuiActionType actionId; - QByteArray data; - - bool isComplete() { return data.length() == length; } - bool isEmpty () { return data.length() == 0 ; } - }; - typedef QList MessageList; - QHash messages; - // Singleton SINGLETON_DECL(MessageHandler) public: @@ -135,31 +58,13 @@ void initConnections(); void checked_in(); - void transmitFrame(MessageHandler::Can_Id vCan_Id, GuiActionType vAction, const QByteArray &vData = 0); + void transmitFrame(Message::Can_Id vCan_Id, GuiActionType vAction, const QByteArray &vData = 0); bool ignoreChannel(const QCanBusFrame &vFrame); - // build message to be sent - QList buildMessage(GuiActionType vAction , const QByteArray &vData); - void addSyncByte (QByteArray &vPayload); - void addActionId (QByteArray &vPayload, GuiActionType vAction); - void addData (QByteArray &vPayload, GuiActionType vAction, const QByteArray &vData); - void addCRC (QByteArray &vPayload); - void addPadding (QByteArray &vPayload); - // CRC tools - quint8 calcCRC (const QByteArray &vData); - bool checkCRC (const QByteArray &vData, quint8 vCRC); - - // strip message to its parts - bool stripMessage (const QCanBusFrame &vFrame, Message &vMessage); - bool hasSyncByte (QByteArray &vPayload); - GuiActionType getActionId (QByteArray &vPayload); - quint8 getLength (QByteArray &vPayload); - QByteArray getData (QByteArray &vPayload, quint8 vLen, bool *ok = nullptr); - // interpret the data into GUI understandable Actions/Data - bool interpretData(const Message &vMessage, GuiActionType &vActionId, GuiActionData &vData); + bool interpretData(const MessagePartitioner::Packet &vMessage, GuiActionType &vActionId, GuiActionData &vData); private slots: // Should be private for thread safety and is connected internally. void onActionTransmit(GuiActionType vAction, const QByteArray &vData); // UI => HD/DG