diff options
author | Laszlo Papp <lpapp@kde.org> | 2013-11-23 10:32:01 +0000 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-11-25 09:55:06 +0100 |
commit | d97a152bf14cd7b58e576bd6b4a5f304527f940e (patch) | |
tree | df8317cc7ad8230dd109ba3b0951bfa0361c2ef4 | |
parent | 1a6ec9148a782c06ee84961c16a57411dc8bcbee (diff) | |
download | qtserialport-d97a152bf14cd7b58e576bd6b4a5f304527f940e.tar.gz |
Try to load udev at runtime if we do not link against it
There was an explicit option previously added into 5.2.0 due to lack of time to
implement this runtime resolution properly. The libudev load variable can now be
eliminated which also simplifies the packaging.
This change also takes care of avoiding the regression by not loading udev at
runtime at all for Qt 4. That has always been the case, so there are no
regressions there. A TODO comment has been left in the code though about it.
The helper free functions cannot be static because they are declared as friend
outside that source file. Ideally, we could have a better approach, but that
would require more significant refactoring, and such a revamp has to become
stable in the 'dev' branch before going into the 'stable' branch. Thereby, this
is an acceptable compromise.
It does not break binary compatibility either to remove or add friend
declarations to a class.
The change has been tested on Archlinux (x86_64) with Qt 4 and then 5 using the
cenumerator example. The change has been tested both with udev and without that,
i.e with sysfs on my Linux system.
Change-Id: Ia7d7ecb336c1de1913e1dd60177ae7469e90c329
Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
-rw-r--r-- | src/serialport/qserialportinfo.h | 3 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_unix.cpp | 62 | ||||
-rw-r--r-- | src/serialport/qtudev_p.h | 2 | ||||
-rw-r--r-- | src/serialport/serialport-lib.pri | 2 |
4 files changed, 41 insertions, 28 deletions
diff --git a/src/serialport/qserialportinfo.h b/src/serialport/qserialportinfo.h index 51f3b10..daa255f 100644 --- a/src/serialport/qserialportinfo.h +++ b/src/serialport/qserialportinfo.h @@ -86,6 +86,9 @@ public: static QList<QSerialPortInfo> availablePorts(); private: + friend QList<QSerialPortInfo> availablePortsByUdev(); + friend QList<QSerialPortInfo> availablePortsBySysfs(); + friend QList<QSerialPortInfo> availablePortsByFiltersOfDevices(); QScopedPointer<QSerialPortInfoPrivate, QSerialPortInfoPrivateDeleter> d_ptr; }; diff --git a/src/serialport/qserialportinfo_unix.cpp b/src/serialport/qserialportinfo_unix.cpp index 5fb110c..fee8b84 100644 --- a/src/serialport/qserialportinfo_unix.cpp +++ b/src/serialport/qserialportinfo_unix.cpp @@ -47,15 +47,11 @@ #include <QtCore/qlockfile.h> #include <QtCore/qfile.h> +#include <QtCore/qdir.h> #ifndef Q_OS_MAC -#if defined(LINK_LIBUDEV) || defined(LOAD_LIBUDEV) #include "qtudev_p.h" -#else -#include <QtCore/qdir.h> -#include <QtCore/qstringlist.h> -#endif #endif @@ -63,8 +59,6 @@ QT_BEGIN_NAMESPACE #ifndef Q_OS_MAC -#if !defined(LINK_LIBUDEV) && !defined(LOAD_LIBUDEV) - static QStringList filteredDeviceFilePaths() { static const QStringList deviceFileNameFilterList = QStringList() @@ -105,16 +99,25 @@ static QStringList filteredDeviceFilePaths() return result; } -QList<QSerialPortInfo> QSerialPortInfo::availablePorts() +QList<QSerialPortInfo> availablePortsByFiltersOfDevices() { QList<QSerialPortInfo> serialPortInfoList; - bool sysfsEnabled = false; + foreach (const QString &deviceFilePath, filteredDeviceFilePaths()) { + QSerialPortInfo serialPortInfo; + serialPortInfo.d_ptr->device = deviceFilePath; + serialPortInfo.d_ptr->portName = QSerialPortPrivate::portNameFromSystemLocation(deviceFilePath); + serialPortInfoList.append(serialPortInfo); + } -#ifdef Q_OS_LINUX + return serialPortInfoList; +} +QList<QSerialPortInfo> availablePortsBySysfs() +{ + QList<QSerialPortInfo> serialPortInfoList; QDir ttySysClassDir(QStringLiteral("/sys/class/tty")); - sysfsEnabled = ttySysClassDir.exists() && ttySysClassDir.isReadable(); + const bool sysfsEnabled = ttySysClassDir.exists() && ttySysClassDir.isReadable(); if (sysfsEnabled) { ttySysClassDir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot); @@ -187,25 +190,12 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() } } -#endif - - if (!sysfsEnabled) { - foreach (const QString &deviceFilePath, filteredDeviceFilePaths()) { - QSerialPortInfo serialPortInfo; - serialPortInfo.d_ptr->device = deviceFilePath; - serialPortInfo.d_ptr->portName = QSerialPortPrivate::portNameFromSystemLocation(deviceFilePath); - serialPortInfoList.append(serialPortInfo); - } - } - return serialPortInfoList; } -#else - -QList<QSerialPortInfo> QSerialPortInfo::availablePorts() +QList<QSerialPortInfo> availablePortsByUdev() { -#ifdef LOAD_LIBUDEV +#ifndef LINK_LIBUDEV static bool symbolsResolved = resolveSymbols(); if (!symbolsResolved) return QList<QSerialPortInfo>(); @@ -298,8 +288,28 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() return serialPortInfoList; } +QList<QSerialPortInfo> QSerialPortInfo::availablePorts() +{ + QList<QSerialPortInfo> serialPortInfoList; + // TODO: Remove this condition once the udev runtime symbol resolution crash + // is fixed for Qt 4. +#if defined(LINK_LIBUDEV) || (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) + serialPortInfoList = availablePortsByUdev(); +#endif + +#ifdef Q_OS_LINUX + if (serialPortInfoList.isEmpty()) + serialPortInfoList = availablePortsBySysfs(); + else + return serialPortInfoList; #endif + if (serialPortInfoList.isEmpty()) + serialPortInfoList = availablePortsByFiltersOfDevices(); + + return serialPortInfoList; +} + #endif QList<qint32> QSerialPortInfo::standardBaudRates() diff --git a/src/serialport/qtudev_p.h b/src/serialport/qtudev_p.h index 457c3f0..1187165 100644 --- a/src/serialport/qtudev_p.h +++ b/src/serialport/qtudev_p.h @@ -47,7 +47,7 @@ extern "C" { #include <libudev.h> } -#elif defined(LOAD_LIBUDEV) +#else #include <QtCore/qlibrary.h> #include <QtCore/qstring.h> #include <QtCore/qdebug.h> diff --git a/src/serialport/serialport-lib.pri b/src/serialport/serialport-lib.pri index 7ad55f8..4c7e732 100644 --- a/src/serialport/serialport-lib.pri +++ b/src/serialport/serialport-lib.pri @@ -1,6 +1,6 @@ INCLUDEPATH += $$PWD -!contains(DEFINES, LOAD_LIBUDEV): unix { +unix { greaterThan(QT_MAJOR_VERSION, 4) { contains(QT_CONFIG, libudev) { DEFINES += LINK_LIBUDEV |