From e402ea76fa427897fd346a0a04d7e5c646bbfa98 Mon Sep 17 00:00:00 2001 From: canpadawan Date: Wed, 29 Jun 2016 11:55:58 +0200 Subject: [PATCH] add CANConManager --- connections/canconmanager.cpp | 92 ++++++++++++++++++++++++++++++++++- connections/canconmanager.h | 34 +++++++++++-- connections/canconnection.cpp | 2 +- connections/canconnection.h | 2 +- connections/socketcan.cpp | 2 +- 5 files changed, 124 insertions(+), 8 deletions(-) diff --git a/connections/canconmanager.cpp b/connections/canconmanager.cpp index c363d2a..b0b5a37 100644 --- a/connections/canconmanager.cpp +++ b/connections/canconmanager.cpp @@ -1,6 +1,94 @@ #include "canconmanager.h" -CANConManager::CANConManager() -{ +CANConManager* CANConManager::mInstance = NULL; + +CANConManager* CANConManager::getInstance() +{ + if(!mInstance) + mInstance = new CANConManager(); + + return mInstance; +} + + +CANConManager::CANConManager(QObject *parent): QObject(parent) +{ + connect(&mTimer, SIGNAL(timeout()), this, SLOT(refreshCanList())); + mTimer.setInterval(500); /*tick twice a second */ + mTimer.setSingleShot(false); + mTimer.start(); +} + + +CANConManager::~CANConManager() +{ + mTimer.stop(); + mInstance = NULL; +} + + +void CANConManager::add(CANConnection* pConn_p) +{ + connect(pConn_p, SIGNAL(notify()), this, SLOT(refreshCanList())); + mConns.append(pConn_p); +} + + +void CANConManager::remove(CANConnection* pConn_p) +{ + disconnect(pConn_p, 0, this, 0); + mConns.removeOne(pConn_p); +} + + +void CANConManager::refreshCanList() +{ + QObject* sender_p = QObject::sender(); + + if( sender_p != &mTimer) + { + /* if we are not the sender, the signal is coming from a connection */ + /* refresh only the given connection */ + if(mConns.contains((CANConnection*) sender_p)) + refreshConnection((CANConnection*)sender_p); + } + else + { + foreach (CANConnection* conn_p, mConns) + refreshConnection((CANConnection*)conn_p); + } +} + + +QList& CANConManager::getConnections() +{ + return mConns; +} + + +CANConnection* CANConManager::getByName(const QString& pName) const +{ + foreach(CANConnection* conn_p, mConns) + { + if(conn_p->getPort() == pName) + return conn_p; + } + + return NULL; +} + + +void CANConManager::refreshConnection(CANConnection* pConn_p) +{ + CANFrame* frame_p = NULL; + QVector frames; + + while( (frame_p = pConn_p->getQueue().peek() ) ) { + frames.append(*frame_p); + pConn_p->getQueue().dequeue(); + } + + if(frames.size()) + emit framesReceived(pConn_p, frames); } diff --git a/connections/canconmanager.h b/connections/canconmanager.h index fd17d6e..66896f1 100644 --- a/connections/canconmanager.h +++ b/connections/canconmanager.h @@ -1,11 +1,39 @@ #ifndef CANCONMANAGER_H #define CANCONMANAGER_H +#include +#include -class CANConManager +#include "canconnection.h" + +class CANConManager : public QObject { + Q_OBJECT + public: - CANConManager(); + static CANConManager* getInstance(); + virtual ~CANConManager(); + + void add(CANConnection* pConn_p); + void remove(CANConnection* pConn_p); + QList& getConnections(); + + CANConnection* getByName(const QString& pName) const; + +signals: + void framesReceived(CANConnection* pConn_p, QVector& pFrames); + +private slots: + void refreshCanList(); + +private: + explicit CANConManager(QObject *parent = 0); + void refreshConnection(CANConnection* pConn_p); + + static CANConManager* mInstance; + QList mConns; + QTimer mTimer; }; -#endif // CANCONMANAGER_H \ No newline at end of file +#endif // CANCONNECTIONMODEL_H + diff --git a/connections/canconnection.cpp b/connections/canconnection.cpp index cb88194..778890f 100644 --- a/connections/canconnection.cpp +++ b/connections/canconnection.cpp @@ -191,7 +191,7 @@ bool CANConnection::sendFrames(const QList& pFrames) } -int CANConnection::getNumBuses() { +int CANConnection::getNumBuses() const{ return mNumBuses; } diff --git a/connections/canconnection.h b/connections/canconnection.h index 117b0c7..b0c8f51 100644 --- a/connections/canconnection.h +++ b/connections/canconnection.h @@ -42,7 +42,7 @@ public: * @brief getNumBuses * @return returns the number of buses of the device */ - int getNumBuses(); + int getNumBuses() const; /** * @brief getPort diff --git a/connections/socketcan.cpp b/connections/socketcan.cpp index 64eaa0f..b49e1fe 100644 --- a/connections/socketcan.cpp +++ b/connections/socketcan.cpp @@ -147,7 +147,7 @@ void SocketCan::errorReceived(QCanBusDevice::CanBusError error) const void SocketCan::framesWritten(qint64 count) { - qDebug() << "Number of frames written:" << count; + //qDebug() << "Number of frames written:" << count; } void SocketCan::framesReceived()