diff options
author | Frederik Gladhorn <frederik.gladhorn@theqtcompany.com> | 2015-01-20 12:33:16 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2015-01-20 12:33:16 +0100 |
commit | 5d819074af293c0855090ceb23a26fe8401a4255 (patch) | |
tree | 7f273c7438387ff86287aa3c344fae8fed6f56c5 | |
parent | b1005907d26a3e2f5dffaec93d656a0c1210961f (diff) | |
parent | 40d3bc669e7f556ac8d6492ad324827dcf6da6c1 (diff) | |
download | qtserialport-5d819074af293c0855090ceb23a26fe8401a4255.tar.gz |
Merge "Merge remote-tracking branch 'origin/5.4' into dev" into refs/staging/dev
-rw-r--r-- | src/serialport/qserialport.cpp | 18 | ||||
-rw-r--r-- | src/serialport/qserialport.h | 2 | ||||
-rw-r--r-- | src/serialport/qserialport_unix.cpp | 37 | ||||
-rw-r--r-- | src/serialport/qserialport_win.cpp | 30 | ||||
-rw-r--r-- | src/serialport/qserialport_wince.cpp | 18 | ||||
-rw-r--r-- | src/serialport/qserialportinfo.cpp | 8 | ||||
-rw-r--r-- | src/serialport/qserialportinfo.h | 1 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_mac.cpp | 68 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_p.h | 12 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_unix.cpp | 74 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_win.cpp | 43 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_wince.cpp | 24 | ||||
-rw-r--r-- | tests/auto/auto.pro | 5 | ||||
-rw-r--r-- | tests/auto/qserialportinfo/qserialportinfo.pro | 11 | ||||
-rw-r--r-- | tests/auto/qserialportinfo/tst_qserialportinfo.cpp | 114 | ||||
-rw-r--r-- | tests/auto/qserialportinfoprivate/qserialportinfoprivate.pro | 4 | ||||
-rw-r--r-- | tests/auto/qserialportinfoprivate/tst_qserialportinfoprivate.cpp | 110 |
17 files changed, 404 insertions, 175 deletions
diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp index f4f7478..cc3e211 100644 --- a/src/serialport/qserialport.cpp +++ b/src/serialport/qserialport.cpp @@ -36,6 +36,7 @@ #include "qserialport.h" #include "qserialportinfo.h" +#include "qserialportinfo_p.h" #include "qserialport_p.h" @@ -448,7 +449,7 @@ QSerialPort::~QSerialPort() void QSerialPort::setPortName(const QString &name) { Q_D(QSerialPort); - d->systemLocation = QSerialPortPrivate::portNameToSystemLocation(name); + d->systemLocation = QSerialPortInfoPrivate::portNameToSystemLocation(name); } /*! @@ -459,7 +460,7 @@ void QSerialPort::setPortName(const QString &name) void QSerialPort::setPort(const QSerialPortInfo &serialPortInfo) { Q_D(QSerialPort); - d->systemLocation = QSerialPortPrivate::portNameToSystemLocation(serialPortInfo.systemLocation()); + d->systemLocation = serialPortInfo.systemLocation(); } /*! @@ -473,23 +474,16 @@ void QSerialPort::setPort(const QSerialPortInfo &serialPortInfo) \li Brief Description \row \li Windows - \li Removes the prefix "\\\\.\\" from the system location + \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 GNU/Linux + \li Unix, BSD \li Removes the prefix "/dev/" from the system location and returns the remainder of the string. - \row - \li Mac OSX - \li Removes the prefix "/dev/cu." and "/dev/tty." from the - system location and returns the remainder of the string. - \row - \li Other *nix - \li The same as for GNU/Linux. \endtable \sa setPort(), QSerialPortInfo::portName() @@ -497,7 +491,7 @@ void QSerialPort::setPort(const QSerialPortInfo &serialPortInfo) QString QSerialPort::portName() const { Q_D(const QSerialPort); - return QSerialPortPrivate::portNameFromSystemLocation(d->systemLocation); + return QSerialPortInfoPrivate::portNameFromSystemLocation(d->systemLocation); } /*! diff --git a/src/serialport/qserialport.h b/src/serialport/qserialport.h index ebd748a..45f2054 100644 --- a/src/serialport/qserialport.h +++ b/src/serialport/qserialport.h @@ -279,7 +279,7 @@ private: Q_DISABLE_COPY(QSerialPort) -#if defined (Q_OS_WIN32) || defined(Q_OS_WIN64) +#if defined (Q_OS_WIN32) Q_PRIVATE_SLOT(d_func(), bool _q_startAsyncWrite()) Q_PRIVATE_SLOT(d_func(), void _q_notified(quint32, quint32, OVERLAPPED*)) #endif diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp index 46c9d8b..e9f69bf 100644 --- a/src/serialport/qserialport_unix.cpp +++ b/src/serialport/qserialport_unix.cpp @@ -34,6 +34,7 @@ ****************************************************************************/ #include "qserialport_p.h" +#include "qserialportinfo_p.h" #include <errno.h> #include <sys/time.h> @@ -148,7 +149,7 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode) { Q_Q(QSerialPort); - QString lockFilePath = serialPortLockFilePath(portNameFromSystemLocation(systemLocation)); + QString lockFilePath = serialPortLockFilePath(QSerialPortInfoPrivate::portNameFromSystemLocation(systemLocation)); bool isLockFileEmpty = lockFilePath.isEmpty(); if (isLockFileEmpty) { qWarning("Failed to create a lock file for opening the device"); @@ -367,7 +368,6 @@ bool QSerialPortPrivate::waitForReadyRead(int msecs) Q_Q(QSerialPort); QElapsedTimer stopWatch; - stopWatch.start(); do { @@ -398,7 +398,6 @@ bool QSerialPortPrivate::waitForBytesWritten(int msecs) return false; QElapsedTimer stopWatch; - stopWatch.start(); forever { @@ -1145,38 +1144,6 @@ qint64 QSerialPortPrivate::readPerChar(char *data, qint64 maxSize) return ret; } -#ifdef Q_OS_MAC -static const QString defaultFilePathPrefix = QStringLiteral("/dev/cu."); -static const QString unusedFilePathPrefix = QStringLiteral("/dev/tty."); -#else -static const QString defaultFilePathPrefix = QStringLiteral("/dev/"); -#endif - -QString QSerialPortPrivate::portNameToSystemLocation(const QString &port) -{ - QString ret = port; - -#ifdef Q_OS_MAC - ret.remove(unusedFilePathPrefix); -#endif - - if (!ret.contains(defaultFilePathPrefix)) - ret.prepend(defaultFilePathPrefix); - return ret; -} - -QString QSerialPortPrivate::portNameFromSystemLocation(const QString &location) -{ - QString ret = location; - -#ifdef Q_OS_MAC - ret.remove(unusedFilePathPrefix); -#endif - - ret.remove(defaultFilePathPrefix); - return ret; -} - typedef QMap<qint32, qint32> BaudRateMap; // The OS specific defines can be found in termios.h diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index e92dce2..db3ef69 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -273,15 +273,15 @@ qint64 QSerialPortPrivate::readData(char *data, qint64 maxSize) bool QSerialPortPrivate::waitForReadyRead(int msecs) { - QElapsedTimer stopWatch; - stopWatch.start(); - if (!writeStarted && !_q_startAsyncWrite()) return false; const qint64 initialReadBufferSize = buffer.size(); qint64 currentReadBufferSize = initialReadBufferSize; + QElapsedTimer stopWatch; + stopWatch.start(); + do { OVERLAPPED *overlapped = waitForNotified(timeoutValue(msecs, stopWatch.elapsed())); if (!overlapped) @@ -321,12 +321,12 @@ bool QSerialPortPrivate::waitForBytesWritten(int msecs) if (writeBuffer.isEmpty()) return false; - QElapsedTimer stopWatch; - stopWatch.start(); - if (!writeStarted && !_q_startAsyncWrite()) return false; + QElapsedTimer stopWatch; + stopWatch.start(); + forever { OVERLAPPED *overlapped = waitForNotified(timeoutValue(msecs, stopWatch.elapsed())); if (!overlapped) @@ -843,24 +843,6 @@ QSerialPort::SerialPortError QSerialPortPrivate::decodeSystemError(int systemErr return error; } -static const QString defaultPathPrefix = QStringLiteral("\\\\.\\"); - -QString QSerialPortPrivate::portNameToSystemLocation(const QString &port) -{ - QString ret = port; - if (!ret.contains(defaultPathPrefix)) - ret.prepend(defaultPathPrefix); - return ret; -} - -QString QSerialPortPrivate::portNameFromSystemLocation(const QString &location) -{ - QString ret = location; - if (ret.contains(defaultPathPrefix)) - ret.remove(defaultPathPrefix); - return ret; -} - // This table contains standard values of baud rates that // are defined in MSDN and/or in Win SDK file winbase.h diff --git a/src/serialport/qserialport_wince.cpp b/src/serialport/qserialport_wince.cpp index 9258e7e..f8a63f2 100644 --- a/src/serialport/qserialport_wince.cpp +++ b/src/serialport/qserialport_wince.cpp @@ -340,7 +340,6 @@ bool QSerialPortPrivate::waitForReadyRead(int msec) return true; QElapsedTimer stopWatch; - stopWatch.start(); forever { @@ -369,7 +368,6 @@ bool QSerialPortPrivate::waitForBytesWritten(int msec) return false; QElapsedTimer stopWatch; - stopWatch.start(); forever { @@ -762,22 +760,6 @@ bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectFor return false; } -QString QSerialPortPrivate::portNameToSystemLocation(const QString &port) -{ - QString ret = port; - if (!ret.contains(QLatin1Char(':'))) - ret.append(QLatin1Char(':')); - return ret; -} - -QString QSerialPortPrivate::portNameFromSystemLocation(const QString &location) -{ - QString ret = location; - if (ret.contains(QLatin1Char(':'))) - ret.remove(QLatin1Char(':')); - return ret; -} - static const QList<qint32> standardBaudRatePairList() { diff --git a/src/serialport/qserialportinfo.cpp b/src/serialport/qserialportinfo.cpp index 23a4d1c..ced4361 100644 --- a/src/serialport/qserialportinfo.cpp +++ b/src/serialport/qserialportinfo.cpp @@ -65,7 +65,6 @@ QT_BEGIN_NAMESPACE \sa isNull() */ QSerialPortInfo::QSerialPortInfo() - : d_ptr(new QSerialPortInfoPrivate) { } @@ -81,7 +80,6 @@ QSerialPortInfo::QSerialPortInfo(const QSerialPortInfo &other) Constructs a QSerialPortInfo object from serial \a port. */ QSerialPortInfo::QSerialPortInfo(const QSerialPort &port) - : d_ptr(new QSerialPortInfoPrivate) { foreach (const QSerialPortInfo &serialPortInfo, availablePorts()) { if (port.portName() == serialPortInfo.portName()) { @@ -99,7 +97,6 @@ QSerialPortInfo::QSerialPortInfo(const QSerialPort &port) instance for that port. */ QSerialPortInfo::QSerialPortInfo(const QString &name) - : d_ptr(new QSerialPortInfoPrivate) { foreach (const QSerialPortInfo &serialPortInfo, availablePorts()) { if (name == serialPortInfo.portName()) { @@ -109,6 +106,11 @@ QSerialPortInfo::QSerialPortInfo(const QString &name) } } +QSerialPortInfo::QSerialPortInfo(const QSerialPortInfoPrivate &dd) + : d_ptr(new QSerialPortInfoPrivate(dd)) +{ +} + /*! Destroys the QSerialPortInfo object. References to the values in the object become invalid. diff --git a/src/serialport/qserialportinfo.h b/src/serialport/qserialportinfo.h index 262e54d..0c7a0f1 100644 --- a/src/serialport/qserialportinfo.h +++ b/src/serialport/qserialportinfo.h @@ -81,6 +81,7 @@ public: static QList<QSerialPortInfo> availablePorts(); private: + QSerialPortInfo(const QSerialPortInfoPrivate &dd); friend QList<QSerialPortInfo> availablePortsByUdev(); friend QList<QSerialPortInfo> availablePortsBySysfs(); friend QList<QSerialPortInfo> availablePortsByFiltersOfDevices(); diff --git a/src/serialport/qserialportinfo_mac.cpp b/src/serialport/qserialportinfo_mac.cpp index 8b1536f..28d6934 100644 --- a/src/serialport/qserialportinfo_mac.cpp +++ b/src/serialport/qserialportinfo_mac.cpp @@ -78,15 +78,15 @@ static quint16 searchShortIntProperty(io_registry_entry_t ioRegistryEntry, return value; } -static bool isCompleteInfo(const QSerialPortInfo &portInfo) +static bool isCompleteInfo(const QSerialPortInfoPrivate &priv) { - return !portInfo.portName().isEmpty() - && !portInfo.systemLocation().isEmpty() - && !portInfo.manufacturer().isEmpty() - && !portInfo.description().isEmpty() - && !portInfo.serialNumber().isEmpty() - && portInfo.hasProductIdentifier() - && portInfo.hasVendorIdentifier(); + return !priv.portName.isEmpty() + && !priv.device.isEmpty() + && !priv.manufacturer.isEmpty() + && !priv.description.isEmpty() + && !priv.serialNumber.isEmpty() + && priv.hasProductIdentifier + && priv.hasVendorIdentifier; } static QString devicePortName(io_registry_entry_t ioRegistryEntry) @@ -160,37 +160,37 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() if (!serialPortService) break; - QSerialPortInfo serialPortInfo; + QSerialPortInfoPrivate priv; forever { - if (serialPortInfo.portName().isEmpty()) - serialPortInfo.d_ptr->portName = devicePortName(serialPortService); + if (priv.portName.isEmpty()) + priv.portName = devicePortName(serialPortService); - if (serialPortInfo.systemLocation().isEmpty()) - serialPortInfo.d_ptr->device = deviceSystemLocation(serialPortService); + if (priv.device.isEmpty()) + priv.device = deviceSystemLocation(serialPortService); - if (serialPortInfo.description().isEmpty()) - serialPortInfo.d_ptr->description = deviceDescription(serialPortService); + if (priv.description.isEmpty()) + priv.description = deviceDescription(serialPortService); - if (serialPortInfo.manufacturer().isEmpty()) - serialPortInfo.d_ptr->manufacturer = deviceManufacturer(serialPortService); + if (priv.manufacturer.isEmpty()) + priv.manufacturer = deviceManufacturer(serialPortService); - if (serialPortInfo.serialNumber().isEmpty()) - serialPortInfo.d_ptr->serialNumber = deviceSerialNumber(serialPortService); + if (priv.serialNumber.isEmpty()) + priv.serialNumber = deviceSerialNumber(serialPortService); - if (!serialPortInfo.hasVendorIdentifier()) { - serialPortInfo.d_ptr->vendorIdentifier = + if (!priv.hasVendorIdentifier) { + priv.vendorIdentifier = deviceVendorIdentifier(serialPortService, - serialPortInfo.d_ptr->hasVendorIdentifier); + priv.hasVendorIdentifier); } - if (!serialPortInfo.hasProductIdentifier()) { - serialPortInfo.d_ptr->productIdentifier = + if (!priv.hasProductIdentifier) { + priv.productIdentifier = deviceProductIdentifier(serialPortService, - serialPortInfo.d_ptr->hasProductIdentifier); + priv.hasProductIdentifier); } - if (isCompleteInfo(serialPortInfo)) { + if (isCompleteInfo(priv)) { ::IOObjectRelease(serialPortService); break; } @@ -200,7 +200,7 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() break; } - serialPortInfoList.append(serialPortInfo); + serialPortInfoList.append(priv); } ::IOObjectRelease(serialPortIterator); @@ -242,4 +242,18 @@ bool QSerialPortInfo::isValid() const return f.exists(); } +QString QSerialPortInfoPrivate::portNameToSystemLocation(const QString &source) +{ + return (source.startsWith(QLatin1Char('/')) + || source.startsWith(QStringLiteral("./")) + || source.startsWith(QStringLiteral("../"))) + ? source : (QStringLiteral("/dev/") + source); +} + +QString QSerialPortInfoPrivate::portNameFromSystemLocation(const QString &source) +{ + return source.startsWith(QStringLiteral("/dev/")) + ? source.mid(5) : source; +} + QT_END_NAMESPACE diff --git a/src/serialport/qserialportinfo_p.h b/src/serialport/qserialportinfo_p.h index 6ab4117..c9f1086 100644 --- a/src/serialport/qserialportinfo_p.h +++ b/src/serialport/qserialportinfo_p.h @@ -51,7 +51,7 @@ QT_BEGIN_NAMESPACE -class QSerialPortInfoPrivate +class Q_AUTOTEST_EXPORT QSerialPortInfoPrivate { public: QSerialPortInfoPrivate() @@ -59,9 +59,15 @@ public: , productIdentifier(0) , hasVendorIdentifier(false) , hasProductIdentifier(false) - {} + { + } + + ~QSerialPortInfoPrivate() + { + } - ~QSerialPortInfoPrivate() {} + static QString portNameToSystemLocation(const QString &source); + static QString portNameFromSystemLocation(const QString &source); QString portName; QString device; diff --git a/src/serialport/qserialportinfo_unix.cpp b/src/serialport/qserialportinfo_unix.cpp index a7a1186..4395d9b 100644 --- a/src/serialport/qserialportinfo_unix.cpp +++ b/src/serialport/qserialportinfo_unix.cpp @@ -98,10 +98,10 @@ QList<QSerialPortInfo> availablePortsByFiltersOfDevices() QList<QSerialPortInfo> serialPortInfoList; foreach (const QString &deviceFilePath, filteredDeviceFilePaths()) { - QSerialPortInfo serialPortInfo; - serialPortInfo.d_ptr->device = deviceFilePath; - serialPortInfo.d_ptr->portName = QSerialPortPrivate::portNameFromSystemLocation(deviceFilePath); - serialPortInfoList.append(serialPortInfo); + QSerialPortInfoPrivate priv; + priv.device = deviceFilePath; + priv.portName = QSerialPortInfoPrivate::portNameFromSystemLocation(deviceFilePath); + serialPortInfoList.append(priv); } return serialPortInfoList; @@ -125,7 +125,7 @@ QList<QSerialPortInfo> availablePortsBySysfs() if (lastIndexOfSlash == -1) continue; - QSerialPortInfo serialPortInfo; + QSerialPortInfoPrivate priv; if (targetPath.contains(QStringLiteral("pnp"))) { // TODO: Obtain more information #ifndef Q_OS_ANDROID @@ -155,26 +155,26 @@ QList<QSerialPortInfo> availablePortsBySysfs() QFile description(QFileInfo(targetDir, QStringLiteral("product")).absoluteFilePath()); if (description.open(QIODevice::ReadOnly | QIODevice::Text)) - serialPortInfo.d_ptr->description = QString::fromLatin1(description.readAll()).simplified(); + priv.description = QString::fromLatin1(description.readAll()).simplified(); QFile manufacturer(QFileInfo(targetDir, QStringLiteral("manufacturer")).absoluteFilePath()); if (manufacturer.open(QIODevice::ReadOnly | QIODevice::Text)) - serialPortInfo.d_ptr->manufacturer = QString::fromLatin1(manufacturer.readAll()).simplified(); + priv.manufacturer = QString::fromLatin1(manufacturer.readAll()).simplified(); QFile serialNumber(QFileInfo(targetDir, QStringLiteral("serial")).absoluteFilePath()); if (serialNumber.open(QIODevice::ReadOnly | QIODevice::Text)) - serialPortInfo.d_ptr->serialNumber = QString::fromLatin1(serialNumber.readAll()).simplified(); + priv.serialNumber = QString::fromLatin1(serialNumber.readAll()).simplified(); QFile vendorIdentifier(QFileInfo(targetDir, QStringLiteral("idVendor")).absoluteFilePath()); if (vendorIdentifier.open(QIODevice::ReadOnly | QIODevice::Text)) { - serialPortInfo.d_ptr->vendorIdentifier = QString::fromLatin1(vendorIdentifier.readAll()) - .toInt(&serialPortInfo.d_ptr->hasVendorIdentifier, 16); + priv.vendorIdentifier = QString::fromLatin1(vendorIdentifier.readAll()) + .toInt(&priv.hasVendorIdentifier, 16); } QFile productIdentifier(QFileInfo(targetDir, QStringLiteral("idProduct")).absoluteFilePath()); if (productIdentifier.open(QIODevice::ReadOnly | QIODevice::Text)) { - serialPortInfo.d_ptr->productIdentifier = QString::fromLatin1(productIdentifier.readAll()) - .toInt(&serialPortInfo.d_ptr->hasProductIdentifier, 16); + priv.productIdentifier = QString::fromLatin1(productIdentifier.readAll()) + .toInt(&priv.hasProductIdentifier, 16); } break; @@ -185,13 +185,13 @@ QList<QSerialPortInfo> availablePortsBySysfs() QDir targetDir(targetPath + QStringLiteral("/device")); QFile vendorIdentifier(QFileInfo(targetDir, QStringLiteral("vendor")).absoluteFilePath()); if (vendorIdentifier.open(QIODevice::ReadOnly | QIODevice::Text)) { - serialPortInfo.d_ptr->vendorIdentifier = QString::fromLatin1(vendorIdentifier.readAll()) - .toInt(&serialPortInfo.d_ptr->hasVendorIdentifier, 16); + priv.vendorIdentifier = QString::fromLatin1(vendorIdentifier.readAll()) + .toInt(&priv.hasVendorIdentifier, 16); } QFile productIdentifier(QFileInfo(targetDir, QStringLiteral("device")).absoluteFilePath()); if (productIdentifier.open(QIODevice::ReadOnly | QIODevice::Text)) { - serialPortInfo.d_ptr->productIdentifier = QString::fromLatin1(productIdentifier.readAll()) - .toInt(&serialPortInfo.d_ptr->hasProductIdentifier, 16); + priv.productIdentifier = QString::fromLatin1(productIdentifier.readAll()) + .toInt(&priv.hasProductIdentifier, 16); } // TODO: Obtain more information about the device } else if (targetPath.contains(QStringLiteral(".serial/tty/tty"))) { @@ -201,9 +201,9 @@ QList<QSerialPortInfo> availablePortsBySysfs() continue; } - serialPortInfo.d_ptr->portName = targetPath.mid(lastIndexOfSlash + 1); - serialPortInfo.d_ptr->device = QSerialPortPrivate::portNameToSystemLocation(serialPortInfo.d_ptr->portName); - serialPortInfoList.append(serialPortInfo); + priv.portName = targetPath.mid(lastIndexOfSlash + 1); + priv.device = QSerialPortInfoPrivate::portNameToSystemLocation(priv.portName); + serialPortInfoList.append(priv); } return serialPortInfoList; @@ -318,25 +318,25 @@ QList<QSerialPortInfo> availablePortsByUdev() if (!dev) return serialPortInfoList; - QSerialPortInfo serialPortInfo; + QSerialPortInfoPrivate priv; - serialPortInfo.d_ptr->device = QString::fromLatin1(::udev_device_get_devnode(dev.data())); - serialPortInfo.d_ptr->portName = QString::fromLatin1(::udev_device_get_sysname(dev.data())); + priv.device = QString::fromLatin1(::udev_device_get_devnode(dev.data())); + priv.portName = QString::fromLatin1(::udev_device_get_sysname(dev.data())); udev_device *parentdev = ::udev_device_get_parent(dev.data()); if (parentdev) { if (checkUdevForSerial8250Driver(parentdev)) continue; - serialPortInfo.d_ptr->description = getUdevModelName(dev.data()); - serialPortInfo.d_ptr->manufacturer = getUdevVendorName(dev.data()); - serialPortInfo.d_ptr->serialNumber = getUdevSerialNumber(dev.data()); - serialPortInfo.d_ptr->vendorIdentifier = getUdevVendorIdentifier(dev.data(), serialPortInfo.d_ptr->hasVendorIdentifier); - serialPortInfo.d_ptr->productIdentifier = getUdevModelIdentifier(dev.data(), serialPortInfo.d_ptr->hasProductIdentifier); + priv.description = getUdevModelName(dev.data()); + priv.manufacturer = getUdevVendorName(dev.data()); + priv.serialNumber = getUdevSerialNumber(dev.data()); + priv.vendorIdentifier = getUdevVendorIdentifier(dev.data(), priv.hasVendorIdentifier); + priv.productIdentifier = getUdevModelIdentifier(dev.data(), priv.hasProductIdentifier); } else { - if (serialPortInfo.d_ptr->portName.startsWith(rfcommDeviceName)) { + if (priv.portName.startsWith(rfcommDeviceName)) { bool ok; - int portNumber = serialPortInfo.d_ptr->portName.mid(rfcommDeviceName.length()).toInt(&ok); + int portNumber = priv.portName.mid(rfcommDeviceName.length()).toInt(&ok); if (!ok || (portNumber < 0) || (portNumber > 255)) continue; } else { @@ -344,7 +344,7 @@ QList<QSerialPortInfo> availablePortsByUdev() } } - serialPortInfoList.append(serialPortInfo); + serialPortInfoList.append(priv); } return serialPortInfoList; @@ -401,4 +401,18 @@ bool QSerialPortInfo::isValid() const return f.exists(); } +QString QSerialPortInfoPrivate::portNameToSystemLocation(const QString &source) +{ + return (source.startsWith(QLatin1Char('/')) + || source.startsWith(QStringLiteral("./")) + || source.startsWith(QStringLiteral("../"))) + ? source : (QStringLiteral("/dev/") + source); +} + +QString QSerialPortInfoPrivate::portNameFromSystemLocation(const QString &source) +{ + return source.startsWith(QStringLiteral("/dev/")) + ? source.mid(5) : source; +} + QT_END_NAMESPACE diff --git a/src/serialport/qserialportinfo_win.cpp b/src/serialport/qserialportinfo_win.cpp index 4b30a09..a94a5ff 100644 --- a/src/serialport/qserialportinfo_win.cpp +++ b/src/serialport/qserialportinfo_win.cpp @@ -312,23 +312,23 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() continue; } - QSerialPortInfo serialPortInfo; + QSerialPortInfoPrivate priv; - serialPortInfo.d_ptr->portName = portName; - serialPortInfo.d_ptr->device = QSerialPortPrivate::portNameToSystemLocation(portName); - serialPortInfo.d_ptr->description = deviceDescription(deviceInfoSet, &deviceInfoData); - serialPortInfo.d_ptr->manufacturer = deviceManufacturer(deviceInfoSet, &deviceInfoData); + priv.portName = portName; + priv.device = QSerialPortInfoPrivate::portNameToSystemLocation(portName); + priv.description = deviceDescription(deviceInfoSet, &deviceInfoData); + priv.manufacturer = deviceManufacturer(deviceInfoSet, &deviceInfoData); const QString instanceIdentifier = deviceInstanceIdentifier(deviceInfoData.DevInst); - serialPortInfo.d_ptr->serialNumber = + priv.serialNumber = deviceSerialNumber(instanceIdentifier, deviceInfoData.DevInst); - serialPortInfo.d_ptr->vendorIdentifier = - deviceVendorIdentifier(instanceIdentifier, serialPortInfo.d_ptr->hasVendorIdentifier); - serialPortInfo.d_ptr->productIdentifier = - deviceProductIdentifier(instanceIdentifier, serialPortInfo.d_ptr->hasProductIdentifier); + priv.vendorIdentifier = + deviceVendorIdentifier(instanceIdentifier, priv.hasVendorIdentifier); + priv.productIdentifier = + deviceProductIdentifier(instanceIdentifier, priv.hasProductIdentifier); - serialPortInfoList.append(serialPortInfo); + serialPortInfoList.append(priv); } ::SetupDiDestroyDeviceInfoList(deviceInfoSet); } @@ -336,10 +336,10 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() foreach (const QString &portName, portNamesFromHardwareDeviceMap()) { if (std::find_if(serialPortInfoList.begin(), serialPortInfoList.end(), SerialPortNameEqualFunctor(portName)) == serialPortInfoList.end()) { - QSerialPortInfo serialPortInfo; - serialPortInfo.d_ptr->portName = portName; - serialPortInfo.d_ptr->device = QSerialPortPrivate::portNameToSystemLocation(portName); - serialPortInfoList.append(serialPortInfo); + QSerialPortInfoPrivate priv; + priv.portName = portName; + priv.device = QSerialPortInfoPrivate::portNameToSystemLocation(portName); + serialPortInfoList.append(priv); } } @@ -379,4 +379,17 @@ bool QSerialPortInfo::isValid() const return true; } +QString QSerialPortInfoPrivate::portNameToSystemLocation(const QString &source) +{ + return source.startsWith(QStringLiteral("COM")) + ? (QStringLiteral("\\\\.\\") + source) : source; +} + +QString QSerialPortInfoPrivate::portNameFromSystemLocation(const QString &source) +{ + return (source.startsWith(QStringLiteral("\\\\.\\")) + || source.startsWith(QStringLiteral("//./"))) + ? source.mid(4) : source; +} + QT_END_NAMESPACE diff --git a/src/serialport/qserialportinfo_wince.cpp b/src/serialport/qserialportinfo_wince.cpp index 4cb8b92..42c3977 100644 --- a/src/serialport/qserialportinfo_wince.cpp +++ b/src/serialport/qserialportinfo_wince.cpp @@ -105,13 +105,13 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() &di); if (hSearch != INVALID_HANDLE_VALUE) { do { - QSerialPortInfo serialPortInfo; - serialPortInfo.d_ptr->device = QString::fromWCharArray(di.szLegacyName); - serialPortInfo.d_ptr->portName = QSerialPortPrivate::portNameFromSystemLocation(serialPortInfo.d_ptr->device); - serialPortInfo.d_ptr->description = findDescription(HKEY_LOCAL_MACHINE, - QString::fromWCharArray(di.szDeviceKey)); + 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(serialPortInfo); + serialPortInfoList.append(priv); } while (::FindNextDevice(hSearch, &di)); ::FindClose(hSearch); @@ -153,4 +153,16 @@ bool QSerialPortInfo::isValid() const return true; } +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/tests/auto/auto.pro b/tests/auto/auto.pro index 14963a2..2fa03f0 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -1,2 +1,5 @@ TEMPLATE = subdirs -SUBDIRS += qserialport cmake +SUBDIRS += qserialport qserialportinfo qserialportinfoprivate cmake + +!contains(QT_CONFIG, private_tests): SUBDIRS -= \ + qserialportinfoprivate diff --git a/tests/auto/qserialportinfo/qserialportinfo.pro b/tests/auto/qserialportinfo/qserialportinfo.pro new file mode 100644 index 0000000..b3e49bd --- /dev/null +++ b/tests/auto/qserialportinfo/qserialportinfo.pro @@ -0,0 +1,11 @@ +QT = core testlib +TARGET = tst_qserialportinfo +#CONFIG += testcase + +greaterThan(QT_MAJOR_VERSION, 4) { + QT += serialport +} else { + include($$QTSERIALPORT_PROJECT_ROOT/src/serialport/qt4support/serialport.prf) +} + +SOURCES = tst_qserialportinfo.cpp diff --git a/tests/auto/qserialportinfo/tst_qserialportinfo.cpp b/tests/auto/qserialportinfo/tst_qserialportinfo.cpp new file mode 100644 index 0000000..afbcf0b --- /dev/null +++ b/tests/auto/qserialportinfo/tst_qserialportinfo.cpp @@ -0,0 +1,114 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Denis Shienkov <denis.shienkov@gmail.com> +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt 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 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> +#include <QtSerialPort/QSerialPort> +#include <QtSerialPort/QSerialPortInfo> + +class tst_QSerialPortInfo : public QObject +{ + Q_OBJECT +public: + explicit tst_QSerialPortInfo(); + +private slots: + void initTestCase(); + + void constructors(); + void assignment(); + +private: + QString m_senderPortName; + QString m_receiverPortName; + QStringList m_availablePortNames; +}; + +tst_QSerialPortInfo::tst_QSerialPortInfo() +{ +} + +void tst_QSerialPortInfo::initTestCase() +{ + m_senderPortName = QString::fromLocal8Bit(qgetenv("QTEST_SERIALPORT_SENDER")); + m_receiverPortName = QString::fromLocal8Bit(qgetenv("QTEST_SERIALPORT_RECEIVER")); + if (m_senderPortName.isEmpty() || m_receiverPortName.isEmpty()) { +#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) + QSKIP("Test doesn't work because the names of serial ports aren't found in env."); +#else + QSKIP("Test doesn't work because the names of serial ports aren't set found env.", SkipAll); +#endif + } else { + m_availablePortNames << m_senderPortName << m_receiverPortName; + } +} + +void tst_QSerialPortInfo::constructors() +{ + QSerialPortInfo empty; + QVERIFY(empty.isNull()); + QSerialPortInfo empty2(QLatin1String("ABCD")); + QVERIFY(empty2.isNull()); + QSerialPortInfo empty3(empty); + QVERIFY(empty3.isNull()); + + QSerialPortInfo exist(m_senderPortName); + QVERIFY(!exist.isNull()); + QSerialPortInfo exist2(exist); + QVERIFY(!exist2.isNull()); +} + +void tst_QSerialPortInfo::assignment() +{ + QSerialPortInfo empty; + QVERIFY(empty.isNull()); + QSerialPortInfo empty2; + empty2 = empty; + QVERIFY(empty2.isNull()); + + QSerialPortInfo exist(m_senderPortName); + QVERIFY(!exist.isNull()); + QSerialPortInfo exist2; + exist2 = exist; + QVERIFY(!exist2.isNull()); +} + +QTEST_MAIN(tst_QSerialPortInfo) +#include "tst_qserialportinfo.moc" diff --git a/tests/auto/qserialportinfoprivate/qserialportinfoprivate.pro b/tests/auto/qserialportinfoprivate/qserialportinfoprivate.pro new file mode 100644 index 0000000..f479a29 --- /dev/null +++ b/tests/auto/qserialportinfoprivate/qserialportinfoprivate.pro @@ -0,0 +1,4 @@ +QT = core testlib serialport-private +TARGET = tst_qserialportinfoprivate +#CONFIG += testcase +SOURCES = tst_qserialportinfoprivate.cpp diff --git a/tests/auto/qserialportinfoprivate/tst_qserialportinfoprivate.cpp b/tests/auto/qserialportinfoprivate/tst_qserialportinfoprivate.cpp new file mode 100644 index 0000000..cfd2d85 --- /dev/null +++ b/tests/auto/qserialportinfoprivate/tst_qserialportinfoprivate.cpp @@ -0,0 +1,110 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Denis Shienkov <denis.shienkov@gmail.com> +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt 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 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> + +#include <private/qserialportinfo_p.h> + +class tst_QSerialPortInfoPrivate : public QObject +{ + Q_OBJECT +public: + explicit tst_QSerialPortInfoPrivate(); + +private slots: + void canonical_data(); + void canonical(); +}; + +tst_QSerialPortInfoPrivate::tst_QSerialPortInfoPrivate() +{ +} + +void tst_QSerialPortInfoPrivate::canonical_data() +{ + QTest::addColumn<QString>("source"); + QTest::addColumn<QString>("name"); + QTest::addColumn<QString>("location"); + +#if defined (Q_OS_WINCE) + QTest::newRow("Test1") << "COM1" << "COM1" << "COM1:"; + QTest::newRow("Test2") << "COM1:" << "COM1" << "COM1:"; +#elif defined (Q_OS_WIN32) + QTest::newRow("Test1") << "COM1" << "COM1" << "\\\\.\\COM1"; + QTest::newRow("Test2") << "\\\\.\\COM1" << "COM1" << "\\\\.\\COM1"; + QTest::newRow("Test3") << "//./COM1" << "COM1" << "//./COM1"; +#elif defined (Q_OS_OSX) + QTest::newRow("Test1") << "ttyS0" << "ttyS0" << "/dev/ttyS0"; + QTest::newRow("Test2") << "cu.serial1" << "cu.serial1" << "/dev/cu.serial1"; + QTest::newRow("Test3") << "tty.serial1" << "tty.serial1" << "/dev/tty.serial1"; + QTest::newRow("Test4") << "/dev/ttyS0" << "ttyS0" << "/dev/ttyS0"; + QTest::newRow("Test5") << "/dev/tty.serial1" << "tty.serial1" << "/dev/tty.serial1"; + QTest::newRow("Test6") << "/dev/cu.serial1" << "cu.serial1" << "/dev/cu.serial1"; + QTest::newRow("Test7") << "/dev/serial/ttyS0" << "serial/ttyS0" << "/dev/serial/ttyS0"; + QTest::newRow("Test8") << "/home/ttyS0" << "/home/ttyS0" << "/home/ttyS0"; + QTest::newRow("Test9") << "/home/serial/ttyS0" << "/home/serial/ttyS0" << "/home/serial/ttyS0"; + QTest::newRow("Test10") << "serial/ttyS0" << "serial/ttyS0" << "/dev/serial/ttyS0"; + QTest::newRow("Test11") << "./ttyS0" << "./ttyS0" << "./ttyS0"; + QTest::newRow("Test12") << "../ttyS0" << "../ttyS0" << "../ttyS0"; +#elif defined (Q_OS_UNIX) + QTest::newRow("Test1") << "ttyS0" << "ttyS0" << "/dev/ttyS0"; + QTest::newRow("Test2") << "/dev/ttyS0" << "ttyS0" << "/dev/ttyS0"; + QTest::newRow("Test3") << "/dev/serial/ttyS0" << "serial/ttyS0" << "/dev/serial/ttyS0"; + QTest::newRow("Test4") << "/home/ttyS0" << "/home/ttyS0" << "/home/ttyS0"; + QTest::newRow("Test5") << "/home/serial/ttyS0" << "/home/serial/ttyS0" << "/home/serial/ttyS0"; + QTest::newRow("Test6") << "serial/ttyS0" << "serial/ttyS0" << "/dev/serial/ttyS0"; + QTest::newRow("Test7") << "./ttyS0" << "./ttyS0" << "./ttyS0"; + QTest::newRow("Test8") << "../ttyS0" << "../ttyS0" << "../ttyS0"; +#endif +} + +void tst_QSerialPortInfoPrivate::canonical() +{ + QFETCH(QString, source); + QFETCH(QString, name); + QFETCH(QString, location); + + QCOMPARE(QSerialPortInfoPrivate::portNameFromSystemLocation(source), name); + QCOMPARE(QSerialPortInfoPrivate::portNameToSystemLocation(source), location); +} + +QTEST_MAIN(tst_QSerialPortInfoPrivate) +#include "tst_qserialportinfoprivate.moc" |