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

source code of qtconnectivity/src/bluetooth/qlowenergydescriptordata.cpp