summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaszlo Papp <lpapp@kde.org>2013-03-24 08:43:25 +0000
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-03-26 08:31:13 +0100
commit865e169fa6c4a5eeb182f87ac6dd3b72d5012ebf (patch)
treef54d8a837901f17c041e017670b0e3c2c2803c1e
parenteacecbe76e967e59a4cd0f873471693df7bb9e63 (diff)
downloadqtserialport-865e169fa6c4a5eeb182f87ac6dd3b72d5012ebf.tar.gz
Add accessor methods for checking the availability of the PID/VID
As mentioned in the previous change, this is necessary for those end users who would like to explicitly know if the device on the serial port has a valid vendor or/and product identifier. This is now providing a nice and convenient API for having a dedicated boolean method for making this query possible. It is a lot more readable and convenient to use than other alternatives including the extended scope for the integer query method and so forth. It is more explicit and clear about the intention that one checks if the device has a valid identifier. It would be more vague with error codes and so forth. One somewhat reasonable alternative could be this, albeit this is also more inconvenient: a) bool vendorIdentifier(quint16 &vendorIdentifier) const; b) bool productIdentifier(quint16 &vendorIdentifier) const; ... as this would require an additional variable from the user, and this could not be used with ternary operators so easily, et cetera. There are even worse ideas like how the exceptional conversion happens in QString and so forth, but that also requires an explicit variable or zero if one is sure the conversion should succeed. One can pass zero in there, but that is still inconvenient and more than passing nothing, and zero can become unclear what it supposed to present half or one year later. The change has been tested on Linux with Qt4 and Qt5, and cenumerator works as expected. The documentation has been added accodingly. Task-number: QTPLAYGROUND-21 Change-Id: I905708ce0e67307bf89d69e645462486ad869b0c Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com> Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
-rw-r--r--examples/cenumerator/main.cpp4
-rw-r--r--examples/enumerator/main.cpp4
-rw-r--r--examples/terminal/settingsdialog.cpp4
-rw-r--r--src/serialport/qserialportinfo.cpp20
-rw-r--r--src/serialport/qserialportinfo.h4
-rw-r--r--src/serialport/qserialportinfo_mac.cpp14
-rw-r--r--src/serialport/qserialportinfo_p.h12
-rw-r--r--src/serialport/qserialportinfo_unix.cpp6
-rw-r--r--src/serialport/qserialportinfo_win.cpp6
9 files changed, 55 insertions, 19 deletions
diff --git a/examples/cenumerator/main.cpp b/examples/cenumerator/main.cpp
index 3ae7d65..f55b92d 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: ") << QByteArray::number(serialPortInfo.vendorIdentifier(), 16) << endl
- << QObject::tr("Product Identifier: ") << QByteArray::number(serialPortInfo.productIdentifier(), 16) << endl
+ << QObject::tr("Vendor Identifier: ") << (serialPortInfo.hasVendorIdentifier() ? QByteArray::number(serialPortInfo.vendorIdentifier(), 16) : QByteArray()) << endl
+ << QObject::tr("Product Identifier: ") << (serialPortInfo.hasProductIdentifier() ? QByteArray::number(serialPortInfo.productIdentifier(), 16) : QByteArray()) << 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 a95ee76..9e34d38 100644
--- a/examples/enumerator/main.cpp
+++ b/examples/enumerator/main.cpp
@@ -61,8 +61,8 @@ int main(int argc, char *argv[])
+ 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("Vendor Identifier: ") + (info.hasVendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString()) + "\n"
+ + QObject::tr("Product Identifier: ") + (info.hasProductIdentifier() ?QString::number(info.productIdentifier(), 16) : QString()) + "\n"
+ QObject::tr("Busy: ") + (info.isBusy() ? QObject::tr("Yes") : QObject::tr("No")) + "\n";
QLabel *label = new QLabel(s);
diff --git a/examples/terminal/settingsdialog.cpp b/examples/terminal/settingsdialog.cpp
index 4533da9..65f6b87 100644
--- a/examples/terminal/settingsdialog.cpp
+++ b/examples/terminal/settingsdialog.cpp
@@ -157,8 +157,8 @@ void SettingsDialog::fillPortsInfo()
<< info.description()
<< info.manufacturer()
<< info.systemLocation()
- << QString::number(info.vendorIdentifier(), 16)
- << QString::number(info.productIdentifier(), 16);
+ << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString())
+ << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : QString());
ui->serialPortInfoListBox->addItem(list.first(), list);
}
diff --git a/src/serialport/qserialportinfo.cpp b/src/serialport/qserialportinfo.cpp
index e16e13e..b78c9eb 100644
--- a/src/serialport/qserialportinfo.cpp
+++ b/src/serialport/qserialportinfo.cpp
@@ -204,6 +204,26 @@ quint16 QSerialPortInfo::productIdentifier() const
}
/*!
+ Returns true if there is a valid 16-bit vendor number present; otherwise
+ returns false.
+*/
+bool QSerialPortInfo::hasVendorIdentifier() const
+{
+ Q_D(const QSerialPortInfo);
+ return !d ? false : d->hasVendorIdentifier;
+}
+
+/*!
+ Returns true if there is a valid 16-bit product number present; otherwise
+ returns false.
+*/
+bool QSerialPortInfo::hasProductIdentifier() const
+{
+ Q_D(const QSerialPortInfo);
+ return !d ? false : d->hasProductIdentifier;
+}
+
+/*!
\fn bool QSerialPortInfo::isNull() const
Returns whether this QSerialPortInfo object holds a
diff --git a/src/serialport/qserialportinfo.h b/src/serialport/qserialportinfo.h
index 19fd5b7..900b91b 100644
--- a/src/serialport/qserialportinfo.h
+++ b/src/serialport/qserialportinfo.h
@@ -71,9 +71,13 @@ public:
QString systemLocation() const;
QString description() const;
QString manufacturer() const;
+
quint16 vendorIdentifier() const;
quint16 productIdentifier() const;
+ bool hasVendorIdentifier() const;
+ bool hasProductIdentifier() const;
+
bool isNull() const;
bool isBusy() const;
bool isValid() const;
diff --git a/src/serialport/qserialportinfo_mac.cpp b/src/serialport/qserialportinfo_mac.cpp
index 59cfad9..44f26b5 100644
--- a/src/serialport/qserialportinfo_mac.cpp
+++ b/src/serialport/qserialportinfo_mac.cpp
@@ -235,20 +235,18 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts()
quint16 value = 0;
if (vendorIdentifier) {
- if (::CFNumberGetValue(CFNumberRef(vendorIdentifier),
- kCFNumberIntType,
- &value)) {
+ serialPortInfo.d_ptr->hasVendorIdentifier = ::CFNumberGetValue(CFNumberRef(vendorIdentifier), kCFNumberIntType, &value);
+ if (serialPortInfo.d_ptr->hasVendorIdentifier)
serialPortInfo.d_ptr->vendorIdentifier = value;
- }
+
::CFRelease(vendorIdentifier);
}
if (productIdentifier) {
- if (::CFNumberGetValue(CFNumberRef(productIdentifier),
- kCFNumberIntType,
- &value)) {
+ serialPortInfo.d_ptr->hasProductIdentifier = ::CFNumberGetValue(CFNumberRef(productIdentifier), kCFNumberIntType, &value);
+ if (serialPortInfo.d_ptr->hasProductIdentifier)
serialPortInfo.d_ptr->productIdentifier = value;
- }
+
::CFRelease(productIdentifier);
}
diff --git a/src/serialport/qserialportinfo_p.h b/src/serialport/qserialportinfo_p.h
index ee7b884..1f12e69 100644
--- a/src/serialport/qserialportinfo_p.h
+++ b/src/serialport/qserialportinfo_p.h
@@ -51,15 +51,25 @@ QT_BEGIN_NAMESPACE
class QSerialPortInfoPrivate
{
public:
- QSerialPortInfoPrivate() : vendorIdentifier(0), productIdentifier(0) {}
+ QSerialPortInfoPrivate()
+ : vendorIdentifier(0)
+ , productIdentifier(0)
+ , hasVendorIdentifier(false)
+ , hasProductIdentifier(false)
+ {}
+
~QSerialPortInfoPrivate() {}
QString portName;
QString device;
QString description;
QString manufacturer;
+
quint16 vendorIdentifier;
quint16 productIdentifier;
+
+ bool hasVendorIdentifier;
+ bool hasProductIdentifier;
};
class QSerialPortInfoPrivateDeleter
diff --git a/src/serialport/qserialportinfo_unix.cpp b/src/serialport/qserialportinfo_unix.cpp
index bfdc3d0..ee76fc6 100644
--- a/src/serialport/qserialportinfo_unix.cpp
+++ b/src/serialport/qserialportinfo_unix.cpp
@@ -147,10 +147,12 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts()
"ID_VENDOR"))).replace('_', ' ');
serialPortInfo.d_ptr->vendorIdentifier =
- QString::fromLatin1(::udev_device_get_property_value(dev, "ID_VENDOR_ID")).toInt(0, 16);
+ QString::fromLatin1(::udev_device_get_property_value(dev,
+ "ID_VENDOR_ID")).toInt(&serialPortInfo.d_ptr->hasVendorIdentifier, 16);
serialPortInfo.d_ptr->productIdentifier =
- QString::fromLatin1(::udev_device_get_property_value(dev, "ID_MODEL_ID")).toInt(0, 16);
+ QString::fromLatin1(::udev_device_get_property_value(dev,
+ "ID_MODEL_ID")).toInt(&serialPortInfo.d_ptr->hasProductIdentifier, 16);
} else if (subsys == QLatin1String("pnp")) { // PNP bus type
// Append this device.
diff --git a/src/serialport/qserialportinfo_win.cpp b/src/serialport/qserialportinfo_win.cpp
index 0a6618e..7ca1f5e 100644
--- a/src/serialport/qserialportinfo_win.cpp
+++ b/src/serialport/qserialportinfo_win.cpp
@@ -178,11 +178,13 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts()
int index = s.indexOf(vendorIdentifierPrefix);
if (index != -1)
- serialPortInfo.d_ptr->vendorIdentifier = s.mid(index + vendorIdentifierPrefix.size(), vendorIdentifierSize).toInt(0, 16);
+ serialPortInfo.d_ptr->vendorIdentifier = s.mid(index + vendorIdentifierPrefix.size(), vendorIdentifierSize)
+ .toInt(&serialPortInfo.d_ptr->hasVendorIdentifier, 16);
index = s.indexOf(productIdentifierPrefix);
if (index != -1)
- serialPortInfo.d_ptr->productIdentifier = s.mid(index + productIdentifierPrefix.size(), productIdentifierSize).toInt(0, 16);
+ serialPortInfo.d_ptr->productIdentifier = s.mid(index + productIdentifierPrefix.size(), productIdentifierSize)
+ .toInt(&serialPortInfo.d_ptr->hasProductIdentifier, 16);
serialPortInfoList.append(serialPortInfo);
}