/*! * * Copyright (c) 2020-2024 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 ProtoInterface.cpp * \author (last) Behrouz NematiPour * \date (last) 18-Jul-2023 * \author (original) Behrouz NematiPour * \date (original) 26-Aug-2020 * */ #include "LeahiMsgDefs.h" #include "ProtoInterface.h" // Qt #include #include #include namespace proto { static void qRegister() { qRegisterMetaType("Can::Message"); } Q_COREAPP_STARTUP_FUNCTION(qRegister) /*! * \brief ProtoInterface::ProtoInterface * \details Constructor * \param parent - QObject parent owner object. * Qt handles the children destruction by their parent objects life-cycle. */ ProtoInterface::ProtoInterface(QObject *parent) : QObject(parent) { } /*! * \brief ProtoInterface::deleteDevice * \details Disconnect the CANBus device and deletes the pointer */ void ProtoInterface::quitDevice() { } /*! * \brief ProtoInterface Initialization * \details Initializes the CANBus and checks if can be connected * \return true if connected, false otherwise */ bool ProtoInterface::init() { if (_init) { return false; } _init = true; initConnections(); return true; } /*! * \brief ProtoInterface::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 ProtoInterface::init(QThread &vThread) { if (! init()) { return false; } initThread(vThread); return true; } void ProtoInterface::onCanMessageReceive(const QDateTime timestamp, const Can::Message msg) { msg.dump(); QByteArray proto_buffer = leahi::canMessageToProtobufByteArray(timestamp, QStringLiteral("test_device"), msg); QString proto_string; for (int i = 0; i < proto_buffer.size(); i++) { proto_string.append(QString(" 0x%1").arg(QString("%1").arg(quint8(proto_buffer[i]), 2, 16, QChar('0')).toUpper())); } qDebug().noquote() << QString("protobuf(%1) =%2\n").arg(proto_buffer.length(), 0, 10, QChar('0')).arg(proto_string); } /*! * \brief ProtoInterface quit * \details quits the class * Calls quitThread */ void ProtoInterface::quit() { quitThread(); // verified } /*! * \brief ProtoInterface connections definition * \details Initializes the required signal/slot connection between this class and other objects * to be able to communicate. */ void ProtoInterface::initConnections() { } /*! * \brief ProtoInterface::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 ProtoInterface::initThread(QThread &vThread) { // runs in main thread Q_ASSERT_X(QThread::currentThread() == qApp->thread() , __func__, "The Class initialization must be done in Main Thread" ); vThread.setObjectName(QString("%1_Thread").arg(metaObject()->className())); connect(qApp, &QCoreApplication::aboutToQuit, this, &ProtoInterface::quit); moveToThread(&vThread); vThread.start(); } /*! * \brief ProtoInterface::quitThread * \details Moves this object to main thread to be handled by QApplication * And to be destroyed there. */ void ProtoInterface::quitThread() { // runs in thread if (QThread::currentThread() == qApp->thread()) { moveToThread(qApp->thread()); // verified } } } // namespace proto