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 "qlowenergyadvertisingparameters.h" |
41 | |
42 | QT_BEGIN_NAMESPACE |
43 | |
44 | class QLowEnergyAdvertisingParametersPrivate : public QSharedData |
45 | { |
46 | public: |
47 | QLowEnergyAdvertisingParametersPrivate() |
48 | : filterPolicy(QLowEnergyAdvertisingParameters::IgnoreWhiteList) |
49 | , mode(QLowEnergyAdvertisingParameters::AdvInd) |
50 | , minInterval(1280) |
51 | , maxInterval(1280) |
52 | { |
53 | } |
54 | |
55 | QList<QLowEnergyAdvertisingParameters::AddressInfo> whiteList; |
56 | QLowEnergyAdvertisingParameters::FilterPolicy filterPolicy; |
57 | QLowEnergyAdvertisingParameters::Mode mode; |
58 | int minInterval; |
59 | int maxInterval; |
60 | }; |
61 | |
62 | /*! |
63 | \since 5.7 |
64 | \class QLowEnergyAdvertisingParameters |
65 | \brief The QLowEnergyAdvertisingParameters class represents the parameters used for |
66 | Bluetooth Low Energy advertising. |
67 | \inmodule QtBluetooth |
68 | \ingroup shared |
69 | |
70 | When running the advertising procedure, a number of parameters can be configured, such as |
71 | how fast to advertise or which clients, if any, can connect to the advertising device. |
72 | These parameters are set via this class, and their values will be used when advertising |
73 | is started by calling \l QLowEnergyController::startAdvertising(). |
74 | |
75 | \sa QLowEnergyAdvertisingData |
76 | \sa QLowEnergyController::startAdvertising() |
77 | */ |
78 | |
79 | /*! |
80 | \enum QLowEnergyAdvertisingParameters::Mode |
81 | |
82 | Specifies in which way to advertise. |
83 | \value AdvInd |
84 | For non-directed, connectable advertising. Advertising is not directed to |
85 | one specific device and a device seeing the advertisement can connect to the |
86 | advertising device or send scan requests. |
87 | \value AdvScanInd |
88 | For non-directed, scannable advertising. Advertising is not directed to |
89 | one specific device and a device seeing the advertisement can send a scan |
90 | request to the advertising device, but cannot connect to it. |
91 | \value AdvNonConnInd |
92 | For non-directed, non-connectable advertising. Advertising is not directed to |
93 | one specific device. A device seeing the advertisement cannot connect to the |
94 | advertising device, nor can it send a scan request. This mode thus implies |
95 | pure broadcasting. |
96 | */ |
97 | |
98 | /*! |
99 | \enum QLowEnergyAdvertisingParameters::FilterPolicy |
100 | |
101 | Specifies the semantics of the white list. |
102 | \value IgnoreWhiteList |
103 | The value of the white list is ignored, that is, no filtering takes place for |
104 | either scan or connection requests when using undirected advertising. |
105 | \value UseWhiteListForScanning |
106 | The white list is used when handling scan requests, but is ignored for connection |
107 | requests. |
108 | \value UseWhiteListForConnecting |
109 | The white list is used when handling connection requests, but is ignored for scan |
110 | requests. |
111 | \value UseWhiteListForScanningAndConnecting |
112 | The white list is used for both connection and scan requests. |
113 | |
114 | \sa QLowEnergyAdvertisingParameters::whiteList() |
115 | */ |
116 | |
117 | /*! |
118 | \class QLowEnergyAdvertisingParameters::AddressInfo |
119 | \inmodule QtBluetooth |
120 | \since 5.7 |
121 | |
122 | \brief The QLowEnergyAdvertisingParameters::AddressInfo defines the elements of a white list. |
123 | |
124 | A list of QLowEnergyAdvertisingParameters::AddressInfo instances is passed to |
125 | \l QLowEnergyAdvertisingParameters::setWhiteList(). White lists are used to |
126 | restrict the devices which have the permission to interact with the peripheral. |
127 | The permitted type of interaction is defined by |
128 | \l QLowEnergyAdvertisingParameters::FilterPolicy. |
129 | |
130 | \sa QLowEnergyAdvertisingParameters::whiteList() |
131 | */ |
132 | |
133 | /*! |
134 | \variable QLowEnergyAdvertisingParameters::AddressInfo::address |
135 | |
136 | This is the Bluetooth address of a remote device. |
137 | */ |
138 | |
139 | /*! |
140 | \variable QLowEnergyAdvertisingParameters::AddressInfo::type |
141 | |
142 | The type of the address (public or private). |
143 | The \l AddressInfo default constructor initialises this value to |
144 | \l QLowEnergyController::PublicAddress. |
145 | */ |
146 | |
147 | /*! |
148 | \fn QLowEnergyAdvertisingParameters::AddressInfo::AddressInfo(const QBluetoothAddress &addr, QLowEnergyController::RemoteAddressType type) |
149 | |
150 | Constructs a new AddressInfo instance. \a addr represents the Bluetooth address of |
151 | the remote device and \a type the nature of the address. |
152 | */ |
153 | |
154 | /*! |
155 | \fn QLowEnergyAdvertisingParameters::AddressInfo::AddressInfo() |
156 | |
157 | Constructs a default constructed AddressInfo instance. |
158 | |
159 | By default the \l AddressInfo::type member is set to |
160 | \l QLowEnergyController::PublicAddress and the \l AddressInfo::address |
161 | member has a null address. |
162 | */ |
163 | |
164 | /*! |
165 | Constructs a new object of this class. All values are initialized to their defaults |
166 | according to the Bluetooth Low Energy specification. |
167 | */ |
168 | QLowEnergyAdvertisingParameters::QLowEnergyAdvertisingParameters() |
169 | : d(new QLowEnergyAdvertisingParametersPrivate) |
170 | { |
171 | } |
172 | |
173 | /*! Constructs a new object of this class that is a copy of \a other. */ |
174 | QLowEnergyAdvertisingParameters::QLowEnergyAdvertisingParameters(const QLowEnergyAdvertisingParameters &other) |
175 | : d(other.d) |
176 | { |
177 | } |
178 | |
179 | /*! Destroys this object. */ |
180 | QLowEnergyAdvertisingParameters::~QLowEnergyAdvertisingParameters() |
181 | { |
182 | } |
183 | |
184 | /*! Makes this object a copy of \a other and returns the new value of this object. */ |
185 | QLowEnergyAdvertisingParameters &QLowEnergyAdvertisingParameters::operator=(const QLowEnergyAdvertisingParameters &other) |
186 | { |
187 | d = other.d; |
188 | return *this; |
189 | } |
190 | |
191 | /*! Sets the advertising mode to \a mode. */ |
192 | void QLowEnergyAdvertisingParameters::setMode(QLowEnergyAdvertisingParameters::Mode mode) |
193 | { |
194 | d->mode = mode; |
195 | } |
196 | |
197 | /*! |
198 | Returns the advertising mode. The default is \l QLowEnergyAdvertisingParameters::AdvInd. |
199 | */ |
200 | QLowEnergyAdvertisingParameters::Mode QLowEnergyAdvertisingParameters::mode() const |
201 | { |
202 | return d->mode; |
203 | } |
204 | |
205 | /*! |
206 | Sets the white list that is potentially used for filtering scan and connection requests. |
207 | The \a whiteList parameter is the list of addresses to use for filtering, and \a policy |
208 | specifies how exactly to use \a whiteList. |
209 | */ |
210 | void QLowEnergyAdvertisingParameters::setWhiteList(const QList<AddressInfo> &whiteList, |
211 | FilterPolicy policy) |
212 | { |
213 | d->whiteList = whiteList; |
214 | d->filterPolicy = policy; |
215 | } |
216 | |
217 | /*! |
218 | Returns the white list used for filtering scan and connection requests. |
219 | By default, this list is empty. |
220 | */ |
221 | QList<QLowEnergyAdvertisingParameters::AddressInfo> QLowEnergyAdvertisingParameters::whiteList() const |
222 | { |
223 | return d->whiteList; |
224 | } |
225 | |
226 | /*! |
227 | Returns the filter policy that determines how the white list is used. The default |
228 | is \l QLowEnergyAdvertisingParameters::IgnoreWhiteList. |
229 | */ |
230 | QLowEnergyAdvertisingParameters::FilterPolicy QLowEnergyAdvertisingParameters::filterPolicy() const |
231 | { |
232 | return d->filterPolicy; |
233 | } |
234 | |
235 | /*! |
236 | Sets the advertising interval. This is a range that gives the controller an upper and a lower |
237 | bound for how often to send the advertising data. Both \a minimum and \a maximum are given |
238 | in milliseconds. |
239 | If \a maximum is smaller than \a minimum, it will be set to the value of \a minimum. |
240 | \note There are limits for the minimum and maximum interval; the exact values depend on |
241 | the mode. If they are exceeded, the lowest or highest possible value will be used, |
242 | respectively. |
243 | */ |
244 | void QLowEnergyAdvertisingParameters::setInterval(quint16 minimum, quint16 maximum) |
245 | { |
246 | d->minInterval = minimum; |
247 | d->maxInterval = qMax(a: minimum, b: maximum); |
248 | } |
249 | |
250 | /*! |
251 | Returns the minimum advertising interval in milliseconds. The default is 1280. |
252 | */ |
253 | int QLowEnergyAdvertisingParameters::minimumInterval() const |
254 | { |
255 | return d->minInterval; |
256 | } |
257 | |
258 | /*! |
259 | Returns the maximum advertising interval in milliseconds. The default is 1280. |
260 | */ |
261 | int QLowEnergyAdvertisingParameters::maximumInterval() const |
262 | { |
263 | return d->maxInterval; |
264 | } |
265 | |
266 | /*! |
267 | \fn void QLowEnergyAdvertisingParameters::swap(QLowEnergyAdvertisingParameters &other) |
268 | Swaps this object with \a other. |
269 | */ |
270 | |
271 | /*! |
272 | Returns \a true if \a p1 and \a p2 are equal with respect to their public state, |
273 | otherwise returns false. |
274 | */ |
275 | bool (const QLowEnergyAdvertisingParameters &p1, |
276 | const QLowEnergyAdvertisingParameters &p2) |
277 | { |
278 | if (p1.d == p2.d) |
279 | return true; |
280 | return p1.filterPolicy() == p2.filterPolicy() |
281 | && p1.minimumInterval() == p2.minimumInterval() |
282 | && p1.maximumInterval() == p2.maximumInterval() |
283 | && p1.mode() == p2.mode() |
284 | && p1.whiteList() == p2.whiteList(); |
285 | } |
286 | |
287 | /*! |
288 | \fn bool operator!=(const QLowEnergyAdvertisingParameters &p1, |
289 | const QLowEnergyAdvertisingParameters &p2) |
290 | Returns \a true if \a p1 and \a p2 are not equal with respect to their public state, |
291 | otherwise returns false. |
292 | */ |
293 | |
294 | QT_END_NAMESPACE |
295 | |