/*! * * Copyright (c) 2019-2019 Diality Inc. - All Rights Reserved. * copyright * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, * IN PART OR IN WHOLE, * WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * file messagepacker.cpp * date 12/9/2019 * author Behrouz NematiPour * */ #include "messagedispatcher.h" // Qt // Project #include "logger.h" #include "applicationcontroller.h" #include "frameinterface.h" using namespace Can; // Singleton SINGLETON_INIT(MessageDispatcher) /*! * \brief MessageDispatcher Constructor * \param parent */ MessageDispatcher::MessageDispatcher(QObject *parent) : QObject(parent) { } /*! * \brief Message Handler initializer */ void MessageDispatcher::init() { initConnections(); } /*! * \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 &))); } /*! * \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 vCanId - CANBUS channel of the frame * \param vPayload - Payload of the frame */ void MessageDispatcher::onFrameReceive(Can_Id vCanId, const QByteArray &vPayload) { // Append a message to the list if (_messageList[vCanId].isEmpty() || _messageList[vCanId].last().isComplete()) { _messageList[vCanId].append(Message()); } // build the message if (! _builder.buildMessage(vPayload, _messageList[vCanId].last(), vCanId)) { _messageList[vCanId].removeLast(); return; } Message mMessage = _messageList[vCanId].last(); // TODO : must be moved to a MessageModel class if (mMessage.isComplete()) { GuiActionType mActionId; QVariantList mData; if (_interpreter.interpretMessage(vCanId, mMessage, mActionId, mData)) { emit didActionReceive(mActionId, mData); } _messageList[vCanId].removeLast(); } } /*! * \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::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) { QByteArray mData; if (! _interpreter.interpretMessage(vActionId, vData, mData)) { LOG_ERROR(tr("Incorrect Message, can't be interpreted")); return; } FrameList frameList; if ( ! _builder.buildFrames(vActionId, mData, frameList) ) { LOG_ERROR(tr("Incorrect Message can't be built")); return; } for (const auto &frame : frameList) { emit didFrameTransmit(eChlid_UI, frame); } }