diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/serialport/doc/src/index.qdoc | 8 | ||||
-rw-r--r-- | src/serialport/doc/src/qtserialport-examples.qdoc | 10 | ||||
-rw-r--r-- | src/serialport/doc/src/qtserialport-module.qdoc | 8 | ||||
-rw-r--r-- | src/serialport/qserialport.cpp | 59 | ||||
-rw-r--r-- | src/serialport/qserialport.h | 42 | ||||
-rw-r--r-- | src/serialport/qserialport_p.h | 60 | ||||
-rw-r--r-- | src/serialport/qserialport_unix.cpp | 291 | ||||
-rw-r--r-- | src/serialport/qserialport_win.cpp | 239 | ||||
-rw-r--r-- | src/serialport/qserialport_wince.cpp | 750 | ||||
-rw-r--r-- | src/serialport/qserialportglobal.h | 32 | ||||
-rw-r--r-- | src/serialport/qserialportinfo.cpp | 48 | ||||
-rw-r--r-- | src/serialport/qserialportinfo.h | 32 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_freebsd.cpp | 35 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_osx.cpp | 36 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_p.h | 32 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_unix.cpp | 47 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_win.cpp | 96 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_wince.cpp | 167 | ||||
-rw-r--r-- | src/serialport/qtudev_p.h | 34 | ||||
-rw-r--r-- | src/serialport/serialport-lib.pri | 6 |
20 files changed, 601 insertions, 1431 deletions
diff --git a/src/serialport/doc/src/index.qdoc b/src/serialport/doc/src/index.qdoc index be1c0dd..3b0f762 100644 --- a/src/serialport/doc/src/index.qdoc +++ b/src/serialport/doc/src/index.qdoc @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2011 - 2012 Denis Shienkov <denis.shienkov@gmail.com> -** Contact: http://www.qt.io/licensing/ +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the documentation of the Qt Toolkit. ** @@ -11,8 +11,8 @@ ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Free Documentation License Usage ** Alternatively, this file may be used under the terms of the GNU Free @@ -20,7 +20,7 @@ ** Foundation and appearing in the file included in the packaging of ** this file. Please review the following information to ensure ** the GNU Free Documentation License version 1.3 requirements -** will be met: http://www.gnu.org/copyleft/fdl.html. +** will be met: https://www.gnu.org/licenses/fdl-1.3.html. ** $QT_END_LICENSE$ ** ****************************************************************************/ diff --git a/src/serialport/doc/src/qtserialport-examples.qdoc b/src/serialport/doc/src/qtserialport-examples.qdoc index 1a04cfa..c680224 100644 --- a/src/serialport/doc/src/qtserialport-examples.qdoc +++ b/src/serialport/doc/src/qtserialport-examples.qdoc @@ -2,8 +2,8 @@ ** ** Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com> ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the documentation of the Qt Toolkit. ** @@ -13,8 +13,8 @@ ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Free Documentation License Usage ** Alternatively, this file may be used under the terms of the GNU Free @@ -22,7 +22,7 @@ ** Foundation and appearing in the file included in the packaging of ** this file. Please review the following information to ensure ** the GNU Free Documentation License version 1.3 requirements -** will be met: http://www.gnu.org/copyleft/fdl.html. +** will be met: https://www.gnu.org/licenses/fdl-1.3.html. ** $QT_END_LICENSE$ ** ****************************************************************************/ diff --git a/src/serialport/doc/src/qtserialport-module.qdoc b/src/serialport/doc/src/qtserialport-module.qdoc index 20452e5..325b780 100644 --- a/src/serialport/doc/src/qtserialport-module.qdoc +++ b/src/serialport/doc/src/qtserialport-module.qdoc @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2011 - 2012 Denis Shienkov <denis.shienkov@gmail.com> -** Contact: http://www.qt.io/licensing/ +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the documentation of the Qt Toolkit. ** @@ -11,8 +11,8 @@ ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Free Documentation License Usage ** Alternatively, this file may be used under the terms of the GNU Free @@ -20,7 +20,7 @@ ** Foundation and appearing in the file included in the packaging of ** this file. Please review the following information to ensure ** the GNU Free Documentation License version 1.3 requirements -** will be met: http://www.gnu.org/copyleft/fdl.html. +** will be met: https://www.gnu.org/licenses/fdl-1.3.html. ** $QT_END_LICENSE$ ** ****************************************************************************/ diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp index ff5789d..b2b6a94 100644 --- a/src/serialport/qserialport.cpp +++ b/src/serialport/qserialport.cpp @@ -4,31 +4,37 @@ ** Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com> ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> ** Copyright (C) 2012 Andre Hartmann <aha_1980@gmx.de> -** Contact: http://www.qt.io/licensing/ +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -94,10 +100,7 @@ QSerialPortPrivate::QSerialPortPrivate() , settingsRestoredOnClose(true) #endif , isBreakEnabled(false) -#if defined(Q_OS_WINCE) - , handle(INVALID_HANDLE_VALUE) - , eventNotifier(0) -#elif defined(Q_OS_WIN32) +#if defined(Q_OS_WIN32) , handle(INVALID_HANDLE_VALUE) , readChunkBuffer(ReadChunkSize, 0) , communicationStarted(false) @@ -194,7 +197,7 @@ void QSerialPortPrivate::setError(const QSerialPortErrorInfo &errorInfo) int numRead = 0, numReadTotal = 0; char buffer[50]; - forever { + for (;;) { numRead = serial.read(buffer, 50); // Do whatever with the array @@ -525,10 +528,6 @@ void QSerialPort::setPort(const QSerialPortInfo &serialPortInfo) \li Removes the prefix "\\\\.\\" or "//./" from the system location and returns the remainder of the string. \row - \li Windows CE - \li Removes the suffix ":" from the system location - and returns the remainder of the string. - \row \li Unix, BSD \li Removes the prefix "/dev/" from the system location and returns the remainder of the string. @@ -578,15 +577,6 @@ bool QSerialPort::open(OpenMode mode) if (!d->open(mode)) return false; - if (!d->setBaudRate() - || !d->setDataBits(d->dataBits) - || !d->setParity(d->parity) - || !d->setStopBits(d->stopBits) - || !d->setFlowControl(d->flowControl)) { - d->close(); - return false; - } - QIODevice::open(mode); return true; } @@ -669,9 +659,9 @@ bool QSerialPort::settingsRestoredOnClose() const after that the opening of the port succeeds. \warning Setting the AllDirections flag is supported on all platforms. - Windows and Windows CE support only this mode. + Windows supports only this mode. - \warning Returns equal baud rate in any direction on Windows, Windows CE. + \warning Returns equal baud rate in any direction on Windows. The default value is Baud9600, i.e. 9600 bits per second. */ @@ -1106,8 +1096,7 @@ bool QSerialPort::clear(Directions directions) */ bool QSerialPort::atEnd() const { - Q_D(const QSerialPort); - return QIODevice::atEnd() && (!isOpen() || (d->buffer.size() == 0)); + return QIODevice::atEnd(); } #if QT_DEPRECATED_SINCE(5, 2) diff --git a/src/serialport/qserialport.h b/src/serialport/qserialport.h index a19e1bf..3289ca1 100644 --- a/src/serialport/qserialport.h +++ b/src/serialport/qserialport.h @@ -2,31 +2,37 @@ ** ** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com> ** Copyright (C) 2013 Laszlo Papp <lpapp@kde.org> -** Contact: http://www.qt.io/licensing/ +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -65,7 +71,7 @@ class Q_SERIALPORT_EXPORT QSerialPort : public QIODevice #endif Q_PROPERTY(bool breakEnabled READ isBreakEnabled WRITE setBreakEnabled NOTIFY breakEnabledChanged) -#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) +#if defined(Q_OS_WIN32) typedef void* Handle; #else typedef int Handle; @@ -194,9 +200,9 @@ public: #endif #endif - explicit QSerialPort(QObject *parent = Q_NULLPTR); - explicit QSerialPort(const QString &name, QObject *parent = Q_NULLPTR); - explicit QSerialPort(const QSerialPortInfo &info, QObject *parent = Q_NULLPTR); + explicit QSerialPort(QObject *parent = nullptr); + explicit QSerialPort(const QString &name, QObject *parent = nullptr); + explicit QSerialPort(const QSerialPortInfo &info, QObject *parent = nullptr); virtual ~QSerialPort(); void setPortName(const QString &name); @@ -237,7 +243,7 @@ public: bool flush(); bool clear(Directions directions = AllDirections); - bool atEnd() const Q_DECL_OVERRIDE; + bool atEnd() const Q_DECL_OVERRIDE; // ### Qt6: remove me #if QT_DEPRECATED_SINCE(5, 2) QT_DEPRECATED bool setDataErrorPolicy(DataErrorPolicy policy = IgnorePolicy); diff --git a/src/serialport/qserialport_p.h b/src/serialport/qserialport_p.h index 567ca19..e240798 100644 --- a/src/serialport/qserialport_p.h +++ b/src/serialport/qserialport_p.h @@ -3,31 +3,37 @@ ** Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com> ** Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com> ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> -** Contact: http://www.qt.io/licensing/ +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -52,10 +58,7 @@ #include <private/qringbuffer_p.h> #include <private/qiodevice_p.h> -#if defined(Q_OS_WINCE) -# include <QtCore/qmutex.h> -# include <qt_windows.h> -#elif defined(Q_OS_WIN32) +#if defined(Q_OS_WIN32) # include <qt_windows.h> #elif defined(Q_OS_UNIX) # include <QtCore/qlockfile.h> @@ -178,28 +181,7 @@ public: bool settingsRestoredOnClose; bool isBreakEnabled; -#if defined(Q_OS_WINCE) - - bool initialize(DWORD eventMask); - bool updateDcb(); - - bool waitForReadOrWrite(bool *selectForRead, bool *selectForWrite, - bool checkRead, bool checkWrite, - int msecs); - - bool notifyRead(); - bool notifyWrite(); - - DCB currentDcb; - DCB restoredDcb; - COMMTIMEOUTS currentCommTimeouts; - COMMTIMEOUTS restoredCommTimeouts; - HANDLE handle; - - QThread *eventNotifier; - QMutex settingsChangeMutex; - -#elif defined(Q_OS_WIN32) +#if defined(Q_OS_WIN32) bool initialize(); bool setDcb(DCB *dcb); diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp index 7662a90..8e06ada 100644 --- a/src/serialport/qserialport_unix.cpp +++ b/src/serialport/qserialport_unix.cpp @@ -3,31 +3,37 @@ ** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com> ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> ** Copyright (C) 2012 Andre Hartmann <aha_1980@gmx.de> -** Contact: http://www.qt.io/licensing/ +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -119,7 +125,7 @@ QString serialPortLockFilePath(const QString &portName) QString lockFilePath; - foreach (const QString &lockDirectoryPath, lockDirectoryPaths) { + for (const QString &lockDirectoryPath : lockDirectoryPaths) { const QString filePath = lockDirectoryPath + fileName; QFileInfo lockDirectoryInfo(lockDirectoryPath); @@ -133,7 +139,7 @@ QString serialPortLockFilePath(const QString &portName) if (lockFilePath.isEmpty()) { qWarning("The following directories are not readable or writable for detaling with lock files\n"); - foreach (const QString &lockDirectoryPath, lockDirectoryPaths) + for (const QString &lockDirectoryPath : lockDirectoryPaths) qWarning("\t%s\n", qPrintable(lockDirectoryPath)); return QString(); } @@ -187,6 +193,120 @@ private: QSerialPortPrivate *dptr; }; +static inline void qt_set_common_props(termios *tio, QIODevice::OpenMode m) +{ +#ifdef Q_OS_SOLARIS + tio->c_iflag &= ~(IMAXBEL|IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); + tio->c_oflag &= ~OPOST; + tio->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); + tio->c_cflag &= ~(CSIZE|PARENB); + tio->c_cflag |= CS8; +#else + ::cfmakeraw(tio); +#endif + + tio->c_cflag |= CLOCAL; + tio->c_cc[VTIME] = 0; + tio->c_cc[VMIN] = 0; + + if (m & QIODevice::ReadOnly) + tio->c_cflag |= CREAD; +} + +static inline void qt_set_databits(termios *tio, QSerialPort::DataBits databits) +{ + tio->c_cflag &= ~CSIZE; + switch (databits) { + case QSerialPort::Data5: + tio->c_cflag |= CS5; + break; + case QSerialPort::Data6: + tio->c_cflag |= CS6; + break; + case QSerialPort::Data7: + tio->c_cflag |= CS7; + break; + case QSerialPort::Data8: + tio->c_cflag |= CS8; + break; + default: + tio->c_cflag |= CS8; + break; + } +} + +static inline void qt_set_parity(termios *tio, QSerialPort::Parity parity) +{ + tio->c_iflag &= ~(PARMRK | INPCK); + tio->c_iflag |= IGNPAR; + + switch (parity) { + +#ifdef CMSPAR + // Here Installation parity only for GNU/Linux where the macro CMSPAR. + case QSerialPort::SpaceParity: + tio->c_cflag &= ~PARODD; + tio->c_cflag |= PARENB | CMSPAR; + break; + case QSerialPort::MarkParity: + tio->c_cflag |= PARENB | CMSPAR | PARODD; + break; +#endif + case QSerialPort::NoParity: + tio->c_cflag &= ~PARENB; + break; + case QSerialPort::EvenParity: + tio->c_cflag &= ~PARODD; + tio->c_cflag |= PARENB; + break; + case QSerialPort::OddParity: + tio->c_cflag |= PARENB | PARODD; + break; + default: + tio->c_cflag |= PARENB; + tio->c_iflag |= PARMRK | INPCK; + tio->c_iflag &= ~IGNPAR; + break; + } +} + +static inline void qt_set_stopbits(termios *tio, QSerialPort::StopBits stopbits) +{ + switch (stopbits) { + case QSerialPort::OneStop: + tio->c_cflag &= ~CSTOPB; + break; + case QSerialPort::TwoStop: + tio->c_cflag |= CSTOPB; + break; + default: + tio->c_cflag &= ~CSTOPB; + break; + } +} + +static inline void qt_set_flowcontrol(termios *tio, QSerialPort::FlowControl flowcontrol) +{ + switch (flowcontrol) { + case QSerialPort::NoFlowControl: + tio->c_cflag &= ~CRTSCTS; + tio->c_iflag &= ~(IXON | IXOFF | IXANY); + break; + case QSerialPort::HardwareControl: + tio->c_cflag |= CRTSCTS; + tio->c_iflag &= ~(IXON | IXOFF | IXANY); + break; + case QSerialPort::SoftwareControl: + tio->c_cflag &= ~CRTSCTS; + tio->c_iflag |= IXON | IXOFF | IXANY; + break; + default: + tio->c_cflag &= ~CRTSCTS; + tio->c_iflag &= ~(IXON | IXOFF | IXANY); + break; + } +} + bool QSerialPortPrivate::open(QIODevice::OpenMode mode) { QString lockFilePath = serialPortLockFilePath(QSerialPortInfoPrivate::portNameFromSystemLocation(systemLocation)); @@ -245,14 +365,14 @@ void QSerialPortPrivate::close() #endif delete readNotifier; - readNotifier = Q_NULLPTR; + readNotifier = nullptr; delete writeNotifier; - writeNotifier = Q_NULLPTR; + writeNotifier = nullptr; qt_safe_close(descriptor); - lockFileScopedPointer.reset(Q_NULLPTR); + lockFileScopedPointer.reset(nullptr); descriptor = -1; pendingBytesWritten = 0; @@ -404,7 +524,7 @@ bool QSerialPortPrivate::waitForBytesWritten(int msecs) QElapsedTimer stopWatch; stopWatch.start(); - forever { + for (;;) { bool readyToRead = false; bool readyToWrite = false; if (!waitForReadOrWrite(&readyToRead, &readyToWrite, true, !writeBuffer.isEmpty(), @@ -599,24 +719,8 @@ bool QSerialPortPrivate::setDataBits(QSerialPort::DataBits dataBits) if (!getTermios(&tio)) return false; - tio.c_cflag &= ~CSIZE; - switch (dataBits) { - case QSerialPort::Data5: - tio.c_cflag |= CS5; - break; - case QSerialPort::Data6: - tio.c_cflag |= CS6; - break; - case QSerialPort::Data7: - tio.c_cflag |= CS7; - break; - case QSerialPort::Data8: - tio.c_cflag |= CS8; - break; - default: - tio.c_cflag |= CS8; - break; - } + qt_set_databits(&tio, dataBits); + return setTermios(&tio); } @@ -626,37 +730,7 @@ bool QSerialPortPrivate::setParity(QSerialPort::Parity parity) if (!getTermios(&tio)) return false; - tio.c_iflag &= ~(PARMRK | INPCK); - tio.c_iflag |= IGNPAR; - - switch (parity) { - -#ifdef CMSPAR - // Here Installation parity only for GNU/Linux where the macro CMSPAR. - case QSerialPort::SpaceParity: - tio.c_cflag &= ~PARODD; - tio.c_cflag |= PARENB | CMSPAR; - break; - case QSerialPort::MarkParity: - tio.c_cflag |= PARENB | CMSPAR | PARODD; - break; -#endif - case QSerialPort::NoParity: - tio.c_cflag &= ~PARENB; - break; - case QSerialPort::EvenParity: - tio.c_cflag &= ~PARODD; - tio.c_cflag |= PARENB; - break; - case QSerialPort::OddParity: - tio.c_cflag |= PARENB | PARODD; - break; - default: - tio.c_cflag |= PARENB; - tio.c_iflag |= PARMRK | INPCK; - tio.c_iflag &= ~IGNPAR; - break; - } + qt_set_parity(&tio, parity); return setTermios(&tio); } @@ -667,17 +741,8 @@ bool QSerialPortPrivate::setStopBits(QSerialPort::StopBits stopBits) if (!getTermios(&tio)) return false; - switch (stopBits) { - case QSerialPort::OneStop: - tio.c_cflag &= ~CSTOPB; - break; - case QSerialPort::TwoStop: - tio.c_cflag |= CSTOPB; - break; - default: - tio.c_cflag &= ~CSTOPB; - break; - } + qt_set_stopbits(&tio, stopBits); + return setTermios(&tio); } @@ -687,24 +752,8 @@ bool QSerialPortPrivate::setFlowControl(QSerialPort::FlowControl flowControl) if (!getTermios(&tio)) return false; - switch (flowControl) { - case QSerialPort::NoFlowControl: - tio.c_cflag &= ~CRTSCTS; - tio.c_iflag &= ~(IXON | IXOFF | IXANY); - break; - case QSerialPort::HardwareControl: - tio.c_cflag |= CRTSCTS; - tio.c_iflag &= ~(IXON | IXOFF | IXANY); - break; - case QSerialPort::SoftwareControl: - tio.c_cflag &= ~CRTSCTS; - tio.c_iflag |= IXON | IXOFF | IXANY; - break; - default: - tio.c_cflag &= ~CRTSCTS; - tio.c_iflag &= ~(IXON | IXOFF | IXANY); - break; - } + qt_set_flowcontrol(&tio, flowControl); + return setTermios(&tio); } @@ -819,25 +868,19 @@ inline bool QSerialPortPrivate::initialize(QIODevice::OpenMode mode) return false; restoredTermios = tio; -#ifdef Q_OS_SOLARIS - tio.c_iflag &= ~(IMAXBEL|IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); - tio.c_oflag &= ~OPOST; - tio.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); - tio.c_cflag &= ~(CSIZE|PARENB); - tio.c_cflag |= CS8; -#else - ::cfmakeraw(&tio); -#endif - tio.c_cflag |= CLOCAL; - tio.c_cc[VTIME] = 0; - tio.c_cc[VMIN] = 0; - if (mode & QIODevice::ReadOnly) - tio.c_cflag |= CREAD; + qt_set_common_props(&tio, mode); + qt_set_databits(&tio, dataBits); + qt_set_parity(&tio, parity); + qt_set_stopbits(&tio, stopBits); + qt_set_flowcontrol(&tio, flowControl); if (!setTermios(&tio)) return false; + if (!setBaudRate()) + return false; + if (mode & QIODevice::ReadOnly) setReadNotificationEnabled(true); @@ -846,7 +889,7 @@ inline bool QSerialPortPrivate::initialize(QIODevice::OpenMode mode) qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize) { - ::memcpy(writeBuffer.reserve(maxSize), data, maxSize); + writeBuffer.append(data, maxSize); if (!writeBuffer.isEmpty() && !isWriteNotificationEnabled()) setWriteNotificationEnabled(true); return maxSize; @@ -976,21 +1019,15 @@ bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectFor Q_ASSERT(selectForRead); Q_ASSERT(selectForWrite); - fd_set fdread; - FD_ZERO(&fdread); + pollfd pfd = qt_make_pollfd(descriptor, 0); + if (checkRead) - FD_SET(descriptor, &fdread); + pfd.events |= POLLIN; - fd_set fdwrite; - FD_ZERO(&fdwrite); if (checkWrite) - FD_SET(descriptor, &fdwrite); + pfd.events |= POLLOUT; - struct timespec tv; - tv.tv_sec = msecs / 1000; - tv.tv_nsec = (msecs % 1000) * 1000 * 1000; - - const int ret = qt_safe_select(descriptor + 1, &fdread, &fdwrite, 0, msecs < 0 ? 0 : &tv); + const int ret = qt_poll_msecs(&pfd, 1, msecs); if (ret < 0) { setError(getSystemError()); return false; @@ -999,9 +1036,13 @@ bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectFor setError(QSerialPortErrorInfo(QSerialPort::TimeoutError)); return false; } + if (pfd.revents & POLLNVAL) { + setError(getSystemError(EBADF)); + return false; + } - *selectForRead = FD_ISSET(descriptor, &fdread); - *selectForWrite = FD_ISSET(descriptor, &fdwrite); + *selectForWrite = ((pfd.revents & POLLOUT) != 0); + *selectForRead = ((pfd.revents & POLLIN) != 0); return true; } diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index 056cc31..9ab4448 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -3,31 +3,37 @@ ** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com> ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> ** Copyright (C) 2012 Andre Hartmann <aha_1980@gmx.de> -** Contact: http://www.qt.io/licensing/ +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -75,6 +81,94 @@ QT_BEGIN_NAMESPACE +static inline void qt_set_common_props(DCB *dcb) +{ + dcb->fBinary = TRUE; + dcb->fAbortOnError = FALSE; + dcb->fNull = FALSE; + dcb->fErrorChar = FALSE; + + if (dcb->fDtrControl == DTR_CONTROL_HANDSHAKE) + dcb->fDtrControl = DTR_CONTROL_DISABLE; +} + +static inline void qt_set_baudrate(DCB *dcb, qint32 baudrate) +{ + dcb->BaudRate = baudrate; +} + +static inline void qt_set_databits(DCB *dcb, QSerialPort::DataBits databits) +{ + dcb->ByteSize = databits; +} + +static inline void qt_set_parity(DCB *dcb, QSerialPort::Parity parity) +{ + dcb->fParity = TRUE; + switch (parity) { + case QSerialPort::NoParity: + dcb->Parity = NOPARITY; + dcb->fParity = FALSE; + break; + case QSerialPort::OddParity: + dcb->Parity = ODDPARITY; + break; + case QSerialPort::EvenParity: + dcb->Parity = EVENPARITY; + break; + case QSerialPort::MarkParity: + dcb->Parity = MARKPARITY; + break; + case QSerialPort::SpaceParity: + dcb->Parity = SPACEPARITY; + break; + default: + dcb->Parity = NOPARITY; + dcb->fParity = FALSE; + break; + } +} + +static inline void qt_set_stopbits(DCB *dcb, QSerialPort::StopBits stopbits) +{ + switch (stopbits) { + case QSerialPort::OneStop: + dcb->StopBits = ONESTOPBIT; + break; + case QSerialPort::OneAndHalfStop: + dcb->StopBits = ONE5STOPBITS; + break; + case QSerialPort::TwoStop: + dcb->StopBits = TWOSTOPBITS; + break; + default: + dcb->StopBits = ONESTOPBIT; + break; + } +} + +static inline void qt_set_flowcontrol(DCB *dcb, QSerialPort::FlowControl flowcontrol) +{ + dcb->fInX = FALSE; + dcb->fOutX = FALSE; + dcb->fOutxCtsFlow = FALSE; + dcb->fRtsControl = RTS_CONTROL_DISABLE; + switch (flowcontrol) { + case QSerialPort::NoFlowControl: + break; + case QSerialPort::SoftwareControl: + dcb->fInX = TRUE; + dcb->fOutX = TRUE; + break; + case QSerialPort::HardwareControl: + dcb->fOutxCtsFlow = TRUE; + dcb->fRtsControl = RTS_CONTROL_HANDSHAKE; + break; + default: + break; + } +} + bool QSerialPortPrivate::open(QIODevice::OpenMode mode) { DWORD desiredAccess = 0; @@ -88,7 +182,7 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode) desiredAccess |= GENERIC_WRITE; handle = ::CreateFile(reinterpret_cast<const wchar_t*>(systemLocation.utf16()), - desiredAccess, 0, Q_NULLPTR, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, Q_NULLPTR); + desiredAccess, 0, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr); if (handle == INVALID_HANDLE_VALUE) { setError(getSystemError()); @@ -107,10 +201,10 @@ void QSerialPortPrivate::close() ::CancelIo(handle); delete notifier; - notifier = Q_NULLPTR; + notifier = nullptr; delete startAsyncWriteTimer; - startAsyncWriteTimer = Q_NULLPTR; + startAsyncWriteTimer = nullptr; communicationStarted = false; readStarted = false; @@ -147,9 +241,9 @@ QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals() ret |= QSerialPort::DataCarrierDetectSignal; DWORD bytesReturned = 0; - if (!::DeviceIoControl(handle, IOCTL_SERIAL_GET_DTRRTS, Q_NULLPTR, 0, + if (!::DeviceIoControl(handle, IOCTL_SERIAL_GET_DTRRTS, nullptr, 0, &modemStat, sizeof(modemStat), - &bytesReturned, Q_NULLPTR)) { + &bytesReturned, nullptr)) { setError(getSystemError()); return ret; } @@ -248,7 +342,7 @@ bool QSerialPortPrivate::waitForReadyRead(int msecs) stopWatch.start(); do { - OVERLAPPED *overlapped = waitForNotified( + const OVERLAPPED *overlapped = waitForNotified( qt_subtract_from_timeout(msecs, stopWatch.elapsed())); if (!overlapped) return false; @@ -281,8 +375,8 @@ bool QSerialPortPrivate::waitForBytesWritten(int msecs) QElapsedTimer stopWatch; stopWatch.start(); - forever { - OVERLAPPED *overlapped = waitForNotified( + for (;;) { + const OVERLAPPED *overlapped = waitForNotified( qt_subtract_from_timeout(msecs, stopWatch.elapsed())); if (!overlapped) return false; @@ -310,7 +404,8 @@ bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions di if (!getDcb(&dcb)) return false; - dcb.BaudRate = baudRate; + qt_set_baudrate(&dcb, baudRate); + return setDcb(&dcb); } @@ -320,7 +415,8 @@ bool QSerialPortPrivate::setDataBits(QSerialPort::DataBits dataBits) if (!getDcb(&dcb)) return false; - dcb.ByteSize = dataBits; + qt_set_databits(&dcb, dataBits); + return setDcb(&dcb); } @@ -330,29 +426,8 @@ bool QSerialPortPrivate::setParity(QSerialPort::Parity parity) if (!getDcb(&dcb)) return false; - dcb.fParity = TRUE; - switch (parity) { - case QSerialPort::NoParity: - dcb.Parity = NOPARITY; - dcb.fParity = FALSE; - break; - case QSerialPort::OddParity: - dcb.Parity = ODDPARITY; - break; - case QSerialPort::EvenParity: - dcb.Parity = EVENPARITY; - break; - case QSerialPort::MarkParity: - dcb.Parity = MARKPARITY; - break; - case QSerialPort::SpaceParity: - dcb.Parity = SPACEPARITY; - break; - default: - dcb.Parity = NOPARITY; - dcb.fParity = FALSE; - break; - } + qt_set_parity(&dcb, parity); + return setDcb(&dcb); } @@ -362,20 +437,8 @@ bool QSerialPortPrivate::setStopBits(QSerialPort::StopBits stopBits) if (!getDcb(&dcb)) return false; - switch (stopBits) { - case QSerialPort::OneStop: - dcb.StopBits = ONESTOPBIT; - break; - case QSerialPort::OneAndHalfStop: - dcb.StopBits = ONE5STOPBITS; - break; - case QSerialPort::TwoStop: - dcb.StopBits = TWOSTOPBITS; - break; - default: - dcb.StopBits = ONESTOPBIT; - break; - } + qt_set_stopbits(&dcb, stopBits); + return setDcb(&dcb); } @@ -385,24 +448,8 @@ bool QSerialPortPrivate::setFlowControl(QSerialPort::FlowControl flowControl) if (!getDcb(&dcb)) return false; - dcb.fInX = FALSE; - dcb.fOutX = FALSE; - dcb.fOutxCtsFlow = FALSE; - dcb.fRtsControl = RTS_CONTROL_DISABLE; - switch (flowControl) { - case QSerialPort::NoFlowControl: - break; - case QSerialPort::SoftwareControl: - dcb.fInX = TRUE; - dcb.fOutX = TRUE; - break; - case QSerialPort::HardwareControl: - dcb.fOutxCtsFlow = TRUE; - dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; - break; - default: - break; - } + qt_set_flowcontrol(&dcb, flowControl); + return setDcb(&dcb); } @@ -422,10 +469,8 @@ bool QSerialPortPrivate::completeAsyncRead(qint64 bytesTransferred) readStarted = false; return false; } - if (bytesTransferred > 0) { - char *ptr = buffer.reserve(bytesTransferred); - ::memcpy(ptr, readChunkBuffer.constData(), bytesTransferred); - } + if (bytesTransferred > 0) + buffer.append(readChunkBuffer.constData(), bytesTransferred); readStarted = false; @@ -499,7 +544,7 @@ bool QSerialPortPrivate::startAsyncRead() } ::ZeroMemory(&readCompletionOverlapped, sizeof(readCompletionOverlapped)); - if (::ReadFile(handle, readChunkBuffer.data(), bytesToRead, Q_NULLPTR, &readCompletionOverlapped)) { + if (::ReadFile(handle, readChunkBuffer.data(), bytesToRead, nullptr, &readCompletionOverlapped)) { readStarted = true; return true; } @@ -526,7 +571,7 @@ bool QSerialPortPrivate::_q_startAsyncWrite() writeChunkBuffer = writeBuffer.read(); ::ZeroMemory(&writeCompletionOverlapped, sizeof(writeCompletionOverlapped)); if (!::WriteFile(handle, writeChunkBuffer.constData(), - writeChunkBuffer.size(), Q_NULLPTR, &writeCompletionOverlapped)) { + writeChunkBuffer.size(), nullptr, &writeCompletionOverlapped)) { QSerialPortErrorInfo error = getSystemError(); if (error.errorCode != QSerialPort::NoError) { @@ -570,7 +615,7 @@ qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize) { Q_Q(QSerialPort); - ::memcpy(writeBuffer.reserve(maxSize), data, maxSize); + writeBuffer.append(data, maxSize); if (!writeBuffer.isEmpty() && !writeStarted) { if (!startAsyncWriteTimer) { @@ -578,7 +623,8 @@ qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize) QObjectPrivate::connect(startAsyncWriteTimer, &QTimer::timeout, this, &QSerialPortPrivate::_q_startAsyncWrite); startAsyncWriteTimer->setSingleShot(true); } - startAsyncWriteTimer->start(0); + if (!startAsyncWriteTimer->isActive()) + startAsyncWriteTimer->start(); } return maxSize; } @@ -588,7 +634,7 @@ OVERLAPPED *QSerialPortPrivate::waitForNotified(int msecs) OVERLAPPED *overlapped = notifier->waitForAnyNotified(msecs); if (!overlapped) { setError(getSystemError(WAIT_TIMEOUT)); - return 0; + return nullptr; } return overlapped; } @@ -596,7 +642,7 @@ OVERLAPPED *QSerialPortPrivate::waitForNotified(int msecs) qint64 QSerialPortPrivate::queuedBytesCount(QSerialPort::Direction direction) const { COMSTAT comstat; - if (::ClearCommError(handle, Q_NULLPTR, &comstat) == 0) + if (::ClearCommError(handle, nullptr, &comstat) == 0) return -1; return (direction == QSerialPort::Input) ? comstat.cbInQue @@ -613,17 +659,12 @@ inline bool QSerialPortPrivate::initialize() restoredDcb = dcb; - dcb.fBinary = TRUE; - dcb.fInX = FALSE; - dcb.fOutX = FALSE; - dcb.fAbortOnError = FALSE; - dcb.fNull = FALSE; - dcb.fErrorChar = FALSE; - - if (dcb.fDtrControl == DTR_CONTROL_HANDSHAKE) - dcb.fDtrControl = DTR_CONTROL_DISABLE; - - dcb.BaudRate = inputBaudRate; + qt_set_common_props(&dcb); + qt_set_baudrate(&dcb, inputBaudRate); + qt_set_databits(&dcb, dataBits); + qt_set_parity(&dcb, parity); + qt_set_stopbits(&dcb, stopBits); + qt_set_flowcontrol(&dcb, flowControl); if (!setDcb(&dcb)) return false; diff --git a/src/serialport/qserialport_wince.cpp b/src/serialport/qserialport_wince.cpp deleted file mode 100644 index 6b063e1..0000000 --- a/src/serialport/qserialport_wince.cpp +++ /dev/null @@ -1,750 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com> -** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> -** Copyright (C) 2012 Andre Hartmann <aha_1980@gmx.de> -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the QtSerialPort module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL21$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qserialport_p.h" - -#include <QtCore/qelapsedtimer.h> -#include <QtCore/qthread.h> -#include <QtCore/qtimer.h> -#include <algorithm> - -#ifndef CTL_CODE -# define CTL_CODE(DeviceType, Function, Method, Access) ( \ - ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) \ - ) -#endif - -#ifndef FILE_DEVICE_SERIAL_PORT -# define FILE_DEVICE_SERIAL_PORT 27 -#endif - -#ifndef METHOD_BUFFERED -# define METHOD_BUFFERED 0 -#endif - -#ifndef FILE_ANY_ACCESS -# define FILE_ANY_ACCESS 0x00000000 -#endif - -#ifndef IOCTL_SERIAL_GET_DTRRTS -# define IOCTL_SERIAL_GET_DTRRTS \ - CTL_CODE(FILE_DEVICE_SERIAL_PORT, 30, METHOD_BUFFERED, FILE_ANY_ACCESS) -#endif - -#ifndef SERIAL_DTR_STATE -# define SERIAL_DTR_STATE 0x00000001 -#endif - -#ifndef SERIAL_RTS_STATE -# define SERIAL_RTS_STATE 0x00000002 -#endif - -QT_BEGIN_NAMESPACE - -class QSerialPortPrivate; - -class CommEventNotifier : public QThread -{ - Q_OBJECT -signals: - void eventMask(quint32 mask); - -public: - CommEventNotifier(DWORD mask, QSerialPortPrivate *d, QObject *parent) - : QThread(parent), dptr(d) { - connect(this, &CommEventNotifier::eventMask, this, &CommEventNotifier::processNotification); - ::SetCommMask(dptr->handle, mask); - } - - virtual ~CommEventNotifier() { - ::SetCommMask(dptr->handle, 0); - } - -protected: - void run() Q_DECL_OVERRIDE { - DWORD mask = 0; - while (true) { - if (::WaitCommEvent(dptr->handle, &mask, FALSE)) { - // Wait until complete the operation changes the port settings, - // see updateDcb(). - dptr->settingsChangeMutex.lock(); - dptr->settingsChangeMutex.unlock(); - emit eventMask(quint32(mask)); - } - } - } - -private slots: - void processNotification(quint32 eventMask) { - - bool error = false; - - // Check for unexpected event. This event triggered when pulled previously - // opened device from the system, when opened as for not to read and not to - // write options and so forth. - if ((eventMask == 0) - || ((eventMask & (EV_ERR | EV_RXCHAR | EV_TXEMPTY)) == 0)) { - error = true; - } - - if (EV_RXCHAR & eventMask) - dptr->notifyRead(); - if (EV_TXEMPTY & eventMask) - dptr->notifyWrite(); - } - -private: - QSerialPortPrivate *dptr; -}; - -class WaitCommEventBreaker : public QThread -{ - Q_OBJECT -public: - WaitCommEventBreaker(HANDLE handle, int timeout, QObject *parent = Q_NULLPTR) - : QThread(parent), handle(handle), timeout(timeout), worked(false) { - start(); - } - - virtual ~WaitCommEventBreaker() { - stop(); - wait(); - } - - void stop() { - exit(0); - } - - bool isWorked() const { - return worked; - } - -protected: - void run() { - QTimer timer; - QObject::connect(&timer, &QTimer::timeout, this, &WaitCommEventBreaker::processTimeout, Qt::DirectConnection); - timer.start(timeout); - exec(); - worked = true; - } - -private slots: - void processTimeout() { - ::SetCommMask(handle, 0); - stop(); - } - -private: - HANDLE handle; - int timeout; - mutable bool worked; -}; - -#include "qserialport_wince.moc" - -bool QSerialPortPrivate::open(QIODevice::OpenMode mode) -{ - DWORD desiredAccess = 0; - DWORD eventMask = 0; - - if (mode & QIODevice::ReadOnly) { - desiredAccess |= GENERIC_READ; - eventMask |= EV_RXCHAR; - } - if (mode & QIODevice::WriteOnly) { - desiredAccess |= GENERIC_WRITE; - eventMask |= EV_TXEMPTY; - } - - handle = ::CreateFile(reinterpret_cast<const wchar_t*>(systemLocation.utf16()), - desiredAccess, 0, Q_NULLPTR, OPEN_EXISTING, 0, Q_NULLPTR); - - if (handle == INVALID_HANDLE_VALUE) { - setError(getSystemError()); - return false; - } - - if (initialize(eventMask)) - return true; - - ::CloseHandle(handle); - return false; -} - -void QSerialPortPrivate::close() -{ - if (eventNotifier) { - eventNotifier->terminate(); - eventNotifier->wait(); - delete eventNotifier; - eventNotifier = Q_NULLPTR; - } - - if (settingsRestoredOnClose) { - ::SetCommState(handle, &restoredDcb); - ::SetCommTimeouts(handle, &restoredCommTimeouts); - } - - ::CloseHandle(handle); - handle = INVALID_HANDLE_VALUE; -} - -QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals() -{ - DWORD modemStat = 0; - - if (!::GetCommModemStatus(handle, &modemStat)) { - setError(getSystemError()); - return QSerialPort::NoSignal; - } - - QSerialPort::PinoutSignals ret = QSerialPort::NoSignal; - - if (modemStat & MS_CTS_ON) - ret |= QSerialPort::ClearToSendSignal; - if (modemStat & MS_DSR_ON) - ret |= QSerialPort::DataSetReadySignal; - if (modemStat & MS_RING_ON) - ret |= QSerialPort::RingIndicatorSignal; - if (modemStat & MS_RLSD_ON) - ret |= QSerialPort::DataCarrierDetectSignal; - - DWORD bytesReturned = 0; - if (!::DeviceIoControl(handle, IOCTL_SERIAL_GET_DTRRTS, Q_NULLPTR, 0, - &modemStat, sizeof(modemStat), - &bytesReturned, Q_NULLPTR)) { - setError(getSystemError()); - return ret; - } - - if (modemStat & SERIAL_DTR_STATE) - ret |= QSerialPort::DataTerminalReadySignal; - if (modemStat & SERIAL_RTS_STATE) - ret |= QSerialPort::RequestToSendSignal; - - return ret; -} - -bool QSerialPortPrivate::setDataTerminalReady(bool set) -{ - if (!::EscapeCommFunction(handle, set ? SETDTR : CLRDTR)) { - setError(getSystemError()); - return false; - } - - currentDcb.fDtrControl = set ? DTR_CONTROL_ENABLE : DTR_CONTROL_DISABLE; - return true; -} - -bool QSerialPortPrivate::setRequestToSend(bool set) -{ - if (!::EscapeCommFunction(handle, set ? SETRTS : CLRRTS)) { - setError(getSystemError()); - return false; - } - - return true; -} - -bool QSerialPortPrivate::flush() -{ - return notifyWrite() && ::FlushFileBuffers(handle); -} - -bool QSerialPortPrivate::clear(QSerialPort::Directions directions) -{ - DWORD flags = 0; - if (directions & QSerialPort::Input) - flags |= PURGE_RXABORT | PURGE_RXCLEAR; - if (directions & QSerialPort::Output) - flags |= PURGE_TXABORT | PURGE_TXCLEAR; - return ::PurgeComm(handle, flags); -} - -bool QSerialPortPrivate::sendBreak(int duration) -{ - if (!setBreakEnabled(true)) - return false; - - ::Sleep(duration); - - if (!setBreakEnabled(false)) - return false; - - return true; -} - -bool QSerialPortPrivate::setBreakEnabled(bool set) -{ - if (set ? !::SetCommBreak(handle) : !::ClearCommBreak(handle)) { - setError(getSystemError()); - return false; - } - - return true; -} - -bool QSerialPortPrivate::waitForReadyRead(int msec) -{ - if (!buffer.isEmpty()) - return true; - - QElapsedTimer stopWatch; - stopWatch.start(); - - forever { - bool readyToRead = false; - bool readyToWrite = false; - if (!waitForReadOrWrite(&readyToRead, &readyToWrite, - true, !writeBuffer.isEmpty(), - qt_subtract_from_timeout(msec, stopWatch.elapsed()))) { - return false; - } - if (readyToRead) { - if (notifyRead()) - return true; - } - if (readyToWrite) - notifyWrite(); - } - return false; -} - -bool QSerialPortPrivate::waitForBytesWritten(int msec) -{ - if (writeBuffer.isEmpty()) - return false; - - QElapsedTimer stopWatch; - stopWatch.start(); - - forever { - bool readyToRead = false; - bool readyToWrite = false; - if (!waitForReadOrWrite(&readyToRead, &readyToWrite, - true, !writeBuffer.isEmpty(), - qt_subtract_from_timeout(msec, stopWatch.elapsed()))) { - return false; - } - if (readyToRead) { - if (!notifyRead()) - return false; - } - if (readyToWrite) { - if (notifyWrite()) - return true; - } - } - return false; -} - -bool QSerialPortPrivate::setBaudRate() -{ - return setBaudRate(inputBaudRate, QSerialPort::AllDirections); -} - -bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions directions) -{ - if (directions != QSerialPort::AllDirections) { - setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError, QSerialPort::tr("Custom baud rate direction is unsupported"))); - return false; - } - currentDcb.BaudRate = baudRate; - return updateDcb(); -} - -bool QSerialPortPrivate::setDataBits(QSerialPort::DataBits dataBits) -{ - currentDcb.ByteSize = dataBits; - return updateDcb(); -} - -bool QSerialPortPrivate::setParity(QSerialPort::Parity parity) -{ - currentDcb.fParity = TRUE; - switch (parity) { - case QSerialPort::NoParity: - currentDcb.Parity = NOPARITY; - currentDcb.fParity = FALSE; - break; - case QSerialPort::OddParity: - currentDcb.Parity = ODDPARITY; - break; - case QSerialPort::EvenParity: - currentDcb.Parity = EVENPARITY; - break; - case QSerialPort::MarkParity: - currentDcb.Parity = MARKPARITY; - break; - case QSerialPort::SpaceParity: - currentDcb.Parity = SPACEPARITY; - break; - default: - currentDcb.Parity = NOPARITY; - currentDcb.fParity = FALSE; - break; - } - return updateDcb(); -} - -bool QSerialPortPrivate::setStopBits(QSerialPort::StopBits stopBits) -{ - switch (stopBits) { - case QSerialPort::OneStop: - currentDcb.StopBits = ONESTOPBIT; - break; - case QSerialPort::OneAndHalfStop: - currentDcb.StopBits = ONE5STOPBITS; - break; - case QSerialPort::TwoStop: - currentDcb.StopBits = TWOSTOPBITS; - break; - default: - currentDcb.StopBits = ONESTOPBIT; - break; - } - return updateDcb(); -} - -bool QSerialPortPrivate::setFlowControl(QSerialPort::FlowControl flowControl) -{ - currentDcb.fInX = FALSE; - currentDcb.fOutX = FALSE; - currentDcb.fOutxCtsFlow = FALSE; - currentDcb.fRtsControl = RTS_CONTROL_DISABLE; - switch (flowControl) { - case QSerialPort::NoFlowControl: - break; - case QSerialPort::SoftwareControl: - currentDcb.fInX = TRUE; - currentDcb.fOutX = TRUE; - break; - case QSerialPort::HardwareControl: - currentDcb.fOutxCtsFlow = TRUE; - currentDcb.fRtsControl = RTS_CONTROL_HANDSHAKE; - break; - default: - break; - } - return updateDcb(); -} - -bool QSerialPortPrivate::notifyRead() -{ - Q_Q(QSerialPort); - - DWORD bytesToRead = ReadChunkSize; - - if (readBufferMaxSize && bytesToRead > (readBufferMaxSize - buffer.size())) { - bytesToRead = readBufferMaxSize - buffer.size(); - if (bytesToRead == 0) { - // Buffer is full. User must read data from the buffer - // before we can read more from the port. - return false; - } - } - - char *ptr = buffer.reserve(bytesToRead); - - DWORD readBytes = 0; - BOOL sucessResult = ::ReadFile(handle, ptr, bytesToRead, &readBytes, Q_NULLPTR); - - if (!sucessResult) { - buffer.chop(bytesToRead); - setError(QSerialPortErrorInfo(QSerialPort::ReadError)); - return false; - } - - buffer.chop(bytesToRead - qMax(readBytes, DWORD(0))); - - if (readBytes > 0) - emit q->readyRead(); - - return true; -} - -bool QSerialPortPrivate::notifyWrite() -{ - Q_Q(QSerialPort); - - int nextSize = writeBuffer.nextDataBlockSize(); - - const char *ptr = writeBuffer.readPointer(); - - DWORD bytesWritten = 0; - if (!::WriteFile(handle, ptr, nextSize, &bytesWritten, Q_NULLPTR)) { - setError(QSerialPortErrorInfo(QSerialPort::WriteError)); - return false; - } - - writeBuffer.free(bytesWritten); - - if (bytesWritten > 0) - emit q->bytesWritten(bytesWritten); - - return true; -} - -qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize) -{ - ::memcpy(writeBuffer.reserve(maxSize), data, maxSize); - if (!writeBuffer.isEmpty()) - notifyWrite(); - return maxSize; -} - -inline bool QSerialPortPrivate::initialize(DWORD eventMask) -{ - Q_Q(QSerialPort); - - ::ZeroMemory(&restoredDcb, sizeof(restoredDcb)); - restoredDcb.DCBlength = sizeof(restoredDcb); - - if (!::GetCommState(handle, &restoredDcb)) { - setError(getSystemError()); - return false; - } - - currentDcb = restoredDcb; - currentDcb.fBinary = true; - currentDcb.fInX = false; - currentDcb.fOutX = false; - currentDcb.fAbortOnError = false; - currentDcb.fNull = false; - currentDcb.fErrorChar = false; - - if (currentDcb.fDtrControl == DTR_CONTROL_HANDSHAKE) - currentDcb.fDtrControl = DTR_CONTROL_DISABLE; - - if (!updateDcb()) - return false; - - if (!::GetCommTimeouts(handle, &restoredCommTimeouts)) { - setError(getSystemError()); - return false; - } - - ::memset(¤tCommTimeouts, 0, sizeof(currentCommTimeouts)); - currentCommTimeouts.ReadIntervalTimeout = MAXDWORD; - - if (!::SetCommTimeouts(handle, ¤tCommTimeouts)) { - setError(getSystemError()); - return false; - } - - eventNotifier = new CommEventNotifier(eventMask, this, q); - eventNotifier->start(); - - return true; -} - -bool QSerialPortPrivate::updateDcb() -{ - QMutexLocker locker(&settingsChangeMutex); - - DWORD eventMask = 0; - // Save the event mask - if (!::GetCommMask(handle, &eventMask)) - return false; - - // Break event notifier from WaitCommEvent - ::SetCommMask(handle, 0); - // Change parameters - bool ret = ::SetCommState(handle, ¤tDcb); - if (!ret) - setError(getSystemError()); - // Restore the event mask - ::SetCommMask(handle, eventMask); - - return ret; -} - -QSerialPortErrorInfo QSerialPortPrivate::getSystemError(int systemErrorCode) const -{ - if (systemErrorCode == -1) - systemErrorCode = ::GetLastError(); - - QSerialPortErrorInfo error; - error.errorString = qt_error_string(systemErrorCode); - - switch (systemErrorCode) { - case ERROR_IO_PENDING: - error.errorCode = QSerialPort::NoError; - break; - case ERROR_MORE_DATA: - error.errorCode = QSerialPort::NoError; - break; - case ERROR_FILE_NOT_FOUND: - error.errorCode = QSerialPort::DeviceNotFoundError; - break; - case ERROR_PATH_NOT_FOUND: - error.errorCode = QSerialPort::DeviceNotFoundError; - break; - case ERROR_INVALID_NAME: - error.errorCode = QSerialPort::DeviceNotFoundError; - break; - case ERROR_ACCESS_DENIED: - error.errorCode = QSerialPort::PermissionError; - break; - case ERROR_INVALID_HANDLE: - error.errorCode = QSerialPort::ResourceError; - break; - case ERROR_INVALID_PARAMETER: - error.errorCode = QSerialPort::UnsupportedOperationError; - break; - case ERROR_BAD_COMMAND: - error.errorCode = QSerialPort::ResourceError; - break; - case ERROR_DEVICE_REMOVED: - error.errorCode = QSerialPort::ResourceError; - break; - default: - error.errorCode = QSerialPort::UnknownError; - break; - } - return error; -} - -bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectForWrite, - bool checkRead, bool checkWrite, - int msecs) -{ - DWORD eventMask = 0; - // FIXME: Here the situation is not properly handled with zero timeout: - // breaker can work out before you call a method WaitCommEvent() - // and so it will loop forever! - WaitCommEventBreaker breaker(handle, qMax(msecs, 0)); - ::WaitCommEvent(handle, &eventMask, Q_NULLPTR); - breaker.stop(); - - if (breaker.isWorked()) { - setError(QSerialPortErrorInfo(QSerialPort::TimeoutError)); - } else { - if (checkRead) { - Q_ASSERT(selectForRead); - *selectForRead = eventMask & EV_RXCHAR; - } - if (checkWrite) { - Q_ASSERT(selectForWrite); - *selectForWrite = eventMask & EV_TXEMPTY; - } - - return true; - } - - return false; -} - -static const QList<qint32> standardBaudRatePairList() -{ - - static const QList<qint32> standardBaudRatesTable = QList<qint32>() - - #ifdef CBR_110 - << CBR_110 - #endif - - #ifdef CBR_300 - << CBR_300 - #endif - - #ifdef CBR_600 - << CBR_600 - #endif - - #ifdef CBR_1200 - << CBR_1200 - #endif - - #ifdef CBR_2400 - << CBR_2400 - #endif - - #ifdef CBR_4800 - << CBR_4800 - #endif - - #ifdef CBR_9600 - << CBR_9600 - #endif - - #ifdef CBR_14400 - << CBR_14400 - #endif - - #ifdef CBR_19200 - << CBR_19200 - #endif - - #ifdef CBR_38400 - << CBR_38400 - #endif - - #ifdef CBR_56000 - << CBR_56000 - #endif - - #ifdef CBR_57600 - << CBR_57600 - #endif - - #ifdef CBR_115200 - << CBR_115200 - #endif - - #ifdef CBR_128000 - << CBR_128000 - #endif - - #ifdef CBR_256000 - << CBR_256000 - #endif - ; - - return standardBaudRatesTable; -}; - -QList<qint32> QSerialPortPrivate::standardBaudRates() -{ - return standardBaudRatePairList(); -} - -QSerialPort::Handle QSerialPort::handle() const -{ - Q_D(const QSerialPort); - return d->handle; -} - -QT_END_NAMESPACE diff --git a/src/serialport/qserialportglobal.h b/src/serialport/qserialportglobal.h index ec0cd5c..f6c5803 100644 --- a/src/serialport/qserialportglobal.h +++ b/src/serialport/qserialportglobal.h @@ -2,31 +2,37 @@ ** ** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com> ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> -** Contact: http://www.qt.io/licensing/ +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** diff --git a/src/serialport/qserialportinfo.cpp b/src/serialport/qserialportinfo.cpp index bfa2b09..d978665 100644 --- a/src/serialport/qserialportinfo.cpp +++ b/src/serialport/qserialportinfo.cpp @@ -3,31 +3,37 @@ ** Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com> ** Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com> ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> -** Contact: http://www.qt.io/licensing/ +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -73,7 +79,7 @@ QSerialPortInfo::QSerialPortInfo() Constructs a copy of \a other. */ QSerialPortInfo::QSerialPortInfo(const QSerialPortInfo &other) - : d_ptr(other.d_ptr ? new QSerialPortInfoPrivate(*other.d_ptr) : Q_NULLPTR) + : d_ptr(other.d_ptr ? new QSerialPortInfoPrivate(*other.d_ptr) : nullptr) { } @@ -81,13 +87,8 @@ QSerialPortInfo::QSerialPortInfo(const QSerialPortInfo &other) Constructs a QSerialPortInfo object from serial \a port. */ QSerialPortInfo::QSerialPortInfo(const QSerialPort &port) + : QSerialPortInfo(port.portName()) { - foreach (const QSerialPortInfo &serialPortInfo, availablePorts()) { - if (port.portName() == serialPortInfo.portName()) { - *this = serialPortInfo; - break; - } - } } /*! @@ -99,9 +100,10 @@ QSerialPortInfo::QSerialPortInfo(const QSerialPort &port) */ QSerialPortInfo::QSerialPortInfo(const QString &name) { - foreach (const QSerialPortInfo &serialPortInfo, availablePorts()) { - if (name == serialPortInfo.portName()) { - *this = serialPortInfo; + const auto infos = QSerialPortInfo::availablePorts(); + for (const QSerialPortInfo &info : infos) { + if (name == info.portName()) { + *this = info; break; } } diff --git a/src/serialport/qserialportinfo.h b/src/serialport/qserialportinfo.h index fc83461..e4c9288 100644 --- a/src/serialport/qserialportinfo.h +++ b/src/serialport/qserialportinfo.h @@ -2,31 +2,37 @@ ** ** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com> ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> -** Contact: http://www.qt.io/licensing/ +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** diff --git a/src/serialport/qserialportinfo_freebsd.cpp b/src/serialport/qserialportinfo_freebsd.cpp index 552db89..066290d 100644 --- a/src/serialport/qserialportinfo_freebsd.cpp +++ b/src/serialport/qserialportinfo_freebsd.cpp @@ -106,7 +106,7 @@ struct NodeInfo static QVector<int> mibFromName(const QString &name) { size_t mibsize = 0; - if (::sysctlnametomib(name.toLocal8Bit().constData(), Q_NULLPTR, &mibsize) < 0 + if (::sysctlnametomib(name.toLocal8Bit().constData(), nullptr, &mibsize) < 0 || mibsize == 0) { return QVector<int>(); } @@ -122,15 +122,15 @@ static QVector<int> nextOid(const QVector<int> &previousOid) QVector<int> mib; mib.append(0); // Magic undocumented code (CTL_UNSPEC ?) mib.append(2); // Magic undocumented code - foreach (int code, previousOid) + for (int code : previousOid) mib.append(code); size_t requiredLength = 0; - if (::sysctl(&mib[0], mib.count(), Q_NULLPTR, &requiredLength, Q_NULLPTR, 0) < 0) + if (::sysctl(&mib[0], mib.count(), nullptr, &requiredLength, nullptr, 0) < 0) return QVector<int>(); const size_t oidLength = requiredLength / sizeof(int); QVector<int> oid(oidLength, 0); - if (::sysctl(&mib[0], mib.count(), &oid[0], &requiredLength, Q_NULLPTR, 0) < 0) + if (::sysctl(&mib[0], mib.count(), &oid[0], &requiredLength, nullptr, 0) < 0) return QVector<int>(); if (previousOid.first() != oid.first()) @@ -144,32 +144,32 @@ static NodeInfo nodeForOid(const QVector<int> &oid) QVector<int> mib; mib.append(0); // Magic undocumented code (CTL_UNSPEC ?) mib.append(1); // Magic undocumented code - foreach (int code, oid) + for (int code : oid) mib.append(code); // query node name size_t requiredLength = 0; - if (::sysctl(&mib[0], mib.count(), Q_NULLPTR, &requiredLength, Q_NULLPTR, 0) < 0) + if (::sysctl(&mib[0], mib.count(), nullptr, &requiredLength, nullptr, 0) < 0) return NodeInfo(); QByteArray name(requiredLength, 0); - if (::sysctl(&mib[0], mib.count(), name.data(), &requiredLength, Q_NULLPTR, 0) < 0) + if (::sysctl(&mib[0], mib.count(), name.data(), &requiredLength, nullptr, 0) < 0) return NodeInfo(); // query node value requiredLength = 0; - if (::sysctl(&oid[0], oid.count(), Q_NULLPTR, &requiredLength, Q_NULLPTR, 0) < 0) + if (::sysctl(&oid[0], oid.count(), nullptr, &requiredLength, nullptr, 0) < 0) return NodeInfo(); QByteArray value(requiredLength, 0); - if (::sysctl(&oid[0], oid.count(), value.data(), &requiredLength, Q_NULLPTR, 0) < 0) + if (::sysctl(&oid[0], oid.count(), value.data(), &requiredLength, nullptr, 0) < 0) return NodeInfo(); // query value format mib[1] = 4; // Magic undocumented code requiredLength = 0; - if (::sysctl(&mib[0], mib.count(), Q_NULLPTR, &requiredLength, Q_NULLPTR, 0) < 0) + if (::sysctl(&mib[0], mib.count(), nullptr, &requiredLength, nullptr, 0) < 0) return NodeInfo(); QByteArray buf(requiredLength, 0); - if (::sysctl(&mib[0], mib.count(), buf.data(), &requiredLength, Q_NULLPTR, 0) < 0) + if (::sysctl(&mib[0], mib.count(), buf.data(), &requiredLength, nullptr, 0) < 0) return NodeInfo(); QDataStream in(buf); @@ -195,7 +195,7 @@ static QList<NodeInfo> enumerateDesiredNodes(const QVector<int> &mib) QVector<int> oid = mib; - forever { + for (;;) { const QVector<int> nextoid = nextOid(oid); if (nextoid.isEmpty()) break; @@ -234,7 +234,8 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() QList<QSerialPortInfo> cuaCandidates; QList<QSerialPortInfo> ttyCandidates; - foreach (const QString &portName, deviceDir.entryList()) { + const auto portNames = deviceDir.entryList(); + for (const QString &portName : portNames) { if (portName.endsWith(QLatin1String(".init")) || portName.endsWith(QLatin1String(".lock"))) { continue; @@ -244,7 +245,7 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() priv.portName = portName; priv.device = QSerialPortInfoPrivate::portNameToSystemLocation(portName); - foreach (const NodeInfo &node, nodes) { + for (const NodeInfo &node : nodes) { const int pnpinfoindex = node.name.indexOf(QLatin1String("\%pnpinfo")); if (pnpinfoindex == -1) continue; @@ -288,7 +289,7 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() const QString descnode = QString(QLatin1String("%1\%desc")).arg(nodebase); // search for description and manufacturer properties - foreach (const NodeInfo &node, nodes) { + for (const NodeInfo &node : nodes) { if (node.name != descnode) continue; @@ -313,10 +314,10 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() QList<QSerialPortInfo> serialPortInfoList; - foreach (const QSerialPortInfo &cuaCandidate, cuaCandidates) { + for (const QSerialPortInfo &cuaCandidate : qAsConst(cuaCandidates)) { const QString cuaPortName = cuaCandidate.portName(); const QString cuaToken = deviceProperty(cuaPortName, "cua"); - foreach (const QSerialPortInfo &ttyCandidate, ttyCandidates) { + for (const QSerialPortInfo &ttyCandidate : qAsConst(ttyCandidates)) { const QString ttyPortName = ttyCandidate.portName(); const QString ttyToken = deviceProperty(ttyPortName, "tty"); if (cuaToken != ttyToken) diff --git a/src/serialport/qserialportinfo_osx.cpp b/src/serialport/qserialportinfo_osx.cpp index e2fc980..03cb59b 100644 --- a/src/serialport/qserialportinfo_osx.cpp +++ b/src/serialport/qserialportinfo_osx.cpp @@ -3,31 +3,37 @@ ** Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com> ** Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com> ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> -** Contact: http://www.qt.io/licensing/ +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -160,7 +166,7 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() QList<QSerialPortInfo> serialPortInfoList; - forever { + for (;;) { io_registry_entry_t serialPortService = ::IOIteratorNext(serialPortIterator); if (!serialPortService) break; @@ -170,7 +176,7 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() QString calloutDevice; QString dialinDevice; - forever { + for (;;) { if (calloutDevice.isEmpty()) calloutDevice = calloutDeviceSystemLocation(serialPortService); diff --git a/src/serialport/qserialportinfo_p.h b/src/serialport/qserialportinfo_p.h index 5a15dcf..483ff8c 100644 --- a/src/serialport/qserialportinfo_p.h +++ b/src/serialport/qserialportinfo_p.h @@ -3,31 +3,37 @@ ** Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com> ** Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com> ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> -** Contact: http://www.qt.io/licensing/ +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** diff --git a/src/serialport/qserialportinfo_unix.cpp b/src/serialport/qserialportinfo_unix.cpp index 8f39045..0f62a9d 100644 --- a/src/serialport/qserialportinfo_unix.cpp +++ b/src/serialport/qserialportinfo_unix.cpp @@ -3,31 +3,37 @@ ** Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com> ** Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com> ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> -** Contact: http://www.qt.io/licensing/ +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -84,7 +90,8 @@ static QStringList filteredDeviceFilePaths() deviceDir.setNameFilters(deviceFileNameFilterList); deviceDir.setFilter(QDir::Files | QDir::System | QDir::NoSymLinks); QStringList deviceFilePaths; - foreach (const QFileInfo &deviceFileInfo, deviceDir.entryInfoList()) { + const auto deviceFileInfos = deviceDir.entryInfoList(); + for (const QFileInfo &deviceFileInfo : deviceFileInfos) { const QString deviceAbsoluteFilePath = deviceFileInfo.absoluteFilePath(); #ifdef Q_OS_FREEBSD @@ -109,7 +116,8 @@ QList<QSerialPortInfo> availablePortsByFiltersOfDevices(bool &ok) { QList<QSerialPortInfo> serialPortInfoList; - foreach (const QString &deviceFilePath, filteredDeviceFilePaths()) { + const auto deviceFilePaths = filteredDeviceFilePaths(); + for (const QString &deviceFilePath : deviceFilePaths) { QSerialPortInfoPrivate priv; priv.device = deviceFilePath; priv.portName = QSerialPortInfoPrivate::portNameFromSystemLocation(deviceFilePath); @@ -158,10 +166,7 @@ static bool isRfcommDevice(const QString &portName) // provided by the tty0tty driver static bool isVirtualNullModemDevice(const QString &portName) { - if (!portName.startsWith(QLatin1String("tnt"))) - return false; - - return true; + return portName.startsWith(QLatin1String("tnt")); } static QString ueventProperty(const QDir &targetDir, const QByteArray &pattern) @@ -244,7 +249,8 @@ QList<QSerialPortInfo> availablePortsBySysfs(bool &ok) QList<QSerialPortInfo> serialPortInfoList; ttySysClassDir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot); - foreach (const QFileInfo &fileInfo, ttySysClassDir.entryInfoList()) { + const auto fileInfos = ttySysClassDir.entryInfoList(); + for (const QFileInfo &fileInfo : fileInfos) { if (!fileInfo.isSymLink()) continue; @@ -382,7 +388,6 @@ QList<QSerialPortInfo> availablePortsByUdev(bool &ok) if (!symbolsResolved) return QList<QSerialPortInfo>(); #endif - static const QString rfcommDeviceName(QStringLiteral("rfcomm")); QScopedPointer<struct ::udev, ScopedPointerUdevDeleter> udev(::udev_new()); diff --git a/src/serialport/qserialportinfo_win.cpp b/src/serialport/qserialportinfo_win.cpp index 1d55197..1bab3e9 100644 --- a/src/serialport/qserialportinfo_win.cpp +++ b/src/serialport/qserialportinfo_win.cpp @@ -3,31 +3,37 @@ ** Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com> ** Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com> ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> -** Contact: http://www.qt.io/licensing/ +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -59,7 +65,7 @@ QT_BEGIN_NAMESPACE static QStringList portNamesFromHardwareDeviceMap() { - HKEY hKey = Q_NULLPTR; + HKEY hKey = nullptr; if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS) return QStringList(); @@ -73,10 +79,10 @@ static QStringList portNamesFromHardwareDeviceMap() std::vector<wchar_t> outputValueName(MaximumValueNameInChars, 0); std::vector<wchar_t> outputBuffer(MAX_PATH + 1, 0); DWORD bytesRequired = MAX_PATH; - forever { + for (;;) { DWORD requiredValueNameChars = MaximumValueNameInChars; const LONG ret = ::RegEnumValue(hKey, index, &outputValueName[0], &requiredValueNameChars, - Q_NULLPTR, Q_NULLPTR, reinterpret_cast<PBYTE>(&outputBuffer[0]), &bytesRequired); + nullptr, nullptr, reinterpret_cast<PBYTE>(&outputBuffer[0]), &bytesRequired); if (ret == ERROR_MORE_DATA) { outputBuffer.resize(bytesRequired / sizeof(wchar_t) + 2, 0); } else if (ret == ERROR_SUCCESS) { @@ -97,7 +103,7 @@ static QString deviceRegistryProperty(HDEVINFO deviceInfoSet, DWORD dataType = 0; std::vector<wchar_t> outputBuffer(MAX_PATH + 1, 0); DWORD bytesRequired = MAX_PATH; - forever { + for (;;) { if (::SetupDiGetDeviceRegistryProperty(deviceInfoSet, deviceInfoData, property, &dataType, reinterpret_cast<PBYTE>(&outputBuffer[0]), bytesRequired, &bytesRequired)) { @@ -152,15 +158,15 @@ static QString devicePortName(HDEVINFO deviceInfoSet, PSP_DEVINFO_DATA deviceInf L"PortNumber\0" }; - static const int keyTokensCount = sizeof(keyTokens) / sizeof(keyTokens[0]); + enum { KeyTokensCount = sizeof(keyTokens) / sizeof(keyTokens[0]) }; QString portName; - for (int i = 0; i < keyTokensCount; ++i) { + for (int i = 0; i < KeyTokensCount; ++i) { DWORD dataType = 0; std::vector<wchar_t> outputBuffer(MAX_PATH + 1, 0); DWORD bytesRequired = MAX_PATH; - forever { - const LONG ret = ::RegQueryValueEx(key, keyTokens[i], Q_NULLPTR, &dataType, + for (;;) { + const LONG ret = ::RegQueryValueEx(key, keyTokens[i], nullptr, &dataType, reinterpret_cast<PBYTE>(&outputBuffer[0]), &bytesRequired); if (ret == ERROR_MORE_DATA) { outputBuffer.resize(bytesRequired / sizeof(wchar_t) + 2, 0); @@ -181,23 +187,6 @@ static QString devicePortName(HDEVINFO deviceInfoSet, PSP_DEVINFO_DATA deviceInf return portName; } -class SerialPortNameEqualFunctor -{ -public: - explicit SerialPortNameEqualFunctor(const QString &serialPortName) - : m_serialPortName(serialPortName) - { - } - - bool operator() (const QSerialPortInfo &serialPortInfo) const - { - return serialPortInfo.portName() == m_serialPortName; - } - -private: - const QString &m_serialPortName; -}; - static QString deviceDescription(HDEVINFO deviceInfoSet, PSP_DEVINFO_DATA deviceInfoData) { @@ -267,7 +256,7 @@ static QString parseDeviceSerialNumber(const QString &instanceIdentifier) static QString deviceSerialNumber(QString instanceIdentifier, DEVINST deviceInstanceNumber) { - forever { + for (;;) { const QString result = parseDeviceSerialNumber(instanceIdentifier); if (!result.isEmpty()) return result; @@ -282,6 +271,15 @@ static QString deviceSerialNumber(QString instanceIdentifier, return QString(); } +static bool anyOfPorts(const QList<QSerialPortInfo> &ports, const QString &portName) +{ + const auto end = ports.end(); + auto isPortNamesEquals = [&portName](const QSerialPortInfo &portInfo) { + return portInfo.portName() == portName; + }; + return std::find_if(ports.begin(), end, isPortNamesEquals) != end; +} + QList<QSerialPortInfo> QSerialPortInfo::availablePorts() { static const struct { @@ -293,12 +291,12 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() { GUID_DEVINTERFACE_MODEM, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE } }; - static const int setupTokensCount = sizeof(setupTokens) / sizeof(setupTokens[0]); + enum { SetupTokensCount = sizeof(setupTokens) / sizeof(setupTokens[0]) }; QList<QSerialPortInfo> serialPortInfoList; - for (int i = 0; i < setupTokensCount; ++i) { - const HDEVINFO deviceInfoSet = ::SetupDiGetClassDevs(&setupTokens[i].guid, Q_NULLPTR, Q_NULLPTR, setupTokens[i].flags); + for (int i = 0; i < SetupTokensCount; ++i) { + const HDEVINFO deviceInfoSet = ::SetupDiGetClassDevs(&setupTokens[i].guid, nullptr, nullptr, setupTokens[i].flags); if (deviceInfoSet == INVALID_HANDLE_VALUE) return serialPortInfoList; @@ -312,10 +310,8 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() if (portName.isEmpty() || portName.contains(QLatin1String("LPT"))) continue; - if (std::find_if(serialPortInfoList.begin(), serialPortInfoList.end(), - SerialPortNameEqualFunctor(portName)) != serialPortInfoList.end()) { + if (anyOfPorts(serialPortInfoList, portName)) continue; - } QSerialPortInfoPrivate priv; @@ -338,9 +334,9 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() ::SetupDiDestroyDeviceInfoList(deviceInfoSet); } - foreach (const QString &portName, portNamesFromHardwareDeviceMap()) { - if (std::find_if(serialPortInfoList.begin(), serialPortInfoList.end(), - SerialPortNameEqualFunctor(portName)) == serialPortInfoList.end()) { + const auto portNames = portNamesFromHardwareDeviceMap(); + for (const QString &portName : portNames) { + if (!anyOfPorts(serialPortInfoList, portName)) { QSerialPortInfoPrivate priv; priv.portName = portName; priv.device = QSerialPortInfoPrivate::portNameToSystemLocation(portName); @@ -355,7 +351,7 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() bool QSerialPortInfo::isBusy() const { const HANDLE handle = ::CreateFile(reinterpret_cast<const wchar_t*>(systemLocation().utf16()), - GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, Q_NULLPTR); + GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr); if (handle == INVALID_HANDLE_VALUE) { if (::GetLastError() == ERROR_ACCESS_DENIED) @@ -371,7 +367,7 @@ bool QSerialPortInfo::isBusy() const bool QSerialPortInfo::isValid() const { const HANDLE handle = ::CreateFile(reinterpret_cast<const wchar_t*>(systemLocation().utf16()), - GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, Q_NULLPTR); + GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr); if (handle == INVALID_HANDLE_VALUE) { if (::GetLastError() != ERROR_ACCESS_DENIED) diff --git a/src/serialport/qserialportinfo_wince.cpp b/src/serialport/qserialportinfo_wince.cpp deleted file mode 100644 index 9755d74..0000000 --- a/src/serialport/qserialportinfo_wince.cpp +++ /dev/null @@ -1,167 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com> -** Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com> -** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the QtSerialPort module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL21$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qserialportinfo.h" -#include "qserialportinfo_p.h" -#include "qserialport_p.h" - -#include <QtCore/qstringlist.h> - -QT_BEGIN_NAMESPACE - -static QString findDescription(HKEY parentKeyHandle, const QString &subKey) -{ - const static QString valueName(QStringLiteral("FriendlyName")); - - QString result; - HKEY hSubKey = Q_NULLPTR; - LONG res = ::RegOpenKeyEx(parentKeyHandle, reinterpret_cast<const wchar_t *>(subKey.utf16()), - 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hSubKey); - - if (res == ERROR_SUCCESS) { - - DWORD dataType = 0; - DWORD dataSize = 0; - res = ::RegQueryValueEx(hSubKey, reinterpret_cast<const wchar_t *>(valueName.utf16()), - Q_NULLPTR, &dataType, Q_NULLPTR, &dataSize); - - if (res == ERROR_SUCCESS) { - QByteArray data(dataSize, 0); - res = ::RegQueryValueEx(hSubKey, reinterpret_cast<const wchar_t *>(valueName.utf16()), - Q_NULLPTR, Q_NULLPTR, - reinterpret_cast<unsigned char *>(data.data()), - &dataSize); - - if (res == ERROR_SUCCESS) { - switch (dataType) { - case REG_EXPAND_SZ: - case REG_SZ: - if (dataSize) - result = QString::fromWCharArray(reinterpret_cast<const wchar_t *>(data.constData())); - break; - default: - break; - } - } - } else { - DWORD index = 0; - dataSize = 255; // Max. key length (see MSDN). - QByteArray data(dataSize, 0); - while (::RegEnumKeyEx(hSubKey, index++, - reinterpret_cast<wchar_t *>(data.data()), &dataSize, - Q_NULLPTR, Q_NULLPTR, Q_NULLPTR, Q_NULLPTR) == ERROR_SUCCESS) { - - result = findDescription(hSubKey, - QString::fromUtf16(reinterpret_cast<ushort *>(data.data()), dataSize)); - if (!result.isEmpty()) - break; - } - } - ::RegCloseKey(hSubKey); - } - return result; -} - -QList<QSerialPortInfo> QSerialPortInfo::availablePorts() -{ - QList<QSerialPortInfo> serialPortInfoList; - - DEVMGR_DEVICE_INFORMATION di; - di.dwSize = sizeof(di); - const HANDLE hSearch = ::FindFirstDevice(DeviceSearchByLegacyName, - L"COM*", - &di); - if (hSearch != INVALID_HANDLE_VALUE) { - do { - QSerialPortInfoPrivate priv; - priv.device = QString::fromWCharArray(di.szLegacyName); - priv.portName = QSerialPortInfoPrivate::portNameFromSystemLocation(priv.device); - priv.description = findDescription(HKEY_LOCAL_MACHINE, - QString::fromWCharArray(di.szDeviceKey)); - - serialPortInfoList.append(priv); - - } while (::FindNextDevice(hSearch, &di)); - ::FindClose(hSearch); - } - - return serialPortInfoList; -} - -#if QT_DEPRECATED_SINCE(5, 6) -bool QSerialPortInfo::isBusy() const -{ - const HANDLE handle = ::CreateFile(reinterpret_cast<const wchar_t*>(systemLocation().utf16()), - GENERIC_READ | GENERIC_WRITE, 0, Q_NULLPTR, OPEN_EXISTING, 0, Q_NULLPTR); - - if (handle == INVALID_HANDLE_VALUE) { - if (::GetLastError() == ERROR_ACCESS_DENIED) - return true; - } else { - ::CloseHandle(handle); - } - return false; -} -#endif // QT_DEPRECATED_SINCE(5, 6) - -#if QT_DEPRECATED_SINCE(5, 2) -bool QSerialPortInfo::isValid() const -{ - const HANDLE handle = ::CreateFile(reinterpret_cast<const wchar_t*>(systemLocation().utf16()), - GENERIC_READ | GENERIC_WRITE, 0, Q_NULLPTR, OPEN_EXISTING, 0, Q_NULLPTR); - - if (handle == INVALID_HANDLE_VALUE) { - if (::GetLastError() != ERROR_ACCESS_DENIED) - return false; - } else { - ::CloseHandle(handle); - } - return true; -} -#endif // QT_DEPRECATED_SINCE(5, 2) - -QString QSerialPortInfoPrivate::portNameToSystemLocation(const QString &source) -{ - return source.endsWith(QLatin1Char(':')) - ? source : (source + QLatin1Char(':')); -} - -QString QSerialPortInfoPrivate::portNameFromSystemLocation(const QString &source) -{ - return source.endsWith(QLatin1Char(':')) - ? source.mid(0, source.size() - 1) : source; -} - -QT_END_NAMESPACE diff --git a/src/serialport/qtudev_p.h b/src/serialport/qtudev_p.h index 6f2cabd..af2dab2 100644 --- a/src/serialport/qtudev_p.h +++ b/src/serialport/qtudev_p.h @@ -1,31 +1,37 @@ /**************************************************************************** ** ** Copyright (C) 2013 Laszlo Papp <lpapp@kde.org> -** Contact: http://www.qt.io/licensing/ +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -68,7 +74,7 @@ struct udev; #define udev_list_entry_foreach(list_entry, first_entry) \ for (list_entry = first_entry; \ - list_entry != NULL; \ + list_entry != nullptr; \ list_entry = udev_list_entry_get_next(list_entry)) struct udev_device; diff --git a/src/serialport/serialport-lib.pri b/src/serialport/serialport-lib.pri index 7179bea..02dc3e5 100644 --- a/src/serialport/serialport-lib.pri +++ b/src/serialport/serialport-lib.pri @@ -27,12 +27,6 @@ win32:!wince* { LIBS_PRIVATE += -lsetupapi -ladvapi32 } -wince* { - SOURCES += \ - $$PWD/qserialport_wince.cpp \ - $$PWD/qserialportinfo_wince.cpp -} - unix { SOURCES += \ $$PWD/qserialport_unix.cpp |