summaryrefslogtreecommitdiffstats
path: root/src/plugins/canbus/peakcan/peakcanbackend.cpp
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2016-05-12 16:34:01 +0300
committerDenis Shienkov <denis.shienkov@gmail.com>2016-05-31 07:45:12 +0000
commit5677c43a8f3a439c48553170f7ce46c5609a1f37 (patch)
treec05f78501e229a526a3a611f040ce8065eeb8e6b /src/plugins/canbus/peakcan/peakcanbackend.cpp
parentce1cb3ad2972ea1e570cf73b1480765a6a8495d6 (diff)
CAN: Simplify the code of the PeakCAN backend
We do not need to keep separate enableWriteNotification(), acquireReadNotification() and releaseReadNotification() methods; their bodies can be moved to the open()/close() methods, that simplified a code. Change-Id: I35577d6637e879da80701658c3d8781ba891a1b8 Reviewed-by: André Hartmann <aha_1980@gmx.de>
Diffstat (limited to 'src/plugins/canbus/peakcan/peakcanbackend.cpp')
-rw-r--r--src/plugins/canbus/peakcan/peakcanbackend.cpp126
1 files changed, 44 insertions, 82 deletions
diff --git a/src/plugins/canbus/peakcan/peakcanbackend.cpp b/src/plugins/canbus/peakcan/peakcanbackend.cpp
index ca666cf..4e733fc 100644
--- a/src/plugins/canbus/peakcan/peakcanbackend.cpp
+++ b/src/plugins/canbus/peakcan/peakcanbackend.cpp
@@ -213,12 +213,28 @@ bool PeakCanBackendPrivate::open()
return false;
}
- if (!acquireReadNotification()) {
- if (TPCANStatus st = ::CAN_Uninitialize(channelIndex) != PCAN_ERROR_OK)
- q->setError(systemErrorString(st), QCanBusDevice::ConnectionError);
+#if defined(Q_OS_WIN32)
+ if (readHandle == INVALID_HANDLE_VALUE) {
+ readHandle = ::CreateEvent(nullptr, FALSE, FALSE, nullptr);
+ if (!readHandle) {
+ q->setError(qt_error_string(::GetLastError()), QCanBusDevice::ConnectionError);
+ return false;
+ }
+ }
+#endif
+
+ if (TPCANStatus st = ::CAN_SetValue(channelIndex, PCAN_RECEIVE_EVENT, &readHandle, sizeof(readHandle))
+ != PCAN_ERROR_OK) {
+ q->setError(systemErrorString(st), QCanBusDevice::ConnectionError);
return false;
}
+ writeNotifier = new WriteNotifier(this, q);
+ writeNotifier->setInterval(0);
+
+ readNotifier = new ReadNotifier(this, q);
+ readNotifier->setEnabled(true);
+
isOpen = true;
return true;
}
@@ -227,17 +243,31 @@ void PeakCanBackendPrivate::close()
{
Q_Q(PeakCanBackend);
- enableWriteNotification(false);
- releaseReadNotification();
+ delete readNotifier;
+ readNotifier = nullptr;
- if (writeNotifier) {
- delete writeNotifier;
- writeNotifier = nullptr;
+ delete writeNotifier;
+ writeNotifier = nullptr;
+
+ quint32 value = 0;
+ if (TPCANStatus st = ::CAN_SetValue(channelIndex, PCAN_RECEIVE_EVENT, &value, sizeof(value))
+ != PCAN_ERROR_OK) {
+ emit q->setError(systemErrorString(st), QCanBusDevice::ConnectionError);
}
if (TPCANStatus st = ::CAN_Uninitialize(channelIndex) != PCAN_ERROR_OK)
emit q->setError(systemErrorString(st), QCanBusDevice::ConnectionError);
+#if defined(Q_OS_WIN32)
+ if (readHandle && (readHandle != INVALID_HANDLE_VALUE)) {
+ if (!::CloseHandle(readHandle))
+ q->setError(qt_error_string(::GetLastError()), QCanBusDevice::ConnectionError);
+ readHandle = INVALID_HANDLE_VALUE;
+ }
+#else
+ readHandle = -1;
+#endif
+
isOpen = false;
}
@@ -313,30 +343,12 @@ QString PeakCanBackendPrivate::systemErrorString(int errorCode)
return QString::fromLatin1(buffer);
}
-void PeakCanBackendPrivate::enableWriteNotification(bool enable)
-{
- Q_Q(PeakCanBackend);
-
- 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 PeakCanBackendPrivate::startWrite()
{
Q_Q(PeakCanBackend);
if (!q->hasOutgoingFrames()) {
- enableWriteNotification(false);
+ writeNotifier->stop();
return;
}
@@ -360,60 +372,8 @@ void PeakCanBackendPrivate::startWrite()
else
emit q->framesWritten(qint64(1));
- if (q->hasOutgoingFrames())
- enableWriteNotification(true);
-}
-
-bool PeakCanBackendPrivate::acquireReadNotification()
-{
- Q_Q(PeakCanBackend);
-
-#if defined(Q_OS_WIN32)
- if (readHandle == INVALID_HANDLE_VALUE) {
- readHandle = ::CreateEvent(nullptr, FALSE, FALSE, nullptr);
- if (!readHandle) {
- q->setError(qt_error_string(::GetLastError()), QCanBusDevice::ReadError);
- return false;
- }
- }
-#endif
-
- if (TPCANStatus st = ::CAN_SetValue(channelIndex, PCAN_RECEIVE_EVENT, &readHandle, sizeof(readHandle))
- != PCAN_ERROR_OK) {
- q->setError(systemErrorString(st), QCanBusDevice::ReadError);
- return false;
- }
-
- if (!readNotifier) {
- readNotifier = new ReadNotifier(this, q);
- readNotifier->setEnabled(true);
- }
-
- return true;
-}
-
-void PeakCanBackendPrivate::releaseReadNotification()
-{
- Q_Q(PeakCanBackend);
-
- quint32 value = 0;
- if (TPCANStatus st = ::CAN_SetValue(channelIndex, PCAN_RECEIVE_EVENT, &value, sizeof(value)) != PCAN_ERROR_OK)
- q->setError(systemErrorString(st), QCanBusDevice::ConnectionError);
-
- if (readNotifier) {
- delete readNotifier;
- readNotifier = nullptr;
- }
-
-#if defined(Q_OS_WIN32)
- if (readHandle && (readHandle != INVALID_HANDLE_VALUE)) {
- if (!::CloseHandle(readHandle))
- q->setError(qt_error_string(::GetLastError()), QCanBusDevice::ConnectionError);
- readHandle = INVALID_HANDLE_VALUE;
- }
-#else
- readHandle = -1;
-#endif
+ if (q->hasOutgoingFrames() && !writeNotifier->isActive())
+ writeNotifier->start();
}
void PeakCanBackendPrivate::startRead()
@@ -555,7 +515,9 @@ bool PeakCanBackend::writeFrame(const QCanBusFrame &newData)
}
enqueueOutgoingFrame(newData);
- d->enableWriteNotification(true);
+
+ if (!d->writeNotifier->isActive())
+ d->writeNotifier->start();
return true;
}