/*! * * Copyright (c) 2019-2020 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 frameinterface.cpp * \date 10/26/2019 * \author Behrouz NematiPour * */ #include "frameinterface.h" // Qt #include #include #include // Project #include "logger.h" #include "messagedispatcher.h" #include "caninterface.h" // namespace using namespace Can; /*! * \brief FrameInterface::FrameInterface * \details Constructor * \param parent - QObject parent owner object. * Qt handles the children destruction by their parent objects life-cycle. */ FrameInterface::FrameInterface(QObject *parent) : QObject(parent) { } /*! * \brief Message Handler initializer */ bool FrameInterface::init() { if ( _init ) return false; _init = true; initConnections(); startTimer(1, Qt::PreciseTimer); LOG_EVENT("UI," + tr("%1 Initialized").arg(metaObject()->className())); return true; } /*! * \brief FrameInterface::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 FrameInterface::init(QThread &vThread) { if ( ! init() ) return false; initThread(vThread); return true; } /*! * \brief FrameInterface::quit * \details quits the class * Calls quitThread */ void FrameInterface::quit() { // coco begin validated: Application termination is not correctly done in coco!!! // it has been tested and works perfectly fine in normal run. quitThread(); // validated } // coco end /*! * \brief FrameInterface connections definition * \details Initializes the required signal/slot connection between this class and other objects * to be able to communicate. */ void FrameInterface::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 &))); connect(&_CanInterface , SIGNAL( didFrameWritten(qint64 )), this , SLOT( onFrameWritten(qint64 ))); } /*! * \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 FrameInterface::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())); _thread->start(); moveToThread(_thread); } /*! * \brief FrameInterface::quitThread * \details Moves this object to main thread to be handled by QApplicaiton * And to be destroyed there. */ void FrameInterface::quitThread() { // coco begin validated: Application termination is not correctly done in coco!!! // it has been tested and works perfectly fine in normal run. if ( ! _thread ) return; // runs in thread moveToThread(qApp->thread()); // validated } // coco end /*! * \brief FrameInterface::transmitFrame * \details Prepares a frame to be transmitted * and emit signal didFrameTransmit with the frame as its argument * \param vFrameId - Channel id of the CANBUS frame * \param vData - The Data this frame is going to carry * \note This frame is created by MessageBuilder * and it can be one of the frames of a message * which has been chopped into frames. */ void FrameInterface::transmitFrame(Can_Id vCan_Id, const QByteArray &vData) { QCanBusFrame mFrame; mFrame.setFrameId(vCan_Id); if (vData.length() > Can::eLenCanFrame) { LOG_DEBUG(QString("Payload can't be larger than %1 bytes").arg(Can::eLenCanFrame)); return; } mFrame.setPayload(vData); emit didFrameTransmit(mFrame); } /*! * \brief FrameInterface::checkChannel * \details Checks for the channel id of the received frame * which needs to be handled or ignored. * \param vFrameId - Channel id of the frame * \param vOK - will be set to true if the channel * is valid and a variable has been passed to * \return The Category if the channels from the UI * perspective FrameInterface::ChannelGroup */ FrameInterface::ChannelGroup FrameInterface::checkChannel(quint32 vFrameId, bool *vOK) { bool ok = true; FrameInterface::ChannelGroup channelGroup = ChannelGroup::eChannel_Unknown; switch (vFrameId) { case eChlid_HD_DG : // coco begin validated: The HD/DG communication has not been defined and implemented yet. case eChlid_DG_HD : // coco end channelGroup = ChannelGroup::eChannel_Ignores; break; case eChlid_HD_UI : case eChlid_HD_Alarm : case eChlid_HD_Sync : // coco begin validated: The UI/DG communication has not been defined and implemented yet. case eChlid_DG_Alarm : case eChlid_DG_UI : case eChlid_DG_Sync : // coco end //case eChlid_DG_UI : // has duplicate value as eChlid_DG_Alarm channelGroup = ChannelGroup::eChannel_Listens; break; // coco begin validated: The UI Alarm and Sync messages has not been defined and implemented yet. case eChlid_UI_Alarm : case eChlid_UI_Sync : //case eChlid_UI_DG : channelGroup = ChannelGroup::eChannel_Outputs; break; // coco end default: ok = false; break; } // coco begin validated: manually tested if (vOK) *vOK = ok; // coco end return channelGroup; } /*! * \brief FrameInterface::onFrameReceive * \details This the slot connected to the CanInterface didFrameReceive signal. * When a frame received over the CANBUS, * this slot will be called to check the channel if should be listened to. * and will emit didFrameReceive if should be handled and ignored otherwise. * \param vFrame - The frame has to be sent */ void FrameInterface::onFrameReceive(const QCanBusFrame &vFrame) { bool ok = false; quint32 mFrameId = vFrame.frameId(); ChannelGroup channelGroup = checkChannel(mFrameId, &ok); if (!ok) { LOG_DEBUG("Unexpected Channel\r\n" + Format::toHexString(mFrameId, false, eLenChannelDigits) + " -- " + vFrame.payload().toHex(' ')); return; } if ( channelGroup != ChannelGroup::eChannel_Listens) { return; } Can_Id mCan_Id = static_cast(mFrameId); emit didFrameReceive(mCan_Id, vFrame.payload()); } /*! * \brief FrameInterface::onFrameTransmit * \details This the slot connected to the MessageDispatcher didFrameTransmit signal. * When a frame needs to be send to CANBUS, * this slot will call transmitFrame method to do the job. * \param vCan_Id - CANBUS Can Id target of the frame. * \param vData - The data which this frame will carry. */ void FrameInterface::onFrameTransmit(Can_Id vCan_Id, const QByteArray &vData) { appendHead(vCan_Id, vData); // Test : qDebug() << _timestamp << "apnd #" << _txFrameList.count(); } /*! * \brief FrameInterface::onFrameWritten * \param vCount */ void FrameInterface::onFrameWritten(qint64 /*vCount*/) { _transmitted = true; removeHead(); // Test : qDebug() << _timestamp << "Sent #" << _txFrameList.count() << vCount; } /*! * \brief FrameInterface::timerEvent * \details This event handler is reimplemented in this subclass to receive timer events for this object. */ void FrameInterface::timerEvent(QTimerEvent *) { static quint8 count = 0; // Test : _timestamp = QTime::currentTime().toString("HH:mm:ss.zzz"); if (++count != _interval) return; // Test : qDebug() << _timestamp; count = 0; _transmitted = false; trnsmtHead(); } /*! * \brief FrameInterface::trnsmtHead * \details Transmits the head of the trasmit buffer * Sends an empty frame with lowest prioriority if the transmit buffer is empty * to keep the UI board CANDriver awake. */ void FrameInterface::trnsmtHead() { if ( _txFrameList.isEmpty() ) { // coco begin validated: This is a fake data generator for CANBus missing/swapped frames Testing // will never be executed on the product and shall be removed after the CANBus issues has been resolved. // has been tested manually if ( gSendEmptyKeepAwake ) { transmitFrame(eChlid_LOWEST,QByteArray()); // Keep the CANBus awake. return; } } // coco end else { Frame frame = _txFrameList.first(); transmitFrame(frame.can_Id, frame.data); // Test : qDebug() << _timestamp << "Tsmt #" << _txFrameList.count(); } } /*! * \brief FrameInterface::removeHead * \details Removes the frame from the head of the transmit buffer * in case transmission has been confirmed by the CANBus driver * in the FrameInterface::onFrameWritten slot * which is connected to CanInterface::didFrameWritten. */ void FrameInterface::removeHead() { if ( _txFrameList.isEmpty() ) { return; } _txFrameList.removeFirst(); } /*! * \brief FrameInterface::appendHead * \details Appends the frame to the transmit buffer to be sent later * \param vCan_Id - the canbus id * \param vData - the data to be sent */ void FrameInterface::appendHead(Can_Id vCan_Id, const QByteArray &vData) { Frame frame = Frame(vCan_Id, vData); // coco begin validated: has been manually tested by sending over 4000 frames and not received by anyother node. if (_txFrameList.count() >= _txFrameList_Max) { LOG_DEBUG(QString("Transmit buffer overflow of %1").arg(_txFrameList_Max)); return; // coco end } _txFrameList.append(frame); }