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 "qlowenergyadvertisingdata.h" |
41 | |
42 | #include <cstring> |
43 | |
44 | QT_BEGIN_NAMESPACE |
45 | |
46 | class QLowEnergyAdvertisingDataPrivate : public QSharedData |
47 | { |
48 | public: |
49 | QLowEnergyAdvertisingDataPrivate() |
50 | : manufacturerId(QLowEnergyAdvertisingData::invalidManufacturerId()) |
51 | , discoverability(QLowEnergyAdvertisingData::DiscoverabilityNone) |
52 | , includePowerLevel(false) |
53 | { |
54 | } |
55 | |
56 | QString localName; |
57 | QByteArray manufacturerData; |
58 | QByteArray rawData; |
59 | QList<QBluetoothUuid> services; |
60 | quint16 manufacturerId; |
61 | QLowEnergyAdvertisingData::Discoverability discoverability; |
62 | bool includePowerLevel; |
63 | }; |
64 | |
65 | /*! |
66 | \since 5.7 |
67 | \class QLowEnergyAdvertisingData |
68 | \brief The QLowEnergyAdvertisingData class represents the data to be broadcast during |
69 | Bluetooth Low Energy advertising. |
70 | \inmodule QtBluetooth |
71 | \ingroup shared |
72 | |
73 | This data can include the device name, GATT services offered by the device, and so on. |
74 | The data set via this class will be used when advertising is started by calling |
75 | \l QLowEnergyController::startAdvertising(). Objects of this class can represent an |
76 | Advertising Data packet or a Scan Response packet. |
77 | \note The actual data packets sent over the advertising channel cannot contain more than 31 |
78 | bytes. If the variable-length data set via this class exceeds that limit, it will |
79 | be left out of the packet or truncated, depending on the type. |
80 | |
81 | \sa QLowEnergyAdvertisingParameters |
82 | \sa QLowEnergyController::startAdvertising() |
83 | */ |
84 | |
85 | /*! |
86 | \enum QLowEnergyAdvertisingData::Discoverability |
87 | |
88 | The discoverability of the advertising device as defined by the Generic Access Profile. |
89 | |
90 | \value DiscoverabilityNone |
91 | The advertising device does not wish to be discoverable by scanning devices. |
92 | \value DiscoverabilityLimited |
93 | The advertising device wishes to be discoverable with a high priority. Note that this mode |
94 | is not compatible with using a white list. The value of |
95 | \l QLowEnergyAdvertisingParameters::filterPolicy() is always assumed to be |
96 | \l QLowEnergyAdvertisingParameters::IgnoreWhiteList when limited discoverability |
97 | is used. |
98 | \value DiscoverabilityGeneral |
99 | The advertising device wishes to be discoverable by scanning devices. |
100 | */ |
101 | |
102 | /*! |
103 | Creates a new object of this class. All values are initialized to their defaults |
104 | according to the Bluetooth Low Energy specification. |
105 | */ |
106 | QLowEnergyAdvertisingData::QLowEnergyAdvertisingData() : d(new QLowEnergyAdvertisingDataPrivate) |
107 | { |
108 | } |
109 | |
110 | /*! Constructs a new object of this class that is a copy of \a other. */ |
111 | QLowEnergyAdvertisingData::QLowEnergyAdvertisingData(const QLowEnergyAdvertisingData &other) |
112 | : d(other.d) |
113 | { |
114 | } |
115 | |
116 | /*! Destroys this object. */ |
117 | QLowEnergyAdvertisingData::~QLowEnergyAdvertisingData() |
118 | { |
119 | } |
120 | |
121 | /*! Makes this object a copy of \a other and returns the new value of this object. */ |
122 | QLowEnergyAdvertisingData &QLowEnergyAdvertisingData::operator=(const QLowEnergyAdvertisingData &other) |
123 | { |
124 | d = other.d; |
125 | return *this; |
126 | } |
127 | |
128 | /*! |
129 | Specifies that \a name should be broadcast as the name of the device. If the full name does not |
130 | fit into the advertising data packet, an abbreviated name is sent, as described by the |
131 | Bluetooth Low Energy specification. |
132 | |
133 | On Android, the local name cannot be changed. Android always uses the device name. |
134 | If this local name is not empty, the Android implementation includes the device name |
135 | in the advertisement packet; otherwise the device name is omitted from the advertisement |
136 | packet. |
137 | |
138 | \sa localName() |
139 | */ |
140 | void QLowEnergyAdvertisingData::setLocalName(const QString &name) |
141 | { |
142 | d->localName = name; |
143 | } |
144 | |
145 | /*! |
146 | Returns the name of the local device that is to be advertised. |
147 | |
148 | \sa setLocalName() |
149 | */ |
150 | QString QLowEnergyAdvertisingData::localName() const |
151 | { |
152 | return d->localName; |
153 | } |
154 | |
155 | /*! |
156 | Sets the manufacturer id and data. The \a id parameter is a company identifier as assigned |
157 | by the Bluetooth SIG. The \a data parameter is an arbitrary value. |
158 | */ |
159 | void QLowEnergyAdvertisingData::setManufacturerData(quint16 id, const QByteArray &data) |
160 | { |
161 | d->manufacturerId = id; |
162 | d->manufacturerData = data; |
163 | } |
164 | |
165 | /*! |
166 | Returns the manufacturer id. |
167 | The default is \l QLowEnergyAdvertisingData::invalidManufacturerId(), which means |
168 | the data will not be advertised. |
169 | */ |
170 | quint16 QLowEnergyAdvertisingData::manufacturerId() const |
171 | { |
172 | return d->manufacturerId; |
173 | } |
174 | |
175 | /*! |
176 | Returns the manufacturer data. The default is an empty byte array. |
177 | */ |
178 | QByteArray QLowEnergyAdvertisingData::manufacturerData() const |
179 | { |
180 | return d->manufacturerData; |
181 | } |
182 | |
183 | /*! |
184 | Specifies whether to include the device's transmit power level in the advertising data. If |
185 | \a doInclude is \c true, the data will be included, otherwise it will not. |
186 | */ |
187 | void QLowEnergyAdvertisingData::setIncludePowerLevel(bool doInclude) |
188 | { |
189 | d->includePowerLevel = doInclude; |
190 | } |
191 | |
192 | /*! |
193 | Returns whether to include the device's transmit power level in the advertising data. |
194 | The default is \c false. |
195 | */ |
196 | bool QLowEnergyAdvertisingData::includePowerLevel() const |
197 | { |
198 | return d->includePowerLevel; |
199 | } |
200 | |
201 | /*! |
202 | Sets the discoverability type of the advertising device to \a mode. |
203 | \note Discoverability information can only appear in an actual advertising data packet. If |
204 | this object acts as scan response data, a call to this function will have no effect |
205 | on the scan response sent. |
206 | */ |
207 | void QLowEnergyAdvertisingData::setDiscoverability(QLowEnergyAdvertisingData::Discoverability mode) |
208 | { |
209 | d->discoverability = mode; |
210 | } |
211 | |
212 | /*! |
213 | Returns the discoverability mode of the advertising device. |
214 | The default is \l DiscoverabilityNone. |
215 | */ |
216 | QLowEnergyAdvertisingData::Discoverability QLowEnergyAdvertisingData::discoverability() const |
217 | { |
218 | return d->discoverability; |
219 | } |
220 | |
221 | /*! |
222 | Specifies that the service UUIDs in \a services should be advertised. |
223 | If the entire list does not fit into the packet, an incomplete list is sent as specified |
224 | by the Bluetooth Low Energy specification. |
225 | */ |
226 | void QLowEnergyAdvertisingData::setServices(const QList<QBluetoothUuid> &services) |
227 | { |
228 | d->services = services; |
229 | } |
230 | |
231 | /*! |
232 | Returns the list of service UUIDs to be advertised. |
233 | By default, this list is empty. |
234 | */ |
235 | QList<QBluetoothUuid> QLowEnergyAdvertisingData::services() const |
236 | { |
237 | return d->services; |
238 | } |
239 | |
240 | /*! |
241 | Sets the data to be advertised to \a data. If the value is not an empty byte array, it will |
242 | be sent as-is as the advertising data and all other data in this object will be ignored. |
243 | This can be used to send non-standard data. |
244 | \note If \a data is longer than 31 bytes, it will be truncated. It is the caller's responsibility |
245 | to ensure that \a data is well-formed. |
246 | */ |
247 | void QLowEnergyAdvertisingData::setRawData(const QByteArray &data) |
248 | { |
249 | d->rawData = data; |
250 | } |
251 | |
252 | /*! |
253 | Returns the user-supplied raw data to be advertised. The default is an empty byte array. |
254 | */ |
255 | QByteArray QLowEnergyAdvertisingData::rawData() const |
256 | { |
257 | return d->rawData; |
258 | } |
259 | |
260 | /*! |
261 | \fn void QLowEnergyAdvertisingData::swap(QLowEnergyAdvertisingData &other) |
262 | Swaps this object with \a other. |
263 | */ |
264 | |
265 | /*! |
266 | Returns \c true if \a data1 and \a data2 are equal with respect to their public state, |
267 | otherwise returns \c false. |
268 | */ |
269 | bool operator==(const QLowEnergyAdvertisingData &data1, const QLowEnergyAdvertisingData &data2) |
270 | { |
271 | if (data1.d == data2.d) |
272 | return true; |
273 | return data1.discoverability() == data2.discoverability() |
274 | && data1.includePowerLevel() == data2.includePowerLevel() |
275 | && data1.localName() == data2.localName() |
276 | && data1.manufacturerData() == data2.manufacturerData() |
277 | && data1.manufacturerId() == data2.manufacturerId() |
278 | && data1.services() == data2.services() |
279 | && data1.rawData() == data2.rawData(); |
280 | } |
281 | |
282 | /*! |
283 | \fn bool operator!=(const QLowEnergyAdvertisingData &data1, |
284 | const QLowEnergyAdvertisingData &data2) |
285 | Returns \c true if \a data1 and \a data2 are not equal with respect to their public state, |
286 | otherwise returns \c false. |
287 | */ |
288 | |
289 | /*! |
290 | \fn static quint16 QLowEnergyAdvertisingData::invalidManufacturerId(); |
291 | Returns an invalid manufacturer id. If this value is set as the manufacturer id |
292 | (which it is by default), no manufacturer data will be present in the advertising data. |
293 | */ |
294 | |
295 | QT_END_NAMESPACE |
296 | |