diff options
author | Denis Shienkov <denis.shienkov@gmail.com> | 2016-05-12 17:26:53 +0300 |
---|---|---|
committer | Denis Shienkov <denis.shienkov@gmail.com> | 2016-05-31 10:54:42 +0000 |
commit | cf066595e079112cc00c3105f3d9244a975af77f (patch) | |
tree | d695c3501ecb05983d59ede291b9c5375d27b6a9 /src/plugins/canbus/tinycan | |
parent | 5677c43a8f3a439c48553170f7ce46c5609a1f37 (diff) |
CAN: Simplify the code of the TinyCAN backend
We do not need to keep separate enableWriteNotification()
and enableReadNotification() methods; their bodies can be
moved to the open()/close() methods, that simplifies the
code.
Change-Id: I7a6c1dd5ccfea51b0506e717c9809faa59f13161
Reviewed-by: André Hartmann <aha_1980@gmx.de>
Diffstat (limited to 'src/plugins/canbus/tinycan')
-rw-r--r-- | src/plugins/canbus/tinycan/tinycanbackend.cpp | 45 | ||||
-rw-r--r-- | src/plugins/canbus/tinycan/tinycanbackend_p.h | 2 |
2 files changed, 14 insertions, 33 deletions
diff --git a/src/plugins/canbus/tinycan/tinycanbackend.cpp b/src/plugins/canbus/tinycan/tinycanbackend.cpp index 43640cc..4a36918 100644 --- a/src/plugins/canbus/tinycan/tinycanbackend.cpp +++ b/src/plugins/canbus/tinycan/tinycanbackend.cpp @@ -188,6 +188,9 @@ bool TinyCanBackendPrivate::open() return false; } + writeNotifier = new WriteNotifier(this, q); + writeNotifier->setInterval(0); + isOpen = true; return true; } @@ -196,6 +199,9 @@ void TinyCanBackendPrivate::close() { Q_Q(TinyCanBackend); + delete writeNotifier; + writeNotifier = nullptr; + if (int ret = ::CanDeviceClose(channelIndex) < 0) q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ConnectionError); @@ -309,30 +315,12 @@ void TinyCanBackendPrivate::setupDefaultConfigurations() q->setConfigurationParameter(QCanBusDevice::BitRateKey, 500000); } -void TinyCanBackendPrivate::enableWriteNotification(bool enable) -{ - Q_Q(TinyCanBackend); - - if (writeNotifier) { - if (enable) { - if (!writeNotifier->isActive()) - writeNotifier->start(); - } else { - writeNotifier->stop(); - } - } else if (enable) { - writeNotifier = new WriteNotifier(this, q); - writeNotifier->setInterval(0); - writeNotifier->start(); - } -} - void TinyCanBackendPrivate::startWrite() { Q_Q(TinyCanBackend); if (!q->hasOutgoingFrames()) { - enableWriteNotification(false); + writeNotifier->stop(); return; } @@ -360,16 +348,8 @@ void TinyCanBackendPrivate::startWrite() emit q->framesWritten(messagesToWrite); } - if (q->hasOutgoingFrames()) - enableWriteNotification(true); -} - -bool TinyCanBackendPrivate::enableReadNotification() -{ - ::CanSetRxEventCallback(&canRxEventCallback); - ::CanSetEvents(EVENT_ENABLE_RX_MESSAGES); - - return true; + if (q->hasOutgoingFrames() && !writeNotifier->isActive()) + writeNotifier->start(); } // this method is called from the different thread! @@ -434,7 +414,8 @@ void TinyCanBackendPrivate::startupDriver() return; } - enableReadNotification(); + ::CanSetRxEventCallback(&canRxEventCallback); + ::CanSetEvents(EVENT_ENABLE_RX_MESSAGES); } else if (driverRefCount < 0) { qCritical("Wrong reference counter: %d", driverRefCount); @@ -563,7 +544,9 @@ bool TinyCanBackend::writeFrame(const QCanBusFrame &newData) } enqueueOutgoingFrame(newData); - d->enableWriteNotification(true); + + if (!d->writeNotifier->isActive()) + d->writeNotifier->start(); return true; } diff --git a/src/plugins/canbus/tinycan/tinycanbackend_p.h b/src/plugins/canbus/tinycan/tinycanbackend_p.h index 14630dd..18c82ef 100644 --- a/src/plugins/canbus/tinycan/tinycanbackend_p.h +++ b/src/plugins/canbus/tinycan/tinycanbackend_p.h @@ -70,9 +70,7 @@ public: QString systemErrorString(int errorCode); void setupChannel(const QString &interfaceName); void setupDefaultConfigurations(); - void enableWriteNotification(bool enable); void startWrite(); - bool enableReadNotification(); void startRead(); void startupDriver(); void cleanupDriver(); |