LCOV - code coverage report
Current view: top level - canbus - CanBusManager.cpp (source / functions) Hit Total Coverage
Test: filtered.info Lines: 29 36 80.6 %
Date: 2025-07-25 11:48:17 Functions: 5 6 83.3 %

          Line data    Source code
       1             : /*!
       2             :  * @file CanBusManager.cpp
       3             :  * @brief Implementation of the CanBusManager class.
       4             :  * @version 0.1
       5             :  * @date 2025-01-29
       6             :  * @author Félix LE BIHAN (@Fle-bihh)
       7             :  * @author Tiago Pereira (@t-pereira06)
       8             :  * @author Ricardo Melo (@reomelo)
       9             :  * @author Michel Batista (@MicchelFAB)
      10             :  *
      11             :  * @details This file contains the implementation of the CanBusManager class,
      12             :  * which manages the CAN bus communication.
      13             :  *
      14             :  * @note This class uses the MCP2515Controller for CAN bus communication.
      15             :  *
      16             :  * @warning Ensure that the MCP2515 controller is properly implemented.
      17             :  *
      18             :  * @see CanBusManager.hpp for the class definition.
      19             :  *
      20             :  * @copyright Copyright (c) 2025
      21             :  */
      22             : 
      23             : #include "CanBusManager.hpp"
      24             : #include <QDebug>
      25             : #include "MCP2515Controller.hpp"
      26             : 
      27             : /*!
      28             :  * @brief Construct a new CanBusManager::CanBusManager object
      29             :  *
      30             :  * @param spi_device The SPI device to use for communication.
      31             :  * @param parent The parent QObject.
      32             :  *
      33             :  * @details This constructor initializes the CanBusManager with a specified SPI
      34             :  * device and sets up the MCP2515 controller.
      35             :  */
      36           0 : CanBusManager::CanBusManager(const std::string &spi_device, QObject *parent)
      37           0 :         : QObject(parent)
      38             : {
      39           0 :         m_controller = new MCP2515Controller(spi_device);
      40           0 :         ownsMCP2515Controller = true;
      41           0 :         connectSignals();
      42           0 : }
      43             : 
      44             : /*!
      45             :  * @brief Construct a new CanBusManager::CanBusManager object
      46             :  *
      47             :  * @param controller The MCP2515 controller to use.
      48             :  * @param parent The parent QObject.
      49             :  *
      50             :  * @details This constructor initializes the CanBusManager with an existing
      51             :  * MCP2515 controller.
      52             :  */
      53           5 : CanBusManager::CanBusManager(IMCP2515Controller *controller, QObject *parent)
      54             :         : QObject(parent)
      55           5 :         , m_controller(controller)
      56             : {
      57           5 :         ownsMCP2515Controller = false;
      58           5 :         connectSignals();
      59           5 : }
      60             : 
      61             : /*!
      62             :  * @brief Destroy the CanBusManager::CanBusManager object
      63             :  *
      64             :  * @details Cleans up the resources used by the CanBusManager, including
      65             :  * stopping the reading thread and deleting the controller if owned.
      66             :  */
      67          10 : CanBusManager::~CanBusManager()
      68             : {
      69           5 :         if (m_thread) {
      70           2 :                 m_controller->stopReading();
      71           2 :                 m_thread->disconnect();
      72           2 :                 m_thread->quit();
      73           2 :                 m_thread->wait();
      74             : 
      75           2 :                 delete m_thread;
      76           2 :                 m_thread = nullptr;
      77             :         }
      78             : 
      79           5 :         if (ownsMCP2515Controller) {
      80           0 :                 delete m_controller;
      81             :         }
      82          10 : }
      83             : 
      84             : /*!
      85             :  * @brief Connects the signals from the MCP2515 controller to the CanBusManager
      86             :  * slots.
      87             :  *
      88             :  * @details This method sets up the connections between the signals emitted by
      89             :  * the MCP2515 controller and the corresponding slots in the CanBusManager.
      90             :  */
      91           5 : void CanBusManager::connectSignals()
      92             : {
      93           5 :         connect(m_controller, &IMCP2515Controller::speedUpdated, this, &CanBusManager::speedUpdated);
      94           5 :         connect(m_controller, &IMCP2515Controller::rpmUpdated, this, &CanBusManager::rpmUpdated);
      95           5 : }
      96             : 
      97             : /*!
      98             :  * @brief Initializes the CanBusManager.
      99             :  *
     100             :  * @details Initializes the MCP2515 controller and starts the reading thread.
     101             :  *
     102             :  * @returns true if initialization is successful, false otherwise.
     103             :  */
     104           3 : bool CanBusManager::initialize()
     105             : {
     106           3 :         if (!m_controller->init()) {
     107           1 :                 return false;
     108             :         }
     109             : 
     110           2 :         m_thread = new QThread(this);
     111           2 :         m_controller->moveToThread(m_thread);
     112             : 
     113           2 :         connect(m_thread, &QThread::started, m_controller, &IMCP2515Controller::processReading);
     114           2 :         connect(m_thread, &QThread::finished, m_controller, &QObject::deleteLater);
     115           2 :         connect(m_thread, &QThread::finished, m_thread, &QObject::deleteLater);
     116             : 
     117           2 :         m_thread->start();
     118           2 :         return true;
     119             : }

Generated by: LCOV version 1.14