Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qlowenergycontroller_bluez.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// Copyright (C) 2016 Javier S. Pedro <maemo@javispedro.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:critical reason:data-parser
5
11#include "bluez/bluez_data_p.h"
12#include "bluez/hcimanager_p.h"
13#include "bluez/objectmanager_p.h"
14#include "bluez/remotedevicemanager_p.h"
15#include "bluez/bluez5_helper_p.h"
16#include "bluez/bluetoothmanagement_p.h"
17
18#include <QtCore/QFileInfo>
19#include <QtCore/QLoggingCategory>
20#include <QtCore/QSettings>
21#include <QtCore/QTimer>
22#include <QtBluetooth/QBluetoothLocalDevice>
23#include <QtBluetooth/QBluetoothSocket>
24#include <QtBluetooth/QLowEnergyCharacteristicData>
25#include <QtBluetooth/QLowEnergyDescriptorData>
26#include <QtBluetooth/QLowEnergyService>
27#include <QtBluetooth/QLowEnergyServiceData>
28
29#include <algorithm>
30#include <climits>
31#include <cstring>
32#include <errno.h>
33#include <sys/types.h>
34#include <sys/socket.h>
35#include <unistd.h>
36
38constexpr quint16 ATT_MAX_LE_MTU = 0x200;
39
40#define GATT_PRIMARY_SERVICE quint16(0x2800)
41#define GATT_SECONDARY_SERVICE quint16(0x2801)
42#define GATT_INCLUDED_SERVICE quint16(0x2802)
43#define GATT_CHARACTERISTIC quint16(0x2803)
44
45//GATT command sizes in bytes
46#define ERROR_RESPONSE_HEADER_SIZE 5
47#define FIND_INFO_REQUEST_HEADER_SIZE 5
48#define GRP_TYPE_REQ_HEADER_SIZE 7
49#define READ_BY_TYPE_REQ_HEADER_SIZE 7
50#define READ_REQUEST_HEADER_SIZE 3
51#define READ_BLOB_REQUEST_HEADER_SIZE 5
52#define WRITE_REQUEST_HEADER_SIZE 3 // same size for WRITE_COMMAND header
53#define PREPARE_WRITE_HEADER_SIZE 5
54#define EXECUTE_WRITE_HEADER_SIZE 2
55#define MTU_EXCHANGE_HEADER_SIZE 3
56
57#define APPEND_VALUE true
58#define NEW_VALUE false
59
60QT_BEGIN_NAMESPACE
61
62Q_DECLARE_LOGGING_CATEGORY(QT_BT_BLUEZ)
63
64using namespace QBluetooth;
65using namespace QtBluetoothPrivate; // for D-Bus wrappers
66
67const int maxPrepareQueueSize = 1024;
68
69/* returns false if the format is incorrect */
70static bool dumpErrorInformation(const QByteArray &response)
71{
72 const char *data = response.constData();
73 if (response.size() != 5
74 || (static_cast<QBluezConst::AttCommand>(data[0])
76 qCWarning(QT_BT_BLUEZ) << QLatin1String("Not a valid error response");
77 return false;
78 }
79
80 QBluezConst::AttCommand lastCommand = static_cast<QBluezConst::AttCommand>(data[1]);
81 quint16 handle = bt_get_le16(&data[2]);
82 QBluezConst::AttError errorCode = static_cast<QBluezConst::AttError>(data[4]);
83
84 QString errorString;
85 switch (errorCode) {
87 errorString = QStringLiteral("invalid handle"); break;
89 errorString = QStringLiteral("not readable attribute - permissions"); break;
91 errorString = QStringLiteral("not writable attribute - permissions"); break;
93 errorString = QStringLiteral("PDU invalid"); break;
95 errorString = QStringLiteral("needs authentication - permissions"); break;
97 errorString = QStringLiteral("server does not support request"); break;
99 errorString = QStringLiteral("offset past end of attribute"); break;
101 errorString = QStringLiteral("need authorization - permissions"); break;
103 errorString = QStringLiteral("run out of prepare queue space"); break;
105 errorString = QStringLiteral("no attribute in given range found"); break;
107 errorString = QStringLiteral("attribute not read/written using read blob"); break;
109 errorString = QStringLiteral("need encryption key size - permissions"); break;
111 errorString = QStringLiteral("written value is invalid size"); break;
113 errorString = QStringLiteral("unlikely error"); break;
115 errorString = QStringLiteral("needs encryption - permissions"); break;
117 errorString = QStringLiteral("unsupported group type"); break;
119 errorString = QStringLiteral("insufficient resources to complete request"); break;
120 default:
123 errorString =
124 QStringLiteral("application error: %1").arg(static_cast<quint8>(errorCode));
125 else
126 errorString = QStringLiteral("unknown error code");
127 break;
128 }
129
130 qCDebug(QT_BT_BLUEZ) << "Error:" << errorCode << "Error description:" << errorString
131 << "last command:" << lastCommand << "handle:" << handle;
132
133 return true;
134}
135
136static int getUuidSize(const QBluetoothUuid &uuid)
137{
138 return uuid.minimumSize() == 2 ? 2 : 16;
139}
140
141template<typename T> static void putDataAndIncrement(const T &src, char *&dst)
142{
143 putBtData(src, dst);
144 dst += sizeof(T);
145}
146template<> void putDataAndIncrement(const QBluetoothUuid &uuid, char *&dst)
147{
148 bool ok;
149 quint16 uuid16 = uuid.toUInt16(&ok);
150 if (ok) {
151 putBtData(uuid16, dst);
152 dst += sizeof(uuid16);
153 } else {
154 QUuid::Id128Bytes btOrder = uuid.toBytes(QSysInfo::LittleEndian);
155 memcpy(dst, btOrder.data, sizeof(btOrder));
156 dst += sizeof(btOrder);
157 }
158}
159template<> void putDataAndIncrement(const QByteArray &value, char *&dst)
160{
161 using namespace std;
162 memcpy(dst, value.constData(), value.size());
163 dst += value.size();
164}
165
166/*!
167 \class QLowEnergyControllerPrivateBluez
168 \inmodule QtBluetooth
169 \internal
170*/
171QLowEnergyControllerPrivateBluez::QLowEnergyControllerPrivateBluez()
173 requestPending(false),
174 mtuSize(ATT_DEFAULT_LE_MTU),
175 securityLevelValue(-1),
176 encryptionChangePending(false)
177{
179 qRegisterMetaType<QList<QLowEnergyHandle> >();
180}
181
182void QLowEnergyControllerPrivateBluez::init()
183{
184 // The HCI manager is shared between this class and the advertiser
185 hciManager = std::make_shared<HciManager>(localAdapter);
186
187 if (!hciManager->isValid()){
188 setError(QLowEnergyController::InvalidBluetoothAdapterError);
189 return;
190 }
191
192 hciManager->monitorEvent(HciManager::HciEvent::EVT_ENCRYPT_CHANGE);
193 connect(hciManager.get(), SIGNAL(encryptionChangedEvent(QBluetoothAddress,bool)),
194 this, SLOT(encryptionChangedEvent(QBluetoothAddress,bool)));
195 hciManager->monitorEvent(HciManager::HciEvent::EVT_LE_META_EVENT);
196 hciManager->monitorAclPackets();
197 connect(hciManager.get(), &HciManager::connectionComplete, this, [this](quint16 handle) {
198 connectionHandle = handle;
199 qCDebug(QT_BT_BLUEZ) << "received connection complete event, handle:" << handle;
200 });
201 connect(hciManager.get(), &HciManager::connectionUpdate, this,
202 [this](quint16 handle, const QLowEnergyConnectionParameters &params) {
203 if (handle == connectionHandle)
204 emit q_ptr->connectionUpdated(params);
205 }
206 );
207 connect(hciManager.get(), &HciManager::signatureResolvingKeyReceived, this,
208 [this](quint16 handle, bool remoteKey, const QUuid::Id128Bytes &csrk) {
209 if (handle != connectionHandle)
210 return;
211 if ((remoteKey && role == QLowEnergyController::CentralRole)
212 || (!remoteKey && role == QLowEnergyController::PeripheralRole)) {
213 return;
214 }
215 qCDebug(QT_BT_BLUEZ) << "received new signature resolving key";
216 signingData.insert(remoteDevice.toUInt64(), SigningData(csrk));
217 }
218 );
219
220 if (role == QLowEnergyController::CentralRole) {
221 if (Q_UNLIKELY(!qEnvironmentVariableIsEmpty("BLUETOOTH_GATT_TIMEOUT"))) {
222 bool ok = false;
223 int value = qEnvironmentVariableIntValue("BLUETOOTH_GATT_TIMEOUT", &ok);
224 if (ok)
225 gattRequestTimeout = value;
226 }
227
228 // permit disabling of timeout behavior via environment variable
229 if (gattRequestTimeout > 0) {
230 qCWarning(QT_BT_BLUEZ) << "Enabling GATT request timeout behavior" << gattRequestTimeout;
231 requestTimer = new QTimer(this);
232 requestTimer->setSingleShot(true);
233 requestTimer->setInterval(gattRequestTimeout);
234 connect(requestTimer, &QTimer::timeout,
235 this, &QLowEnergyControllerPrivateBluez::handleGattRequestTimeout);
236 }
237 }
238}
239
240void QLowEnergyControllerPrivateBluez::handleGattRequestTimeout()
241{
242 // antyhing open that might require cancellation or a warning?
243 if (encryptionChangePending) {
244 // We cannot really recover for now but the warning is essential for debugging
245 qCWarning(QT_BT_BLUEZ) << "****** Encryption change event blocking further GATT requests";
246 return;
247 }
248
249 if (!openRequests.isEmpty() && requestPending) {
250 const Request currentRequest = openRequests.dequeue();
251 requestPending = false; // reset pending flag
252
253 qCWarning(QT_BT_BLUEZ).nospace() << "****** Request type 0x" << currentRequest.command
254 << " to server/peripheral timed out";
255 qCWarning(QT_BT_BLUEZ) << "****** Looks like the characteristic or descriptor does NOT act in"
256 << "accordance to Bluetooth 4.x spec.";
257 qCWarning(QT_BT_BLUEZ) << "****** Please check server implementation."
258 << "Continuing under reservation.";
259
260 QBluezConst::AttCommand command = currentRequest.command;
261 const auto createRequestErrorMessage = [](QBluezConst::AttCommand opcodeWithError,
262 QLowEnergyHandle handle) {
263 QByteArray errorPackage(ERROR_RESPONSE_HEADER_SIZE, Qt::Uninitialized);
264 errorPackage[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_ERROR_RESPONSE);
265 errorPackage[1] = static_cast<quint8>(
266 opcodeWithError); // e.g. QBluezConst::AttCommand::ATT_OP_READ_REQUEST
267 putBtData(handle, errorPackage.data() + 2); //
268 errorPackage[4] = static_cast<quint8>(QBluezConst::AttError::ATT_ERROR_REQUEST_STALLED);
269
270 return errorPackage;
271 };
272
273 switch (command) {
275 // never received reply to MTU request
276 // it is safe to skip and go to next request
277 break;
278 case QBluezConst::AttCommand::ATT_OP_READ_BY_GROUP_REQUEST: // primary or secondary service
279 // discovery
280 case QBluezConst::AttCommand::ATT_OP_READ_BY_TYPE_REQUEST: // characteristic or included
281 // service discovery
282 // jump back into usual response handling with custom error code
283 // 2nd param "0" as required by spec
284 processReply(currentRequest, createRequestErrorMessage(command, 0));
285 break;
286 case QBluezConst::AttCommand::ATT_OP_READ_REQUEST: // read descriptor or characteristic
287 // value
288 case QBluezConst::AttCommand::ATT_OP_READ_BLOB_REQUEST: // read long descriptor or
289 // characteristic
290 case QBluezConst::AttCommand::ATT_OP_WRITE_REQUEST: // write descriptor or characteristic
291 {
292 uint handleData = currentRequest.reference.toUInt();
293 const QLowEnergyHandle charHandle = (handleData & 0xffff);
294 const QLowEnergyHandle descriptorHandle = ((handleData >> 16) & 0xffff);
295 processReply(currentRequest, createRequestErrorMessage(command,
296 descriptorHandle ? descriptorHandle : charHandle));
297 } break;
298 case QBluezConst::AttCommand::ATT_OP_FIND_INFORMATION_REQUEST: // get descriptor information
299 processReply(currentRequest, createRequestErrorMessage(
300 command, currentRequest.reference2.toUInt()));
301 break;
302 case QBluezConst::AttCommand::ATT_OP_PREPARE_WRITE_REQUEST: // prepare to write long desc or
303 // char
304 case QBluezConst::AttCommand::ATT_OP_EXECUTE_WRITE_REQUEST: // execute long write of desc or
305 // char
306 {
307 uint handleData = currentRequest.reference.toUInt();
308 const QLowEnergyHandle attrHandle = (handleData & 0xffff);
309 processReply(currentRequest,
310 createRequestErrorMessage(command, attrHandle));
311 } break;
312 default:
313 // not a command used by central role implementation
314 qCWarning(QT_BT_BLUEZ) << "Missing response for ATT peripheral command: "
315 << Qt::hex << command;
316 break;
317 }
318
319 // spin openRequest queue further
320 sendNextPendingRequest();
321 }
322}
323
324QLowEnergyControllerPrivateBluez::~QLowEnergyControllerPrivateBluez()
325{
326 closeServerSocket();
327 delete cmacCalculator;
328 cmacCalculator = nullptr;
329}
330
332{
333public:
334 bool listen(const QBluetoothAddress &localAdapter)
335 {
336 m_socket = ::socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
337 if (m_socket == -1) {
338 qCWarning(QT_BT_BLUEZ) << "socket creation failed:" << qt_error_string(errno);
339 return false;
340 }
341 sockaddr_l2 addr;
342
343 // memset should be in std namespace for C++ compilers, but we also need to support
344 // broken ones that put it in the global one.
345 using namespace std;
346 memset(&addr, 0, sizeof addr);
347
348 addr.l2_family = AF_BLUETOOTH;
350 addr.l2_bdaddr_type = BDADDR_LE_PUBLIC;
351 convertAddress(localAdapter.toUInt64(), addr.l2_bdaddr.b);
352 if (::bind(m_socket, reinterpret_cast<sockaddr *>(&addr), sizeof addr) == -1) {
353 qCWarning(QT_BT_BLUEZ) << "bind() failed:" << qt_error_string(errno);
354 return false;
355 }
356 if (::listen(m_socket, 1)) {
357 qCWarning(QT_BT_BLUEZ) << "listen() failed:" << qt_error_string(errno);
358 return false;
359 }
360 return true;
361 }
362
364 {
365 if (m_socket != -1)
366 close(m_socket);
367 }
368
370 {
371 const int socket = m_socket;
372 m_socket = -1;
373 return socket;
374 }
375
376private:
377 int m_socket = -1;
378};
379
380
381void QLowEnergyControllerPrivateBluez::startAdvertising(const QLowEnergyAdvertisingParameters &params,
382 const QLowEnergyAdvertisingData &advertisingData,
383 const QLowEnergyAdvertisingData &scanResponseData)
384{
385 qCDebug(QT_BT_BLUEZ) << "Starting to advertise";
386 if (!advertiser) {
387 advertiser = new QLeAdvertiserBluez(params, advertisingData, scanResponseData, hciManager,
388 this);
389 connect(advertiser, &QLeAdvertiser::errorOccurred, this,
390 &QLowEnergyControllerPrivateBluez::handleAdvertisingError);
391 }
392 setState(QLowEnergyController::AdvertisingState);
393 advertiser->startAdvertising();
394 if (params.mode() == QLowEnergyAdvertisingParameters::AdvNonConnInd
395 || params.mode() == QLowEnergyAdvertisingParameters::AdvScanInd) {
396 qCDebug(QT_BT_BLUEZ) << "Non-connectable advertising requested, "
397 "not listening for connections.";
398 return;
399 }
400
401 ServerSocket serverSocket;
402 if (!serverSocket.listen(localAdapter)) {
403 setError(QLowEnergyController::AdvertisingError);
404 setState(QLowEnergyController::UnconnectedState);
405 return;
406 }
407
408 const int socketFd = serverSocket.takeSocket();
409 serverSocketNotifier = new QSocketNotifier(socketFd, QSocketNotifier::Read, this);
410 connect(serverSocketNotifier, &QSocketNotifier::activated, this,
411 &QLowEnergyControllerPrivateBluez::handleConnectionRequest);
412}
413
414void QLowEnergyControllerPrivateBluez::stopAdvertising()
415{
416 setState(QLowEnergyController::UnconnectedState);
417 advertiser->stopAdvertising();
418}
419
420void QLowEnergyControllerPrivateBluez::requestConnectionUpdate(const QLowEnergyConnectionParameters &params)
421{
422 // The spec says that the connection update command can be used by both slave and master
423 // devices, but BlueZ allows it only for master devices. So for slave devices, we have to use a
424 // connection parameter update request, which we need to wrap in an ACL command, as BlueZ
425 // does not allow user-space sockets for the signaling channel.
426 if (role == QLowEnergyController::CentralRole)
427 hciManager->sendConnectionUpdateCommand(connectionHandle, params);
428 else
429 hciManager->sendConnectionParameterUpdateRequest(connectionHandle, params);
430}
431
432void QLowEnergyControllerPrivateBluez::connectToDevice()
433{
434 if (remoteDevice.isNull()) {
435 qCWarning(QT_BT_BLUEZ) << "Invalid/null remote device address";
436 setError(QLowEnergyController::UnknownRemoteDeviceError);
437 return;
438 }
439
440 setState(QLowEnergyController::ConnectingState);
441 if (l2cpSocket) {
442 delete l2cpSocket;
443 l2cpSocket = nullptr;
444 }
445
446 createServicesForCentralIfRequired();
447
448 // check for active running connections
449 // BlueZ 5.37+ (maybe even earlier versions) can have pending BTLE connections
450 // Only one active L2CP socket to CID 0x4 possible at a time
451
452 const QList<quint16> activeHandles = hciManager->activeLowEnergyConnections();
453 if (!activeHandles.isEmpty()) {
454 qCWarning(QT_BT_BLUEZ) << "Cannot connect due to pending active LE connections";
455
456 if (!device1Manager) {
457 device1Manager = new RemoteDeviceManager(localAdapter, this);
458 connect(device1Manager, &RemoteDeviceManager::finished,
459 this, &QLowEnergyControllerPrivateBluez::activeConnectionTerminationDone);
460 }
461
462 QList<QBluetoothAddress> connectedAddresses;
463 for (const auto handle: activeHandles) {
464 const QBluetoothAddress addr = hciManager->addressForConnectionHandle(handle);
465 if (!addr.isNull())
466 connectedAddresses.push_back(addr);
467 }
468 device1Manager->scheduleJob(RemoteDeviceManager::JobType::JobDisconnectDevice, connectedAddresses);
469 } else {
470 establishL2cpClientSocket();
471 }
472}
473
474/*!
475 * Handles outcome of attempts to close external connections.
476 */
477void QLowEnergyControllerPrivateBluez::activeConnectionTerminationDone()
478{
479 if (!device1Manager)
480 return;
481
482 qCDebug(QT_BT_BLUEZ) << "RemoteDeviceManager finished attempting"
483 << "to close external connections";
484
485 QList<quint16> activeHandles = hciManager->activeLowEnergyConnections();
486 if (!activeHandles.isEmpty()) {
487 qCWarning(QT_BT_BLUEZ) << "Cannot close pending external BTLE connections. Aborting connect attempt";
488 setError(QLowEnergyController::ConnectionError);
489 setState(QLowEnergyController::UnconnectedState);
490 l2cpDisconnected();
491 return;
492 } else {
493 establishL2cpClientSocket();
494 }
495}
496
497/*!
498 * Establishes the L2CP client socket.
499 */
500void QLowEnergyControllerPrivateBluez::establishL2cpClientSocket()
501{
502 //we are already in Connecting state
503
504 l2cpSocket = new QBluetoothSocket(QBluetoothServiceInfo::L2capProtocol, this);
505 connect(l2cpSocket, SIGNAL(connected()), this, SLOT(l2cpConnected()));
506 connect(l2cpSocket, SIGNAL(disconnected()), this, SLOT(l2cpDisconnected()));
507 connect(l2cpSocket, SIGNAL(errorOccurred(QBluetoothSocket::SocketError)), this,
508 SLOT(l2cpErrorChanged(QBluetoothSocket::SocketError)));
509 connect(l2cpSocket, SIGNAL(readyRead()), this, SLOT(l2cpReadyRead()));
510
511 quint32 addressTypeToUse = (addressType == QLowEnergyController::PublicAddress)
514 // if monitoring is possible and it's private then we force it to the relevant option
515 if (BluetoothManagement::instance()->isAddressRandom(remoteDevice)) {
516 addressTypeToUse = BDADDR_LE_RANDOM;
517 }
518 }
519
520 qCDebug(QT_BT_BLUEZ) << "addresstypeToUse:"
521 << (addressTypeToUse == BDADDR_LE_RANDOM
522 ? QStringLiteral("Random") : QStringLiteral("Public"));
523
524 l2cpSocket->d_ptr->lowEnergySocketType = addressTypeToUse;
525
526 int sockfd = l2cpSocket->socketDescriptor();
527 if (sockfd < 0) {
528 qCWarning(QT_BT_BLUEZ) << "l2cp socket not initialised";
529 setError(QLowEnergyController::ConnectionError);
530 setState(QLowEnergyController::UnconnectedState);
531 return;
532 }
533
534 struct sockaddr_l2 addr;
535 memset(&addr, 0, sizeof(addr));
536 addr.l2_family = AF_BLUETOOTH;
538 addr.l2_bdaddr_type = BDADDR_LE_PUBLIC;
539 convertAddress(localAdapter.toUInt64(), addr.l2_bdaddr.b);
540
541 // bind the socket to the local device
542 if (::bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
543 qCWarning(QT_BT_BLUEZ) << qt_error_string(errno);
544 setError(QLowEnergyController::ConnectionError);
545 setState(QLowEnergyController::UnconnectedState);
546 return;
547 }
548
549 // connect
550 // Unbuffered mode required to separate each GATT packet
551 l2cpSocket->connectToService(remoteDevice, ATTRIBUTE_CHANNEL_ID,
552 QIODevice::ReadWrite | QIODevice::Unbuffered);
553 loadSigningDataIfNecessary(LocalSigningKey);
554}
555
556void QLowEnergyControllerPrivateBluez::createServicesForCentralIfRequired()
557{
558 bool ok = false;
559 int value = qEnvironmentVariableIntValue("QT_DEFAULT_CENTRAL_SERVICES", &ok);
560 if (Q_UNLIKELY(ok && value == 0))
561 return; //nothing to do
562
563 //do not add the services each time we start a connection
564 if (localServices.contains(QBluetoothUuid(QBluetoothUuid::ServiceClassUuid::GenericAccess)))
565 return;
566
567 qCDebug(QT_BT_BLUEZ) << "Creating default GAP/GATT services";
568
569 //populate Generic Access service
570 //for now the values are static
571 QLowEnergyServiceData gapServiceData;
572 gapServiceData.setType(QLowEnergyServiceData::ServiceTypePrimary);
573 gapServiceData.setUuid(QBluetoothUuid::ServiceClassUuid::GenericAccess);
574
575 QLowEnergyCharacteristicData gapDeviceName;
576 gapDeviceName.setUuid(QBluetoothUuid::CharacteristicType::DeviceName);
577 gapDeviceName.setProperties(QLowEnergyCharacteristic::Read);
578
579 QBluetoothLocalDevice mainAdapter;
580 gapDeviceName.setValue(mainAdapter.name().toLatin1()); //static name
581
582 QLowEnergyCharacteristicData gapAppearance;
583 gapAppearance.setUuid(QBluetoothUuid::CharacteristicType::Appearance);
584 gapAppearance.setProperties(QLowEnergyCharacteristic::Read);
585 gapAppearance.setValue(QByteArray::fromHex("80")); // Generic Computer (0x80)
586
587 QLowEnergyCharacteristicData gapPrivacyFlag;
588 gapPrivacyFlag.setUuid(QBluetoothUuid::CharacteristicType::PeripheralPrivacyFlag);
589 gapPrivacyFlag.setProperties(QLowEnergyCharacteristic::Read);
590 gapPrivacyFlag.setValue(QByteArray::fromHex("00")); // disable privacy
591
592 gapServiceData.addCharacteristic(gapDeviceName);
593 gapServiceData.addCharacteristic(gapAppearance);
594 gapServiceData.addCharacteristic(gapPrivacyFlag);
595
596 Q_Q(QLowEnergyController);
597 QLowEnergyService *service = addServiceHelper(gapServiceData);
598 if (service)
599 service->setParent(q);
600
601 QLowEnergyServiceData gattServiceData;
602 gattServiceData.setType(QLowEnergyServiceData::ServiceTypePrimary);
603 gattServiceData.setUuid(QBluetoothUuid::ServiceClassUuid::GenericAttribute);
604
605 QLowEnergyCharacteristicData serviceChangedChar;
606 serviceChangedChar.setUuid(QBluetoothUuid::CharacteristicType::ServiceChanged);
607 serviceChangedChar.setProperties(QLowEnergyCharacteristic::Indicate);
608 //arbitrary range of 2 bit handle range (1-4
609 serviceChangedChar.setValue(QByteArray::fromHex("0104"));
610
611 const QLowEnergyDescriptorData clientConfig(
612 QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration,
613 QByteArray(2, 0));
614 serviceChangedChar.addDescriptor(clientConfig);
615 gattServiceData.addCharacteristic(serviceChangedChar);
616
617 service = addServiceHelper(gattServiceData);
618 if (service)
619 service->setParent(q);
620}
621
622void QLowEnergyControllerPrivateBluez::l2cpConnected()
623{
624 Q_Q(QLowEnergyController);
625
626 securityLevelValue = securityLevel();
627 exchangeMTU();
628
629 setState(QLowEnergyController::ConnectedState);
630 emit q->connected();
631}
632
633void QLowEnergyControllerPrivateBluez::disconnectFromDevice()
634{
635 setState(QLowEnergyController::ClosingState);
636 if (l2cpSocket)
637 l2cpSocket->close();
638 resetController();
639
640 // this may happen when RemoteDeviceManager::JobType::JobDisconnectDevice
641 // is pending.
642 if (!l2cpSocket) {
643 qWarning(QT_BT_BLUEZ) << "Unexpected closure of device. Cleaning up internal states.";
644 l2cpDisconnected();
645 }
646}
647
648void QLowEnergyControllerPrivateBluez::l2cpDisconnected()
649{
650 Q_Q(QLowEnergyController);
651
652 if (role == QLowEnergyController::PeripheralRole) {
653 storeClientConfigurations();
654 remoteDevice.clear();
655 remoteName.clear();
656 }
658 resetController();
659 setState(QLowEnergyController::UnconnectedState);
660 emit q->disconnected();
661}
662
663void QLowEnergyControllerPrivateBluez::l2cpErrorChanged(QBluetoothSocket::SocketError e)
664{
665 switch (e) {
666 case QBluetoothSocket::SocketError::HostNotFoundError:
667 setError(QLowEnergyController::UnknownRemoteDeviceError);
668 qCDebug(QT_BT_BLUEZ) << "The passed remote device address cannot be found";
669 break;
670 case QBluetoothSocket::SocketError::NetworkError:
671 setError(QLowEnergyController::NetworkError);
672 qCDebug(QT_BT_BLUEZ) << "Network IO error while talking to LE device";
673 break;
674 case QBluetoothSocket::SocketError::RemoteHostClosedError:
675 setError(QLowEnergyController::RemoteHostClosedError);
676 qCDebug(QT_BT_BLUEZ) << "Remote host closed the connection";
677 break;
678 case QBluetoothSocket::SocketError::UnknownSocketError:
679 case QBluetoothSocket::SocketError::UnsupportedProtocolError:
680 case QBluetoothSocket::SocketError::OperationError:
681 case QBluetoothSocket::SocketError::ServiceNotFoundError:
682 default:
683 // these errors shouldn't happen -> as it means
684 // the code in this file has bugs
685 qCDebug(QT_BT_BLUEZ) << "Unknown l2cp socket error: " << e << l2cpSocket->errorString();
686 setError(QLowEnergyController::UnknownError);
687 break;
688 }
689
691 resetController();
692 setState(QLowEnergyController::UnconnectedState);
693}
694
695
696void QLowEnergyControllerPrivateBluez::resetController()
697{
698 openRequests.clear();
699 openPrepareWriteRequests.clear();
700 scheduledIndications.clear();
701 indicationInFlight = false;
702 requestPending = false;
703 encryptionChangePending = false;
704 receivedMtuExchangeRequest = false;
705 mtuSize = ATT_DEFAULT_LE_MTU;
706 securityLevelValue = -1;
707 connectionHandle = 0;
708
709 if (role == QLowEnergyController::PeripheralRole) {
710 // public API behavior requires stop of advertisement
711 if (advertiser) {
712 advertiser->stopAdvertising();
713 delete advertiser;
714 advertiser = nullptr;
715 }
716 localAttributes.clear();
717 }
718}
719
720void QLowEnergyControllerPrivateBluez::restartRequestTimer()
721{
722 if (!requestTimer)
723 return;
724
725 if (gattRequestTimeout > 0)
726 requestTimer->start(gattRequestTimeout);
727}
728
729void QLowEnergyControllerPrivateBluez::l2cpReadyRead()
730{
731 const QByteArray incomingPacket = l2cpSocket->readAll();
732 qCDebug(QT_BT_BLUEZ) << "Received size:" << incomingPacket.size() << "data:"
733 << incomingPacket.toHex();
734 if (incomingPacket.isEmpty())
735 return;
736
737 const QBluezConst::AttCommand command =
738 static_cast<QBluezConst::AttCommand>(incomingPacket.constData()[0]);
739 switch (command) {
741 processUnsolicitedReply(incomingPacket);
742 return;
743 }
745 //send confirmation
746 QByteArray packet;
747 packet.append(static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_HANDLE_VAL_CONFIRMATION));
748 sendPacket(packet);
749
750 processUnsolicitedReply(incomingPacket);
751 return;
752 }
753 //--------------------------------------------------
754 // Peripheral side packet handling
755 case QBluezConst::AttCommand::ATT_OP_EXCHANGE_MTU_REQUEST:
756 handleExchangeMtuRequest(incomingPacket);
757 return;
758 case QBluezConst::AttCommand::ATT_OP_FIND_INFORMATION_REQUEST:
759 handleFindInformationRequest(incomingPacket);
760 return;
761 case QBluezConst::AttCommand::ATT_OP_FIND_BY_TYPE_VALUE_REQUEST:
762 handleFindByTypeValueRequest(incomingPacket);
763 return;
764 case QBluezConst::AttCommand::ATT_OP_READ_BY_TYPE_REQUEST:
765 handleReadByTypeRequest(incomingPacket);
766 return;
767 case QBluezConst::AttCommand::ATT_OP_READ_REQUEST:
768 handleReadRequest(incomingPacket);
769 return;
770 case QBluezConst::AttCommand::ATT_OP_READ_BLOB_REQUEST:
771 handleReadBlobRequest(incomingPacket);
772 return;
773 case QBluezConst::AttCommand::ATT_OP_READ_MULTIPLE_REQUEST:
774 handleReadMultipleRequest(incomingPacket);
775 return;
776 case QBluezConst::AttCommand::ATT_OP_READ_BY_GROUP_REQUEST:
777 handleReadByGroupTypeRequest(incomingPacket);
778 return;
779 case QBluezConst::AttCommand::ATT_OP_WRITE_REQUEST:
780 case QBluezConst::AttCommand::ATT_OP_WRITE_COMMAND:
781 case QBluezConst::AttCommand::ATT_OP_SIGNED_WRITE_COMMAND:
782 handleWriteRequestOrCommand(incomingPacket);
783 return;
784 case QBluezConst::AttCommand::ATT_OP_PREPARE_WRITE_REQUEST:
785 handlePrepareWriteRequest(incomingPacket);
786 return;
787 case QBluezConst::AttCommand::ATT_OP_EXECUTE_WRITE_REQUEST:
788 handleExecuteWriteRequest(incomingPacket);
789 return;
791 if (indicationInFlight) {
792 indicationInFlight = false;
793 sendNextIndication();
794 } else {
795 qCWarning(QT_BT_BLUEZ) << "received unexpected handle value confirmation";
796 }
797 return;
798 //--------------------------------------------------
799 default:
800 //only solicited replies finish pending requests
801 requestPending = false;
802 break;
803 }
804
805 if (openRequests.isEmpty()) {
806 qCWarning(QT_BT_BLUEZ) << "Received unexpected packet from peer, disconnecting.";
808 return;
809 }
810
811 const Request request = openRequests.dequeue();
812 processReply(request, incomingPacket);
813
814 sendNextPendingRequest();
815}
816
817/*!
818 * Called when the request for socket encryption has been
819 * processed by the kernel. Such requests take time as the kernel
820 * has to renegotiate the link parameters with the remote device.
821 *
822 * Therefore any such request delays the pending ATT commands until this
823 * callback is called. The first pending request in the queue is the request
824 * that triggered the encryption request.
825 */
826void QLowEnergyControllerPrivateBluez::encryptionChangedEvent(
827 const QBluetoothAddress &address, bool wasSuccess)
828{
829 if (!encryptionChangePending) // somebody else caused change event
830 return;
831
832 if (remoteDevice != address)
833 return;
834
835 securityLevelValue = securityLevel();
836
837 // On success continue to process ATT command queue
838 if (!wasSuccess) {
839 // We could not increase the security of the link
840 // The next request was requeued due to security error
841 // skip it to avoid endless loop of security negotiations
842 Q_ASSERT(!openRequests.isEmpty());
843 Request failedRequest = openRequests.takeFirst();
844
845 if (failedRequest.command == QBluezConst::AttCommand::ATT_OP_WRITE_REQUEST) {
846 // Failing write requests trigger some sort of response
847 uint ref = failedRequest.reference.toUInt();
848 const QLowEnergyHandle charHandle = (ref & 0xffff);
849 const QLowEnergyHandle descriptorHandle = ((ref >> 16) & 0xffff);
850
851 QSharedPointer<QLowEnergyServicePrivate> service
852 = serviceForHandle(charHandle);
853 if (!service.isNull() && service->characteristicList.contains(charHandle)) {
854 if (!descriptorHandle)
855 service->setError(QLowEnergyService::CharacteristicWriteError);
856 else
857 service->setError(QLowEnergyService::DescriptorWriteError);
858 }
859 } else if (failedRequest.command == QBluezConst::AttCommand::ATT_OP_PREPARE_WRITE_REQUEST) {
860 uint handleData = failedRequest.reference.toUInt();
861 const QLowEnergyHandle attrHandle = (handleData & 0xffff);
862 const QByteArray newValue = failedRequest.reference2.toByteArray();
863
864 // Prepare command failed, cancel pending prepare queue on
865 // the device. The appropriate (Descriptor|Characteristic)WriteError
866 // is emitted too once the execute write request comes through
867 sendExecuteWriteRequest(attrHandle, newValue, true);
868 }
869 }
870
871 encryptionChangePending = false;
872 sendNextPendingRequest();
873}
874
875void QLowEnergyControllerPrivateBluez::sendPacket(const QByteArray &packet)
876{
877 qint64 result = l2cpSocket->write(packet.constData(),
878 packet.size());
879 // We ignore result == 0 which is likely to be caused by EAGAIN.
880 // This packet is effectively discarded but the controller can still recover
881
882 if (result == -1) {
883 qCDebug(QT_BT_BLUEZ) << "Cannot write L2CP packet:" << Qt::hex
884 << packet.toHex()
885 << l2cpSocket->errorString();
886 setError(QLowEnergyController::NetworkError);
887 } else if (result < packet.size()) {
888 qCWarning(QT_BT_BLUEZ) << "L2CP write request incomplete:"
889 << result << "of" << packet.size();
890 }
891
892}
893
894void QLowEnergyControllerPrivateBluez::sendNextPendingRequest()
895{
896 if (openRequests.isEmpty() || requestPending || encryptionChangePending)
897 return;
898
899 const Request &request = openRequests.head();
900// qCDebug(QT_BT_BLUEZ) << "Sending request, type:" << Qt::hex << request.command
901// << request.payload.toHex();
902
903 requestPending = true;
904 restartRequestTimer();
905 sendPacket(request.payload);
906}
907
910 const char *data, quint16 elementLength)
911{
912 Q_ASSERT(charData);
913 Q_ASSERT(data);
914 Q_ASSERT(elementLength >= 5);
915
916 QLowEnergyHandle attributeHandle = bt_get_le16(&data[0]);
917 charData->properties =
918 (QLowEnergyCharacteristic::PropertyTypes)(data[2] & 0xff);
919 charData->valueHandle = bt_get_le16(&data[3]);
920
921 // Bluetooth LE data comes as little endian
922 if (elementLength == 7) // 16 bit uuid
923 charData->uuid = QBluetoothUuid(bt_get_le16(&data[5]));
924 else if (elementLength == 21) // 128 bit uuid
925 charData->uuid = QUuid::fromBytes(&data[5], QSysInfo::LittleEndian);
926
927 qCDebug(QT_BT_BLUEZ) << "Found handle:" << Qt::hex << attributeHandle
928 << "properties:" << charData->properties
929 << "value handle:" << charData->valueHandle
930 << "uuid:" << charData->uuid.toString();
931
932 return attributeHandle;
933}
934
936 QList<QBluetoothUuid> *foundServices,
937 const char *data, quint16 elementLength)
938{
939 Q_ASSERT(foundServices);
940 Q_ASSERT(data);
941 Q_ASSERT(elementLength >= 6);
942
943 QLowEnergyHandle attributeHandle = bt_get_le16(&data[0]);
944
945 // the next 2 elements are not required as we have discovered
946 // all (primary/secondary) services already. Now we are only
947 // interested in their relationship to each other
948 // data[2] -> included service start handle
949 // data[4] -> included service end handle
950
951 // TODO: Spec v. 5.3, Vol. 3, Part G, 4.5.1 mentions that only
952 // 16-bit UUID can be returned here. If the UUID is 128-bit,
953 // then it is omitted from the response, and should be requested
954 // separately with the ATT_READ_REQ command.
955
956 if (elementLength == 8) //16 bit uuid
957 foundServices->append(QBluetoothUuid(bt_get_le16(&data[6])));
958 else if (elementLength == 22) // 128 bit uuid
959 foundServices->append(QUuid::fromBytes(&data[6], QSysInfo::LittleEndian));
960
961 qCDebug(QT_BT_BLUEZ) << "Found included service: " << Qt::hex
962 << attributeHandle << "uuid:" << *foundServices;
963
964 return attributeHandle;
965}
966
968static void reportMalformedData(QBluezConst::AttCommand cmd, const QByteArray &response)
969{
970 qCDebug(QT_BT_BLUEZ, "%s malformed data: %s", qt_getEnumName(cmd),
971 response.toHex().constData());
972}
973
974void QLowEnergyControllerPrivateBluez::processReply(
975 const Request &request, const QByteArray &response)
976{
977 Q_Q(QLowEnergyController);
978
979 // We already have an isEmpty() check at the only calling site that reads
980 // incoming data, so Q_ASSERT is enough.
981 Q_ASSERT(!response.isEmpty());
982
983 QBluezConst::AttCommand command = static_cast<QBluezConst::AttCommand>(response.constData()[0]);
984
985 bool isErrorResponse = false;
986 // if error occurred 2. byte is previous request type
988 if (!dumpErrorInformation(response))
989 return;
990 command = static_cast<QBluezConst::AttCommand>(response.constData()[1]);
991 isErrorResponse = true;
992 }
993
994 switch (command) {
997 Q_ASSERT(request.command == QBluezConst::AttCommand::ATT_OP_EXCHANGE_MTU_REQUEST);
998 quint16 oldMtuSize = mtuSize;
999 if (isErrorResponse) {
1000 mtuSize = ATT_DEFAULT_LE_MTU;
1001 } else {
1002 if (response.size() < 3) {
1003 reportMalformedData(command, response);
1004 break;
1005 }
1006 const char *data = response.constData();
1007 quint16 mtu = bt_get_le16(&data[1]);
1008 mtuSize = mtu;
1009 if (mtuSize < ATT_DEFAULT_LE_MTU)
1010 mtuSize = ATT_DEFAULT_LE_MTU;
1011
1012 qCDebug(QT_BT_BLUEZ) << "Server MTU:" << mtu << "resulting mtu:" << mtuSize;
1013 }
1014 if (oldMtuSize != mtuSize)
1015 emit q->mtuChanged(mtuSize);
1016 } break;
1019 // Discovering services
1020 Q_ASSERT(request.command == QBluezConst::AttCommand::ATT_OP_READ_BY_GROUP_REQUEST);
1021
1022 const quint16 type = request.reference.toUInt();
1023
1024 if (isErrorResponse) {
1025 if (type == GATT_SECONDARY_SERVICE) {
1026 setState(QLowEnergyController::DiscoveredState);
1027 q->discoveryFinished();
1028 } else { // search for secondary services
1029 sendReadByGroupRequest(0x0001, 0xFFFF, GATT_SECONDARY_SERVICE);
1030 }
1031 break;
1032 }
1033
1034 // response[1] == elementLength. According to the spec it should be
1035 // at least 4 bytes. See Spec v5.3, Vol 3, Part F, 3.4.4.10
1036 if (response.size() < 2 || response[1] < 4) {
1037 reportMalformedData(command, response);
1038 break;
1039 }
1040
1041 QLowEnergyHandle start = 0, end = 0;
1042 const quint16 elementLength = response.constData()[1]; // value checked above
1043 const quint16 numElements = (response.size() - 2) / elementLength;
1044 quint16 offset = 2;
1045 const char *data = response.constData();
1046 for (int i = 0; i < numElements; i++) {
1047 start = bt_get_le16(&data[offset]);
1048 end = bt_get_le16(&data[offset+2]);
1049
1050 QBluetoothUuid uuid;
1051 if (elementLength == 6) //16 bit uuid
1052 uuid = QBluetoothUuid(bt_get_le16(&data[offset+4]));
1053 else if (elementLength == 20) //128 bit uuid
1054 uuid = QUuid::fromBytes(&data[offset+4], QSysInfo::LittleEndian);
1055 //else -> do nothing
1056
1057 offset += elementLength;
1058
1059
1060 qCDebug(QT_BT_BLUEZ) << "Found uuid:" << uuid << "start handle:" << Qt::hex
1061 << start << "end handle:" << end;
1062
1064 priv->uuid = uuid;
1065 priv->startHandle = start;
1066 priv->endHandle = end;
1067 if (type != GATT_PRIMARY_SERVICE) //unset PrimaryService bit
1068 priv->type &= ~QLowEnergyService::PrimaryService;
1069 priv->setController(this);
1070
1071 QSharedPointer<QLowEnergyServicePrivate> pointer(priv);
1072
1073 serviceList.insert(uuid, pointer);
1074 emit q->serviceDiscovered(uuid);
1075 }
1076
1077 if (end != 0xFFFF) {
1078 sendReadByGroupRequest(end+1, 0xFFFF, type);
1079 } else {
1080 if (type == GATT_SECONDARY_SERVICE) {
1081 setState(QLowEnergyController::DiscoveredState);
1082 emit q->discoveryFinished();
1083 } else { // search for secondary services
1084 sendReadByGroupRequest(0x0001, 0xFFFF, GATT_SECONDARY_SERVICE);
1085 }
1086 }
1087 } break;
1090 // Discovering characteristics
1091 Q_ASSERT(request.command == QBluezConst::AttCommand::ATT_OP_READ_BY_TYPE_REQUEST);
1092
1093 QSharedPointer<QLowEnergyServicePrivate> p =
1094 request.reference.value<QSharedPointer<QLowEnergyServicePrivate> >();
1095 const quint16 attributeType = request.reference2.toUInt();
1096
1097 if (isErrorResponse) {
1098 if (attributeType == GATT_CHARACTERISTIC) {
1099 // we reached end of service handle
1100 // just finished up characteristic discovery
1101 // continue with values of characteristics
1102 if (!p->characteristicList.isEmpty()) {
1103 readServiceValues(p->uuid, true);
1104 } else {
1105 // discovery finished since the service doesn't have any
1106 // characteristics
1107 p->setState(QLowEnergyService::RemoteServiceDiscovered);
1108 }
1109 } else if (attributeType == GATT_INCLUDED_SERVICE) {
1110 // finished up include discovery
1111 // continue with characteristic discovery
1112 sendReadByTypeRequest(p, p->startHandle, GATT_CHARACTERISTIC);
1113 }
1114 break;
1115 }
1116
1117 /* packet format:
1118 * if GATT_CHARACTERISTIC discovery (Spec 5.3, Vol. 3, Part G, 4.6)
1119 * <opcode><elementLength>
1120 * [<handle><property><charHandle><uuid>]+
1121 * The minimum elementLength is 7 bytes (uuid is always included)
1122 *
1123 * if GATT_INCLUDE discovery (Spec 5.3, Vol. 3, Part G, 4.5.1)
1124 * <opcode><elementLength>
1125 * [<handle><startHandle_included><endHandle_included><uuid>]+
1126 * The minimum elementLength is 6 bytes (uuid can be omitted).
1127 *
1128 * The uuid can be 16 or 128 bit.
1129 */
1130
1131 const quint8 minimumElementLength = attributeType == GATT_CHARACTERISTIC ? 7 : 6;
1132 if (response.size() < 2 || response[1] < minimumElementLength) {
1133 reportMalformedData(command, response);
1134 break;
1135 }
1136
1137 QLowEnergyHandle lastHandle = 0;
1138 const quint16 elementLength = response.constData()[1];
1139 const quint16 numElements = (response.size() - 2) / elementLength;
1140 quint16 offset = 2;
1141 const char *data = response.constData();
1142 for (int i = 0; i < numElements; i++) {
1143 if (attributeType == GATT_CHARACTERISTIC) {
1144 QLowEnergyServicePrivate::CharData characteristic;
1145 lastHandle = parseReadByTypeCharDiscovery(
1146 &characteristic, &data[offset], elementLength);
1147 p->characteristicList[lastHandle] = characteristic;
1148 offset += elementLength;
1149 } else if (attributeType == GATT_INCLUDED_SERVICE) {
1150 QList<QBluetoothUuid> includedServices;
1151 lastHandle = parseReadByTypeIncludeDiscovery(
1152 &includedServices, &data[offset], elementLength);
1153 p->includedServices = includedServices;
1154 for (const QBluetoothUuid &uuid : std::as_const(includedServices)) {
1155 if (serviceList.contains(uuid))
1156 serviceList[uuid]->type |= QLowEnergyService::IncludedService;
1157 }
1158 }
1159 }
1160
1161 if (lastHandle + 1 < p->endHandle) { // more chars to discover
1162 sendReadByTypeRequest(p, lastHandle + 1, attributeType);
1163 } else {
1164 if (attributeType == GATT_INCLUDED_SERVICE)
1165 sendReadByTypeRequest(p, p->startHandle, GATT_CHARACTERISTIC);
1166 else
1167 readServiceValues(p->uuid, true);
1168 }
1169 } break;
1172 //Reading characteristics and descriptors
1173 Q_ASSERT(request.command == QBluezConst::AttCommand::ATT_OP_READ_REQUEST);
1174
1175 uint handleData = request.reference.toUInt();
1176 const QLowEnergyHandle charHandle = (handleData & 0xffff);
1177 const QLowEnergyHandle descriptorHandle = ((handleData >> 16) & 0xffff);
1178
1179 QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(charHandle);
1180 Q_ASSERT(!service.isNull());
1181 bool isServiceDiscoveryRun
1182 = !(service->state == QLowEnergyService::RemoteServiceDiscovered);
1183
1184 if (isErrorResponse) {
1185 Q_ASSERT(!encryptionChangePending);
1186 QBluezConst::AttError err = static_cast<QBluezConst::AttError>(response.constData()[4]);
1187 encryptionChangePending = increaseEncryptLevelfRequired(err);
1188 if (encryptionChangePending) {
1189 // Just requested a security level change.
1190 // Retry the same command again once the change has happened
1191 openRequests.prepend(request);
1192 break;
1193 } else if (!isServiceDiscoveryRun) {
1194 // not encryption problem -> abort readCharacteristic()/readDescriptor() run
1195 if (!descriptorHandle)
1196 service->setError(QLowEnergyService::CharacteristicReadError);
1197 else
1198 service->setError(QLowEnergyService::DescriptorReadError);
1199 }
1200 } else {
1201 if (!descriptorHandle)
1202 updateValueOfCharacteristic(charHandle, response.mid(1), NEW_VALUE);
1203 else
1204 updateValueOfDescriptor(charHandle, descriptorHandle,
1205 response.mid(1), NEW_VALUE);
1206
1207 if (response.size() == mtuSize) {
1208 qCDebug(QT_BT_BLUEZ) << "Switching to blob reads for"
1209 << charHandle << descriptorHandle
1210 << service->characteristicList[charHandle].uuid.toString();
1211 // Potentially more data -> switch to blob reads
1212 readServiceValuesByOffset(handleData, mtuSize-1,
1213 request.reference2.toBool());
1214 break;
1215 } else if (!isServiceDiscoveryRun) {
1216 // readCharacteristic() or readDescriptor() ongoing
1217 if (!descriptorHandle) {
1218 QLowEnergyCharacteristic ch(service, charHandle);
1219 emit service->characteristicRead(ch, response.mid(1));
1220 } else {
1221 QLowEnergyDescriptor descriptor(service, charHandle, descriptorHandle);
1222 emit service->descriptorRead(descriptor, response.mid(1));
1223 }
1224 break;
1225 }
1226 }
1227
1228 if (request.reference2.toBool() && isServiceDiscoveryRun) {
1229 // we only run into this code path during the initial service discovery
1230 // and not when processing readCharacteristics() after service discovery
1231
1232 //last characteristic -> progress to descriptor discovery
1233 //last descriptor -> service discovery is done
1234 if (!descriptorHandle)
1235 discoverServiceDescriptors(service->uuid);
1236 else
1237 service->setState(QLowEnergyService::RemoteServiceDiscovered);
1238 }
1239 } break;
1242 //Reading characteristic or descriptor with value longer value than MTU
1243 Q_ASSERT(request.command == QBluezConst::AttCommand::ATT_OP_READ_BLOB_REQUEST);
1244
1245 uint handleData = request.reference.toUInt();
1246 const QLowEnergyHandle charHandle = (handleData & 0xffff);
1247 const QLowEnergyHandle descriptorHandle = ((handleData >> 16) & 0xffff);
1248
1249 QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(charHandle);
1250 Q_ASSERT(!service.isNull());
1251
1252 /*
1253 * READ_BLOB does not require encryption setup code. BLOB commands
1254 * are only issued after read request if the read request is too long
1255 * for single MTU. The preceding read request would have triggered
1256 * the setup of the encryption already.
1257 */
1258 if (!isErrorResponse) {
1259 quint16 length = 0;
1260 if (!descriptorHandle)
1261 length = updateValueOfCharacteristic(charHandle, response.mid(1), APPEND_VALUE);
1262 else
1263 length = updateValueOfDescriptor(charHandle, descriptorHandle,
1264 response.mid(1), APPEND_VALUE);
1265
1266 if (response.size() == mtuSize) {
1267 readServiceValuesByOffset(handleData, length,
1268 request.reference2.toBool());
1269 break;
1270 } else if (service->state == QLowEnergyService::RemoteServiceDiscovered) {
1271 // readCharacteristic() or readDescriptor() ongoing
1272 if (!descriptorHandle) {
1273 QLowEnergyCharacteristic ch(service, charHandle);
1274 emit service->characteristicRead(ch, ch.value());
1275 } else {
1276 QLowEnergyDescriptor descriptor(service, charHandle, descriptorHandle);
1277 emit service->descriptorRead(descriptor, descriptor.value());
1278 }
1279 break;
1280 }
1281 } else {
1282 qWarning() << "READ BLOB for char:" << charHandle
1283 << "descriptor:" << descriptorHandle << "on service"
1284 << service->uuid.toString() << "failed (service discovery run:"
1285 << (service->state == QLowEnergyService::RemoteServiceDiscovered) << ")";
1286 }
1287
1288 if (request.reference2.toBool()) {
1289 //last overlong characteristic -> progress to descriptor discovery
1290 //last overlong descriptor -> service discovery is done
1291
1292 if (!descriptorHandle)
1293 discoverServiceDescriptors(service->uuid);
1294 else
1295 service->setState(QLowEnergyService::RemoteServiceDiscovered);
1296 }
1297
1298 } break;
1301 //Discovering descriptors
1303
1304 /* packet format:
1305 * <opcode><format>[<handle><descriptor_uuid>]+
1306 *
1307 * The uuid can be 16 or 128 bit which is indicated by format.
1308 */
1309
1310 QList<QLowEnergyHandle> keys = request.reference.value<QList<QLowEnergyHandle> >();
1311 if (keys.isEmpty()) {
1312 qCWarning(QT_BT_BLUEZ) << "Descriptor discovery for unknown characteristic received";
1313 break;
1314 }
1315 QLowEnergyHandle charHandle = keys.first();
1316
1317 QSharedPointer<QLowEnergyServicePrivate> p =
1318 serviceForHandle(charHandle);
1319 Q_ASSERT(!p.isNull());
1320
1321 if (isErrorResponse) {
1322 if (keys.size() == 1) {
1323 // no more descriptors to discover
1324 readServiceValues(p->uuid, false); //read descriptor values
1325 } else {
1326 // hop to the next descriptor
1327 keys.removeFirst();
1328 discoverNextDescriptor(p, keys, keys.first());
1329 }
1330 break;
1331 }
1332
1333 // Spec 5.3, Vol. 3, Part F, 3.4.3.2
1334 if (response.size() < 6) {
1335 reportMalformedData(command, response);
1336 break;
1337 }
1338
1339 const quint8 format = response[1];
1340 quint16 elementLength;
1341 switch (format) {
1342 case 0x01:
1343 elementLength = 2 + 2; //sizeof(QLowEnergyHandle) + 16bit uuid
1344 break;
1345 case 0x02:
1346 elementLength = 2 + 16; //sizeof(QLowEnergyHandle) + 128bit uuid
1347 break;
1348 default:
1349 qCWarning(QT_BT_BLUEZ) << "Unknown format in FIND_INFORMATION_RESPONSE";
1350 return;
1351 }
1352
1353 const quint16 numElements = (response.size() - 2) / elementLength;
1354
1355 quint16 offset = 2;
1356 QLowEnergyHandle descriptorHandle {};
1357 QBluetoothUuid uuid;
1358 const char *data = response.constData();
1359 for (int i = 0; i < numElements; i++) {
1360 descriptorHandle = bt_get_le16(&data[offset]);
1361
1362 if (format == 0x01)
1363 uuid = QBluetoothUuid(bt_get_le16(&data[offset+2]));
1364 else if (format == 0x02)
1365 uuid = QUuid::fromBytes(&data[offset+2], QSysInfo::LittleEndian);
1366
1367 offset += elementLength;
1368
1369 // ignore all attributes which are not of type descriptor
1370 // examples are the characteristics value or
1371 bool ok = false;
1372 quint16 shortUuid = uuid.toUInt16(&ok);
1373 if (ok && shortUuid >= QLowEnergyServicePrivate::PrimaryService
1375 qCDebug(QT_BT_BLUEZ) << "Suppressing primary/characteristic" << Qt::hex << shortUuid;
1376 continue;
1377 }
1378
1379 // ignore value handle
1380 if (descriptorHandle == p->characteristicList[charHandle].valueHandle) {
1381 qCDebug(QT_BT_BLUEZ) << "Suppressing char handle" << Qt::hex << descriptorHandle;
1382 continue;
1383 }
1384
1386 data.uuid = uuid;
1387 p->characteristicList[charHandle].descriptorList.insert(
1388 descriptorHandle, data);
1389
1390 qCDebug(QT_BT_BLUEZ) << "Descriptor found, uuid:"
1391 << uuid.toString()
1392 << "descriptor handle:" << Qt::hex << descriptorHandle;
1393 }
1394
1395 const QLowEnergyHandle nextPotentialHandle = descriptorHandle + 1;
1396 if (keys.size() == 1) {
1397 // Reached last characteristic of service
1398
1399 // The endhandle of a service is always the last handle of
1400 // the current service. We must either continue until we have reached
1401 // the starting handle of the next service (endHandle+1) or
1402 // the last physical handle address (0xffff). Note that
1403 // the endHandle of the last service on the device is 0xffff.
1404
1405 if ((p->endHandle != 0xffff && nextPotentialHandle >= p->endHandle + 1)
1406 || (descriptorHandle == 0xffff)) {
1407 keys.removeFirst();
1408 // last descriptor of last characteristic found
1409 // continue with reading descriptor values
1410 readServiceValues(p->uuid, false);
1411 } else {
1412 discoverNextDescriptor(p, keys, nextPotentialHandle);
1413 }
1414 } else {
1415 if (nextPotentialHandle >= keys[1]) //reached next char
1416 keys.removeFirst();
1417 discoverNextDescriptor(p, keys, nextPotentialHandle);
1418 }
1419 } break;
1422 //Write command response
1423 Q_ASSERT(request.command == QBluezConst::AttCommand::ATT_OP_WRITE_REQUEST);
1424
1425 uint ref = request.reference.toUInt();
1426 const QLowEnergyHandle charHandle = (ref & 0xffff);
1427 const QLowEnergyHandle descriptorHandle = ((ref >> 16) & 0xffff);
1428
1429 QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(charHandle);
1430 if (service.isNull() || !service->characteristicList.contains(charHandle))
1431 break;
1432
1433 if (isErrorResponse) {
1434 Q_ASSERT(!encryptionChangePending);
1435 QBluezConst::AttError err = static_cast<QBluezConst::AttError>(response.constData()[4]);
1436 encryptionChangePending = increaseEncryptLevelfRequired(err);
1437 if (encryptionChangePending) {
1438 openRequests.prepend(request);
1439 break;
1440 }
1441
1442 if (!descriptorHandle)
1443 service->setError(QLowEnergyService::CharacteristicWriteError);
1444 else
1445 service->setError(QLowEnergyService::DescriptorWriteError);
1446 break;
1447 }
1448
1449 const QByteArray newValue = request.reference2.toByteArray();
1450 if (!descriptorHandle) {
1451 QLowEnergyCharacteristic ch(service, charHandle);
1452 if (ch.properties() & QLowEnergyCharacteristic::Read)
1453 updateValueOfCharacteristic(charHandle, newValue, NEW_VALUE);
1454 emit service->characteristicWritten(ch, newValue);
1455 } else {
1456 updateValueOfDescriptor(charHandle, descriptorHandle, newValue, NEW_VALUE);
1457 QLowEnergyDescriptor descriptor(service, charHandle, descriptorHandle);
1458 emit service->descriptorWritten(descriptor, newValue);
1459 }
1460 } break;
1463 //Prepare write command response
1464 Q_ASSERT(request.command == QBluezConst::AttCommand::ATT_OP_PREPARE_WRITE_REQUEST);
1465
1466 uint handleData = request.reference.toUInt();
1467 const QLowEnergyHandle attrHandle = (handleData & 0xffff);
1468 const QByteArray newValue = request.reference2.toByteArray();
1469 const int writtenPayload = ((handleData >> 16) & 0xffff);
1470
1471 if (isErrorResponse) {
1472 Q_ASSERT(!encryptionChangePending);
1473 QBluezConst::AttError err = static_cast<QBluezConst::AttError>(response.constData()[4]);
1474 encryptionChangePending = increaseEncryptLevelfRequired(err);
1475 if (encryptionChangePending) {
1476 openRequests.prepend(request);
1477 break;
1478 }
1479 //emits error on cancellation and aborts existing prepare reuqests
1480 sendExecuteWriteRequest(attrHandle, newValue, true);
1481 } else {
1482 if (writtenPayload < newValue.size()) {
1483 sendNextPrepareWriteRequest(attrHandle, newValue, writtenPayload);
1484 } else {
1485 sendExecuteWriteRequest(attrHandle, newValue, false);
1486 }
1487 }
1488 } break;
1491 // right now used in connection with long characteristic/descriptor value writes
1492 // not catering for reliable writes
1493 Q_ASSERT(request.command == QBluezConst::AttCommand::ATT_OP_EXECUTE_WRITE_REQUEST);
1494
1495 uint handleData = request.reference.toUInt();
1496 const QLowEnergyHandle attrHandle = handleData & 0xffff;
1497 bool wasCancellation = !((handleData >> 16) & 0xffff);
1498 const QByteArray newValue = request.reference2.toByteArray();
1499
1500 // is it a descriptor or characteristic?
1501 const QLowEnergyDescriptor descriptor = descriptorForHandle(attrHandle);
1502 QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(attrHandle);
1503 Q_ASSERT(!service.isNull());
1504
1505 if (isErrorResponse || wasCancellation) {
1506 // charHandle == 0 -> cancellation
1507 if (descriptor.isValid())
1508 service->setError(QLowEnergyService::DescriptorWriteError);
1509 else
1510 service->setError(QLowEnergyService::CharacteristicWriteError);
1511 } else {
1512 if (descriptor.isValid()) {
1513 updateValueOfDescriptor(descriptor.characteristicHandle(),
1514 attrHandle, newValue, NEW_VALUE);
1515 emit service->descriptorWritten(descriptor, newValue);
1516 } else {
1517 QLowEnergyCharacteristic ch(service, attrHandle);
1518 if (ch.properties() & QLowEnergyCharacteristic::Read)
1519 updateValueOfCharacteristic(attrHandle, newValue, NEW_VALUE);
1520 emit service->characteristicWritten(ch, newValue);
1521 }
1522 }
1523 } break;
1524 default:
1525 qCDebug(QT_BT_BLUEZ) << "Unknown packet: " << response.toHex();
1526 break;
1527 }
1528}
1529
1530void QLowEnergyControllerPrivateBluez::discoverServices()
1531{
1532 sendReadByGroupRequest(0x0001, 0xFFFF, GATT_PRIMARY_SERVICE);
1533}
1534
1535void QLowEnergyControllerPrivateBluez::sendReadByGroupRequest(
1536 QLowEnergyHandle start, QLowEnergyHandle end, quint16 type)
1537{
1538 //call for primary and secondary services
1539 quint8 packet[GRP_TYPE_REQ_HEADER_SIZE];
1540
1541 packet[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_READ_BY_GROUP_REQUEST);
1542 putBtData(start, &packet[1]);
1543 putBtData(end, &packet[3]);
1544 putBtData(type, &packet[5]);
1545
1546 QByteArray data(GRP_TYPE_REQ_HEADER_SIZE, Qt::Uninitialized);
1547 memcpy(data.data(), packet, GRP_TYPE_REQ_HEADER_SIZE);
1548 qCDebug(QT_BT_BLUEZ) << "Sending read_by_group_type request, startHandle:" << Qt::hex
1549 << start << "endHandle:" << end << type;
1550
1551 Request request;
1552 request.payload = data;
1554 request.reference = type;
1555 openRequests.enqueue(request);
1556
1557 sendNextPendingRequest();
1558}
1559
1560void QLowEnergyControllerPrivateBluez::discoverServiceDetails(const QBluetoothUuid &service,
1561 QLowEnergyService::DiscoveryMode mode)
1562{
1563 if (!serviceList.contains(service)) {
1564 qCWarning(QT_BT_BLUEZ) << "Discovery of unknown service" << service.toString()
1565 << "not possible";
1566 return;
1567 }
1568
1569 QSharedPointer<QLowEnergyServicePrivate> serviceData = serviceList.value(service);
1570 serviceData->mode = mode;
1571 serviceData->characteristicList.clear();
1572 sendReadByTypeRequest(serviceData, serviceData->startHandle, GATT_INCLUDED_SERVICE);
1573}
1574
1575void QLowEnergyControllerPrivateBluez::sendReadByTypeRequest(
1576 QSharedPointer<QLowEnergyServicePrivate> serviceData,
1577 QLowEnergyHandle nextHandle, quint16 attributeType)
1578{
1579 quint8 packet[READ_BY_TYPE_REQ_HEADER_SIZE];
1580
1581 packet[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_READ_BY_TYPE_REQUEST);
1582 putBtData(nextHandle, &packet[1]);
1583 putBtData(serviceData->endHandle, &packet[3]);
1584 putBtData(attributeType, &packet[5]);
1585
1586 QByteArray data(READ_BY_TYPE_REQ_HEADER_SIZE, Qt::Uninitialized);
1587 memcpy(data.data(), packet, READ_BY_TYPE_REQ_HEADER_SIZE);
1588 qCDebug(QT_BT_BLUEZ) << "Sending read_by_type request, startHandle:" << Qt::hex
1589 << nextHandle << "endHandle:" << serviceData->endHandle
1590 << "type:" << attributeType << "packet:" << data.toHex();
1591
1592 Request request;
1593 request.payload = data;
1595 request.reference = QVariant::fromValue(serviceData);
1596 request.reference2 = attributeType;
1597 openRequests.enqueue(request);
1598
1599 sendNextPendingRequest();
1600}
1601
1602/*!
1603 \internal
1604
1605 Reads all values of specific characteristic and descriptor. This function is
1606 used during the initial service discovery process.
1607
1608 \a readCharacteristics determines whether we intend to read a characteristic;
1609 otherwise we read a descriptor.
1610 */
1611void QLowEnergyControllerPrivateBluez::readServiceValues(
1612 const QBluetoothUuid &serviceUuid, bool readCharacteristics)
1613{
1614 quint8 packet[READ_REQUEST_HEADER_SIZE];
1615 if (QT_BT_BLUEZ().isDebugEnabled()) {
1616 if (readCharacteristics)
1617 qCDebug(QT_BT_BLUEZ) << "Reading all characteristic values for"
1618 << serviceUuid.toString();
1619 else
1620 qCDebug(QT_BT_BLUEZ) << "Reading all descriptor values for"
1621 << serviceUuid.toString();
1622 }
1623
1624 QSharedPointer<QLowEnergyServicePrivate> service = serviceList.value(serviceUuid);
1625
1626 if (service->mode == QLowEnergyService::SkipValueDiscovery) {
1627 if (readCharacteristics) {
1628 // -> continue with descriptor discovery
1629 discoverServiceDescriptors(service->uuid);
1630 } else {
1631 service->setState(QLowEnergyService::RemoteServiceDiscovered);
1632 }
1633 return;
1634 }
1635
1636 // pair.first -> target attribute
1637 // pair.second -> context information for read request
1638 QPair<QLowEnergyHandle, quint32> pair;
1639
1640 // Create list of attribute handles which need to be read
1641 QList<QPair<QLowEnergyHandle, quint32> > targetHandles;
1642
1643 CharacteristicDataMap::const_iterator charIt = service->characteristicList.constBegin();
1644 for ( ; charIt != service->characteristicList.constEnd(); ++charIt) {
1645 const QLowEnergyHandle charHandle = charIt.key();
1646 const QLowEnergyServicePrivate::CharData &charDetails = charIt.value();
1647
1648 if (readCharacteristics) {
1649 // Collect handles of all characteristic value attributes
1650
1651 // Don't try to read writeOnly characteristic
1652 if (!(charDetails.properties & QLowEnergyCharacteristic::Read))
1653 continue;
1654
1655 pair.first = charDetails.valueHandle;
1656 pair.second = charHandle;
1657 targetHandles.append(pair);
1658
1659 } else {
1660 // Collect handles of all descriptor attributes
1661 DescriptorDataMap::const_iterator descIt = charDetails.descriptorList.constBegin();
1662 for ( ; descIt != charDetails.descriptorList.constEnd(); ++descIt) {
1663 const QLowEnergyHandle descriptorHandle = descIt.key();
1664
1665 pair.first = descriptorHandle;
1666 pair.second = (charHandle | (descriptorHandle << 16));
1667 targetHandles.append(pair);
1668 }
1669 }
1670 }
1671
1672
1673 if (targetHandles.isEmpty()) {
1674 if (readCharacteristics) {
1675 // none of the characteristics is readable
1676 // -> continue with descriptor discovery
1677 discoverServiceDescriptors(service->uuid);
1678 } else {
1679 // characteristic w/o descriptors
1680 service->setState(QLowEnergyService::RemoteServiceDiscovered);
1681 }
1682 return;
1683 }
1684
1685 for (qsizetype i = 0; i < targetHandles.size(); i++) {
1686 pair = targetHandles.at(i);
1687 packet[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_READ_REQUEST);
1688 putBtData(pair.first, &packet[1]);
1689
1690 QByteArray data(READ_REQUEST_HEADER_SIZE, Qt::Uninitialized);
1691 memcpy(data.data(), packet, READ_REQUEST_HEADER_SIZE);
1692
1693 Request request;
1694 request.payload = data;
1696 request.reference = pair.second;
1697 // last entry?
1698 request.reference2 = QVariant((bool)(i + 1 == targetHandles.size()));
1699 openRequests.enqueue(request);
1700 }
1701
1702 sendNextPendingRequest();
1703}
1704
1705/*!
1706 \internal
1707
1708 This function is used when reading a handle value that is
1709 longer than the mtuSize.
1710
1711 The BLOB read request is prepended to the list of
1712 open requests to finish the current value read up before
1713 starting the next read request.
1714 */
1715void QLowEnergyControllerPrivateBluez::readServiceValuesByOffset(
1716 uint handleData, quint16 offset, bool isLastValue)
1717{
1718 const QLowEnergyHandle charHandle = (handleData & 0xffff);
1719 const QLowEnergyHandle descriptorHandle = ((handleData >> 16) & 0xffff);
1720
1721 QByteArray data(READ_BLOB_REQUEST_HEADER_SIZE, Qt::Uninitialized);
1722 data[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_READ_BLOB_REQUEST);
1723
1724 QLowEnergyHandle handleToRead = charHandle;
1725 if (descriptorHandle) {
1726 handleToRead = descriptorHandle;
1727 qCDebug(QT_BT_BLUEZ) << "Reading descriptor via blob request"
1728 << Qt::hex << descriptorHandle;
1729 } else {
1730 //charHandle is not the char's value handle
1731 QSharedPointer<QLowEnergyServicePrivate> service =
1732 serviceForHandle(charHandle);
1733 if (!service.isNull()
1734 && service->characteristicList.contains(charHandle)) {
1735 handleToRead = service->characteristicList[charHandle].valueHandle;
1736 qCDebug(QT_BT_BLUEZ) << "Reading characteristic via blob request"
1737 << Qt::hex << handleToRead;
1738 } else {
1739 Q_ASSERT(false);
1740 }
1741 }
1742
1743 putBtData(handleToRead, data.data() + 1);
1744 putBtData(offset, data.data() + 3);
1745
1746 Request request;
1747 request.payload = data;
1749 request.reference = handleData;
1750 request.reference2 = isLastValue;
1751 openRequests.prepend(request);
1752}
1753
1754void QLowEnergyControllerPrivateBluez::discoverServiceDescriptors(
1755 const QBluetoothUuid &serviceUuid)
1756{
1757 qCDebug(QT_BT_BLUEZ) << "Discovering descriptor values for"
1758 << serviceUuid.toString();
1759 QSharedPointer<QLowEnergyServicePrivate> service = serviceList.value(serviceUuid);
1760
1761 if (service->characteristicList.isEmpty()) { // service has no characteristics
1762 // implies that characteristic & descriptor discovery can be skipped
1763 service->setState(QLowEnergyService::RemoteServiceDiscovered);
1764 return;
1765 }
1766
1767 // start handle of all known characteristics
1768 QList<QLowEnergyHandle> keys = service->characteristicList.keys();
1769 std::sort(keys.begin(), keys.end());
1770
1771 discoverNextDescriptor(service, keys, keys[0]);
1772}
1773
1774void QLowEnergyControllerPrivateBluez::processUnsolicitedReply(const QByteArray &payload)
1775{
1776 Q_ASSERT(!payload.isEmpty());
1777
1778 const char *data = payload.constData();
1779 const auto command = static_cast<QBluezConst::AttCommand>(data[0]);
1780 bool isNotification = (command
1782
1783 if (payload.size() < 3) {
1784 reportMalformedData(command, payload);
1785 return;
1786 }
1787
1788 const QLowEnergyHandle changedHandle = bt_get_le16(&data[1]);
1789
1790 if (QT_BT_BLUEZ().isDebugEnabled()) {
1791 if (isNotification)
1792 qCDebug(QT_BT_BLUEZ) << "Change notification for handle" << Qt::hex << changedHandle;
1793 else
1794 qCDebug(QT_BT_BLUEZ) << "Change indication for handle" << Qt::hex << changedHandle;
1795 }
1796
1797 const QLowEnergyCharacteristic ch = characteristicForHandle(changedHandle);
1798 if (ch.isValid() && ch.handle() == changedHandle) {
1799 if (ch.properties() & QLowEnergyCharacteristic::Read)
1800 updateValueOfCharacteristic(ch.attributeHandle(), payload.mid(3), NEW_VALUE);
1801 emit ch.d_ptr->characteristicChanged(ch, payload.mid(3));
1802 } else {
1803 qCWarning(QT_BT_BLUEZ) << "Cannot find matching characteristic for "
1804 "notification/indication";
1805 }
1806}
1807
1808void QLowEnergyControllerPrivateBluez::exchangeMTU()
1809{
1810 qCDebug(QT_BT_BLUEZ) << "Exchanging MTU";
1811
1812 quint8 packet[MTU_EXCHANGE_HEADER_SIZE];
1813 packet[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_EXCHANGE_MTU_REQUEST);
1814 putBtData(ATT_MAX_LE_MTU, &packet[1]);
1815
1816 QByteArray data(MTU_EXCHANGE_HEADER_SIZE, Qt::Uninitialized);
1817 memcpy(data.data(), packet, MTU_EXCHANGE_HEADER_SIZE);
1818
1819 Request request;
1820 request.payload = data;
1822 openRequests.enqueue(request);
1823
1824 sendNextPendingRequest();
1825}
1826
1827int QLowEnergyControllerPrivateBluez::securityLevel() const
1828{
1829 int socket = l2cpSocket->socketDescriptor();
1830 if (socket < 0) {
1831 qCWarning(QT_BT_BLUEZ) << "Invalid l2cp socket, aborting getting of sec level";
1832 return -1;
1833 }
1834
1835 struct bt_security secData;
1836 socklen_t length = sizeof(secData);
1837 memset(&secData, 0, length);
1838
1839 if (getsockopt(socket, SOL_BLUETOOTH, BT_SECURITY, &secData, &length) == 0) {
1840 qCDebug(QT_BT_BLUEZ) << "Current l2cp sec level:" << secData.level;
1841 return secData.level;
1842 }
1843
1844 if (errno != ENOPROTOOPT) //older kernel, fall back to L2CAP_LM option
1845 return -1;
1846
1847 // cater for older kernels
1848 int optval;
1849 length = sizeof(optval);
1850 if (getsockopt(socket, SOL_L2CAP, L2CAP_LM, &optval, &length) == 0) {
1851 int level = BT_SECURITY_SDP;
1852 if (optval & L2CAP_LM_AUTH)
1853 level = BT_SECURITY_LOW;
1854 if (optval & L2CAP_LM_ENCRYPT)
1855 level = BT_SECURITY_MEDIUM;
1856 if (optval & L2CAP_LM_SECURE)
1857 level = BT_SECURITY_HIGH;
1858
1859 qCDebug(QT_BT_BLUEZ) << "Current l2cp sec level (old):" << level;
1860 return level;
1861 }
1862
1863 return -1;
1864}
1865
1866bool QLowEnergyControllerPrivateBluez::setSecurityLevel(int level)
1867{
1868 if (level > BT_SECURITY_HIGH || level < BT_SECURITY_LOW)
1869 return false;
1870
1871 int socket = l2cpSocket->socketDescriptor();
1872 if (socket < 0) {
1873 qCWarning(QT_BT_BLUEZ) << "Invalid l2cp socket, aborting setting of sec level";
1874 return false;
1875 }
1876
1877 struct bt_security secData;
1878 socklen_t length = sizeof(secData);
1879 memset(&secData, 0, length);
1880 secData.level = level;
1881
1882 if (setsockopt(socket, SOL_BLUETOOTH, BT_SECURITY, &secData, length) == 0) {
1883 qCDebug(QT_BT_BLUEZ) << "Setting new l2cp sec level:" << secData.level;
1884 return true;
1885 }
1886
1887 if (errno != ENOPROTOOPT) //older kernel
1888 return false;
1889
1890 int optval = 0;
1891 switch (level) { // fall through intendeds
1892 case BT_SECURITY_HIGH:
1893 optval |= L2CAP_LM_SECURE;
1894 Q_FALLTHROUGH();
1895 case BT_SECURITY_MEDIUM:
1896 optval |= L2CAP_LM_ENCRYPT;
1897 Q_FALLTHROUGH();
1898 case BT_SECURITY_LOW:
1899 optval |= L2CAP_LM_AUTH;
1900 break;
1901 default:
1902 return false;
1903 }
1904
1905 if (setsockopt(socket, SOL_L2CAP, L2CAP_LM, &optval, sizeof(optval)) == 0) {
1906 qCDebug(QT_BT_BLUEZ) << "Old l2cp sec level:" << optval;
1907 return true;
1908 }
1909
1910 return false;
1911}
1912
1913void QLowEnergyControllerPrivateBluez::discoverNextDescriptor(
1914 QSharedPointer<QLowEnergyServicePrivate> serviceData,
1915 const QList<QLowEnergyHandle> pendingCharHandles,
1916 const QLowEnergyHandle startingHandle)
1917{
1918 Q_ASSERT(!pendingCharHandles.isEmpty());
1919 Q_ASSERT(!serviceData.isNull());
1920
1921 qCDebug(QT_BT_BLUEZ) << "Sending find_info request" << Qt::hex
1922 << pendingCharHandles << startingHandle;
1923
1924 quint8 packet[FIND_INFO_REQUEST_HEADER_SIZE];
1925 packet[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_FIND_INFORMATION_REQUEST);
1926
1927 const QLowEnergyHandle charStartHandle = startingHandle;
1928 QLowEnergyHandle charEndHandle = 0;
1929 if (pendingCharHandles.size() == 1) //single characteristic
1930 charEndHandle = serviceData->endHandle;
1931 else
1932 charEndHandle = pendingCharHandles[1] - 1;
1933
1934 putBtData(charStartHandle, &packet[1]);
1935 putBtData(charEndHandle, &packet[3]);
1936
1937 QByteArray data(FIND_INFO_REQUEST_HEADER_SIZE, Qt::Uninitialized);
1938 memcpy(data.data(), packet, FIND_INFO_REQUEST_HEADER_SIZE);
1939
1940 Request request;
1941 request.payload = data;
1943 request.reference = QVariant::fromValue<QList<QLowEnergyHandle> >(pendingCharHandles);
1944 request.reference2 = startingHandle;
1945 openRequests.enqueue(request);
1946
1947 sendNextPendingRequest();
1948}
1949
1950void QLowEnergyControllerPrivateBluez::sendNextPrepareWriteRequest(
1951 const QLowEnergyHandle handle, const QByteArray &newValue,
1952 quint16 offset)
1953{
1954 // is it a descriptor or characteristic?
1955 QLowEnergyHandle targetHandle = 0;
1956 const QLowEnergyDescriptor descriptor = descriptorForHandle(handle);
1957 if (descriptor.isValid())
1958 targetHandle = descriptor.handle();
1959 else
1960 targetHandle = characteristicForHandle(handle).handle();
1961
1962 if (!targetHandle) {
1963 qCWarning(QT_BT_BLUEZ) << "sendNextPrepareWriteRequest cancelled due to invalid handle"
1964 << handle;
1965 return;
1966 }
1967
1968 quint8 packet[PREPARE_WRITE_HEADER_SIZE];
1969 packet[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_PREPARE_WRITE_REQUEST);
1970 putBtData(targetHandle, &packet[1]); // attribute handle
1971 putBtData(offset, &packet[3]); // offset into newValue
1972
1973 qCDebug(QT_BT_BLUEZ) << "Writing long characteristic (prepare):"
1974 << Qt::hex << handle;
1975
1976
1977 const qsizetype maxAvailablePayload = qsizetype(mtuSize) - PREPARE_WRITE_HEADER_SIZE;
1978 const qsizetype requiredPayload = (std::min)(newValue.size() - offset, maxAvailablePayload);
1979 const qsizetype dataSize = PREPARE_WRITE_HEADER_SIZE + requiredPayload;
1980
1981 Q_ASSERT((offset + requiredPayload) <= newValue.size());
1982 Q_ASSERT(dataSize <= mtuSize);
1983
1984 QByteArray data(dataSize, Qt::Uninitialized);
1985 memcpy(data.data(), packet, PREPARE_WRITE_HEADER_SIZE);
1986 memcpy(&(data.data()[PREPARE_WRITE_HEADER_SIZE]), &(newValue.constData()[offset]),
1987 requiredPayload);
1988
1989 Request request;
1990 request.payload = data;
1992 request.reference = (handle | ((offset + requiredPayload) << 16));
1993 request.reference2 = newValue;
1994 openRequests.enqueue(request);
1995}
1996
1997/*!
1998 Sends an "Execute Write Request" for a long characteristic or descriptor write.
1999 This cannot be used for executes in relation to reliable write requests.
2000
2001 A cancellation removes all pending prepare write request on the GATT server.
2002 Otherwise this function sends an execute request for all pending prepare
2003 write requests.
2004 */
2005void QLowEnergyControllerPrivateBluez::sendExecuteWriteRequest(
2006 const QLowEnergyHandle attrHandle, const QByteArray &newValue,
2007 bool isCancelation)
2008{
2009 quint8 packet[EXECUTE_WRITE_HEADER_SIZE];
2010 packet[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_EXECUTE_WRITE_REQUEST);
2011 if (isCancelation)
2012 packet[1] = 0x00; // cancel pending write prepare requests
2013 else
2014 packet[1] = 0x01; // execute pending write prepare requests
2015
2016 QByteArray data(EXECUTE_WRITE_HEADER_SIZE, Qt::Uninitialized);
2017 memcpy(data.data(), packet, EXECUTE_WRITE_HEADER_SIZE);
2018
2019 qCDebug(QT_BT_BLUEZ) << "Sending Execute Write Request for long characteristic value"
2020 << Qt::hex << attrHandle;
2021
2022 Request request;
2023 request.payload = data;
2025 request.reference = (attrHandle | ((isCancelation ? 0x00 : 0x01) << 16));
2026 request.reference2 = newValue;
2027 openRequests.prepend(request);
2028}
2029
2030
2031/*!
2032 Writes long (prepare write request), short (write request)
2033 and writeWithoutResponse characteristic values.
2034
2035 TODO Reliable/prepare write across multiple characteristics is not supported
2036 */
2037void QLowEnergyControllerPrivateBluez::writeCharacteristic(
2038 const QSharedPointer<QLowEnergyServicePrivate> service,
2039 const QLowEnergyHandle charHandle,
2040 const QByteArray &newValue,
2041 QLowEnergyService::WriteMode mode)
2042{
2043 Q_ASSERT(!service.isNull());
2044
2045 if (!service->characteristicList.contains(charHandle))
2046 return;
2047
2048 QLowEnergyServicePrivate::CharData &charData = service->characteristicList[charHandle];
2049 if (role == QLowEnergyController::PeripheralRole)
2050 writeCharacteristicForPeripheral(charData, newValue);
2051 else
2052 writeCharacteristicForCentral(service, charHandle, charData.valueHandle, newValue, mode);
2053}
2054
2055void QLowEnergyControllerPrivateBluez::writeDescriptor(
2056 const QSharedPointer<QLowEnergyServicePrivate> service,
2057 const QLowEnergyHandle charHandle,
2058 const QLowEnergyHandle descriptorHandle,
2059 const QByteArray &newValue)
2060{
2061 Q_ASSERT(!service.isNull());
2062
2063 if (role == QLowEnergyController::PeripheralRole)
2064 writeDescriptorForPeripheral(service, charHandle, descriptorHandle, newValue);
2065 else
2066 writeDescriptorForCentral(charHandle, descriptorHandle, newValue);
2067}
2068
2069/*!
2070 \internal
2071
2072 Reads the value of one specific characteristic.
2073 */
2074void QLowEnergyControllerPrivateBluez::readCharacteristic(
2075 const QSharedPointer<QLowEnergyServicePrivate> service,
2076 const QLowEnergyHandle charHandle)
2077{
2078 Q_ASSERT(!service.isNull());
2079 if (!service->characteristicList.contains(charHandle))
2080 return;
2081
2082 const QLowEnergyServicePrivate::CharData &charDetails
2083 = service->characteristicList[charHandle];
2084 if (!(charDetails.properties & QLowEnergyCharacteristic::Read)) {
2085 // if this succeeds the device has a bug, char is advertised as
2086 // non-readable. We try to be permissive and let the remote
2087 // device answer to the read attempt
2088 qCWarning(QT_BT_BLUEZ) << "Reading non-readable char" << charHandle;
2089 }
2090
2091 quint8 packet[READ_REQUEST_HEADER_SIZE];
2092 packet[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_READ_REQUEST);
2093 putBtData(charDetails.valueHandle, &packet[1]);
2094
2095 QByteArray data(READ_REQUEST_HEADER_SIZE, Qt::Uninitialized);
2096 memcpy(data.data(), packet, READ_REQUEST_HEADER_SIZE);
2097
2098 qCDebug(QT_BT_BLUEZ) << "Targeted reading characteristic" << Qt::hex << charHandle;
2099
2100 Request request;
2101 request.payload = data;
2103 request.reference = charHandle;
2104 // reference2 not really required but false prevents service discovery
2105 // code from running in QBluezConst::AttCommand::ATT_OP_READ_RESPONSE handler
2106 request.reference2 = false;
2107 openRequests.enqueue(request);
2108
2109 sendNextPendingRequest();
2110}
2111
2112void QLowEnergyControllerPrivateBluez::readDescriptor(
2113 const QSharedPointer<QLowEnergyServicePrivate> service,
2114 const QLowEnergyHandle charHandle,
2115 const QLowEnergyHandle descriptorHandle)
2116{
2117 Q_ASSERT(!service.isNull());
2118 if (!service->characteristicList.contains(charHandle))
2119 return;
2120
2121 const QLowEnergyServicePrivate::CharData &charDetails
2122 = service->characteristicList[charHandle];
2123 if (!charDetails.descriptorList.contains(descriptorHandle))
2124 return;
2125
2126 quint8 packet[READ_REQUEST_HEADER_SIZE];
2127 packet[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_READ_REQUEST);
2128 putBtData(descriptorHandle, &packet[1]);
2129
2130 QByteArray data(READ_REQUEST_HEADER_SIZE, Qt::Uninitialized);
2131 memcpy(data.data(), packet, READ_REQUEST_HEADER_SIZE);
2132
2133 qCDebug(QT_BT_BLUEZ) << "Targeted reading descriptor" << Qt::hex << descriptorHandle;
2134
2135 Request request;
2136 request.payload = data;
2138 request.reference = (charHandle | (descriptorHandle << 16));
2139 // reference2 not really required but false prevents service discovery
2140 // code from running in QBluezConst::AttCommand::ATT_OP_READ_RESPONSE handler
2141 request.reference2 = false;
2142 openRequests.enqueue(request);
2143
2144 sendNextPendingRequest();
2145}
2146
2147/*!
2148 * Returns true if the encryption change was successfully requested.
2149 * The request is triggered if we got a related ATT error.
2150 */
2151bool QLowEnergyControllerPrivateBluez::increaseEncryptLevelfRequired(
2152 QBluezConst::AttError errorCode)
2153{
2154 if (securityLevelValue == BT_SECURITY_HIGH)
2155 return false;
2156
2157 switch (errorCode) {
2161 if (!hciManager->isValid())
2162 return false;
2163 if (!hciManager->monitorEvent(HciManager::HciEvent::EVT_ENCRYPT_CHANGE))
2164 return false;
2165 if (securityLevelValue != BT_SECURITY_HIGH) {
2166 qCDebug(QT_BT_BLUEZ) << "Requesting encrypted link";
2167 if (setSecurityLevel(BT_SECURITY_HIGH)) {
2168 restartRequestTimer();
2169 return true;
2170 }
2171 }
2172 break;
2173 default:
2174 break;
2175 }
2176
2177 return false;
2178}
2179
2180void QLowEnergyControllerPrivateBluez::handleAdvertisingError()
2181{
2182 qCWarning(QT_BT_BLUEZ) << "received advertising error";
2183 setError(QLowEnergyController::AdvertisingError);
2184 setState(QLowEnergyController::UnconnectedState);
2185}
2186
2187bool QLowEnergyControllerPrivateBluez::checkPacketSize(const QByteArray &packet, int minSize,
2188 int maxSize)
2189{
2190 if (maxSize == -1)
2191 maxSize = minSize;
2192 if (Q_LIKELY(packet.size() >= minSize && packet.size() <= maxSize))
2193 return true;
2194 qCWarning(QT_BT_BLUEZ) << "client request of type" << packet.at(0)
2195 << "has unexpected packet size" << packet.size();
2196 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), 0,
2197 QBluezConst::AttError::ATT_ERROR_INVALID_PDU);
2198 return false;
2199}
2200
2201bool QLowEnergyControllerPrivateBluez::checkHandle(const QByteArray &packet, QLowEnergyHandle handle)
2202{
2203 if (handle != 0 && handle <= lastLocalHandle)
2204 return true;
2205 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), handle,
2206 QBluezConst::AttError::ATT_ERROR_INVALID_HANDLE);
2207 return false;
2208}
2209
2210bool QLowEnergyControllerPrivateBluez::checkHandlePair(QBluezConst::AttCommand request,
2211 QLowEnergyHandle startingHandle,
2212 QLowEnergyHandle endingHandle)
2213{
2214 if (startingHandle == 0 || startingHandle > endingHandle) {
2215 qCDebug(QT_BT_BLUEZ) << "handle range invalid";
2216 sendErrorResponse(request, startingHandle, QBluezConst::AttError::ATT_ERROR_INVALID_HANDLE);
2217 return false;
2218 }
2219 return true;
2220}
2221
2222void QLowEnergyControllerPrivateBluez::handleExchangeMtuRequest(const QByteArray &packet)
2223{
2224 // Spec v4.2, Vol 3, Part F, 3.4.2
2225
2226 if (!checkPacketSize(packet, 3))
2227 return;
2228 if (receivedMtuExchangeRequest) { // Client must only send this once per connection.
2229 qCDebug(QT_BT_BLUEZ) << "Client sent extraneous MTU exchange packet";
2230 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), 0,
2231 QBluezConst::AttError::ATT_ERROR_REQUEST_NOT_SUPPORTED);
2232 return;
2233 }
2234 receivedMtuExchangeRequest = true;
2235
2236 // Send reply.
2237 QByteArray reply(MTU_EXCHANGE_HEADER_SIZE, Qt::Uninitialized);
2238 reply[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_EXCHANGE_MTU_RESPONSE);
2239 putBtData(ATT_MAX_LE_MTU, reply.data() + 1);
2240 sendPacket(reply);
2241
2242 // Apply requested MTU.
2243 const quint16 clientRxMtu = bt_get_le16(packet.constData() + 1);
2244 mtuSize = std::clamp(clientRxMtu, ATT_DEFAULT_LE_MTU, ATT_MAX_LE_MTU);
2245 qCDebug(QT_BT_BLUEZ) << "MTU request from client:" << clientRxMtu
2246 << "effective client RX MTU:" << mtuSize;
2247 qCDebug(QT_BT_BLUEZ) << "Sending server RX MTU" << ATT_MAX_LE_MTU;
2248}
2249
2250void QLowEnergyControllerPrivateBluez::handleFindInformationRequest(const QByteArray &packet)
2251{
2252 // Spec v4.2, Vol 3, Part F, 3.4.3.1-2
2253
2254 if (!checkPacketSize(packet, 5))
2255 return;
2256QT_WARNING_PUSH
2257QT_WARNING_DISABLE_GCC("-Warray-bounds")
2258 const QLowEnergyHandle startingHandle = bt_get_le16(packet.constData() + 1);
2259 const QLowEnergyHandle endingHandle = bt_get_le16(packet.constData() + 3);
2260QT_WARNING_POP
2261 qCDebug(QT_BT_BLUEZ) << "client sends find information request; start:" << startingHandle
2262 << "end:" << endingHandle;
2263 if (!checkHandlePair(static_cast<QBluezConst::AttCommand>(packet.at(0)), startingHandle,
2264 endingHandle))
2265 return;
2266
2267 QList<Attribute> results = getAttributes(startingHandle, endingHandle);
2268 if (results.isEmpty()) {
2269 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), startingHandle,
2270 QBluezConst::AttError::ATT_ERROR_ATTRIBUTE_NOT_FOUND);
2271 return;
2272 }
2273 ensureUniformUuidSizes(results);
2274
2275 QByteArray responsePrefix(2, Qt::Uninitialized);
2276 const int uuidSize = getUuidSize(results.first().type);
2277 responsePrefix[0] =
2279 responsePrefix[1] = uuidSize == 2 ? 0x1 : 0x2;
2280 const int elementSize = sizeof(QLowEnergyHandle) + uuidSize;
2281 const auto elemWriter = [](const Attribute &attr, char *&data) {
2282 putDataAndIncrement(attr.handle, data);
2283 putDataAndIncrement(attr.type, data);
2284 };
2285 sendListResponse(responsePrefix, elementSize, results, elemWriter);
2286
2287}
2288
2289void QLowEnergyControllerPrivateBluez::handleFindByTypeValueRequest(const QByteArray &packet)
2290{
2291 // Spec v4.2, Vol 3, Part F, 3.4.3.3-4
2292
2293 if (!checkPacketSize(packet, 7, mtuSize))
2294 return;
2295QT_WARNING_PUSH
2296QT_WARNING_DISABLE_GCC("-Warray-bounds")
2297 const QLowEnergyHandle startingHandle = bt_get_le16(packet.constData() + 1);
2298 const QLowEnergyHandle endingHandle = bt_get_le16(packet.constData() + 3);
2299 const quint16 type = bt_get_le16(packet.constData() + 5);
2300QT_WARNING_POP
2301 const QByteArray value = QByteArray::fromRawData(packet.constData() + 7, packet.size() - 7);
2302 qCDebug(QT_BT_BLUEZ) << "client sends find by type value request; start:" << startingHandle
2303 << "end:" << endingHandle << "type:" << type
2304 << "value:" << value.toHex();
2305 if (!checkHandlePair(static_cast<QBluezConst::AttCommand>(packet.at(0)), startingHandle,
2306 endingHandle))
2307 return;
2308
2309 const auto predicate = [value, this, type](const Attribute &attr) {
2310 return attr.type == QBluetoothUuid(type) && attr.value == value
2311 && checkReadPermissions(attr) == QBluezConst::AttError::ATT_ERROR_NO_ERROR;
2312 };
2313 const QList<Attribute> results = getAttributes(startingHandle, endingHandle, predicate);
2314 if (results.isEmpty()) {
2315 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), startingHandle,
2316 QBluezConst::AttError::ATT_ERROR_ATTRIBUTE_NOT_FOUND);
2317 return;
2318 }
2319
2320 QByteArray responsePrefix(
2322 const int elemSize = 2 * sizeof(QLowEnergyHandle);
2323 const auto elemWriter = [](const Attribute &attr, char *&data) {
2324 putDataAndIncrement(attr.handle, data);
2325 putDataAndIncrement(attr.groupEndHandle, data);
2326 };
2327 sendListResponse(responsePrefix, elemSize, results, elemWriter);
2328}
2329
2330void QLowEnergyControllerPrivateBluez::handleReadByTypeRequest(const QByteArray &packet)
2331{
2332 // Spec v4.2, Vol 3, Part F, 3.4.4.1-2
2333
2334 if (!checkPacketSize(packet, 7, 21))
2335 return;
2336QT_WARNING_PUSH
2337QT_WARNING_DISABLE_GCC("-Warray-bounds")
2338 const QLowEnergyHandle startingHandle = bt_get_le16(packet.constData() + 1);
2339 const QLowEnergyHandle endingHandle = bt_get_le16(packet.constData() + 3);
2340QT_WARNING_POP
2341 const void * const typeStart = packet.constData() + 5;
2342 const bool is16BitUuid = packet.size() == 7;
2343 const bool is128BitUuid = packet.size() == 21;
2344 QBluetoothUuid type;
2345 if (is16BitUuid) {
2346 type = QBluetoothUuid(bt_get_le16(typeStart));
2347 } else if (is128BitUuid) {
2348 type = QUuid::fromBytes(typeStart, QSysInfo::LittleEndian);
2349 } else {
2350 qCWarning(QT_BT_BLUEZ) << "read by type request has invalid packet size" << packet.size();
2351 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), 0,
2352 QBluezConst::AttError::ATT_ERROR_INVALID_PDU);
2353 return;
2354 }
2355 qCDebug(QT_BT_BLUEZ) << "client sends read by type request, start:" << startingHandle
2356 << "end:" << endingHandle << "type:" << type;
2357 if (!checkHandlePair(static_cast<QBluezConst::AttCommand>(packet.at(0)), startingHandle,
2358 endingHandle))
2359 return;
2360
2361 // Get all attributes with matching type.
2362 QList<Attribute> results =
2363 getAttributes(startingHandle, endingHandle,
2364 [type](const Attribute &attr) { return attr.type == type; });
2365 ensureUniformValueSizes(results);
2366
2367 if (results.isEmpty()) {
2368 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), startingHandle,
2369 QBluezConst::AttError::ATT_ERROR_ATTRIBUTE_NOT_FOUND);
2370 return;
2371 }
2372
2373 const QBluezConst::AttError error = checkReadPermissions(results);
2375 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)),
2376 results.first().handle, error);
2377 return;
2378 }
2379
2380 const qsizetype elementSize = sizeof(QLowEnergyHandle) + results.first().value.size();
2381 QByteArray responsePrefix(2, Qt::Uninitialized);
2382 responsePrefix[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_READ_BY_TYPE_RESPONSE);
2383 responsePrefix[1] = elementSize;
2384 const auto elemWriter = [](const Attribute &attr, char *&data) {
2385 putDataAndIncrement(attr.handle, data);
2386 putDataAndIncrement(attr.value, data);
2387 };
2388 sendListResponse(responsePrefix, elementSize, results, elemWriter);
2389}
2390
2391void QLowEnergyControllerPrivateBluez::handleReadRequest(const QByteArray &packet)
2392{
2393 // Spec v4.2, Vol 3, Part F, 3.4.4.3-4
2394
2395 if (!checkPacketSize(packet, 3))
2396 return;
2397 const QLowEnergyHandle handle = bt_get_le16(packet.constData() + 1);
2398 qCDebug(QT_BT_BLUEZ) << "client sends read request; handle:" << handle;
2399
2400 if (!checkHandle(packet, handle))
2401 return;
2402 const Attribute &attribute = localAttributes.at(handle);
2403 const QBluezConst::AttError permissionsError = checkReadPermissions(attribute);
2404 if (permissionsError != QBluezConst::AttError::ATT_ERROR_NO_ERROR) {
2405 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), handle,
2406 permissionsError);
2407 return;
2408 }
2409
2410 const qsizetype sentValueLength = (std::min)(attribute.value.size(), qsizetype(mtuSize) - 1);
2411 QByteArray response(1 + sentValueLength, Qt::Uninitialized);
2412 response[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_READ_RESPONSE);
2413 using namespace std;
2414 memcpy(response.data() + 1, attribute.value.constData(), sentValueLength);
2415 qCDebug(QT_BT_BLUEZ) << "sending response:" << response.toHex();
2416 sendPacket(response);
2417}
2418
2419void QLowEnergyControllerPrivateBluez::handleReadBlobRequest(const QByteArray &packet)
2420{
2421 // Spec v4.2, Vol 3, Part F, 3.4.4.5-6
2422
2423 if (!checkPacketSize(packet, 5))
2424 return;
2425QT_WARNING_PUSH
2426QT_WARNING_DISABLE_GCC("-Warray-bounds")
2427 const QLowEnergyHandle handle = bt_get_le16(packet.constData() + 1);
2428 const quint16 valueOffset = bt_get_le16(packet.constData() + 3);
2429QT_WARNING_POP
2430 qCDebug(QT_BT_BLUEZ) << "client sends read blob request; handle:" << handle
2431 << "offset:" << valueOffset;
2432
2433 if (!checkHandle(packet, handle))
2434 return;
2435 const Attribute &attribute = localAttributes.at(handle);
2436 const QBluezConst::AttError permissionsError = checkReadPermissions(attribute);
2437 if (permissionsError != QBluezConst::AttError::ATT_ERROR_NO_ERROR) {
2438 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), handle,
2439 permissionsError);
2440 return;
2441 }
2442 if (valueOffset > attribute.value.size()) {
2443 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), handle,
2444 QBluezConst::AttError::ATT_ERROR_INVALID_OFFSET);
2445 return;
2446 }
2447 if (attribute.value.size() <= mtuSize - 3) {
2448 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), handle,
2449 QBluezConst::AttError::ATT_ERROR_ATTRIBUTE_NOT_LONG);
2450 return;
2451 }
2452
2453 // Yes, this value can be zero.
2454 const qsizetype sentValueLength = (std::min)(attribute.value.size() - valueOffset,
2455 qsizetype(mtuSize) - 1);
2456
2457 QByteArray response(1 + sentValueLength, Qt::Uninitialized);
2458 response[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_READ_BLOB_RESPONSE);
2459 using namespace std;
2460 memcpy(response.data() + 1, attribute.value.constData() + valueOffset, sentValueLength);
2461 qCDebug(QT_BT_BLUEZ) << "sending response:" << response.toHex();
2462 sendPacket(response);
2463}
2464
2465void QLowEnergyControllerPrivateBluez::handleReadMultipleRequest(const QByteArray &packet)
2466{
2467 // Spec v4.2, Vol 3, Part F, 3.4.4.7-8
2468
2469 if (!checkPacketSize(packet, 5, mtuSize))
2470 return;
2471 QList<QLowEnergyHandle> handles((packet.size() - 1) / sizeof(QLowEnergyHandle));
2472 auto *packetPtr = reinterpret_cast<const QLowEnergyHandle *>(packet.constData() + 1);
2473 for (qsizetype i = 0; i < handles.size(); ++i, ++packetPtr)
2474 handles[i] = bt_get_le16(packetPtr);
2475 qCDebug(QT_BT_BLUEZ) << "client sends read multiple request for handles" << handles;
2476
2477 const auto it = std::find_if(handles.constBegin(), handles.constEnd(),
2478 [this](QLowEnergyHandle handle) { return handle >= lastLocalHandle; });
2479 if (it != handles.constEnd()) {
2480 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), *it,
2481 QBluezConst::AttError::ATT_ERROR_INVALID_HANDLE);
2482 return;
2483 }
2484 const QList<Attribute> results = getAttributes(handles.first(), handles.last());
2485 QByteArray response(
2487 for (const Attribute &attr : results) {
2488 const QBluezConst::AttError error = checkReadPermissions(attr);
2489 if (error != QBluezConst::AttError::ATT_ERROR_NO_ERROR) {
2490 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), attr.handle,
2491 error);
2492 return;
2493 }
2494
2495 // Note: We do not abort if no more values fit into the packet, because we still have to
2496 // report possible permission errors for the other handles.
2497 response += attr.value.left(mtuSize - response.size());
2498 }
2499
2500 qCDebug(QT_BT_BLUEZ) << "sending response:" << response.toHex();
2501 sendPacket(response);
2502}
2503
2504void QLowEnergyControllerPrivateBluez::handleReadByGroupTypeRequest(const QByteArray &packet)
2505{
2506 // Spec v4.2, Vol 3, Part F, 3.4.4.9-10
2507
2508 if (!checkPacketSize(packet, 7, 21))
2509 return;
2510QT_WARNING_PUSH
2511QT_WARNING_DISABLE_GCC("-Warray-bounds")
2512 const QLowEnergyHandle startingHandle = bt_get_le16(packet.constData() + 1);
2513 const QLowEnergyHandle endingHandle = bt_get_le16(packet.constData() + 3);
2514QT_WARNING_POP
2515 const bool is16BitUuid = packet.size() == 7;
2516 const bool is128BitUuid = packet.size() == 21;
2517 const void * const typeStart = packet.constData() + 5;
2518 QBluetoothUuid type;
2519 if (is16BitUuid) {
2520 type = QBluetoothUuid(bt_get_le16(typeStart));
2521 } else if (is128BitUuid) {
2522 type = QUuid::fromBytes(typeStart, QSysInfo::LittleEndian);
2523 } else {
2524 qCWarning(QT_BT_BLUEZ) << "read by group type request has invalid packet size"
2525 << packet.size();
2526 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), 0,
2527 QBluezConst::AttError::ATT_ERROR_INVALID_PDU);
2528 return;
2529 }
2530 qCDebug(QT_BT_BLUEZ) << "client sends read by group type request, start:" << startingHandle
2531 << "end:" << endingHandle << "type:" << type;
2532
2533 if (!checkHandlePair(static_cast<QBluezConst::AttCommand>(packet.at(0)), startingHandle,
2534 endingHandle))
2535 return;
2536 if (type != QBluetoothUuid(static_cast<quint16>(GATT_PRIMARY_SERVICE))
2537 && type != QBluetoothUuid(static_cast<quint16>(GATT_SECONDARY_SERVICE))) {
2538 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), startingHandle,
2539 QBluezConst::AttError::ATT_ERROR_UNSUPPRTED_GROUP_TYPE);
2540 return;
2541 }
2542
2543 QList<Attribute> results =
2544 getAttributes(startingHandle, endingHandle,
2545 [type](const Attribute &attr) { return attr.type == type; });
2546 if (results.isEmpty()) {
2547 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), startingHandle,
2548 QBluezConst::AttError::ATT_ERROR_ATTRIBUTE_NOT_FOUND);
2549 return;
2550 }
2551 const QBluezConst::AttError error = checkReadPermissions(results);
2553 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)),
2554 results.first().handle, error);
2555 return;
2556 }
2557
2558 ensureUniformValueSizes(results);
2559
2560 const qsizetype elementSize = 2 * sizeof(QLowEnergyHandle) + results.first().value.size();
2561 QByteArray responsePrefix(2, Qt::Uninitialized);
2562 responsePrefix[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_READ_BY_GROUP_RESPONSE);
2563 responsePrefix[1] = elementSize;
2564 const auto elemWriter = [](const Attribute &attr, char *&data) {
2565 putDataAndIncrement(attr.handle, data);
2566 putDataAndIncrement(attr.groupEndHandle, data);
2567 putDataAndIncrement(attr.value, data);
2568 };
2569 sendListResponse(responsePrefix, elementSize, results, elemWriter);
2570}
2571
2572void QLowEnergyControllerPrivateBluez::updateLocalAttributeValue(
2573 QLowEnergyHandle handle,
2574 const QByteArray &value,
2575 QLowEnergyCharacteristic &characteristic,
2576 QLowEnergyDescriptor &descriptor)
2577{
2578 localAttributes[handle].value = value;
2579 for (const auto &service : std::as_const(localServices)) {
2580 if (handle < service->startHandle || handle > service->endHandle)
2581 continue;
2582 for (auto charIt = service->characteristicList.begin();
2583 charIt != service->characteristicList.end(); ++charIt) {
2584 QLowEnergyServicePrivate::CharData &charData = charIt.value();
2585 if (handle == charIt.key() + 1) { // Char value decl comes right after char decl.
2586 charData.value = value;
2587 characteristic = QLowEnergyCharacteristic(service, charIt.key());
2588 return;
2589 }
2590 for (auto descIt = charData.descriptorList.begin();
2591 descIt != charData.descriptorList.end(); ++descIt) {
2592 if (handle == descIt.key()) {
2593 descIt.value().value = value;
2594 descriptor = QLowEnergyDescriptor(service, charIt.key(), handle);
2595 return;
2596 }
2597 }
2598 }
2599 }
2600 qFatal("local services map inconsistent with local attribute map");
2601}
2602
2603static bool isNotificationEnabled(quint16 clientConfigValue) { return clientConfigValue & 0x1; }
2604static bool isIndicationEnabled(quint16 clientConfigValue) { return clientConfigValue & 0x2; }
2605
2606void QLowEnergyControllerPrivateBluez::writeCharacteristicForPeripheral(
2608 const QByteArray &newValue)
2609{
2610 const QLowEnergyHandle valueHandle = charData.valueHandle;
2611 Q_ASSERT(valueHandle <= lastLocalHandle);
2612 Attribute &attribute = localAttributes[valueHandle];
2613 if (newValue.size() < attribute.minLength || newValue.size() > attribute.maxLength) {
2614 qCWarning(QT_BT_BLUEZ) << "ignoring value of invalid length" << newValue.size()
2615 << "for attribute" << valueHandle;
2616 return;
2617 }
2618 attribute.value = newValue;
2619 charData.value = newValue;
2620 const bool hasNotifyProperty = attribute.properties & QLowEnergyCharacteristic::Notify;
2621 const bool hasIndicateProperty
2622 = attribute.properties & QLowEnergyCharacteristic::Indicate;
2623 if (!hasNotifyProperty && !hasIndicateProperty)
2624 return;
2625 for (const QLowEnergyServicePrivate::DescData &desc : std::as_const(charData.descriptorList)) {
2626 if (desc.uuid != QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration)
2627 continue;
2628
2629 // Notify/indicate currently connected client.
2630 const bool isConnected = state == QLowEnergyController::ConnectedState;
2631 if (isConnected) {
2632 Q_ASSERT(desc.value.size() == 2);
2633 quint16 configValue = bt_get_le16(desc.value.constData());
2634 if (isNotificationEnabled(configValue) && hasNotifyProperty) {
2635 sendNotification(valueHandle);
2636 } else if (isIndicationEnabled(configValue) && hasIndicateProperty) {
2637 if (indicationInFlight)
2638 scheduledIndications << valueHandle;
2639 else
2640 sendIndication(valueHandle);
2641 }
2642 }
2643
2644 // Prepare notification/indication of unconnected, bonded clients.
2645 for (auto it = clientConfigData.begin(); it != clientConfigData.end(); ++it) {
2646 if (isConnected && it.key() == remoteDevice.toUInt64())
2647 continue;
2648 QList<ClientConfigurationData> &configDataList = it.value();
2649 for (ClientConfigurationData &configData : configDataList) {
2650 if (configData.charValueHandle != valueHandle)
2651 continue;
2652 if ((isNotificationEnabled(configData.configValue) && hasNotifyProperty)
2653 || (isIndicationEnabled(configData.configValue) && hasIndicateProperty)) {
2654 configData.charValueWasUpdated = true;
2655 break;
2656 }
2657 }
2658 }
2659 break;
2660 }
2661}
2662
2663void QLowEnergyControllerPrivateBluez::writeCharacteristicForCentral(const QSharedPointer<QLowEnergyServicePrivate> &service,
2664 QLowEnergyHandle charHandle,
2665 QLowEnergyHandle valueHandle,
2666 const QByteArray &newValue,
2667 QLowEnergyService::WriteMode mode)
2668{
2669 QByteArray packet(WRITE_REQUEST_HEADER_SIZE + newValue.size(), Qt::Uninitialized);
2670 putBtData(valueHandle, packet.data() + 1);
2671 memcpy(packet.data() + 3, newValue.constData(), newValue.size());
2672 bool writeWithResponse = false;
2673 switch (mode) {
2674 case QLowEnergyService::WriteWithResponse:
2675 if (newValue.size() > (mtuSize - WRITE_REQUEST_HEADER_SIZE)) {
2676 sendNextPrepareWriteRequest(charHandle, newValue, 0);
2677 sendNextPendingRequest();
2678 return;
2679 }
2680 // write value fits into single package
2681 packet[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_WRITE_REQUEST);
2682 writeWithResponse = true;
2683 break;
2684 case QLowEnergyService::WriteWithoutResponse:
2685 packet[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_WRITE_COMMAND);
2686 break;
2687 case QLowEnergyService::WriteSigned:
2688 packet[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_SIGNED_WRITE_COMMAND);
2689 if (!isBonded()) {
2690 qCWarning(QT_BT_BLUEZ) << "signed write not possible: requires bond between devices";
2691 service->setError(QLowEnergyService::CharacteristicWriteError);
2692 return;
2693 }
2694 if (securityLevel() >= BT_SECURITY_MEDIUM) {
2695 qCWarning(QT_BT_BLUEZ) << "signed write not possible: not allowed on encrypted link";
2696 service->setError(QLowEnergyService::CharacteristicWriteError);
2697 return;
2698 }
2699 const auto signingDataIt = signingData.find(remoteDevice.toUInt64());
2700 if (signingDataIt == signingData.end()) {
2701 qCWarning(QT_BT_BLUEZ) << "signed write not possible: no signature key found";
2702 service->setError(QLowEnergyService::CharacteristicWriteError);
2703 return;
2704 }
2705 ++signingDataIt.value().counter;
2706 packet = LeCmacCalculator::createFullMessage(packet, signingDataIt.value().counter);
2707 const quint64 mac = LeCmacCalculator().calculateMac(packet, signingDataIt.value().key);
2708 packet.resize(packet.size() + sizeof mac);
2709 putBtData(mac, packet.data() + packet.size() - sizeof mac);
2710 storeSignCounter(LocalSigningKey);
2711 break;
2712 }
2713
2714 qCDebug(QT_BT_BLUEZ) << "Writing characteristic" << Qt::hex << charHandle
2715 << "(size:" << packet.size() << "with response:"
2716 << (mode == QLowEnergyService::WriteWithResponse)
2717 << "signed:" << (mode == QLowEnergyService::WriteSigned) << ")";
2718
2719 // Advantage of write without response is the quick turnaround.
2720 // It can be sent at any time and does not produce responses.
2721 // Therefore we will not put them into the openRequest queue at all.
2722 if (!writeWithResponse) {
2723 sendPacket(packet);
2724 return;
2725 }
2726
2727 Request request;
2728 request.payload = packet;
2730 request.reference = charHandle;
2731 request.reference2 = newValue;
2732 openRequests.enqueue(request);
2733
2734 sendNextPendingRequest();
2735}
2736
2737void QLowEnergyControllerPrivateBluez::writeDescriptorForPeripheral(
2738 const QSharedPointer<QLowEnergyServicePrivate> &service,
2739 const QLowEnergyHandle charHandle,
2740 const QLowEnergyHandle descriptorHandle,
2741 const QByteArray &newValue)
2742{
2743 Q_ASSERT(descriptorHandle <= lastLocalHandle);
2744 Attribute &attribute = localAttributes[descriptorHandle];
2745 if (newValue.size() < attribute.minLength || newValue.size() > attribute.maxLength) {
2746 qCWarning(QT_BT_BLUEZ) << "invalid value of size" << newValue.size()
2747 << "for attribute" << descriptorHandle;
2748 return;
2749 }
2750 attribute.value = newValue;
2751 service->characteristicList[charHandle].descriptorList[descriptorHandle].value = newValue;
2752}
2753
2754void QLowEnergyControllerPrivateBluez::writeDescriptorForCentral(
2755 const QLowEnergyHandle charHandle,
2756 const QLowEnergyHandle descriptorHandle,
2757 const QByteArray &newValue)
2758{
2759 if (newValue.size() > (mtuSize - WRITE_REQUEST_HEADER_SIZE)) {
2760 sendNextPrepareWriteRequest(descriptorHandle, newValue, 0);
2761 sendNextPendingRequest();
2762 return;
2763 }
2764
2765 quint8 packet[WRITE_REQUEST_HEADER_SIZE];
2766 packet[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_WRITE_REQUEST);
2767 putBtData(descriptorHandle, &packet[1]);
2768
2769 const qsizetype size = WRITE_REQUEST_HEADER_SIZE + newValue.size();
2770 QByteArray data(size, Qt::Uninitialized);
2771 memcpy(data.data(), packet, WRITE_REQUEST_HEADER_SIZE);
2772 memcpy(&(data.data()[WRITE_REQUEST_HEADER_SIZE]), newValue.constData(), newValue.size());
2773
2774 qCDebug(QT_BT_BLUEZ) << "Writing descriptor" << Qt::hex << descriptorHandle
2775 << "(size:" << size << ")";
2776
2777 Request request;
2778 request.payload = data;
2780 request.reference = (charHandle | (descriptorHandle << 16));
2781 request.reference2 = newValue;
2782 openRequests.enqueue(request);
2783
2784 sendNextPendingRequest();
2785}
2786
2787void QLowEnergyControllerPrivateBluez::handleWriteRequestOrCommand(const QByteArray &packet)
2788{
2789 // Spec v4.2, Vol 3, Part F, 3.4.5.1-3
2790
2791 const bool isRequest = static_cast<QBluezConst::AttCommand>(packet.at(0))
2793 const bool isSigned = static_cast<QBluezConst::AttCommand>(packet.at(0))
2795 if (!checkPacketSize(packet, isSigned ? 15 : 3, mtuSize))
2796 return;
2797 const QLowEnergyHandle handle = bt_get_le16(packet.constData() + 1);
2798 qCDebug(QT_BT_BLUEZ) << "client sends" << (isSigned ? "signed" : "") << "write"
2799 << (isRequest ? "request" : "command") << "for handle" << handle;
2800
2801 if (!checkHandle(packet, handle))
2802 return;
2803
2804 Attribute &attribute = localAttributes[handle];
2805 const QLowEnergyCharacteristic::PropertyType type = isRequest
2806 ? QLowEnergyCharacteristic::Write : isSigned
2807 ? QLowEnergyCharacteristic::WriteSigned : QLowEnergyCharacteristic::WriteNoResponse;
2808 const QBluezConst::AttError permissionsError = checkPermissions(attribute, type);
2809 if (permissionsError != QBluezConst::AttError::ATT_ERROR_NO_ERROR) {
2810 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), handle,
2811 permissionsError);
2812 return;
2813 }
2814
2815 int valueLength;
2816 if (isSigned) {
2817 if (!isBonded()) {
2818 qCWarning(QT_BT_BLUEZ) << "Ignoring signed write from non-bonded device.";
2819 return;
2820 }
2821 if (securityLevel() >= BT_SECURITY_MEDIUM) {
2822 qCWarning(QT_BT_BLUEZ) << "Ignoring signed write on encrypted link.";
2823 return;
2824 }
2825 const auto signingDataIt = signingData.find(remoteDevice.toUInt64());
2826 if (signingDataIt == signingData.constEnd()) {
2827 qCWarning(QT_BT_BLUEZ) << "No CSRK found for peer device, ignoring signed write";
2828 return;
2829 }
2830
2831 const quint32 signCounter = getBtData<quint32>(packet.data() + packet.size() - 12);
2832 if (signCounter < signingDataIt.value().counter + 1) {
2833 qCWarning(QT_BT_BLUEZ) << "Client's' sign counter" << signCounter
2834 << "not greater than local sign counter"
2835 << signingDataIt.value().counter
2836 << "; ignoring signed write command.";
2837 return;
2838 }
2839
2840QT_WARNING_PUSH
2841QT_WARNING_DISABLE_GCC("-Warray-bounds")
2842 // This is a signed write packet, so it has at least 15 bytes.
2843 const quint64 macFromClient = getBtData<quint64>(packet.data() + packet.size() - 8);
2844QT_WARNING_POP
2845 const bool signatureCorrect = verifyMac(packet.left(packet.size() - 12),
2846 signingDataIt.value().key, signCounter, macFromClient);
2847 if (!signatureCorrect) {
2848 qCWarning(QT_BT_BLUEZ) << "Signed Write packet has wrong signature, disconnecting";
2849 disconnectFromDevice(); // Recommended by spec v4.2, Vol 3, part C, 10.4.2
2850 return;
2851 }
2852
2853 signingDataIt.value().counter = signCounter;
2854 storeSignCounter(RemoteSigningKey);
2855 valueLength = packet.size() - 15;
2856 } else {
2857 valueLength = packet.size() - 3;
2858 }
2859
2860 if (valueLength > attribute.maxLength) {
2861 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), handle,
2862 QBluezConst::AttError::ATT_ERROR_INVAL_ATTR_VALUE_LEN);
2863 return;
2864 }
2865
2866 // If the attribute value has a fixed size and the value in the packet is shorter,
2867 // then we overwrite only the start of the attribute value and keep the rest.
2868 QByteArray value = packet.mid(3, valueLength);
2869 if (attribute.minLength == attribute.maxLength && valueLength < attribute.minLength)
2870 value += attribute.value.mid(valueLength, attribute.maxLength - valueLength);
2871
2872 QLowEnergyCharacteristic characteristic;
2873 QLowEnergyDescriptor descriptor;
2874 updateLocalAttributeValue(handle, value, characteristic, descriptor);
2875
2876 if (isRequest) {
2877 const QByteArray response =
2878 QByteArray(1, static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_WRITE_RESPONSE));
2879 sendPacket(response);
2880 }
2881
2882 if (characteristic.isValid()) {
2883 emit characteristic.d_ptr->characteristicChanged(characteristic, value);
2884 } else {
2885 Q_ASSERT(descriptor.isValid());
2886 emit descriptor.d_ptr->descriptorWritten(descriptor, value);
2887 }
2888}
2889
2890void QLowEnergyControllerPrivateBluez::handlePrepareWriteRequest(const QByteArray &packet)
2891{
2892 // Spec v4.2, Vol 3, Part F, 3.4.6.1
2893
2894 if (!checkPacketSize(packet, 5, mtuSize))
2895 return;
2896 const quint16 handle = bt_get_le16(packet.constData() + 1);
2897 qCDebug(QT_BT_BLUEZ) << "client sends prepare write request for handle" << handle;
2898
2899 if (!checkHandle(packet, handle))
2900 return;
2901 const Attribute &attribute = localAttributes.at(handle);
2902 const QBluezConst::AttError permissionsError =
2903 checkPermissions(attribute, QLowEnergyCharacteristic::Write);
2904 if (permissionsError != QBluezConst::AttError::ATT_ERROR_NO_ERROR) {
2905 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), handle,
2906 permissionsError);
2907 return;
2908 }
2909 if (openPrepareWriteRequests.size() >= maxPrepareQueueSize) {
2910 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)), handle,
2911 QBluezConst::AttError::ATT_ERROR_PREPARE_QUEUE_FULL);
2912 return;
2913 }
2914
2915 // The value is not checked here, but on the Execute request.
2916 openPrepareWriteRequests << WriteRequest(handle, bt_get_le16(packet.constData() + 3),
2917 packet.mid(5));
2918
2919 QByteArray response = packet;
2920 response[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_PREPARE_WRITE_RESPONSE);
2921 sendPacket(response);
2922}
2923
2924void QLowEnergyControllerPrivateBluez::handleExecuteWriteRequest(const QByteArray &packet)
2925{
2926 // Spec v4.2, Vol 3, Part F, 3.4.6.3
2927
2928 if (!checkPacketSize(packet, 2))
2929 return;
2930 const bool cancel = packet.at(1) == 0;
2931 qCDebug(QT_BT_BLUEZ) << "client sends execute write request; flag is"
2932 << (cancel ? "cancel" : "flush");
2933
2934 QList<WriteRequest> requests = openPrepareWriteRequests;
2935 openPrepareWriteRequests.clear();
2936 QList<QLowEnergyCharacteristic> characteristics;
2937 QList<QLowEnergyDescriptor> descriptors;
2938 if (!cancel) {
2939 for (const WriteRequest &request : std::as_const(requests)) {
2940 Attribute &attribute = localAttributes[request.handle];
2941 if (request.valueOffset > attribute.value.size()) {
2942 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)),
2943 request.handle, QBluezConst::AttError::ATT_ERROR_INVALID_OFFSET);
2944 return;
2945 }
2946 const QByteArray newValue = attribute.value.left(request.valueOffset) + request.value;
2947 if (newValue.size() > attribute.maxLength) {
2948 sendErrorResponse(static_cast<QBluezConst::AttCommand>(packet.at(0)),
2949 request.handle,
2950 QBluezConst::AttError::ATT_ERROR_INVAL_ATTR_VALUE_LEN);
2951 return;
2952 }
2953 QLowEnergyCharacteristic characteristic;
2954 QLowEnergyDescriptor descriptor;
2955 // TODO: Redundant attribute lookup for the case of the same handle appearing
2956 // more than once.
2957 updateLocalAttributeValue(request.handle, newValue, characteristic, descriptor);
2958 if (characteristic.isValid()) {
2959 characteristics << characteristic;
2960 } else if (descriptor.isValid()) {
2961 Q_ASSERT(descriptor.isValid());
2962 descriptors << descriptor;
2963 }
2964 }
2965 }
2966
2967 sendPacket(QByteArray(
2968 1, static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_EXECUTE_WRITE_RESPONSE)));
2969
2970 for (const QLowEnergyCharacteristic &characteristic : std::as_const(characteristics))
2971 emit characteristic.d_ptr->characteristicChanged(characteristic, characteristic.value());
2972 for (const QLowEnergyDescriptor &descriptor : std::as_const(descriptors))
2973 emit descriptor.d_ptr->descriptorWritten(descriptor, descriptor.value());
2974}
2975
2976void QLowEnergyControllerPrivateBluez::sendErrorResponse(QBluezConst::AttCommand request,
2977 quint16 handle, QBluezConst::AttError code)
2978{
2979 // An ATT command never receives an error response.
2982 return;
2983
2984 QByteArray packet(ERROR_RESPONSE_HEADER_SIZE, Qt::Uninitialized);
2985 packet[0] = static_cast<quint8>(QBluezConst::AttCommand::ATT_OP_ERROR_RESPONSE);
2986 packet[1] = static_cast<quint8>(request);
2987 putBtData(handle, packet.data() + 2);
2988 packet[4] = static_cast<quint8>(code);
2989 qCWarning(QT_BT_BLUEZ) << "sending error response; request:"
2990 << request << "handle:" << handle
2991 << "code:" << code;
2992 sendPacket(packet);
2993}
2994
2995void QLowEnergyControllerPrivateBluez::sendListResponse(const QByteArray &packetStart,
2996 qsizetype elemSize,
2997 const QList<Attribute> &attributes,
2998 const ElemWriter &elemWriter)
2999{
3000 const qsizetype offset = packetStart.size();
3001 const qsizetype elemCount = (std::min)(attributes.size(), (mtuSize - offset) / elemSize);
3002 const qsizetype totalPacketSize = offset + elemCount * elemSize;
3003 QByteArray response(totalPacketSize, Qt::Uninitialized);
3004 using namespace std;
3005 memcpy(response.data(), packetStart.constData(), offset);
3006 char *data = response.data() + offset;
3007 for_each(attributes.constBegin(), attributes.constBegin() + elemCount,
3008 [&data, elemWriter](const Attribute &attr) { elemWriter(attr, data); });
3009 qCDebug(QT_BT_BLUEZ) << "sending response:" << response.toHex();
3010 sendPacket(response);
3011}
3012
3013void QLowEnergyControllerPrivateBluez::sendNotification(QLowEnergyHandle handle)
3014{
3015 sendNotificationOrIndication(QBluezConst::AttCommand::ATT_OP_HANDLE_VAL_NOTIFICATION, handle);
3016}
3017
3018void QLowEnergyControllerPrivateBluez::sendIndication(QLowEnergyHandle handle)
3019{
3020 Q_ASSERT(!indicationInFlight);
3021 indicationInFlight = true;
3022 sendNotificationOrIndication(QBluezConst::AttCommand::ATT_OP_HANDLE_VAL_INDICATION, handle);
3023}
3024
3025void QLowEnergyControllerPrivateBluez::sendNotificationOrIndication(QBluezConst::AttCommand opCode,
3026 QLowEnergyHandle handle)
3027{
3028 Q_ASSERT(handle <= lastLocalHandle);
3029 const Attribute &attribute = localAttributes.at(handle);
3030 const qsizetype maxValueLength = (std::min)(attribute.value.size(), qsizetype(mtuSize) - 3);
3031 QByteArray packet(3 + maxValueLength, Qt::Uninitialized);
3032 packet[0] = static_cast<quint8>(opCode);
3033 putBtData(handle, packet.data() + 1);
3034 using namespace std;
3035 memcpy(packet.data() + 3, attribute.value.constData(), maxValueLength);
3036 qCDebug(QT_BT_BLUEZ) << "sending notification/indication:" << packet.toHex();
3037 sendPacket(packet);
3038}
3039
3040void QLowEnergyControllerPrivateBluez::sendNextIndication()
3041{
3042 if (!scheduledIndications.isEmpty())
3043 sendIndication(scheduledIndications.takeFirst());
3044}
3045
3046static QString nameOfRemoteCentral(const QBluetoothAddress &peerAddress)
3047{
3048 const QString peerAddressString = peerAddress.toString();
3049 initializeBluez5();
3050 OrgFreedesktopDBusObjectManagerInterface manager(QStringLiteral("org.bluez"),
3051 QStringLiteral("/"),
3052 QDBusConnection::systemBus());
3053 QDBusPendingReply<ManagedObjectList> reply = manager.GetManagedObjects();
3054 reply.waitForFinished();
3055 if (reply.isError())
3056 return QString();
3057
3058 ManagedObjectList managedObjectList = reply.value();
3059 for (ManagedObjectList::const_iterator it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) {
3060 const InterfaceList &ifaceList = it.value();
3061
3062 for (InterfaceList::const_iterator jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) {
3063 const QString &iface = jt.key();
3064 const QVariantMap &ifaceValues = jt.value();
3065
3066 if (iface == QStringLiteral("org.bluez.Device1")) {
3067 if (ifaceValues.value(QStringLiteral("Address")).toString() == peerAddressString)
3068 return ifaceValues.value(QStringLiteral("Alias")).toString();
3069 }
3070 }
3071 }
3072 return QString();
3073}
3074
3075void QLowEnergyControllerPrivateBluez::handleConnectionRequest()
3076{
3077 if (state != QLowEnergyController::AdvertisingState) {
3078 qCWarning(QT_BT_BLUEZ) << "Incoming connection request in unexpected state" << state;
3079 return;
3080 }
3081 Q_ASSERT(serverSocketNotifier);
3082 serverSocketNotifier->setEnabled(false);
3083 sockaddr_l2 clientAddr;
3084 socklen_t clientAddrSize = sizeof clientAddr;
3085 const int clientSocket = accept(serverSocketNotifier->socket(),
3086 reinterpret_cast<sockaddr *>(&clientAddr), &clientAddrSize);
3087 if (clientSocket == -1) {
3088 // Not fatal in itself. The next one might succeed.
3089 qCWarning(QT_BT_BLUEZ) << "accept() failed:" << qt_error_string(errno);
3090 serverSocketNotifier->setEnabled(true);
3091 return;
3092 }
3093
3094 remoteDevice = QBluetoothAddress(convertAddress(clientAddr.l2_bdaddr.b));
3095 remoteName = nameOfRemoteCentral(remoteDevice);
3096 qCDebug(QT_BT_BLUEZ) << "GATT connection from device" << remoteDevice << remoteName;
3097
3098 if (connectionHandle == 0)
3099 qCWarning(QT_BT_BLUEZ) << "Received client connection, but no connection complete event";
3100
3101 if (l2cpSocket) {
3102 disconnect(l2cpSocket);
3103 if (l2cpSocket->isOpen())
3104 l2cpSocket->close();
3105
3106 l2cpSocket->deleteLater();
3107 l2cpSocket = nullptr;
3108 }
3109 closeServerSocket();
3110
3111 QBluetoothSocketPrivateBluez *rawSocketPrivate = new QBluetoothSocketPrivateBluez(nullptr);
3112 l2cpSocket = new QBluetoothSocket(
3113 rawSocketPrivate, QBluetoothServiceInfo::L2capProtocol, this);
3114 connect(l2cpSocket, &QBluetoothSocket::disconnected,
3115 this, &QLowEnergyControllerPrivateBluez::l2cpDisconnected);
3116 connect(l2cpSocket, &QBluetoothSocket::errorOccurred, this,
3117 &QLowEnergyControllerPrivateBluez::l2cpErrorChanged);
3118 connect(l2cpSocket, &QIODevice::readyRead, this, &QLowEnergyControllerPrivateBluez::l2cpReadyRead);
3119 l2cpSocket->d_ptr->lowEnergySocketType = addressType == QLowEnergyController::PublicAddress
3121 l2cpSocket->setSocketDescriptor(clientSocket, QBluetoothServiceInfo::L2capProtocol,
3122 QBluetoothSocket::SocketState::ConnectedState, QIODevice::ReadWrite | QIODevice::Unbuffered);
3123 restoreClientConfigurations();
3124 loadSigningDataIfNecessary(RemoteSigningKey);
3125
3126 Q_Q(QLowEnergyController);
3127 setState(QLowEnergyController::ConnectedState);
3128 emit q->connected();
3129}
3130
3131void QLowEnergyControllerPrivateBluez::closeServerSocket()
3132{
3133 if (!serverSocketNotifier)
3134 return;
3135 serverSocketNotifier->disconnect();
3136 close(serverSocketNotifier->socket());
3137 serverSocketNotifier->deleteLater();
3138 serverSocketNotifier = nullptr;
3139}
3140
3141bool QLowEnergyControllerPrivateBluez::isBonded() const
3142{
3143 // Pairing does not necessarily imply bonding, but we don't know whether the
3144 // bonding flag was set in the original pairing request.
3145 return QBluetoothLocalDevice(localAdapter).pairingStatus(remoteDevice)
3146 != QBluetoothLocalDevice::Unpaired;
3147}
3148
3149QList<QLowEnergyControllerPrivateBluez::TempClientConfigurationData>
3150QLowEnergyControllerPrivateBluez::gatherClientConfigData()
3151{
3152 QList<TempClientConfigurationData> data;
3153 for (const auto &service : std::as_const(localServices)) {
3154 for (auto charIt = service->characteristicList.begin();
3155 charIt != service->characteristicList.end(); ++charIt) {
3156 QLowEnergyServicePrivate::CharData &charData = charIt.value();
3157 for (auto descIt = charData.descriptorList.begin();
3158 descIt != charData.descriptorList.end(); ++descIt) {
3159 QLowEnergyServicePrivate::DescData &descData = descIt.value();
3160 if (descData.uuid == QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration) {
3161 data << TempClientConfigurationData(&descData, charData.valueHandle,
3162 descIt.key());
3163 break;
3164 }
3165 }
3166 }
3167 }
3168 return data;
3169}
3170
3171void QLowEnergyControllerPrivateBluez::storeClientConfigurations()
3172{
3173 if (!isBonded()) {
3174 clientConfigData.remove(remoteDevice.toUInt64());
3175 return;
3176 }
3177 QList<ClientConfigurationData> clientConfigs;
3178 const QList<TempClientConfigurationData> &tempConfigList = gatherClientConfigData();
3179 for (const auto &tempConfigData : tempConfigList) {
3180 Q_ASSERT(tempConfigData.descData->value.size() == 2);
3181 const quint16 value = bt_get_le16(tempConfigData.descData->value.constData());
3182 if (value != 0) {
3183 clientConfigs << ClientConfigurationData(tempConfigData.charValueHandle,
3184 tempConfigData.configHandle, value);
3185 }
3186 }
3187 clientConfigData.insert(remoteDevice.toUInt64(), clientConfigs);
3188}
3189
3190void QLowEnergyControllerPrivateBluez::restoreClientConfigurations()
3191{
3192 const QList<TempClientConfigurationData> &tempConfigList = gatherClientConfigData();
3193 const QList<ClientConfigurationData> &restoredClientConfigs = isBonded()
3194 ? clientConfigData.value(remoteDevice.toUInt64())
3195 : QList<ClientConfigurationData>();
3196 QList<QLowEnergyHandle> notifications;
3197 for (const auto &tempConfigData : tempConfigList) {
3198 bool wasRestored = false;
3199 for (const auto &restoredData : restoredClientConfigs) {
3200 if (restoredData.charValueHandle == tempConfigData.charValueHandle) {
3201 Q_ASSERT(tempConfigData.descData->value.size() == 2);
3202 putBtData(restoredData.configValue, tempConfigData.descData->value.data());
3203 wasRestored = true;
3204 if (restoredData.charValueWasUpdated) {
3205 if (isNotificationEnabled(restoredData.configValue))
3206 notifications << restoredData.charValueHandle;
3207 else if (isIndicationEnabled(restoredData.configValue))
3208 scheduledIndications << restoredData.charValueHandle;
3209 }
3210 break;
3211 }
3212 }
3213 if (!wasRestored)
3214 tempConfigData.descData->value = QByteArray(2, 0); // Default value.
3215 Q_ASSERT(lastLocalHandle >= tempConfigData.configHandle);
3216 Q_ASSERT(tempConfigData.configHandle > tempConfigData.charValueHandle);
3217 localAttributes[tempConfigData.configHandle].value = tempConfigData.descData->value;
3218 }
3219
3220 for (const QLowEnergyHandle handle : std::as_const(notifications))
3221 sendNotification(handle);
3222 sendNextIndication();
3223}
3224
3225void QLowEnergyControllerPrivateBluez::loadSigningDataIfNecessary(SigningKeyType keyType)
3226{
3227 const auto signingDataIt = signingData.constFind(remoteDevice.toUInt64());
3228 if (signingDataIt != signingData.constEnd())
3229 return; // We are up to date for this device.
3230 const QString settingsFilePath = keySettingsFilePath();
3231 if (!QFileInfo(settingsFilePath).exists()) {
3232 qCDebug(QT_BT_BLUEZ) << "No settings found for peer device.";
3233 return;
3234 }
3235 QSettings settings(settingsFilePath, QSettings::IniFormat);
3236 const QString group = signingKeySettingsGroup(keyType);
3237 settings.beginGroup(group);
3238 const QByteArray keyString = settings.value(QLatin1String("Key")).toByteArray();
3239 if (keyString.isEmpty()) {
3240 qCDebug(QT_BT_BLUEZ) << "Group" << group << "not found in settings file";
3241 return;
3242 }
3243 const QByteArray keyData = QByteArray::fromHex(keyString);
3244 if (keyData.size() != qsizetype(sizeof(BluezUint128))) {
3245 qCWarning(QT_BT_BLUEZ) << "Signing key in settings file has invalid size"
3246 << keyString.size();
3247 return;
3248 }
3249 const quint32 counter = settings.value(QLatin1String("Counter"), 0).toUInt();
3250 using namespace std;
3251 BluezUint128 csrk;
3252 memcpy(csrk.data, keyData.constData(), keyData.size());
3253 signingData.insert(remoteDevice.toUInt64(), SigningData(csrk, counter - 1));
3254}
3255
3256void QLowEnergyControllerPrivateBluez::storeSignCounter(SigningKeyType keyType) const
3257{
3258 const auto signingDataIt = signingData.constFind(remoteDevice.toUInt64());
3259 if (signingDataIt == signingData.constEnd())
3260 return;
3261 const QString settingsFilePath = keySettingsFilePath();
3262 if (!QFileInfo(settingsFilePath).exists())
3263 return;
3264 QSettings settings(settingsFilePath, QSettings::IniFormat);
3265 if (!settings.isWritable())
3266 return;
3267 settings.beginGroup(signingKeySettingsGroup(keyType));
3268 const QString counterKey = QLatin1String("Counter");
3269 if (!settings.allKeys().contains(counterKey))
3270 return;
3271 const quint32 counterValue = signingDataIt.value().counter + 1;
3272 if (counterValue == settings.value(counterKey).toUInt())
3273 return;
3274 settings.setValue(counterKey, counterValue);
3275}
3276
3277QString QLowEnergyControllerPrivateBluez::signingKeySettingsGroup(SigningKeyType keyType) const
3278{
3279 return QLatin1String(keyType == LocalSigningKey ? "LocalSignatureKey" : "RemoteSignatureKey");
3280}
3281
3282QString QLowEnergyControllerPrivateBluez::keySettingsFilePath() const
3283{
3284 return QString::fromLatin1("/var/lib/bluetooth/%1/%2/info")
3285 .arg(localAdapter.toString(), remoteDevice.toString());
3286}
3287
3288static QByteArray uuidToByteArray(const QBluetoothUuid &uuid)
3289{
3290 QByteArray ba(sizeof(uuid), Qt::Uninitialized);
3291 char *ptr = ba.data();
3292 putDataAndIncrement(uuid, ptr);
3293 ba.resize(ptr - ba.constData());
3294 return ba;
3295}
3296
3297void QLowEnergyControllerPrivateBluez::addToGenericAttributeList(const QLowEnergyServiceData &service,
3298 QLowEnergyHandle startHandle)
3299{
3300 // Construct generic attribute data for the service with handles as keys.
3301 // Otherwise a number of request handling functions will be awkward to write
3302 // as well as computationally inefficient.
3303
3304 localAttributes.resize(lastLocalHandle + 1);
3305 Attribute serviceAttribute;
3306 serviceAttribute.handle = startHandle;
3307 serviceAttribute.type = QBluetoothUuid(static_cast<quint16>(service.type()));
3308 serviceAttribute.properties = QLowEnergyCharacteristic::Read;
3309 serviceAttribute.value = uuidToByteArray(service.uuid());
3310 QLowEnergyHandle currentHandle = startHandle;
3311 const QList<QLowEnergyService *> includedServices = service.includedServices();
3312 for (const QLowEnergyService * const service : includedServices) {
3313 Attribute attribute;
3314 attribute.handle = ++currentHandle;
3315 attribute.type = QBluetoothUuid(GATT_INCLUDED_SERVICE);
3316 attribute.properties = QLowEnergyCharacteristic::Read;
3317 const bool includeUuidInValue = service->serviceUuid().minimumSize() == 2;
3318 attribute.value.resize((2 + includeUuidInValue) * sizeof(QLowEnergyHandle));
3319 char *valueData = attribute.value.data();
3320 putDataAndIncrement(service->d_ptr->startHandle, valueData);
3321 putDataAndIncrement(service->d_ptr->endHandle, valueData);
3322 if (includeUuidInValue)
3323 putDataAndIncrement(service->serviceUuid(), valueData);
3324 localAttributes[attribute.handle] = attribute;
3325 }
3326 const QList<QLowEnergyCharacteristicData> characteristics = service.characteristics();
3327 for (const QLowEnergyCharacteristicData &cd : characteristics) {
3328 Attribute attribute;
3329
3330 // Characteristic declaration;
3331 attribute.handle = ++currentHandle;
3332 attribute.groupEndHandle = attribute.handle + 1 + cd.descriptors().size();
3333 attribute.type = QBluetoothUuid(GATT_CHARACTERISTIC);
3334 attribute.properties = QLowEnergyCharacteristic::Read;
3335 attribute.value.resize(1 + sizeof(QLowEnergyHandle) + cd.uuid().minimumSize());
3336 char *valueData = attribute.value.data();
3337 putDataAndIncrement(static_cast<quint8>(cd.properties()), valueData);
3338 putDataAndIncrement(QLowEnergyHandle(currentHandle + 1), valueData);
3339 putDataAndIncrement(cd.uuid(), valueData);
3340 localAttributes[attribute.handle] = attribute;
3341
3342 // Characteristic value declaration.
3343 attribute.handle = ++currentHandle;
3344 attribute.groupEndHandle = attribute.handle;
3345 attribute.type = cd.uuid();
3346 attribute.properties = cd.properties();
3347 attribute.readConstraints = cd.readConstraints();
3348 attribute.writeConstraints = cd.writeConstraints();
3349 attribute.value = cd.value();
3350 attribute.minLength = cd.minimumValueLength();
3351 attribute.maxLength = cd.maximumValueLength();
3352 localAttributes[attribute.handle] = attribute;
3353
3354 const QList<QLowEnergyDescriptorData> descriptors = cd.descriptors();
3355 for (const QLowEnergyDescriptorData &dd : descriptors) {
3356 attribute.handle = ++currentHandle;
3357 attribute.groupEndHandle = attribute.handle;
3358 attribute.type = dd.uuid();
3359 attribute.properties = QLowEnergyCharacteristic::PropertyTypes();
3360 attribute.readConstraints = AttAccessConstraints();
3361 attribute.writeConstraints = AttAccessConstraints();
3362 attribute.minLength = 0;
3363 attribute.maxLength = INT_MAX;
3364
3365 // Spec v4.2, Vol. 3, Part G, 3.3.3.x
3366 if (attribute.type == QBluetoothUuid::DescriptorType::CharacteristicExtendedProperties) {
3367 attribute.properties = QLowEnergyCharacteristic::Read;
3368 attribute.minLength = attribute.maxLength = 2;
3369 } else if (attribute.type == QBluetoothUuid::DescriptorType::CharacteristicPresentationFormat) {
3370 attribute.properties = QLowEnergyCharacteristic::Read;
3371 attribute.minLength = attribute.maxLength = 7;
3372 } else if (attribute.type == QBluetoothUuid::DescriptorType::CharacteristicAggregateFormat) {
3373 attribute.properties = QLowEnergyCharacteristic::Read;
3374 attribute.minLength = 4;
3375 } else if (attribute.type == QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration
3376 || attribute.type == QBluetoothUuid::DescriptorType::ServerCharacteristicConfiguration) {
3377 attribute.properties = QLowEnergyCharacteristic::Read
3378 | QLowEnergyCharacteristic::Write
3379 | QLowEnergyCharacteristic::WriteNoResponse
3380 | QLowEnergyCharacteristic::WriteSigned;
3381 attribute.writeConstraints = dd.writeConstraints();
3382 attribute.minLength = attribute.maxLength = 2;
3383 } else {
3384 if (dd.isReadable())
3385 attribute.properties |= QLowEnergyCharacteristic::Read;
3386 if (dd.isWritable()) {
3387 attribute.properties |= QLowEnergyCharacteristic::Write;
3388 attribute.properties |= QLowEnergyCharacteristic::WriteNoResponse;
3389 attribute.properties |= QLowEnergyCharacteristic::WriteSigned;
3390 }
3391 attribute.readConstraints = dd.readConstraints();
3392 attribute.writeConstraints = dd.writeConstraints();
3393 }
3394
3395 attribute.value = dd.value();
3396 if (attribute.value.size() < attribute.minLength
3397 || attribute.value.size() > attribute.maxLength) {
3398 qCWarning(QT_BT_BLUEZ) << "attribute of type" << attribute.type
3399 << "has invalid length of" << attribute.value.size()
3400 << "bytes";
3401 attribute.value = QByteArray(attribute.minLength, 0);
3402 }
3403 localAttributes[attribute.handle] = attribute;
3404 }
3405 }
3406 serviceAttribute.groupEndHandle = currentHandle;
3407 localAttributes[serviceAttribute.handle] = serviceAttribute;
3408}
3409
3410int QLowEnergyControllerPrivateBluez::mtu() const
3411{
3412 return mtuSize;
3413}
3414
3415void QLowEnergyControllerPrivateBluez::ensureUniformAttributes(
3416 QList<Attribute> &attributes, const std::function<int(const Attribute &)> &getSize)
3417{
3418 if (attributes.isEmpty())
3419 return;
3420 const int firstSize = getSize(attributes.first());
3421 const auto it = std::find_if(attributes.begin() + 1, attributes.end(),
3422 [firstSize, getSize](const Attribute &attr) { return getSize(attr) != firstSize; });
3423 if (it != attributes.end())
3424 attributes.erase(it, attributes.end());
3425
3426}
3427
3428void QLowEnergyControllerPrivateBluez::ensureUniformUuidSizes(QList<Attribute> &attributes)
3429{
3430 ensureUniformAttributes(attributes,
3431 [](const Attribute &attr) { return getUuidSize(attr.type); });
3432}
3433
3434void QLowEnergyControllerPrivateBluez::ensureUniformValueSizes(QList<Attribute> &attributes)
3435{
3436 ensureUniformAttributes(attributes,
3437 [](const Attribute &attr) { return attr.value.size(); });
3438}
3439
3440QList<QLowEnergyControllerPrivateBluez::Attribute>
3441QLowEnergyControllerPrivateBluez::getAttributes(QLowEnergyHandle startHandle,
3442 QLowEnergyHandle endHandle,
3443 const AttributePredicate &attributePredicate)
3444{
3445 QList<Attribute> results;
3446 if (startHandle > lastLocalHandle)
3447 return results;
3448 if (lastLocalHandle == 0) // We have no services at all.
3449 return results;
3450 Q_ASSERT(startHandle <= endHandle); // Must have been checked before.
3451 const QLowEnergyHandle firstHandle = qMin(startHandle, lastLocalHandle);
3452 const QLowEnergyHandle lastHandle = qMin(endHandle, lastLocalHandle);
3453 for (QLowEnergyHandle i = firstHandle; i <= lastHandle; ++i) {
3454 const Attribute &attr = localAttributes.at(i);
3455 if (attributePredicate(attr))
3456 results << attr;
3457 }
3458 return results;
3459}
3460
3462QLowEnergyControllerPrivateBluez::checkPermissions(const Attribute &attr,
3463 QLowEnergyCharacteristic::PropertyType type)
3464{
3465 const bool isReadAccess = type == QLowEnergyCharacteristic::Read;
3466 const bool isWriteCommand = type == QLowEnergyCharacteristic::WriteNoResponse;
3467 const bool isWriteAccess = type == QLowEnergyCharacteristic::Write
3468 || type == QLowEnergyCharacteristic::WriteSigned
3469 || isWriteCommand;
3470 Q_ASSERT(isReadAccess || isWriteAccess);
3471 if (!(attr.properties & type)) {
3472 if (isReadAccess)
3474
3475 // The spec says: If an attribute requires a signed write, then a non-signed write command
3476 // can also be used if the link is encrypted.
3477 const bool unsignedWriteOk = isWriteCommand
3478 && (attr.properties & QLowEnergyCharacteristic::WriteSigned)
3479 && securityLevel() >= BT_SECURITY_MEDIUM;
3480 if (!unsignedWriteOk)
3482 }
3483 const AttAccessConstraints constraints = isReadAccess
3484 ? attr.readConstraints : attr.writeConstraints;
3485 if (constraints.testFlag(AttAccessConstraint::AttAuthorizationRequired))
3486 return QBluezConst::AttError::ATT_ERROR_INSUF_AUTHORIZATION; // TODO: emit signal (and offer
3487 // authorization function)?
3488 if (constraints.testFlag(AttAccessConstraint::AttEncryptionRequired)
3489 && securityLevel() < BT_SECURITY_MEDIUM)
3491 if (constraints.testFlag(AttAccessConstraint::AttAuthenticationRequired)
3492 && securityLevel() < BT_SECURITY_HIGH)
3494 if (false)
3497}
3498
3499QBluezConst::AttError QLowEnergyControllerPrivateBluez::checkReadPermissions(const Attribute &attr)
3500{
3501 return checkPermissions(attr, QLowEnergyCharacteristic::Read);
3502}
3503
3505QLowEnergyControllerPrivateBluez::checkReadPermissions(QList<Attribute> &attributes)
3506{
3507 if (attributes.isEmpty())
3509
3510 // The logic prescribed in the spec is as follows:
3511 // 1) If the first in a list of matching attributes has a permissions error,
3512 // then that error is returned via an error response.
3513 // 2) If any other element of that list would cause a permissions error, then all
3514 // attributes from this one on are not part of the result set, but no error is returned.
3515 const QBluezConst::AttError error = checkReadPermissions(attributes.first());
3517 return error;
3518 const auto it =
3519 std::find_if(attributes.begin() + 1, attributes.end(), [this](const Attribute &attr) {
3520 return checkReadPermissions(attr) != QBluezConst::AttError::ATT_ERROR_NO_ERROR;
3521 });
3522 if (it != attributes.end())
3523 attributes.erase(it, attributes.end());
3525}
3526
3527bool QLowEnergyControllerPrivateBluez::verifyMac(const QByteArray &message, BluezUint128 csrk,
3528 quint32 signCounter, quint64 expectedMac)
3529{
3530 if (!cmacCalculator)
3531 cmacCalculator = new LeCmacCalculator;
3532 return cmacCalculator->verify(LeCmacCalculator::createFullMessage(message, signCounter), csrk,
3533 expectedMac);
3534}
3535
3536QT_END_NAMESPACE
3537
3538#include "moc_qlowenergycontroller_bluez_p.cpp"
QMap< QDBusObjectPath, InterfaceList > ManagedObjectList
#define htobs(d)
#define L2CAP_LM_ENCRYPT
#define BT_SECURITY_LOW
#define BT_SECURITY
#define SOL_L2CAP
#define SOL_BLUETOOTH
#define L2CAP_LM
#define BT_SECURITY_SDP
QUuid::Id128Bytes BluezUint128
#define L2CAP_LM_AUTH
#define ATTRIBUTE_CHANNEL_ID
#define BT_SECURITY_MEDIUM
#define BTPROTO_L2CAP
#define L2CAP_LM_SECURE
#define BDADDR_LE_PUBLIC
#define BT_SECURITY_HIGH
#define BDADDR_LE_RANDOM
static BluetoothManagement * instance()
QBluetoothSocketPrivateBluez(QBluetoothSocket *qq)
void discoverServiceDetails(const QBluetoothUuid &service, QLowEnergyService::DiscoveryMode mode) override
void readCharacteristic(const QSharedPointer< QLowEnergyServicePrivate > service, const QLowEnergyHandle charHandle) override
void writeCharacteristic(const QSharedPointer< QLowEnergyServicePrivate > service, const QLowEnergyHandle charHandle, const QByteArray &newValue, QLowEnergyService::WriteMode mode) override
Writes long (prepare write request), short (write request) and writeWithoutResponse characteristic va...
void writeDescriptor(const QSharedPointer< QLowEnergyServicePrivate > service, const QLowEnergyHandle charHandle, const QLowEnergyHandle descriptorHandle, const QByteArray &newValue) override
void startAdvertising(const QLowEnergyAdvertisingParameters &params, const QLowEnergyAdvertisingData &advertisingData, const QLowEnergyAdvertisingData &scanResponseData) override
void readDescriptor(const QSharedPointer< QLowEnergyServicePrivate > service, const QLowEnergyHandle charHandle, const QLowEnergyHandle descriptorHandle) override
void addToGenericAttributeList(const QLowEnergyServiceData &service, QLowEnergyHandle startHandle) override
void setController(QLowEnergyControllerPrivate *control)
bool listen(const QBluetoothAddress &localAdapter)
void registerQLowEnergyControllerMetaType()
void putDataAndIncrement(const QBluetoothUuid &uuid, char *&dst)
#define GATT_CHARACTERISTIC
#define GATT_SECONDARY_SERVICE
static bool isNotificationEnabled(quint16 clientConfigValue)
QLowEnergyHandle parseReadByTypeIncludeDiscovery(QList< QBluetoothUuid > *foundServices, const char *data, quint16 elementLength)
#define GATT_PRIMARY_SERVICE
static int getUuidSize(const QBluetoothUuid &uuid)
#define APPEND_VALUE
#define WRITE_REQUEST_HEADER_SIZE
static Q_DECL_COLD_FUNCTION void reportMalformedData(QBluezConst::AttCommand cmd, const QByteArray &response)
constexpr quint16 ATT_MAX_LE_MTU
QLowEnergyHandle parseReadByTypeCharDiscovery(QLowEnergyServicePrivate::CharData *charData, const char *data, quint16 elementLength)
#define GRP_TYPE_REQ_HEADER_SIZE
static bool dumpErrorInformation(const QByteArray &response)
#define READ_REQUEST_HEADER_SIZE
#define READ_BY_TYPE_REQ_HEADER_SIZE
#define EXECUTE_WRITE_HEADER_SIZE
const int maxPrepareQueueSize
static QString nameOfRemoteCentral(const QBluetoothAddress &peerAddress)
#define PREPARE_WRITE_HEADER_SIZE
#define GATT_INCLUDED_SERVICE
#define READ_BLOB_REQUEST_HEADER_SIZE
static void putDataAndIncrement(const T &src, char *&dst)
static bool isIndicationEnabled(quint16 clientConfigValue)
#define MTU_EXCHANGE_HEADER_SIZE
#define FIND_INFO_REQUEST_HEADER_SIZE
#define NEW_VALUE
#define ERROR_RESPONSE_HEADER_SIZE
static QByteArray uuidToByteArray(const QBluetoothUuid &uuid)
constexpr quint16 ATT_DEFAULT_LE_MTU
unsigned short l2_cid