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 QBLUETOOTHLOCALDEVICE_P_H |
41 | #define QBLUETOOTHLOCALDEVICE_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 <QtBluetooth/qtbluetoothglobal.h> |
55 | |
56 | #include "qbluetoothlocaldevice.h" |
57 | |
58 | #if QT_CONFIG(bluez) |
59 | #include <QObject> |
60 | #include <QDBusContext> |
61 | #include <QDBusObjectPath> |
62 | #include <QDBusMessage> |
63 | #include <QSet> |
64 | #include "bluez/bluez5_helper_p.h" |
65 | |
66 | class OrgBluezAdapterInterface; |
67 | class OrgBluezAdapter1Interface; |
68 | class OrgFreedesktopDBusPropertiesInterface; |
69 | class OrgFreedesktopDBusObjectManagerInterface; |
70 | class OrgBluezAgentAdaptor; |
71 | class OrgBluezDeviceInterface; |
72 | class OrgBluezDevice1Interface; |
73 | class OrgBluezManagerInterface; |
74 | |
75 | QT_BEGIN_NAMESPACE |
76 | class QDBusPendingCallWatcher; |
77 | QT_END_NAMESPACE |
78 | #endif |
79 | |
80 | #ifdef QT_ANDROID_BLUETOOTH |
81 | #include <jni.h> |
82 | #include <QtAndroidExtras/QAndroidJniEnvironment> |
83 | #include <QtAndroidExtras/QAndroidJniObject> |
84 | #include <QtCore/QPair> |
85 | #endif |
86 | |
87 | #ifdef QT_WINRT_BLUETOOTH |
88 | #include <wrl.h> |
89 | |
90 | namespace ABI { |
91 | namespace Windows { |
92 | namespace Devices { |
93 | namespace Bluetooth { |
94 | struct IBluetoothDeviceStatics; |
95 | struct IBluetoothLEDeviceStatics; |
96 | } |
97 | } |
98 | } |
99 | } |
100 | #endif |
101 | |
102 | QT_BEGIN_NAMESPACE |
103 | |
104 | extern void registerQBluetoothLocalDeviceMetaType(); |
105 | |
106 | class QBluetoothAddress; |
107 | |
108 | #ifdef QT_ANDROID_BLUETOOTH |
109 | class LocalDeviceBroadcastReceiver; |
110 | class QBluetoothLocalDevicePrivate : public QObject |
111 | { |
112 | Q_OBJECT |
113 | public: |
114 | QBluetoothLocalDevicePrivate( |
115 | QBluetoothLocalDevice *q, const QBluetoothAddress &address = QBluetoothAddress()); |
116 | ~QBluetoothLocalDevicePrivate(); |
117 | |
118 | QAndroidJniObject *adapter(); |
119 | void initialize(const QBluetoothAddress &address); |
120 | static bool startDiscovery(); |
121 | static bool cancelDiscovery(); |
122 | static bool isDiscovering(); |
123 | bool isValid() const; |
124 | |
125 | private slots: |
126 | void processHostModeChange(QBluetoothLocalDevice::HostMode newMode); |
127 | void processPairingStateChanged(const QBluetoothAddress &address, |
128 | QBluetoothLocalDevice::Pairing pairing); |
129 | void processConnectDeviceChanges(const QBluetoothAddress &address, bool isConnectEvent); |
130 | void processDisplayConfirmation(const QBluetoothAddress &address, const QString &pin); |
131 | void processDisplayPinCode(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 | void InterfacesAdded(const QDBusObjectPath &object_path, |
206 | InterfaceList interfaces_and_properties); |
207 | void InterfacesRemoved(const QDBusObjectPath &object_path, |
208 | const QStringList &interfaces); |
209 | void processPairingBluez5(const QString &objectPath, |
210 | QBluetoothLocalDevice::Pairing target); |
211 | void pairingDiscoveryTimedOut(); |
212 | |
213 | private: |
214 | void createCache(); |
215 | void connectDeviceChanges(); |
216 | |
217 | QDBusMessage msgConfirmation; |
218 | QDBusConnection *msgConnection = nullptr; |
219 | QString deviceAdapterPath; |
220 | |
221 | QBluetoothLocalDevice *q_ptr; |
222 | |
223 | void initializeAdapter(); |
224 | void initializeAdapterBluez5(); |
225 | }; |
226 | #elif defined(QT_WINRT_BLUETOOTH) |
227 | class QBluetoothLocalDevicePrivate : public QObject |
228 | { |
229 | Q_DECLARE_PUBLIC(QBluetoothLocalDevice) |
230 | public: |
231 | QBluetoothLocalDevicePrivate(QBluetoothLocalDevice *q, |
232 | QBluetoothAddress = QBluetoothAddress()); |
233 | |
234 | bool isValid() const; |
235 | |
236 | private: |
237 | QBluetoothLocalDevice *q_ptr; |
238 | Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::IBluetoothDeviceStatics> mStatics; |
239 | Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::IBluetoothLEDeviceStatics> mLEStatics; |
240 | }; |
241 | #elif !defined(QT_OSX_BLUETOOTH) // dummy backend |
242 | class QBluetoothLocalDevicePrivate : public QObject |
243 | { |
244 | public: |
245 | QBluetoothLocalDevicePrivate(QBluetoothLocalDevice * = nullptr, |
246 | QBluetoothAddress = QBluetoothAddress()) |
247 | { |
248 | } |
249 | |
250 | bool isValid() const |
251 | { |
252 | return false; |
253 | } |
254 | }; |
255 | #endif |
256 | |
257 | QT_END_NAMESPACE |
258 | |
259 | #endif // QBLUETOOTHLOCALDEVICE_P_H |
260 | |