#include "MessageDispatcherAutoGen.h" // Qt #include #include // Project #include "Logger.h" #include "ApplicationController.h" #include "FrameInterface.h" #include "MessageAcknowModel.h" //#define DEBUG_ACKBACK_HD_TO_UI //#define DEBUG_OUT_OF_SYNC using namespace Can; /*! * \brief MessageDispatcher::MessageDispatcher * \details Constructor * \param parent - QObject parent owner object. * Qt handles the children destruction by their parent objects life-cycle. */ MessageDispatcher::MessageDispatcher(QObject *parent) : QObject(parent) { } /*! * \brief Message Handler initializer */ bool MessageDispatcher::init() { if ( _init ) return false; _init = true; // runs in the thread initConnections(); LOG_DEBUG(QString("%1 Initialized").arg(metaObject()->className())); return true; } /*! * \brief MessageDispatcher::init * \details Initialized the Class by calling the init() method first * And initializes the thread vThread by calling initThread * on success init(). * \param vThread - the thread * \return returns the return value of the init() method */ bool MessageDispatcher::init(QThread &vThread) { if ( ! init() ) return false; initThread(vThread); return true; } /*! * \brief MessageDispatcher::quit * \details quits the class * Calls quitThread */ void MessageDispatcher::quit() { quitThread(); // validated } /*! * \brief Message Handler connections definition * \details Initializes the required signal/slot connection between this class and other objects * to be able to communicate. */ void MessageDispatcher::initConnections() { // From GUI connect(&_ApplicationController, SIGNAL(didActionTransmit(GuiActionType , const QVariantList &)), this , SLOT( onActionTransmit(GuiActionType , const QVariantList &))); // From HD connect(&_FrameInterface , SIGNAL(didFrameReceive (Can_Id , const QByteArray &)), this , SLOT( onFrameReceive (Can_Id , const QByteArray &))); // From Message Acknow Model timer timeout. connect(&_MessageAcknowModel , SIGNAL(didFramesTransmit(Can_Id, Sequence, const FrameList &)), this , SLOT( onFramesTransmit(Can_Id, Sequence, const FrameList &))); connect(&_MessageAcknowModel , SIGNAL(didFailedTransmit( Sequence )), this , SLOT( onFailedTransmit( Sequence ))); // Application Settings are ready connect(&_ApplicationController, SIGNAL(didSettingsDone ()), this , SLOT( onSettingsDone ())); // ---- Signal/Slots ADJUST_TRANSMT_MODEL_BRIDGE_CONNECTIONS(_ApplicationController) ACTION_RECEIVE_MODEL_BRIDGE_CONNECTIONS(_interpreter ) } /*! * \brief ApplicationController::initThread * \details Moves this object into the thread vThread. * And checks that this method is called from main thread. * Also connects quitThread to application aboutToQuit. * \param vThread - the thread */ void MessageDispatcher::initThread(QThread &vThread) { // runs in main thread Q_ASSERT_X(QThread::currentThread() == qApp->thread() , __func__, "The Class initialization must be done in Main Thread" ); _thread = &vThread; _thread->setObjectName(QString("%1_Thread").arg(metaObject()->className())); connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(quit())); moveToThread(_thread); _thread->start(); } /*! * \brief MessageDispatcher::quitThread * \details Moves this object to main thread to be handled by QApplication * And to be destroyed there. */ void MessageDispatcher::quitThread() { if ( ! _thread ) return; // runs in thread moveToThread(qApp->thread()); // validated } /*! * \brief MessageDispatcher::onFrameReceive * \details Upon message has been received over CANBus this slot will be called * by FrameInterface::didFrameReceive signal to process the frame * Upon completion of collected all the required frames * on successful interpretation of the message, emits didActionReceived signal. * The message will be removed from list of the channel vCan_Id messages. * \param vCan_Id - CANBus channel of the frame * \param vPayload - Payload of the frame */ void MessageDispatcher::onFrameReceive(Can_Id vCan_Id, const QByteArray &vPayload) { // Append a message to the list // because if the list is empty there is no last() item if (_messageList[vCan_Id].isEmpty() || _messageList[vCan_Id].last().isComplete()) { _messageList[vCan_Id].append(Message()); } // build the message and check. if (! buildMessage(vCan_Id, vPayload)) { return; } Message mMessage = _messageList[vCan_Id].last(); // TODO : must be moved to a MessageModel class if (mMessage.isComplete()) { rxCount(); #ifdef DEBUG_OUT_OF_SYNC 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 vSequence - sequence of the message which is going to be resent. (not used) * \param vFrameList - frame(s) to be sent */ void MessageDispatcher::onFramesTransmit(Can_Id vCan_Id, Sequence vSequence, const FrameList &vFrameList) { Q_UNUSED(vSequence) framesTransmit(vCan_Id, vFrameList); } /*! * \brief MessageDispatcher::onFailedTransmit * \details the slot which will be called on transmit failed on MessageAcknowModel * \param vSequence */ void MessageDispatcher::onFailedTransmit(Sequence vSequence) { emit didFailedTransmit(vSequence); } /*! * \brief MessageDispatcher::onActionTransmit * \details This slot will be called by ApplicationController::didActionTransmit * upon UI message transmit request and calls MessageDispatcher::actionTransmit method. * \param vActionId - The ActionID of the message * \param vData - The data of the Message */ void MessageDispatcher::onActionTransmit(GuiActionType vActionId, const QVariantList &vData) { actionTransmit(vActionId, vData); } /*! * \brief MessageDispatcher::onSettingsDone * \details The slot to handle didSettingsDone signal of the ApplicationController */ void MessageDispatcher::onSettingsDone() { _interpreter.updateUnhandledMessages(); } // ---------------------------------------------------------------------------------------------------- /* START OF AUTO GENERATED CODE */ /*! * \brief MessageDispatcher::onAdjustment * \details This method transmits the ID_UIActiveAlarmsListRequest message. * \param vData - Data model of UIActiveAlarmsListRequestData. * \return void */ void MessageDispatcher::onAdjustment(const UIActiveAlarmsListRequestData &) { QVariantList mData; actionTransmit(GuiActionType::ID_UIActiveAlarmsListRequest, mData); } /*! * \brief MessageDispatcher::onAdjustment * \details This method transmits the ID_FWVersionsRequest message. * \param vData - Data model of FWVersionsRequestData. * \return void */ void MessageDispatcher::onAdjustment(const FWVersionsRequestData &vData) { QVariantList mData; mData += vData.mMajor ; mData += vData.mMinor ; mData += vData.mMicro ; mData += vData.mBuild ; mData += vData.mCompatibility; actionTransmit(GuiActionType::ID_FWVersionsRequest, mData); } /*! * \brief MessageDispatcher::onAdjustment * \details This method transmits the ID_UICheckIN message. * \param vData - Data model of UICheckINData. * \return void */ void MessageDispatcher::onAdjustment(const UICheckINData &) { QVariantList mData; actionTransmit(GuiActionType::ID_UICheckIN, mData); } /*! * \brief MessageDispatcher::onAdjustment * \details This method transmits the ID_UITreatmentParamsTOValidate message. * \param vData - Data model of UITreatmentParamsTOValidateData. * \return void */ void MessageDispatcher::onAdjustment(const UITreatmentParamsTOValidateData &vData) { QVariantList mData; mData += vData.mBloodflowrate_ml_min ; mData += vData.mDialysateflowrate_ml_min ; mData += vData.mTreatmentduration_min ; mData += vData.mSalinebolusvolume_ml ; mData += vData.mHepstoptime_min ; mData += vData.mHeptype ; mData += vData.mAcidconcentrate ; mData += vData.mBicarbconcentrate ; mData += vData.mDialyzertype ; mData += vData.mBpinterval_min ; mData += vData.mRinsebackflowrate_ml_min ; mData += vData.mRinsebackvolume_ml ; mData += vData.mArterialpressurelimitwindow_mmhg ; mData += vData.mVenouspressurelimitwindow_mmhg ; mData += vData.mVenouspressurelimitasymmetric_mmhg; mData += vData.mTmplimitwindow_mmhg ; mData += vData.mDialysatetemperature_degc ; mData += vData.mHepdispenserate_ml_hr ; mData += vData.mHepbolusvolume_ml ; actionTransmit(GuiActionType::ID_UITreatmentParamsTOValidate, mData); } /*! * \brief MessageDispatcher::onAdjustment * \details This method transmits the ID_UIUltrafiltrationVolumeTOValidate message. * \param vData - Data model of UIUltrafiltrationVolumeTOValidateData. * \return void */ void MessageDispatcher::onAdjustment(const UIUltrafiltrationVolumeTOValidateData &vData) { QVariantList mData; mData += vData.mUfvolumeml; actionTransmit(GuiActionType::ID_UIUltrafiltrationVolumeTOValidate, mData); } /*! * \brief MessageDispatcher::onAdjustment * \details This method transmits the ID_UITreatmentParamsConfirmed message. * \param vData - Data model of UITreatmentParamsConfirmedData. * \return void */ void MessageDispatcher::onAdjustment(const UITreatmentParamsConfirmedData &vData) { QVariantList mData; mData += vData.mConfirmed; actionTransmit(GuiActionType::ID_UITreatmentParamsConfirmed, mData); } /*! * \brief MessageDispatcher::onAdjustment * \details This method transmits the ID_UIInitiateTreatmentWorkflow message. * \param vData - Data model of UIInitiateTreatmentWorkflowData. * \return void */ void MessageDispatcher::onAdjustment(const UIInitiateTreatmentWorkflowData &vData) { QVariantList mData; mData += vData.mCmd; actionTransmit(GuiActionType::ID_UIInitiateTreatmentWorkflow, mData); } /*! * \brief MessageDispatcher::onAdjustment * \details This method transmits the ID_UIUFPauseResumeRequest message. * \param vData - Data model of UIUFPauseResumeRequestData. * \return void */ void MessageDispatcher::onAdjustment(const UIUFPauseResumeRequestData &vData) { QVariantList mData; mData += vData.mPayload; actionTransmit(GuiActionType::ID_UIUFPauseResumeRequest, mData); } /* END OF AUTO GENERATED CODE */ // ---------------------------------------------------------------------------------------------------- /*! * \brief MessageDispatcher::actionTransmit * \details This method is called by slot MessageDispatcher::onActionTransmit * which emits didFrameTransmit on successful interpretation of the requested message * and successfully creating of frame(s). * \param vActionId - The ActionID of the message * \param vData - The data of the Message */ void MessageDispatcher::actionTransmit(GuiActionType vActionId, const QVariantList &vData, Sequence vSequence, Can_Id vCanId) { txCount(); if (vSequence == 0) { // initialize // it's obvious that this assignment does not effect outside of the function. // but is make it easier to just assume the correct value has been passed // and still using the same variable (function parameter) as a local variable. vSequence = _txSequence; } QByteArray mData; Can_Id canid = vCanId; if (! _interpreter.interpretMessage(vActionId, vData, mData, canid)) { LOG_DEBUG(QString("Incorrect Message, cannot be interpreted, %1").arg(Format::toHexString(vActionId))); // TODO : LogInfo Improvement return; } // TODO : Create a buildFrames method FrameList frameList; Sequence mSequence = vSequence; bool mNeedsAcknow = needsAcknow(vActionId); if (mNeedsAcknow) { mSequence = -mSequence; if ( ! gDisableAcknowLog ) { LOG_APPED_UI(tr("Ack Req, Sq:%1, ID:%2").arg(mSequence).arg(Format::toHexString(vActionId))); } #ifdef DEBUG_ACKBACK_HD_TO_UI qDebug() << tr(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ UI AckReq : %1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ").arg(mSequence); #endif } // still checking here in case the logic has changed therefore buildFrame should still validate the message for developer safety. if ( ! _builder.buildFrames(vActionId, mData, frameList, mSequence) ) { LOG_DEBUG(QString("Incorrect Message cannot be built")); // TODO : LogInfo Improvement return; } 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(canid, vSequence, frameList); } framesTransmit(canid, 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 * \param vPayload - Payload of the frame * \return false on error */ bool MessageDispatcher::buildMessage(Can_Id vCan_Id, const QByteArray &vPayload) { if (vPayload.length() < eLenCanFrame) { // Each frame has to have exactly 8 (eLenCanFrame) bytes of data and unused bytes should be passed as 00. LOG_DEBUG(QString("Incorrect frame length. Exp:%1,got:%2").arg(eLenCanFrame).arg(vPayload.length())); return false; } if (! _builder.buildMessage(vPayload, _messageList[vCan_Id].last(), vCan_Id)) { _messageList[vCan_Id].removeLast(); return false; } return true; } /*! * \brief MessageDispatcher::checkAcknowReceived * \details check if the message was an acknowledge. * \param vMessage - The received message * \param vSrcText - The source entity identifier text * \return true if the message is Acknow */ bool MessageDispatcher::checkAcknowReceived(const Message &vMessage, const QString &vSrcText) { GuiActionType mActionId = vMessage.actionId; Sequence mSequence = vMessage.sequence; bool ok = false; if ( mActionId == GuiActionType::ID_AckMessageThatRequiresAck ) { ok = true; if ( ! gDisableAcknowLog ) { LOG_APPED(tr(" ,%1,Ack Bak, Sq:%2").arg(vSrcText).arg(mSequence)); } #ifdef DEBUG_ACKBACK_HD_TO_UI qDebug() << tr(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HD AckBak : %1 %2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ").arg(mSequence).arg(vMessage.actionId); #endif emit didAcknowReceive(mSequence); } return ok; } /*! * \brief MessageDispatcher::checkAcknowTransmit * \details Sends the Ack message. * \param vMessage - the received message which to check if it may need Ack. * \param vSrcText - the source of the message which needs Ack. * \return returns true if the message needs an Ack (Negative Sequence) */ bool MessageDispatcher::checkAcknowTransmit(const Message &vMessage, const QString &vSrcText) { bool ok = false; GuiActionType mActionId = vMessage.actionId; Sequence mSequence = vMessage.sequence; // UI shall acknowledge the messages is intended for UI. if ( ! needsAcknow(vMessage.can_id)) return ok; // false if (mSequence < 0) { ok = true; if ( ! gDisableAcknowLog ) { LOG_APPED(tr(" ,%1,Ack Req, Sq:%2, ID:%3").arg(vSrcText).arg(mSequence).arg(Format::toHexString(mActionId))); } #ifdef DEBUG_ACKBACK_HD_TO_UI qDebug() << tr(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HD AckReq : %1 %2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ").arg(mSequence).arg(vMessage.actionId); #endif // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ UI AckBak is immediately handled at the same place. QString dstText; Can_Id dstID = MessageInterpreter::identifyDestination(vMessage.can_id, &dstText); actionTransmit(GuiActionType::ID_AckMessageThatRequiresAck, {}, -mSequence, dstID); if ( ! gDisableAcknowLog ) { LOG_APPED_UI(tr("Ack Bak, Sq:%1, Dst:%2").arg(-mSequence).arg(dstText)); } #ifdef DEBUG_ACKBACK_HD_TO_UI qDebug() << tr(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ UI AckBak : %1 %2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ").arg(-mSequence).arg(vMessage.actionId); #endif } return ok; } /*! * \brief MessageDispatcher::interpretMessage * \details Calls the MessageInterpreter interpretMessage method * Regarding the Message Id and the sequence emit different signals * to handle the normal or acknowledge messages. * \param vMessage - The Message * \return false on error */ bool MessageDispatcher::interpretMessage(const Message &vMessage) { bool ok = false; QVariantList mData; QString srcText; MessageInterpreter::identifySource(vMessage.can_id, &srcText); if ( ! checkAcknowReceived(vMessage, srcText) ) { // check if the message was an acknowledge. checkAcknowTransmit(vMessage, srcText); // then if needs acknow send it immediately. } if ( _interpreter.interpretMessage( vMessage, mData ) ) { ok = true; emit didActionReceive(vMessage.actionId, mData); } _messageList[vMessage.can_id].removeLast(); return ok; } /*! * \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) { return _needsAcknow.contains(vActionId); } /*! * \brief MessageDispatcher::needsAcknow * \details List of the CAN channels which need Acknow * \param vCan_Id - The message CANBus id * \return true if needs an Acknow */ bool MessageDispatcher::needsAcknow(Can_Id vCan_Id) { bool ok = true; switch(vCan_Id) { // list if the channels UI shall not Ack case eChlid_TD_DD : case eChlid_DD_TD : case eChlid_DD_FP : case eChlid_FP_DD : case eDialin_TD : case eTD_Dialin : case eDialin_DD : case eDD_Dialin : case eDialin_FP : case eFP_Dialin : ok = false; break; default: break; } return ok; }