From d4f0bd4b196b81439dfd372e151cf061c61490a8 Mon Sep 17 00:00:00 2001 From: canpadawan Date: Fri, 24 Jun 2016 17:26:30 +0200 Subject: [PATCH] add frame write support --- connections/canconconst.h | 7 ----- connections/canconnection.cpp | 53 ++++++++++++++++++----------------- connections/canconnection.h | 35 ++++++++++------------- connections/gvretserial.cpp | 4 +-- connections/gvretserial.h | 3 +- connections/socketcan.cpp | 27 ++++++++++-------- connections/socketcan.h | 3 +- 7 files changed, 62 insertions(+), 70 deletions(-) diff --git a/connections/canconconst.h b/connections/canconconst.h index b31301b..707355d 100644 --- a/connections/canconconst.h +++ b/connections/canconconst.h @@ -19,13 +19,6 @@ namespace CANCon { SOCKETCAN, NONE }; - - /* test */ - enum cbtype - { - READ, - WRITE - }; } #endif // CANCONCONST_H diff --git a/connections/canconnection.cpp b/connections/canconnection.cpp index d0d8014..cb88194 100644 --- a/connections/canconnection.cpp +++ b/connections/canconnection.cpp @@ -22,11 +22,8 @@ CANConnection::CANConnection(QString pPort, mIsCapSuspended(false), mStatus(CANCon::NOT_CONNECTED), mStarted(false), - mCallback(NULL), mThread_p(NULL) { - qDebug() << "CANConnection()"; - /* register types */ qRegisterMetaType("CANBus"); qRegisterMetaType("CANFrame"); @@ -53,7 +50,6 @@ CANConnection::CANConnection(QString pPort, CANConnection::~CANConnection() { - qDebug() << "~CANConnection()"; /* stop and delete thread */ if(mThread_p) { mThread_p->quit(); @@ -107,7 +103,7 @@ void CANConnection::suspend(bool pSuspend) void CANConnection::stop() { /* 1) execute in mThread_p context */ - if( mThread_p && (mThread_p != QThread::currentThread()) ) + if( mThread_p && mStarted && (mThread_p != QThread::currentThread()) ) { /* if thread is finished, it means we call this function for the second time so we can leave */ if( !mThread_p->isFinished() ) @@ -161,31 +157,37 @@ void CANConnection::setBusSettings(int pBusIdx, CANBus pBus) } -void CANConnection::sendFrame(const CANFrame& pFrame) +bool CANConnection::sendFrame(const CANFrame& pFrame) { /* make sure we execute in mThread context */ - if( mThread_p && (mThread_p != QThread::currentThread()) ) { + if( mThread_p && (mThread_p != QThread::currentThread()) ) + { + bool ret; QMetaObject::invokeMethod(this, "sendFrame", Qt::BlockingQueuedConnection, + Q_RETURN_ARG(bool, ret), Q_ARG(const CANFrame&, pFrame)); - return; + return ret; } return piSendFrame(pFrame); } -void CANConnection::sendFrameBatch(const QList& pFrames) +bool CANConnection::sendFrames(const QList& pFrames) { /* make sure we execute in mThread context */ - if( mThread_p && (mThread_p != QThread::currentThread()) ) { - QMetaObject::invokeMethod(this, "sendFrameBatch", + if( mThread_p && (mThread_p != QThread::currentThread()) ) + { + bool ret; + QMetaObject::invokeMethod(this, "sendFrames", Qt::BlockingQueuedConnection, + Q_RETURN_ARG(bool, ret), Q_ARG(const QList&, pFrames)); - return; + return ret; } - return piSendFrameBatch(pFrames); + return piSendFrames(pFrames); } @@ -244,14 +246,6 @@ CANCon::status CANConnection::getStatus() { return (CANCon::status) mStatus.load(); } - -bool CANConnection::setCallback(std::function pCallback) { - if(mStarted || !pCallback) return false; - mCallback = pCallback; - return true; -} - - void CANConnection::setStatus(CANCon::status pStatus) { mStatus.store(pStatus); } @@ -264,11 +258,6 @@ void CANConnection::setCapSuspended(bool pIsSuspended) { mIsCapSuspended = pIsSuspended; } -void CANConnection::callback(CANCon::cbtype pCbType) { - if(mCallback) - mCallback(pCbType); -} - bool CANConnection::setFilters(int pBusId, const QVector& pFilters, bool pFilterOut) { /* make sure we execute in mThread context */ @@ -317,6 +306,18 @@ bool CANConnection::discard(int pBusId, quint32 pId, bool& pNotify) } +bool CANConnection::piSendFrames(const QList& pFrames) +{ + foreach(const CANFrame& frame, pFrames) + { + if(!piSendFrame(frame)) + return false; + } + + return true; +} + + /* default implementation of piSetFilters */ void CANConnection::piSetFilters(int pBusId, const QVector& pFilters) { diff --git a/connections/canconnection.h b/connections/canconnection.h index 99cd668..117b0c7 100644 --- a/connections/canconnection.h +++ b/connections/canconnection.h @@ -14,7 +14,7 @@ class CANConnection : public QObject { Q_OBJECT -public: +protected: /** * @brief CANConnection constructor @@ -29,6 +29,9 @@ public: int pNumBuses, int pQueueLen, bool pUseThread); + +public: + /** * @brief CANConnection destructor */ @@ -65,13 +68,6 @@ public: */ CANCon::status getStatus(); - /** - * @brief set the callback function, this call has to be placed before calling start - * @param pCallback - * @return true if callback can be set - */ - bool setCallback(std::function pCallback); - signals: /*not implemented yet */ @@ -131,23 +127,26 @@ public slots: * @brief suspends/restarts data capture * @param pSuspend: suspends capture if true else restarts it * @note this calls piSuspend (in the working thread context if one has been started) - * @note the caller shall not access the queue when capture is suspended, it is then safe for callee to flush the queue + * @note the caller shall not access the queue when capture is suspended, + * @note the callee is expected to flush the queue */ void suspend(bool pSuspend); /** * @brief provides device with the frame to send * @param pFrame: the frame to send + * @return false if parameter is invalid (bus id for instance) * @note this calls piSendFrame (in the working thread context if one has been started) */ - void sendFrame(const CANFrame& pFrame); + bool sendFrame(const CANFrame& pFrame); /** * @brief provides device with a list of frames to send * @param pFrame: the list of frames to send + * @return false if parameter is invalid (bus id for instance) * @note this calls piSendFrameBatch (in the working thread context if one has been started) */ - void sendFrameBatch(const QList& pFrames); + bool sendFrames(const QList& pFrames); /** * @brief sets a filter list. Filters can be used to send a signal or filter out messages @@ -207,12 +206,6 @@ protected: */ void setCapSuspended(bool pIsSuspended); - /** - * @brief call callback function - * @param pCbType callback type (read or write) - */ - void callback(CANCon::cbtype pCbType); - /** * @brief used to check if a message shall be discarded. The function also update pNotify if a notification is expected for that message * @param pBusId: the bus id on which the frame has been received @@ -264,14 +257,17 @@ protected: /** * @brief provides device with the frame to send * @param pFrame: the frame to send + * @return false if parameter is invalid (bus id for instance) */ - virtual void piSendFrame(const CANFrame&) = 0; + virtual bool piSendFrame(const CANFrame&) = 0; /** * @brief provides device with a list of frames to send * @param pFrame: the list of frames to send + * @return false if parameter is invalid (bus id for instance) + * @note implementing this function is optional */ - virtual void piSendFrameBatch(const QList&) = 0; + virtual bool piSendFrames(const QList&); /** * @brief sets a hardware filter list @@ -289,7 +285,6 @@ private: bool mIsCapSuspended; QAtomicInt mStatus; bool mStarted; - std::function mCallback; BusData* mBusData_p; QThread* mThread_p; }; diff --git a/connections/gvretserial.cpp b/connections/gvretserial.cpp index 1d34f38..d9408b3 100644 --- a/connections/gvretserial.cpp +++ b/connections/gvretserial.cpp @@ -141,8 +141,8 @@ void GVRetSerial::piSetBusSettings(int pBusIdx, CANBus bus) } -void GVRetSerial::piSendFrame(const CANFrame&) {} -void GVRetSerial::piSendFrameBatch(const QList&){} +bool GVRetSerial::piSendFrame(const CANFrame&) {return false;} + /****************************************************************/ diff --git a/connections/gvretserial.h b/connections/gvretserial.h index 4ef824c..4038a01 100644 --- a/connections/gvretserial.h +++ b/connections/gvretserial.h @@ -51,8 +51,7 @@ protected: virtual void piSetBusSettings(int pBusIdx, CANBus pBus); virtual bool piGetBusSettings(int pBusIdx, CANBus& pBus); virtual void piSuspend(bool pSuspend); - virtual void piSendFrame(const CANFrame&) ; - virtual void piSendFrameBatch(const QList&); + virtual bool piSendFrame(const CANFrame&) ; void disconnectDevice(); diff --git a/connections/socketcan.cpp b/connections/socketcan.cpp index 7da09f0..64eaa0f 100644 --- a/connections/socketcan.cpp +++ b/connections/socketcan.cpp @@ -15,14 +15,12 @@ SocketCan::SocketCan(QString portName) : mDev_p(NULL), mTimer(this) /*NB: set connection as parent of timer to manage it from working thread */ { - qDebug() << "SocketCan()"; } SocketCan::~SocketCan() { stop(); - qDebug() << "~SocketCan()"; } @@ -102,8 +100,20 @@ void SocketCan::piSetBusSettings(int pBusIdx, CANBus bus) } -void SocketCan::piSendFrame(const CANFrame&) {} -void SocketCan::piSendFrameBatch(const QList&){} +bool SocketCan::piSendFrame(const CANFrame& pFrame) +{ + /* sanity checks */ + if(0 != pFrame.bus || pFrame.len>8) + return false; + + /* fill frame */ + QCanBusFrame frame; + frame.setFrameId(pFrame.ID); + frame.setExtendedFrameFormat(false); + frame.setPayload(QByteArray((const char*)pFrame.data, pFrame.len)); + + return mDev_p->writeFrame(frame); +} /***********************************/ @@ -143,7 +153,6 @@ void SocketCan::framesWritten(qint64 count) void SocketCan::framesReceived() { /* test */ - bool enqueued = false; bool sndNotif = false; /* sanity checks */ @@ -180,18 +189,14 @@ void SocketCan::framesReceived() /* enqueue frame */ getQueue().queue(); - /* set enqueued */ - enqueued = true; } +#if 0 else qDebug() << "can't get a frame, ERROR"; +#endif } } - if(enqueued) { - callback(CANCon::READ); - } - if(sndNotif) { emit notify(); } diff --git a/connections/socketcan.h b/connections/socketcan.h index 0d0af5b..c303236 100644 --- a/connections/socketcan.h +++ b/connections/socketcan.h @@ -25,8 +25,7 @@ protected: virtual void piSetBusSettings(int pBusIdx, CANBus pBus); virtual bool piGetBusSettings(int pBusIdx, CANBus& pBus); virtual void piSuspend(bool pSuspend); - virtual void piSendFrame(const CANFrame&) ; - virtual void piSendFrameBatch(const QList&); + virtual bool piSendFrame(const CANFrame&); void disconnectDevice();