| 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 | #include "qlowenergydescriptordata.h" |
| 41 | |
| 42 | #include <QtCore/qbytearray.h> |
| 43 | |
| 44 | QT_BEGIN_NAMESPACE |
| 45 | |
| 46 | struct QLowEnergyDescriptorDataPrivate : public QSharedData |
| 47 | { |
| 48 | QLowEnergyDescriptorDataPrivate() : readable(true), writable(true) {} |
| 49 | |
| 50 | QBluetoothUuid uuid; |
| 51 | QByteArray value; |
| 52 | QBluetooth::AttAccessConstraints readConstraints; |
| 53 | QBluetooth::AttAccessConstraints writeConstraints; |
| 54 | bool readable; |
| 55 | bool writable; |
| 56 | }; |
| 57 | |
| 58 | /*! |
| 59 | \since 5.7 |
| 60 | \class QLowEnergyDescriptorData |
| 61 | \brief The QLowEnergyDescriptorData class is used to create GATT service data. |
| 62 | \inmodule QtBluetooth |
| 63 | \ingroup shared |
| 64 | |
| 65 | An object of this class provides a descriptor to be added to a |
| 66 | \l QLowEnergyCharacteristicData object via \l QLowEnergyCharacteristicData::addDescriptor(). |
| 67 | |
| 68 | \note The member functions related to access permissions are only applicable to those |
| 69 | types of descriptors for which the Bluetooth specification does not prescribe if |
| 70 | and how their values can be accessed. |
| 71 | |
| 72 | \sa QLowEnergyCharacteristicData |
| 73 | \sa QLowEnergyServiceData |
| 74 | \sa QLowEnergyController::addService |
| 75 | */ |
| 76 | |
| 77 | /*! Creates a new invalid object of this class. */ |
| 78 | QLowEnergyDescriptorData::QLowEnergyDescriptorData() : d(new QLowEnergyDescriptorDataPrivate) |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | /*! |
| 83 | Creates a new object of this class with UUID and value being provided by \a uuid and \a value, |
| 84 | respectively. |
| 85 | */ |
| 86 | QLowEnergyDescriptorData::QLowEnergyDescriptorData(const QBluetoothUuid &uuid, |
| 87 | const QByteArray &value) |
| 88 | : d(new QLowEnergyDescriptorDataPrivate) |
| 89 | { |
| 90 | setUuid(uuid); |
| 91 | setValue(value); |
| 92 | } |
| 93 | |
| 94 | /*! Constructs a new object of this class that is a copy of \a other. */ |
| 95 | QLowEnergyDescriptorData::QLowEnergyDescriptorData(const QLowEnergyDescriptorData &other) |
| 96 | : d(other.d) |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | /*! Destroys this object. */ |
| 101 | QLowEnergyDescriptorData::~QLowEnergyDescriptorData() |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | /*! Makes this object a copy of \a other and returns the new value of this object. */ |
| 106 | QLowEnergyDescriptorData &QLowEnergyDescriptorData::operator=(const QLowEnergyDescriptorData &other) |
| 107 | { |
| 108 | d = other.d; |
| 109 | return *this; |
| 110 | } |
| 111 | |
| 112 | /*! Returns the value of this descriptor. */ |
| 113 | QByteArray QLowEnergyDescriptorData::value() const |
| 114 | { |
| 115 | return d->value; |
| 116 | } |
| 117 | |
| 118 | /*! |
| 119 | Sets the value of this descriptor to \a value. It will be sent to a peer device |
| 120 | exactly the way it is provided here, so callers need to take care of things such as endianness. |
| 121 | */ |
| 122 | void QLowEnergyDescriptorData::setValue(const QByteArray &value) |
| 123 | { |
| 124 | d->value = value; |
| 125 | } |
| 126 | |
| 127 | /*! Returns the UUID of this descriptor. */ |
| 128 | QBluetoothUuid QLowEnergyDescriptorData::uuid() const |
| 129 | { |
| 130 | return d->uuid; |
| 131 | } |
| 132 | |
| 133 | /*! Sets the UUID of this descriptor to \a uuid. */ |
| 134 | void QLowEnergyDescriptorData::setUuid(const QBluetoothUuid &uuid) |
| 135 | { |
| 136 | d->uuid = uuid; |
| 137 | } |
| 138 | |
| 139 | /*! Returns true if and only if this object has a non-null UUID. */ |
| 140 | bool QLowEnergyDescriptorData::isValid() const |
| 141 | { |
| 142 | return !uuid().isNull(); |
| 143 | } |
| 144 | |
| 145 | /*! |
| 146 | Specifies whether the value of this descriptor is \a readable and if so, under which |
| 147 | \a constraints. |
| 148 | \sa setWritePermissions() |
| 149 | */ |
| 150 | void QLowEnergyDescriptorData::setReadPermissions(bool readable, |
| 151 | QBluetooth::AttAccessConstraints constraints) |
| 152 | { |
| 153 | d->readable = readable; |
| 154 | d->readConstraints = constraints; |
| 155 | } |
| 156 | |
| 157 | /*! Returns \c true if the value of this descriptor is readable and \c false otherwise. */ |
| 158 | bool QLowEnergyDescriptorData::isReadable() const |
| 159 | { |
| 160 | return d->readable; |
| 161 | } |
| 162 | |
| 163 | /*! |
| 164 | Returns the constraints under which the value of this descriptor can be read. This value |
| 165 | is only relevant if \l isReadable() returns \c true. |
| 166 | */ |
| 167 | QBluetooth::AttAccessConstraints QLowEnergyDescriptorData::readConstraints() const |
| 168 | { |
| 169 | return d->readConstraints; |
| 170 | } |
| 171 | |
| 172 | /*! |
| 173 | Specifies whether the value of this descriptor is \a writable and if so, under which |
| 174 | \a constraints. |
| 175 | \sa setReadPermissions() |
| 176 | */ |
| 177 | void QLowEnergyDescriptorData::setWritePermissions(bool writable, |
| 178 | QBluetooth::AttAccessConstraints constraints) |
| 179 | { |
| 180 | d->writable = writable; |
| 181 | d->writeConstraints = constraints; |
| 182 | } |
| 183 | |
| 184 | /*! Returns \c true if the value of this descriptor is writable and \c false otherwise. */ |
| 185 | bool QLowEnergyDescriptorData::isWritable() const |
| 186 | { |
| 187 | return d->writable; |
| 188 | } |
| 189 | |
| 190 | /*! |
| 191 | Returns the constraints under which the value of this descriptor can be written. This value |
| 192 | is only relevant if \l isWritable() returns \c true. |
| 193 | */ |
| 194 | QBluetooth::AttAccessConstraints QLowEnergyDescriptorData::writeConstraints() const |
| 195 | { |
| 196 | return d->writeConstraints; |
| 197 | } |
| 198 | |
| 199 | /*! |
| 200 | \fn void QLowEnergyDescriptorData::swap(QLowEnergyDescriptorData &other) |
| 201 | Swaps this object with \a other. |
| 202 | */ |
| 203 | |
| 204 | /*! |
| 205 | Returns \c true if \a d1 and \a d2 are equal with respect to their public state, |
| 206 | otherwise returns \c false. |
| 207 | */ |
| 208 | bool operator==(const QLowEnergyDescriptorData &d1, const QLowEnergyDescriptorData &d2) |
| 209 | { |
| 210 | return d1.d == d2.d || ( |
| 211 | d1.uuid() == d2.uuid() |
| 212 | && d1.value() == d2.value() |
| 213 | && d1.isReadable() == d2.isReadable() |
| 214 | && d1.isWritable() == d2.isWritable() |
| 215 | && d1.readConstraints() == d2.readConstraints() |
| 216 | && d1.writeConstraints() == d2.writeConstraints()); |
| 217 | } |
| 218 | |
| 219 | /*! |
| 220 | \fn bool operator!=(const QLowEnergyDescriptorData &d1, const QLowEnergyDescriptorData &d2) |
| 221 | Returns \c true if \a d1 and \a d2 are not equal with respect to their public state, |
| 222 | otherwise returns \c false. |
| 223 | */ |
| 224 | |
| 225 | QT_END_NAMESPACE |
| 226 | |