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