/*! * * 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 #include // Project #include "CanMessage.h" // 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 public: CanInterface(QObject *parent = nullptr); void enableConsoleOut(bool vEnabled); void quitDevice(); QString status() const; public Q_SLOTS: bool init(); bool init(QThread &vThread); void quit(); Q_SIGNALS: /*! * \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 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 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); private: static QString frameFlags(const QCanBusFrame &vFrame); void consoleOut (const QCanBusFrame &vFrame, const QString &vFrameCount); void initConnections(); bool initDevice(); void initThread(QThread &vThread); void quitThread(); void status (const QString &vDescription, QString vError = ""); bool testDevice(); bool transmit (const QCanBusFrame &vFrame); FrameCount rxCount(); FrameCount txCount(); FrameCount erCount(); // constants const QString _canType = "socketcan"; const QString _canInterface = "can0"; const int _canBitRate = 250000; const bool _canFDKey = false; // member variables QSharedPointer _canDevice = nullptr; QString _canStatus = ""; bool _enableConsoleOut = false; // QThread *_thread = nullptr; bool _init = false; FrameCount _rxFrameCount = 0; FrameCount _txFrameCount = 0; FrameCount _erFrameCount = 0; private Q_SLOTS: void onFrameTransmit (const QCanBusFrame &vFrame); void onFrameReceive (); void onFrameError (QCanBusDevice::CanBusError vError); void onFrameWritten (qint64 vFramesCount); }; } // namespace Can