add filters & callback support
This commit is contained in:
+100
-20
@@ -1,6 +1,15 @@
|
||||
#include <QThread>
|
||||
#include "canconnection.h"
|
||||
|
||||
|
||||
struct BusData {
|
||||
CANBus mBus;
|
||||
bool mConfigured;
|
||||
QVector<CANFlt> mFilters;
|
||||
bool mFilterOut;
|
||||
};
|
||||
|
||||
|
||||
CANConnection::CANConnection(QString pPort,
|
||||
CANCon::type pType,
|
||||
int pNumBuses,
|
||||
@@ -12,6 +21,8 @@ CANConnection::CANConnection(QString pPort,
|
||||
mType(pType),
|
||||
mIsCapSuspended(false),
|
||||
mStatus(CANCon::NOT_CONNECTED),
|
||||
mStarted(false),
|
||||
mCallback(NULL),
|
||||
mThread_p(NULL)
|
||||
{
|
||||
qDebug() << "CANConnection()";
|
||||
@@ -20,16 +31,18 @@ CANConnection::CANConnection(QString pPort,
|
||||
qRegisterMetaType<CANBus>("CANBus");
|
||||
qRegisterMetaType<CANFrame>("CANFrame");
|
||||
qRegisterMetaType<CANCon::status>("CANCon::status");
|
||||
qRegisterMetaType<CANFlt>("CANFlt");
|
||||
|
||||
/* set queue size */
|
||||
mQueue.setSize(pQueueLen); /*TODO add check on returned value */
|
||||
|
||||
/* allocate buses */
|
||||
mBus = new CANBus[mNumBuses];
|
||||
mConfigured = new bool[mNumBuses];
|
||||
|
||||
for(int i=0 ; i<mNumBuses ; i++)
|
||||
mConfigured[i] = false;
|
||||
/* TODO: change those tables for a vector */
|
||||
mBusData_p = new BusData[mNumBuses];
|
||||
for(int i=0 ; i<mNumBuses ; i++) {
|
||||
mBusData_p[i].mConfigured = false;
|
||||
mBusData_p[i].mFilterOut = false;
|
||||
}
|
||||
|
||||
/* if needed, create a thread and move ourself into it */
|
||||
if(pUseThread) {
|
||||
@@ -49,12 +62,10 @@ CANConnection::~CANConnection()
|
||||
mThread_p = NULL;
|
||||
}
|
||||
|
||||
/* delete bus table */
|
||||
delete[] mBus;
|
||||
mBus = NULL;
|
||||
/* configured table */
|
||||
delete[] mConfigured;
|
||||
mConfigured = NULL;
|
||||
if(mBusData_p) {
|
||||
delete[] mBusData_p;
|
||||
mBusData_p = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -71,6 +82,9 @@ void CANConnection::start()
|
||||
return;
|
||||
}
|
||||
|
||||
/* set started flag */
|
||||
mStarted = true;
|
||||
|
||||
/* in multithread case, this will be called before entering thread event loop */
|
||||
return piStarted();
|
||||
}
|
||||
@@ -181,33 +195,33 @@ int CANConnection::getNumBuses() {
|
||||
|
||||
|
||||
bool CANConnection::isConfigured(int pBusId) {
|
||||
if( pBusId < 0 || pBusId >= mNumBuses)
|
||||
if( pBusId < 0 || pBusId >= getNumBuses())
|
||||
return false;
|
||||
return mConfigured[pBusId];
|
||||
return mBusData_p[pBusId].mConfigured;
|
||||
}
|
||||
|
||||
void CANConnection::setConfigured(int pBusId, bool pConfigured) {
|
||||
if( pBusId < 0 || pBusId >= mNumBuses)
|
||||
if( pBusId < 0 || pBusId >= getNumBuses())
|
||||
return;
|
||||
mConfigured[pBusId] = pConfigured;
|
||||
mBusData_p[pBusId].mConfigured = pConfigured;
|
||||
}
|
||||
|
||||
|
||||
bool CANConnection::getBusConfig(int pBusId, CANBus& pBus) {
|
||||
if( pBusId < 0 || pBusId >= mNumBuses || !isConfigured(pBusId))
|
||||
if( pBusId < 0 || pBusId >= getNumBuses() || !isConfigured(pBusId))
|
||||
return false;
|
||||
|
||||
pBus = mBus[pBusId];
|
||||
pBus = mBusData_p[pBusId].mBus;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void CANConnection::setBusConfig(int pBusId, CANBus& pBus) {
|
||||
if( pBusId < 0 || pBusId >= mNumBuses)
|
||||
if( pBusId < 0 || pBusId >= getNumBuses())
|
||||
return;
|
||||
|
||||
mConfigured[pBusId] = true;
|
||||
mBus[pBusId] = pBus;
|
||||
mBusData_p[pBusId].mConfigured = true;
|
||||
mBusData_p[pBusId].mBus = pBus;
|
||||
}
|
||||
|
||||
|
||||
@@ -231,6 +245,13 @@ CANCon::status CANConnection::getStatus() {
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -243,3 +264,62 @@ 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 */
|
||||
if( mThread_p && (mThread_p != QThread::currentThread()) ) {
|
||||
bool ret;
|
||||
QMetaObject::invokeMethod(this, "setFilters",
|
||||
Qt::BlockingQueuedConnection,
|
||||
Q_RETURN_ARG(bool, ret),
|
||||
Q_ARG(int , pBusId),
|
||||
Q_ARG(const QVector<CANFlt>&, pFilters),
|
||||
Q_ARG(bool , pFilterOut));
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* sanity checks */
|
||||
if(pBusId<0 || pBusId>=getNumBuses())
|
||||
return false;
|
||||
|
||||
/* copy filters */
|
||||
mBusData_p[pBusId].mFilters = pFilters;
|
||||
mBusData_p[pBusId].mFilterOut = pFilterOut;
|
||||
|
||||
/* set hardware filtering if available */
|
||||
if(pFilterOut)
|
||||
piSetFilters(pBusId, pFilters);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool CANConnection::discard(int pBusId, quint32 pId, bool& pNotify)
|
||||
{
|
||||
if(pBusId<0 || pBusId>=getNumBuses())
|
||||
return true;
|
||||
|
||||
foreach (const CANFlt& filter, mBusData_p[pBusId].mFilters)
|
||||
{
|
||||
if( (filter.id & filter.mask) == (pId & filter.mask) )
|
||||
{
|
||||
if(filter.notify)
|
||||
pNotify = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return mBusData_p[pBusId].mFilterOut;
|
||||
}
|
||||
|
||||
|
||||
/* default implementation of piSetFilters */
|
||||
void CANConnection::piSetFilters(int pBusId, const QVector<CANFlt>& pFilters)
|
||||
{
|
||||
Q_UNUSED(pBusId);
|
||||
Q_UNUSED(pFilters);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user