summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2014-11-28 21:25:36 +0000
committerDenis Shienkov <denis.shienkov@gmail.com>2014-12-03 11:57:59 +0100
commit015c8a9053ffdf550f460e9ef10c4bb26284feaa (patch)
tree3a1a1efcd8703a4cb9f31c65287d7ae819bed53f
parent2e627bdd20b77b323b22d214283560db0056b79e (diff)
downloadqtserialport-015c8a9053ffdf550f460e9ef10c4bb26284feaa.tar.gz
Improve QSPI algorithm on Unix
QSPI uses the wrong algorithm of devices search. In case no any devices are detected with udev (or with sysfs), starts a search with a filters. In this case a user gets a list of devices that are not a serial ports, that is wrong. The main idea should be in that udev or sysfs are the main sources of obtaining information. In case they returns an empty list this means that system has no serial ports. Algorithm is: Try to find devices through udev. If it fails (system error or udev is not present), then try to find using sysfs; otherwise return udev result, even if it has an empty list. If sysfs is fails (permission denied or sysfs is not present), then try to find devices in /dev/ as last attempt; otherwise return sysfs result, even if it has an empty list. Tested on Linux (with udev, sysfs, filters) using FTDI serial ports. Change-Id: I0132e27f720b007ea3f4861e9cd7ed77833cff8c Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
-rw-r--r--src/serialport/qserialportinfo.h6
-rw-r--r--src/serialport/qserialportinfo_unix.cpp30
2 files changed, 22 insertions, 14 deletions
diff --git a/src/serialport/qserialportinfo.h b/src/serialport/qserialportinfo.h
index 0c7a0f1..714708d 100644
--- a/src/serialport/qserialportinfo.h
+++ b/src/serialport/qserialportinfo.h
@@ -82,9 +82,9 @@ public:
private:
QSerialPortInfo(const QSerialPortInfoPrivate &dd);
- friend QList<QSerialPortInfo> availablePortsByUdev();
- friend QList<QSerialPortInfo> availablePortsBySysfs();
- friend QList<QSerialPortInfo> availablePortsByFiltersOfDevices();
+ friend QList<QSerialPortInfo> availablePortsByUdev(bool &ok);
+ friend QList<QSerialPortInfo> availablePortsBySysfs(bool &ok);
+ friend QList<QSerialPortInfo> availablePortsByFiltersOfDevices(bool &ok);
QScopedPointer<QSerialPortInfoPrivate, QSerialPortInfoPrivateDeleter> d_ptr;
};
diff --git a/src/serialport/qserialportinfo_unix.cpp b/src/serialport/qserialportinfo_unix.cpp
index 9637581..8bf5692 100644
--- a/src/serialport/qserialportinfo_unix.cpp
+++ b/src/serialport/qserialportinfo_unix.cpp
@@ -93,7 +93,7 @@ static QStringList filteredDeviceFilePaths()
return result;
}
-QList<QSerialPortInfo> availablePortsByFiltersOfDevices()
+QList<QSerialPortInfo> availablePortsByFiltersOfDevices(bool &ok)
{
QList<QSerialPortInfo> serialPortInfoList;
@@ -104,15 +104,18 @@ QList<QSerialPortInfo> availablePortsByFiltersOfDevices()
serialPortInfoList.append(priv);
}
+ ok = true;
return serialPortInfoList;
}
-QList<QSerialPortInfo> availablePortsBySysfs()
+QList<QSerialPortInfo> availablePortsBySysfs(bool &ok)
{
QDir ttySysClassDir(QStringLiteral("/sys/class/tty"));
- if (!(ttySysClassDir.exists() && ttySysClassDir.isReadable()))
+ if (!(ttySysClassDir.exists() && ttySysClassDir.isReadable())) {
+ ok = false;
return QList<QSerialPortInfo>();
+ }
QList<QSerialPortInfo> serialPortInfoList;
ttySysClassDir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
@@ -206,6 +209,7 @@ QList<QSerialPortInfo> availablePortsBySysfs()
serialPortInfoList.append(priv);
}
+ ok = true;
return serialPortInfoList;
}
@@ -282,8 +286,10 @@ QString getUdevSerialNumber(struct ::udev_device *dev)
return getUdevPropertyValue(dev,"ID_SERIAL_SHORT");
}
-QList<QSerialPortInfo> availablePortsByUdev()
+QList<QSerialPortInfo> availablePortsByUdev(bool &ok)
{
+ ok = false;
+
#ifndef LINK_LIBUDEV
static bool symbolsResolved = resolveSymbols(udevLibrary());
if (!symbolsResolved)
@@ -311,6 +317,8 @@ QList<QSerialPortInfo> availablePortsByUdev()
udev_list_entry *dev_list_entry;
udev_list_entry_foreach(dev_list_entry, devices) {
+ ok = true;
+
QScopedPointer<udev_device, ScopedPointerUdevDeviceDeleter>
dev(::udev_device_new_from_syspath(
udev.data(), ::udev_list_entry_get_name(dev_list_entry)));
@@ -352,17 +360,17 @@ QList<QSerialPortInfo> availablePortsByUdev()
QList<QSerialPortInfo> QSerialPortInfo::availablePorts()
{
- QList<QSerialPortInfo> serialPortInfoList = availablePortsByUdev();
+ bool ok;
+
+ QList<QSerialPortInfo> serialPortInfoList = availablePortsByUdev(ok);
#ifdef Q_OS_LINUX
- if (serialPortInfoList.isEmpty())
- serialPortInfoList = availablePortsBySysfs();
- else
- return serialPortInfoList;
+ if (!ok)
+ serialPortInfoList = availablePortsBySysfs(ok);
#endif
- if (serialPortInfoList.isEmpty())
- serialPortInfoList = availablePortsByFiltersOfDevices();
+ if (!ok)
+ serialPortInfoList = availablePortsByFiltersOfDevices(ok);
return serialPortInfoList;
}