/*! * * 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 messagehandler.cpp * date 10/26/2019 * author Behrouz NematiPour * */ #include "messagehandler.h" // Qt #include #include // Project #include "maintimer.h" #include "messagedispatcher.h" #include "caninterface.h" #include "format.h" // namespace using namespace Can; // Singleton SINGLETON_INIT(MessageHandler) /*! * \brief MessageHandler Constructor * \param parent */ MessageHandler::MessageHandler(QObject *parent) : QObject(parent) { } /*! * \brief Message Handler initializer */ void MessageHandler::init() { initConnections(); } /*! * \brief MessageHandler connections definition * \details Initializes the required signal/slot connection between this class and other objects\n * to be able to communicate. */ void MessageHandler::initConnections() { // From GUI connect(_MessageDispatcher, SIGNAL(didFrameTransmit(Can_Id, const QByteArray &)), this , SLOT( onFrameTransmit(Can_Id, const QByteArray &))); // From CAN connect(_CanInterface , SIGNAL( didFrameReceive( const QCanBusFrame &)), this , SLOT( onFrameReceive( const QCanBusFrame &))); } /*! * \brief MessageHandler::transmitFrame \n * \details Prepares a frame to be transmitted \n * and emit signal didFrameTransmit with the frame as its argument \n * \param vFrameId - Channel id of the CANBUS frame \n * \param vData - The Data this frame is going to carry \n * \note This frame is created by MessageBuilder \n * and it can be one of the frames of a message \n * which has been chopped into frames. \n */ void MessageHandler::transmitFrame(Can_Id vFrameId, const QByteArray &vData) { QCanBusFrame mFrame; mFrame.setFrameId(vFrameId); if (vData.length() > Can::eLenCanFrame) { qDebug() << "Error :" << tr("Payload can't be larger than %1 bytes").arg(Can::eLenCanFrame); return; } mFrame.setPayload(vData); emit didFrameTransmit(mFrame); } /*! * \brief MessageHandler::checkChannel \n * \details Checks for the channel id of the received frame \n * which needs to be handled or ignored. \n * \param vFrameId - Channel id of the frame \n * \param vOK - will be set to true if the channel \n * is valid and a variable has been passed to \n * \return The Category if the channels from the UI \n * perspective \ref MessageHandler::ChannelGroup \n */ MessageHandler::ChannelGroup MessageHandler::checkChannel(quint32 vFrameId, bool *vOK) { bool ok = true; MessageHandler::ChannelGroup channelGroup = ChannelGroup::eChannel_Unknown; switch (vFrameId) { case eChlid_HD_DG : case eChlid_DG_HD : channelGroup = ChannelGroup::eChannel_Ignores; break; case eChlid_HD : case eChlid_HD_Alarm : case eChlid_DG_Alarm : case eChlid_HD_Sync : case eChlid_DG_Sync : //case eChlid_DG_UI : // has duplicate value as eChlid_DG_Alarm //case eChlid_UI_DG : // has duplicate value as eChlid_UI_Sync channelGroup = ChannelGroup::eChannel_Listens; break; case eChlid_UI_Alarm : case eChlid_UI_Sync : case eChlid_UI : channelGroup = ChannelGroup::eChannel_Outputs; break; default: ok = false; break; } if (vOK) *vOK = ok; return channelGroup; } /*! * \brief MessageHandler::onFrameTransmit \n * \details This the slot connected to the MessageDispatcher didFrameTransmit signal. \n * When a frame needs to be send to CANBUS, \n * this slot will call transmitFrame method to do the job. \n * \param vCan_ID - CANBUS Can Id target of the frame. \n * \param vData - The data which this frame will carry. \n */ void MessageHandler::onFrameTransmit(Can_Id vCan_ID, const QByteArray &vData) { transmitFrame(vCan_ID, vData); } /*! * \brief MessageHandler::onFrameReceive * \details This the slot connected to the CanInterface didFrameReceive signal. \n * When a frame received over the CANBUS, \n * this slot will be called to check the channel if should be listened to. \n * and will emit didFrameReceive if should be handled and ignored otherwise. \n * \param vFrame - The frame has to be sent */ void MessageHandler::onFrameReceive(const QCanBusFrame &vFrame) { bool ok = false; quint32 mFrameId = vFrame.frameId(); ChannelGroup channelGroup = checkChannel(mFrameId, &ok); if (!ok){ qDebug() << "ERROR :" << "Unexpected Channel"; qDebug() << Format::toHexString(mFrameId, false, 3) + " -- " + vFrame.payload().toHex(' '); return; } if ( channelGroup != ChannelGroup::eChannel_Listens) { return; } Can_Id mCanId = static_cast(mFrameId); emit didFrameReceive(mCanId, vFrame.payload()); }