1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtBluetooth module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QBLUETOOTHSERVICEDISCOVERYAGENT_P_H
41#define QBLUETOOTHSERVICEDISCOVERYAGENT_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include "qbluetoothaddress.h"
55#include "qbluetoothdeviceinfo.h"
56#include "qbluetoothserviceinfo.h"
57#include "qbluetoothservicediscoveryagent.h"
58
59#include <QStack>
60#include <QStringList>
61
62#if QT_CONFIG(bluez)
63class OrgBluezManagerInterface;
64class OrgBluezAdapterInterface;
65class OrgBluezDeviceInterface;
66class OrgFreedesktopDBusObjectManagerInterface;
67#include <QtCore/qprocess.h>
68
69QT_BEGIN_NAMESPACE
70class QDBusPendingCallWatcher;
71class QXmlStreamReader;
72QT_END_NAMESPACE
73#endif
74
75#ifdef QT_WIN_BLUETOOTH
76#include <QtCore/QVariant>
77
78QT_BEGIN_NAMESPACE
79class QThread;
80
81class ThreadWorkerFind : public QObject
82{
83 Q_OBJECT
84signals:
85 void findFinished(QVariant result);
86};
87QT_END_NAMESPACE
88
89#elif defined(QT_WINRT_BLUETOOTH)
90#include <QtCore/QPointer>
91#endif
92
93#ifdef QT_OSX_BLUETOOTH
94#include "osx/btdelegates_p.h"
95#include "osx/btraii_p.h"
96#endif
97
98QT_BEGIN_NAMESPACE
99
100class QBluetoothDeviceDiscoveryAgent;
101#ifdef QT_ANDROID_BLUETOOTH
102class ServiceDiscoveryBroadcastReceiver;
103class LocalDeviceBroadcastReceiver;
104#include <QtAndroidExtras/QAndroidJniObject>
105#include <QtBluetooth/QBluetoothLocalDevice>
106#endif
107
108#ifdef QT_WINRT_BLUETOOTH
109class QWinRTBluetoothServiceDiscoveryWorker;
110#endif
111
112class QBluetoothServiceDiscoveryAgentPrivate
113#if defined QT_WINRT_BLUETOOTH || defined QT_WIN_BLUETOOTH
114 : public QObject
115{
116 Q_OBJECT
117#elif defined(QT_OSX_BLUETOOTH)
118 : public QObject, public DarwinBluetooth::SDPInquiryDelegate
119{
120#else
121{
122#endif
123 Q_DECLARE_PUBLIC(QBluetoothServiceDiscoveryAgent)
124
125public:
126 enum DiscoveryState {
127 Inactive,
128 DeviceDiscovery,
129 ServiceDiscovery,
130 };
131
132 QBluetoothServiceDiscoveryAgentPrivate(QBluetoothServiceDiscoveryAgent *qp,
133 const QBluetoothAddress &deviceAdapter);
134 ~QBluetoothServiceDiscoveryAgentPrivate();
135
136 void startDeviceDiscovery();
137 void stopDeviceDiscovery();
138 void startServiceDiscovery();
139 void stopServiceDiscovery();
140
141 void setDiscoveryState(DiscoveryState s) { state = s; }
142 inline DiscoveryState discoveryState() { return state; }
143
144 void setDiscoveryMode(QBluetoothServiceDiscoveryAgent::DiscoveryMode m) { mode = m; }
145 QBluetoothServiceDiscoveryAgent::DiscoveryMode DiscoveryMode() { return mode; }
146
147 void _q_deviceDiscoveryFinished();
148 void _q_deviceDiscovered(const QBluetoothDeviceInfo &info);
149 void _q_serviceDiscoveryFinished();
150 void _q_deviceDiscoveryError(QBluetoothDeviceDiscoveryAgent::Error);
151#if QT_CONFIG(bluez)
152 void _q_discoveredServices(QDBusPendingCallWatcher *watcher);
153 void _q_createdDevice(QDBusPendingCallWatcher *watcher);
154 void _q_foundDevice(QDBusPendingCallWatcher *watcher);
155 //Slots below are used for discovering Bluetooth Low Energy devices. It will be used with Bluez 5.x version.
156 /*
157 void _g_discoveredGattService();
158 void _q_discoverGattCharacteristics(QDBusPendingCallWatcher *watcher);
159 void _q_discoveredGattCharacteristic(QDBusPendingCallWatcher *watcher);
160 */
161 void _q_sdpScannerDone(int exitCode, QProcess::ExitStatus status);
162 void _q_finishSdpScan(QBluetoothServiceDiscoveryAgent::Error errorCode,
163 const QString &errorDescription,
164 const QStringList &xmlRecords);
165#endif
166#ifdef QT_ANDROID_BLUETOOTH
167 void _q_processFetchedUuids(const QBluetoothAddress &address, const QList<QBluetoothUuid> &uuids);
168
169 void populateDiscoveredServices(const QBluetoothDeviceInfo &remoteDevice,
170 const QList<QBluetoothUuid> &uuids);
171 void _q_fetchUuidsTimeout();
172 void _q_hostModeStateChanged(QBluetoothLocalDevice::HostMode state);
173#endif
174#ifdef QT_WIN_BLUETOOTH
175 void _q_nextSdpScan(const QVariant &input);
176 bool serviceMatches(const QBluetoothServiceInfo &info);
177#endif
178
179private:
180 void start(const QBluetoothAddress &address);
181 void stop();
182 bool isDuplicatedService(const QBluetoothServiceInfo &serviceInfo) const;
183
184#if QT_CONFIG(bluez)
185 void startBluez5(const QBluetoothAddress &address);
186 void runExternalSdpScan(const QBluetoothAddress &remoteAddress,
187 const QBluetoothAddress &localAddress);
188 void sdpScannerDone(int exitCode, QProcess::ExitStatus exitStatus);
189 QVariant readAttributeValue(QXmlStreamReader &xml);
190 QBluetoothServiceInfo parseServiceXml(const QString& xml);
191 void performMinimalServiceDiscovery(const QBluetoothAddress &deviceAddress);
192 void discoverServices(const QString &deviceObjectPath);
193#endif
194
195public:
196 QBluetoothServiceDiscoveryAgent::Error error;
197 QString errorString;
198 QBluetoothAddress deviceAddress;
199 QList<QBluetoothServiceInfo> discoveredServices;
200 QList<QBluetoothDeviceInfo> discoveredDevices;
201 QBluetoothAddress m_deviceAdapterAddress;
202
203private:
204 DiscoveryState state;
205 QList<QBluetoothUuid> uuidFilter;
206
207 QBluetoothDeviceDiscoveryAgent *deviceDiscoveryAgent = nullptr;
208
209 QBluetoothServiceDiscoveryAgent::DiscoveryMode mode;
210
211 bool singleDevice;
212#if QT_CONFIG(bluez)
213 QString foundHostAdapterPath;
214 OrgBluezManagerInterface *manager = nullptr;
215 OrgFreedesktopDBusObjectManagerInterface *managerBluez5 = nullptr;
216 OrgBluezAdapterInterface *adapter = nullptr;
217 OrgBluezDeviceInterface *device = nullptr;
218 QProcess *sdpScannerProcess = nullptr;
219#endif
220
221#ifdef QT_ANDROID_BLUETOOTH
222 ServiceDiscoveryBroadcastReceiver *receiver = nullptr;
223 LocalDeviceBroadcastReceiver *localDeviceReceiver = nullptr;
224
225 QAndroidJniObject btAdapter;
226 QMap<QBluetoothAddress,QPair<QBluetoothDeviceInfo,QList<QBluetoothUuid> > > sdpCache;
227#endif
228
229#ifdef QT_WIN_BLUETOOTH
230private:
231 bool pendingStop;
232 bool pendingFinish;
233
234 QThread *threadFind = nullptr;
235 ThreadWorkerFind *threadWorkerFind = nullptr;
236#endif
237
238#ifdef QT_WINRT_BLUETOOTH
239private slots:
240 void processFoundService(quint64 deviceAddress, const QBluetoothServiceInfo &info);
241 void onScanFinished(quint64 deviceAddress);
242 void onScanCanceled();
243 void onError();
244
245private:
246 QPointer<QWinRTBluetoothServiceDiscoveryWorker> worker;
247#endif
248
249#ifdef QT_OSX_BLUETOOTH
250 // SDPInquiryDelegate:
251 void SDPInquiryFinished(void *device) override;
252 void SDPInquiryError(void *device, IOReturn errorCode) override;
253
254 void performMinimalServiceDiscovery(const QBluetoothAddress &deviceAddress);
255 //void serviceDiscoveryFinished();
256
257 bool serviceHasMatchingUuid(const QBluetoothServiceInfo &serviceInfo) const;
258
259 DarwinBluetooth::ScopedPointer serviceInquiry;
260#endif // QT_OSX_BLUETOOTH
261
262protected:
263 QBluetoothServiceDiscoveryAgent *q_ptr;
264};
265
266QT_END_NAMESPACE
267
268#endif
269

source code of qtconnectivity/src/bluetooth/qbluetoothservicediscoveryagent_p.h