| 1 | // Copyright (C) 2022 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 BLUEZ_PERIPHERAL_OBJECTS_P_H | 
| 5 | #define BLUEZ_PERIPHERAL_OBJECTS_P_H | 
| 6 |  | 
| 7 | // | 
| 8 | //  W A R N I N G | 
| 9 | //  ------------- | 
| 10 | // | 
| 11 | // This file is not part of the Qt API.  It exists purely as an | 
| 12 | // implementation detail.  This header file may change from version to | 
| 13 | // version without notice, or even be removed. | 
| 14 | // | 
| 15 | // We mean it. | 
| 16 | // | 
| 17 |  | 
| 18 | #include "bluez5_helper_p.h" | 
| 19 |  | 
| 20 | #include <QtBluetooth/qbluetooth.h> | 
| 21 | #include <QtBluetooth/QBluetoothUuid> | 
| 22 | #include <QtBluetooth/QLowEnergyDescriptorData> | 
| 23 | #include <QtBluetooth/QLowEnergyCharacteristicData> | 
| 24 | #include <QtBluetooth/QLowEnergyServiceData> | 
| 25 |  | 
| 26 | class OrgFreedesktopDBusPropertiesAdaptor; | 
| 27 | class OrgBluezGattCharacteristic1Adaptor; | 
| 28 | class OrgBluezGattDescriptor1Adaptor; | 
| 29 | class OrgBluezGattService1Adaptor; | 
| 30 |  | 
| 31 | QT_BEGIN_NAMESPACE | 
| 32 |  | 
| 33 | // The QtBluezPeripheralGattObject is the base class for services, characteristics, and descriptors | 
| 34 | class QtBluezPeripheralGattObject : public QObject | 
| 35 | { | 
| 36 |     Q_OBJECT | 
| 37 |  | 
| 38 | public: | 
| 39 |     QtBluezPeripheralGattObject(const QString& objectPath, const QString& uuid, | 
| 40 |                QLowEnergyHandle handle, QObject* parent = nullptr); | 
| 41 |     virtual ~QtBluezPeripheralGattObject(); | 
| 42 |  | 
| 43 |     // List of properties exposed by this object, used when Bluez inquiries details | 
| 44 |     virtual InterfaceList properties() const = 0; | 
| 45 |  | 
| 46 |     bool registerObject(); | 
| 47 |     void unregisterObject(); | 
| 48 |  | 
| 49 | public: | 
| 50 |     // DBus object path | 
| 51 |     QString objectPath; | 
| 52 |     // UUID of the gatt object | 
| 53 |     QString uuid; | 
| 54 |     // QtBluetooth internal handle and reference to the application | 
| 55 |     // to read and write values towards the Qt API | 
| 56 |     QLowEnergyHandle handle; | 
| 57 |     // Bluez DBus Gatt objects need to provide this | 
| 58 |     OrgFreedesktopDBusPropertiesAdaptor* propertiesAdaptor{}; | 
| 59 |  | 
| 60 | signals: | 
| 61 |     void remoteDeviceAccessEvent(const QString& remoteDeviceObjectPath, quint16 mtu); | 
| 62 |  | 
| 63 | protected: | 
| 64 |     void accessEvent(const QVariantMap& options); | 
| 65 |  | 
| 66 | private: | 
| 67 |     bool m_registered = false; | 
| 68 | }; | 
| 69 |  | 
| 70 | class QtBluezPeripheralDescriptor : public QtBluezPeripheralGattObject | 
| 71 | { | 
| 72 |     Q_OBJECT | 
| 73 |  | 
| 74 | public: | 
| 75 |     QtBluezPeripheralDescriptor(const QLowEnergyDescriptorData& descriptorData, | 
| 76 |                                 const QString& characteristicPath, quint16 ordinal, | 
| 77 |                                 QLowEnergyHandle handle, QLowEnergyHandle characteristicHandle, | 
| 78 |                                 QObject* parent); | 
| 79 |  | 
| 80 |     InterfaceList properties() const final; | 
| 81 |  | 
| 82 |     // org.bluez.GattDescriptor1 | 
| 83 |     // This function is invoked when remote device reads the value. Sets error if any | 
| 84 |     Q_INVOKABLE QByteArray ReadValue(const QVariantMap &options, QString &error); | 
| 85 |  | 
| 86 |     // org.bluez.GattDescriptor1 | 
| 87 |     // This function is invoked when remote device writes a value. Returns Bluez DBus error if any | 
| 88 |     Q_INVOKABLE QString WriteValue(const QByteArray &value, const QVariantMap &options); | 
| 89 |  | 
| 90 |     // Call this function when value has been updated locally (server/user application side) | 
| 91 |     bool localValueUpdate(const QByteArray& value); | 
| 92 |  | 
| 93 | signals: | 
| 94 |     void valueUpdatedByRemote(QLowEnergyHandle characteristicHandle, | 
| 95 |                               QLowEnergyHandle descriptorHandle, const QByteArray& value); | 
| 96 |  | 
| 97 | private: | 
| 98 |     void initializeFlags(const QLowEnergyDescriptorData& data); | 
| 99 |  | 
| 100 |     OrgBluezGattDescriptor1Adaptor* m_adaptor{}; | 
| 101 |     QString m_characteristicPath; | 
| 102 |     QByteArray m_value; | 
| 103 |     QStringList m_flags; | 
| 104 |     QLowEnergyHandle m_characteristicHandle; | 
| 105 | }; | 
| 106 |  | 
| 107 |  | 
| 108 | class QtBluezPeripheralCharacteristic : public QtBluezPeripheralGattObject | 
| 109 | { | 
| 110 |     Q_OBJECT | 
| 111 |  | 
| 112 | public: | 
| 113 |     QtBluezPeripheralCharacteristic(const QLowEnergyCharacteristicData& characteristicData, | 
| 114 |                                     const QString& servicePath, quint16 ordinal, | 
| 115 |                                     QLowEnergyHandle handle, QObject* parent); | 
| 116 |  | 
| 117 |     InterfaceList properties() const final; | 
| 118 |  | 
| 119 |     // org.bluez.GattCharacteristic1 | 
| 120 |     // This function is invoked when remote device reads the value. Sets error if any | 
| 121 |     Q_INVOKABLE QByteArray ReadValue(const QVariantMap &options, QString& error); | 
| 122 |  | 
| 123 |     // org.bluez.GattCharacteristic1 | 
| 124 |     // This function is invoked when remote device writes a value. Returns Bluez DBus error if any | 
| 125 |     Q_INVOKABLE QString WriteValue(const QByteArray &value, const QVariantMap &options); | 
| 126 |  | 
| 127 |     // org.bluez.GattCharacteristic1 | 
| 128 |     // These are called when remote client enables or disables NTF/IND | 
| 129 |     Q_INVOKABLE void StartNotify(); | 
| 130 |     Q_INVOKABLE void StopNotify(); | 
| 131 |  | 
| 132 |     // Call this function when value has been updated locally (server/user application side) | 
| 133 |     bool localValueUpdate(const QByteArray& value); | 
| 134 |  | 
| 135 | signals: | 
| 136 |     void valueUpdatedByRemote(QLowEnergyHandle handle, const QByteArray& value); | 
| 137 |  | 
| 138 | private: | 
| 139 |     void initializeValue(const QByteArray& value); | 
| 140 |     void initializeFlags(const QLowEnergyCharacteristicData& data); | 
| 141 |  | 
| 142 |     OrgBluezGattCharacteristic1Adaptor* m_adaptor{}; | 
| 143 |     QString m_servicePath; | 
| 144 |     bool m_notifying{false}; | 
| 145 |     QByteArray m_value; | 
| 146 |     QStringList m_flags; | 
| 147 |     int m_minimumValueLength; | 
| 148 |     int m_maximumValueLength; | 
| 149 | }; | 
| 150 |  | 
| 151 | class QtBluezPeripheralService : public QtBluezPeripheralGattObject | 
| 152 | { | 
| 153 |     Q_OBJECT | 
| 154 | public: | 
| 155 |     QtBluezPeripheralService(const QLowEnergyServiceData &serviceData, | 
| 156 |                              const QString& applicationPath, quint16 ordinal, | 
| 157 |                              QLowEnergyHandle handle, QObject* parent); | 
| 158 |  | 
| 159 |     InterfaceList properties() const final; | 
| 160 |     void addIncludedService(const QString& objectPath); | 
| 161 |  | 
| 162 | private: | 
| 163 |     const bool m_isPrimary; | 
| 164 |     OrgBluezGattService1Adaptor* m_adaptor{}; | 
| 165 |     QList<QDBusObjectPath> m_includedServices; | 
| 166 | }; | 
| 167 |  | 
| 168 |  | 
| 169 | QT_END_NAMESPACE | 
| 170 |  | 
| 171 | #endif | 
| 172 |  |