summaryrefslogtreecommitdiffstats
path: root/src/bluetooth/qlowenergyadvertisingdata.cpp
blob: bf41c051e599de7f92d9aa9821752ed483837716 (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
281
282
283
284
285
286
/***************************************************************************
**
** 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 "qlowenergyadvertisingdata.h"

#include <cstring>

QT_BEGIN_NAMESPACE

class QLowEnergyAdvertisingDataPrivate : public QSharedData
{
public:
    QLowEnergyAdvertisingDataPrivate()
        : manufacturerId(QLowEnergyAdvertisingData::invalidManufacturerId())
        , discoverability(QLowEnergyAdvertisingData::DiscoverabilityNone)
        , includePowerLevel(false)
    {
    }

    QString localName;
    QByteArray manufacturerData;
    QByteArray rawData;
    QList<QBluetoothUuid> services;
    quint16 manufacturerId;
    QLowEnergyAdvertisingData::Discoverability discoverability;
    bool includePowerLevel;
};

/*!
    \since 5.7
    \class QLowEnergyAdvertisingData
    \brief The QLowEnergyAdvertisingData class represents the data to be broadcast during
           Bluetooth Low Energy advertising.
    \inmodule QtBluetooth
    \ingroup shared

    This data can include the device name, GATT services offered by the device, and so on.
    The data set via this class will be used when advertising is started by calling
    \l QLowEnergyController::startAdvertising(). Objects of this class can represent an
    Advertising Data packet or a Scan Response packet.
    \note The actual data packets sent over the advertising channel cannot contain more than 31
          bytes. If the variable-length data set via this class exceeds that limit, it will
          be left out of the packet or truncated, depending on the type.

    \sa QLowEnergyAdvertisingParameters
    \sa QLowEnergyController::startAdvertising()
*/

/*!
   \enum QLowEnergyAdvertisingData::Discoverability

   The discoverability of the advertising device as defined by the Generic Access Profile.

   \value DiscoverabilityNone
       The advertising device does not wish to be discoverable by scanning devices.
   \value DiscoverabilityLimited
       The advertising device wishes to be discoverable with a high priority. Note that this mode
       is not compatible with using a white list. The value of
       \l QLowEnergyAdvertisingParameters::filterPolicy() is always assumed to be
       \l QLowEnergyAdvertisingParameters::IgnoreWhiteList when limited discoverability
       is used.
   \value DiscoverabilityGeneral
       The advertising device wishes to be discoverable by scanning devices.
 */

/*!
   Creates a new object of this class. All values are initialized to their defaults
   according to the Bluetooth Low Energy specification.
 */
QLowEnergyAdvertisingData::QLowEnergyAdvertisingData() : d(new QLowEnergyAdvertisingDataPrivate)
{
}

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

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

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

/*!
   Specifies that \a name should be broadcast as the name of the device. If the full name does not
   fit into the advertising data packet, an abbreviated name is sent, as described by the
   Bluetooth Low Energy specification.
 */
void QLowEnergyAdvertisingData::setLocalName(const QString &name)
{
    d->localName = name;
}

/*!
   Returns the name of the local device that is to be advertised.
 */
QString QLowEnergyAdvertisingData::localName() const
{
    return d->localName;
}

/*!
   Sets the manufacturer id and data. The \a id parameter is a company identifier as assigned
   by the Bluetooth SIG. The \a data parameter is an arbitrary value.
 */
void QLowEnergyAdvertisingData::setManufacturerData(quint16 id, const QByteArray &data)
{
    d->manufacturerId = id;
    d->manufacturerData = data;
}

/*!
   Returns the manufacturer id.
   The default is \l QLowEnergyAdvertisingData::invalidManufacturerId(), which means
   the data will not be advertised.
 */
quint16 QLowEnergyAdvertisingData::manufacturerId() const
{
    return d->manufacturerId;
}

/*!
   Returns the manufacturer data. The default is an empty byte array.
 */
QByteArray QLowEnergyAdvertisingData::manufacturerData() const
{
    return d->manufacturerData;
}

/*!
   Specifies whether to include the device's transmit power level in the advertising data. If
   \a doInclude is \c true, the data will be included, otherwise it will not.
 */
void QLowEnergyAdvertisingData::setIncludePowerLevel(bool doInclude)
{
    d->includePowerLevel = doInclude;
}

/*!
   Returns whether to include the device's transmit power level in the advertising data.
   The default is \c false.
 */
bool QLowEnergyAdvertisingData::includePowerLevel() const
{
    return d->includePowerLevel;
}

/*!
   Sets the discoverability type of the advertising device to \a mode.
   \note Discoverability information can only appear in an actual advertising data packet. If
         this object acts as scan response data, a call to this function will have no effect
         on the scan response sent.
 */
void QLowEnergyAdvertisingData::setDiscoverability(QLowEnergyAdvertisingData::Discoverability mode)
{
    d->discoverability = mode;
}

/*!
   Returns the discoverability mode of the advertising device.
   The default is \l DiscoverabilityNone.
 */
QLowEnergyAdvertisingData::Discoverability QLowEnergyAdvertisingData::discoverability() const
{
    return d->discoverability;
}

/*!
   Specifies that the service UUIDs in \a services should be advertised.
   If the entire list does not fit into the packet, an incomplete list is sent as specified
   by the Bluetooth Low Energy specification.
 */
void QLowEnergyAdvertisingData::setServices(const QList<QBluetoothUuid> &services)
{
    d->services = services;
}

/*!
   Returns the list of service UUIDs to be advertised.
   By default, this list is empty.
 */
QList<QBluetoothUuid> QLowEnergyAdvertisingData::services() const
{
    return d->services;
}

/*!
  Sets the data to be advertised to \a data. If the value is not an empty byte array, it will
  be sent as-is as the advertising data and all other data in this object will be ignored.
  This can be used to send non-standard data.
  \note If \a data is longer than 31 bytes, it will be truncated. It is the caller's responsibility
        to ensure that \a data is well-formed.
 */
void QLowEnergyAdvertisingData::setRawData(const QByteArray &data)
{
    d->rawData = data;
}

/*!
  Returns the user-supplied raw data to be advertised. The default is an empty byte array.
 */
QByteArray QLowEnergyAdvertisingData::rawData() const
{
    return d->rawData;
}

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

/*!
   Returns \c true if \a data1 and \a data2 are equal with respect to their public state,
   otherwise returns \c false.
 */
bool operator==(const QLowEnergyAdvertisingData &data1, const QLowEnergyAdvertisingData &data2)
{
    if (data1.d == data2.d)
        return true;
    return data1.discoverability() == data2.discoverability()
            && data1.includePowerLevel() == data2.includePowerLevel()
            && data1.localName() == data2.localName()
            && data1.manufacturerData() == data2.manufacturerData()
            && data1.manufacturerId() == data2.manufacturerId()
            && data1.services() == data2.services()
            && data1.rawData() == data2.rawData();
}

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

/*!
   \fn static quint16 QLowEnergyAdvertisingData::invalidManufacturerId();
   Returns an invalid manufacturer id. If this value is set as the manufacturer id
   (which it is by default), no manufacturer data will be present in the advertising data.
 */

QT_END_NAMESPACE