diff options
author | Laszlo Papp <lpapp@kde.org> | 2013-03-23 18:45:26 +0000 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-03-25 10:27:50 +0100 |
commit | 23f8a58652e3ac4398d508691d227b3c75811e8f (patch) | |
tree | 0347d6caa4d0cc2333c87c8aface2e3d13dd3b73 | |
parent | 43d137e4b2023e6b5d9df9b89281eca4b70eecf8 (diff) | |
download | qtserialport-23f8a58652e3ac4398d508691d227b3c75811e8f.tar.gz |
Change the return type of the vendor/productIdentifier() API
The API was established with a bit of insanity, namely: it returns raw strings
for the 16-bit vendor and product _numbers_. This brings lots of limitations
into the system aside from its silly nature, like how to handle lower and upper
case hex letters (a-f, A-F) and so forth.
Also, it is not clear whether the API should return zero aligned strings, or
whether or not to return the leading indicator of the hex format (0x). This is
now all being eliminated by switching to the 16-bit unsigned integer use.
There are mostly two use cases for the usage of this:
a) Comparison or mapping to find the requested port and device
This is getting very simple because it will mean a simple 16-bit
unsigned integer comparison which is cross-platform by its nature for
sure.
b) Display the string to the end user
This is for instance with an enumerator or terminal emulator
application, but in this special case the user can format the output of
the integer as wished. Then, it can easily become a cross-platform
display. Even customizing is simple enough to provide the native look'n
feel without the library forcing one particular type.
The documentation is being changed correspondingly. As for the future, if the
need even arises, the following methods could be considered for the info
provider class (QSerialPortInfo):
bool hasVendorIdentifier() const;
bool hasProductIdentifier() const;
... both can be added without any issues (i.e. in a binary compatible way).
The examples are updated now as well with presenting the usage of the new API.
The GUI emulator example got the string management a bit updated to be in line
with the console based emulator (cenumerator). In general, it is also a way more
reasonable way of putting a string together with so many placeholders. Also, the
argument and list management got a bit nicer formatting style per line break.
Testing: the change has been tested on Linux with Qt4 and Qt5.
Task-number: QTPLAYGROUND-21
Change-Id: I33683061787af94a797685794be9ebcfa90c499a
Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
-rw-r--r-- | examples/cenumerator/main.cpp | 4 | ||||
-rw-r--r-- | examples/enumerator/main.cpp | 19 | ||||
-rw-r--r-- | examples/terminal/settingsdialog.cpp | 9 | ||||
-rw-r--r-- | src/serialport/qserialportinfo.cpp | 18 | ||||
-rw-r--r-- | src/serialport/qserialportinfo.h | 4 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_mac.cpp | 6 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_p.h | 6 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_unix.cpp | 9 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_win.cpp | 4 |
9 files changed, 38 insertions, 41 deletions
diff --git a/examples/cenumerator/main.cpp b/examples/cenumerator/main.cpp index 11af618..3ae7d65 100644 --- a/examples/cenumerator/main.cpp +++ b/examples/cenumerator/main.cpp @@ -59,8 +59,8 @@ int main(int argc, char *argv[]) << QObject::tr("Location: ") << serialPortInfo.systemLocation() << endl << QObject::tr("Description: ") << serialPortInfo.description() << endl << QObject::tr("Manufacturer: ") << serialPortInfo.manufacturer() << endl - << QObject::tr("Vendor Identifier: ") << serialPortInfo.vendorIdentifier() << endl - << QObject::tr("Product Identifier: ") << serialPortInfo.productIdentifier() << endl + << QObject::tr("Vendor Identifier: ") << QByteArray::number(serialPortInfo.vendorIdentifier(), 16) << endl + << QObject::tr("Product Identifier: ") << QByteArray::number(serialPortInfo.productIdentifier(), 16) << endl << QObject::tr("Busy: ") << (serialPortInfo.isBusy() ? QObject::tr("Yes") : QObject::tr("No")) << endl; } diff --git a/examples/enumerator/main.cpp b/examples/enumerator/main.cpp index 0bd3fc0..a95ee76 100644 --- a/examples/enumerator/main.cpp +++ b/examples/enumerator/main.cpp @@ -57,18 +57,13 @@ int main(int argc, char *argv[]) QVBoxLayout *layout = new QVBoxLayout; foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { - QString s(QObject::tr("Port: %1\n" - "Location: %2\n" - "Description: %3\n" - "Manufacturer: %4\n" - "Vendor Identifier: %5\n" - "Product Identifier: %6\n" - "Busy: %7\n")); - - s = s.arg(info.portName()).arg(info.systemLocation()) - .arg(info.description()).arg(info.manufacturer()) - .arg(info.vendorIdentifier()).arg(info.productIdentifier()) - .arg(info.isBusy() ? QObject::tr("Yes") : QObject::tr("No")); + QString s = QObject::tr("Port: ") + info.portName() + "\n" + + QObject::tr("Location: ") + info.systemLocation() + "\n" + + QObject::tr("Description: ") + info.description() + "\n" + + QObject::tr("Manufacturer: ") + info.manufacturer() + "\n" + + QObject::tr("Vendor Identifier: ") + QString::number(info.vendorIdentifier(), 16) + "\n" + + QObject::tr("Product Identifier: ") + QString::number(info.productIdentifier(), 16) + "\n" + + QObject::tr("Busy: ") + (info.isBusy() ? QObject::tr("Yes") : QObject::tr("No")) + "\n"; QLabel *label = new QLabel(s); layout->addWidget(label); diff --git a/examples/terminal/settingsdialog.cpp b/examples/terminal/settingsdialog.cpp index f077933..c516a75 100644 --- a/examples/terminal/settingsdialog.cpp +++ b/examples/terminal/settingsdialog.cpp @@ -153,9 +153,12 @@ void SettingsDialog::fillPortsInfo() ui->serialPortInfoListBox->clear(); foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { QStringList list; - list << info.portName() << info.description() - << info.manufacturer() << info.systemLocation() - << info.vendorIdentifier() << info.productIdentifier(); + list << info.portName() + << info.description() + << info.manufacturer() + << info.systemLocation() + << QString::number(info.vendorIdentifier(), 16) + << QString::number(info.productIdentifier(), 16); ui->serialPortInfoListBox->addItem(list.first(), list); } diff --git a/src/serialport/qserialportinfo.cpp b/src/serialport/qserialportinfo.cpp index 6d3ea33..e16e13e 100644 --- a/src/serialport/qserialportinfo.cpp +++ b/src/serialport/qserialportinfo.cpp @@ -184,25 +184,23 @@ QString QSerialPortInfo::manufacturer() const } /*! - Returns the vendor identifier string of the serial - port in hexadecimal format, if available; otherwise - returns an empty string. + Returns the 16-bit vendor number for the serial port, if available; + otherwise returns zero. */ -QString QSerialPortInfo::vendorIdentifier() const +quint16 QSerialPortInfo::vendorIdentifier() const { Q_D(const QSerialPortInfo); - return !d ? QString() : d->vendorIdentifier; + return !d ? 0 : d->vendorIdentifier; } /*! - Returns the product identifier string of the serial - port in hexadecimal format, if available; otherwise - returns an empty string. + Returns the 16-bit product number for the serial port, if available; + otherwise returns zero. */ -QString QSerialPortInfo::productIdentifier() const +quint16 QSerialPortInfo::productIdentifier() const { Q_D(const QSerialPortInfo); - return !d ? QString() : d->productIdentifier; + return !d ? 0 : d->productIdentifier; } /*! diff --git a/src/serialport/qserialportinfo.h b/src/serialport/qserialportinfo.h index 96d48d7..19fd5b7 100644 --- a/src/serialport/qserialportinfo.h +++ b/src/serialport/qserialportinfo.h @@ -71,8 +71,8 @@ public: QString systemLocation() const; QString description() const; QString manufacturer() const; - QString vendorIdentifier() const; - QString productIdentifier() const; + quint16 vendorIdentifier() const; + quint16 productIdentifier() const; bool isNull() const; bool isBusy() const; diff --git a/src/serialport/qserialportinfo_mac.cpp b/src/serialport/qserialportinfo_mac.cpp index 0378f19..59cfad9 100644 --- a/src/serialport/qserialportinfo_mac.cpp +++ b/src/serialport/qserialportinfo_mac.cpp @@ -232,13 +232,13 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() ::CFRelease(manufacturer); } - int value = 0; + quint16 value = 0; if (vendorIdentifier) { if (::CFNumberGetValue(CFNumberRef(vendorIdentifier), kCFNumberIntType, &value)) { - serialPortInfo.d_ptr->vendorIdentifier = QString::number(value, 16); + serialPortInfo.d_ptr->vendorIdentifier = value; } ::CFRelease(vendorIdentifier); } @@ -247,7 +247,7 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() if (::CFNumberGetValue(CFNumberRef(productIdentifier), kCFNumberIntType, &value)) { - serialPortInfo.d_ptr->productIdentifier = QString::number(value, 16); + serialPortInfo.d_ptr->productIdentifier = value; } ::CFRelease(productIdentifier); } diff --git a/src/serialport/qserialportinfo_p.h b/src/serialport/qserialportinfo_p.h index 259752e..ee7b884 100644 --- a/src/serialport/qserialportinfo_p.h +++ b/src/serialport/qserialportinfo_p.h @@ -51,15 +51,15 @@ QT_BEGIN_NAMESPACE class QSerialPortInfoPrivate { public: - QSerialPortInfoPrivate() {} + QSerialPortInfoPrivate() : vendorIdentifier(0), productIdentifier(0) {} ~QSerialPortInfoPrivate() {} QString portName; QString device; QString description; QString manufacturer; - QString vendorIdentifier; - QString productIdentifier; + quint16 vendorIdentifier; + quint16 productIdentifier; }; class QSerialPortInfoPrivateDeleter diff --git a/src/serialport/qserialportinfo_unix.cpp b/src/serialport/qserialportinfo_unix.cpp index c474e75..bfdc3d0 100644 --- a/src/serialport/qserialportinfo_unix.cpp +++ b/src/serialport/qserialportinfo_unix.cpp @@ -145,12 +145,13 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() serialPortInfo.d_ptr->manufacturer = QString( QLatin1String(::udev_device_get_property_value(dev, "ID_VENDOR"))).replace('_', ' '); + serialPortInfo.d_ptr->vendorIdentifier = - QLatin1String(::udev_device_get_property_value(dev, - "ID_VENDOR_ID")); + QString::fromLatin1(::udev_device_get_property_value(dev, "ID_VENDOR_ID")).toInt(0, 16); + serialPortInfo.d_ptr->productIdentifier = - QLatin1String(::udev_device_get_property_value(dev, - "ID_MODEL_ID")); + QString::fromLatin1(::udev_device_get_property_value(dev, "ID_MODEL_ID")).toInt(0, 16); + } else if (subsys == QLatin1String("pnp")) { // PNP bus type // Append this device. // FIXME: How to get additional information about serial devices diff --git a/src/serialport/qserialportinfo_win.cpp b/src/serialport/qserialportinfo_win.cpp index 37880a7..0a6618e 100644 --- a/src/serialport/qserialportinfo_win.cpp +++ b/src/serialport/qserialportinfo_win.cpp @@ -178,11 +178,11 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() int index = s.indexOf(vendorIdentifierPrefix); if (index != -1) - serialPortInfo.d_ptr->vendorIdentifier = s.mid(index + vendorIdentifierPrefix.size(), vendorIdentifierSize); + serialPortInfo.d_ptr->vendorIdentifier = s.mid(index + vendorIdentifierPrefix.size(), vendorIdentifierSize).toInt(0, 16); index = s.indexOf(productIdentifierPrefix); if (index != -1) - serialPortInfo.d_ptr->productIdentifier = s.mid(index + productIdentifierPrefix.size(), productIdentifierSize); + serialPortInfo.d_ptr->productIdentifier = s.mid(index + productIdentifierPrefix.size(), productIdentifierSize).toInt(0, 16); serialPortInfoList.append(serialPortInfo); } |