diff options
author | Denis Shienkov <denis.shienkov@gmail.com> | 2014-06-01 14:48:59 +0000 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-06-07 14:16:57 +0200 |
commit | 6aa21015ea0a956d8a563dd3b52f4701368b4f2d (patch) | |
tree | f81763c861b459285a218964badd9ff3f388251b /src/serialport/qserialportinfo_unix.cpp | |
parent | d598a00e823d8509fc67fc2452dcaf2939092804 (diff) |
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>
Diffstat (limited to 'src/serialport/qserialportinfo_unix.cpp')
-rw-r--r-- | src/serialport/qserialportinfo_unix.cpp | 80 |
1 files changed, 47 insertions, 33 deletions
diff --git a/src/serialport/qserialportinfo_unix.cpp b/src/serialport/qserialportinfo_unix.cpp index 4fc66508..61d21d63 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; |