1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Copyright (C) 2014 Denis Shienkov <denis.shienkov@gmail.com> |
5 | ** Contact: https://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the QtBluetooth module of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:LGPL$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at https://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU Lesser General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
22 | ** packaging of this file. Please review the following information to |
23 | ** ensure the GNU Lesser General Public License version 3 requirements |
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
25 | ** |
26 | ** GNU General Public License Usage |
27 | ** Alternatively, this file may be used under the terms of the GNU |
28 | ** General Public License version 2.0 or (at your option) the GNU General |
29 | ** Public license version 3 or any later version approved by the KDE Free |
30 | ** Qt Foundation. The licenses are as published by the Free Software |
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
32 | ** included in the packaging of this file. Please review the following |
33 | ** information to ensure the GNU General Public License requirements will |
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
36 | ** |
37 | ** $QT_END_LICENSE$ |
38 | ** |
39 | ****************************************************************************/ |
40 | |
41 | #ifndef QBLUETOOTHLOCALDEVICE_P_H |
42 | #define QBLUETOOTHLOCALDEVICE_P_H |
43 | |
44 | // |
45 | // W A R N I N G |
46 | // ------------- |
47 | // |
48 | // This file is not part of the Qt API. It exists purely as an |
49 | // implementation detail. This header file may change from version to |
50 | // version without notice, or even be removed. |
51 | // |
52 | // We mean it. |
53 | // |
54 | |
55 | #include <QtBluetooth/qtbluetoothglobal.h> |
56 | |
57 | #include "qbluetoothlocaldevice.h" |
58 | |
59 | #if QT_CONFIG(bluez) |
60 | #include <QObject> |
61 | #include <QDBusContext> |
62 | #include <QDBusObjectPath> |
63 | #include <QDBusMessage> |
64 | #include <QSet> |
65 | #include "bluez/bluez5_helper_p.h" |
66 | |
67 | class OrgBluezAdapterInterface; |
68 | class OrgBluezAdapter1Interface; |
69 | class OrgFreedesktopDBusPropertiesInterface; |
70 | class OrgFreedesktopDBusObjectManagerInterface; |
71 | class OrgBluezAgentAdaptor; |
72 | class OrgBluezDeviceInterface; |
73 | class OrgBluezDevice1Interface; |
74 | class OrgBluezManagerInterface; |
75 | |
76 | QT_BEGIN_NAMESPACE |
77 | class QDBusPendingCallWatcher; |
78 | QT_END_NAMESPACE |
79 | #endif |
80 | |
81 | #ifdef QT_ANDROID_BLUETOOTH |
82 | #include <jni.h> |
83 | #include <QtAndroidExtras/QAndroidJniEnvironment> |
84 | #include <QtAndroidExtras/QAndroidJniObject> |
85 | #include <QtCore/QPair> |
86 | #endif |
87 | |
88 | #ifdef QT_WINRT_BLUETOOTH |
89 | #include <wrl.h> |
90 | |
91 | namespace ABI { |
92 | namespace Windows { |
93 | namespace Devices { |
94 | namespace Bluetooth { |
95 | struct IBluetoothDeviceStatics; |
96 | struct IBluetoothLEDeviceStatics; |
97 | } |
98 | } |
99 | } |
100 | } |
101 | #endif |
102 | |
103 | QT_BEGIN_NAMESPACE |
104 | |
105 | extern void registerQBluetoothLocalDeviceMetaType(); |
106 | |
107 | class QBluetoothAddress; |
108 | |
109 | #ifdef QT_ANDROID_BLUETOOTH |
110 | class LocalDeviceBroadcastReceiver; |
111 | class QBluetoothLocalDevicePrivate : public QObject |
112 | { |
113 | Q_OBJECT |
114 | public: |
115 | QBluetoothLocalDevicePrivate( |
116 | QBluetoothLocalDevice *q, const QBluetoothAddress &address = QBluetoothAddress()); |
117 | ~QBluetoothLocalDevicePrivate(); |
118 | |
119 | QAndroidJniObject *adapter(); |
120 | void initialize(const QBluetoothAddress &address); |
121 | static bool startDiscovery(); |
122 | static bool cancelDiscovery(); |
123 | static bool isDiscovering(); |
124 | bool isValid() const; |
125 | |
126 | private slots: |
127 | void processHostModeChange(QBluetoothLocalDevice::HostMode newMode); |
128 | void processPairingStateChanged(const QBluetoothAddress &address, |
129 | QBluetoothLocalDevice::Pairing pairing); |
130 | void processConnectDeviceChanges(const QBluetoothAddress &address, bool isConnectEvent); |
131 | void processDisplayConfirmation(const QBluetoothAddress &address, const QString &pin); |
132 | |
133 | private: |
134 | QBluetoothLocalDevice *q_ptr; |
135 | QAndroidJniObject *obj = nullptr; |
136 | |
137 | int pendingPairing(const QBluetoothAddress &address); |
138 | |
139 | public: |
140 | LocalDeviceBroadcastReceiver *receiver; |
141 | bool pendingHostModeTransition = false; |
142 | QList<QPair<QBluetoothAddress, bool> > pendingPairings; |
143 | |
144 | QList<QBluetoothAddress> connectedDevices; |
145 | }; |
146 | |
147 | #elif QT_CONFIG(bluez) |
148 | class QBluetoothLocalDevicePrivate : public QObject, protected QDBusContext |
149 | { |
150 | Q_OBJECT |
151 | Q_DECLARE_PUBLIC(QBluetoothLocalDevice) |
152 | public: |
153 | QBluetoothLocalDevicePrivate(QBluetoothLocalDevice *q, |
154 | QBluetoothAddress localAddress = QBluetoothAddress()); |
155 | ~QBluetoothLocalDevicePrivate(); |
156 | |
157 | QSet<OrgBluezDeviceInterface *> devices; |
158 | QSet<QBluetoothAddress> connectedDevicesSet; |
159 | OrgBluezAdapterInterface *adapter = nullptr; //Bluez 4 |
160 | OrgBluezAdapter1Interface *adapterBluez5 = nullptr; //Bluez 5 |
161 | OrgFreedesktopDBusPropertiesInterface *adapterProperties = nullptr; //Bluez 5 |
162 | OrgFreedesktopDBusObjectManagerInterface *managerBluez5 = nullptr; //Bluez 5 |
163 | QMap<QString, OrgFreedesktopDBusPropertiesInterface *> deviceChangeMonitors; //Bluez 5 |
164 | OrgBluezAgentAdaptor *agent = nullptr; |
165 | OrgBluezManagerInterface *manager = nullptr; |
166 | |
167 | QList<QBluetoothAddress> connectedDevices() const; |
168 | |
169 | QString agent_path; |
170 | QBluetoothAddress localAddress; |
171 | QBluetoothAddress address; |
172 | QBluetoothLocalDevice::Pairing pairing; |
173 | OrgBluezDevice1Interface *pairingTarget = nullptr; |
174 | QTimer *pairingDiscoveryTimer = nullptr; |
175 | QBluetoothLocalDevice::HostMode currentMode; |
176 | int pendingHostModeChange; |
177 | |
178 | public slots: |
179 | void Authorize(const QDBusObjectPath &in0, const QString &in1); |
180 | void Cancel(); |
181 | void ConfirmModeChange(const QString &in0); |
182 | void DisplayPasskey(const QDBusObjectPath &in0, uint in1, uchar in2); |
183 | void Release(); |
184 | uint RequestPasskey(const QDBusObjectPath &in0); |
185 | |
186 | void RequestConfirmation(const QDBusObjectPath &in0, uint in1); |
187 | QString RequestPinCode(const QDBusObjectPath &in0); |
188 | |
189 | void pairingCompleted(QDBusPendingCallWatcher *); |
190 | |
191 | void PropertyChanged(QString, QDBusVariant); |
192 | void _q_deviceCreated(const QDBusObjectPath &device); |
193 | void _q_deviceRemoved(const QDBusObjectPath &device); |
194 | void _q_devicePropertyChanged(const QString &property, const QDBusVariant &value); |
195 | bool isValid() const; |
196 | void adapterRemoved(const QDBusObjectPath &device); |
197 | |
198 | void requestPairingBluez5(const QBluetoothAddress &address, |
199 | QBluetoothLocalDevice::Pairing targetPairing); |
200 | |
201 | private Q_SLOTS: |
202 | void PropertiesChanged(const QString &interface, |
203 | const QVariantMap &changed_properties, |
204 | const QStringList &invalidated_properties, |
205 | const QDBusMessage &signal); |
206 | void InterfacesAdded(const QDBusObjectPath &object_path, |
207 | InterfaceList interfaces_and_properties); |
208 | void InterfacesRemoved(const QDBusObjectPath &object_path, |
209 | const QStringList &interfaces); |
210 | void processPairingBluez5(const QString &objectPath, |
211 | QBluetoothLocalDevice::Pairing target); |
212 | void pairingDiscoveryTimedOut(); |
213 | |
214 | private: |
215 | void createCache(); |
216 | void connectDeviceChanges(); |
217 | |
218 | QDBusMessage msgConfirmation; |
219 | QDBusConnection *msgConnection = nullptr; |
220 | QString deviceAdapterPath; |
221 | |
222 | QBluetoothLocalDevice *q_ptr; |
223 | |
224 | void initializeAdapter(); |
225 | void initializeAdapterBluez5(); |
226 | }; |
227 | |
228 | #elif defined(QT_WIN_BLUETOOTH) |
229 | |
230 | class QBluetoothLocalDevicePrivate : public QObject |
231 | { |
232 | Q_OBJECT |
233 | Q_DECLARE_PUBLIC(QBluetoothLocalDevice) |
234 | public: |
235 | QBluetoothLocalDevicePrivate(QBluetoothLocalDevice *q, |
236 | const QBluetoothAddress &address = QBluetoothAddress()); |
237 | |
238 | ~QBluetoothLocalDevicePrivate(); |
239 | bool isValid() const; |
240 | void initialize(const QBluetoothAddress &address); |
241 | |
242 | static QList<QBluetoothHostInfo> localAdapters(); |
243 | |
244 | QBluetoothAddress deviceAddress; |
245 | QString deviceName; |
246 | bool deviceValid; |
247 | private: |
248 | QBluetoothLocalDevice *q_ptr; |
249 | }; |
250 | #elif defined(QT_WINRT_BLUETOOTH) |
251 | class QBluetoothLocalDevicePrivate : public QObject |
252 | { |
253 | Q_DECLARE_PUBLIC(QBluetoothLocalDevice) |
254 | public: |
255 | QBluetoothLocalDevicePrivate(QBluetoothLocalDevice *q, |
256 | QBluetoothAddress = QBluetoothAddress()); |
257 | ~QBluetoothLocalDevicePrivate(); |
258 | |
259 | bool isValid() const; |
260 | |
261 | private: |
262 | QBluetoothLocalDevice *q_ptr; |
263 | Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::IBluetoothDeviceStatics> mStatics; |
264 | Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::IBluetoothLEDeviceStatics> mLEStatics; |
265 | }; |
266 | #elif !defined(QT_OSX_BLUETOOTH) // dummy backend |
267 | class QBluetoothLocalDevicePrivate : public QObject |
268 | { |
269 | public: |
270 | QBluetoothLocalDevicePrivate(QBluetoothLocalDevice * = nullptr, |
271 | QBluetoothAddress = QBluetoothAddress()) |
272 | { |
273 | } |
274 | |
275 | bool isValid() const |
276 | { |
277 | return false; |
278 | } |
279 | }; |
280 | #endif |
281 | |
282 | QT_END_NAMESPACE |
283 | |
284 | #endif // QBLUETOOTHLOCALDEVICE_P_H |
285 | |