summaryrefslogtreecommitdiffstats
path: root/src/bluetooth/qlowenergycontrollerbase.cpp
diff options
context:
space:
mode:
authorAlex Blasche <alexander.blasche@qt.io>2017-10-25 13:59:39 +0200
committerAlex Blasche <alexander.blasche@qt.io>2017-11-03 13:08:48 +0000
commit7a9d2e77befb13c1f714f842d4b0ee3fea2d4a6c (patch)
treed95662929b254a4bcd421e7ee2082b8433f360a7 /src/bluetooth/qlowenergycontrollerbase.cpp
parent59ae3cc2ac7baee7e9df1e6ceffcfc882fbe6121 (diff)
Move common helper function from QLECPrivate to QLECPrivateBase
Change-Id: I5f126fabebf36b61a25e23ddc12c4dae21156cbc Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
Diffstat (limited to 'src/bluetooth/qlowenergycontrollerbase.cpp')
-rw-r--r--src/bluetooth/qlowenergycontrollerbase.cpp116
1 files changed, 116 insertions, 0 deletions
diff --git a/src/bluetooth/qlowenergycontrollerbase.cpp b/src/bluetooth/qlowenergycontrollerbase.cpp
index 5e9228e2..0b8218e9 100644
--- a/src/bluetooth/qlowenergycontrollerbase.cpp
+++ b/src/bluetooth/qlowenergycontrollerbase.cpp
@@ -141,4 +141,120 @@ QSharedPointer<QLowEnergyServicePrivate> QLowEnergyControllerPrivateBase::servic
return QSharedPointer<QLowEnergyServicePrivate>();
}
+/*!
+ Returns a valid characteristic if the given handle is the
+ handle of the characteristic itself or one of its descriptors
+ */
+QLowEnergyCharacteristic QLowEnergyControllerPrivateBase::characteristicForHandle(
+ QLowEnergyHandle handle)
+{
+ QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(handle);
+ if (service.isNull())
+ return QLowEnergyCharacteristic();
+
+ if (service->characteristicList.isEmpty())
+ return QLowEnergyCharacteristic();
+
+ // check whether it is the handle of a characteristic header
+ if (service->characteristicList.contains(handle))
+ return QLowEnergyCharacteristic(service, handle);
+
+ // check whether it is the handle of the characteristic value or its descriptors
+ QList<QLowEnergyHandle> charHandles = service->characteristicList.keys();
+ std::sort(charHandles.begin(), charHandles.end());
+ for (int i = charHandles.size() - 1; i >= 0; i--) {
+ if (charHandles.at(i) > handle)
+ continue;
+
+ return QLowEnergyCharacteristic(service, charHandles.at(i));
+ }
+
+ return QLowEnergyCharacteristic();
+}
+
+/*!
+ Returns a valid descriptor if \a handle belongs to a descriptor;
+ otherwise an invalid one.
+ */
+QLowEnergyDescriptor QLowEnergyControllerPrivateBase::descriptorForHandle(
+ QLowEnergyHandle handle)
+{
+ const QLowEnergyCharacteristic matchingChar = characteristicForHandle(handle);
+ if (!matchingChar.isValid())
+ return QLowEnergyDescriptor();
+
+ const QLowEnergyServicePrivate::CharData charData = matchingChar.
+ d_ptr->characteristicList[matchingChar.attributeHandle()];
+
+ if (charData.descriptorList.contains(handle))
+ return QLowEnergyDescriptor(matchingChar.d_ptr, matchingChar.attributeHandle(),
+ handle);
+
+ return QLowEnergyDescriptor();
+}
+
+/*!
+ Returns the length of the updated characteristic value.
+ */
+quint16 QLowEnergyControllerPrivateBase::updateValueOfCharacteristic(
+ QLowEnergyHandle charHandle,const QByteArray &value, bool appendValue)
+{
+ QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(charHandle);
+ if (!service.isNull()) {
+ CharacteristicDataMap::iterator charIt = service->characteristicList.find(charHandle);
+ if (charIt != service->characteristicList.end()) {
+ QLowEnergyServicePrivate::CharData &charDetails = charIt.value();
+
+ if (appendValue)
+ charDetails.value += value;
+ else
+ charDetails.value = value;
+
+ return charDetails.value.size();
+ }
+ }
+
+ return 0;
+}
+
+/*!
+ Returns the length of the updated descriptor value.
+ */
+quint16 QLowEnergyControllerPrivateBase::updateValueOfDescriptor(
+ QLowEnergyHandle charHandle, QLowEnergyHandle descriptorHandle,
+ const QByteArray &value, bool appendValue)
+{
+ QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(charHandle);
+ 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 0;
+}
+
+void QLowEnergyControllerPrivateBase::invalidateServices()
+{
+ foreach (const QSharedPointer<QLowEnergyServicePrivate> service, serviceList.values()) {
+ service->setController(0);
+ service->setState(QLowEnergyService::InvalidService);
+ }
+
+ serviceList.clear();
+}
+
QT_END_NAMESPACE