summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLaszlo Papp <lpapp@kde.org>2013-07-27 16:57:08 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-03-02 21:05:07 +0100
commit959775c41683033adbd99faab7e3d70e0009c143 (patch)
tree4d6586946d44617271dbbc56840d45196e855dec /src
parent5a2314414fc89c6ef44521f6d13899045b6da7af (diff)
Add API for querying the serial number
Thanks go to Massimo Callegari for the initial patch and the request to remind us again. Thanks also go to Denis and Sergey for working on the windows serial number parser. Task-number: QTBUG-31981 Change-Id: I60d882280f481eb99d275e0a9c81da50292b1c61 Reviewed-by: Massimo Callegari <massimocallegari@yahoo.it> Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/serialport/qserialportinfo.cpp20
-rw-r--r--src/serialport/qserialportinfo.h1
-rw-r--r--src/serialport/qserialportinfo_mac.cpp26
-rw-r--r--src/serialport/qserialportinfo_p.h1
-rw-r--r--src/serialport/qserialportinfo_symbian.cpp2
-rw-r--r--src/serialport/qserialportinfo_unix.cpp3
-rw-r--r--src/serialport/qserialportinfo_win.cpp24
7 files changed, 74 insertions, 3 deletions
diff --git a/src/serialport/qserialportinfo.cpp b/src/serialport/qserialportinfo.cpp
index 401973fb..90b22cec 100644
--- a/src/serialport/qserialportinfo.cpp
+++ b/src/serialport/qserialportinfo.cpp
@@ -170,7 +170,7 @@ QString QSerialPortInfo::systemLocation() const
Returns the description string of the serial port,
if available; otherwise returns an empty string.
- \sa manufacturer()
+ \sa manufacturer(), serialNumber()
*/
QString QSerialPortInfo::description() const
{
@@ -182,7 +182,7 @@ QString QSerialPortInfo::description() const
Returns the manufacturer string of the serial port,
if available; otherwise returns an empty string.
- \sa description()
+ \sa description(), serialNumber()
*/
QString QSerialPortInfo::manufacturer() const
{
@@ -191,6 +191,22 @@ QString QSerialPortInfo::manufacturer() const
}
/*!
+ \since 5.3
+
+ Returns the serial number string of the serial port,
+ if available; otherwise returns an empty string.
+
+ \note The serial number may include letters.
+
+ \sa description(), manufacturer()
+*/
+QString QSerialPortInfo::serialNumber() const
+{
+ Q_D(const QSerialPortInfo);
+ return !d ? QString() : d->serialNumber;
+}
+
+/*!
Returns the 16-bit vendor number for the serial port, if available;
otherwise returns zero.
diff --git a/src/serialport/qserialportinfo.h b/src/serialport/qserialportinfo.h
index 8320fc5a..7635caa6 100644
--- a/src/serialport/qserialportinfo.h
+++ b/src/serialport/qserialportinfo.h
@@ -71,6 +71,7 @@ public:
QString systemLocation() const;
QString description() const;
QString manufacturer() const;
+ QString serialNumber() const;
quint16 vendorIdentifier() const;
quint16 productIdentifier() const;
diff --git a/src/serialport/qserialportinfo_mac.cpp b/src/serialport/qserialportinfo_mac.cpp
index c69a4a30..2c2564e9 100644
--- a/src/serialport/qserialportinfo_mac.cpp
+++ b/src/serialport/qserialportinfo_mac.cpp
@@ -63,7 +63,7 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts()
{
QList<QSerialPortInfo> serialPortInfoList;
- static const int propertyCount = 6;
+ static const int propertyCount = 7;
::CFMutableDictionaryRef matching = ::IOServiceMatching(kIOSerialBSDServiceValue);
@@ -90,6 +90,7 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts()
::CFTypeRef portName = 0;
::CFTypeRef description = 0;
::CFTypeRef manufacturer = 0;
+ ::CFTypeRef serialNumber = 0;
::CFTypeRef vendorIdentifier = 0;
::CFTypeRef productIdentifier = 0;
@@ -159,6 +160,19 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts()
}
+ if (!serialNumber) {
+ serialNumber =
+ ::IORegistryEntrySearchCFProperty(entry,
+ kIOServicePlane,
+ CFSTR(kUSBSerialNumberString),
+ kCFAllocatorDefault,
+ 0);
+ if (serialNumber)
+ ++matchingPropertiesCounter;
+
+ }
+
+
if (!vendorIdentifier) {
vendorIdentifier =
::IORegistryEntrySearchCFProperty(entry,
@@ -237,6 +251,16 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts()
::CFRelease(manufacturer);
}
+ if (serialNumber) {
+ if (::CFStringGetCString(CFStringRef(serialNumber),
+ buffer.data(),
+ buffer.size(),
+ kCFStringEncodingUTF8)) {
+ serialPortInfo.d_ptr->serialNumber = QString::fromUtf8(buffer);
+ }
+ ::CFRelease(serialNumber);
+ }
+
quint16 value = 0;
if (vendorIdentifier) {
diff --git a/src/serialport/qserialportinfo_p.h b/src/serialport/qserialportinfo_p.h
index 1f12e69b..47319410 100644
--- a/src/serialport/qserialportinfo_p.h
+++ b/src/serialport/qserialportinfo_p.h
@@ -64,6 +64,7 @@ public:
QString device;
QString description;
QString manufacturer;
+ QString serialNumber;
quint16 vendorIdentifier;
quint16 productIdentifier;
diff --git a/src/serialport/qserialportinfo_symbian.cpp b/src/serialport/qserialportinfo_symbian.cpp
index cae00e97..3f851b50 100644
--- a/src/serialport/qserialportinfo_symbian.cpp
+++ b/src/serialport/qserialportinfo_symbian.cpp
@@ -125,6 +125,7 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts()
serialPortInfo.d_ptr->description =
QString::fromUtf16(nativeSerialInfo.iDescription.Ptr(), nativeSerialInfo.iDescription.Length());
serialPortInfo.d_ptr->manufacturer = QString(QObject::tr("Unknown."));
+ serialPortInfo.d_ptr->serialNumber = QString(QObject::tr("Unknown."));
serialPortInfoList.append(serialPortInfo);
}
}
@@ -146,6 +147,7 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts()
serialPortInfo.d_ptr->description =
QString::fromUtf16(nativeSerialInfo.iDescription.Ptr(), nativeSerialInfo.iDescription.Length());
serialPortInfo.d_ptr->manufacturer = QString(QObject::tr("Unknown."));
+ serialPortInfo.d_ptr->serialNumber = QString(QObject::tr("Unknown."));
serialPortInfoList.append(serialPortInfo);
}
}
diff --git a/src/serialport/qserialportinfo_unix.cpp b/src/serialport/qserialportinfo_unix.cpp
index a8bb6d3d..9ef113ba 100644
--- a/src/serialport/qserialportinfo_unix.cpp
+++ b/src/serialport/qserialportinfo_unix.cpp
@@ -251,6 +251,9 @@ QList<QSerialPortInfo> availablePortsByUdev()
serialPortInfo.d_ptr->manufacturer = QString::fromLatin1(::udev_device_get_property_value(dev,
"ID_VENDOR")).replace(QLatin1Char('_'), QLatin1Char(' '));
+ serialPortInfo.d_ptr->serialNumber = QString(
+ QLatin1String(::udev_device_get_property_value(dev, "ID_SERIAL_SHORT")));
+
serialPortInfo.d_ptr->vendorIdentifier =
QString::fromLatin1(::udev_device_get_property_value(dev,
"ID_VENDOR_ID")).toInt(&serialPortInfo.d_ptr->hasVendorIdentifier, 16);
diff --git a/src/serialport/qserialportinfo_win.cpp b/src/serialport/qserialportinfo_win.cpp
index 85d40cc3..448f3edc 100644
--- a/src/serialport/qserialportinfo_win.cpp
+++ b/src/serialport/qserialportinfo_win.cpp
@@ -195,6 +195,28 @@ private:
const QString &m_serialPortName;
};
+static QString deviceSerialNumber(const QString &instanceIdentifier)
+{
+ int firstbound = instanceIdentifier.lastIndexOf(QLatin1Char('\\'));
+ int lastbound = instanceIdentifier.indexOf(QLatin1Char('_'), firstbound);
+ if (instanceIdentifier.startsWith(QStringLiteral("USB\\"))) {
+ if (lastbound != instanceIdentifier.size() - 3)
+ lastbound = instanceIdentifier.size();
+ int ampersand = instanceIdentifier.indexOf(QLatin1Char('&'), firstbound);
+ if (ampersand != -1 && ampersand < lastbound)
+ return QString();
+ } else if (instanceIdentifier.startsWith(QStringLiteral("FTDIBUS\\"))) {
+ firstbound = instanceIdentifier.lastIndexOf(QLatin1Char('+'));
+ lastbound = instanceIdentifier.indexOf(QLatin1Char('\\'), firstbound);
+ if (lastbound == -1)
+ return QString();
+ } else {
+ return QString();
+ }
+
+ return instanceIdentifier.mid(firstbound + 1, lastbound - firstbound - 1);
+}
+
QList<QSerialPortInfo> QSerialPortInfo::availablePorts()
{
static const QString usbVendorIdentifierPrefix(QStringLiteral("VID_"));
@@ -238,6 +260,8 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts()
s = deviceInstanceIdentifier(deviceInfoSet, &deviceInfoData).toUpper();
+ serialPortInfo.d_ptr->serialNumber = deviceSerialNumber(s);
+
int index = s.indexOf(usbVendorIdentifierPrefix);
if (index != -1) {
serialPortInfo.d_ptr->vendorIdentifier = s.mid(index + usbVendorIdentifierPrefix.size(), vendorIdentifierSize)