Index: sources/canbus/messagedispatcher.cpp =================================================================== diff -u -r8c69137f18382bdc55a5678e6ed44a7683fe4dea -r0e87420e50dd94c37eb25f289ef3262e0e45d7f4 --- sources/canbus/messagedispatcher.cpp (.../messagedispatcher.cpp) (revision 8c69137f18382bdc55a5678e6ed44a7683fe4dea) +++ sources/canbus/messagedispatcher.cpp (.../messagedispatcher.cpp) (revision 0e87420e50dd94c37eb25f289ef3262e0e45d7f4) @@ -22,6 +22,8 @@ #include "applicationcontroller.h" #include "frameinterface.h" +#define DEBUG_ACKBACK_HD_TO_UI + using namespace Can; /*! @@ -87,6 +89,9 @@ // From HD connect(&_FrameInterface , SIGNAL(didFrameReceive ( Can_Id, const QByteArray &)), this , SLOT( onFrameReceive ( Can_Id, const QByteArray &))); + + connect(this ,SIGNAL(didFramesTransmit( Can_Id, const FrameList &)), + this , SLOT( onFramesTransmit( Can_Id, const FrameList &))); } /*! @@ -145,11 +150,29 @@ // TODO : must be moved to a MessageModel class if (mMessage.isComplete()) { + rxCount(); + #ifdef QT_DEBUG + if (_rxSequence != mMessage.sequence) { + qDebug() << tr("Out of Sync : %1 , %2").arg(_rxSequence).arg(mMessage.sequence); + } + #endif interpretMessage(mMessage); } } /*! + * \brief MessageDispatcher::onFramesTransmit + * \details this slots calls the framesTransmit to emit the didFrameTransmit signal + * to queue the frame(s) to be sent + * \param vCan_Id - CanBus channel + * \param vFrameList - frame(s) to be sent + */ +void MessageDispatcher::onFramesTransmit(Can_Id vCan_Id, const FrameList &vFrameList) +{ + framesTransmit(vCan_Id, vFrameList); +} + +/*! * \brief MessageDispatcher::onActionTransmit * \details This slot will be called by ApplicationController::didActionTransmit * upon UI message transmit request and calls MessageDispatcher::actionTransmit method. @@ -158,7 +181,7 @@ */ void MessageDispatcher::onActionTransmit(GuiActionType vActionId, const QVariantList &vData) { - actionTransmit(vActionId, vData); + actionTransmit(vActionId, vData, txCount()); } /*! @@ -169,25 +192,49 @@ * \param vActionId - The ActionID of the message * \param vData - The data of the Message */ -void MessageDispatcher::actionTransmit(GuiActionType vActionId, const QVariantList &vData, Sequence /*vSequence*/) +void MessageDispatcher::actionTransmit(GuiActionType vActionId, const QVariantList &vData, Sequence vSequence) { QByteArray mData; if (! _interpreter.interpretMessage(vActionId, vData, mData)) { LOG_ERROR(tr("Incorrect Message, can't be interpreted")); return; } + // TODO : Create a buildFrames method FrameList frameList; - if ( ! _builder.buildFrames(vActionId, mData, frameList) ) { + Sequence mSequence = vSequence; + bool mNeedsAcknow = needsAcknow(vActionId); + if (mNeedsAcknow) mSequence = -mSequence; + + if ( ! _builder.buildFrames(vActionId, mData, frameList, mSequence) ) { LOG_ERROR(tr("Incorrect Message can't be built")); return; } - for (const auto &frame : frameList) { - emit didFrameTransmit(eChlid_UI_HD, frame); + if (mNeedsAcknow) { + // NOTE : here vSequence should be used which is not negative + // because when we get the Acknow it is not the negative + // since it does not need Re-Acknow + // and this is the sequence number which will be used + // to remove the message from the Acknow list. + emit didAcknowTransmit(vSequence, eChlid_UI_HD, frameList); } + framesTransmit(eChlid_UI_HD, frameList); } /*! + * \brief MessageDispatcher::framesTransmit + * \details iterates through all the frames and emits to send the frames + * \param vCan_Id - The channel to send the frames to + * \param vFrameList - List of the frames to be sent + */ +void MessageDispatcher::framesTransmit(Can_Id vCan_Id, const FrameList &vFrameList) +{ + for (const auto &frame : vFrameList) { + emit didFrameTransmit(vCan_Id, frame); + } +} + +/*! * \brief MessageDispatcher::buildMessage * \details Calls the messageBuilder buildMessage method. * \param vCan_Id - CANBUS channel of the frame @@ -228,7 +275,8 @@ break; default: if (vMessage.sequence < 0) { - //actionTransmit(-vMessage.sequence, GuiActionType::Acknow,{}); + actionTransmit(GuiActionType::Acknow, {}, vMessage.sequence); + qDebug() << tr(" ~~~~~~~~~~ Ack Back : %1~~~~~~~~~~ ").arg(vMessage.sequence); } emit didActionReceive(mActionId, mData); break; @@ -239,3 +287,58 @@ } +/*! + * \brief MessageDispatcher::rxCount + * \details count received messages up the size of the Sequence type size + * \return message count + */ +Sequence MessageDispatcher::rxCount() +{ + if ( _rxSequence < SEQUENCE_MAX ) { + ++_rxSequence; + } else { + _rxSequence = 1; + } + return _rxSequence; +} + +/*! + * \brief MessageDispatcher::txCount + * \details count transmitted messages up the size of the Sequence type size + * \return message count + */ +Sequence MessageDispatcher::txCount() +{ + if ( _txSequence < SEQUENCE_MAX ) { + ++_txSequence; + } else { + _txSequence = 1; + } + return _txSequence; +} + +/*! + * \brief MessageDispatcher::needsAcknow + * \details List of the Action types which need Acknow + * \param vActionId - Action Type id + * \return true if needs an Acknow + */ +bool MessageDispatcher::needsAcknow(GuiActionType vActionId) +{ + switch(vActionId) { + case GuiActionType::Acknow: + // this is the Acknow itself + // since the actionTransmit will make any needsAcknow sequence to negative + // and the Ack Back should be in positive after receiving it in negative + // actually this make it positive and send it back. + return true; + default: + // TEST : this code is for test. +#ifdef DEBUG_ACKBACK_HD_TO_UI + return ( ! (_txSequence % 21) ); +#else + return false; +#endif + } + return false; +}