summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/serialbus/qcanbusdevice.cpp14
-rw-r--r--src/serialbus/qcanbusdevice.h12
-rw-r--r--tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp86
3 files changed, 112 insertions, 0 deletions
diff --git a/src/serialbus/qcanbusdevice.cpp b/src/serialbus/qcanbusdevice.cpp
index 6723f77..c62cd2e 100644
--- a/src/serialbus/qcanbusdevice.cpp
+++ b/src/serialbus/qcanbusdevice.cpp
@@ -142,6 +142,20 @@ Q_LOGGING_CATEGORY(QT_CANBUS, "qt.canbus")
*/
/*!
+ \fn QCanBusDevice::Filter::operator==(const Filter &other)
+
+ Returns true, if the filter \a other is equal to this filter,
+ otherwise returns false.
+*/
+
+/*!
+ \fn QCanBusDevice::Filter::operator!=(const Filter &other)
+
+ Returns true, if the filter \a other is not equal to this filter,
+ otherwise returns false.
+*/
+
+/*!
\enum QCanBusDevice::Filter::FormatFilter
This enum describes the format pattern, which is used to filter incoming
CAN bus frames.
diff --git a/src/serialbus/qcanbusdevice.h b/src/serialbus/qcanbusdevice.h
index a47fac4..b590510 100644
--- a/src/serialbus/qcanbusdevice.h
+++ b/src/serialbus/qcanbusdevice.h
@@ -84,6 +84,17 @@ public:
struct Filter
{
+ bool operator==(const Filter &other) const
+ {
+ return frameId == other.frameId && frameIdMask == other.frameIdMask
+ && type == other.type && format == other.format;
+ }
+
+ bool operator!=(const Filter &other) const
+ {
+ return !operator==(other);
+ }
+
enum FormatFilter {
MatchBaseFormat = 0x0001,
MatchExtendedFormat = 0x0002,
@@ -171,6 +182,7 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(QCanBusDevice::Directions)
QT_END_NAMESPACE
+Q_DECLARE_METATYPE(QCanBusDevice::Filter)
Q_DECLARE_METATYPE(QCanBusDevice::Filter::FormatFilter)
Q_DECLARE_METATYPE(QList<QCanBusDevice::Filter>)
diff --git a/tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp b/tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp
index 85d445f..24127d9 100644
--- a/tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp
+++ b/tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp
@@ -149,6 +149,8 @@ private slots:
void error();
void cleanupTestCase();
void tst_filtering();
+ void filterEqual_data();
+ void filterEqual();
void tst_bufferingAttribute();
void tst_waitForFramesReceived();
@@ -161,6 +163,7 @@ tst_QCanBusDevice::tst_QCanBusDevice()
{
qRegisterMetaType<QCanBusDevice::CanBusDeviceState>();
qRegisterMetaType<QCanBusDevice::CanBusError>();
+ qRegisterMetaType<QCanBusDevice::Filter>();
}
void tst_QCanBusDevice::initTestCase()
@@ -421,6 +424,89 @@ void tst_QCanBusDevice::tst_filtering()
QVERIFY(!(newFilter.at(1).format & QCanBusDevice::Filter::MatchExtendedFormat));
}
+void tst_QCanBusDevice::filterEqual_data()
+{
+ using Filter = QCanBusDevice::Filter;
+ using Frame = QCanBusFrame;
+
+ QTest::addColumn<QCanBusDevice::Filter>("first");
+ QTest::addColumn<QCanBusDevice::Filter>("second");
+ QTest::addColumn<bool>("isEqual");
+
+ auto filter = [](quint32 frameId, quint32 frameIdMask,
+ Frame::FrameType type,
+ Filter::FormatFilter format) {
+ Filter result;
+ result.frameId = frameId;
+ result.frameIdMask = frameIdMask;
+ result.type = type;
+ result.format = format;
+ return result;
+ };
+
+ QTest::newRow("empty-equal")
+ << Filter()
+ << Filter()
+ << true;
+ QTest::newRow("empty-default-equal")
+ << Filter()
+ << filter(0, 0, Frame::InvalidFrame, Filter::MatchBaseAndExtendedFormat)
+ << true;
+ QTest::newRow("empty-non-default-different")
+ << Filter()
+ << filter(1, 2, Frame::ErrorFrame, Filter::MatchBaseFormat)
+ << false;
+
+ QTest::newRow("frame-id-equal")
+ << filter(0x345, 0x800, Frame::RemoteRequestFrame, Filter::MatchBaseFormat)
+ << filter(0x345, 0x800, Frame::RemoteRequestFrame, Filter::MatchBaseFormat)
+ << true;
+ QTest::newRow("frame-id-different")
+ << filter(0x345, 0x000, Frame::RemoteRequestFrame, Filter::MatchBaseFormat)
+ << filter(0x346, 0x000, Frame::RemoteRequestFrame, Filter::MatchBaseFormat)
+ << false;
+
+ QTest::newRow("frame-mask-equal")
+ << filter(0x123, 0x7FF, Frame::InvalidFrame, Filter::MatchBaseAndExtendedFormat)
+ << filter(0x123, 0x7FF, Frame::InvalidFrame, Filter::MatchBaseAndExtendedFormat)
+ << true;
+ QTest::newRow("frame-mask-different")
+ << filter(0x123, 0x7FF, Frame::InvalidFrame, Filter::MatchBaseAndExtendedFormat)
+ << filter(0x123, 0x7FE, Frame::InvalidFrame, Filter::MatchBaseAndExtendedFormat)
+ << false;
+
+ QTest::newRow("frame-type-equal")
+ << filter(0xFFF, 0xBFF, Frame::DataFrame, Filter::MatchBaseAndExtendedFormat)
+ << filter(0xFFF, 0xBFF, Frame::DataFrame, Filter::MatchBaseAndExtendedFormat)
+ << true;
+ QTest::newRow("frame-type-different")
+ << filter(0xFFF, 0xBFF, Frame::DataFrame, Filter::MatchBaseAndExtendedFormat)
+ << filter(0xFFF, 0xBFF, Frame::InvalidFrame, Filter::MatchBaseAndExtendedFormat)
+ << false;
+
+ QTest::newRow("filter-equal")
+ << filter(0xFFF, 0xBFF, Frame::ErrorFrame, Filter::MatchExtendedFormat)
+ << filter(0xFFF, 0xBFF, Frame::ErrorFrame, Filter::MatchExtendedFormat)
+ << true;
+ QTest::newRow("filter-different")
+ << filter(0xFFF, 0xBFF, Frame::ErrorFrame, Filter::MatchExtendedFormat)
+ << filter(0xFFF, 0xBFF, Frame::ErrorFrame, Filter::MatchBaseAndExtendedFormat)
+ << false;
+}
+
+void tst_QCanBusDevice::filterEqual()
+{
+ QFETCH(QCanBusDevice::Filter, first);
+ QFETCH(QCanBusDevice::Filter, second);
+ QFETCH(bool, isEqual);
+
+ if (isEqual) {
+ QCOMPARE(first, second);
+ } else {
+ QVERIFY(first != second);
+ }
+}
+
void tst_QCanBusDevice::tst_bufferingAttribute()
{
std::unique_ptr<tst_Backend> canDevice(new tst_Backend);