1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtBluetooth module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qlowenergycontrollerbase_p.h" |
41 | |
42 | #include <QtCore/QLoggingCategory> |
43 | |
44 | #include <QtBluetooth/QBluetoothLocalDevice> |
45 | #include <QtBluetooth/QLowEnergyCharacteristicData> |
46 | #include <QtBluetooth/QLowEnergyDescriptorData> |
47 | #include <QtBluetooth/QLowEnergyServiceData> |
48 | |
49 | QT_BEGIN_NAMESPACE |
50 | |
51 | Q_DECLARE_LOGGING_CATEGORY(QT_BT) |
52 | |
53 | QLowEnergyControllerPrivate::QLowEnergyControllerPrivate() |
54 | : QObject() |
55 | { |
56 | } |
57 | |
58 | QLowEnergyControllerPrivate::~QLowEnergyControllerPrivate() |
59 | { |
60 | } |
61 | |
62 | bool QLowEnergyControllerPrivate::isValidLocalAdapter() |
63 | { |
64 | #if defined(QT_WINRT_BLUETOOTH) || defined(Q_OS_DARWIN) |
65 | return true; |
66 | #endif |
67 | if (localAdapter.isNull()) |
68 | return false; |
69 | |
70 | const QList<QBluetoothHostInfo> foundAdapters = QBluetoothLocalDevice::allDevices(); |
71 | bool adapterFound = false; |
72 | |
73 | for (const QBluetoothHostInfo &info : foundAdapters) { |
74 | if (info.address() == localAdapter) { |
75 | adapterFound = true; |
76 | break; |
77 | } |
78 | } |
79 | |
80 | return adapterFound; |
81 | } |
82 | |
83 | |
84 | void QLowEnergyControllerPrivate::setError( |
85 | QLowEnergyController::Error newError) |
86 | { |
87 | Q_Q(QLowEnergyController); |
88 | error = newError; |
89 | |
90 | switch (newError) { |
91 | case QLowEnergyController::UnknownRemoteDeviceError: |
92 | errorString = QLowEnergyController::tr(s: "Remote device cannot be found" ); |
93 | break; |
94 | case QLowEnergyController::InvalidBluetoothAdapterError: |
95 | errorString = QLowEnergyController::tr(s: "Cannot find local adapter" ); |
96 | break; |
97 | case QLowEnergyController::NetworkError: |
98 | errorString = QLowEnergyController::tr(s: "Error occurred during connection I/O" ); |
99 | break; |
100 | case QLowEnergyController::ConnectionError: |
101 | errorString = QLowEnergyController::tr(s: "Error occurred trying to connect to remote device." ); |
102 | break; |
103 | case QLowEnergyController::AdvertisingError: |
104 | errorString = QLowEnergyController::tr(s: "Error occurred trying to start advertising" ); |
105 | break; |
106 | case QLowEnergyController::RemoteHostClosedError: |
107 | errorString = QLowEnergyController::tr(s: "Remote device closed the connection" ); |
108 | break; |
109 | case QLowEnergyController::AuthorizationError: |
110 | errorString = QLowEnergyController::tr(s: "Failed to authorize on the remote device" ); |
111 | break; |
112 | case QLowEnergyController::NoError: |
113 | return; |
114 | default: |
115 | case QLowEnergyController::UnknownError: |
116 | errorString = QLowEnergyController::tr(s: "Unknown Error" ); |
117 | break; |
118 | } |
119 | |
120 | emit q->error(newError); |
121 | } |
122 | |
123 | void QLowEnergyControllerPrivate::setState( |
124 | QLowEnergyController::ControllerState newState) |
125 | { |
126 | qCDebug(QT_BT) << "QLowEnergyControllerPrivate setting state to" << newState; |
127 | Q_Q(QLowEnergyController); |
128 | if (state == newState) |
129 | return; |
130 | |
131 | state = newState; |
132 | if (state == QLowEnergyController::UnconnectedState |
133 | && role == QLowEnergyController::PeripheralRole) { |
134 | remoteDevice.clear(); |
135 | } |
136 | emit q->stateChanged(state); |
137 | } |
138 | |
139 | QSharedPointer<QLowEnergyServicePrivate> QLowEnergyControllerPrivate::serviceForHandle( |
140 | QLowEnergyHandle handle) |
141 | { |
142 | ServiceDataMap ¤tList = serviceList; |
143 | if (role == QLowEnergyController::PeripheralRole) |
144 | currentList = localServices; |
145 | |
146 | const QList<QSharedPointer<QLowEnergyServicePrivate>> values = currentList.values(); |
147 | for (auto service: values) |
148 | if (service->startHandle <= handle && handle <= service->endHandle) |
149 | return service; |
150 | |
151 | return QSharedPointer<QLowEnergyServicePrivate>(); |
152 | } |
153 | |
154 | /*! |
155 | Returns a valid characteristic if the given handle is the |
156 | handle of the characteristic itself or one of its descriptors |
157 | */ |
158 | QLowEnergyCharacteristic QLowEnergyControllerPrivate::characteristicForHandle( |
159 | QLowEnergyHandle handle) |
160 | { |
161 | QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(handle); |
162 | if (service.isNull()) |
163 | return QLowEnergyCharacteristic(); |
164 | |
165 | if (service->characteristicList.isEmpty()) |
166 | return QLowEnergyCharacteristic(); |
167 | |
168 | // check whether it is the handle of a characteristic header |
169 | if (service->characteristicList.contains(akey: handle)) |
170 | return QLowEnergyCharacteristic(service, handle); |
171 | |
172 | // check whether it is the handle of the characteristic value or its descriptors |
173 | QList<QLowEnergyHandle> charHandles = service->characteristicList.keys(); |
174 | std::sort(first: charHandles.begin(), last: charHandles.end()); |
175 | for (int i = charHandles.size() - 1; i >= 0; i--) { |
176 | if (charHandles.at(i) > handle) |
177 | continue; |
178 | |
179 | return QLowEnergyCharacteristic(service, charHandles.at(i)); |
180 | } |
181 | |
182 | return QLowEnergyCharacteristic(); |
183 | } |
184 | |
185 | /*! |
186 | Returns a valid descriptor if \a handle belongs to a descriptor; |
187 | otherwise an invalid one. |
188 | */ |
189 | QLowEnergyDescriptor QLowEnergyControllerPrivate::descriptorForHandle( |
190 | QLowEnergyHandle handle) |
191 | { |
192 | const QLowEnergyCharacteristic matchingChar = characteristicForHandle(handle); |
193 | if (!matchingChar.isValid()) |
194 | return QLowEnergyDescriptor(); |
195 | |
196 | const QLowEnergyServicePrivate::CharData charData = matchingChar. |
197 | d_ptr->characteristicList[matchingChar.attributeHandle()]; |
198 | |
199 | if (charData.descriptorList.contains(akey: handle)) |
200 | return QLowEnergyDescriptor(matchingChar.d_ptr, matchingChar.attributeHandle(), |
201 | handle); |
202 | |
203 | return QLowEnergyDescriptor(); |
204 | } |
205 | |
206 | /*! |
207 | Returns the length of the updated characteristic value. |
208 | */ |
209 | quint16 QLowEnergyControllerPrivate::updateValueOfCharacteristic( |
210 | QLowEnergyHandle charHandle,const QByteArray &value, bool appendValue) |
211 | { |
212 | QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(handle: charHandle); |
213 | if (!service.isNull()) { |
214 | CharacteristicDataMap::iterator charIt = service->characteristicList.find(akey: charHandle); |
215 | if (charIt != service->characteristicList.end()) { |
216 | QLowEnergyServicePrivate::CharData &charDetails = charIt.value(); |
217 | |
218 | if (appendValue) |
219 | charDetails.value += value; |
220 | else |
221 | charDetails.value = value; |
222 | |
223 | return charDetails.value.size(); |
224 | } |
225 | } |
226 | |
227 | return 0; |
228 | } |
229 | |
230 | /*! |
231 | Returns the length of the updated descriptor value. |
232 | */ |
233 | quint16 QLowEnergyControllerPrivate::updateValueOfDescriptor( |
234 | QLowEnergyHandle charHandle, QLowEnergyHandle descriptorHandle, |
235 | const QByteArray &value, bool appendValue) |
236 | { |
237 | QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(handle: charHandle); |
238 | if (!service.isNull()) { |
239 | CharacteristicDataMap::iterator charIt = service->characteristicList.find(akey: charHandle); |
240 | if (charIt != service->characteristicList.end()) { |
241 | QLowEnergyServicePrivate::CharData &charDetails = charIt.value(); |
242 | |
243 | DescriptorDataMap::iterator descIt = charDetails.descriptorList.find(akey: descriptorHandle); |
244 | if (descIt != charDetails.descriptorList.end()) { |
245 | QLowEnergyServicePrivate::DescData &descDetails = descIt.value(); |
246 | |
247 | if (appendValue) |
248 | descDetails.value += value; |
249 | else |
250 | descDetails.value = value; |
251 | |
252 | return descDetails.value.size(); |
253 | } |
254 | } |
255 | } |
256 | |
257 | return 0; |
258 | } |
259 | |
260 | void QLowEnergyControllerPrivate::invalidateServices() |
261 | { |
262 | for (QSharedPointer<QLowEnergyServicePrivate> service : serviceList.values()) |
263 | service->setController(nullptr); |
264 | |
265 | for (QSharedPointer<QLowEnergyServicePrivate> service : localServices.values()) |
266 | service->setController(nullptr); |
267 | |
268 | serviceList.clear(); |
269 | localServices.clear(); |
270 | lastLocalHandle = {}; |
271 | } |
272 | |
273 | QLowEnergyService *QLowEnergyControllerPrivate::addServiceHelper( |
274 | const QLowEnergyServiceData &service) |
275 | { |
276 | // Spec says services "should" be grouped by uuid length (16-bit first, then 128-bit). |
277 | // Since this is not mandatory, we ignore it here and let the caller take responsibility |
278 | // for it. |
279 | |
280 | const auto servicePrivate = QSharedPointer<QLowEnergyServicePrivate>::create(); |
281 | servicePrivate->setController(this); |
282 | servicePrivate->state = QLowEnergyService::LocalService; |
283 | servicePrivate->uuid = service.uuid(); |
284 | servicePrivate->type = service.type() == QLowEnergyServiceData::ServiceTypePrimary |
285 | ? QLowEnergyService::PrimaryService : QLowEnergyService::IncludedService; |
286 | const QList<QLowEnergyService *> includedServices = service.includedServices(); |
287 | for (QLowEnergyService * const includedService : includedServices) { |
288 | servicePrivate->includedServices << includedService->serviceUuid(); |
289 | includedService->d_ptr->type |= QLowEnergyService::IncludedService; |
290 | } |
291 | |
292 | // Spec v4.2, Vol 3, Part G, Section 3. |
293 | const QLowEnergyHandle oldLastHandle = this->lastLocalHandle; |
294 | servicePrivate->startHandle = ++this->lastLocalHandle; // Service declaration. |
295 | this->lastLocalHandle += servicePrivate->includedServices.count(); // Include declarations. |
296 | const QList<QLowEnergyCharacteristicData> characteristics = service.characteristics(); |
297 | for (const QLowEnergyCharacteristicData &cd : characteristics) { |
298 | const QLowEnergyHandle declHandle = ++this->lastLocalHandle; |
299 | QLowEnergyServicePrivate::CharData charData; |
300 | charData.valueHandle = ++this->lastLocalHandle; |
301 | charData.uuid = cd.uuid(); |
302 | charData.properties = cd.properties(); |
303 | charData.value = cd.value(); |
304 | const QList<QLowEnergyDescriptorData> descriptors = cd.descriptors(); |
305 | for (const QLowEnergyDescriptorData &dd : descriptors) { |
306 | QLowEnergyServicePrivate::DescData descData; |
307 | descData.uuid = dd.uuid(); |
308 | descData.value = dd.value(); |
309 | charData.descriptorList.insert(akey: ++this->lastLocalHandle, avalue: descData); |
310 | } |
311 | servicePrivate->characteristicList.insert(akey: declHandle, avalue: charData); |
312 | } |
313 | servicePrivate->endHandle = this->lastLocalHandle; |
314 | const bool handleOverflow = this->lastLocalHandle <= oldLastHandle; |
315 | if (handleOverflow) { |
316 | qCWarning(QT_BT) << "Not enough attribute handles left to create this service" ; |
317 | this->lastLocalHandle = oldLastHandle; |
318 | return nullptr; |
319 | } |
320 | |
321 | if (localServices.contains(akey: servicePrivate->uuid)) { |
322 | qWarning() << "Overriding existing local service with uuid" |
323 | << servicePrivate->uuid; |
324 | } |
325 | this->localServices.insert(akey: servicePrivate->uuid, avalue: servicePrivate); |
326 | |
327 | this->addToGenericAttributeList(service, servicePrivate->startHandle); |
328 | return new QLowEnergyService(servicePrivate); |
329 | } |
330 | |
331 | QT_END_NAMESPACE |
332 | |