| 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 QBLUETOOTHLOCALDEVICE_P_H |
| 6 | #define QBLUETOOTHLOCALDEVICE_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 <QtBluetooth/qtbluetoothglobal.h> |
| 20 | |
| 21 | #include "qbluetoothlocaldevice.h" |
| 22 | |
| 23 | #if QT_CONFIG(bluez) |
| 24 | #include <QObject> |
| 25 | #include <QDBusObjectPath> |
| 26 | #include <QDBusMessage> |
| 27 | #include <QSet> |
| 28 | #include "bluez/bluez5_helper_p.h" |
| 29 | |
| 30 | namespace QtBluetoothPrivate { |
| 31 | |
| 32 | class OrgBluezAdapter1Interface; |
| 33 | class OrgFreedesktopDBusPropertiesInterface; |
| 34 | class OrgFreedesktopDBusObjectManagerInterface; |
| 35 | class OrgBluezDevice1Interface; |
| 36 | |
| 37 | } // namespace QtBluetoothPrivate |
| 38 | |
| 39 | QT_BEGIN_NAMESPACE |
| 40 | class QDBusPendingCallWatcher; |
| 41 | QT_END_NAMESPACE |
| 42 | #endif |
| 43 | |
| 44 | #ifdef QT_WINRT_BLUETOOTH |
| 45 | #include "qbluetoothutils_winrt_p.h" |
| 46 | #include <winrt/Windows.Devices.Bluetooth.h> |
| 47 | QT_BEGIN_NAMESPACE |
| 48 | struct PairingWorker; |
| 49 | QT_END_NAMESPACE |
| 50 | #endif |
| 51 | |
| 52 | #ifdef QT_ANDROID_BLUETOOTH |
| 53 | #include <jni.h> |
| 54 | #include <QtCore/QJniEnvironment> |
| 55 | #include <QtCore/QJniObject> |
| 56 | #include <QtCore/QPair> |
| 57 | #endif |
| 58 | |
| 59 | QT_BEGIN_NAMESPACE |
| 60 | |
| 61 | extern void registerQBluetoothLocalDeviceMetaType(); |
| 62 | |
| 63 | class QBluetoothAddress; |
| 64 | |
| 65 | #ifdef QT_ANDROID_BLUETOOTH |
| 66 | class LocalDeviceBroadcastReceiver; |
| 67 | class QBluetoothLocalDevicePrivate : public QObject |
| 68 | { |
| 69 | Q_OBJECT |
| 70 | public: |
| 71 | QBluetoothLocalDevicePrivate( |
| 72 | QBluetoothLocalDevice *q, const QBluetoothAddress &address = QBluetoothAddress()); |
| 73 | ~QBluetoothLocalDevicePrivate(); |
| 74 | |
| 75 | QJniObject *adapter(); |
| 76 | void initialize(const QBluetoothAddress &address); |
| 77 | static bool startDiscovery(); |
| 78 | static bool cancelDiscovery(); |
| 79 | static bool isDiscovering(); |
| 80 | bool isValid() const; |
| 81 | |
| 82 | private slots: |
| 83 | void processHostModeChange(QBluetoothLocalDevice::HostMode newMode); |
| 84 | void processPairingStateChanged(const QBluetoothAddress &address, |
| 85 | QBluetoothLocalDevice::Pairing pairing); |
| 86 | void processConnectDeviceChanges(const QBluetoothAddress &address, bool isConnectEvent); |
| 87 | |
| 88 | private: |
| 89 | QBluetoothLocalDevice *q_ptr; |
| 90 | QJniObject *obj = nullptr; |
| 91 | |
| 92 | int pendingPairing(const QBluetoothAddress &address); |
| 93 | |
| 94 | public: |
| 95 | LocalDeviceBroadcastReceiver *receiver; |
| 96 | bool pendingConnectableHostModeTransition = false; |
| 97 | QList<QPair<QBluetoothAddress, bool> > pendingPairings; |
| 98 | |
| 99 | QList<QBluetoothAddress> connectedDevices; |
| 100 | }; |
| 101 | |
| 102 | #elif QT_CONFIG(bluez) |
| 103 | class QBluetoothLocalDevicePrivate : public QObject |
| 104 | { |
| 105 | Q_OBJECT |
| 106 | Q_DECLARE_PUBLIC(QBluetoothLocalDevice) |
| 107 | public: |
| 108 | QBluetoothLocalDevicePrivate(QBluetoothLocalDevice *q, |
| 109 | QBluetoothAddress localAddress = QBluetoothAddress()); |
| 110 | ~QBluetoothLocalDevicePrivate(); |
| 111 | |
| 112 | QSet<QBluetoothAddress> connectedDevicesSet; |
| 113 | QtBluetoothPrivate::OrgBluezAdapter1Interface *adapter = nullptr; |
| 114 | QtBluetoothPrivate::OrgFreedesktopDBusPropertiesInterface *adapterProperties = nullptr; |
| 115 | QtBluetoothPrivate::OrgFreedesktopDBusObjectManagerInterface *manager = nullptr; |
| 116 | QMap<QString, QtBluetoothPrivate::OrgFreedesktopDBusPropertiesInterface *> deviceChangeMonitors; |
| 117 | |
| 118 | QList<QBluetoothAddress> connectedDevices() const; |
| 119 | |
| 120 | QBluetoothAddress localAddress; |
| 121 | QBluetoothAddress address; |
| 122 | QBluetoothLocalDevice::Pairing pairing; |
| 123 | QtBluetoothPrivate::OrgBluezDevice1Interface *pairingTarget = nullptr; |
| 124 | QTimer *pairingDiscoveryTimer = nullptr; |
| 125 | QBluetoothLocalDevice::HostMode currentMode; |
| 126 | int pendingHostModeChange; |
| 127 | bool pairingRequestCanceled = false; |
| 128 | |
| 129 | public slots: |
| 130 | void pairingCompleted(QDBusPendingCallWatcher *); |
| 131 | |
| 132 | bool isValid() const; |
| 133 | |
| 134 | void requestPairing(const QBluetoothAddress &address, |
| 135 | QBluetoothLocalDevice::Pairing targetPairing); |
| 136 | |
| 137 | private Q_SLOTS: |
| 138 | void PropertiesChanged(const QString &interface, |
| 139 | const QVariantMap &changed_properties, |
| 140 | const QStringList &invalidated_properties, |
| 141 | const QDBusMessage &signal); |
| 142 | void InterfacesAdded(const QDBusObjectPath &object_path, |
| 143 | InterfaceList interfaces_and_properties); |
| 144 | void InterfacesRemoved(const QDBusObjectPath &object_path, |
| 145 | const QStringList &interfaces); |
| 146 | void processPairing(const QString &objectPath, QBluetoothLocalDevice::Pairing target); |
| 147 | void pairingDiscoveryTimedOut(); |
| 148 | |
| 149 | private: |
| 150 | void connectDeviceChanges(); |
| 151 | |
| 152 | QString deviceAdapterPath; |
| 153 | |
| 154 | QBluetoothLocalDevice *q_ptr; |
| 155 | |
| 156 | void initializeAdapter(); |
| 157 | }; |
| 158 | |
| 159 | #elif defined(QT_WINRT_BLUETOOTH) |
| 160 | class QBluetoothLocalDevicePrivate : public QObject |
| 161 | { |
| 162 | Q_OBJECT |
| 163 | Q_DECLARE_PUBLIC(QBluetoothLocalDevice) |
| 164 | public: |
| 165 | QBluetoothLocalDevicePrivate(QBluetoothLocalDevice *q, |
| 166 | QBluetoothAddress = QBluetoothAddress()); |
| 167 | ~QBluetoothLocalDevicePrivate(); |
| 168 | |
| 169 | bool isValid() const; |
| 170 | |
| 171 | void updateAdapterState(QBluetoothLocalDevice::HostMode mode); |
| 172 | Q_SLOT void onAdapterRemoved(winrt::hstring id); |
| 173 | Q_SLOT void onAdapterAdded(winrt::hstring id); |
| 174 | Q_SLOT void radioModeChanged(winrt::hstring id, QBluetoothLocalDevice::HostMode mode); |
| 175 | Q_SLOT void onDeviceAdded(const QBluetoothAddress &address); |
| 176 | Q_SLOT void onDeviceRemoved(const QBluetoothAddress &address); |
| 177 | |
| 178 | QBluetoothLocalDevice *q_ptr; |
| 179 | winrt::com_ptr<PairingWorker> mPairingWorker; |
| 180 | winrt::Windows::Devices::Bluetooth::BluetoothAdapter mAdapter; |
| 181 | winrt::hstring mDeviceId; |
| 182 | QString mAdapterName; |
| 183 | QBluetoothLocalDevice::HostMode mMode; |
| 184 | winrt::event_token mModeChangeToken; |
| 185 | |
| 186 | signals: |
| 187 | void updateMode(winrt::hstring id, QBluetoothLocalDevice::HostMode mode); |
| 188 | }; |
| 189 | #elif !defined(QT_OSX_BLUETOOTH) // dummy backend |
| 190 | class QBluetoothLocalDevicePrivate : public QObject |
| 191 | { |
| 192 | public: |
| 193 | QBluetoothLocalDevicePrivate(QBluetoothLocalDevice * = nullptr, |
| 194 | QBluetoothAddress = QBluetoothAddress()) |
| 195 | { |
| 196 | } |
| 197 | |
| 198 | bool isValid() const |
| 199 | { |
| 200 | return false; |
| 201 | } |
| 202 | }; |
| 203 | #endif |
| 204 | |
| 205 | QT_END_NAMESPACE |
| 206 | |
| 207 | #endif // QBLUETOOTHLOCALDEVICE_P_H |
| 208 | |