File indexing completed on 2024-05-05 03:52:28

0001 /*
0002  * BluezQt - Asynchronous BlueZ wrapper library
0003  *
0004  * SPDX-FileCopyrightText: 2019 Manuel Weichselbaumer <mincequi@web.de>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #ifndef BLUEZQT_GATTCHARACTERISTIC_H
0010 #define BLUEZQT_GATTCHARACTERISTIC_H
0011 
0012 #include "bluezqt_export.h"
0013 
0014 #include <QDBusObjectPath>
0015 
0016 #include <memory>
0017 
0018 namespace BluezQt
0019 {
0020 class GattService;
0021 
0022 class BLUEZQT_EXPORT GattCharacteristic : public QObject
0023 {
0024     Q_OBJECT
0025 
0026 public:
0027     /**
0028      * Creates a new GattCharacteristic object.
0029      *
0030      * This constructor creates a characteristic with the Read and Write flags set.
0031      *
0032      * @param uuid The UUID of the characteristic.
0033      * @param service The parent service.
0034      */
0035     explicit GattCharacteristic(const QString &uuid, GattService *service);
0036 
0037     /**
0038      * Creates a new GattCharacteristic object.
0039      *
0040      * @param uuid The UUID of the characteristic.
0041      * @param flags Flags indicating the characteristic usage.
0042      * @param service The parent service.
0043      *
0044      * @since 6.0
0045      */
0046     GattCharacteristic(const QString &uuid, const QStringList &flags, GattService *service);
0047 
0048     /**
0049      * Destroys a GattCharacteristic object.
0050      */
0051     ~GattCharacteristic() override;
0052 
0053     /**
0054      * Reads the value of the characteristic.
0055      */
0056     QByteArray readValue();
0057 
0058     /**
0059      * Writes the value of the characteristic.
0060      */
0061     void writeValue(const QByteArray &value);
0062 
0063     /**
0064      * Provide a read callback to operate in *pull* mode.
0065      */
0066     using ReadCallback = std::function<QByteArray()>;
0067     void setReadCallback(ReadCallback callback);
0068 
0069     /**
0070      * 128-bit GATT characteristic UUID.
0071      *
0072      * @return uuid of characteristic
0073      */
0074     QString uuid() const;
0075 
0076     /**
0077      * The GATT service the characteristic belongs to.
0078      *
0079      * @return service this characteristic belongs to
0080      */
0081     const GattService *service() const;
0082 
0083     /**
0084      * The flags of this characteristic.
0085      *
0086      * @return flags associated with this characteristic
0087      *
0088      * @since 6.0
0089      */
0090     QStringList flags() const;
0091 
0092     /**
0093      * Enables notifications for this characteristic, if supported. Does nothing otherwise.
0094      *
0095      * @since 6.0
0096      */
0097     void startNotify();
0098 
0099     /**
0100      * Disables notifications for this characteristic.
0101      *
0102      * @since 6.0
0103      */
0104     void stopNotify();
0105 
0106     /**
0107      * Indicates if this characteristic currently has notifications enabled.
0108      *
0109      * @return True if notifications are enabled, false otherwise
0110      *
0111      * @since 6.0
0112      */
0113     bool isNotifying() const;
0114 
0115 Q_SIGNALS:
0116     /**
0117      * Indicates that a value was written.
0118      */
0119     void valueWritten(const QByteArray &value);
0120 
0121 protected:
0122     /**
0123      * D-Bus object path of the GattCharacteristic.
0124      *
0125      * The path where the GattCharacteristic will be registered.
0126      *
0127      * @note You must provide valid object path!
0128      *
0129      * @return object path of GattCharacteristic
0130      */
0131     virtual QDBusObjectPath objectPath() const;
0132 
0133 private:
0134     std::unique_ptr<class GattCharacterisiticPrivate> const d;
0135 
0136     friend class GattApplicationPrivate;
0137     friend class GattCharacteristicAdaptor;
0138     friend class GattDescriptor;
0139     friend class GattDescriptorPrivate;
0140     friend class GattManager;
0141 };
0142 
0143 } // namespace BluezQt
0144 
0145 #endif