/*! * * 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.h * \date 2019/09/30 * \author Behrouz NematiPour * */ #pragma once // Qt #include #include // Project #include "main.h" #include "messageglobals.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 doesn't contain the CAN Bus. * Application would know nothing about the SPI-TO-CAN */ class CanInterface : public QObject { Q_OBJECT // friends friend class ::tst_canbus; friend class ::tst_acknow; // constants const char *_canType = "socketcan"; QString _canInterface = "can0"; const int _canBitRate = 250000; // 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; // Singleton SINGLETON(CanInterface) public slots: bool init(); bool init(QThread &vThread); private slots: void quit(); public: QString status() const; void enableConsoleOut(bool vEnabled) { _enableConsoleOut = 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 emmited. * \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); }; }