summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2014-06-01 14:48:59 +0000
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-06-07 14:16:57 +0200
commit6aa21015ea0a956d8a563dd3b52f4701368b4f2d (patch)
treef81763c861b459285a218964badd9ff3f388251b
parentd598a00e823d8509fc67fc2452dcaf2939092804 (diff)
downloadqtserialport-6aa21015ea0a956d8a563dd3b52f4701368b4f2d.tar.gz
Simplify of the availablePortsByUdev()
There is no sense to use udev_device_get_subsystem() for comparing with the predetermined names of subsystems (like "pnp", "usb" and so on). This idea was preliminary and required further revising after accumulation of some statistic. The main ideas in favor of refactoring are given below: * It seems that reasonable to use the "ID_MODEL", "ID_VENDOR" and others identifiers to query of properties for all types of serial ports. We lose nothing, because in the worst case the getUdevPropertyValue() function will simply return an empty string, as before. * Earlier we excluded from the list all ports which have a subsystem with the "platform" name. Practice showed that for these devices is used the serial8250 driver. Therefore it is reasonable to exclude this type of devices by the name of drivers instead of by the name of subsystems. Also are made related changes: * Is added the resolving of chars for the new udev_device_get_driver() function * The functions of getting properties are wrapped for reduction of length up to 100 characters. Tested on ArchLinux 64-bit with the on-board, PL2303 serial devices using Qt4 and then Qt5. Tested build on Android x86 using Qt5. Change-Id: I1a66164ca1893180e1ed97524cff98b4a933a63b Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com> Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
-rw-r--r--src/serialport/qserialportinfo_unix.cpp80
-rw-r--r--src/serialport/qtudev_p.h2
2 files changed, 49 insertions, 33 deletions
diff --git a/src/serialport/qserialportinfo_unix.cpp b/src/serialport/qserialportinfo_unix.cpp
index 4fc6650..61d21d6 100644
--- a/src/serialport/qserialportinfo_unix.cpp
+++ b/src/serialport/qserialportinfo_unix.cpp
@@ -234,6 +234,45 @@ QString getUdevPropertyValue(struct ::udev_device *dev, const char *name)
return QString::fromLatin1(::udev_device_get_property_value(dev, name));
}
+static
+bool checkUdevForSerial8250Driver(struct ::udev_device *dev)
+{
+ const QString driverName = QString::fromLatin1(::udev_device_get_driver(dev));
+ return (driverName == QStringLiteral("serial8250"));
+}
+
+static
+QString getUdevModelName(struct ::udev_device *dev)
+{
+ return getUdevPropertyValue(dev, "ID_MODEL")
+ .replace(QLatin1Char('_'), QLatin1Char(' '));
+}
+
+static
+QString getUdevVendorName(struct ::udev_device *dev)
+{
+ return getUdevPropertyValue(dev, "ID_VENDOR")
+ .replace(QLatin1Char('_'), QLatin1Char(' '));
+}
+
+static
+quint16 getUdevModelIdentifier(struct ::udev_device *dev, bool &hasIdentifier)
+{
+ return getUdevPropertyValue(dev, "ID_MODEL_ID").toInt(&hasIdentifier, 16);
+}
+
+static
+quint16 getUdevVendorIdentifier(struct ::udev_device *dev, bool &hasIdentifier)
+{
+ return getUdevPropertyValue(dev, "ID_VENDOR_ID").toInt(&hasIdentifier, 16);
+}
+
+static
+QString getUdevSerialNumber(struct ::udev_device *dev)
+{
+ return getUdevPropertyValue(dev,"ID_SERIAL_SHORT");
+}
+
QList<QSerialPortInfo> availablePortsByUdev()
{
#ifndef LINK_LIBUDEV
@@ -276,43 +315,18 @@ QList<QSerialPortInfo> availablePortsByUdev()
if (parentdev) {
- QString subsys = QString::fromLatin1(::udev_device_get_subsystem(parentdev));
-
- if (subsys == QStringLiteral("usb-serial")
- || subsys == QStringLiteral("usb")) {
- serialPortInfo.d_ptr->description =
- getUdevPropertyValue(dev.data(), "ID_MODEL").replace(QLatin1Char('_'), QLatin1Char(' '));
-
- serialPortInfo.d_ptr->manufacturer =
- getUdevPropertyValue(dev.data(), "ID_VENDOR").replace(QLatin1Char('_'), QLatin1Char(' '));
-
- serialPortInfo.d_ptr->serialNumber = getUdevPropertyValue(dev.data(),"ID_SERIAL_SHORT");
-
- serialPortInfo.d_ptr->vendorIdentifier =
- getUdevPropertyValue(dev.data(), "ID_VENDOR_ID").toInt(&serialPortInfo.d_ptr->hasVendorIdentifier, 16);
-
- serialPortInfo.d_ptr->productIdentifier =
- getUdevPropertyValue(dev.data(), "ID_MODEL_ID").toInt(&serialPortInfo.d_ptr->hasProductIdentifier, 16);
-
- } else if (subsys == QStringLiteral("pnp")) {
- // TODO: Obtain more information
- } else if (subsys == QStringLiteral("platform")) {
+ if (checkUdevForSerial8250Driver(parentdev))
continue;
- } else if (subsys == QStringLiteral("pci")) {
- serialPortInfo.d_ptr->description =
- getUdevPropertyValue(dev.data(), "ID_MODEL");
- serialPortInfo.d_ptr->manufacturer =
- getUdevPropertyValue(dev.data(), "ID_VENDOR");
+ 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 =
- getUdevPropertyValue(dev.data(), "ID_VENDOR_ID").toInt(&serialPortInfo.d_ptr->hasVendorIdentifier, 16);
+ serialPortInfo.d_ptr->vendorIdentifier = getUdevModelIdentifier(
+ dev.data(), serialPortInfo.d_ptr->hasVendorIdentifier);
- serialPortInfo.d_ptr->productIdentifier =
- getUdevPropertyValue(dev.data(), "ID_MODEL_ID").toInt(&serialPortInfo.d_ptr->hasProductIdentifier, 16);
- } else {
- // FIXME: Obtain more information
- }
+ serialPortInfo.d_ptr->productIdentifier = getUdevVendorIdentifier(
+ dev.data(), serialPortInfo.d_ptr->hasProductIdentifier);
} else {
if (serialPortInfo.d_ptr->portName.startsWith(rfcommDeviceName)) {
bool ok;
diff --git a/src/serialport/qtudev_p.h b/src/serialport/qtudev_p.h
index 434292d..0f0246d 100644
--- a/src/serialport/qtudev_p.h
+++ b/src/serialport/qtudev_p.h
@@ -82,6 +82,7 @@ GENERATE_SYMBOL_VARIABLE(struct udev_device *, udev_device_new_from_syspath, str
GENERATE_SYMBOL_VARIABLE(const char *, udev_list_entry_get_name, struct udev_list_entry *)
GENERATE_SYMBOL_VARIABLE(const char *, udev_device_get_devnode, struct udev_device *)
GENERATE_SYMBOL_VARIABLE(const char *, udev_device_get_sysname, struct udev_device *)
+GENERATE_SYMBOL_VARIABLE(const char *, udev_device_get_driver, struct udev_device *)
GENERATE_SYMBOL_VARIABLE(struct udev_device *, udev_device_get_parent, struct udev_device *)
GENERATE_SYMBOL_VARIABLE(const char *, udev_device_get_subsystem, struct udev_device *)
GENERATE_SYMBOL_VARIABLE(const char *, udev_device_get_property_value, struct udev_device *, const char *)
@@ -127,6 +128,7 @@ inline bool resolveSymbols(QLibrary *udevLibrary)
RESOLVE_SYMBOL(udev_list_entry_get_name)
RESOLVE_SYMBOL(udev_device_get_devnode)
RESOLVE_SYMBOL(udev_device_get_sysname)
+ RESOLVE_SYMBOL(udev_device_get_driver)
RESOLVE_SYMBOL(udev_device_get_parent)
RESOLVE_SYMBOL(udev_device_get_subsystem)
RESOLVE_SYMBOL(udev_device_get_property_value)