| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // Copyright (C) 2014 Denis Shienkov <denis.shienkov@gmail.com> |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | |
| 5 | #ifndef QBLUETOOTHDEVICEDISCOVERYAGENT_P_H |
| 6 | #define QBLUETOOTHDEVICEDISCOVERYAGENT_P_H |
| 7 | |
| 8 | // |
| 9 | // W A R N I N G |
| 10 | // ------------- |
| 11 | // |
| 12 | // This file is not part of the Qt API. It exists purely as an |
| 13 | // implementation detail. This header file may change from version to |
| 14 | // version without notice, or even be removed. |
| 15 | // |
| 16 | // We mean it. |
| 17 | // |
| 18 | |
| 19 | #include "qbluetoothdevicediscoveryagent.h" |
| 20 | #ifdef QT_ANDROID_BLUETOOTH |
| 21 | #include <QtCore/QJniObject> |
| 22 | #include "android/devicediscoverybroadcastreceiver_p.h" |
| 23 | #include <QtCore/QTimer> |
| 24 | #endif |
| 25 | |
| 26 | #ifdef Q_OS_DARWIN |
| 27 | #include "darwin/btdelegates_p.h" |
| 28 | #include "darwin/btraii_p.h" |
| 29 | #endif // Q_OS_DARWIN |
| 30 | |
| 31 | #include <QtCore/QVariantMap> |
| 32 | |
| 33 | #include <QtBluetooth/QBluetoothAddress> |
| 34 | #include <QtBluetooth/QBluetoothLocalDevice> |
| 35 | |
| 36 | #if QT_CONFIG(bluez) |
| 37 | #include "bluez/bluez5_helper_p.h" |
| 38 | |
| 39 | namespace QtBluetoothPrivate { |
| 40 | |
| 41 | class OrgBluezManagerInterface; |
| 42 | class OrgBluezAdapterInterface; |
| 43 | class OrgFreedesktopDBusObjectManagerInterface; |
| 44 | class OrgFreedesktopDBusPropertiesInterface; |
| 45 | class OrgBluezAdapter1Interface; |
| 46 | class OrgBluezDevice1Interface; |
| 47 | |
| 48 | } // namespace QtBluetoothPrivate |
| 49 | |
| 50 | QT_BEGIN_NAMESPACE |
| 51 | class QDBusVariant; |
| 52 | QT_END_NAMESPACE |
| 53 | #endif |
| 54 | |
| 55 | #ifdef QT_WINRT_BLUETOOTH |
| 56 | #include <QtCore/QPointer> |
| 57 | #include <QtCore/QTimer> |
| 58 | |
| 59 | using ManufacturerData = QHash<quint16, QByteArray>; |
| 60 | using ServiceData = QHash<QBluetoothUuid, QByteArray>; |
| 61 | QT_DECL_METATYPE_EXTERN(ManufacturerData, Q_BLUETOOTH_EXPORT) |
| 62 | QT_DECL_METATYPE_EXTERN(ServiceData, Q_BLUETOOTH_EXPORT) |
| 63 | #endif |
| 64 | |
| 65 | QT_BEGIN_NAMESPACE |
| 66 | |
| 67 | #ifdef QT_WINRT_BLUETOOTH |
| 68 | class QWinRTBluetoothDeviceDiscoveryWorker; |
| 69 | #endif |
| 70 | |
| 71 | class QBluetoothDeviceDiscoveryAgentPrivate |
| 72 | #if defined(QT_ANDROID_BLUETOOTH) || defined(QT_WINRT_BLUETOOTH) \ |
| 73 | || defined(Q_OS_DARWIN) |
| 74 | : public QObject |
| 75 | #if defined(Q_OS_MACOS) |
| 76 | , public DarwinBluetooth::DeviceInquiryDelegate |
| 77 | #endif // Q_OS_MACOS |
| 78 | { |
| 79 | Q_OBJECT |
| 80 | #else // BlueZ |
| 81 | { |
| 82 | #endif |
| 83 | Q_DECLARE_PUBLIC(QBluetoothDeviceDiscoveryAgent) |
| 84 | public: |
| 85 | QBluetoothDeviceDiscoveryAgentPrivate( |
| 86 | const QBluetoothAddress &deviceAdapter, |
| 87 | QBluetoothDeviceDiscoveryAgent *parent); |
| 88 | ~QBluetoothDeviceDiscoveryAgentPrivate(); |
| 89 | |
| 90 | void start(QBluetoothDeviceDiscoveryAgent::DiscoveryMethods methods); |
| 91 | void stop(); |
| 92 | bool isActive() const; |
| 93 | |
| 94 | #if QT_CONFIG(bluez) |
| 95 | void _q_InterfacesAdded(const QDBusObjectPath &object_path, |
| 96 | InterfaceList interfaces_and_properties); |
| 97 | void _q_discoveryFinished(); |
| 98 | void _q_discoveryInterrupted(const QString &path); |
| 99 | void _q_PropertiesChanged(const QString &interface, |
| 100 | const QString &path, |
| 101 | const QVariantMap &changed_properties, |
| 102 | const QStringList &invalidated_properties); |
| 103 | #endif |
| 104 | |
| 105 | private: |
| 106 | QList<QBluetoothDeviceInfo> discoveredDevices; |
| 107 | |
| 108 | QBluetoothDeviceDiscoveryAgent::Error lastError = QBluetoothDeviceDiscoveryAgent::NoError; |
| 109 | QString errorString; |
| 110 | QBluetoothAddress adapterAddress; |
| 111 | |
| 112 | #ifdef QT_ANDROID_BLUETOOTH |
| 113 | private slots: |
| 114 | void processSdpDiscoveryFinished(); |
| 115 | void processDiscoveredDevices(const QBluetoothDeviceInfo &info, bool isLeResult); |
| 116 | friend void QtBluetoothLE_leScanResult(JNIEnv *, jobject, jlong, jobject); |
| 117 | void stopLowEnergyScan(); |
| 118 | |
| 119 | private: |
| 120 | void startLowEnergyScan(); |
| 121 | void classicDiscoveryStartFail(); |
| 122 | bool setErrorIfPowerOff(); |
| 123 | |
| 124 | DeviceDiscoveryBroadcastReceiver *receiver = nullptr; |
| 125 | short m_active; |
| 126 | QJniObject adapter; |
| 127 | QJniObject leScanner; |
| 128 | QTimer *leScanTimeout = nullptr; |
| 129 | QTimer *deviceDiscoveryStartTimeout = nullptr; |
| 130 | short deviceDiscoveryStartAttemptsLeft; |
| 131 | |
| 132 | bool pendingCancel = false; |
| 133 | bool pendingStart = false; |
| 134 | #elif QT_CONFIG(bluez) |
| 135 | bool pendingCancel = false; |
| 136 | bool pendingStart = false; |
| 137 | QtBluetoothPrivate::OrgFreedesktopDBusObjectManagerInterface *manager = nullptr; |
| 138 | QtBluetoothPrivate::OrgBluezAdapter1Interface *adapter = nullptr; |
| 139 | QTimer *discoveryTimer = nullptr; |
| 140 | QList<QtBluetoothPrivate::OrgFreedesktopDBusPropertiesInterface *> propertyMonitors; |
| 141 | |
| 142 | void deviceFound(const QString &devicePath, const QVariantMap &properties); |
| 143 | |
| 144 | QMap<QString, QVariantMap> devicesProperties; |
| 145 | #endif |
| 146 | |
| 147 | #ifdef QT_WINRT_BLUETOOTH |
| 148 | private slots: |
| 149 | void registerDevice(const QBluetoothDeviceInfo &info); |
| 150 | void updateDeviceData(const QBluetoothAddress &address, QBluetoothDeviceInfo::Fields fields, |
| 151 | qint16 rssi, ManufacturerData manufacturerData, ServiceData serviceData); |
| 152 | void onErrorOccured(QBluetoothDeviceDiscoveryAgent::Error e); |
| 153 | void onScanFinished(); |
| 154 | |
| 155 | private: |
| 156 | void disconnectAndClearWorker(); |
| 157 | std::shared_ptr<QWinRTBluetoothDeviceDiscoveryWorker> worker = nullptr; |
| 158 | #endif |
| 159 | |
| 160 | #ifdef Q_OS_DARWIN |
| 161 | |
| 162 | void startLE(); |
| 163 | |
| 164 | #ifdef Q_OS_MACOS |
| 165 | |
| 166 | void startClassic(); |
| 167 | |
| 168 | // Classic (IOBluetooth) inquiry delegate's methods: |
| 169 | void inquiryFinished() override; |
| 170 | void error(IOReturn error) override; |
| 171 | void classicDeviceFound(void *device) override; |
| 172 | // Classic (IOBluetooth) errors: |
| 173 | void setError(IOReturn error, const QString &text = QString()); |
| 174 | |
| 175 | #endif // Q_OS_MACOS |
| 176 | |
| 177 | // LE scan delegates (CoreBluetooth, all Darwin OSes): |
| 178 | void LEinquiryFinished(); |
| 179 | void LEinquiryError(QBluetoothDeviceDiscoveryAgent::Error error); |
| 180 | void LEnotSupported(); |
| 181 | |
| 182 | // LE errors: |
| 183 | void setError(QBluetoothDeviceDiscoveryAgent::Error, |
| 184 | const QString &text = QString()); |
| 185 | |
| 186 | // Both LE and Classic devices go there: |
| 187 | void deviceFound(const QBluetoothDeviceInfo &newDeviceInfo); |
| 188 | |
| 189 | enum AgentState { |
| 190 | NonActive, |
| 191 | ClassicScan, // macOS (IOBluetooth) only |
| 192 | LEScan |
| 193 | } agentState; |
| 194 | |
| 195 | bool startPending = false; |
| 196 | bool stopPending = false; |
| 197 | |
| 198 | #ifdef Q_OS_MACOS |
| 199 | |
| 200 | DarwinBluetooth::ScopedPointer controller; |
| 201 | DarwinBluetooth::ScopedPointer inquiry; |
| 202 | |
| 203 | #endif // Q_OS_MACOS |
| 204 | |
| 205 | DarwinBluetooth::ScopedPointer inquiryLE; |
| 206 | |
| 207 | #endif // Q_OS_DARWIN |
| 208 | |
| 209 | int lowEnergySearchTimeout = 40000; |
| 210 | QBluetoothDeviceDiscoveryAgent::DiscoveryMethods requestedMethods; |
| 211 | QBluetoothDeviceDiscoveryAgent *q_ptr; |
| 212 | }; |
| 213 | |
| 214 | QT_END_NAMESPACE |
| 215 | |
| 216 | #endif |
| 217 | |