| 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 QLOWENERGYCONTROLLER_H |
| 41 | #define QLOWENERGYCONTROLLER_H |
| 42 | |
| 43 | #include <QtCore/QObject> |
| 44 | #include <QtBluetooth/QBluetoothAddress> |
| 45 | #include <QtBluetooth/QBluetoothDeviceInfo> |
| 46 | #include <QtBluetooth/QBluetoothUuid> |
| 47 | #include <QtBluetooth/QLowEnergyAdvertisingData> |
| 48 | #include <QtBluetooth/QLowEnergyService> |
| 49 | |
| 50 | QT_BEGIN_NAMESPACE |
| 51 | |
| 52 | class QLowEnergyAdvertisingParameters; |
| 53 | class QLowEnergyConnectionParameters; |
| 54 | class QLowEnergyControllerPrivate; |
| 55 | class QLowEnergyServiceData; |
| 56 | |
| 57 | class Q_BLUETOOTH_EXPORT QLowEnergyController : public QObject |
| 58 | { |
| 59 | Q_OBJECT |
| 60 | public: |
| 61 | enum Error { |
| 62 | NoError, |
| 63 | UnknownError, |
| 64 | UnknownRemoteDeviceError, |
| 65 | NetworkError, |
| 66 | InvalidBluetoothAdapterError, |
| 67 | ConnectionError, |
| 68 | AdvertisingError, |
| 69 | RemoteHostClosedError, |
| 70 | AuthorizationError |
| 71 | }; |
| 72 | Q_ENUM(Error) |
| 73 | |
| 74 | enum ControllerState { |
| 75 | UnconnectedState = 0, |
| 76 | ConnectingState, |
| 77 | ConnectedState, |
| 78 | DiscoveringState, |
| 79 | DiscoveredState, |
| 80 | ClosingState, |
| 81 | AdvertisingState, |
| 82 | }; |
| 83 | Q_ENUM(ControllerState) |
| 84 | |
| 85 | enum RemoteAddressType { |
| 86 | PublicAddress = 0, |
| 87 | RandomAddress |
| 88 | }; |
| 89 | Q_ENUM(RemoteAddressType) |
| 90 | |
| 91 | enum Role { CentralRole, PeripheralRole }; |
| 92 | Q_ENUM(Role) |
| 93 | |
| 94 | explicit QLowEnergyController(const QBluetoothAddress &remoteDevice, |
| 95 | QObject *parent = nullptr); // TODO Qt 6 remove ctor |
| 96 | explicit QLowEnergyController(const QBluetoothDeviceInfo &remoteDevice, |
| 97 | QObject *parent = nullptr); // TODO Qt 6 make private |
| 98 | explicit QLowEnergyController(const QBluetoothAddress &remoteDevice, |
| 99 | const QBluetoothAddress &localDevice, |
| 100 | QObject *parent = nullptr); // TODO Qt 6 remove ctor |
| 101 | |
| 102 | static QLowEnergyController *createCentral(const QBluetoothDeviceInfo &remoteDevice, |
| 103 | QObject *parent = nullptr); |
| 104 | static QLowEnergyController *createCentral(const QBluetoothAddress &remoteDevice, |
| 105 | const QBluetoothAddress &localDevice, |
| 106 | QObject *parent = nullptr); |
| 107 | static QLowEnergyController *createPeripheral(QObject *parent = nullptr); |
| 108 | |
| 109 | // TODO: Allow to set connection timeout (disconnect when no data has been exchanged for n seconds). |
| 110 | |
| 111 | ~QLowEnergyController(); |
| 112 | |
| 113 | QBluetoothAddress localAddress() const; |
| 114 | QBluetoothAddress remoteAddress() const; |
| 115 | QBluetoothUuid remoteDeviceUuid() const; |
| 116 | |
| 117 | QString remoteName() const; |
| 118 | |
| 119 | ControllerState state() const; |
| 120 | |
| 121 | // TODO Qt6 remove this property. It is not longer needed when using Bluez DBus backend |
| 122 | RemoteAddressType remoteAddressType() const; |
| 123 | void setRemoteAddressType(RemoteAddressType type); |
| 124 | |
| 125 | void connectToDevice(); |
| 126 | void disconnectFromDevice(); |
| 127 | |
| 128 | void discoverServices(); |
| 129 | QList<QBluetoothUuid> services() const; |
| 130 | QLowEnergyService *createServiceObject(const QBluetoothUuid &service, QObject *parent = nullptr); |
| 131 | |
| 132 | void startAdvertising(const QLowEnergyAdvertisingParameters ¶meters, |
| 133 | const QLowEnergyAdvertisingData &advertisingData, |
| 134 | const QLowEnergyAdvertisingData &scanResponseData = QLowEnergyAdvertisingData()); |
| 135 | void stopAdvertising(); |
| 136 | |
| 137 | QLowEnergyService *addService(const QLowEnergyServiceData &service, QObject *parent = nullptr); |
| 138 | |
| 139 | void requestConnectionUpdate(const QLowEnergyConnectionParameters ¶meters); |
| 140 | |
| 141 | Error error() const; |
| 142 | QString errorString() const; |
| 143 | |
| 144 | Role role() const; |
| 145 | |
| 146 | Q_SIGNALS: |
| 147 | void connected(); |
| 148 | void disconnected(); |
| 149 | void stateChanged(QLowEnergyController::ControllerState state); |
| 150 | void error(QLowEnergyController::Error newError); |
| 151 | |
| 152 | void serviceDiscovered(const QBluetoothUuid &newService); |
| 153 | void discoveryFinished(); |
| 154 | void connectionUpdated(const QLowEnergyConnectionParameters ¶meters); |
| 155 | |
| 156 | private: |
| 157 | explicit QLowEnergyController(QObject *parent = nullptr); // For the peripheral role. |
| 158 | |
| 159 | Q_DECLARE_PRIVATE(QLowEnergyController) |
| 160 | QLowEnergyControllerPrivate *d_ptr; |
| 161 | }; |
| 162 | |
| 163 | QT_END_NAMESPACE |
| 164 | |
| 165 | Q_DECLARE_METATYPE(QLowEnergyController::Error) |
| 166 | Q_DECLARE_METATYPE(QLowEnergyController::ControllerState) |
| 167 | Q_DECLARE_METATYPE(QLowEnergyController::RemoteAddressType) |
| 168 | Q_DECLARE_METATYPE(QLowEnergyController::Role) |
| 169 | |
| 170 | #endif // QLOWENERGYCONTROLLER_H |
| 171 | |