From 8919ea86c80001be7e826f5363f1b7548c7a8091 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Fri, 26 Jun 2015 16:40:44 +0300 Subject: Improve the processing of errors Sometimes the error string would contain a wrong description which did not correspond to the system error code. The reason was qt_error_string() being called too late, when the system error might have already been overwritten. The error processing is now in QSPP::getSystemError(), which returns both the error code and the error description as soon as possible. * Now the QSPP::getSystemError() returns the new class QSerialPortErrorInfo which contains all necessary fields. * The new method QSPP::setError() which accepts the QSerialPortErrorInfo as input parameter is used. * The old private method QSP::setError() is removed, because it is not used anywhere. Change-Id: Ia7e4d617b863e2131175c52812cdf426ed963795 Reviewed-by: Sergey Belyashov Reviewed-by: Oswald Buddenhagen Reviewed-by: Denis Shienkov --- src/serialport/qserialport.cpp | 48 +++++----- src/serialport/qserialport.h | 2 - src/serialport/qserialport_p.h | 23 ++++- src/serialport/qserialport_unix.cpp | 170 +++++++++++++++-------------------- src/serialport/qserialport_win.cpp | 154 +++++++++++++------------------ src/serialport/qserialport_wince.cpp | 99 +++++++++----------- 6 files changed, 220 insertions(+), 276 deletions(-) diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp index 1aea7ca4..b6c4252b 100644 --- a/src/serialport/qserialport.cpp +++ b/src/serialport/qserialport.cpp @@ -97,6 +97,15 @@ int QSerialPortPrivate::timeoutValue(int msecs, int elapsed) return qMax(msecs, 0); } +void QSerialPortPrivate::setError(const QSerialPortErrorInfo &errorInfo) +{ + Q_Q(QSerialPort); + + error = errorInfo.errorCode; + q->setErrorString(errorInfo.errorString); + emit q->error(error); +} + /*! \class QSerialPort @@ -512,14 +521,14 @@ bool QSerialPort::open(OpenMode mode) Q_D(QSerialPort); if (isOpen()) { - setError(QSerialPort::OpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::OpenError)); return false; } // Define while not supported modes. static const OpenMode unsupportedModes = Append | Truncate | Text | Unbuffered; if ((mode & unsupportedModes) || mode == NotOpen) { - setError(QSerialPort::UnsupportedOperationError); + d->setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError)); return false; } @@ -552,7 +561,7 @@ void QSerialPort::close() { Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); return; } @@ -862,7 +871,7 @@ bool QSerialPort::setDataTerminalReady(bool set) Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -909,7 +918,7 @@ bool QSerialPort::setRequestToSend(bool set) Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -959,7 +968,7 @@ QSerialPort::PinoutSignals QSerialPort::pinoutSignals() Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return QSerialPort::NoSignal; } @@ -989,7 +998,7 @@ bool QSerialPort::flush() Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1011,7 +1020,7 @@ bool QSerialPort::clear(Directions directions) Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1073,7 +1082,7 @@ bool QSerialPort::setDataErrorPolicy(DataErrorPolicy policy) Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1124,7 +1133,8 @@ QSerialPort::SerialPortError QSerialPort::error() const void QSerialPort::clearError() { - setError(QSerialPort::NoError); + Q_D(QSerialPort); + d->setError(QSerialPortErrorInfo(QSerialPort::NoError)); } /*! @@ -1294,7 +1304,7 @@ bool QSerialPort::sendBreak(int duration) Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1323,7 +1333,7 @@ bool QSerialPort::setBreakEnabled(bool set) Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1387,20 +1397,6 @@ qint64 QSerialPort::writeData(const char *data, qint64 maxSize) return d->writeData(data, maxSize); } -void QSerialPort::setError(QSerialPort::SerialPortError serialPortError, const QString &errorString) -{ - Q_D(QSerialPort); - - d->error = serialPortError; - - if (errorString.isNull() && (serialPortError != QSerialPort::NoError)) - setErrorString(qt_error_string(-1)); - else - setErrorString(errorString); - - emit error(serialPortError); -} - #include "moc_qserialport.cpp" QT_END_NAMESPACE diff --git a/src/serialport/qserialport.h b/src/serialport/qserialport.h index 7531e471..36ae788f 100644 --- a/src/serialport/qserialport.h +++ b/src/serialport/qserialport.h @@ -276,8 +276,6 @@ protected: qint64 writeData(const char *data, qint64 maxSize) Q_DECL_OVERRIDE; private: - void setError(QSerialPort::SerialPortError error, const QString &errorString = QString()); - // ### Qt6: remove me. QSerialPortPrivate * const d_dummy; diff --git a/src/serialport/qserialport_p.h b/src/serialport/qserialport_p.h index a39bc222..571b0de9 100644 --- a/src/serialport/qserialport_p.h +++ b/src/serialport/qserialport_p.h @@ -106,6 +106,19 @@ class QSocketNotifier; QString serialPortLockFilePath(const QString &portName); #endif +class QSerialPortErrorInfo +{ +public: + explicit QSerialPortErrorInfo(QSerialPort::SerialPortError errorCode = QSerialPort::UnknownError, + const QString &errorString = QString()) + : errorCode(errorCode) + , errorString(errorString) + { + } + QSerialPort::SerialPortError errorCode; + QString errorString; +}; + class QSerialPortPrivate : public QIODevicePrivate { Q_DECLARE_PUBLIC(QSerialPort) @@ -144,7 +157,9 @@ public: bool setFlowControl(QSerialPort::FlowControl flowControl); bool setDataErrorPolicy(QSerialPort::DataErrorPolicy policy); - QSerialPort::SerialPortError decodeSystemError(int systemErrorCode = -1) const; + QSerialPortErrorInfo getSystemError(int systemErrorCode = -1) const; + + void setError(const QSerialPortErrorInfo &errorInfo); qint64 writeData(const char *data, qint64 maxSize); @@ -237,11 +252,11 @@ public: bool initialize(QIODevice::OpenMode mode); bool updateTermios(); - QSerialPort::SerialPortError setBaudRate_helper(qint32 baudRate, + QSerialPortErrorInfo setBaudRate_helper(qint32 baudRate, QSerialPort::Directions directions); - QSerialPort::SerialPortError setCustomBaudRate(qint32 baudRate, + QSerialPortErrorInfo setCustomBaudRate(qint32 baudRate, QSerialPort::Directions directions); - QSerialPort::SerialPortError setStandardBaudRate(qint32 baudRate, + QSerialPortErrorInfo setStandardBaudRate(qint32 baudRate, QSerialPort::Directions directions); bool isReadNotificationEnabled() const; diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp index d2c0eca2..a80d6b34 100644 --- a/src/serialport/qserialport_unix.cpp +++ b/src/serialport/qserialport_unix.cpp @@ -158,20 +158,18 @@ private: bool QSerialPortPrivate::open(QIODevice::OpenMode mode) { - Q_Q(QSerialPort); - QString lockFilePath = serialPortLockFilePath(QSerialPortInfoPrivate::portNameFromSystemLocation(systemLocation)); bool isLockFileEmpty = lockFilePath.isEmpty(); if (isLockFileEmpty) { qWarning("Failed to create a lock file for opening the device"); - q->setError(QSerialPort::PermissionError); + setError(QSerialPortErrorInfo(QSerialPort::PermissionError)); return false; } QScopedPointer newLockFileScopedPointer(new QLockFile(lockFilePath)); if (!newLockFileScopedPointer->tryLock()) { - q->setError(QSerialPort::PermissionError); + setError(QSerialPortErrorInfo(QSerialPort::PermissionError)); return false; } @@ -192,7 +190,7 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode) descriptor = qt_safe_open(systemLocation.toLocal8Bit().constData(), flags); if (descriptor == -1) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -208,16 +206,14 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode) void QSerialPortPrivate::close() { - Q_Q(QSerialPort); - if (settingsRestoredOnClose) { if (::tcsetattr(descriptor, TCSANOW, &restoredTermios) == -1) - q->setError(decodeSystemError()); + setError(getSystemError()); } #ifdef TIOCNXCL if (::ioctl(descriptor, TIOCNXCL) == -1) - q->setError(decodeSystemError()); + setError(getSystemError()); #endif if (readNotifier) { @@ -233,7 +229,7 @@ void QSerialPortPrivate::close() } if (qt_safe_close(descriptor) == -1) - q->setError(decodeSystemError()); + setError(getSystemError()); lockFileScopedPointer.reset(Q_NULLPTR); @@ -244,12 +240,10 @@ void QSerialPortPrivate::close() QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals() { - Q_Q(QSerialPort); - int arg = 0; if (::ioctl(descriptor, TIOCMGET, &arg) == -1) { - q->setError(decodeSystemError()); + setError(getSystemError()); return QSerialPort::NoSignal; } @@ -303,11 +297,9 @@ QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals() bool QSerialPortPrivate::setDataTerminalReady(bool set) { - Q_Q(QSerialPort); - int status = TIOCM_DTR; if (::ioctl(descriptor, set ? TIOCMBIS : TIOCMBIC, &status) == -1) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -316,11 +308,9 @@ bool QSerialPortPrivate::setDataTerminalReady(bool set) bool QSerialPortPrivate::setRequestToSend(bool set) { - Q_Q(QSerialPort); - int status = TIOCM_RTS; if (::ioctl(descriptor, set ? TIOCMBIS : TIOCMBIC, &status) == -1) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -334,11 +324,9 @@ bool QSerialPortPrivate::flush() bool QSerialPortPrivate::clear(QSerialPort::Directions directions) { - Q_Q(QSerialPort); - if (::tcflush(descriptor, (directions == QSerialPort::AllDirections) ? TCIOFLUSH : (directions & QSerialPort::Input) ? TCIFLUSH : TCOFLUSH) == -1) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -347,10 +335,8 @@ bool QSerialPortPrivate::clear(QSerialPort::Directions directions) bool QSerialPortPrivate::sendBreak(int duration) { - Q_Q(QSerialPort); - if (::tcsendbreak(descriptor, duration) == -1) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -359,10 +345,8 @@ bool QSerialPortPrivate::sendBreak(int duration) bool QSerialPortPrivate::setBreakEnabled(bool set) { - Q_Q(QSerialPort); - if (::ioctl(descriptor, set ? TIOCSBRK : TIOCCBRK) == -1) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -425,22 +409,22 @@ bool QSerialPortPrivate::setBaudRate() && setBaudRate(outputBaudRate, QSerialPort::Output)); } -QSerialPort::SerialPortError +QSerialPortErrorInfo QSerialPortPrivate::setBaudRate_helper(qint32 baudRate, QSerialPort::Directions directions) { if ((directions & QSerialPort::Input) && ::cfsetispeed(¤tTermios, baudRate) < 0) - return decodeSystemError(); + return getSystemError(); if ((directions & QSerialPort::Output) && ::cfsetospeed(¤tTermios, baudRate) < 0) - return decodeSystemError(); + return getSystemError(); - return QSerialPort::NoError; + return QSerialPortErrorInfo(QSerialPort::NoError); } #if defined(Q_OS_LINUX) -QSerialPort::SerialPortError +QSerialPortErrorInfo QSerialPortPrivate::setStandardBaudRate(qint32 baudRate, QSerialPort::Directions directions) { struct serial_struct currentSerialInfo; @@ -452,7 +436,7 @@ QSerialPortPrivate::setStandardBaudRate(qint32 baudRate, QSerialPort::Directions currentSerialInfo.flags &= ~ASYNC_SPD_CUST; currentSerialInfo.custom_divisor = 0; if (::ioctl(descriptor, TIOCSSERIAL, ¤tSerialInfo) == -1) - return decodeSystemError(); + return getSystemError(); } return setBaudRate_helper(baudRate, directions); @@ -460,7 +444,7 @@ QSerialPortPrivate::setStandardBaudRate(qint32 baudRate, QSerialPort::Directions #else -QSerialPort::SerialPortError +QSerialPortErrorInfo QSerialPortPrivate::setStandardBaudRate(qint32 baudRate, QSerialPort::Directions directions) { return setBaudRate_helper(baudRate, directions); @@ -470,7 +454,7 @@ QSerialPortPrivate::setStandardBaudRate(qint32 baudRate, QSerialPort::Directions #if defined(Q_OS_LINUX) -QSerialPort::SerialPortError +QSerialPortErrorInfo QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions directions) { Q_UNUSED(directions); @@ -480,14 +464,14 @@ QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions d ::memset(¤tSerialInfo, 0, sizeof(currentSerialInfo)); if (::ioctl(descriptor, TIOCGSERIAL, ¤tSerialInfo) == -1) - return decodeSystemError(); + return getSystemError(); currentSerialInfo.flags &= ~ASYNC_SPD_MASK; currentSerialInfo.flags |= (ASYNC_SPD_CUST /* | ASYNC_LOW_LATENCY*/); currentSerialInfo.custom_divisor = currentSerialInfo.baud_base / baudRate; if (currentSerialInfo.custom_divisor == 0) - return QSerialPort::UnsupportedOperationError; + return QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError); if (currentSerialInfo.custom_divisor * baudRate != currentSerialInfo.baud_base) { qWarning("Baud rate of serial port %s is set to %d instead of %d: divisor %f unsupported", @@ -497,31 +481,31 @@ QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions d } if (::ioctl(descriptor, TIOCSSERIAL, ¤tSerialInfo) == -1) - return decodeSystemError(); + return getSystemError(); return setBaudRate_helper(B38400, directions); } #elif defined(Q_OS_MAC) -QSerialPort::SerialPortError +QSerialPortErrorInfo QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions directions) { Q_UNUSED(directions); #if defined(MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4) if (::ioctl(descriptor, IOSSIOSPEED, &baudRate) == -1) - return decodeSystemError(); + return getSystemError(); - return QSerialPort::NoError; + return QSerialPortErrorInfo(QSerialPort::NoError); #endif - return QSerialPort::UnsupportedOperationError; + return QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError); } #elif defined(Q_OS_QNX) -QSerialPort::SerialPortError +QSerialPortErrorInfo QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions directions) { // On QNX, the values of the 'Bxxxx' constants are set to 'xxxx' (i.e. @@ -533,36 +517,34 @@ QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions d #else -QSerialPort::SerialPortError +QSerialPortErrorInfo QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions directions) { Q_UNUSED(baudRate); Q_UNUSED(directions); - return QSerialPort::UnsupportedOperationError; + return QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError); } #endif bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions directions) { - Q_Q(QSerialPort); - if (baudRate <= 0) { - q->setError(QSerialPort::UnsupportedOperationError); + setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError)); return false; } const qint32 unixBaudRate = QSerialPortPrivate::settingFromBaudRate(baudRate); - const QSerialPort::SerialPortError error = (unixBaudRate > 0) + const QSerialPortErrorInfo error = (unixBaudRate > 0) ? setStandardBaudRate(unixBaudRate, directions) : setCustomBaudRate(baudRate, directions); - if (error == QSerialPort::NoError) + if (error.errorCode == QSerialPort::NoError) return updateTermios(); - q->setError(error); + setError(error); return false; } @@ -719,12 +701,12 @@ bool QSerialPortPrivate::readNotification() const qint64 readBytes = readFromPort(ptr, bytesToRead); if (readBytes <= 0) { - QSerialPort::SerialPortError error = decodeSystemError(); - if (error != QSerialPort::ResourceError) - error = QSerialPort::ReadError; + QSerialPortErrorInfo error = getSystemError(); + if (error.errorCode != QSerialPort::ResourceError) + error.errorCode = QSerialPort::ReadError; else setReadNotificationEnabled(false); - q->setError(error); + setError(error); buffer.chop(bytesToRead); return false; } @@ -751,18 +733,16 @@ bool QSerialPortPrivate::readNotification() bool QSerialPortPrivate::startAsyncWrite() { - Q_Q(QSerialPort); - if (writeBuffer.isEmpty() || writeSequenceStarted) return true; // Attempt to write it all in one chunk. qint64 written = writeToPort(writeBuffer.readPointer(), writeBuffer.nextDataBlockSize()); if (written < 0) { - QSerialPort::SerialPortError error = decodeSystemError(); - if (error != QSerialPort::ResourceError) - error = QSerialPort::WriteError; - q->setError(error); + QSerialPortErrorInfo error = getSystemError(); + if (error.errorCode != QSerialPort::ResourceError) + error.errorCode = QSerialPort::WriteError; + setError(error); return false; } @@ -800,15 +780,13 @@ bool QSerialPortPrivate::completeAsyncWrite() inline bool QSerialPortPrivate::initialize(QIODevice::OpenMode mode) { - Q_Q(QSerialPort); - #ifdef TIOCEXCL if (::ioctl(descriptor, TIOCEXCL) == -1) - q->setError(decodeSystemError()); + setError(getSystemError()); #endif if (::tcgetattr(descriptor, &restoredTermios) == -1) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -848,71 +826,72 @@ qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize) bool QSerialPortPrivate::updateTermios() { - Q_Q(QSerialPort); - if (::tcsetattr(descriptor, TCSANOW, ¤tTermios) == -1) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } return true; } -QSerialPort::SerialPortError QSerialPortPrivate::decodeSystemError(int systemErrorCode) const +QSerialPortErrorInfo QSerialPortPrivate::getSystemError(int systemErrorCode) const { - Q_UNUSED(systemErrorCode); + if (systemErrorCode == -1) + systemErrorCode = errno; + + QSerialPortErrorInfo error; + error.errorString = qt_error_string(systemErrorCode); - QSerialPort::SerialPortError error; - switch (errno) { + switch (systemErrorCode) { case ENODEV: - error = QSerialPort::DeviceNotFoundError; + error.errorCode = QSerialPort::DeviceNotFoundError; break; #ifdef ENOENT case ENOENT: - error = QSerialPort::DeviceNotFoundError; + error.errorCode = QSerialPort::DeviceNotFoundError; break; #endif case EACCES: - error = QSerialPort::PermissionError; + error.errorCode = QSerialPort::PermissionError; break; case EBUSY: - error = QSerialPort::PermissionError; + error.errorCode = QSerialPort::PermissionError; break; case EAGAIN: - error = QSerialPort::ResourceError; + error.errorCode = QSerialPort::ResourceError; break; case EIO: - error = QSerialPort::ResourceError; + error.errorCode = QSerialPort::ResourceError; break; case EBADF: - error = QSerialPort::ResourceError; + error.errorCode = QSerialPort::ResourceError; break; #ifdef Q_OS_MAC case ENXIO: - error = QSerialPort::ResourceError; + error.errorCode = QSerialPort::ResourceError; break; #endif #ifdef EINVAL case EINVAL: - error = QSerialPort::UnsupportedOperationError; + error.errorCode = QSerialPort::UnsupportedOperationError; break; #endif #ifdef ENOIOCTLCMD case ENOIOCTLCMD: - error = QSerialPort::UnsupportedOperationError; + error.errorCode = QSerialPort::UnsupportedOperationError; break; #endif #ifdef ENOTTY case ENOTTY: - error = QSerialPort::UnsupportedOperationError; + error.errorCode = QSerialPort::UnsupportedOperationError; break; #endif #ifdef EPERM case EPERM: - error = QSerialPort::PermissionError; + error.errorCode = QSerialPort::PermissionError; break; #endif default: - error = QSerialPort::UnknownError; + error.errorCode = QSerialPort::UnknownError; break; } return error; @@ -956,8 +935,6 @@ bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectFor bool checkRead, bool checkWrite, int msecs) { - Q_Q(QSerialPort); - Q_ASSERT(selectForRead); Q_ASSERT(selectForWrite); @@ -977,11 +954,11 @@ bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectFor const int ret = ::select(descriptor + 1, &fdread, &fdwrite, 0, msecs < 0 ? 0 : &tv); if (ret < 0) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } if (ret == 0) { - q->setError(QSerialPort::TimeoutError); + setError(QSerialPortErrorInfo(QSerialPort::TimeoutError)); return false; } @@ -1068,8 +1045,6 @@ qint64 QSerialPortPrivate::writePerChar(const char *data, qint64 maxSize) qint64 QSerialPortPrivate::readPerChar(char *data, qint64 maxSize) { - Q_Q(QSerialPort); - qint64 ret = 0; quint8 const charMask = (0xFF >> (8 - dataBits)); @@ -1115,13 +1090,16 @@ qint64 QSerialPortPrivate::readPerChar(char *data, qint64 maxSize) switch (policy) { case QSerialPort::SkipPolicy: continue; //ignore received character - case QSerialPort::StopReceivingPolicy: + case QSerialPort::StopReceivingPolicy: { + QSerialPortErrorInfo error; if (parity != QSerialPort::NoParity) - q->setError(QSerialPort::ParityError); + error.errorCode = QSerialPort::ParityError; else - q->setError(*data == '\0' ? - QSerialPort::BreakConditionError : QSerialPort::FramingError); + error.errorCode = *data == '\0' ? + QSerialPort::BreakConditionError : QSerialPort::FramingError; + setError(error); return ++ret; //abort receiving + } break; case QSerialPort::UnknownPolicy: // Unknown error policy is used! Falling back to PassZeroPolicy diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index 761a26e0..d05c9fb2 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -77,8 +77,6 @@ QT_BEGIN_NAMESPACE bool QSerialPortPrivate::open(QIODevice::OpenMode mode) { - Q_Q(QSerialPort); - DWORD desiredAccess = 0; originalEventMask = EV_ERR; @@ -93,7 +91,7 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode) desiredAccess, 0, Q_NULLPTR, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, Q_NULLPTR); if (handle == INVALID_HANDLE_VALUE) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -106,10 +104,8 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode) void QSerialPortPrivate::close() { - Q_Q(QSerialPort); - if (!::CancelIo(handle)) - q->setError(decodeSystemError()); + setError(getSystemError()); if (notifier) notifier->deleteLater(); @@ -123,25 +119,23 @@ void QSerialPortPrivate::close() if (settingsRestoredOnClose) { if (!::SetCommState(handle, &restoredDcb)) - q->setError(decodeSystemError()); + setError(getSystemError()); else if (!::SetCommTimeouts(handle, &restoredCommTimeouts)) - q->setError(decodeSystemError()); + setError(getSystemError()); } if (!::CloseHandle(handle)) - q->setError(decodeSystemError()); + setError(getSystemError()); handle = INVALID_HANDLE_VALUE; } QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals() { - Q_Q(QSerialPort); - DWORD modemStat = 0; if (!::GetCommModemStatus(handle, &modemStat)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return QSerialPort::NoSignal; } @@ -160,7 +154,7 @@ QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals() if (!::DeviceIoControl(handle, IOCTL_SERIAL_GET_DTRRTS, Q_NULLPTR, 0, &modemStat, sizeof(modemStat), &bytesReturned, Q_NULLPTR)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return ret; } @@ -174,10 +168,8 @@ QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals() bool QSerialPortPrivate::setDataTerminalReady(bool set) { - Q_Q(QSerialPort); - if (!::EscapeCommFunction(handle, set ? SETDTR : CLRDTR)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -187,10 +179,8 @@ bool QSerialPortPrivate::setDataTerminalReady(bool set) bool QSerialPortPrivate::setRequestToSend(bool set) { - Q_Q(QSerialPort); - if (!::EscapeCommFunction(handle, set ? SETRTS : CLRRTS)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -204,8 +194,6 @@ bool QSerialPortPrivate::flush() bool QSerialPortPrivate::clear(QSerialPort::Directions directions) { - Q_Q(QSerialPort); - DWORD flags = 0; if (directions & QSerialPort::Input) flags |= PURGE_RXABORT | PURGE_RXCLEAR; @@ -214,7 +202,7 @@ bool QSerialPortPrivate::clear(QSerialPort::Directions directions) actualBytesToWrite = 0; } if (!::PurgeComm(handle, flags)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -242,10 +230,8 @@ bool QSerialPortPrivate::sendBreak(int duration) bool QSerialPortPrivate::setBreakEnabled(bool set) { - Q_Q(QSerialPort); - if (set ? !::SetCommBreak(handle) : !::ClearCommBreak(handle)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -315,10 +301,8 @@ bool QSerialPortPrivate::setBaudRate() bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions directions) { - Q_Q(QSerialPort); - if (directions != QSerialPort::AllDirections) { - q->setError(QSerialPort::UnsupportedOperationError); + setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError)); return false; } currentDcb.BaudRate = baudRate; @@ -462,15 +446,13 @@ bool QSerialPortPrivate::completeAsyncWrite(qint64 bytesTransferred) bool QSerialPortPrivate::startAsyncCommunication() { - Q_Q(QSerialPort); - ::ZeroMemory(&communicationOverlapped, sizeof(communicationOverlapped)); if (!::WaitCommEvent(handle, &triggeredEventMask, &communicationOverlapped)) { - QSerialPort::SerialPortError error = decodeSystemError(); - if (error != QSerialPort::NoError) { - if (error == QSerialPort::PermissionError) - error = QSerialPort::ResourceError; - q->setError(error); + QSerialPortErrorInfo error = getSystemError(); + if (error.errorCode != QSerialPort::NoError) { + if (error.errorCode == QSerialPort::PermissionError) + error.errorCode = QSerialPort::ResourceError; + setError(error); return false; } } @@ -479,8 +461,6 @@ bool QSerialPortPrivate::startAsyncCommunication() bool QSerialPortPrivate::startAsyncRead() { - Q_Q(QSerialPort); - if (readStarted) return true; @@ -501,13 +481,13 @@ bool QSerialPortPrivate::startAsyncRead() return true; } - QSerialPort::SerialPortError error = decodeSystemError(); - if (error != QSerialPort::NoError) { - if (error == QSerialPort::PermissionError) - error = QSerialPort::ResourceError; - if (error != QSerialPort::ResourceError) - error = QSerialPort::ReadError; - q->setError(error); + QSerialPortErrorInfo error = getSystemError(); + if (error.errorCode != QSerialPort::NoError) { + if (error.errorCode == QSerialPort::PermissionError) + error.errorCode = QSerialPort::ResourceError; + if (error.errorCode != QSerialPort::ResourceError) + error.errorCode = QSerialPort::ReadError; + setError(error); return false; } @@ -517,8 +497,6 @@ bool QSerialPortPrivate::startAsyncRead() bool QSerialPortPrivate::_q_startAsyncWrite() { - Q_Q(QSerialPort); - if (writeBuffer.isEmpty() || writeStarted) return true; @@ -527,11 +505,11 @@ bool QSerialPortPrivate::_q_startAsyncWrite() if (!::WriteFile(handle, writeBuffer.readPointer(), writeBytes, Q_NULLPTR, &writeCompletionOverlapped)) { - QSerialPort::SerialPortError error = decodeSystemError(); - if (error != QSerialPort::NoError) { - if (error != QSerialPort::ResourceError) - error = QSerialPort::WriteError; - q->setError(error); + QSerialPortErrorInfo error = getSystemError(); + if (error.errorCode != QSerialPort::NoError) { + if (error.errorCode != QSerialPort::ResourceError) + error.errorCode = QSerialPort::WriteError; + setError(error); return false; } } @@ -543,11 +521,9 @@ bool QSerialPortPrivate::_q_startAsyncWrite() void QSerialPortPrivate::_q_notified(DWORD numberOfBytes, DWORD errorCode, OVERLAPPED *overlapped) { - Q_Q(QSerialPort); - - const QSerialPort::SerialPortError error = decodeSystemError(errorCode); - if (error != QSerialPort::NoError) { - q->setError(error); + const QSerialPortErrorInfo error = getSystemError(errorCode); + if (error.errorCode != QSerialPort::NoError) { + setError(error); return; } @@ -616,33 +592,31 @@ qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize) void QSerialPortPrivate::handleLineStatusErrors() { - Q_Q(QSerialPort); - DWORD errors = 0; if (!::ClearCommError(handle, &errors, Q_NULLPTR)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return; } + QSerialPortErrorInfo error; + if (errors & CE_FRAME) { - q->setError(QSerialPort::FramingError); + error.errorCode = QSerialPort::FramingError; } else if (errors & CE_RXPARITY) { - q->setError(QSerialPort::ParityError); + error.errorCode = QSerialPort::ParityError; parityErrorOccurred = true; } else if (errors & CE_BREAK) { - q->setError(QSerialPort::BreakConditionError); - } else { - q->setError(QSerialPort::UnknownError); + error.errorCode = QSerialPort::BreakConditionError; } + + setError(error); } OVERLAPPED *QSerialPortPrivate::waitForNotified(int msecs) { - Q_Q(QSerialPort); - OVERLAPPED *overlapped = notifier->waitForAnyNotified(msecs); if (!overlapped) { - q->setError(decodeSystemError(WAIT_TIMEOUT)); + setError(getSystemError(WAIT_TIMEOUT)); return 0; } return overlapped; @@ -656,7 +630,7 @@ inline bool QSerialPortPrivate::initialize() restoredDcb.DCBlength = sizeof(restoredDcb); if (!::GetCommState(handle, &restoredDcb)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -677,7 +651,7 @@ inline bool QSerialPortPrivate::initialize() return false; if (!::GetCommTimeouts(handle, &restoredCommTimeouts)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -688,7 +662,7 @@ inline bool QSerialPortPrivate::initialize() return false; if (!::SetCommMask(handle, originalEventMask)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -706,10 +680,8 @@ inline bool QSerialPortPrivate::initialize() bool QSerialPortPrivate::updateDcb() { - Q_Q(QSerialPort); - if (!::SetCommState(handle, ¤tDcb)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } return true; @@ -717,60 +689,60 @@ bool QSerialPortPrivate::updateDcb() bool QSerialPortPrivate::updateCommTimeouts() { - Q_Q(QSerialPort); - if (!::SetCommTimeouts(handle, ¤tCommTimeouts)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } return true; } -QSerialPort::SerialPortError QSerialPortPrivate::decodeSystemError(int systemErrorCode) const +QSerialPortErrorInfo QSerialPortPrivate::getSystemError(int systemErrorCode) const { if (systemErrorCode == -1) systemErrorCode = ::GetLastError(); - QSerialPort::SerialPortError error; + QSerialPortErrorInfo error; + error.errorString = qt_error_string(systemErrorCode); + switch (systemErrorCode) { case ERROR_SUCCESS: - error = QSerialPort::NoError; + error.errorCode = QSerialPort::NoError; break; case ERROR_IO_PENDING: - error = QSerialPort::NoError; + error.errorCode = QSerialPort::NoError; break; case ERROR_MORE_DATA: - error = QSerialPort::NoError; + error.errorCode = QSerialPort::NoError; break; case ERROR_FILE_NOT_FOUND: - error = QSerialPort::DeviceNotFoundError; + error.errorCode = QSerialPort::DeviceNotFoundError; break; case ERROR_INVALID_NAME: - error = QSerialPort::DeviceNotFoundError; + error.errorCode = QSerialPort::DeviceNotFoundError; break; case ERROR_ACCESS_DENIED: - error = QSerialPort::PermissionError; + error.errorCode = QSerialPort::PermissionError; break; case ERROR_INVALID_HANDLE: - error = QSerialPort::ResourceError; + error.errorCode = QSerialPort::ResourceError; break; case ERROR_INVALID_PARAMETER: - error = QSerialPort::UnsupportedOperationError; + error.errorCode = QSerialPort::UnsupportedOperationError; break; case ERROR_BAD_COMMAND: - error = QSerialPort::ResourceError; + error.errorCode = QSerialPort::ResourceError; break; case ERROR_DEVICE_REMOVED: - error = QSerialPort::ResourceError; + error.errorCode = QSerialPort::ResourceError; break; case ERROR_OPERATION_ABORTED: - error = QSerialPort::ResourceError; + error.errorCode = QSerialPort::ResourceError; break; case WAIT_TIMEOUT: - error = QSerialPort::TimeoutError; + error.errorCode = QSerialPort::TimeoutError; break; default: - error = QSerialPort::UnknownError; + error.errorCode = QSerialPort::UnknownError; break; } return error; diff --git a/src/serialport/qserialport_wince.cpp b/src/serialport/qserialport_wince.cpp index 04b00a77..cd28d254 100644 --- a/src/serialport/qserialport_wince.cpp +++ b/src/serialport/qserialport_wince.cpp @@ -178,8 +178,6 @@ private: bool QSerialPortPrivate::open(QIODevice::OpenMode mode) { - Q_Q(QSerialPort); - DWORD desiredAccess = 0; DWORD eventMask = EV_ERR; @@ -196,7 +194,7 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode) desiredAccess, 0, Q_NULLPTR, OPEN_EXISTING, 0, Q_NULLPTR); if (handle == INVALID_HANDLE_VALUE) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -227,12 +225,10 @@ void QSerialPortPrivate::close() QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals() { - Q_Q(QSerialPort); - DWORD modemStat = 0; if (!::GetCommModemStatus(handle, &modemStat)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return QSerialPort::NoSignal; } @@ -251,7 +247,7 @@ QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals() if (!::DeviceIoControl(handle, IOCTL_SERIAL_GET_DTRRTS, Q_NULLPTR, 0, &modemStat, sizeof(modemStat), &bytesReturned, Q_NULLPTR)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return ret; } @@ -265,10 +261,8 @@ QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals() bool QSerialPortPrivate::setDataTerminalReady(bool set) { - Q_Q(QSerialPort); - if (!::EscapeCommFunction(handle, set ? SETDTR : CLRDTR)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -278,10 +272,8 @@ bool QSerialPortPrivate::setDataTerminalReady(bool set) bool QSerialPortPrivate::setRequestToSend(bool set) { - Q_Q(QSerialPort); - if (!::EscapeCommFunction(handle, set ? SETRTS : CLRRTS)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -318,10 +310,8 @@ bool QSerialPortPrivate::sendBreak(int duration) bool QSerialPortPrivate::setBreakEnabled(bool set) { - Q_Q(QSerialPort); - if (set ? !::SetCommBreak(handle) : !::ClearCommBreak(handle)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -389,10 +379,8 @@ bool QSerialPortPrivate::setBaudRate() bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions directions) { - Q_Q(QSerialPort); - if (directions != QSerialPort::AllDirections) { - q->setError(QSerialPort::UnsupportedOperationError); + setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError)); return false; } currentDcb.BaudRate = baudRate; @@ -503,7 +491,7 @@ bool QSerialPortPrivate::notifyRead() if (!sucessResult) { buffer.chop(bytesToRead); - q->setError(QSerialPort::ReadError); + setError(QSerialPortErrorInfo(QSerialPort::ReadError)); return false; } @@ -546,7 +534,7 @@ bool QSerialPortPrivate::notifyWrite() DWORD bytesWritten = 0; if (!::WriteFile(handle, ptr, nextSize, &bytesWritten, Q_NULLPTR)) { - q->setError(QSerialPort::WriteError); + setError(QSerialPortErrorInfo(QSerialPort::WriteError)); return false; } @@ -566,31 +554,31 @@ qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize) return maxSize; } -void QSerialPortPrivate::processIoErrors(bool error) +void QSerialPortPrivate::processIoErrors(bool hasError) { - Q_Q(QSerialPort); - - if (error) { - q->setError(QSerialPort::ResourceError); + if (hasError) { + setError(QSerialPortErrorInfo(QSerialPort::ResourceError)); return; } DWORD errors = 0; if (!::ClearCommError(handle, &errors, Q_NULLPTR)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return; } + QSerialPortErrorInfo error; + if (errors & CE_FRAME) { - q->setError(QSerialPort::FramingError); + error.errorCode = QSerialPort::FramingError; } else if (errors & CE_RXPARITY) { - q->setError(QSerialPort::ParityError); + error.errorCode = QSerialPort::ParityError; parityErrorOccurred = true; } else if (errors & CE_BREAK) { - q->setError(QSerialPort::BreakConditionError); - } else { - q->setError(QSerialPort::UnknownError); + error.errorCode = QSerialPort::BreakConditionError; } + + setError(error); } inline bool QSerialPortPrivate::initialize(DWORD eventMask) @@ -601,7 +589,7 @@ inline bool QSerialPortPrivate::initialize(DWORD eventMask) restoredDcb.DCBlength = sizeof(restoredDcb); if (!::GetCommState(handle, &restoredDcb)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -620,7 +608,7 @@ inline bool QSerialPortPrivate::initialize(DWORD eventMask) return false; if (!::GetCommTimeouts(handle, &restoredCommTimeouts)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -638,8 +626,6 @@ inline bool QSerialPortPrivate::initialize(DWORD eventMask) bool QSerialPortPrivate::updateDcb() { - Q_Q(QSerialPort); - QMutexLocker locker(&settingsChangeMutex); DWORD eventMask = 0; @@ -652,7 +638,7 @@ bool QSerialPortPrivate::updateDcb() // Change parameters bool ret = ::SetCommState(handle, ¤tDcb); if (!ret) - q->setError(decodeSystemError()); + setError(getSystemError()); // Restore the event mask ::SetCommMask(handle, eventMask); @@ -661,50 +647,51 @@ bool QSerialPortPrivate::updateDcb() bool QSerialPortPrivate::updateCommTimeouts() { - Q_Q(QSerialPort); - if (!::SetCommTimeouts(handle, ¤tCommTimeouts)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } return true; } -QSerialPort::SerialPortError QSerialPortPrivate::decodeSystemError(int systemErrorCode) const +QSerialPortErrorInfo QSerialPortPrivate::getSystemError(int systemErrorCode) const { - Q_UNUSED(systemErrorCode); + if (systemErrorCode == -1) + systemErrorCode = ::GetLastError(); - QSerialPort::SerialPortError error; - switch (::GetLastError()) { + QSerialPortErrorInfo error; + error.errorString = qt_error_string(systemErrorCode); + + switch (systemErrorCode) { case ERROR_IO_PENDING: - error = QSerialPort::NoError; + error.errorCode = QSerialPort::NoError; break; case ERROR_MORE_DATA: - error = QSerialPort::NoError; + error.errorCode = QSerialPort::NoError; break; case ERROR_FILE_NOT_FOUND: - error = QSerialPort::DeviceNotFoundError; + error.errorCode = QSerialPort::DeviceNotFoundError; break; case ERROR_INVALID_NAME: - error = QSerialPort::DeviceNotFoundError; + error.errorCode = QSerialPort::DeviceNotFoundError; break; case ERROR_ACCESS_DENIED: - error = QSerialPort::PermissionError; + error.errorCode = QSerialPort::PermissionError; break; case ERROR_INVALID_HANDLE: - error = QSerialPort::ResourceError; + error.errorCode = QSerialPort::ResourceError; break; case ERROR_INVALID_PARAMETER: - error = QSerialPort::UnsupportedOperationError; + error.errorCode = QSerialPort::UnsupportedOperationError; break; case ERROR_BAD_COMMAND: - error = QSerialPort::ResourceError; + error.errorCode = QSerialPort::ResourceError; break; case ERROR_DEVICE_REMOVED: - error = QSerialPort::ResourceError; + error.errorCode = QSerialPort::ResourceError; break; default: - error = QSerialPort::UnknownError; + error.errorCode = QSerialPort::UnknownError; break; } return error; @@ -714,8 +701,6 @@ bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectFor bool checkRead, bool checkWrite, int msecs) { - Q_Q(QSerialPort); - DWORD eventMask = 0; // FIXME: Here the situation is not properly handled with zero timeout: // breaker can work out before you call a method WaitCommEvent() @@ -725,7 +710,7 @@ bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectFor breaker.stop(); if (breaker.isWorked()) { - q->setError(QSerialPort::TimeoutError); + setError(QSerialPortErrorInfo(QSerialPort::TimeoutError)); } else { if (checkRead) { Q_ASSERT(selectForRead); -- cgit v1.2.3