From 2758fa8a0915fb3f857b757d34c091b3fb7a1fb8 Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Fri, 30 Dec 2016 14:24:17 -0500 Subject: [PATCH] Removed a few more warnings, switched the way CAN callbacks work. --- SavvyCAN.pro | 1 - can_structs.h | 12 +++++++- connections/canconmanager.cpp | 38 ++++++++---------------- connections/canconmanager.h | 16 ++++++---- connections/canconnection.cpp | 55 +++++++++++++++++++++++++---------- connections/canconnection.h | 19 +++++++++--- firmwareuploaderwindow.cpp | 7 ++--- framefileio.cpp | 6 ++-- re/flowviewwindow.cpp | 4 +-- scriptcontainer.cpp | 4 +-- 10 files changed, 96 insertions(+), 66 deletions(-) diff --git a/SavvyCAN.pro b/SavvyCAN.pro index f293ad7..784936a 100644 --- a/SavvyCAN.pro +++ b/SavvyCAN.pro @@ -107,7 +107,6 @@ HEADERS += mainwindow.h \ connections/connectionwindow.h \ re/graphingwindow.h \ re/newgraphdialog.h \ - ui/bisectwindow.h \ bisectwindow.h FORMS += ui/candatagrid.ui \ diff --git a/can_structs.h b/can_structs.h index aaba0b2..40428b5 100644 --- a/can_structs.h +++ b/can_structs.h @@ -1,6 +1,7 @@ #ifndef CAN_STRUCTS_H #define CAN_STRUCTS_H +#include #include #include @@ -16,10 +17,19 @@ public: uint64_t timestamp; }; -struct CANFlt +class CANFlt { +public: quint32 id; quint32 mask; + QObject * observer; //used to target the specific object that setup this filter + + bool operator ==(const CANFlt &b) const + { + if ( (id == b.id) && (mask == b.mask) && (observer == b.observer) ) return true; + + return false; + } }; struct J1939ID diff --git a/connections/canconmanager.cpp b/connections/canconmanager.cpp index 9bdf220..5cc5a1f 100644 --- a/connections/canconmanager.cpp +++ b/connections/canconmanager.cpp @@ -37,9 +37,7 @@ CANConManager::~CANConManager() void CANConManager::add(CANConnection* pConn_p) -{ - //connect(pConn_p, SIGNAL(notify()), this, SLOT(refreshCanList())); - connect(pConn_p, SIGNAL(targettedFrameReceived(CANFrame)), this, SLOT(gotTargettedFrame(CANFrame))); +{ mConns.append(pConn_p); emit connectionStatusUpdated(getNumBuses()); } @@ -47,7 +45,7 @@ void CANConManager::add(CANConnection* pConn_p) void CANConManager::remove(CANConnection* pConn_p) { - disconnect(pConn_p, 0, this, 0); + //disconnect(pConn_p, 0, this, 0); mConns.removeOne(pConn_p); emit connectionStatusUpdated(getNumBuses()); } @@ -184,21 +182,21 @@ bool CANConManager::sendFrames(const QList& pFrames) //For each device associated with buses go through and see if that device has a bus //that the filter should apply to. If so forward the data on but fudge //the bus numbers if bus wasn't -1 so that they're local to the device -bool CANConManager::addTargettedFrame(int pBusId, const CANFlt &target) +bool CANConManager::addTargettedFrame(int pBusId, uint32_t ID, uint32_t mask, QObject *receiver) { int tempBusVal; - int busBase = 0; + int busBase = 0; foreach (CANConnection* conn, mConns) { - if (pBusId == -1) conn->addTargettedFrame(pBusId, target); + if (pBusId == -1) conn->addTargettedFrame(pBusId, ID, mask, receiver); else { tempBusVal = pBusId >> busBase; tempBusVal &= ((1 << conn->getNumBuses()) - 1); if (tempBusVal) { qDebug() << "Forwarding targetted frame setting to a connection object"; - conn->addTargettedFrame(tempBusVal, target); + conn->addTargettedFrame(tempBusVal, ID, mask, receiver); } } busBase += conn->getNumBuses(); @@ -206,43 +204,31 @@ bool CANConManager::addTargettedFrame(int pBusId, const CANFlt &target) return true; } -bool CANConManager::removeTargettedFrame(int pBusId, const CANFlt &target) +bool CANConManager::removeTargettedFrame(int pBusId, uint32_t ID, uint32_t mask, QObject *receiver) { int tempBusVal; int busBase = 0; foreach (CANConnection* conn, mConns) { - if (pBusId == -1) conn->removeTargettedFrame(pBusId, target); + if (pBusId == -1) conn->removeTargettedFrame(pBusId, ID, mask, receiver); else { tempBusVal = pBusId >> busBase; tempBusVal &= ((1 << conn->getNumBuses()) - 1); - if (tempBusVal) conn->removeTargettedFrame(tempBusVal, target); + if (tempBusVal) conn->removeTargettedFrame(tempBusVal, ID, mask, receiver); } busBase += conn->getNumBuses(); } return true; } -/* - * A connected CANConnection object has passed us a frame that one or more - * other objects are interested in. Fix up the bus number to be a system global bus - * number instead of connection local bus number then send it off again. -*/ -void CANConManager::gotTargettedFrame(CANFrame frame) +bool CANConManager::removeAllTargettedFrames(QObject *receiver) { - int busBase = 0; - foreach (CANConnection* conn, mConns) { - if (conn != sender()) busBase += conn->getNumBuses(); - else break; + conn->removeAllTargettedFrames(receiver); } - qDebug() << "Targetted frame, offset was " << busBase << " id was " << frame.ID; - - frame.bus += busBase; - - emit targettedFrameReceived(frame); + return true; } diff --git a/connections/canconmanager.h b/connections/canconmanager.h index 9467f8a..dc85686 100644 --- a/connections/canconmanager.h +++ b/connections/canconmanager.h @@ -39,27 +39,31 @@ public: /** * @brief Add a new filter for the targetted frames. If a frame matches it will immediately be sent via the targettedFrameReceived signal * @param pBusId - Which bus to bond to. -1 for any, otherwise a bitfield of buses (but 0 = first bus, etc) - * @param target - The filter to use for selected targetted frames + * @param ID - 11 or 29 bit ID to match against + * @param mask - 11 or 29 bit mask used for filter + * @param receiver - Pointer to a QObject that wants to receive notification when filter is matched * @return true if filter was able to be added, false otherwise. */ - bool addTargettedFrame(int pBusId, const CANFlt &target); + bool addTargettedFrame(int pBusId, uint32_t ID, uint32_t mask, QObject *receiver); /** * @brief Try to find a matching filter in the list and remove it, no longer targetting those frames * @param pBusId - Which bus to bond to. Doesn't have to match the call to addTargettedFrame exactly. You could disconnect just one bus for instance. - * @param target - The filter that was set + * @param ID - 11 or 29 bit ID to match against + * @param mask - 11 or 29 bit mask used for filter + * @param receiver - Pointer to a QObject that wants to receive notification when filter is matched * @return true if filter was found and deleted, false otherwise. */ - bool removeTargettedFrame(int pBusId, const CANFlt &target); + bool removeTargettedFrame(int pBusId, uint32_t ID, uint32_t mask, QObject *receiver); + + bool removeAllTargettedFrames(QObject *receiver); signals: void framesReceived(CANConnection* pConn_p, QVector& pFrames); - void targettedFrameReceived(CANFrame frame); void connectionStatusUpdated(int conns); private slots: void refreshCanList(); - void gotTargettedFrame(CANFrame frame); private: explicit CANConManager(QObject *parent = 0); diff --git a/connections/canconnection.cpp b/connections/canconnection.cpp index b9b5f5b..663817c 100644 --- a/connections/canconnection.cpp +++ b/connections/canconnection.cpp @@ -256,19 +256,21 @@ void CANConnection::setCapSuspended(bool pIsSuspended) { mIsCapSuspended = pIsSuspended; } -bool CANConnection::addTargettedFrame(int pBusId, const CANFlt &target) +bool CANConnection::addTargettedFrame(int pBusId, uint32_t ID, uint32_t mask, QObject *receiver) { - /* make sure we execute in mThread context */ +/* if( mThread_p && (mThread_p != QThread::currentThread()) ) { bool ret; QMetaObject::invokeMethod(this, "addTargettedFrame", Qt::BlockingQueuedConnection, Q_RETURN_ARG(bool, ret), - Q_ARG(int , pBusId), - Q_ARG(const CANFlt&, target)); + Q_ARG(int, pBusId), + Q_ARG(uint32_t , ID), + Q_ARG(uint32_t , mask), + Q_ARG(QObject *, receiver)); return ret; } - +*/ /* sanity checks */ if(pBusId < -1 || pBusId >= (1 << getNumBuses())) return false; @@ -277,6 +279,10 @@ bool CANConnection::addTargettedFrame(int pBusId, const CANFlt &target) { if ( (pBusId == -1) || (pBusId && (1 << i)) ) { qDebug() << "Connection is registering a new targetted frame filter"; + CANFlt target; + target.id = ID; + target.mask = mask; + target.observer = receiver; mBusData_p[i].mTargettedFrames.append(target); } } @@ -284,27 +290,47 @@ bool CANConnection::addTargettedFrame(int pBusId, const CANFlt &target) return true; } -bool CANConnection::removeTargettedFrame(int pBusId, const CANFlt &target) +bool CANConnection::removeTargettedFrame(int pBusId, uint32_t ID, uint32_t mask, QObject *receiver) { - /* make sure we execute in mThread context */ +/* if( mThread_p && (mThread_p != QThread::currentThread()) ) { bool ret; - QMetaObject::invokeMethod(this, "addTargettedFrame", + QMetaObject::invokeMethod(this, "removeTargettedFrame", Qt::BlockingQueuedConnection, Q_RETURN_ARG(bool, ret), - Q_ARG(int , pBusId), - Q_ARG(const CANFlt&, target)); + Q_ARG(int, pBusId), + Q_ARG(uint32_t , ID), + Q_ARG(uint32_t , mask), + Q_ARG(QObject *, receiver)); return ret; } - +*/ /* sanity checks */ if(pBusId < -1 || pBusId >= (1 << getNumBuses())) return false; for (int i = 0; i < getNumBuses(); i++) { - //if (pBusId == -1 || (pBusId && (1 << i))) - //mBusData_p[i].mTargettedFrames.removeOne(target); + if (pBusId == -1 || (pBusId && (1 << i))) + { + CANFlt target; + target.id = ID; + target.mask = mask; + target.observer = receiver; + mBusData_p[i].mTargettedFrames.removeAll(target); + } + } + + return true; +} + +bool CANConnection::removeAllTargettedFrames(QObject *receiver) +{ + for (int i = 0; i < getNumBuses(); i++) { + foreach (const CANFlt filt, mBusData_p[i].mTargettedFrames) + { + if (filt.observer == receiver) mBusData_p[i].mTargettedFrames.removeOne(filt); + } } return true; @@ -318,8 +344,7 @@ void CANConnection::checkTargettedFrame(CANFrame &frame) maskedID = frame.ID & filt.mask; if (maskedID == filt.id) { qDebug() << "In connection object I got a targetted frame. Forwarding it."; - emit targettedFrameReceived(frame); - return; //only match once then stop. + QMetaObject::invokeMethod(filt.observer, "gotTargettedFrame",Qt::QueuedConnection, Q_ARG(CANFrame, frame)); } } } diff --git a/connections/canconnection.h b/connections/canconnection.h index 8633a2a..c6471e2 100644 --- a/connections/canconnection.h +++ b/connections/canconnection.h @@ -151,18 +151,29 @@ public slots: /** * @brief Add a new filter for the targetted frames. If a frame matches it will immediately be sent via the targettedFrameReceived signal * @param pBusId - Which bus to bond to. -1 for any, otherwise a bitfield of buses (but 0 = first bus, etc) - * @param target - The filter to use for selected targetted frames + * @param ID - 11 or 29 bit ID to match against + * @param mask - 11 or 29 bit mask used for filter + * @param receiver - Pointer to a QObject that wants to receive notification when filter is matched * @return true if filter was able to be added, false otherwise. */ - bool addTargettedFrame(int pBusId, const CANFlt &target); + bool addTargettedFrame(int pBusId, uint32_t ID, uint32_t mask, QObject *receiver); /** * @brief Try to find a matching filter in the list and remove it, no longer targetting those frames * @param pBusId - Which bus to bond to. Doesn't have to match the call to addTargettedFrame exactly. You could disconnect just one bus for instance. - * @param target - The filter that was set + * @param ID - 11 or 29 bit ID to match against + * @param mask - 11 or 29 bit mask used for filter + * @param receiver - Pointer to a QObject that wants to receive notification when filter is matched * @return true if filter was found and deleted, false otherwise. */ - bool removeTargettedFrame(int pBusId, const CANFlt &target); + bool removeTargettedFrame(int pBusId, uint32_t ID, uint32_t mask, QObject *receiver); + + /** + * @brief Removes all registered filters for the passed receiver + * @param receiver - Pointer to a QObject that registered one or more filters + * @return true if filter(s) was/were found and deleted, false otherwise. + */ + bool removeAllTargettedFrames(QObject *receiver); protected: diff --git a/firmwareuploaderwindow.cpp b/firmwareuploaderwindow.cpp index aaec100..3aa788e 100644 --- a/firmwareuploaderwindow.cpp +++ b/firmwareuploaderwindow.cpp @@ -27,22 +27,19 @@ FirmwareUploaderWindow::FirmwareUploaderWindow(const QVector *frames, timer = new QTimer(); timer->setInterval(100); //100ms without a reply will cause us to attempt a resend - CANFlt firmwareFilter; - firmwareFilter.id = 0x100; - firmwareFilter.mask = 0x7F0; - CANConManager::getInstance()->addTargettedFrame(-1, firmwareFilter); + CANConManager::getInstance()->addTargettedFrame(-1, 0x100, 0x7F0, this); //MainWindow::getReference()->setTargettedID(baseAddress + 0x10); connect(MainWindow::getReference(), SIGNAL(framesUpdated(int)), this, SLOT(updatedFrames(int))); connect(ui->btnLoadFile, SIGNAL(clicked(bool)), this, SLOT(handleLoadFile())); connect(ui->btnStartStop, SIGNAL(clicked(bool)), this, SLOT(handleStartStopTransfer())); connect(timer, SIGNAL(timeout()), this, SLOT(timerElapsed())); - connect(CANConManager::getInstance(), SIGNAL(targettedFrameReceived(CANFrame)), this, SLOT(gotTargettedFrame(CANFrame))); } FirmwareUploaderWindow::~FirmwareUploaderWindow() { timer->stop(); + CANConManager::getInstance()->removeAllTargettedFrames(this); delete timer; delete ui; } diff --git a/framefileio.cpp b/framefileio.cpp index 9e7defb..380b30b 100644 --- a/framefileio.cpp +++ b/framefileio.cpp @@ -1051,8 +1051,8 @@ bool FrameFileIO::saveCANDOFile(QString filename, const QVector* frame ms = (thisFrame.timestamp / 1000); data[0] = (((ms / 1000) % 60) << 2) + ((ms % 1000) >> 8); data[1] = (char)(ms & 0xFF); - data[2] = 0xFF; - data[3] = 0xFF; + data[2] = (char)0xFF; + data[3] = (char)0xFF; for (int l = 0; l < 8; l++) data[4 + l] = 0; outFile->write(data); @@ -1064,7 +1064,7 @@ bool FrameFileIO::saveCANDOFile(QString filename, const QVector* frame qApp->processEvents(); lineCounter = 0; } - for (int j = 0; j < 8; j++) data[4 + j] = 0xFF; + for (int j = 0; j < 8; j++) data[4 + j] = (char)0xFF; thisFrame = frames->at(c); if (!thisFrame.extended) diff --git a/re/flowviewwindow.cpp b/re/flowviewwindow.cpp index 37c2c55..a46aa58 100644 --- a/re/flowviewwindow.cpp +++ b/re/flowviewwindow.cpp @@ -382,7 +382,7 @@ void FlowViewWindow::updatedFrames(int numFrames) if (!foundID.contains(thisFrame.ID)) { foundID.append(thisFrame.ID); - QListWidgetItem* item = new QListWidgetItem(Utility::formatNumber(thisFrame.ID), ui->listFrameID); + /*QListWidgetItem* item =*/ new QListWidgetItem(Utility::formatNumber(thisFrame.ID), ui->listFrameID); } if (thisFrame.ID == refID) @@ -495,7 +495,7 @@ void FlowViewWindow::refreshIDList() if (!foundID.contains(id)) { foundID.append(id); - QListWidgetItem* item = new QListWidgetItem(Utility::formatNumber(id), ui->listFrameID); + /*QListWidgetItem* item = */ new QListWidgetItem(Utility::formatNumber(id), ui->listFrameID); } } //default is to sort in ascending order diff --git a/scriptcontainer.cpp b/scriptcontainer.cpp index 21a1ca5..3d326ce 100644 --- a/scriptcontainer.cpp +++ b/scriptcontainer.cpp @@ -77,9 +77,7 @@ void ScriptContainer::setFilter(QJSValue id, QJSValue mask, QJSValue bus) foreach(const CANFilter& flt, filters) { if(flt.bus==i) { - canFlt.id = idVal; - canFlt.mask = maskVal; - conn_p->addTargettedFrame(busVal, canFlt); + conn_p->addTargettedFrame(busVal, idVal, maskVal, this); } } }