/*! * * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. * \copyright \n * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, \n * IN PART OR IN WHOLE, \n * WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. \n * * \file caninterface.cpp * \date 2019/09/30 * \author Behrouz NematiPour * */ #include "caninterface.h" // Qt #include // Project #include "messagehandler.h" // namespace using namespace Can; // Singleton SINGLETON_INIT(CanInterface) /*! * \brief Caninterface Constructor * \param parent object */ CanInterface::CanInterface(QObject *parent) : QObject(parent) {} /*! * \brief CanInterface Initialization * \details Initializes the CANBUS and check if can be connected * \return true if connected false otherwise */ bool CanInterface::init() { QString mError; _canDevice = QCanBus::instance()->createDevice(_canType, _canInterface, &mError); if (!_canDevice) { status(tr("Error: Device Creation"),mError); return false; } _numberFramesWritten = 0; initConnections(); if (!_canDevice->connectDevice()) { status(tr("Error: Connection")); delete _canDevice; _canDevice = nullptr; return false; } status(tr("Connected")); return true; } /*! * \brief CanInterface status * \details CanInterface status description * \return */ QString CanInterface::status() const { return _canStatus; } /*! * \brief CanInterface connection * \details Initializes the required signal/slot connection between this class and other objects\n * to be able to communicate. */ void CanInterface::initConnections() { if (_canDevice) { connect(_canDevice, SIGNAL( framesReceived()), this , SLOT (onFrameReceive ())); connect(_canDevice, SIGNAL( errorOccurred(QCanBusDevice::CanBusError)), this , SLOT (onError (QCanBusDevice::CanBusError))); } connect(_MessageHandler, SIGNAL(didFrameTransmit(QCanBusFrame)), this , SLOT( onFrameTransmit(QCanBusFrame))); } /*! * \brief CanInterface status * \details Sets the Can interface status description * \param vDescription Description about the Can Interface error * \param vError Qt Can Interface Error */ void CanInterface::status(const QString &vDescription, QString vError) { QString mError=""; if (_canDevice) { mError = _canDevice->errorString() + vError; } else { mError = vError; } _canStatus = tr("%1 '%2[%3]', %4") .arg(vDescription) .arg(_canType) .arg(_canInterface) .arg(mError) ; } /*! * \brief CanInterface send * \details send a frame over the CAN Bus * \param vFrame CAN message frame */ void CanInterface::transmit(const QCanBusFrame &vFrame) { if( !_canDevice ) return; _canDevice->writeFrame(vFrame); } /*! * \brief CanInterface quit * \details Quit the Can Interface by disconnecting the bus and device. */ void CanInterface::quit() { if (!_canDevice) return; _canDevice->disconnectDevice(); delete _canDevice; _canDevice = nullptr; status(tr("Disconnected")); } /*! * \brief frameFlags * \details Can message frame type as flags * \param frame Can message frame * \return Frame flag */ static QString frameFlags(const QCanBusFrame &frame) { QString result = QLatin1String(" --- "); if (frame.hasBitrateSwitch()) result[1] = QLatin1Char('B'); if (frame.hasErrorStateIndicator()) result[2] = QLatin1Char('E'); if (frame.hasLocalEcho()) result[3] = QLatin1Char('L'); return result; } /*! * \brief CanInterface onError * \details Can Bus error handler which sets the can status description * \param vError CanBus error */ void CanInterface::onError(QCanBusDevice::CanBusError vError) { switch (vError) { case QCanBusDevice::ReadError: case QCanBusDevice::WriteError: case QCanBusDevice::ConnectionError: case QCanBusDevice::ConfigurationError: case QCanBusDevice::UnknownError: _canStatus = _canDevice->errorString(); break; default: break; } emit didError(_canStatus); } /*! * \brief CanInterface onRead * \details CanBus read handler */ void CanInterface::onFrameReceive () { if (!_canDevice) return; while (_canDevice->framesAvailable()) { const QCanBusFrame frame = _canDevice->readFrame(); QString view; if (frame.frameType() == QCanBusFrame::ErrorFrame) { view = _canDevice->interpretErrorFrame(frame); } else { view = frame.toString(); } const QString time = QString::fromLatin1("%1.%2 ") .arg(frame.timeStamp().seconds(), 10, 10, QLatin1Char(' ')) .arg(frame.timeStamp().microSeconds() / 100, 4, 10, QLatin1Char('0')); const QString flags = frameFlags(frame); // qDebug() << time + flags + view; // TODO : Needs to be investigated on how the messages on canBus will be received. // may require to be moved at the end of the function, to not to be called for each message. emit didRead(frame); } } /*! * \brief CanInterface onActionPerform * \details sends a Can message frame of the can message of the performed action * This is a response from application UI to HD device * \param vFrame Can message frame */ void CanInterface::onFrameTransmit(const QCanBusFrame &vFrame) { // TODO : Process Frame transmit(vFrame); }