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 "qlowenergycharacteristicdata.h" |
41 | |
42 | #include "qlowenergydescriptordata.h" |
43 | |
44 | #include <QtCore/qbytearray.h> |
45 | #include <QtCore/qloggingcategory.h> |
46 | #include <QtCore/qdebug.h> |
47 | |
48 | #include <climits> |
49 | |
50 | QT_BEGIN_NAMESPACE |
51 | |
52 | Q_DECLARE_LOGGING_CATEGORY(QT_BT) |
53 | |
54 | struct QLowEnergyCharacteristicDataPrivate : public QSharedData |
55 | { |
56 | QLowEnergyCharacteristicDataPrivate() |
57 | : properties(QLowEnergyCharacteristic::Unknown) |
58 | , minimumValueLength(0) |
59 | , maximumValueLength(INT_MAX) |
60 | {} |
61 | |
62 | QBluetoothUuid uuid; |
63 | QLowEnergyCharacteristic::PropertyTypes properties; |
64 | QList<QLowEnergyDescriptorData> descriptors; |
65 | QByteArray value; |
66 | QBluetooth::AttAccessConstraints readConstraints; |
67 | QBluetooth::AttAccessConstraints writeConstraints; |
68 | int minimumValueLength; |
69 | int maximumValueLength; |
70 | }; |
71 | |
72 | /*! |
73 | \since 5.7 |
74 | \class QLowEnergyCharacteristicData |
75 | \brief The QLowEnergyCharacteristicData class is used to set up GATT service data. |
76 | \inmodule QtBluetooth |
77 | \ingroup shared |
78 | |
79 | An Object of this class provides a characteristic to be added to a |
80 | \l QLowEnergyServiceData object via \l QLowEnergyServiceData::addCharacteristic(). |
81 | |
82 | \sa QLowEnergyServiceData |
83 | \sa QLowEnergyController::addService |
84 | */ |
85 | |
86 | /*! Creates a new invalid object of this class. */ |
87 | QLowEnergyCharacteristicData::QLowEnergyCharacteristicData() |
88 | : d(new QLowEnergyCharacteristicDataPrivate) |
89 | { |
90 | } |
91 | |
92 | /*! Constructs a new object of this class that is a copy of \a other. */ |
93 | QLowEnergyCharacteristicData::QLowEnergyCharacteristicData(const QLowEnergyCharacteristicData &other) |
94 | : d(other.d) |
95 | { |
96 | } |
97 | |
98 | /*! Destroys this object. */ |
99 | QLowEnergyCharacteristicData::~QLowEnergyCharacteristicData() |
100 | { |
101 | } |
102 | |
103 | /*! Makes this object a copy of \a other and returns the new value of this object. */ |
104 | QLowEnergyCharacteristicData &QLowEnergyCharacteristicData::operator=(const QLowEnergyCharacteristicData &other) |
105 | { |
106 | d = other.d; |
107 | return *this; |
108 | } |
109 | |
110 | /*! Returns the UUID of this characteristic. */ |
111 | QBluetoothUuid QLowEnergyCharacteristicData::uuid() const |
112 | { |
113 | return d->uuid; |
114 | } |
115 | |
116 | /*! Sets the UUID of this characteristic to \a uuid. */ |
117 | void QLowEnergyCharacteristicData::setUuid(const QBluetoothUuid &uuid) |
118 | { |
119 | d->uuid = uuid; |
120 | } |
121 | |
122 | /*! Returns the value of this characteristic. */ |
123 | QByteArray QLowEnergyCharacteristicData::value() const |
124 | { |
125 | return d->value; |
126 | } |
127 | |
128 | /*! Sets the value of this characteristic to \a value. */ |
129 | void QLowEnergyCharacteristicData::setValue(const QByteArray &value) |
130 | { |
131 | d->value = value; |
132 | } |
133 | |
134 | /*! Returns the properties of this characteristic. */ |
135 | QLowEnergyCharacteristic::PropertyTypes QLowEnergyCharacteristicData::properties() const |
136 | { |
137 | return d->properties; |
138 | } |
139 | |
140 | /*! Sets the properties of this characteristic to \a properties. */ |
141 | void QLowEnergyCharacteristicData::setProperties(QLowEnergyCharacteristic::PropertyTypes properties) |
142 | { |
143 | d->properties = properties; |
144 | } |
145 | |
146 | /*! Returns the descriptors of this characteristic. */ |
147 | QList<QLowEnergyDescriptorData> QLowEnergyCharacteristicData::descriptors() const |
148 | { |
149 | return d->descriptors; |
150 | } |
151 | |
152 | /*! |
153 | Sets the descriptors of this characteristic to \a descriptors. Only valid descriptors |
154 | are considered. |
155 | \sa addDescriptor() |
156 | */ |
157 | void QLowEnergyCharacteristicData::setDescriptors(const QList<QLowEnergyDescriptorData> &descriptors) |
158 | { |
159 | d->descriptors.clear(); |
160 | for (const QLowEnergyDescriptorData &desc : descriptors) |
161 | addDescriptor(descriptor: desc); |
162 | } |
163 | |
164 | /*! |
165 | Adds \a descriptor to the list of descriptors of this characteristic, if it is valid. |
166 | \sa setDescriptors() |
167 | */ |
168 | void QLowEnergyCharacteristicData::addDescriptor(const QLowEnergyDescriptorData &descriptor) |
169 | { |
170 | if (descriptor.isValid()) |
171 | d->descriptors << descriptor; |
172 | else |
173 | qCWarning(QT_BT) << "not adding invalid descriptor to characteristic" ; |
174 | } |
175 | |
176 | /*! |
177 | Specifies that clients need to fulfill \a constraints to read the value of this characteristic. |
178 | */ |
179 | void QLowEnergyCharacteristicData::setReadConstraints(QBluetooth::AttAccessConstraints constraints) |
180 | { |
181 | d->readConstraints = constraints; |
182 | } |
183 | |
184 | /*! |
185 | Returns the constraints needed for a client to read the value of this characteristic. |
186 | If \l properties() does not include \l QLowEnergyCharacteristic::Read, this value is irrelevant. |
187 | By default, there are no read constraints. |
188 | */ |
189 | QBluetooth::AttAccessConstraints QLowEnergyCharacteristicData::readConstraints() const |
190 | { |
191 | return d->readConstraints; |
192 | } |
193 | |
194 | /*! |
195 | Specifies that clients need to fulfill \a constraints to write the value of this characteristic. |
196 | */ |
197 | void QLowEnergyCharacteristicData::setWriteConstraints(QBluetooth::AttAccessConstraints constraints) |
198 | { |
199 | d->writeConstraints = constraints; |
200 | } |
201 | |
202 | /*! |
203 | Returns the constraints needed for a client to write the value of this characteristic. |
204 | If \l properties() does not include either of \l QLowEnergyCharacteristic::Write, |
205 | \l QLowEnergyCharacteristic::WriteNoResponse and \l QLowEnergyCharacteristic::WriteSigned, |
206 | this value is irrelevant. |
207 | By default, there are no write constraints. |
208 | */ |
209 | QBluetooth::AttAccessConstraints QLowEnergyCharacteristicData::writeConstraints() const |
210 | { |
211 | return d->writeConstraints; |
212 | } |
213 | |
214 | /*! |
215 | Specifies \a minimum and \a maximum to be the smallest and largest length, respectively, |
216 | that the value of this characteristic can have. The unit is bytes. If \a minimum and |
217 | \a maximum are equal, the characteristic has a fixed-length value. |
218 | */ |
219 | void QLowEnergyCharacteristicData::setValueLength(int minimum, int maximum) |
220 | { |
221 | d->minimumValueLength = minimum; |
222 | d->maximumValueLength = qMax(a: minimum, b: maximum); |
223 | } |
224 | |
225 | /*! |
226 | Returns the minimum length in bytes that the value of this characteristic can have. |
227 | The default is zero. |
228 | */ |
229 | int QLowEnergyCharacteristicData::minimumValueLength() const |
230 | { |
231 | return d->minimumValueLength; |
232 | } |
233 | |
234 | /*! |
235 | Returns the maximum length in bytes that the value of this characteristic can have. |
236 | By default, there is no limit beyond the constraints of the data type. |
237 | */ |
238 | int QLowEnergyCharacteristicData::maximumValueLength() const |
239 | { |
240 | return d->maximumValueLength; |
241 | } |
242 | |
243 | /*! |
244 | Returns true if and only if this characteristic is valid, that is, it has a non-null UUID. |
245 | */ |
246 | bool QLowEnergyCharacteristicData::isValid() const |
247 | { |
248 | return !uuid().isNull(); |
249 | } |
250 | |
251 | /*! |
252 | \fn void QLowEnergyCharacteristicData::swap(QLowEnergyCharacteristicData &other) |
253 | Swaps this object with \a other. |
254 | */ |
255 | |
256 | /*! |
257 | Returns \c true if \a cd1 and \a cd2 are equal with respect to their public state, |
258 | otherwise returns \c false. |
259 | */ |
260 | bool operator==(const QLowEnergyCharacteristicData &cd1, const QLowEnergyCharacteristicData &cd2) |
261 | { |
262 | return cd1.d == cd2.d || ( |
263 | cd1.uuid() == cd2.uuid() |
264 | && cd1.properties() == cd2.properties() |
265 | && cd1.descriptors() == cd2.descriptors() |
266 | && cd1.value() == cd2.value() |
267 | && cd1.readConstraints() == cd2.readConstraints() |
268 | && cd1.writeConstraints() == cd2.writeConstraints() |
269 | && cd1.minimumValueLength() == cd2.maximumValueLength() |
270 | && cd1.maximumValueLength() == cd2.maximumValueLength()); |
271 | } |
272 | |
273 | /*! |
274 | \fn bool operator!=(const QLowEnergyCharacteristicData &cd1, |
275 | const QLowEnergyCharacteristicData &cd2) |
276 | Returns \c true if \a cd1 and \a cd2 are not equal with respect to their public state, |
277 | otherwise returns \c false. |
278 | */ |
279 | |
280 | QT_END_NAMESPACE |
281 | |