From d869b6a126ece3d77d14d195bc31c61523509777 Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Sun, 20 Aug 2017 21:34:05 -0400 Subject: [PATCH] Switch ISOTP and UDS handlers to no longer be static. Now each can be instantiated multiple times with each instance handling a different ISOTP or UDS stream. This should streamline some upcoming code --- bus_protocols/isotp_handler.cpp | 20 ++++++---------- bus_protocols/isotp_handler.h | 4 +--- bus_protocols/uds_handler.cpp | 41 +++++++++++++++------------------ bus_protocols/uds_handler.h | 5 +++- re/isotp_interpreterwindow.cpp | 2 +- re/udsscanwindow.cpp | 25 +++++++++++--------- re/udsscanwindow.h | 1 + 7 files changed, 46 insertions(+), 52 deletions(-) diff --git a/bus_protocols/isotp_handler.cpp b/bus_protocols/isotp_handler.cpp index 18d537c..db47747 100644 --- a/bus_protocols/isotp_handler.cpp +++ b/bus_protocols/isotp_handler.cpp @@ -1,19 +1,6 @@ #include "isotp_handler.h" #include "connections/canconmanager.h" -ISOTP_HANDLER* ISOTP_HANDLER::mInstance = NULL; - -ISOTP_HANDLER* ISOTP_HANDLER::getInstance() -{ - if(!mInstance) - { - mInstance = new ISOTP_HANDLER(); - mInstance->modelFrames = MainWindow::getReference()->getCANFrameModel()->getListReference(); - } - - return mInstance; -} - ISOTP_HANDLER::ISOTP_HANDLER() { useExtendedAddressing = false; @@ -23,9 +10,16 @@ ISOTP_HANDLER::ISOTP_HANDLER() lastSenderBus = 0; lastSenderID = 0; + modelFrames = MainWindow::getReference()->getCANFrameModel()->getListReference(); + connect(&frameTimer, SIGNAL(timeout()), this, SLOT(frameTimerTick())); } +ISOTP_HANDLER::~ISOTP_HANDLER() +{ + disconnect(&frameTimer, SIGNAL(timeout()), this, SLOT(frameTimerTick())); +} + void ISOTP_HANDLER::setExtendedAddressing(bool mode) { useExtendedAddressing = mode; diff --git a/bus_protocols/isotp_handler.h b/bus_protocols/isotp_handler.h index bda09ea..c8a3868 100644 --- a/bus_protocols/isotp_handler.h +++ b/bus_protocols/isotp_handler.h @@ -16,8 +16,8 @@ class ISOTP_HANDLER : public QObject public: ISOTP_HANDLER(); + ~ISOTP_HANDLER(); void setExtendedAddressing(bool mode); - static ISOTP_HANDLER* getInstance(); void setReception(bool mode); //set whether to accept and forward frames or not void sendISOTPFrame(int bus, int ID, QVector data); void setProcessAll(bool state); @@ -51,8 +51,6 @@ private: void processFrame(const CANFrame &frame); void checkNeedFlush(uint64_t ID); - - static ISOTP_HANDLER* mInstance; }; #endif // ISOTP_HANDLER_H diff --git a/bus_protocols/uds_handler.cpp b/bus_protocols/uds_handler.cpp index 22dedfb..01f514b 100644 --- a/bus_protocols/uds_handler.cpp +++ b/bus_protocols/uds_handler.cpp @@ -1,6 +1,7 @@ #include "uds_handler.h" #include "connections/canconmanager.h" #include "mainwindow.h" +#include "isotp_handler.h" #include QVector UDS_SERVICE_DESC = { @@ -131,23 +132,17 @@ UDS_MESSAGE::UDS_MESSAGE() isErrorReply = false; } -UDS_HANDLER* UDS_HANDLER::mInstance = NULL; - -UDS_HANDLER* UDS_HANDLER::getInstance() -{ - if(!mInstance) - { - mInstance = new UDS_HANDLER(); - mInstance->modelFrames = MainWindow::getReference()->getCANFrameModel()->getListReference(); - } - - return mInstance; -} - UDS_HANDLER::UDS_HANDLER() { isReceiving = false; useExtendedAddressing = false; + modelFrames = MainWindow::getReference()->getCANFrameModel()->getListReference(); + isoHandler = new ISOTP_HANDLER(); +} + +UDS_HANDLER::~UDS_HANDLER() +{ + delete isoHandler; } void UDS_HANDLER::gotISOTPFrame(ISOTP_MESSAGE msg) @@ -189,7 +184,7 @@ void UDS_HANDLER::gotISOTPFrame(ISOTP_MESSAGE msg) void UDS_HANDLER::setFlowCtrl(bool state) { - ISOTP_HANDLER::getInstance()->setFlowCtrl(state); + isoHandler->setFlowCtrl(state); } void UDS_HANDLER::setReception(bool mode) @@ -200,14 +195,14 @@ void UDS_HANDLER::setReception(bool mode) if (isReceiving) { - connect(ISOTP_HANDLER::getInstance(), SIGNAL(newISOMessage(ISOTP_MESSAGE)), this, SLOT(gotISOTPFrame(ISOTP_MESSAGE))); - ISOTP_HANDLER::getInstance()->setReception(true); //must enable ISOTP reception too. + connect(isoHandler, SIGNAL(newISOMessage(ISOTP_MESSAGE)), this, SLOT(gotISOTPFrame(ISOTP_MESSAGE))); + isoHandler->setReception(true); //must enable ISOTP reception too. qDebug() << "Enabling reception of ISO-TP frames in UDS handler"; } else { - disconnect(ISOTP_HANDLER::getInstance(), SIGNAL(newISOMessage(ISOTP_MESSAGE)), this, SLOT(gotISOTPFrame(ISOTP_MESSAGE))); - //can't disable ISOTP reception because something else might be using it. + disconnect(isoHandler, SIGNAL(newISOMessage(ISOTP_MESSAGE)), this, SLOT(gotISOTPFrame(ISOTP_MESSAGE))); + isoHandler->setReception(false); qDebug() << "Disabling reception of ISOTP frames in UDS handler"; } } @@ -226,7 +221,7 @@ void UDS_HANDLER::sendUDSFrame(const UDS_MESSAGE &msg) } data.append(msg.data); - ISOTP_HANDLER::getInstance()->sendISOTPFrame(msg.bus, msg.ID, data); + isoHandler->sendISOTPFrame(msg.bus, msg.ID, data); qDebug() << "Sent UDS service: " << getServiceShortDesc(msg.service) << " on bus " << msg.bus; } @@ -272,22 +267,22 @@ QString UDS_HANDLER::getNegativeResponseLong(int respCode) //Little shim functions that drop straight through to the ISO_TP handler void UDS_HANDLER::setProcessAllIDs(bool state) { - ISOTP_HANDLER::getInstance()->setProcessAll(state); + isoHandler->setProcessAll(state); } void UDS_HANDLER::addID(uint32_t id) { - ISOTP_HANDLER::getInstance()->addID(id); + isoHandler->addID(id); } void UDS_HANDLER::removeID(uint32_t id) { - ISOTP_HANDLER::getInstance()->removeID(id); + isoHandler->removeID(id); } void UDS_HANDLER::clearAllIDs() { - ISOTP_HANDLER::getInstance()->clearAllIDs(); + isoHandler->clearAllIDs(); } diff --git a/bus_protocols/uds_handler.h b/bus_protocols/uds_handler.h index d655b48..7436b6e 100644 --- a/bus_protocols/uds_handler.h +++ b/bus_protocols/uds_handler.h @@ -7,6 +7,8 @@ #include "can_structs.h" #include "isotp_message.h" +class ISOTP_HANDLER; + namespace UDS_SERVICES { enum @@ -85,6 +87,7 @@ class UDS_HANDLER : public QObject public: UDS_HANDLER(); + ~UDS_HANDLER(); void setExtendedAddressing(bool mode); static UDS_HANDLER* getInstance(); void setReception(bool mode); //set whether to accept and forward frames or not @@ -113,7 +116,7 @@ private: void processFrame(const CANFrame &frame); - static UDS_HANDLER* mInstance; + ISOTP_HANDLER *isoHandler; }; diff --git a/re/isotp_interpreterwindow.cpp b/re/isotp_interpreterwindow.cpp index 3a1bc45..79f4622 100644 --- a/re/isotp_interpreterwindow.cpp +++ b/re/isotp_interpreterwindow.cpp @@ -9,7 +9,7 @@ ISOTP_InterpreterWindow::ISOTP_InterpreterWindow(const QVector *frames ui->setupUi(this); modelFrames = frames; - decoder = ISOTP_HANDLER::getInstance(); + decoder = new ISOTP_HANDLER; decoder->setReception(true); decoder->setProcessAll(true); diff --git a/re/udsscanwindow.cpp b/re/udsscanwindow.cpp index 62d7792..e880421 100644 --- a/re/udsscanwindow.cpp +++ b/re/udsscanwindow.cpp @@ -18,8 +18,10 @@ UDSScanWindow::UDSScanWindow(const QVector *frames, QWidget *parent) : waitTimer = new QTimer; waitTimer->setInterval(100); + udsHandler = new UDS_HANDLER; + connect(MainWindow::getReference(), SIGNAL(framesUpdated(int)), this, SLOT(updatedFrames(int))); - connect(UDS_HANDLER::getInstance(), &UDS_HANDLER::newUDSMessage, this, &UDSScanWindow::gotUDSReply); + connect(udsHandler, &UDS_HANDLER::newUDSMessage, this, &UDSScanWindow::gotUDSReply); connect(ui->btnScan, &QPushButton::clicked, this, &UDSScanWindow::scanUDS); connect(waitTimer, &QTimer::timeout, this, &UDSScanWindow::timeOut); connect(ui->btnSaveResults, &QPushButton::clicked, this, &UDSScanWindow::saveResults); @@ -44,6 +46,7 @@ UDSScanWindow::~UDSScanWindow() delete ui; waitTimer->stop(); delete waitTimer; + delete udsHandler; } void UDSScanWindow::adaptiveToggled() @@ -184,17 +187,17 @@ void UDSScanWindow::scanUDS() { waitTimer->stop(); sendingFrames.clear(); - UDS_HANDLER::getInstance()->setReception(false); - UDS_HANDLER::getInstance()->setProcessAllIDs(false); - UDS_HANDLER::getInstance()->setFlowCtrl(false); + udsHandler->setReception(false); + udsHandler->setProcessAllIDs(false); + udsHandler->setFlowCtrl(false); currentlyRunning = false; ui->btnScan->setText("Start Scan"); return; } - UDS_HANDLER::getInstance()->setReception(true); - UDS_HANDLER::getInstance()->setProcessAllIDs(true); - UDS_HANDLER::getInstance()->setFlowCtrl(true); + udsHandler->setReception(true); + udsHandler->setProcessAllIDs(true); + udsHandler->setFlowCtrl(true); waitTimer->setInterval(ui->spinDelay->value()); @@ -342,7 +345,7 @@ void UDSScanWindow::gotUDSReply(UDS_MESSAGE msg) if ((id == (uint32_t)(sentFrame.ID + offset)) || ui->cbAllowAdaptiveOffset->isChecked()) { - serviceShortName = UDS_HANDLER::getInstance()->getServiceShortDesc(sentFrame.service); + serviceShortName = udsHandler->getServiceShortDesc(sentFrame.service); if (serviceShortName.length() < 3) serviceShortName = QString::number(sentFrame.service, 16); if (msg.service == 0x40 + sendingFrames[currIdx].service) { @@ -368,7 +371,7 @@ void UDSScanWindow::gotUDSReply(UDS_MESSAGE msg) setupNodes(); QTreeWidgetItem *nodeNegative = new QTreeWidgetItem(); qDebug() << ui->spinNumBytes->value(); - nodeNegative->setText(0, "NEGATIVE - " + UDS_HANDLER::getInstance()->getNegativeResponseShort(msg.data[0])); + nodeNegative->setText(0, "NEGATIVE - " + udsHandler->getNegativeResponseShort(msg.data[0])); nodeNegative->setForeground(0, QBrush(Qt::darkRed)); nodeSubFunc->addChild(nodeNegative); nodeSubFunc->setForeground(0, QBrush(Qt::darkRed)); @@ -385,7 +388,7 @@ void UDSScanWindow::gotUDSReply(UDS_MESSAGE msg) void UDSScanWindow::setupNodes() { - QString serviceShortName = UDS_HANDLER::getInstance()->getServiceShortDesc(sendingFrames[currIdx].service); + QString serviceShortName = udsHandler->getServiceShortDesc(sendingFrames[currIdx].service); if (serviceShortName.length() < 3) serviceShortName = QString::number(sendingFrames[currIdx].service, 16); if (!nodeID || nodeID->text(0) != Utility::formatHexNum(sendingFrames[currIdx].ID)) @@ -427,7 +430,7 @@ void UDSScanWindow::sendNextMsg() currIdx++; if (currIdx < sendingFrames.count()) { - UDS_HANDLER::getInstance()->sendUDSFrame(sendingFrames[currIdx]); + udsHandler->sendUDSFrame(sendingFrames[currIdx]); waitTimer->start(); } else diff --git a/re/udsscanwindow.h b/re/udsscanwindow.h index b89498f..35629a6 100644 --- a/re/udsscanwindow.h +++ b/re/udsscanwindow.h @@ -38,6 +38,7 @@ private slots: private: Ui::UDSScanWindow *ui; const QVector *modelFrames; + UDS_HANDLER *udsHandler; QTimer *waitTimer; QList sendingFrames; QTreeWidgetItem *nodeID;