Mbed OS Reference
Loading...
Searching...
No Matches
AdvertisingDataBuilder.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2020 ARM Limited
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#ifndef MBED_GAP_ADVERTISING_DATA_H__
20#define MBED_GAP_ADVERTISING_DATA_H__
21
22#include <cstdint>
23#include <cstring>
24#include <cstdlib>
25#include "platform/NonCopyable.h"
26
27#include "ble/common/UUID.h"
28#include "ble/common/BLETypes.h"
29#include "ble/common/blecommon.h"
30#include "ble/gap/AdvertisingDataTypes.h"
31#include "ble/gap/Types.h"
32
33namespace ble {
34
35/**
36 * @addtogroup ble
37 * @{
38 * @addtogroup gap
39 * @{
40 */
41
42/**
43 * Build advertising data.
44 *
45 * The builder accepts an array of bytes in input and returns the result of the
46 * construction with getAdvertisingData().
47 */
49public:
50 /** Advertising data needs a user-provided buffer to store the data.
51 *
52 * @param buffer Buffer used to store the data.
53 * @note Use Gap::getMaxAdvertisingDataLength() to find out how much can be accepted.
54 */
56
57 /** Advertising data needs a user provided buffer to store the data.
58 *
59 * @param buffer Pointer to buffer to be used for storing advertising data.
60 * @param buffer_size Size of the buffer.
61 * @note Use Gap::getMaxAdvertisingDataLength() to find out how much can be accepted.
62 */
63 AdvertisingDataBuilder(uint8_t *buffer, size_t buffer_size);
64
65 /**
66 * Get the subspan of the buffer containing valid data.
67 *
68 * @return A Span containing the payload.
69 */
71
72 /**
73 * Add a new field into the payload. Returns an error if type is already present.
74 *
75 * @note Data size for individual types cannot exceed 255 bytes.
76 *
77 * @param[in] advDataType The type of the field to add.
78 * @param[in] fieldData Span of data to add.
79 *
80 * @retval BLE_ERROR_NONE on success.
81 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
82 * @retval BLE_ERROR_OPERATION_NOT_PERMITTED if data type already present.
83 * @retval BLE_ERROR_INVALID_PARAM if size of data is too big too fit in an individual data field.
84 */
86 adv_data_type_t advDataType,
88 );
89
90 /**
91 * Replace a new field into the payload. Will fail if type is not already present.
92 *
93 * @note Data size for individual types cannot exceed 255 bytes.
94 *
95 * @param[in] advDataType The type of the field to add.
96 * @param[in] fieldData Span of data to add.
97 *
98 * @retval BLE_ERROR_NONE on success.
99 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
100 * @retval BLE_ERROR_NOT_FOUND if data type not present.
101 * @retval BLE_ERROR_INVALID_PARAM if size of data is too big too fit in an individual data field.
102 */
104 adv_data_type_t advDataType,
106 );
107
108 /**
109 * Append data to an existing field in the payload. Will fail if type is not already
110 * present.
111 *
112 * @note Data size for individual types cannot exceed 255 bytes.
113 *
114 * @param[in] advDataType The type of the field to add.
115 * @param[in] fieldData Span of data to add.
116 *
117 * @retval BLE_ERROR_NONE on success.
118 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
119 * @retval BLE_ERROR_NOT_FOUND if data type not present.
120 * @retval BLE_ERROR_INVALID_PARAM if size of data is too big too fit in an individual data field.
121 */
123 adv_data_type_t advDataType,
125 );
126
127 /**
128 * Remove existing date of given type. Will return an error if type is not present.
129 *
130 * @param[in] advDataType The type of the field to remove.
131 *
132 * @return BLE_ERROR_NONE returned on success, BLE_ERROR_INVALID_PARAM if field doesn't exist
133 */
135
136 /**
137 * Adds a new field into the payload. If the supplied advertising data type is
138 * already present in the advertising payload, then the value is replaced.
139 *
140 * @note Data size for individual types cannot exceed 255 bytes.
141 *
142 * @param[in] advDataType The type of the field to add.
143 * @param[in] fieldData Span of data to add.
144 *
145 * @retval BLE_ERROR_NONE on success.
146 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
147 * @retval BLE_ERROR_INVALID_PARAM if size of data is too big too fit in an individual data field.
148 */
150 adv_data_type_t advDataType,
152 );
153
154 /**
155 * Adds a new field into the payload. If the supplied advertising data type is
156 * already present in the advertising payload, then the value is replaced.
157 *
158 * @note Data size for individual types cannot exceed 255 bytes.
159 *
160 * @param[in] advDataType The type of the field to add.
161 * @param[in] fieldData Span of data to add.
162 *
163 * @retval BLE_ERROR_NONE on success.
164 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
165 * @retval BLE_ERROR_INVALID_PARAM if size of data is too big too fit in an individual data field.
166 */
168 adv_data_type_t advDataType,
170 );
171
172 /**
173 * Clears the advertising data payload.
174 *
175 * @post getPayloadLen() returns 0.
176 */
177 void clear();
178
179 /**
180 * Add device appearance in the advertising payload.
181 *
182 * @param[in] appearance The appearance to advertise.
183 *
184 * @retval BLE_ERROR_NONE on success.
185 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
186 *
187 * @note This call is equivalent to calling addOrReplaceData() with
188 * adv_data_type_t::APPEARANCE as the field type.
189 */
191
192 /**
193 * Add BLE flags in the advertising payload.
194 *
195 * @param[in] flags Bitfield describing the capability of the device. See
196 * allowed flags in Flags_t.
197 *
198 * @retval BLE_ERROR_NONE on success.
199 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
200 *
201 * @note This call is equivalent to calling addOrReplaceData() with
202 * adv_data_type_t::FLAGS as the field type.
203 */
205 adv_data_flags_t flags = adv_data_flags_t::default_flags
206 );
207
208 /**
209 * Add the advertising TX in the advertising payload.
210 *
211 * @param[in] txPower Transmission power level in dB.
212 *
213 * @retval BLE_ERROR_NONE on success.
214 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
215 *
216 * @note This call is equivalent to calling addOrReplaceData() with
217 * adv_data_type_t::TX_POWER_LEVEL as the field type.
218 */
219 ble_error_t setTxPowerAdvertised(advertising_power_t txPower);
220
221 /**
222 * Add device name to the advertising payload.
223 *
224 * @note Data size for individual types cannot exceed 255 bytes.
225 *
226 * @param[in] name Null terminated string containing the name.
227 * @param[in] complete Complete local name if true, otherwise
228 *
229 * @retval BLE_ERROR_NONE on success.
230 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
231 * @retval BLE_ERROR_INVALID_PARAM if size of data is too big too fit in an individual data field.
232 */
233 ble_error_t setName(const char *name, bool complete = true);
234
235 /**
236 * Add manufacturer-specific data to the advertising payload.
237 *
238 * @note Data size for individual types cannot exceed 255 bytes.
239 *
240 * @param[in] data New data to be added.
241 *
242 * @retval BLE_ERROR_NONE on success.
243 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
244 * @retval BLE_ERROR_INVALID_PARAM if size of data is too big too fit in an individual
245 * data field or the data is too small (must contain
246 * 2 bytes of manufacturer ID)
247 */
249
250 /**
251 * Add advertising interval to the payload. This field can only carry 2 bytes.
252 *
253 * @param interval Interval to advertise. Cannot be larger than 0xFFFF.
254
255 * @retval BLE_ERROR_NONE on success.
256 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
257 * @retval BLE_ERROR_INVALID_PARAM if interval value outside of valid range.
258 */
259 ble_error_t setAdvertisingInterval(adv_interval_t interval);
260
261 /**
262 * Add connection interval preferences to the payload
263 *
264 * @param min Minimum connection interval to advertise.
265 * @param max Maximum connection interval to advertise.
266
267 * @retval BLE_ERROR_NONE on success.
268 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
269 */
271 conn_interval_t min,
272 conn_interval_t max
273 );
274
275 /**
276 * Add service data data to the advertising payload.
277 *
278 * @note Data size for individual types cannot exceed 255 bytes.
279 *
280 * @param[in] service UUID of the service.
281 * @param[in] data New data to be added.
282 *
283 * @retval BLE_ERROR_NONE on success.
284 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
285 * @retval BLE_ERROR_INVALID_PARAM if size of data is too big too fit in an individual data field.
286 */
288
289 /**
290 * Add local service IDs to the advertising payload. If the data can't fit,
291 * no modification will take place.
292 *
293 * @note Data size for individual types cannot exceed 255 bytes.
294 *
295 * @param[in] data New data to be added.
296 * @param[in] complete True if this is a complete list.
297 *
298 * @retval BLE_ERROR_NONE on success.
299 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
300 * @retval BLE_ERROR_INVALID_PARAM if number of UUIDs of any one type is too high.
301 */
304 bool complete = true
305 );
306
307 /**
308 * Add a list of UUIDs of solicited services.
309 *
310 * @note Data size for individual types cannot exceed 255 bytes.
311 *
312 * @param[in] data List of 128 or 16 bit service UUIDs.
313 *
314 * @retval BLE_ERROR_NONE on success.
315 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
316 * @retval BLE_ERROR_INVALID_PARAM if number of UUIDs of any one type is too high.
317 */
319
320 /**
321 * Return a span of data containing the the type of data requested.
322 *
323 * @param[out] data Span used to return the requested data.
324 * @param[in] advDataType Data type to return.
325 *
326 * @return BLE_ERROR_NONE if data was found and BLE_ERROR_NOT_FOUND if not.
327 */
330 adv_data_type_t advDataType
331 );
332
333private:
334 /**
335 * Search advertisement data for a specific field.
336 *
337 * @param[in] type The type of the field to find.
338 *
339 * @return A pointer to the first element in the field if found. The first
340 * element being the length of the field followed by the value of the field.
341 * NULL if the field is not present in the payload.
342 */
343 uint8_t *findField(adv_data_type_t type);
344
345 /**
346 * Get field size (includes type and size bytes)
347 *
348 * @param type The field type.
349 *
350 * @return Size of the whole field including type and size bytes.
351 */
352 uint8_t getFieldSize(adv_data_type_t type);
353
354 /**
355 * Append advertising data based on the specified type.
356 *
357 * @note Data size for individual types cannot exceed 255 bytes.
358 *
359 * @param[in] advDataType Type of the new data.
360 * @param[in] fieldData Span of data to add.
361 *
362 * @retval BLE_ERROR_NONE on success.
363 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
364 */
365 ble_error_t addField(
366 adv_data_type_t advDataType,
368 );
369
370 /**
371 * Append data to a field in the advertising payload.
372 *
373 * @note Data size for individual types cannot exceed 255 bytes.
374 *
375 * @param[in] fieldData Span of data to add.
376 * @param[in] field Pointer to the field in the advertising buffer.
377 *
378 * @return BLE_ERROR_NONE on success.
379 */
380 ble_error_t appendToField(
382 uint8_t *field
383 );
384
385 /**
386 * Update in place the value of a field in the advertising payload.
387 *
388 * @note Data size for individual types cannot exceed 255 bytes.
389 *
390 * @param[in] advDataType Type of the new data.
391 * @param[in] fieldData Span of data to add.
392 * @param[in] field Pointer to the field of type @p advDataType in the
393 * advertising buffer.
394 *
395 * @retval BLE_ERROR_NONE on success.
396 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
397 */
398 ble_error_t replaceField(
399 adv_data_type_t advDataType,
401 uint8_t *field
402 );
403
404 /**
405 * Remove the field.
406 *
407 * @param[in] field Pointer to the field in the advertising buffer.
408 *
409 * @return BLE_ERROR_NONE on success.
410 */
411 ble_error_t removeField(uint8_t *field);
412
413 /**
414 * Add a list of UUIDs to given types.
415 *
416 * @note Data size for individual types cannot exceed 255 bytes.
417 *
418 * @param[in] data List of 128 or 16 bit service UUIDs.
419 * @param[in] shortType Type of field to add the short UUIDs to.
420 * @param[in] longType Type of field to add the long UUIDs to.
421 *
422 * @retval BLE_ERROR_NONE on success.
423 * @retval BLE_ERROR_BUFFER_OVERFLOW if buffer is too small to contain the new data.
424 * @retval BLE_ERROR_INVALID_PARAM if number of UUIDs of any one type is too high.
425 */
426 ble_error_t setUUIDData(
428 adv_data_type_t shortType,
429 adv_data_type_t longType
430 );
431
432private:
433 /** The memory backing the the data provided by the user. */
434 mbed::Span<uint8_t> _buffer;
435
436 /** Length of the data added to the advertising buffer. */
437 uint8_t _payload_length;
438};
439
440/**
441 * @}
442 * @}
443 */
444
445} // namespace ble
446
447#endif /* ifndef MBED_GAP_ADVERTISING_DATA_H__ */
Representation of a Universally Unique Identifier (UUID).
Definition: common/UUID.h:76
ble_error_t addData(adv_data_type_t advDataType, mbed::Span< const uint8_t > fieldData)
Add a new field into the payload.
ble_error_t setManufacturerSpecificData(mbed::Span< const uint8_t > data)
Add manufacturer-specific data to the advertising payload.
ble_error_t setConnectionIntervalPreference(conn_interval_t min, conn_interval_t max)
Add connection interval preferences to the payload.
ble_error_t setTxPowerAdvertised(advertising_power_t txPower)
Add the advertising TX in the advertising payload.
ble_error_t setFlags(adv_data_flags_t flags=adv_data_flags_t::default_flags)
Add BLE flags in the advertising payload.
AdvertisingDataBuilder(uint8_t *buffer, size_t buffer_size)
Advertising data needs a user provided buffer to store the data.
ble_error_t setAdvertisingInterval(adv_interval_t interval)
Add advertising interval to the payload.
ble_error_t getData(mbed::Span< const uint8_t > &data, adv_data_type_t advDataType)
Return a span of data containing the the type of data requested.
ble_error_t addOrReplaceData(adv_data_type_t advDataType, mbed::Span< const uint8_t > fieldData)
Adds a new field into the payload.
ble_error_t replaceData(adv_data_type_t advDataType, mbed::Span< const uint8_t > fieldData)
Replace a new field into the payload.
ble_error_t setServiceData(UUID service, mbed::Span< const uint8_t > data)
Add service data data to the advertising payload.
ble_error_t removeData(adv_data_type_t advDataType)
Remove existing date of given type.
AdvertisingDataBuilder(mbed::Span< uint8_t > buffer)
Advertising data needs a user-provided buffer to store the data.
ble_error_t appendData(adv_data_type_t advDataType, mbed::Span< const uint8_t > fieldData)
Append data to an existing field in the payload.
ble_error_t setAppearance(adv_data_appearance_t appearance)
Add device appearance in the advertising payload.
mbed::Span< const uint8_t > getAdvertisingData() const
Get the subspan of the buffer containing valid data.
ble_error_t setName(const char *name, bool complete=true)
Add device name to the advertising payload.
ble_error_t addOrAppendData(adv_data_type_t advDataType, mbed::Span< const uint8_t > fieldData)
Adds a new field into the payload.
ble_error_t setRequestedServiceList(mbed::Span< const UUID > data)
Add a list of UUIDs of solicited services.
void clear()
Clears the advertising data payload.
ble_error_t setLocalServiceList(mbed::Span< const UUID > data, bool complete=true)
Add local service IDs to the advertising payload.
ble_error_t
Error codes for the BLE API.
Entry namespace for all BLE API definitions.
Enumeration of values for the adv_data_type_t::APPEARANCE.
Set of advertising flags.
Nonowning view to a sequence of contiguous elements.
Definition: Span.h:215