Index: sources/canbus/messagehandler.h =================================================================== diff -u -rd6c7d6c382fe56e19d132f6ba0bcbbc1e6b2060f -r212de04bcfd9c0ec4c13faca1d4227a8ae30e978 --- sources/canbus/messagehandler.h (.../messagehandler.h) (revision d6c7d6c382fe56e19d132f6ba0bcbbc1e6b2060f) +++ sources/canbus/messagehandler.h (.../messagehandler.h) (revision 212de04bcfd9c0ec4c13faca1d4227a8ae30e978) @@ -44,14 +44,14 @@ * \brief The MessageHandler class * \details This class is handling the can message by building and striping it. * // -- CAN MESSAGE STRUCTURE -- - * #1 #2 #3 #4 #5 #6 #7 #8 - * +---+---+---+---+---+---+---+---+...........+ + * #0 #1 #2 #3 #4 #5 #6 #7 + * +---+---+---+---+---+---+---+---+--.......--+ * | A5| MsgId |Len| Payload |CRC|..padding..| - * +---+---+---+---+---+---+---+---+...........+ + * +---+---+---+---+---+---+---+---+--.......--+ * Header Frame: * 1 - CRC is last after payload - * Ex1 - If Len=0 then CRC is #5 - * Ex2 - If Len=1 then CRC is #6 and payload is #5 + * 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: @@ -65,37 +65,24 @@ // friends friend class ::unittests; - struct Message { - quInt8 length; - GuiActionType actionId; - QByteArray data; - bool isFull() { - return length == data.length(); - } + const QHash payloadLen { + {GuiActionType::PowerOff, 0x01}, + {GuiActionType::Check_In, 0x00} }; - typedef QList MessageList; - QHash messages; - - const QMap payloadLen { - {GuiActions::PowerOff, 0x01}, - {GuiActions::Check_In, 0x00} - }; - // constants enum Payload_Data : quint8 { ePayload_None = 0x00, ePayload_Sync = 0xA5, }; enum Frame_Data : quint8 { - eCanFrameLen = 8, - eMaxHeaderData = 3, + eLenCanFrame = 8, + eLenMaxHeaderData = 3, - eLenSync = 1, - eLenActionId = 2, - eLenPayload = 1, - eLenChecksum = 1, + eLenSyncByte = 1, + eLenActionId = 2, + eLenLength = 1, }; @@ -122,46 +109,53 @@ 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: - void init(); + public: + void init(); private: void connection(); void checked_in(); // TODO : needs to be renamed and called from the main timer to tell HD, UI is alive // build message to be sent QByteArray buildMessage (GuiActionType vAction , GuiActionData vData = GuiActionData::NoData); - void addSyncByte ( QByteArray &vPayload ); - void addActionId ( QByteArray &vPayload , GuiActionType vAction); - void addLegth ( QByteArray &vPayload , GuiActionType vAction); - void addPayload ( QByteArray &vPayload , GuiActionData vData = GuiActionData::NoData); - void addChecksum ( QByteArray &vPayload ); - void addPadding ( QByteArray &vPayload ); + void addSyncByte (QByteArray &vPayload ); + void addActionId (QByteArray &vPayload , GuiActionType vAction); + void addLegth (QByteArray &vPayload , GuiActionType vAction); + void addData (QByteArray &vPayload , GuiActionData vData = GuiActionData::NoData); + void addCRC (QByteArray &vPayload ); + void addPadding (QByteArray &vPayload ); - quint8 checksum ( QByteArray &vBytes ); + quint8 calcCRC (const QByteArray &vData ); + bool checkCRC (const QByteArray &vData , quint8 vCRC); // strip message to its parts - QByteArray stripMessage ( const QCanBusFrame &vFrame ); - bool hasSyncByte ( QByteArray &vPayload ); - GuiActionType getActionId ( QByteArray &vPayload ); - quint8 getLength ( QByteArray &vPayload ); - QByteArray getPayload ( QByteArray &vPayload ); - bool getChecksum ( QByteArray &vPayload ); + QByteArray stripMessage (const QCanBusFrame &vFrame ); + bool hasSyncByte (QByteArray &vPayload ); + GuiActionType getActionId (QByteArray &vPayload ); + quint8 getLength (QByteArray &vPayload ); + QByteArray getData (QByteArray &vPayload , quint8 vLen , bool *ok = nullptr ); + // 0 - command definition // 0.0- Can ID // 0.1- command id // 0.2- command payload length - // 1 - create, frame, schedule, send - - // 2 - read, categorize, buffer, act private slots: // Should be private for thread safety and is connected internally.