diff options
author | Andre Hartmann <aha_1980@gmx.de> | 2017-01-18 21:45:16 +0100 |
---|---|---|
committer | André Hartmann <aha_1980@gmx.de> | 2017-01-24 08:56:54 +0000 |
commit | 96406689fa3be46b1e7f4193c2d82b0407f543ad (patch) | |
tree | 57eec75fe0888fd63025d56815759fb2c0e7fd71 | |
parent | fa979925d9afa58aab29a57f4289c0b679ac03e9 (diff) |
CAN: Improve error handling code
* Add Q_UNLIKELY to error handling branches
* Prefers qWarning("%ls", qUtf16Printable(str))
over qWarning() << str;
Task-number: QTBUG-55045
Change-Id: I3e49380bdec0edb2ea00e1b5e3421464b295aacc
Reviewed-by: Rolf Eike Beer <eb@emlix.com>
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r-- | src/plugins/canbus/peakcan/peakcanbackend.cpp | 40 | ||||
-rw-r--r-- | src/plugins/canbus/socketcan/socketcanbackend.cpp | 46 | ||||
-rw-r--r-- | src/plugins/canbus/tinycan/tinycanbackend.cpp | 44 | ||||
-rw-r--r-- | src/plugins/canbus/vectorcan/vectorcanbackend.cpp | 39 | ||||
-rw-r--r-- | src/serialbus/qcanbusdevice.cpp | 18 | ||||
-rw-r--r-- | src/serialbus/qcanbusframe.h | 4 | ||||
-rw-r--r-- | src/tools/canbusutil/readtask.cpp | 4 |
7 files changed, 99 insertions, 96 deletions
diff --git a/src/plugins/canbus/peakcan/peakcanbackend.cpp b/src/plugins/canbus/peakcan/peakcanbackend.cpp index f256d82..7d4e123 100644 --- a/src/plugins/canbus/peakcan/peakcanbackend.cpp +++ b/src/plugins/canbus/peakcan/peakcanbackend.cpp @@ -64,7 +64,7 @@ bool PeakCanBackend::canCreate(QString *errorReason) return true; #else static bool symbolsResolved = resolveSymbols(pcanLibrary()); - if (!symbolsResolved) { + if (Q_UNLIKELY(!symbolsResolved)) { *errorReason = pcanLibrary()->errorString(); return false; } @@ -259,7 +259,7 @@ bool PeakCanBackendPrivate::open() const int bitrateCode = bitrateCodeFromBitrate(bitrate); const TPCANStatus st = ::CAN_Initialize(channelIndex, bitrateCode, 0, 0, 0); - if (st != PCAN_ERROR_OK) { + if (Q_UNLIKELY(st != PCAN_ERROR_OK)) { q->setError(systemErrorString(st), QCanBusDevice::ConnectionError); return false; } @@ -275,7 +275,7 @@ bool PeakCanBackendPrivate::open() #endif const TPCANStatus err = ::CAN_SetValue(channelIndex, PCAN_RECEIVE_EVENT, &readHandle, sizeof(readHandle)); - if (err != PCAN_ERROR_OK) { + if (Q_UNLIKELY(err != PCAN_ERROR_OK)) { q->setError(systemErrorString(err), QCanBusDevice::ConnectionError); return false; } @@ -302,16 +302,16 @@ void PeakCanBackendPrivate::close() quint32 value = 0; const TPCANStatus err = ::CAN_SetValue(channelIndex, PCAN_RECEIVE_EVENT, &value, sizeof(value)); - if (err != PCAN_ERROR_OK) + if (Q_UNLIKELY(err != PCAN_ERROR_OK)) emit q->setError(systemErrorString(err), QCanBusDevice::ConnectionError); const TPCANStatus st = ::CAN_Uninitialize(channelIndex); - if (st != PCAN_ERROR_OK) + if (Q_UNLIKELY(st != PCAN_ERROR_OK)) q->setError(systemErrorString(st), QCanBusDevice::ConnectionError); #if defined(Q_OS_WIN32) if (readHandle && (readHandle != INVALID_HANDLE_VALUE)) { - if (!::CloseHandle(readHandle)) + if (Q_UNLIKELY(!::CloseHandle(readHandle))) q->setError(qt_error_string(::GetLastError()), QCanBusDevice::ConnectionError); readHandle = INVALID_HANDLE_VALUE; } @@ -355,7 +355,7 @@ void PeakCanBackendPrivate::setupDefaultConfigurations() QString PeakCanBackendPrivate::systemErrorString(int errorCode) { QByteArray buffer(256, 0); - if (::CAN_GetErrorText(errorCode, 0, buffer.data()) != PCAN_ERROR_OK) + if (Q_UNLIKELY(::CAN_GetErrorText(errorCode, 0, buffer.data()) != PCAN_ERROR_OK)) return PeakCanBackend::tr("Unable to retrieve an error string"); return QString::fromLatin1(buffer); } @@ -385,7 +385,7 @@ void PeakCanBackendPrivate::startWrite() ::memcpy(message.DATA, payload.constData(), sizeof(message.DATA)); const TPCANStatus st = ::CAN_Write(channelIndex, &message); - if (st != PCAN_ERROR_OK) + if (Q_UNLIKELY(st != PCAN_ERROR_OK)) q->setError(systemErrorString(st), QCanBusDevice::WriteError); else emit q->framesWritten(qint64(1)); @@ -408,7 +408,7 @@ void PeakCanBackendPrivate::startRead() const TPCANStatus st = ::CAN_Read(channelIndex, &message, ×tamp); if (st != PCAN_ERROR_OK) { - if (st != PCAN_ERROR_QRCVEMPTY) + if (Q_UNLIKELY(st != PCAN_ERROR_QRCVEMPTY)) q->setError(systemErrorString(st), QCanBusDevice::ReadError); break; } @@ -430,13 +430,13 @@ bool PeakCanBackendPrivate::verifyBitRate(int bitrate) { Q_Q(PeakCanBackend); - if (isOpen) { + if (Q_UNLIKELY(isOpen)) { q->setError(PeakCanBackend::tr("Impossible to reconfigure bitrate for the opened device"), QCanBusDevice::ConfigurationError); return false; } - if (bitrateCodeFromBitrate(bitrate) == -1) { + if (Q_UNLIKELY(bitrateCodeFromBitrate(bitrate) == -1)) { q->setError(PeakCanBackend::tr("Unsupported bitrate value"), QCanBusDevice::ConfigurationError); return false; @@ -470,7 +470,7 @@ bool PeakCanBackend::open() Q_D(PeakCanBackend); if (!d->isOpen) { - if (!d->open()) + if (Q_UNLIKELY(!d->open())) return false; // apply all stored configurations except bitrate, because @@ -481,9 +481,9 @@ bool PeakCanBackend::open() continue; const QVariant param = configurationParameter(key); const bool success = d->setConfigurationParameter(key, param); - if (!success) { - qWarning() << "Cannot apply parameter:" << key - << "with value:" << param; + if (Q_UNLIKELY(!success)) { + qWarning("Cannot apply parameter: %d with value: %ls.", + key, qUtf16Printable(param.toString())); } } } @@ -513,23 +513,23 @@ bool PeakCanBackend::writeFrame(const QCanBusFrame &newData) { Q_D(PeakCanBackend); - if (state() != QCanBusDevice::ConnectedState) + if (Q_UNLIKELY(state() != QCanBusDevice::ConnectedState)) return false; - if (!newData.isValid()) { + if (Q_UNLIKELY(!newData.isValid())) { setError(tr("Cannot write invalid QCanBusFrame"), QCanBusDevice::WriteError); return false; } - if (newData.frameType() != QCanBusFrame::DataFrame - && newData.frameType() != QCanBusFrame::RemoteRequestFrame) { + if (Q_UNLIKELY(newData.frameType() != QCanBusFrame::DataFrame + && newData.frameType() != QCanBusFrame::RemoteRequestFrame)) { setError(tr("Unable to write a frame with unacceptable type"), QCanBusDevice::WriteError); return false; } // CAN FD frame format not implemented at this stage - if (newData.payload().size() > 8) { + if (Q_UNLIKELY(newData.payload().size() > 8)) { setError(tr("CAN FD frame format not supported."), QCanBusDevice::WriteError); return false; } diff --git a/src/plugins/canbus/socketcan/socketcanbackend.cpp b/src/plugins/canbus/socketcan/socketcanbackend.cpp index 0790b9d..f65e42a 100644 --- a/src/plugins/canbus/socketcan/socketcanbackend.cpp +++ b/src/plugins/canbus/socketcan/socketcanbackend.cpp @@ -194,7 +194,8 @@ bool SocketCanBackend::applyConfigurationParameter(int key, const QVariant &valu case QCanBusDevice::LoopbackKey: { const int loopback = value.toBool() ? 1 : 0; - if (setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &loopback, sizeof(loopback)) < 0) { + if (Q_UNLIKELY(setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_LOOPBACK, + &loopback, sizeof(loopback)) < 0)) { setError(qt_error_string(errno), QCanBusDevice::CanBusError::ConfigurationError); break; @@ -205,8 +206,8 @@ bool SocketCanBackend::applyConfigurationParameter(int key, const QVariant &valu case QCanBusDevice::ReceiveOwnKey: { const int receiveOwnMessages = value.toBool() ? 1 : 0; - if (setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS, - &receiveOwnMessages, sizeof(receiveOwnMessages)) < 0) { + if (Q_UNLIKELY(setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS, + &receiveOwnMessages, sizeof(receiveOwnMessages)) < 0)) { setError(qt_error_string(errno), QCanBusDevice::CanBusError::ConfigurationError); break; @@ -217,8 +218,8 @@ bool SocketCanBackend::applyConfigurationParameter(int key, const QVariant &valu case QCanBusDevice::ErrorFilterKey: { const int errorMask = value.value<QCanBusFrame::FrameErrors>(); - if (setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, - &errorMask, sizeof(errorMask)) < 0) { + if (Q_UNLIKELY(setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, + &errorMask, sizeof(errorMask)) < 0)) { setError(qt_error_string(errno), QCanBusDevice::CanBusError::ConfigurationError); break; @@ -234,9 +235,9 @@ bool SocketCanBackend::applyConfigurationParameter(int key, const QVariant &valu // permit every frame - no restrictions (filter reset) can_filter filters = {0, 0}; socklen_t s = sizeof(can_filter); - if (setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_FILTER, - &filters, s) != 0) { - qWarning() << "Cannot unset socket filters"; + if (Q_UNLIKELY(setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_FILTER, + &filters, s) != 0)) { + qWarning("Cannot unset socket filters"); setError(qt_error_string(errno), QCanBusDevice::CanBusError::ConfigurationError); break; @@ -288,8 +289,8 @@ bool SocketCanBackend::applyConfigurationParameter(int key, const QVariant &valu filters[i] = filter; } - if (setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_FILTER, - filters.constData(), sizeof(filters[0]) * filters.size()) < 0) { + if (Q_UNLIKELY(setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_FILTER, + filters.constData(), sizeof(filters[0]) * filters.size()) < 0)) { setError(qt_error_string(errno), QCanBusDevice::CanBusError::ConfigurationError); break; @@ -300,7 +301,8 @@ bool SocketCanBackend::applyConfigurationParameter(int key, const QVariant &valu case QCanBusDevice::CanFdKey: { const int fd_frames = value.toBool() ? 1 : 0; - if (setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &fd_frames, sizeof(fd_frames)) < 0) { + if (Q_UNLIKELY(setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, + &fd_frames, sizeof(fd_frames)) < 0)) { setError(qt_error_string(errno), QCanBusDevice::CanBusError::ConfigurationError); break; @@ -323,14 +325,14 @@ bool SocketCanBackend::connectSocket() struct sockaddr_can address; struct ifreq interface; - if ((canSocket = socket(PF_CAN, SOCK_RAW | SOCK_NONBLOCK, CAN_RAW)) < 0) { + if (Q_UNLIKELY((canSocket = socket(PF_CAN, SOCK_RAW | SOCK_NONBLOCK, CAN_RAW)) < 0)) { setError(qt_error_string(errno), QCanBusDevice::CanBusError::ConnectionError); return false; } qstrncpy(interface.ifr_name, canSocketName.toLatin1().constData(), sizeof(interface.ifr_name)); - if (ioctl(canSocket, SIOCGIFINDEX, &interface) < 0) { + if (Q_UNLIKELY(ioctl(canSocket, SIOCGIFINDEX, &interface) < 0)) { setError(qt_error_string(errno), QCanBusDevice::CanBusError::ConnectionError); return false; @@ -339,7 +341,7 @@ bool SocketCanBackend::connectSocket() address.can_family = AF_CAN; address.can_ifindex = interface.ifr_ifindex; - if (bind(canSocket, reinterpret_cast<struct sockaddr *>(&address), sizeof(address)) < 0) { + if (Q_UNLIKELY(bind(canSocket, reinterpret_cast<struct sockaddr *>(&address), sizeof(address)) < 0)) { setError(qt_error_string(errno), QCanBusDevice::CanBusError::ConnectionError); return false; @@ -356,9 +358,9 @@ bool SocketCanBackend::connectSocket() for (int key : keys) { const QVariant param = configurationParameter(key); bool success = applyConfigurationParameter(key, param); - if (!success) { - qWarning() << "Cannot apply parameter:" << QCanBusDevice::ConfigurationKey(key) - << "with value:" << param; + if (Q_UNLIKELY(!success)) { + qWarning("Cannot apply parameter: %d with value: %ls", + key, qUtf16Printable(param.toString())); } } @@ -409,7 +411,7 @@ bool SocketCanBackend::writeFrame(const QCanBusFrame &newData) if (state() != ConnectedState) return false; - if (!newData.isValid()) { + if (Q_UNLIKELY(!newData.isValid())) { setError(tr("Cannot write invalid QCanBusFrame"), QCanBusDevice::WriteError); return false; } @@ -460,7 +462,7 @@ bool SocketCanBackend::writeFrame(const QCanBusFrame &newData) bytesWritten = ::write(canSocket, &frame, sizeof(frame)); } - if (bytesWritten < 0) { + if (Q_UNLIKELY(bytesWritten < 0)) { setError(qt_error_string(errno), QCanBusDevice::CanBusError::WriteError); return false; @@ -646,18 +648,18 @@ void SocketCanBackend::readSocket() if (bytesReceived <= 0) { break; - } else if (bytesReceived != CANFD_MTU && bytesReceived != CAN_MTU) { + } else if (Q_UNLIKELY(bytesReceived != CANFD_MTU && bytesReceived != CAN_MTU)) { setError(tr("ERROR SocketCanBackend: incomplete CAN frame"), QCanBusDevice::CanBusError::ReadError); continue; - } else if (frame.len > bytesReceived - offsetof(canfd_frame, data)) { + } else if (Q_UNLIKELY(frame.len > bytesReceived - offsetof(canfd_frame, data))) { setError(tr("ERROR SocketCanBackend: invalid CAN frame length"), QCanBusDevice::CanBusError::ReadError); continue; } struct timeval timeStamp; - if (ioctl(canSocket, SIOCGSTAMP, &timeStamp) < 0) { + if (Q_UNLIKELY(ioctl(canSocket, SIOCGSTAMP, &timeStamp) < 0)) { setError(qt_error_string(errno), QCanBusDevice::CanBusError::ReadError); memset(&timeStamp, 0, sizeof(timeStamp)); diff --git a/src/plugins/canbus/tinycan/tinycanbackend.cpp b/src/plugins/canbus/tinycan/tinycanbackend.cpp index af9e3be..cea1cd6 100644 --- a/src/plugins/canbus/tinycan/tinycanbackend.cpp +++ b/src/plugins/canbus/tinycan/tinycanbackend.cpp @@ -60,7 +60,7 @@ bool TinyCanBackend::canCreate(QString *errorReason) return true; #else static bool symbolsResolved = resolveSymbols(mhstcanLibrary()); - if (!symbolsResolved) { + if (Q_UNLIKELY(!symbolsResolved)) { *errorReason = mhstcanLibrary()->errorString(); return false; } @@ -176,7 +176,7 @@ bool TinyCanBackendPrivate::open() { char options[] = "AutoConnect=1;AutoReopen=0"; const int ret = ::CanSetOptions(options); - if (ret < 0) { + if (Q_UNLIKELY(ret < 0)) { q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ConnectionError); return false; } @@ -184,7 +184,7 @@ bool TinyCanBackendPrivate::open() { const int ret = ::CanDeviceOpen(channelIndex, nullptr); - if (ret < 0) { + if (Q_UNLIKELY(ret < 0)) { q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ConnectionError); return false; } @@ -192,7 +192,7 @@ bool TinyCanBackendPrivate::open() { const int ret = ::CanSetMode(channelIndex, OP_CAN_START, CAN_CMD_ALL_CLEAR); - if (ret < 0) { + if (Q_UNLIKELY(ret < 0)) { q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ConnectionError); ::CanDeviceClose(channelIndex); return false; @@ -214,7 +214,7 @@ void TinyCanBackendPrivate::close() writeNotifier = nullptr; const int ret = ::CanDeviceClose(channelIndex); - if (ret < 0) + if (Q_UNLIKELY(ret < 0)) q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ConnectionError); isOpen = false; @@ -343,8 +343,8 @@ void TinyCanBackendPrivate::startWrite() TCanMsg message; ::memset(&message, 0, sizeof(message)); - if (payload.size() > int(sizeof(message.Data.Bytes))) { - qWarning("Impossible to write the message with unacceptable data size %d, ignored", payload.size()); + if (Q_UNLIKELY(payload.size() > int(sizeof(message.Data.Bytes)))) { + qWarning("Can not write frame with payload size %d, ignored", payload.size()); } else { message.Id = frame.frameId(); message.Flags.Flag.Len = payload.size(); @@ -356,7 +356,7 @@ void TinyCanBackendPrivate::startWrite() const qint32 messagesToWrite = 1; ::memcpy(message.Data.Bytes, payload.constData(), sizeof(message.Data.Bytes)); const int ret = ::CanTransmit(channelIndex, &message, messagesToWrite); - if (ret < 0) + if (Q_UNLIKELY(ret < 0)) q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::WriteError); else emit q->framesWritten(messagesToWrite); @@ -382,7 +382,7 @@ void TinyCanBackendPrivate::startRead() const int messagesToRead = 1; const int ret = ::CanReceive(channelIndex, &message, messagesToRead); - if (ret < 0) { + if (Q_UNLIKELY(ret < 0)) { q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ReadError); TDeviceStatus status; @@ -425,7 +425,7 @@ void TinyCanBackendPrivate::startupDriver() if (driverRefCount == 0) { const int ret = ::CanInitDriver(nullptr); - if (ret < 0) { + if (Q_UNLIKELY(ret < 0)) { q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ConnectionError); return; } @@ -433,7 +433,7 @@ void TinyCanBackendPrivate::startupDriver() ::CanSetRxEventCallback(&canRxEventCallback); ::CanSetEvents(EVENT_ENABLE_RX_MESSAGES); - } else if (driverRefCount < 0) { + } else if (Q_UNLIKELY(driverRefCount < 0)) { qCritical("Wrong reference counter: %d", driverRefCount); return; } @@ -445,7 +445,7 @@ void TinyCanBackendPrivate::cleanupDriver() { --driverRefCount; - if (driverRefCount < 0) { + if (Q_UNLIKELY(driverRefCount < 0)) { qCritical("Wrong reference counter: %d", driverRefCount); driverRefCount = 0; } else if (driverRefCount == 0) { @@ -459,7 +459,7 @@ bool TinyCanBackendPrivate::setBitRate(int bitrate) Q_Q(TinyCanBackend); const int bitrateCode = bitrateCodeFromBitrate(bitrate); - if (bitrateCode == -1) { + if (Q_UNLIKELY(bitrateCode == -1)) { q->setError(TinyCanBackend::tr("Unsupported bitrate value"), QCanBusDevice::ConfigurationError); return false; @@ -467,7 +467,7 @@ bool TinyCanBackendPrivate::setBitRate(int bitrate) if (isOpen) { const int ret = ::CanSetSpeed(channelIndex, bitrateCode); - if (ret < 0) { + if (Q_UNLIKELY(ret < 0)) { q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ConfigurationError); return false; } @@ -507,9 +507,9 @@ bool TinyCanBackend::open() for (int key : keys) { const QVariant param = configurationParameter(key); const bool success = d->setConfigurationParameter(key, param); - if (!success) { - qWarning() << "Cannot apply parameter:" << key - << "with value:" << param; + if (Q_UNLIKELY(!success)) { + qWarning("Cannot apply parameter: %d with value: %ls", + key, qUtf16Printable(param.toString())); } } } @@ -539,24 +539,24 @@ bool TinyCanBackend::writeFrame(const QCanBusFrame &newData) { Q_D(TinyCanBackend); - if (state() != QCanBusDevice::ConnectedState) + if (Q_UNLIKELY(state() != QCanBusDevice::ConnectedState)) return false; - if (!newData.isValid()) { + if (Q_UNLIKELY(!newData.isValid())) { setError(tr("Cannot write invalid QCanBusFrame"), QCanBusDevice::WriteError); return false; } - if (newData.frameType() != QCanBusFrame::DataFrame + if (Q_UNLIKELY(newData.frameType() != QCanBusFrame::DataFrame && newData.frameType() != QCanBusFrame::RemoteRequestFrame - && newData.frameType() != QCanBusFrame::ErrorFrame) { + && newData.frameType() != QCanBusFrame::ErrorFrame)) { setError(tr("Unable to write a frame with unacceptable type"), QCanBusDevice::WriteError); return false; } // CAN FD frame format not supported at this stage - if (newData.payload().size() > 8) { + if (Q_UNLIKELY(newData.payload().size() > 8)) { setError(tr("CAN FD frame format not supported."), QCanBusDevice::WriteError); return false; } diff --git a/src/plugins/canbus/vectorcan/vectorcanbackend.cpp b/src/plugins/canbus/vectorcan/vectorcanbackend.cpp index e8b3b91..f1035e4 100644 --- a/src/plugins/canbus/vectorcan/vectorcanbackend.cpp +++ b/src/plugins/canbus/vectorcan/vectorcanbackend.cpp @@ -59,7 +59,7 @@ bool VectorCanBackend::canCreate(QString *errorReason) return true; #else static bool symbolsResolved = resolveSymbols(vectorcanLibrary()); - if (!symbolsResolved) { + if (Q_UNLIKELY(!symbolsResolved)) { *errorReason = vectorcanLibrary()->errorString(); return false; } @@ -169,7 +169,7 @@ bool VectorCanBackendPrivate::open() channelMask, &permissionMask, queueSize, XL_INTERFACE_VERSION, XL_BUS_TYPE_CAN); - if (status != XL_SUCCESS || portHandle == XL_INVALID_PORTHANDLE) { + if (Q_UNLIKELY(status != XL_SUCCESS || portHandle == XL_INVALID_PORTHANDLE)) { q->setError(systemErrorString(status), QCanBusDevice::ConnectionError); return false; @@ -179,7 +179,7 @@ bool VectorCanBackendPrivate::open() { const XLstatus status = ::xlActivateChannel(portHandle, channelMask, XL_BUS_TYPE_CAN, XL_ACTIVATE_RESET_CLOCK); - if (status != XL_SUCCESS) { + if (Q_UNLIKELY(status != XL_SUCCESS)) { q->setError(systemErrorString(status), QCanBusDevice::CanBusError::ConnectionError); return false; @@ -189,7 +189,7 @@ bool VectorCanBackendPrivate::open() { const int queueLevel = 1; const XLstatus status = ::xlSetNotification(portHandle, &readHandle, queueLevel); - if (status != XL_SUCCESS) { + if (Q_UNLIKELY(status != XL_SUCCESS)) { q->setError(systemErrorString(status), QCanBusDevice::ConnectionError); return false; @@ -216,7 +216,7 @@ void VectorCanBackendPrivate::close() { const XLstatus status = ::xlDeactivateChannel(portHandle, channelMask); - if (status != XL_SUCCESS) { + if (Q_UNLIKELY(status != XL_SUCCESS)) { q->setError(systemErrorString(status), QCanBusDevice::CanBusError::ConfigurationError); } @@ -224,7 +224,7 @@ void VectorCanBackendPrivate::close() { const XLstatus status = ::xlClosePort(portHandle); - if (status != XL_SUCCESS) { + if (Q_UNLIKELY(status != XL_SUCCESS)) { q->setError(systemErrorString(status), QCanBusDevice::ConnectionError); } @@ -249,7 +249,7 @@ bool VectorCanBackendPrivate::setConfigurationParameter(int key, const QVariant void VectorCanBackendPrivate::setupChannel(const QString &interfaceName) { - if (interfaceName.startsWith(QStringLiteral("can"))) { + if (Q_LIKELY(interfaceName.startsWith(QStringLiteral("can")))) { const QStringRef ref = interfaceName.midRef(3); bool ok = false; const int channelIndex = ref.toInt(&ok); @@ -259,7 +259,7 @@ void VectorCanBackendPrivate::setupChannel(const QString &interfaceName) } } - qCritical() << "Unable to parse the channel from an interface"; + qCritical("Unable to parse the channel %ls", qUtf16Printable(interfaceName)); } void VectorCanBackendPrivate::setupDefaultConfigurations() @@ -271,7 +271,8 @@ void VectorCanBackendPrivate::setupDefaultConfigurations() QString VectorCanBackendPrivate::systemErrorString(int errorCode) const { - if (const char *string = ::xlGetErrorString(errorCode)) + const char *string = ::xlGetErrorString(errorCode); + if (Q_LIKELY(string)) return QString::fromUtf8(string); return VectorCanBackend::tr("Unable to retrieve an error string"); } @@ -311,7 +312,7 @@ void VectorCanBackendPrivate::startWrite() quint32 eventCount = 1; const XLstatus status = ::xlCanTransmit(portHandle, channelMask, &eventCount, &event); - if (status != XL_SUCCESS) { + if (Q_UNLIKELY(status != XL_SUCCESS)) { q->setError(systemErrorString(status), QCanBusDevice::WriteError); } else { @@ -334,7 +335,7 @@ void VectorCanBackendPrivate::startRead() ::memset(&event, 0, sizeof(event)); const XLstatus status = ::xlReceive(portHandle, &eventCount, &event); - if (status != XL_SUCCESS) { + if (Q_UNLIKELY(status != XL_SUCCESS)) { if (status != XL_ERR_QUEUE_IS_EMPTY) { q->setError(systemErrorString(status), QCanBusDevice::ReadError); @@ -394,7 +395,7 @@ void VectorCanBackendPrivate::cleanupDriver() { --driverRefCount; - if (driverRefCount < 0) { + if (Q_UNLIKELY(driverRefCount < 0)) { qCritical("Wrong reference counter: %d", driverRefCount); driverRefCount = 0; } else if (driverRefCount == 0) { @@ -408,7 +409,7 @@ bool VectorCanBackendPrivate::setBitRate(quint32 bitrate) if (q->state() != QCanBusDevice::UnconnectedState) { const XLstatus status = ::xlCanSetChannelBitrate(portHandle, channelMask, bitrate); - if (status != XL_SUCCESS) { + if (Q_UNLIKELY(status != XL_SUCCESS)) { q->setError(systemErrorString(status), QCanBusDevice::CanBusError::ConfigurationError); return false; @@ -450,8 +451,8 @@ bool VectorCanBackend::open() const QVariant param = configurationParameter(key); const bool success = d->setConfigurationParameter(key, param); if (!success) { - qWarning() << "Cannot apply parameter:" << key - << "with value:" << param; + qWarning("Cannot apply parameter: %d with value: %ls", + key, param.toString()); } } @@ -483,22 +484,22 @@ bool VectorCanBackend::writeFrame(const QCanBusFrame &newData) if (state() != QCanBusDevice::ConnectedState) return false; - if (!newData.isValid()) { + if (Q_UNLIKELY(!newData.isValid())) { setError(tr("Cannot write invalid QCanBusFrame"), QCanBusDevice::WriteError); return false; } - if (newData.frameType() != QCanBusFrame::DataFrame + if (Q_UNLIKELY(newData.frameType() != QCanBusFrame::DataFrame && newData.frameType() != QCanBusFrame::RemoteRequestFrame - && newData.frameType() != QCanBusFrame::ErrorFrame) { + && newData.frameType() != QCanBusFrame::ErrorFrame)) { setError(tr("Unable to write a frame with unacceptable type"), QCanBusDevice::WriteError); return false; } // CAN FD frame format not implemented at this stage - if (newData.payload().size() > MAX_MSG_LEN) { + if (Q_UNLIKELY(newData.payload().size() > MAX_MSG_LEN)) { setError(tr("CAN FD frame format not supported."), QCanBusDevice::WriteError); return false; diff --git a/src/serialbus/qcanbusdevice.cpp b/src/serialbus/qcanbusdevice.cpp index 1acf98a..1475732 100644 --- a/src/serialbus/qcanbusdevice.cpp +++ b/src/serialbus/qcanbusdevice.cpp @@ -251,7 +251,7 @@ void QCanBusDevice::enqueueReceivedFrames(const QVector<QCanBusFrame> &newFrames { Q_D(QCanBusDevice); - if (newFrames.isEmpty()) + if (Q_UNLIKELY(newFrames.isEmpty())) return; d->incomingFramesGuard.lock(); @@ -282,7 +282,7 @@ QCanBusFrame QCanBusDevice::dequeueOutgoingFrame() { Q_D(QCanBusDevice); - if (d->outgoingFrames.isEmpty()) + if (Q_UNLIKELY(d->outgoingFrames.isEmpty())) return QCanBusFrame(QCanBusFrame::InvalidFrame); return d->outgoingFrames.takeFirst(); } @@ -439,7 +439,7 @@ qint64 QCanBusDevice::framesToWrite() const bool QCanBusDevice::waitForFramesWritten(int msecs) { // do not enter this function recursively - if (d_func()->waitForWrittenEntered) { + if (Q_UNLIKELY(d_func()->waitForWrittenEntered)) { qWarning("QCanBusDevice::waitForFramesWritten() must not be called " "recursively. Check that no slot containing waitForFramesReceived() " "is called in response to framesWritten(qint64) or errorOccurred(CanBusError)" @@ -492,7 +492,7 @@ bool QCanBusDevice::waitForFramesWritten(int msecs) bool QCanBusDevice::waitForFramesReceived(int msecs) { // do not enter this function recursively - if (d_func()->waitForReceivedEntered) { + if (Q_UNLIKELY(d_func()->waitForReceivedEntered)) { qWarning("QCanBusDevice::waitForFramesReceived() must not be called " "recursively. Check that no slot containing waitForFramesReceived() " "is called in response to framesReceived() or errorOccurred(CanBusError) " @@ -571,12 +571,12 @@ QCanBusFrame QCanBusDevice::readFrame() { Q_D(QCanBusDevice); - if (d->state != ConnectedState) + if (Q_UNLIKELY(d->state != ConnectedState)) return QCanBusFrame(QCanBusFrame::InvalidFrame); QMutexLocker locker(&d->incomingFramesGuard); - if (d->incomingFrames.isEmpty()) + if (Q_UNLIKELY(d->incomingFrames.isEmpty())) return QCanBusFrame(QCanBusFrame::InvalidFrame); return d->incomingFrames.takeFirst(); @@ -631,7 +631,7 @@ bool QCanBusDevice::connectDevice() { Q_D(QCanBusDevice); - if (d->state != QCanBusDevice::UnconnectedState) { + if (Q_UNLIKELY(d->state != QCanBusDevice::UnconnectedState)) { setError(tr("Can not connect an already connected device"), QCanBusDevice::ConnectionError); return false; @@ -658,8 +658,8 @@ void QCanBusDevice::disconnectDevice() { Q_D(QCanBusDevice); - if (d->state == QCanBusDevice::UnconnectedState - || d->state == QCanBusDevice::ClosingState) { + if (Q_UNLIKELY(d->state == QCanBusDevice::UnconnectedState + || d->state == QCanBusDevice::ClosingState)) { qWarning("Can not disconnect an unconnected device"); return; } diff --git a/src/serialbus/qcanbusframe.h b/src/serialbus/qcanbusframe.h index 7cab3e9..9ca766b 100644 --- a/src/serialbus/qcanbusframe.h +++ b/src/serialbus/qcanbusframe.h @@ -181,13 +181,13 @@ public: quint32 frameId() const Q_DECL_NOTHROW { - if (format == ErrorFrame) + if (Q_UNLIKELY(format == ErrorFrame)) return 0; return (canId & 0x1FFFFFFFU); } void setFrameId(quint32 newFrameId) { - if (newFrameId < 0x20000000U) { + if (Q_LIKELY(newFrameId < 0x20000000U)) { isValidFrameId = true; canId = newFrameId; setExtendedFrameFormat(isExtendedFrame || (newFrameId & 0x1FFFF800U)); diff --git a/src/tools/canbusutil/readtask.cpp b/src/tools/canbusutil/readtask.cpp index 90858c2..93fcae1 100644 --- a/src/tools/canbusutil/readtask.cpp +++ b/src/tools/canbusutil/readtask.cpp @@ -48,7 +48,7 @@ void ReadTask::setShowTimeStamp(bool showTimeStamp) void ReadTask::checkMessages() { auto canDevice = qobject_cast<QCanBusDevice *>(QObject::sender()); if (canDevice == nullptr) { - qWarning() << "ReadTask::checkMessages: Unknown sender"; + qWarning("ReadTask::checkMessages: Unknown sender"); return; } @@ -75,7 +75,7 @@ void ReadTask::checkMessages() { void ReadTask::receiveError(QCanBusDevice::CanBusError /*error*/) { auto canDevice = qobject_cast<QCanBusDevice *>(QObject::sender()); if (canDevice == nullptr) { - qWarning() << "ReadTask::receiveError: Unknown sender"; + qWarning("ReadTask::receiveError: Unknown sender"); return; } |