| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // Copyright (C) 2016 Javier S. Pedro <maemo@javispedro.com> |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | #ifndef QLOWENERGYSERVICE_H |
| 5 | #define QLOWENERGYSERVICE_H |
| 6 | |
| 7 | #include <QtBluetooth/QBluetoothAddress> |
| 8 | #include <QtBluetooth/QBluetoothUuid> |
| 9 | #include <QtBluetooth/QLowEnergyCharacteristic> |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | class QLowEnergyServicePrivate; |
| 14 | class Q_BLUETOOTH_EXPORT QLowEnergyService : public QObject |
| 15 | { |
| 16 | Q_OBJECT |
| 17 | public: |
| 18 | enum ServiceType { |
| 19 | PrimaryService = 0x0001, |
| 20 | IncludedService = 0x0002 |
| 21 | }; |
| 22 | Q_ENUM(ServiceType) |
| 23 | Q_DECLARE_FLAGS(ServiceTypes, ServiceType) |
| 24 | |
| 25 | enum ServiceError { |
| 26 | NoError = 0, |
| 27 | OperationError, |
| 28 | CharacteristicWriteError, |
| 29 | DescriptorWriteError, |
| 30 | UnknownError, |
| 31 | CharacteristicReadError, |
| 32 | DescriptorReadError |
| 33 | }; |
| 34 | Q_ENUM(ServiceError) |
| 35 | |
| 36 | enum ServiceState { |
| 37 | InvalidService = 0, |
| 38 | RemoteService, |
| 39 | RemoteServiceDiscovering, // discoverDetails() called and running |
| 40 | RemoteServiceDiscovered, // all details have been synchronized |
| 41 | LocalService, |
| 42 | |
| 43 | #if QT_DEPRECATED_SINCE(6, 2) |
| 44 | // for source compatibility: |
| 45 | DiscoveryRequired |
| 46 | Q_DECL_ENUMERATOR_DEPRECATED_X( |
| 47 | "DiscoveryRequired was renamed to RemoteService." ) |
| 48 | = RemoteService, |
| 49 | DiscoveringService |
| 50 | Q_DECL_ENUMERATOR_DEPRECATED_X( |
| 51 | "DiscoveringService was renamed to RemoteServiceDiscovering." ) |
| 52 | = RemoteServiceDiscovering, |
| 53 | ServiceDiscovered |
| 54 | Q_DECL_ENUMERATOR_DEPRECATED_X( |
| 55 | "ServiceDiscovered was renamed to RemoteServiceDiscovered." ) |
| 56 | = RemoteServiceDiscovered, |
| 57 | #endif |
| 58 | }; |
| 59 | Q_ENUM(ServiceState) |
| 60 | |
| 61 | enum DiscoveryMode { |
| 62 | FullDiscovery, // standard, reads all attributes |
| 63 | SkipValueDiscovery // does not read characteristic values and descriptors |
| 64 | }; |
| 65 | Q_ENUM(DiscoveryMode) |
| 66 | |
| 67 | enum WriteMode { |
| 68 | WriteWithResponse = 0, |
| 69 | WriteWithoutResponse, |
| 70 | WriteSigned |
| 71 | }; |
| 72 | Q_ENUM(WriteMode) |
| 73 | |
| 74 | ~QLowEnergyService(); |
| 75 | |
| 76 | QList<QBluetoothUuid> includedServices() const; |
| 77 | |
| 78 | QLowEnergyService::ServiceTypes type() const; |
| 79 | QLowEnergyService::ServiceState state() const; |
| 80 | |
| 81 | QLowEnergyCharacteristic characteristic(const QBluetoothUuid &uuid) const; |
| 82 | QList<QLowEnergyCharacteristic> characteristics() const; |
| 83 | QBluetoothUuid serviceUuid() const; |
| 84 | QString serviceName() const; |
| 85 | |
| 86 | void discoverDetails(DiscoveryMode mode = FullDiscovery); |
| 87 | |
| 88 | ServiceError error() const; |
| 89 | |
| 90 | bool contains(const QLowEnergyCharacteristic &characteristic) const; |
| 91 | void readCharacteristic(const QLowEnergyCharacteristic &characteristic); |
| 92 | void writeCharacteristic(const QLowEnergyCharacteristic &characteristic, |
| 93 | const QByteArray &newValue, |
| 94 | WriteMode mode = WriteWithResponse); |
| 95 | |
| 96 | bool contains(const QLowEnergyDescriptor &descriptor) const; |
| 97 | void readDescriptor(const QLowEnergyDescriptor &descriptor); |
| 98 | void writeDescriptor(const QLowEnergyDescriptor &descriptor, |
| 99 | const QByteArray &newValue); |
| 100 | |
| 101 | Q_SIGNALS: |
| 102 | void stateChanged(QLowEnergyService::ServiceState newState); |
| 103 | void characteristicChanged(const QLowEnergyCharacteristic &info, |
| 104 | const QByteArray &value); |
| 105 | void characteristicRead(const QLowEnergyCharacteristic &info, |
| 106 | const QByteArray &value); |
| 107 | void characteristicWritten(const QLowEnergyCharacteristic &info, |
| 108 | const QByteArray &value); |
| 109 | void descriptorRead(const QLowEnergyDescriptor &info, |
| 110 | const QByteArray &value); |
| 111 | void descriptorWritten(const QLowEnergyDescriptor &info, |
| 112 | const QByteArray &value); |
| 113 | void errorOccurred(QLowEnergyService::ServiceError error); |
| 114 | |
| 115 | private: |
| 116 | Q_DECLARE_PRIVATE(QLowEnergyService) |
| 117 | QSharedPointer<QLowEnergyServicePrivate> d_ptr; |
| 118 | |
| 119 | // QLowEnergyController is the factory for this class |
| 120 | friend class QLowEnergyController; |
| 121 | friend class QLowEnergyControllerPrivate; |
| 122 | friend class QLowEnergyControllerPrivateBluez; |
| 123 | friend class QLowEnergyControllerPrivateAndroid; |
| 124 | friend class QLowEnergyControllerPrivateDarwin; |
| 125 | QLowEnergyService(QSharedPointer<QLowEnergyServicePrivate> p, |
| 126 | QObject *parent = nullptr); |
| 127 | }; |
| 128 | |
| 129 | Q_DECLARE_OPERATORS_FOR_FLAGS(QLowEnergyService::ServiceTypes) |
| 130 | |
| 131 | QT_END_NAMESPACE |
| 132 | |
| 133 | QT_DECL_METATYPE_EXTERN_TAGGED(QLowEnergyService::ServiceError, QLowEnergyService__ServiceError, |
| 134 | Q_BLUETOOTH_EXPORT) |
| 135 | QT_DECL_METATYPE_EXTERN_TAGGED(QLowEnergyService::ServiceState, QLowEnergyService__ServiceState, |
| 136 | Q_BLUETOOTH_EXPORT) |
| 137 | QT_DECL_METATYPE_EXTERN_TAGGED(QLowEnergyService::ServiceType, QLowEnergyService__ServiceType, |
| 138 | Q_BLUETOOTH_EXPORT) |
| 139 | QT_DECL_METATYPE_EXTERN_TAGGED(QLowEnergyService::WriteMode, QLowEnergyService__WriteMode, |
| 140 | Q_BLUETOOTH_EXPORT) |
| 141 | |
| 142 | #endif // QLOWENERGYSERVICE_H |
| 143 | |