| 1 | // Copyright (C) 2017 The Qt Company Ltd. | 
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only | 
| 3 |  | 
| 4 | #ifndef QLOWENERGYCONTROLLERPRIVATEDBUS_P_H | 
| 5 | #define QLOWENERGYCONTROLLERPRIVATEDBUS_P_H | 
| 6 |  | 
| 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 "qlowenergycontroller.h" | 
| 20 | #include "qlowenergycontrollerbase_p.h" | 
| 21 | #include "qleadvertiser_bluezdbus_p.h" | 
| 22 |  | 
| 23 | #include <QtDBus/QDBusObjectPath> | 
| 24 |  | 
| 25 | class OrgBluezAdapter1Interface; | 
| 26 | class OrgBluezBattery1Interface; | 
| 27 | class OrgBluezDevice1Interface; | 
| 28 | class OrgBluezGattCharacteristic1Interface; | 
| 29 | class OrgBluezGattDescriptor1Interface; | 
| 30 | class OrgBluezGattService1Interface; | 
| 31 | class OrgFreedesktopDBusObjectManagerInterface; | 
| 32 | class OrgFreedesktopDBusPropertiesInterfaceBluetooth; | 
| 33 |  | 
| 34 | QT_BEGIN_NAMESPACE | 
| 35 |  | 
| 36 | class QtBluezPeripheralApplication; | 
| 37 | class QtBluezPeripheralConnectionManager; | 
| 38 | class QDBusPendingCallWatcher; | 
| 39 |  | 
| 40 | class QLowEnergyControllerPrivateBluezDBus final : public QLowEnergyControllerPrivate | 
| 41 | { | 
| 42 |     Q_OBJECT | 
| 43 | public: | 
| 44 |     QLowEnergyControllerPrivateBluezDBus(const QString& adapterPathWithPeripheralSupport = {}); | 
| 45 |     ~QLowEnergyControllerPrivateBluezDBus() override; | 
| 46 |  | 
| 47 |     void init() override; | 
| 48 |     void connectToDevice() override; | 
| 49 |     void disconnectFromDevice() override; | 
| 50 |  | 
| 51 |     void discoverServices() override; | 
| 52 |     void discoverServiceDetails(const QBluetoothUuid &service, | 
| 53 |                                 QLowEnergyService::DiscoveryMode mode) override; | 
| 54 |  | 
| 55 |     void readCharacteristic( | 
| 56 |                 const QSharedPointer<QLowEnergyServicePrivate> service, | 
| 57 |                 const QLowEnergyHandle charHandle) override; | 
| 58 |     void readDescriptor( | 
| 59 |                 const QSharedPointer<QLowEnergyServicePrivate> service, | 
| 60 |                 const QLowEnergyHandle charHandle, | 
| 61 |                 const QLowEnergyHandle descriptorHandle) override; | 
| 62 |  | 
| 63 |     void writeCharacteristic( | 
| 64 |                 const QSharedPointer<QLowEnergyServicePrivate> service, | 
| 65 |                 const QLowEnergyHandle charHandle, | 
| 66 |                 const QByteArray &newValue, | 
| 67 |                 QLowEnergyService::WriteMode writeMode) override; | 
| 68 |     void writeDescriptor( | 
| 69 |                 const QSharedPointer<QLowEnergyServicePrivate> service, | 
| 70 |                 const QLowEnergyHandle charHandle, | 
| 71 |                 const QLowEnergyHandle descriptorHandle, | 
| 72 |                 const QByteArray &newValue) override; | 
| 73 |  | 
| 74 |     void startAdvertising( | 
| 75 |                 const QLowEnergyAdvertisingParameters ¶ms, | 
| 76 |                 const QLowEnergyAdvertisingData &advertisingData, | 
| 77 |                 const QLowEnergyAdvertisingData &scanResponseData) override; | 
| 78 |     void stopAdvertising() override; | 
| 79 |  | 
| 80 |     void requestConnectionUpdate( | 
| 81 |                 const QLowEnergyConnectionParameters & params) override; | 
| 82 |     void addToGenericAttributeList( | 
| 83 |                         const QLowEnergyServiceData &service, | 
| 84 |                         QLowEnergyHandle startHandle) override; | 
| 85 |  | 
| 86 |     int mtu() const override; | 
| 87 |  | 
| 88 | private: | 
| 89 |     void connectToDeviceHelper(); | 
| 90 |     void resetController(); | 
| 91 |  | 
| 92 |     void scheduleNextJob(); | 
| 93 |  | 
| 94 | private slots: | 
| 95 |     void devicePropertiesChanged(const QString &interface, const QVariantMap &changedProperties, | 
| 96 |                                  const QStringList &invalidatedProperties); | 
| 97 |     void characteristicPropertiesChanged(QLowEnergyHandle charHandle, const QString &interface, | 
| 98 |                                     const QVariantMap &changedProperties, | 
| 99 |                                     const QStringList &invalidatedProperties); | 
| 100 |     void interfacesRemoved(const QDBusObjectPath &objectPath, const QStringList &interfaces); | 
| 101 |  | 
| 102 |     void onCharReadFinished(QDBusPendingCallWatcher *call); | 
| 103 |     void onDescReadFinished(QDBusPendingCallWatcher *call); | 
| 104 |     void onCharWriteFinished(QDBusPendingCallWatcher *call); | 
| 105 |     void onDescWriteFinished(QDBusPendingCallWatcher *call); | 
| 106 | private: | 
| 107 |  | 
| 108 |     OrgBluezAdapter1Interface* adapter{}; | 
| 109 |     OrgBluezDevice1Interface* device{}; | 
| 110 |     OrgFreedesktopDBusObjectManagerInterface* managerBluez{}; | 
| 111 |     OrgFreedesktopDBusPropertiesInterfaceBluetooth* deviceMonitor{}; | 
| 112 |     QString adapterPathWithPeripheralSupport; | 
| 113 |  | 
| 114 |     int remoteMtu{-1}; | 
| 115 |     QtBluezPeripheralApplication* peripheralApplication{}; | 
| 116 |     QtBluezPeripheralConnectionManager* peripheralConnectionManager{}; | 
| 117 |     QLeDBusAdvertiser *advertiser{}; | 
| 118 |     void handleAdvertisingError(); | 
| 119 |     void handlePeripheralApplicationError(); | 
| 120 |     void handlePeripheralApplicationRegistered(); | 
| 121 |     void handlePeripheralConnectivityChanged(bool connected); | 
| 122 |     void handlePeripheralCharacteristicValueUpdate(QLowEnergyHandle handle, | 
| 123 |                                                    const QByteArray& value); | 
| 124 |     void handlePeripheralDescriptorValueUpdate(QLowEnergyHandle characteristicHandle, | 
| 125 |                                                QLowEnergyHandle descriptorHandle, | 
| 126 |                                                const QByteArray& value); | 
| 127 |     void handlePeripheralRemoteDeviceChanged(const QBluetoothAddress& address, | 
| 128 |                                              const QString& name, | 
| 129 |                                              quint16 mtu); | 
| 130 |     bool pendingConnect = false; | 
| 131 |     bool disconnectSignalRequired = false; | 
| 132 |  | 
| 133 |     struct GattCharacteristic | 
| 134 |     { | 
| 135 |         QSharedPointer<OrgBluezGattCharacteristic1Interface> characteristic; | 
| 136 |         QSharedPointer<OrgFreedesktopDBusPropertiesInterfaceBluetooth> charMonitor; | 
| 137 |         QList<QSharedPointer<OrgBluezGattDescriptor1Interface>> descriptors; | 
| 138 |     }; | 
| 139 |  | 
| 140 |     struct GattService | 
| 141 |     { | 
| 142 |         QString servicePath; | 
| 143 |         QList<GattCharacteristic> characteristics; | 
| 144 |  | 
| 145 |         bool hasBatteryService = false; | 
| 146 |         QSharedPointer<OrgBluezBattery1Interface> batteryInterface; | 
| 147 |     }; | 
| 148 |  | 
| 149 |     QHash<QBluetoothUuid, GattService> dbusServices; | 
| 150 |     QLowEnergyHandle runningHandle = 1; | 
| 151 |  | 
| 152 |     struct GattJob { | 
| 153 |         enum JobFlag { | 
| 154 |             Unset                   = 0x00, | 
| 155 |             CharRead                = 0x01, | 
| 156 |             CharWrite               = 0x02, | 
| 157 |             DescRead                = 0x04, | 
| 158 |             DescWrite               = 0x08, | 
| 159 |             ServiceDiscovery        = 0x10, | 
| 160 |             LastServiceDiscovery    = 0x20 | 
| 161 |         }; | 
| 162 |         Q_DECLARE_FLAGS(JobFlags, JobFlag) | 
| 163 |  | 
| 164 |         JobFlags flags = GattJob::Unset; | 
| 165 |         QLowEnergyHandle handle; | 
| 166 |         QByteArray value; | 
| 167 |         QLowEnergyService::WriteMode writeMode = QLowEnergyService::WriteWithResponse; | 
| 168 |         QSharedPointer<QLowEnergyServicePrivate> service; | 
| 169 |     }; | 
| 170 |  | 
| 171 |     QList<GattJob> jobs; | 
| 172 |     bool jobPending = false; | 
| 173 |  | 
| 174 |     void prepareNextJob(); | 
| 175 |     void discoverBatteryServiceDetails(GattService &dbusData, | 
| 176 |                                        QSharedPointer<QLowEnergyServicePrivate> serviceData); | 
| 177 |     void executeClose(QLowEnergyController::Error newError); | 
| 178 | }; | 
| 179 |  | 
| 180 | QT_END_NAMESPACE | 
| 181 |  | 
| 182 | #endif // QLOWENERGYCONTROLLERPRIVATEDBUS_P_H | 
| 183 |  |