diff options
author | Konstantin Ritt <ritt.ks@gmail.com> | 2015-09-14 02:35:20 +0400 |
---|---|---|
committer | Konstantin Ritt <ritt.ks@gmail.com> | 2015-09-16 13:35:56 +0000 |
commit | 7a0a5edcc747bdef71f11638a8c3c47fb31f8be6 (patch) | |
tree | 50f97c613097a084abffa7880fd252c5b0b74071 | |
parent | ff59294c0d7a7464870a7a7d3bc1449c7a7695a7 (diff) |
[Bluetooth] Optimize loops
Decrease complexity from O(N+N*logN) to just O(N) for cases like
foreach (Key key, map.keys())
Value value = map.value(key);
, by rewriting with use of iterators.
Change-Id: I81f1334797f16b624293fcebdee885b2be3c89f1
Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
-rw-r--r-- | src/bluetooth/bluez/bluez5_helper.cpp | 11 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp | 11 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothlocaldevice_bluez.cpp | 48 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothservicediscoveryagent_bluez.cpp | 18 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothserviceinfo.cpp | 2 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothserviceinfo_osx.mm | 2 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothsocket_bluez.cpp | 16 | ||||
-rw-r--r-- | src/bluetooth/qlowenergycharacteristic.cpp | 17 | ||||
-rw-r--r-- | src/bluetooth/qlowenergycontroller.cpp | 59 | ||||
-rw-r--r-- | src/bluetooth/qlowenergycontroller_android.cpp | 8 | ||||
-rw-r--r-- | src/bluetooth/qlowenergycontroller_bluez.cpp | 20 | ||||
-rw-r--r-- | src/bluetooth/qlowenergycontroller_osx.mm | 61 | ||||
-rw-r--r-- | src/bluetooth/qlowenergyservice.cpp | 10 | ||||
-rw-r--r-- | src/bluetooth/qlowenergyservice_osx.mm | 10 | ||||
-rw-r--r-- | src/bluetooth/qlowenergyserviceprivate_p.h | 3 |
15 files changed, 196 insertions, 100 deletions
diff --git a/src/bluetooth/bluez/bluez5_helper.cpp b/src/bluetooth/bluez/bluez5_helper.cpp index 28b5c12d..384b9979 100644 --- a/src/bluetooth/bluez/bluez5_helper.cpp +++ b/src/bluetooth/bluez/bluez5_helper.cpp @@ -302,9 +302,14 @@ QString findAdapterForAddress(const QBluetoothAddress &wantedAddress, bool *ok = typedef QPair<QString, QBluetoothAddress> AddressForPathType; QList<AddressForPathType> localAdapters; - foreach (const QDBusObjectPath &path, reply.value().keys()) { - const InterfaceList ifaceList = reply.value().value(path); - foreach (const QString &iface, ifaceList.keys()) { + ManagedObjectList managedObjectList = reply.value(); + for (ManagedObjectList::const_iterator it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) { + const QDBusObjectPath &path = it.key(); + const InterfaceList &ifaceList = it.value(); + + for (InterfaceList::const_iterator jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) { + const QString &iface = jt.key(); + if (iface == QStringLiteral("org.bluez.Adapter1")) { AddressForPathType pair; pair.first = path.path(); diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp index 7f477a02..b0fd88d5 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp @@ -239,9 +239,14 @@ void QBluetoothDeviceDiscoveryAgentPrivate::startBluez5() QDBusPendingReply<ManagedObjectList> reply = managerBluez5->GetManagedObjects(); reply.waitForFinished(); if (!reply.isError()) { - foreach (const QDBusObjectPath &path, reply.value().keys()) { - const InterfaceList ifaceList = reply.value().value(path); - foreach (const QString &iface, ifaceList.keys()) { + ManagedObjectList managedObjectList = reply.value(); + for (ManagedObjectList::const_iterator it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) { + const QDBusObjectPath &path = it.key(); + const InterfaceList &ifaceList = it.value(); + + for (InterfaceList::const_iterator jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) { + const QString &iface = jt.key(); + if (iface == QStringLiteral("org.bluez.Device1")) { if (path.path().indexOf(adapterBluez5->path()) != 0) diff --git a/src/bluetooth/qbluetoothlocaldevice_bluez.cpp b/src/bluetooth/qbluetoothlocaldevice_bluez.cpp index cae21e1b..d2ec70b9 100644 --- a/src/bluetooth/qbluetoothlocaldevice_bluez.cpp +++ b/src/bluetooth/qbluetoothlocaldevice_bluez.cpp @@ -213,17 +213,22 @@ QList<QBluetoothHostInfo> QBluetoothLocalDevice::allDevices() if (reply.isError()) return localDevices; - foreach (const QDBusObjectPath &path, reply.value().keys()) { - const InterfaceList ifaceList = reply.value().value(path); - foreach (const QString &iface, ifaceList.keys()) { + ManagedObjectList managedObjectList = reply.value(); + for (ManagedObjectList::const_iterator it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) { + const InterfaceList &ifaceList = it.value(); + + for (InterfaceList::const_iterator jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) { + const QString &iface = jt.key(); + const QVariantMap &ifaceValues = jt.value(); + if (iface == QStringLiteral("org.bluez.Adapter1")) { QBluetoothHostInfo hostInfo; - const QString temp = ifaceList.value(iface).value(QStringLiteral("Address")).toString(); + const QString temp = ifaceValues.value(QStringLiteral("Address")).toString(); hostInfo.setAddress(QBluetoothAddress(temp)); if (hostInfo.address().isNull()) continue; - hostInfo.setName(ifaceList.value(iface).value(QStringLiteral("Name")).toString()); + hostInfo.setName(ifaceValues.value(QStringLiteral("Name")).toString()); localDevices.append(hostInfo); } } @@ -447,10 +452,13 @@ void QBluetoothLocalDevicePrivate::requestPairingBluez5(const QBluetoothAddress return; } + ManagedObjectList managedObjectList = reply.value(); + for (ManagedObjectList::const_iterator it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) { + const QDBusObjectPath &path = it.key(); + const InterfaceList &ifaceList = it.value(); - foreach (const QDBusObjectPath &path, reply.value().keys()) { - const InterfaceList ifaceList = reply.value().value(path); - foreach (const QString &iface, ifaceList.keys()) { + for (InterfaceList::const_iterator jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) { + const QString &iface = jt.key(); if (iface == QStringLiteral("org.bluez.Device1")) { @@ -603,9 +611,13 @@ QBluetoothLocalDevice::Pairing QBluetoothLocalDevice::pairingStatus( if (reply.isError()) return Unpaired; - foreach (const QDBusObjectPath &path, reply.value().keys()) { - const InterfaceList ifaceList = reply.value().value(path); - foreach (const QString &iface, ifaceList.keys()) { + ManagedObjectList managedObjectList = reply.value(); + for (ManagedObjectList::const_iterator it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) { + const QDBusObjectPath &path = it.key(); + const InterfaceList &ifaceList = it.value(); + + for (InterfaceList::const_iterator jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) { + const QString &iface = jt.key(); if (iface == QStringLiteral("org.bluez.Device1")) { @@ -670,9 +682,16 @@ void QBluetoothLocalDevicePrivate::connectDeviceChanges() return; OrgFreedesktopDBusPropertiesInterface *monitor = 0; - foreach (const QDBusObjectPath &path, reply.value().keys()) { - const InterfaceList ifaceList = reply.value().value(path); - foreach (const QString &iface, ifaceList.keys()) { + + ManagedObjectList managedObjectList = reply.value(); + for (ManagedObjectList::const_iterator it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) { + const QDBusObjectPath &path = it.key(); + const InterfaceList &ifaceList = it.value(); + + for (InterfaceList::const_iterator jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) { + const QString &iface = jt.key(); + const QVariantMap &ifaceValues = jt.value(); + if (iface == QStringLiteral("org.bluez.Device1")) { monitor = new OrgFreedesktopDBusPropertiesInterface(QStringLiteral("org.bluez"), path.path(), @@ -681,7 +700,6 @@ void QBluetoothLocalDevicePrivate::connectDeviceChanges() SLOT(PropertiesChanged(QString,QVariantMap,QStringList))); deviceChangeMonitors.insert(path.path(), monitor); - const QVariantMap ifaceValues = ifaceList.value(QStringLiteral("org.bluez.Device1")); if (ifaceValues.value(QStringLiteral("Connected"), false).toBool()) { QBluetoothAddress address(ifaceValues.value(QStringLiteral("Address")).toString()); connectedDevicesSet.insert(address); diff --git a/src/bluetooth/qbluetoothservicediscoveryagent_bluez.cpp b/src/bluetooth/qbluetoothservicediscoveryagent_bluez.cpp index de12e6f2..ae51d681 100644 --- a/src/bluetooth/qbluetoothservicediscoveryagent_bluez.cpp +++ b/src/bluetooth/qbluetoothservicediscoveryagent_bluez.cpp @@ -652,14 +652,18 @@ void QBluetoothServiceDiscoveryAgentPrivate::performMinimalServiceDiscovery(cons } QStringList uuidStrings; - foreach (const QDBusObjectPath &path, reply.value().keys()) { - const InterfaceList ifaceList = reply.value().value(path); - foreach (const QString &iface, ifaceList.keys()) { + + ManagedObjectList managedObjectList = reply.value(); + for (ManagedObjectList::const_iterator it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) { + const InterfaceList &ifaceList = it.value(); + + for (InterfaceList::const_iterator jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) { + const QString &iface = jt.key(); + const QVariantMap &ifaceValues = jt.value(); + if (iface == QStringLiteral("org.bluez.Device1")) { - const QVariantMap details = ifaceList.value(iface); - if (deviceAddress.toString() - == details.value(QStringLiteral("Address")).toString()) { - uuidStrings = details.value(QStringLiteral("UUIDs")).toStringList(); + if (deviceAddress.toString() == ifaceValues.value(QStringLiteral("Address")).toString()) { + uuidStrings = ifaceValues.value(QStringLiteral("UUIDs")).toStringList(); break; } } diff --git a/src/bluetooth/qbluetoothserviceinfo.cpp b/src/bluetooth/qbluetoothserviceinfo.cpp index d6cc633b..fefb2549 100644 --- a/src/bluetooth/qbluetoothserviceinfo.cpp +++ b/src/bluetooth/qbluetoothserviceinfo.cpp @@ -375,7 +375,7 @@ bool QBluetoothServiceInfo::isValid() const */ bool QBluetoothServiceInfo::isComplete() const { - return d_ptr->attributes.keys().contains(ProtocolDescriptorList); + return d_ptr->attributes.contains(ProtocolDescriptorList); } /*! diff --git a/src/bluetooth/qbluetoothserviceinfo_osx.mm b/src/bluetooth/qbluetoothserviceinfo_osx.mm index 743fb377..d79156e2 100644 --- a/src/bluetooth/qbluetoothserviceinfo_osx.mm +++ b/src/bluetooth/qbluetoothserviceinfo_osx.mm @@ -309,7 +309,7 @@ bool QBluetoothServiceInfo::isValid() const bool QBluetoothServiceInfo::isComplete() const { - return d_ptr->attributes.keys().contains(ProtocolDescriptorList); + return d_ptr->attributes.contains(ProtocolDescriptorList); } QBluetoothDeviceInfo QBluetoothServiceInfo::device() const diff --git a/src/bluetooth/qbluetoothsocket_bluez.cpp b/src/bluetooth/qbluetoothsocket_bluez.cpp index 6947e9d0..71f26296 100644 --- a/src/bluetooth/qbluetoothsocket_bluez.cpp +++ b/src/bluetooth/qbluetoothsocket_bluez.cpp @@ -384,13 +384,17 @@ QString QBluetoothSocketPrivate::peerName() const if (reply.isError()) return QString(); - foreach (const QDBusObjectPath &path, reply.value().keys()) { - const InterfaceList ifaceList = reply.value().value(path); - foreach (const QString &iface, ifaceList.keys()) { + ManagedObjectList managedObjectList = reply.value(); + for (ManagedObjectList::const_iterator it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) { + const InterfaceList &ifaceList = it.value(); + + for (InterfaceList::const_iterator jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) { + const QString &iface = jt.key(); + const QVariantMap &ifaceValues = jt.value(); + if (iface == QStringLiteral("org.bluez.Device1")) { - if (ifaceList.value(iface).value(QStringLiteral("Address")).toString() - == peerAddress) - return ifaceList.value(iface).value(QStringLiteral("Alias")).toString(); + if (ifaceValues.value(QStringLiteral("Address")).toString() == peerAddress) + return ifaceValues.value(QStringLiteral("Alias")).toString(); } } } diff --git a/src/bluetooth/qlowenergycharacteristic.cpp b/src/bluetooth/qlowenergycharacteristic.cpp index aba4a66b..885b1337 100644 --- a/src/bluetooth/qlowenergycharacteristic.cpp +++ b/src/bluetooth/qlowenergycharacteristic.cpp @@ -338,11 +338,18 @@ QLowEnergyDescriptor QLowEnergyCharacteristic::descriptor(const QBluetoothUuid & if (d_ptr.isNull() || !data) return QLowEnergyDescriptor(); - QList<QLowEnergyHandle> descriptorKeys = d_ptr->characteristicList[data->handle]. - descriptorList.keys(); - foreach (const QLowEnergyHandle descHandle, descriptorKeys) { - if (uuid == d_ptr->characteristicList[data->handle].descriptorList[descHandle].uuid) - return QLowEnergyDescriptor(d_ptr, data->handle, descHandle); + CharacteristicDataMap::const_iterator charIt = d_ptr->characteristicList.constFind(data->handle); + if (charIt != d_ptr->characteristicList.constEnd()) { + const QLowEnergyServicePrivate::CharData &charDetails = charIt.value(); + + DescriptorDataMap::const_iterator descIt = charDetails.descriptorList.constBegin(); + for ( ; descIt != charDetails.descriptorList.constEnd(); ++descIt) { + const QLowEnergyHandle descHandle = descIt.key(); + const QLowEnergyServicePrivate::DescData &descDetails = descIt.value(); + + if (descDetails.uuid == uuid) + return QLowEnergyDescriptor(d_ptr, data->handle, descHandle); + } } return QLowEnergyDescriptor(); diff --git a/src/bluetooth/qlowenergycontroller.cpp b/src/bluetooth/qlowenergycontroller.cpp index 44b6a6da..99a3724e 100644 --- a/src/bluetooth/qlowenergycontroller.cpp +++ b/src/bluetooth/qlowenergycontroller.cpp @@ -339,13 +339,18 @@ quint16 QLowEnergyControllerPrivate::updateValueOfCharacteristic( QLowEnergyHandle charHandle,const QByteArray &value, bool appendValue) { QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(charHandle); - if (!service.isNull() && service->characteristicList.contains(charHandle)) { - if (appendValue) - service->characteristicList[charHandle].value += value; - else - service->characteristicList[charHandle].value = value; + if (!service.isNull()) { + CharacteristicDataMap::iterator charIt = service->characteristicList.find(charHandle); + if (charIt != service->characteristicList.end()) { + QLowEnergyServicePrivate::CharData &charDetails = charIt.value(); - return service->characteristicList[charHandle].value.size(); + if (appendValue) + charDetails.value += value; + else + charDetails.value = value; + + return charDetails.value.size(); + } } return 0; @@ -359,18 +364,26 @@ quint16 QLowEnergyControllerPrivate::updateValueOfDescriptor( const QByteArray &value, bool appendValue) { QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(charHandle); - if (service.isNull() || !service->characteristicList.contains(charHandle)) - return 0; - - if (!service->characteristicList[charHandle].descriptorList.contains(descriptorHandle)) - return 0; - - if (appendValue) - service->characteristicList[charHandle].descriptorList[descriptorHandle].value += value; - else - service->characteristicList[charHandle].descriptorList[descriptorHandle].value = value; + if (!service.isNull()) { + CharacteristicDataMap::iterator charIt = service->characteristicList.find(charHandle); + if (charIt != service->characteristicList.end()) { + QLowEnergyServicePrivate::CharData &charDetails = charIt.value(); + + DescriptorDataMap::iterator descIt = charDetails.descriptorList.find(descriptorHandle); + if (descIt != charDetails.descriptorList.end()) { + QLowEnergyServicePrivate::DescData &descDetails = descIt.value(); + + if (appendValue) + descDetails.value += value; + else + descDetails.value = value; + + return descDetails.value.size(); + } + } + } - return service->characteristicList[charHandle].descriptorList[descriptorHandle].value.size(); + return 0; } /*! @@ -641,11 +654,15 @@ QLowEnergyService *QLowEnergyController::createServiceObject( const QBluetoothUuid &serviceUuid, QObject *parent) { Q_D(QLowEnergyController); - if (!d->serviceList.contains(serviceUuid)) - return 0; - QLowEnergyService *service = new QLowEnergyService( - d->serviceList.value(serviceUuid), parent); + QLowEnergyService *service = Q_NULLPTR; + + ServiceDataMap::const_iterator it = d->serviceList.constFind(serviceUuid); + if (it != d->serviceList.constEnd()) { + const QSharedPointer<QLowEnergyServicePrivate> &serviceData = it.value(); + + service = new QLowEnergyService(serviceData, parent); + } return service; } diff --git a/src/bluetooth/qlowenergycontroller_android.cpp b/src/bluetooth/qlowenergycontroller_android.cpp index f48c0e85..b04ddedd 100644 --- a/src/bluetooth/qlowenergycontroller_android.cpp +++ b/src/bluetooth/qlowenergycontroller_android.cpp @@ -453,9 +453,11 @@ void QLowEnergyControllerPrivate::descriptorRead( serviceList.value(serviceUuid); bool entryUpdated = false; - foreach (QLowEnergyHandle charHandle, service->characteristicList.keys()) { - QLowEnergyServicePrivate::CharData &charDetails = - service->characteristicList[charHandle]; + + CharacteristicDataMap::iterator charIt = service->characteristicList.begin(); + for ( ; charIt != service->characteristicList.end(); ++charIt) { + QLowEnergyServicePrivate::CharData &charDetails = charIt.value(); + if (charDetails.uuid != charUuid) continue; diff --git a/src/bluetooth/qlowenergycontroller_bluez.cpp b/src/bluetooth/qlowenergycontroller_bluez.cpp index 8906eb06..a7b2e794 100644 --- a/src/bluetooth/qlowenergycontroller_bluez.cpp +++ b/src/bluetooth/qlowenergycontroller_bluez.cpp @@ -1151,12 +1151,11 @@ void QLowEnergyControllerPrivate::readServiceValues( // Create list of attribute handles which need to be read QList<QPair<QLowEnergyHandle, quint32> > targetHandles; - const QList<QLowEnergyHandle> keys = service->characteristicList.keys(); - for (int i = 0; i < keys.count(); i++) { - const QLowEnergyHandle charHandle = keys[i]; - const QLowEnergyServicePrivate::CharData &charDetails = - service->characteristicList[charHandle]; + CharacteristicDataMap::const_iterator charIt = service->characteristicList.constBegin(); + for ( ; charIt != service->characteristicList.constEnd(); ++charIt) { + const QLowEnergyHandle charHandle = charIt.key(); + const QLowEnergyServicePrivate::CharData &charDetails = charIt.value(); if (readCharacteristics) { // Collect handles of all characteristic value attributes @@ -1171,7 +1170,10 @@ void QLowEnergyControllerPrivate::readServiceValues( } else { // Collect handles of all descriptor attributes - foreach (QLowEnergyHandle descriptorHandle, charDetails.descriptorList.keys()) { + DescriptorDataMap::const_iterator descIt = charDetails.descriptorList.constBegin(); + for ( ; descIt != charDetails.descriptorList.constEnd(); ++descIt) { + const QLowEnergyHandle descriptorHandle = descIt.key(); + pair.first = descriptorHandle; pair.second = (charHandle | (descriptorHandle << 16)); targetHandles.append(pair); @@ -1270,15 +1272,15 @@ void QLowEnergyControllerPrivate::discoverServiceDescriptors( qCDebug(QT_BT_BLUEZ) << "Discovering descriptor values for" << serviceUuid.toString(); QSharedPointer<QLowEnergyServicePrivate> service = serviceList.value(serviceUuid); - // start handle of all known characteristics - QList<QLowEnergyHandle> keys = service->characteristicList.keys(); - if (keys.isEmpty()) { // service has no characteristics + if (service->characteristicList.isEmpty()) { // service has no characteristics // implies that characteristic & descriptor discovery can be skipped service->setState(QLowEnergyService::ServiceDiscovered); return; } + // start handle of all known characteristics + QList<QLowEnergyHandle> keys = service->characteristicList.keys(); std::sort(keys.begin(), keys.end()); discoverNextDescriptor(service, keys, keys[0]); diff --git a/src/bluetooth/qlowenergycontroller_osx.mm b/src/bluetooth/qlowenergycontroller_osx.mm index c4628069..d0d65248 100644 --- a/src/bluetooth/qlowenergycontroller_osx.mm +++ b/src/bluetooth/qlowenergycontroller_osx.mm @@ -277,7 +277,9 @@ void QLowEnergyControllerPrivateOSX::serviceDiscoveryFinished(LEServices service qCDebug(QT_BT_OSX) << Q_FUNC_INFO << "no services found"; } - foreach (const QBluetoothUuid &uuid, discoveredServices.keys()) { + for (ServiceMap::const_iterator it = discoveredServices.constBegin(); it != discoveredServices.constEnd(); ++it) { + const QBluetoothUuid &uuid = it.key(); + QMetaObject::invokeMethod(q_ptr, "serviceDiscovered", Qt::QueuedConnection, Q_ARG(QBluetoothUuid, uuid)); } @@ -642,13 +644,18 @@ quint16 QLowEnergyControllerPrivateOSX::updateValueOfCharacteristic(QLowEnergyHa bool appendValue) { QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(charHandle); - if (!service.isNull() && service->characteristicList.contains(charHandle)) { - if (appendValue) - service->characteristicList[charHandle].value += value; - else - service->characteristicList[charHandle].value = value; + if (!service.isNull()) { + CharacteristicDataMap::iterator charIt = service->characteristicList.find(charHandle); + if (charIt != service->characteristicList.end()) { + QLowEnergyServicePrivate::CharData &charData = charIt.value(); + + if (appendValue) + charData.value += value; + else + charData.value = value; - return service->characteristicList[charHandle].value.size(); + return charData.value.size(); + } } return 0; @@ -693,19 +700,27 @@ void QLowEnergyControllerPrivateOSX::writeDescriptor(QSharedPointer<QLowEnergySe quint16 QLowEnergyControllerPrivateOSX::updateValueOfDescriptor(QLowEnergyHandle charHandle, QLowEnergyHandle descHandle, const QByteArray &value, bool appendValue) { - ServicePrivate service(serviceForHandle(charHandle)); - if (service.isNull() || !service->characteristicList.contains(charHandle)) - return 0; + QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(charHandle); + if (!service.isNull()) { + CharacteristicDataMap::iterator charIt = service->characteristicList.find(charHandle); + if (charIt != service->characteristicList.end()) { + QLowEnergyServicePrivate::CharData &charData = charIt.value(); - if (!service->characteristicList[charHandle].descriptorList.contains(descHandle)) - return 0; + DescriptorDataMap::iterator descIt = charData.descriptorList.find(descHandle); + if (descIt != charData.descriptorList.end()) { + QLowEnergyServicePrivate::DescData &descDetails = descIt.value(); - if (appendValue) - service->characteristicList[charHandle].descriptorList[descHandle].value += value; - else - service->characteristicList[charHandle].descriptorList[descHandle].value = value; + if (appendValue) + descDetails.value += value; + else + descDetails.value = value; - return service->characteristicList[charHandle].descriptorList[descHandle].value.size(); + return descDetails.value.size(); + } + } + } + + return 0; } QSharedPointer<QLowEnergyServicePrivate> QLowEnergyControllerPrivateOSX::serviceForHandle(QLowEnergyHandle handle) @@ -952,10 +967,16 @@ QLowEnergyService *QLowEnergyController::createServiceObject(const QBluetoothUui { OSX_D_PTR; - if (!osx_d_ptr->discoveredServices.contains(serviceUuid)) - return Q_NULLPTR; + QLowEnergyService *service = Q_NULLPTR; + + QLowEnergyControllerPrivateOSX::ServiceMap::const_iterator it = osx_d_ptr->discoveredServices.constFind(serviceUuid); + if (it != osx_d_ptr->discoveredServices.constEnd()) { + const QSharedPointer<QLowEnergyServicePrivate> &serviceData = it.value(); + + service = new QLowEnergyService(serviceData, parent); + } - return new QLowEnergyService(osx_d_ptr->discoveredServices.value(serviceUuid), parent); + return service; } QLowEnergyController::Error QLowEnergyController::error() const diff --git a/src/bluetooth/qlowenergyservice.cpp b/src/bluetooth/qlowenergyservice.cpp index 67bd7c77..9fb8e47c 100644 --- a/src/bluetooth/qlowenergyservice.cpp +++ b/src/bluetooth/qlowenergyservice.cpp @@ -454,9 +454,13 @@ QLowEnergyService::ServiceTypes QLowEnergyService::type() const */ QLowEnergyCharacteristic QLowEnergyService::characteristic(const QBluetoothUuid &uuid) const { - foreach (const QLowEnergyHandle handle, d_ptr->characteristicList.keys()) { - if (d_ptr->characteristicList[handle].uuid == uuid) - return QLowEnergyCharacteristic(d_ptr, handle); + CharacteristicDataMap::const_iterator charIt = d_ptr->characteristicList.constBegin(); + for ( ; charIt != d_ptr->characteristicList.constEnd(); ++charIt) { + const QLowEnergyHandle charHandle = charIt.key(); + const QLowEnergyServicePrivate::CharData &charDetails = charIt.value(); + + if (charDetails.uuid == uuid) + return QLowEnergyCharacteristic(d_ptr, charHandle); } return QLowEnergyCharacteristic(); diff --git a/src/bluetooth/qlowenergyservice_osx.mm b/src/bluetooth/qlowenergyservice_osx.mm index 78861dc1..da1636cb 100644 --- a/src/bluetooth/qlowenergyservice_osx.mm +++ b/src/bluetooth/qlowenergyservice_osx.mm @@ -104,9 +104,13 @@ QLowEnergyService::ServiceState QLowEnergyService::state() const QLowEnergyCharacteristic QLowEnergyService::characteristic(const QBluetoothUuid &uuid) const { - foreach (const QLowEnergyHandle handle, d_ptr->characteristicList.keys()) { - if (d_ptr->characteristicList[handle].uuid == uuid) - return QLowEnergyCharacteristic(d_ptr, handle); + CharacteristicDataMap::const_iterator charIt = d_ptr->characteristicList.constBegin(); + for ( ; charIt != d_ptr->characteristicList.constEnd(); ++charIt) { + const QLowEnergyHandle charHandle = charIt.key(); + const QLowEnergyServicePrivate::CharData &charDetails = charIt.value(); + + if (charDetails.uuid == uuid) + return QLowEnergyCharacteristic(d_ptr, charHandle); } return QLowEnergyCharacteristic(); diff --git a/src/bluetooth/qlowenergyserviceprivate_p.h b/src/bluetooth/qlowenergyserviceprivate_p.h index a020341f..0232b912 100644 --- a/src/bluetooth/qlowenergyserviceprivate_p.h +++ b/src/bluetooth/qlowenergyserviceprivate_p.h @@ -104,6 +104,9 @@ public: QPointer<QLowEnergyControllerPrivate> controller; }; +typedef QHash<QLowEnergyHandle, QLowEnergyServicePrivate::CharData> CharacteristicDataMap; +typedef QHash<QLowEnergyHandle, QLowEnergyServicePrivate::DescData> DescriptorDataMap; + QT_END_NAMESPACE Q_DECLARE_METATYPE(QSharedPointer<QLowEnergyServicePrivate>) |