diff --git a/tools/CAN/CAN_interface.cpp b/tools/CAN/CAN_interface.cpp index 8ec6c46..4483bf7 100644 --- a/tools/CAN/CAN_interface.cpp +++ b/tools/CAN/CAN_interface.cpp @@ -1,9 +1,12 @@ #include "CAN_interface.hpp" +#include "spdlog/spdlog.h" +#include "magic_enum.hpp" CAN_Interface::CAN_Interface(QString dev, QObject *parent) - : QObject(parent), m_mid(0) + : QObject(parent) { if (!QCanBus::instance()->plugins().contains(QStringLiteral("socketcan"))) { + spdlog::critical("Runtime error: CAN Interface: socketcan plugin not found"); throw std::runtime_error("CAN Interface: socketcan plugin not found"); } QString errorString; @@ -11,9 +14,13 @@ CAN_Interface::CAN_Interface(QString dev, QObject *parent) if (!m_canDevice) { // Error handling goes here + spdlog::critical("Runtime error: CAN Interface: {}", errorString.toStdString()); throw std::runtime_error("CAN Interface: " + errorString.toStdString()); } + connect(m_canDevice, &QCanBusDevice::errorOccurred, + this, [=, this](QCanBusDevice::CanBusError error){processErrors(error);}); + m_canDevice->connectDevice(); connect(m_canDevice, &QCanBusDevice::framesReceived, this, [this] { for (const QCanBusFrame &frame : m_canDevice->readAllFrames()) @@ -33,3 +40,18 @@ void CAN_Interface::send_frame(const QCanBusFrame &frame) { m_canDevice->writeFrame(frame); } + +void CAN_Interface::processErrors(QCanBusDevice::CanBusError error) const +{ + switch (error) { + case QCanBusDevice::ReadError: + case QCanBusDevice::WriteError: + case QCanBusDevice::ConnectionError: + case QCanBusDevice::ConfigurationError: + case QCanBusDevice::UnknownError: + spdlog::critical("CAN Interface: {} {}", magic_enum::enum_name(error), m_canDevice->errorString().toStdString()); + break; + default: + break; + } +} diff --git a/tools/CAN/CAN_interface.hpp b/tools/CAN/CAN_interface.hpp index 1ed1674..e3db1dc 100644 --- a/tools/CAN/CAN_interface.hpp +++ b/tools/CAN/CAN_interface.hpp @@ -16,5 +16,6 @@ class CAN_Interface : public QObject void frame_received(const QCanBusFrame frame); private: + void processErrors(QCanBusDevice::CanBusError error) const; QCanBusDevice *m_canDevice; };