Index: sources/canbus/messagebuilder.cpp =================================================================== diff -u -r227f28b202f81ab3dd04a81868697025ccab6220 -re58b907a69d4ca7daa77d69791593b886d1b80e8 --- sources/canbus/messagebuilder.cpp (.../messagebuilder.cpp) (revision 227f28b202f81ab3dd04a81868697025ccab6220) +++ sources/canbus/messagebuilder.cpp (.../messagebuilder.cpp) (revision e58b907a69d4ca7daa77d69791593b886d1b80e8) @@ -19,6 +19,7 @@ #include "logger.h" #include "crc.h" #include "format.h" +#include "types.h" // namespace using namespace Can; @@ -191,6 +192,27 @@ } /*! + * \brief MessageBuilder::checkCRC + * \details Overloaded CheckCRC which checks the CRC and Log the error if there is. + * \param vMessage + * \return + */ +bool MessageBuilder::checkCRC(const Message &vMessage) +{ + consoleOut("", false, Can_Id::eChlid_NONE); + QByteArray crcData = vMessage.head + vMessage.data; + quint8 mExpected = 0; + quint8 mBeenRead = 0; + if ( ! checkCRC(crcData, mExpected, mBeenRead ) ) { // CRC is always next byte after Data + LOG_ERROR(tr("CRC error, expected %1 but got %2") + .arg(Format::toHexString(mExpected, true, eLenCRCDigits)) + .arg(Format::toHexString(mBeenRead, true, eLenCRCDigits))); + return false; + } + return true; +} + +/*! * \brief MessageBuilder::buildMessage * \details Builds Message out of vPayload of type QByteArray * by adding Sync byte, ActionID(MessageID), data length, data, CANBUS channel id for header @@ -208,6 +230,9 @@ consoleOut(vPayload, true, vCan_Id); vMessage.can_id = vCan_Id; vMessage.head = getHeader (mPayload); // keep header before taking it out of the payload. doesn't affect payload +#ifndef DISABLE_SEQUENCE + vMessage.sequence = getSequence (mPayload); +#endif vMessage.actionId = getActionId (mPayload); vMessage.length = getLength (mPayload); vMessage.data = getData (mPayload, vMessage.length); @@ -226,16 +251,8 @@ // and when Message model identifies the message is complete // will SIGNAL builder to check for crc. if (vMessage.isComplete()) { - consoleOut("", false, Can_Id::eChlid_NONE); - QByteArray crcData = vMessage.head + vMessage.data; - quint8 mExpected = 0; - quint8 mBeenRead = 0; - if ( ! checkCRC(crcData, mExpected, mBeenRead ) ) { // CRC is always next byte after Data - LOG_ERROR(tr("CRC error, expected %1 but got %2") - .arg(Format::toHexString(mExpected, true, eLenCRCDigits)) - .arg(Format::toHexString(mBeenRead, true, eLenCRCDigits))); - return false; - } + bool ok = checkCRC(vMessage); + if (!ok) return false; } return true; @@ -261,6 +278,24 @@ } /*! + * \brief MessageBuilder::getSequence + * \details Extract the 2 bytes of the sequence + * out of the vPayload of type QByteArray + * \param vPayload - The payload of the CANBUS message + * \return Returns ActionId of type GuiActionType + * \note Removes the 2 bytes of ActionID from vPayload + * It starts from the first byte so those 2 bytes should be the first 2 bytes. + */ +qint16 MessageBuilder::getSequence(QByteArray &vPayload) +{ + Types::S16 mSequence; + int index = 0; + Types::getValue<>(vPayload, index, mSequence); + vPayload = vPayload.mid(eLenSequence); + return mSequence.value; +} + +/*! * \brief MessageBuilder::getHeader * \details Collect the 3 bytes (Frame_Data::eLenHeaderInfo) * as header portion of the payload @@ -277,6 +312,10 @@ QByteArray MessageBuilder::getHeader(const QByteArray &vPayload) { QByteArray headInfo; + if (vPayload.length() < eLenHeaderInfo) { + LOG_ERROR("Incorrect Message Header"); + return headInfo; + } for (int i = 0; i < eLenHeaderInfo; i++) { headInfo += vPayload[i]; } @@ -296,10 +335,6 @@ { quint16 mActionId; mActionId = (vPayload[0] << 8) | vPayload[1]; - - //TODO : It needs to be checked that the ActionID is Correct. - //return GuiActionType::Unknown; - vPayload = vPayload.mid(eLenActionId); return static_cast(mActionId); }