/*! * * 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 CanInterface.h * \author (last) Behrouz NematiPour * \date (last) 18-Jul-2023 * \author (original) Behrouz NematiPour * \date (original) 26-Aug-2020 * */ #pragma once // Qt #include #include // Project #include "main.h" // Doxygen : do not remove #include "MessageGlobalsAutoGen.h" // Define #define _CanInterface Can::CanInterface::I() // forward declarations class tst_canbus; class tst_acknow; // namespace namespace Can { /*! * \brief CANBus interface * \details This class contains the interface to CANBus * And utilizes Qt QCanBus to interact with the CANBus * This class works only with the QCanBusFrame frames. * On the OS side there is a driver installed to convert SPI to CAN * Since the GUI Board by itself does not contain the CANBus. * Application would know nothing about the SPI-TO-CAN */ class CanInterface : public QObject { Q_OBJECT // Singleton SINGLETON(CanInterface) // friends friend class ::tst_canbus; friend class ::tst_acknow; // constants const char *_canType = "socketcan"; QString _canInterface = gActiveCANBus; // can0 by default, can be altered by -A(--active-can-bus) const int _canBitRate = 250000; const bool _canFDKey = false; // member variables QCanBusDevice *_canDevice = nullptr; QString _canStatus = ""; bool _enableConsoleOut = false; QThread *_thread = nullptr; bool _init = false; FrameCount _rxFrameCount = 0; FrameCount _txFrameCount = 0; FrameCount _erFrameCount = 0; public slots: bool init(); bool init(QThread &vThread); void quit(); public: QString status() const; void enableConsoleOut(bool vEnabled); void quitDevice(); private: void initConnections(); void initThread(QThread &vThread); void quitThread(); bool initDevice(); bool testDevice(); void status (const QString &vDescription, QString vError = ""); bool transmit (const QCanBusFrame &vFrame); void consoleOut (const QCanBusFrame &vFrame, const QString &vFrameCount); FrameCount rxCount(); FrameCount txCount(); FrameCount erCount(); static QString frameFlags(const QCanBusFrame &vFrame); signals: /*! * \brief didFrameReceive * \details This signal will be emitted when a frame has been received * \param vFrame - The Frame which has been received */ void didFrameReceive (const QCanBusFrame &vFrame ); /*! * \brief didFrameError * \details If and error occurs on CanDevice after the error is processed * this signal can be used as a notifier. * \param vStatus - CanDevice status with some extra information. */ void didFrameError (const QString &vStatus); /*! * \brief didFrameTransmit * \details After the frame has been transmitted this signal can be used as a notifier. * \param ok - is true if the frame has been transmitted successfully */ void didFrameTransmit(bool ok); /*! * \brief didFrameWritten * \details After the frame transmission is done * and acknowledged by a node on the CANBus, * this signal is emitted. * \param vCount is the number of frame which has been written */ void didFrameWritten(qint64 vCount); public slots: private slots: void onFrameTransmit (const QCanBusFrame &vFrame); void onFrameReceive (); void onFrameError (QCanBusDevice::CanBusError vError); void onFrameWritten (qint64 vFramesCount); }; }