summaryrefslogtreecommitdiffstats
path: root/src/bluetooth/qlowenergycharacteristicdata.cpp
blob: 9e0d69ad35903adf62e0a8938d38fbe041c16488 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/***************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtBluetooth module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qlowenergycharacteristicdata.h"

#include "qlowenergydescriptordata.h"

#include <QtCore/qbytearray.h>
#include <QtCore/qloggingcategory.h>
#include <QtCore/qdebug.h>

#include <climits>

QT_BEGIN_NAMESPACE

Q_DECLARE_LOGGING_CATEGORY(QT_BT)

struct QLowEnergyCharacteristicDataPrivate : public QSharedData
{
    QLowEnergyCharacteristicDataPrivate()
        : properties(QLowEnergyCharacteristic::Unknown)
        , minimumValueLength(0)
        , maximumValueLength(INT_MAX)
    {}

    QBluetoothUuid uuid;
    QLowEnergyCharacteristic::PropertyTypes properties;
    QList<QLowEnergyDescriptorData> descriptors;
    QByteArray value;
    QBluetooth::AttAccessConstraints readConstraints;
    QBluetooth::AttAccessConstraints writeConstraints;
    int minimumValueLength;
    int maximumValueLength;
};

/*!
    \since 5.7
    \class QLowEnergyCharacteristicData
    \brief The QLowEnergyCharacteristicData class is used to set up GATT service data.
    \inmodule QtBluetooth
    \ingroup shared

    An Object of this class provides a characteristic to be added to a
    \l QLowEnergyServiceData object via \l QLowEnergyServiceData::addCharacteristic().

    \sa QLowEnergyServiceData
    \sa QLowEnergyController::addService
*/

/*! Creates a new invalid object of this class. */
QLowEnergyCharacteristicData::QLowEnergyCharacteristicData()
    : d(new QLowEnergyCharacteristicDataPrivate)
{
}

/*! Constructs a new object of this class that is a copy of \a other. */
QLowEnergyCharacteristicData::QLowEnergyCharacteristicData(const QLowEnergyCharacteristicData &other)
    : d(other.d)
{
}

/*! Destroys this object. */
QLowEnergyCharacteristicData::~QLowEnergyCharacteristicData()
{
}

/*! Makes this object a copy of \a other and returns the new value of this object. */
QLowEnergyCharacteristicData &QLowEnergyCharacteristicData::operator=(const QLowEnergyCharacteristicData &other)
{
    d = other.d;
    return *this;
}

/*! Returns the UUID of this characteristic. */
QBluetoothUuid QLowEnergyCharacteristicData::uuid() const
{
    return d->uuid;
}

/*! Sets the UUID of this characteristic to \a uuid. */
void QLowEnergyCharacteristicData::setUuid(const QBluetoothUuid &uuid)
{
    d->uuid = uuid;
}

/*! Returns the value of this characteristic. */
QByteArray QLowEnergyCharacteristicData::value() const
{
    return d->value;
}

/*! Sets the value of this characteristic to \a value. */
void QLowEnergyCharacteristicData::setValue(const QByteArray &value)
{
    d->value = value;
}

/*! Returns the properties of this characteristic. */
QLowEnergyCharacteristic::PropertyTypes QLowEnergyCharacteristicData::properties() const
{
    return d->properties;
}

/*! Sets the properties of this characteristic to \a properties. */
void QLowEnergyCharacteristicData::setProperties(QLowEnergyCharacteristic::PropertyTypes properties)
{
    d->properties = properties;
}

/*! Returns the descriptors of this characteristic. */
QList<QLowEnergyDescriptorData> QLowEnergyCharacteristicData::descriptors() const
{
    return d->descriptors;
}

/*!
  Sets the descriptors of this characteristic to \a descriptors. Only valid descriptors
  are considered.
  \sa addDescriptor()
  */
void QLowEnergyCharacteristicData::setDescriptors(const QList<QLowEnergyDescriptorData> &descriptors)
{
    d->descriptors.clear();
    foreach (const QLowEnergyDescriptorData &desc, descriptors)
        addDescriptor(desc);
}

