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