Index: sources/canbus/caninterface.cpp =================================================================== diff -u -r805119c460b4a266d6401c8705f4427e7fbe270f -r44a85c96ab55e424866ec4cca0270aa218355f82 --- sources/canbus/caninterface.cpp (.../caninterface.cpp) (revision 805119c460b4a266d6401c8705f4427e7fbe270f) +++ sources/canbus/caninterface.cpp (.../caninterface.cpp) (revision 44a85c96ab55e424866ec4cca0270aa218355f82) @@ -1,32 +1,31 @@ -/*! - * - * 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 - * - */ +/*! + * + * 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 caninterface.cpp + * \author (last) Behrouz NematiPour + * \date (last) 07-May-2020 + * \author (original) Behrouz NematiPour + * \date (original) 28-Oct-2019 + * + */ #include "caninterface.h" // Qt #include #include +// stl +#include + // Project #include "logger.h" #include "messageglobals.h" #include "frameinterface.h" -// stl -#include - -#define FrameCount_MAX UINT64_MAX - // namespace using namespace Can; @@ -48,7 +47,9 @@ if ( _init ) return false; _init = true; + // coco begin validated: Manually tested since required to disable and enable the canbus if ( ! initDevice() ) return false; + // coco end if ( ! testDevice() ) return false; initConnections(); @@ -70,7 +71,9 @@ */ bool CanInterface::init(QThread &vThread) { + // coco begin validated: Manually tested since required to disable and enable the canbus if ( ! init() ) return false; + // coco end initThread(vThread); return true; } @@ -84,7 +87,7 @@ { // coco begin validated: Application termination is not correctly done in coco!!! // it has been tested and works perfectly fine in normal run. - quitThread(); + quitThread(); // verified } // coco end @@ -105,15 +108,17 @@ */ void CanInterface::initConnections() { + // coco begin validated: Manually tested since required to disable and enable the canbus if (_canDevice) { + // coco end connect(_canDevice, SIGNAL( framesReceived()), this , SLOT (onFrameReceive ())); connect(_canDevice, SIGNAL( errorOccurred(QCanBusDevice::CanBusError)), this , SLOT (onFrameError (QCanBusDevice::CanBusError))); connect(_canDevice, SIGNAL( framesWritten(qint64)), - this , SLOT (onFrameWrittern(qint64))); + this , SLOT (onFrameWritten (qint64))); } connect(&_FrameInterface, SIGNAL(didFrameTransmit(QCanBusFrame)), this , SLOT( onFrameTransmit(QCanBusFrame))); @@ -150,7 +155,7 @@ if (! _thread) return; // runs in thread - moveToThread(qApp->thread()); + moveToThread(qApp->thread()); // verified } // coco end @@ -163,11 +168,13 @@ { QString mError; _canDevice = QCanBus::instance()->createDevice(_canType, _canInterface, &mError); + // coco begin validated: Manually tested since required to disable and enable the canbus if (!_canDevice) { status(tr("Device Creation"), mError); LOG_ERROR(status()); return false; } + // coco end return true; } @@ -194,8 +201,9 @@ */ void CanInterface::quitDevice() { - if (!_canDevice) - return; + // coco begin validated: Manually tested since required to disable and enable the canbus + if (!_canDevice) return; + // coco end _canDevice->disconnectDevice(); delete _canDevice; _canDevice = nullptr; @@ -233,8 +241,9 @@ */ bool CanInterface::transmit(const QCanBusFrame &vFrame) { - if( !_canDevice ) - return false; + // coco begin validated: Manually tested since required to disable and enable the canbus + if( !_canDevice ) return false; + //coco end return _canDevice->writeFrame(vFrame); } @@ -246,6 +255,7 @@ */ void CanInterface::consoleOut(const QCanBusFrame &vFrame, const QString &vFrameCount) { + // coco begin validated: This code is only for debugging purposes and had been tested manually. if ( ! _enableConsoleOut ) return; LOG_EVENT_ONCE(QObject::tr("console out CanInterface Enabled")); @@ -263,19 +273,16 @@ } fprintf(stderr, "%s %s %s %i %s\n", vFrameCount.toLatin1().constData(), time.toLatin1().constData(), flags.toLatin1().constData(), vFrame.frameId(), view.toLatin1().constData()); } +// coco end /*! * \brief CanInterface::rxCount * \details count received frames up the size of the FrameCount type size * \return frame count */ -CanInterface::FrameCount CanInterface::rxCount() +FrameCount CanInterface::rxCount() { - if ( _rxFrameCount <= FrameCount_MAX ) { - ++_rxFrameCount; - } else { - _rxFrameCount = 1; - } + Types::safeIncrement(_rxFrameCount); return _rxFrameCount; } @@ -284,24 +291,36 @@ * \details count transmitted frames up the size of the FrameCount type size * \return frame count */ -CanInterface::FrameCount CanInterface::txCount() +FrameCount CanInterface::txCount() { - if ( _txFrameCount <= FrameCount_MAX ) { - ++_txFrameCount; - } else { - _txFrameCount = 1; - } + Types::safeIncrement(_txFrameCount); return _txFrameCount; } /*! + * \brief CanInterface::erCount + * \details count errors happened + * \return error count + */ +FrameCount CanInterface::erCount() +{ + // coco begin validated: CANBus error handling has been tested manually. + // since it requires massive can messages sent/received to catch the error + Types::safeIncrement(_erFrameCount); + return _erFrameCount; + // coco end +} + +/*! * \brief frameFlags * \details CANBUS message frame type as flags * \param vFrame - CANBUS message frame * \return Frame flag as QString */ QString CanInterface::frameFlags(const QCanBusFrame &vFrame) { + // coco begin validated: CANBus error handling has been tested manually. + // since it requires massive can messages sent/received to catch the error QString result = QLatin1String(" --- "); if (vFrame.hasBitrateSwitch()) @@ -312,6 +331,7 @@ result[3] = QLatin1Char('L'); return result; + // coco end } /*! @@ -321,32 +341,40 @@ */ void CanInterface::onFrameError(QCanBusDevice::CanBusError vError) { + // coco begin validated: CANBus error handling has been tested manually. + // since it requires massive can messages sent/received to catch the error + erCount(); switch (vError) { case QCanBusDevice::ReadError: case QCanBusDevice::WriteError: case QCanBusDevice::ConnectionError: case QCanBusDevice::ConfigurationError: case QCanBusDevice::UnknownError: _canStatus = _canDevice->errorString(); + LOG_ERROR(QString("%1 - %2").arg(_erFrameCount).arg(_canStatus)); break; + default: break; + } emit didFrameError(_canStatus); } +// coco end /*! - * \brief CanInterface::onFrameWrittern + * \brief CanInterface::onFrameWritten * \details This is the slot connected to the signal * which is emitted every time a payload of frames * has been written to the CANBUS bus. * \param vFramesCount - The framesCount argument is set to the number of frames * that were written in this payload. */ -void CanInterface::onFrameWrittern(qint64 /*vFramesCount*/) +void CanInterface::onFrameWritten(qint64 vFramesCount) { - //_txFrameCount = vFramesCount; - //qDebug() << "onFrameWrittern::FramesCount : " << vFramesCount; + static FrameCount mFrameCount = 0; + Types::safeIncrement(mFrameCount, vFramesCount); + emit didFrameWritten(vFramesCount); } /*! @@ -355,13 +383,16 @@ */ void CanInterface::onFrameReceive () { - if (!_canDevice) - return; + // coco begin validated: Manually tested since required to disable and enable the canbus + if (!_canDevice) return; + // coco end while (_canDevice->framesAvailable()) { const QCanBusFrame frame = _canDevice->readFrame(); rxCount(); + // coco begin validated: This code is only for debugging purposes and had been tested manually. if ( _enableConsoleOut ) consoleOut(frame, QString("Rx:%1").arg(_rxFrameCount)); + // coco end emit didFrameReceive(frame); } } @@ -376,7 +407,9 @@ { bool ok = transmit(vFrame); txCount(); + // coco begin validated: This code is only for debugging purposes and had been tested manually. if ( _enableConsoleOut ) consoleOut(vFrame, QString("Tx:%1").arg(_txFrameCount)); + // coco end emit didFrameTransmit(ok); }