/*!
  Adds \a descriptor to the list of descriptors of this characteristic, if it is valid.
  \sa setDescriptors()
 */
void QLowEnergyCharacteristicData::addDescriptor(const QLowEnergyDescriptorData &descriptor)
{
    if (descriptor.isValid())
        d->descriptors << descriptor;
    else
        qCWarning(QT_BT) << "not adding invalid descriptor to characteristic";
}

/*!
  Specifies that clients need to fulfill \a constraints to read the value of this characteristic.
 */
void QLowEnergyCharacteristicData::setReadConstraints(QBluetooth::AttAccessConstraints constraints)
{
    d->readConstraints = constraints;
}

/*!
  Returns the constraints needed for a client to read the value of this characteristic.
  If \l properties() does not include \l QLowEnergyCharacteristic::Read, this value is irrelevant.
  By default, there are no read constraints.
 */
QBluetooth::AttAccessConstraints QLowEnergyCharacteristicData::readConstraints() const
{
    return d->readConstraints;
}

/*!
  Specifies that clients need to fulfill \a constraints to write the value of this characteristic.
 */
void QLowEnergyCharacteristicData::setWriteConstraints(QBluetooth::AttAccessConstraints constraints)
{
    d->writeConstraints = constraints;
}

/*!
  Returns the constraints needed for a client to write the value of this characteristic.
  If \l properties() does not include either of \l QLowEnergyCharacteristic::Write,
  \l QLowEnergyCharacteristic::WriteNoResponse and \l QLowEnergyCharacteristic::WriteSigned,
  this value is irrelevant.
  By default, there are no write constraints.
 */
QBluetooth::AttAccessConstraints QLowEnergyCharacteristicData::writeConstraints() const
{
    return d->writeConstraints;
}

/*!
  Specifies \a minimum and \a maximum to be the smallest and largest length, respectively,
  that the value of this characteristic can have. The unit is bytes. If \a minimum and
  \a maximum are equal, the characteristic has a fixed-length value.
 */
void QLowEnergyCharacteristicData::setValueLength(int minimum, int maximum)
{
    d->minimumValueLength = minimum;
    d->maximumValueLength = qMax(minimum, maximum);
}

/*!
  Returns the minimum length in bytes that the value of this characteristic can have.
  The default is zero.
 */
int QLowEnergyCharacteristicData::minimumValueLength() const
{
    return d->minimumValueLength;
}

/*!
  Returns the maximum length in bytes that the value of this characteristic can have.
  By default, there is no limit beyond the constraints of the data type.
 */
int QLowEnergyCharacteristicData::maximumValueLength() const
{
    return d->maximumValueLength;
}

/*!
  Returns true if and only if this characteristic is valid, that is, it has a non-null UUID.
 */
bool QLowEnergyCharacteristicData::isValid() const
{
    return !uuid().isNull();
}

/*!
   \fn void QLowEnergyCharacteristicData::swap(QLowEnergyCharacteristicData &other)
    Swaps this object with \a other.
 */

/*!
   Returns \c true if \a cd1 and \a cd2 are equal with respect to their public state,
   otherwise returns \c false.
 */
bool operator==(const QLowEnergyCharacteristicData &cd1, const QLowEnergyCharacteristicData &cd2)
{
    return cd1.d == cd2.d || (
                cd1.uuid() == cd2.uuid()
                && cd1.properties() == cd2.properties()
                && cd1.descriptors() == cd2.descriptors()
                && cd1.value() == cd2.value()
                && cd1.readConstraints() == cd2.readConstraints()
                && cd1.writeConstraints() == cd2.writeConstraints()
                && cd1.minimumValueLength() == cd2.maximumValueLength()
                && cd1.maximumValueLength() == cd2.maximumValueLength());
}

/*!
   \fn bool operator!=(const QLowEnergyCharacteristicData &cd1,
                       const QLowEnergyCharacteristicData &cd2)
   Returns \c true if \a cd1 and \a cd2 are not equal with respect to their public state,
   otherwise returns \c false.
 */

QT_END_NAMESPACE