add frame write support

This commit is contained in:
canpadawan
2016-06-24 17:26:30 +02:00
parent 10515e4cdf
commit d4f0bd4b19
7 changed files with 62 additions and 70 deletions
-7
View File
@@ -19,13 +19,6 @@ namespace CANCon {
SOCKETCAN,
NONE
};
/* test */
enum cbtype
{
READ,
WRITE
};
}
#endif // CANCONCONST_H
+27 -26
View File
@@ -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>("CANBus");
qRegisterMetaType<CANFrame>("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<CANFrame>& pFrames)
bool CANConnection::sendFrames(const QList<CANFrame>& 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<CANFrame>&, 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<void(CANCon::cbtype)> 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<CANFlt>& 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<CANFrame>& pFrames)
{
foreach(const CANFrame& frame, pFrames)
{
if(!piSendFrame(frame))
return false;
}
return true;
}
/* default implementation of piSetFilters */
void CANConnection::piSetFilters(int pBusId, const QVector<CANFlt>& pFilters)
{
+15 -20
View File
@@ -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<void(CANCon::cbtype)> 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<CANFrame>& pFrames);
bool sendFrames(const QList<CANFrame>& 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<CANFrame>&) = 0;
virtual bool piSendFrames(const QList<CANFrame>&);
/**
* @brief sets a hardware filter list
@@ -289,7 +285,6 @@ private:
bool mIsCapSuspended;
QAtomicInt mStatus;
bool mStarted;
std::function<void(CANCon::cbtype)> mCallback;
BusData* mBusData_p;
QThread* mThread_p;
};
+2 -2
View File
@@ -141,8 +141,8 @@ void GVRetSerial::piSetBusSettings(int pBusIdx, CANBus bus)
}
void GVRetSerial::piSendFrame(const CANFrame&) {}
void GVRetSerial::piSendFrameBatch(const QList<CANFrame>&){}
bool GVRetSerial::piSendFrame(const CANFrame&) {return false;}
/****************************************************************/
+1 -2
View File
@@ -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<CANFrame>&);
virtual bool piSendFrame(const CANFrame&) ;
void disconnectDevice();
+16 -11
View File
@@ -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<CANFrame>&){}
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();
}
+1 -2
View File
@@ -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<CANFrame>&);
virtual bool piSendFrame(const CANFrame&);
void disconnectDevice();