Index: sources/canbus/caninterface.cpp =================================================================== diff -u -re1605219ac2baf49ef21d0889f845ac53d59c3c1 -r56d00a82669a7a2c00ab90109a89dbec8db27527 --- sources/canbus/caninterface.cpp (.../caninterface.cpp) (revision e1605219ac2baf49ef21d0889f845ac53d59c3c1) +++ sources/canbus/caninterface.cpp (.../caninterface.cpp) (revision 56d00a82669a7a2c00ab90109a89dbec8db27527) @@ -14,13 +14,13 @@ #include "caninterface.h" // Qt -#include +#include #include // Project #include "logger.h" -#include "frameinterface.h" #include "messageglobals.h" +#include "frameinterface.h" // namespace using namespace Can; @@ -38,44 +38,45 @@ */ bool CanInterface::init() { + if ( _init ) return false; + _init = true; - // This is required for Signal/Slots in threading. - qRegisterMetaType("QCanBusFrame"); + if ( ! initDevice() ) return false; + if ( ! testDevice() ) return false; - // runs in main thread - Q_ASSERT_X(QThread::currentThread() == qApp->thread() , "_CanInterface::init", "The Class initialization must be done in Main Thread" ); - _CanFrame_Thread.setObjectName("CAN Frame Thread"); - connect(qApp, &QApplication::aboutToQuit, this, &CanInterface::quit); - _CanFrame_Thread.start(); - moveToThread(&_CanFrame_Thread); - - if ( ! createDevice() ) return false; - if ( ! checkDevice () ) return false; - _numberFramesWritten = 0; initConnections(); status(tr("Connected")); - LOG_EVENT(QObject::tr("CanInterface Initialized")); LOG_EVENT(status()); + LOG_EVENT(QObject::tr("%1 Initialized").arg(metaObject()->className())); + return true; } /*! + * \brief CanInterface::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 CanInterface::init(QThread &vThread) +{ + if ( ! init() ) return false; + initThread(vThread); + return true; +} + +/*! * \brief CanInterface quit - * \details Quit the CANBUS Interface by disconnecting the bus and device. + * \details quits the class + * Calls quitThread */ void CanInterface::quit() { - deleteDevice(); - - // runs in Logger thread - moveToThread(qApp->thread()); - - // runs in main thread - _CanFrame_Thread.quit(); - _CanFrame_Thread.wait(); - + quitThread(); } /*! @@ -104,18 +105,48 @@ connect(_canDevice, SIGNAL( framesWritten(qint64)), this , SLOT (onFrameWrittern(qint64))); - } connect(&_FrameInterface, SIGNAL(didFrameTransmit(QCanBusFrame)), this , SLOT( onFrameTransmit(QCanBusFrame))); } /*! + * \brief CanInterface::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 CanInterface::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 CanInterface::quitThread + * \details Moves this object to main thread to be handled by QApplicaiton + * And to be destroyed there. + */ +void CanInterface::quitThread() +{ + if (! _thread) return; + + // runs in thread + moveToThread(qApp->thread()); +} + +/*! * \brief CanInterface::createDevice * \details Creates the CANBus device * \return false if can't create the device */ -bool CanInterface::createDevice() +bool CanInterface::initDevice() { QString mError; _canDevice = QCanBus::instance()->createDevice(_canType, _canInterface, &mError); @@ -132,7 +163,7 @@ * \details Checks if the device has been connected. * \return */ -bool CanInterface::checkDevice() +bool CanInterface::testDevice() { if (!_canDevice->connectDevice()) { status(tr("Connection")); @@ -148,7 +179,7 @@ * \brief CanInterface::deleteDevice * \details Disconnect the CANBus device and deletes the pointer */ -void CanInterface::deleteDevice() +void CanInterface::quitDevice() { if (!_canDevice) return;