forked from h2zero/esp-nimble-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNimBLEAdvertising.cpp
More file actions
623 lines (554 loc) · 20.8 KB
/
NimBLEAdvertising.cpp
File metadata and controls
623 lines (554 loc) · 20.8 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
* Copyright 2020-2025 Ryan Powell <ryan@nable-embedded.io> and
* esp-nimble-cpp, NimBLE-Arduino contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "NimBLEAdvertising.h"
#if (CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_BROADCASTER) && !MYNEWT_VAL(BLE_EXT_ADV)) || defined(_DOXYGEN_)
# if defined(CONFIG_NIMBLE_CPP_IDF)
# include "services/gap/ble_svc_gap.h"
# else
# include "nimble/nimble/host/services/gap/include/services/gap/ble_svc_gap.h"
# endif
# include "NimBLEDevice.h"
# include "NimBLEServer.h"
# include "NimBLEUtils.h"
# include "NimBLELog.h"
static const char* LOG_TAG = "NimBLEAdvertising";
/**
* @brief Construct a default advertising object.
*/
NimBLEAdvertising::NimBLEAdvertising()
: m_advData{},
m_scanData{},
m_advParams{},
m_advCompCb{nullptr},
m_slaveItvl{0},
m_duration{BLE_HS_FOREVER},
m_scanResp{false},
m_advDataSet{false} {
# if !MYNEWT_VAL(BLE_ROLE_PERIPHERAL)
m_advParams.conn_mode = BLE_GAP_CONN_MODE_NON;
# else
m_advParams.conn_mode = BLE_GAP_CONN_MODE_UND;
m_advData.setFlags(BLE_HS_ADV_F_DISC_GEN);
# endif
m_advParams.disc_mode = BLE_GAP_DISC_MODE_GEN;
} // NimBLEAdvertising
/**
* @brief Stops the current advertising and resets the advertising data to the default values.
* @return True if successful.
*/
bool NimBLEAdvertising::reset() {
if (!stop()) {
return false;
}
*this = NimBLEAdvertising();
return true;
} // reset
/**
* @brief Set the type of connectable mode to advertise.
* @param [in] mode The connectable mode:
* * BLE_GAP_CONN_MODE_NON (0) - not connectable advertising
* * BLE_GAP_CONN_MODE_DIR (1) - directed connectable advertising
* * BLE_GAP_CONN_MODE_UND (2) - undirected connectable advertising
* @return True if the connectable mode was set, false if the mode is invalid.
*/
bool NimBLEAdvertising::setConnectableMode(uint8_t mode) {
if (mode > BLE_GAP_CONN_MODE_UND) {
NIMBLE_LOGE(LOG_TAG, "Invalid connectable mode: %u", mode);
return false;
}
if (mode == BLE_GAP_CONN_MODE_NON) { // Non-connectable advertising doesn't need flags.
m_advData.setFlags(0);
}
m_advParams.conn_mode = mode;
return true;
} // setAdvertisementType
/**
* @brief Set the discoverable mode to use.
* @param [in] mode The discoverable mode:
* * BLE_GAP_DISC_MODE_NON (0) - non-discoverable
* * BLE_GAP_DISC_MODE_LTD (1) - limited discoverable
* * BLE_GAP_DISC_MODE_GEN (2) - general discoverable
* @return True if the discoverable mode was set, false if the mode is invalid.
*/
bool NimBLEAdvertising::setDiscoverableMode(uint8_t mode) {
switch (mode) {
case BLE_GAP_DISC_MODE_NON:
m_advData.setFlags(BLE_HS_ADV_F_BREDR_UNSUP);
break;
case BLE_GAP_DISC_MODE_LTD:
m_advData.setFlags(BLE_HS_ADV_F_DISC_LTD);
break;
case BLE_GAP_DISC_MODE_GEN:
m_advData.setFlags(BLE_HS_ADV_F_DISC_GEN);
break;
default:
NIMBLE_LOGE(LOG_TAG, "Invalid discoverable mode: %u", mode);
return false;
}
m_advParams.disc_mode = mode;
return true;
} // setDiscoverableMode
/**
* @brief Set the advertising interval.
* @param [in] interval The advertising interval in 0.625ms units, 0 = use default.
*/
void NimBLEAdvertising::setAdvertisingInterval(uint16_t interval) {
m_advParams.itvl_min = interval;
m_advParams.itvl_max = interval;
} // setAdvertisingInterval
/**
* @brief Set the minimum advertising interval.
* @param [in] minInterval Minimum value for advertising interval in 0.625ms units, 0 = use default.
*/
void NimBLEAdvertising::setMinInterval(uint16_t minInterval) {
m_advParams.itvl_min = minInterval;
} // setMinInterval
/**
* @brief Set the maximum advertising interval.
* @param [in] maxInterval Maximum value for advertising interval in 0.625ms units, 0 = use default.
*/
void NimBLEAdvertising::setMaxInterval(uint16_t maxInterval) {
m_advParams.itvl_max = maxInterval;
} // setMaxInterval
/**
* @brief Enable scan response data.
* @param [in] enable If true, scan response data will be available, false disabled, default = disabled.
* @details The scan response data is sent in response to a scan request from a peer device.
*/
void NimBLEAdvertising::enableScanResponse(bool enable) {
m_scanResp = enable;
m_advDataSet = false;
} // enableScanResponse
/**
* @brief Set the filtering for the scan filter.
* @param [in] scanRequestWhitelistOnly If true, only allow scan requests from those on the white list.
* @param [in] connectWhitelistOnly If true, only allow connections from those on the white list.
*/
void NimBLEAdvertising::setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly) {
if (!scanRequestWhitelistOnly && !connectWhitelistOnly) {
m_advParams.filter_policy = BLE_HCI_ADV_FILT_NONE;
return;
}
if (scanRequestWhitelistOnly && !connectWhitelistOnly) {
m_advParams.filter_policy = BLE_HCI_ADV_FILT_SCAN;
return;
}
if (!scanRequestWhitelistOnly && connectWhitelistOnly) {
m_advParams.filter_policy = BLE_HCI_ADV_FILT_CONN;
return;
}
if (scanRequestWhitelistOnly && connectWhitelistOnly) {
m_advParams.filter_policy = BLE_HCI_ADV_FILT_BOTH;
return;
}
} // setScanFilter
/**
* @brief Start advertising.
* @param [in] duration The duration, in milliseconds, to advertise, 0 == advertise forever.
* @param [in] dirAddr The address of a peer to directly advertise to.
* @return True if advertising started successfully.
*/
bool NimBLEAdvertising::start(uint32_t duration, const NimBLEAddress* dirAddr) {
NIMBLE_LOGD(LOG_TAG,
">> Advertising start: duration=%" PRIu32 ", dirAddr=%s",
duration,
dirAddr ? dirAddr->toString().c_str() : "NULL");
if (!NimBLEDevice::m_synced) {
NIMBLE_LOGE(LOG_TAG, "Host not synced!");
return false;
}
if (ble_gap_adv_active()) {
NIMBLE_LOGW(LOG_TAG, "Advertising already active");
return true;
}
# if MYNEWT_VAL(BLE_ROLE_PERIPHERAL)
NimBLEServer* pServer = NimBLEDevice::getServer();
if (pServer != nullptr) {
pServer->start(); // make sure the GATT server is ready before advertising
}
# endif
if (!m_advDataSet) {
if (!setAdvertisementData(m_advData)) {
return false;
}
if (m_scanResp && m_scanData.getPayload().size() > 0) {
if (!setScanResponseData(m_scanData)) {
return false;
}
}
}
// Save the duration incase of host reset so we can restart with the same params
m_duration = duration;
if (duration == 0) {
duration = BLE_HS_FOREVER;
}
# if MYNEWT_VAL(BLE_ROLE_PERIPHERAL)
int rc = ble_gap_adv_start(NimBLEDevice::m_ownAddrType,
(dirAddr != nullptr) ? dirAddr->getBase() : NULL,
duration,
&m_advParams,
(pServer != nullptr) ? NimBLEServer::handleGapEvent : NimBLEAdvertising::handleGapEvent,
this);
# else
int rc =
ble_gap_adv_start(NimBLEDevice::m_ownAddrType, NULL, duration, &m_advParams, NimBLEAdvertising::handleGapEvent, this);
# endif
if (rc != 0 && rc != BLE_HS_EALREADY) {
NIMBLE_LOGE(LOG_TAG, "Error enabling advertising; rc=%d, %s", rc, NimBLEUtils::returnCodeToString(rc));
return false;
}
NIMBLE_LOGD(LOG_TAG, "<< Advertising start");
return true;
} // start
/**
* @brief Stop advertising.
* @return True if advertising stopped successfully.
*/
bool NimBLEAdvertising::stop() {
int rc = ble_gap_adv_stop();
if (rc != 0 && rc != BLE_HS_EALREADY) {
NIMBLE_LOGE(LOG_TAG, "ble_gap_adv_stop rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
return false;
}
return true;
} // stop
/**
* @brief Set the callback to be invoked when advertising stops.
* @param [in] callback The callback to be invoked when advertising stops.
*/
void NimBLEAdvertising::setAdvertisingCompleteCallback(advCompleteCB_t callback) {
m_advCompCb = callback;
} // setAdvertisingCompleteCallback
/**
* @brief Check if currently advertising.
* @return True if advertising is active.
*/
bool NimBLEAdvertising::isAdvertising() {
return ble_gap_adv_active();
} // isAdvertising
/*
* Host reset seems to clear advertising data,
* we need clear the flag so it reloads it.
*/
void NimBLEAdvertising::onHostSync() {
m_advDataSet = false;
// If we were advertising forever, restart it now
if (m_duration == 0) {
start(m_duration);
} else {
// Otherwise we should tell the app that advertising stopped.
if (m_advCompCb != nullptr) {
m_advCompCb(this);
}
}
} // onHostSync
/**
* @brief Handler for gap events when not using peripheral role.
* @param [in] event the event data.
* @param [in] arg pointer to the advertising instance.
*/
int NimBLEAdvertising::handleGapEvent(struct ble_gap_event* event, void* arg) {
NimBLEAdvertising* pAdv = (NimBLEAdvertising*)arg;
if (event->type == BLE_GAP_EVENT_ADV_COMPLETE) {
switch (event->adv_complete.reason) {
// Don't call the callback if host reset, we want to
// preserve the active flag until re-sync to restart advertising.
case BLE_HS_ETIMEOUT_HCI:
case BLE_HS_EOS:
case BLE_HS_ECONTROLLER:
case BLE_HS_ENOTSYNCED:
NIMBLE_LOGE(LOG_TAG, "host reset, rc=%d", event->adv_complete.reason);
NimBLEDevice::onReset(event->adv_complete.reason);
return 0;
default:
break;
}
if (pAdv->m_advCompCb != nullptr) {
pAdv->m_advCompCb(pAdv);
}
}
return 0;
} // handleGapEvent
/* -------------------------------------------------------------------------- */
/* Advertisement Data */
/* -------------------------------------------------------------------------- */
/**
* @brief Set the advertisement data that is to be broadcast in a regular advertisement.
* @param [in] data The data to be broadcast.
* @return True if the data was set successfully.
*/
bool NimBLEAdvertising::setAdvertisementData(const NimBLEAdvertisementData& data) {
int rc = ble_gap_adv_set_data(&data.getPayload()[0], data.getPayload().size());
if (rc != 0) {
NIMBLE_LOGE(LOG_TAG, "ble_gap_adv_set_data: %d %s", rc, NimBLEUtils::returnCodeToString(rc));
return false;
}
NIMBLE_LOGD(LOG_TAG, "setAdvertisementData: %s", data.toString().c_str());
m_advData = data; // make a copy in the member object in case this is custom.
m_advDataSet = true; // Set the flag that indicates the data was set already so we don't set it again.
return true;
} // setAdvertisementData
/**
* @brief Get the current advertisement data.
* @returns a reference to the current advertisement data.
*/
const NimBLEAdvertisementData& NimBLEAdvertising::getAdvertisementData() {
return m_advData;
} // getAdvertisementData
/**
* @brief Set the data that is to be provided in a scan response.
* @param [in] data The data to be provided in the scan response
* @return True if the data was set successfully.
* @details The scan response data is sent in response to a scan request from a peer device.
* If this is set without setting the advertisement data when advertising starts this may be overwritten.
*/
bool NimBLEAdvertising::setScanResponseData(const NimBLEAdvertisementData& data) {
int rc = ble_gap_adv_rsp_set_data(&data.getPayload()[0], data.getPayload().size());
if (rc != 0) {
NIMBLE_LOGE(LOG_TAG, "ble_gap_adv_rsp_set_data: %d %s", rc, NimBLEUtils::returnCodeToString(rc));
return false;
}
NIMBLE_LOGD(LOG_TAG, "setScanResponseData: %s", data.toString().c_str());
m_scanData = data; // copy the data into the member object in case this is custom.
return true;
} // setScanResponseData
/**
* @brief Get the current scan response data.
* @returns a reference to the current scan response data.
*/
const NimBLEAdvertisementData& NimBLEAdvertising::getScanData() {
return m_advData;
} // getScanData
/**
* @brief Clear the advertisement and scan response data and set the flags to BLE_HS_ADV_F_DISC_GEN.
*/
void NimBLEAdvertising::clearData() {
m_advData.clearData();
m_advData.setFlags(BLE_HS_ADV_F_DISC_GEN);
m_scanData.clearData();
m_advDataSet = false;
} // clearData
/**
* @brief Refresh advertsing data dynamically without stop/start cycle.
* For instance allows refreshing manufacturer data dynamically.
*
* @return True if the data was set successfully.
* @details If scan response is enabled it will be refreshed as well.
*/
bool NimBLEAdvertising::refreshAdvertisingData() {
bool success = setAdvertisementData(m_advData);
if (m_scanResp) {
success = setScanResponseData(m_scanData);
}
return success;
} // refreshAdvertisingData
/**
* @brief Add a service uuid to exposed list of services.
* @param [in] serviceUUID The UUID of the service to expose.
* @return True if the service was added successfully.
*/
bool NimBLEAdvertising::addServiceUUID(const NimBLEUUID& serviceUUID) {
if (!m_advData.addServiceUUID(serviceUUID)) {
if (!m_scanData.addServiceUUID(serviceUUID)) {
return false;
}
}
m_advDataSet = false;
return true;
} // addServiceUUID
/**
* @brief Add a service uuid to exposed list of services.
* @param [in] serviceUUID The string representation of the service to expose.
* @return True if the service was added successfully.
*/
bool NimBLEAdvertising::addServiceUUID(const char* serviceUUID) {
return addServiceUUID(NimBLEUUID(serviceUUID));
} // addServiceUUID
/**
* @brief Remove a service UUID from the advertisement.
* @param [in] serviceUUID The UUID of the service to remove.
* @return True if the service was removed successfully.
*/
bool NimBLEAdvertising::removeServiceUUID(const NimBLEUUID& serviceUUID) {
bool success = m_advData.removeServiceUUID(serviceUUID);
success = m_scanData.removeServiceUUID(serviceUUID);
m_advDataSet = false;
return success;
} // removeServiceUUID
/**
* @brief Remove a service UUID from the advertisement.
* @param [in] serviceUUID The UUID of the service to remove.
* @return True if the service was removed successfully.
*/
bool NimBLEAdvertising::removeServiceUUID(const char* serviceUUID) {
return removeServiceUUID(NimBLEUUID(serviceUUID));
} // removeServiceUUID
/**
* @brief Remove all service UUIDs from the advertisement.
* @return True if the services were removed successfully.
*/
bool NimBLEAdvertising::removeServices() {
bool success = m_advData.removeServices();
success = m_advDataSet = m_scanData.removeServices();
m_advDataSet = false;
return success;
} // removeServices
/**
* @brief Set the device appearance in the advertising data.
* @param [in] appearance The appearance of the device in the advertising data.
* If the appearance value is 0 then the appearance will not be in the advertisement.
* @return True if the appearance was set successfully.
*/
bool NimBLEAdvertising::setAppearance(uint16_t appearance) {
if (!m_advData.setAppearance(appearance)) {
if (!m_scanData.setAppearance(appearance)) {
return false;
}
}
m_advDataSet = false;
return true;
} // setAppearance
/**
* @brief Set the preferred min and max connection intervals to advertise.
* @param [in] minInterval The minimum preferred connection interval.
* @param [in] maxInterval The Maximum preferred connection interval.
* @return True if the preferred connection interval was set successfully.
* @details Range = 0x0006(7.5ms) to 0x0C80(4000ms), values not within the range will be limited to this range.
*/
bool NimBLEAdvertising::setPreferredParams(uint16_t minInterval, uint16_t maxInterval) {
if (!m_advData.setPreferredParams(minInterval, maxInterval)) {
if (!m_scanData.setPreferredParams(minInterval, maxInterval)) {
return false;
}
}
m_advDataSet = false;
return true;
} // setPreferredParams
/**
* @brief Add the transmission power level to the advertisement packet.
* @return True if the transmission power level was added successfully.
*/
bool NimBLEAdvertising::addTxPower() {
if (!m_advData.addTxPower()) {
if (!m_scanData.addTxPower()) {
return false;
}
}
m_advDataSet = false;
return true;
} // addTxPower
/**
* @brief Set the advertised name of the device.
* @param [in] name The name to advertise.
* @return True if the name was set successfully.
* @note If the name is too long it will be truncated.
* @details If scan response is enabled the name will be set in the scan response data.
*/
bool NimBLEAdvertising::setName(const std::string& name) {
if (m_scanResp && m_scanData.setName(name)) {
m_advDataSet = false;
return true;
}
if (!m_advData.setName(name)) {
return false;
}
m_advDataSet = false;
return true;
} // setName
/**
* @brief Set the advertised manufacturer data.
* @param [in] data The data to advertise.
* @param [in] length The length of the data.
* @return True if the manufacturer data was set successfully.
*/
bool NimBLEAdvertising::setManufacturerData(const uint8_t* data, size_t length) {
if (!m_advData.setManufacturerData(data, length)) {
if (!m_scanData.setManufacturerData(data, length)) {
return false;
}
}
m_advDataSet = false;
return true;
} // setManufacturerData
/**
* @brief Set the advertised manufacturer data.
* @param [in] data The data to advertise.
* @return True if the manufacturer data was set successfully.
*/
bool NimBLEAdvertising::setManufacturerData(const std::string& data) {
return setManufacturerData(reinterpret_cast<const uint8_t*>(data.data()), data.length());
} // setManufacturerData
/**
* @brief Set the advertised manufacturer data.
* @param [in] data The data to advertise.
* @return True if the manufacturer data was set successfully.
*/
bool NimBLEAdvertising::setManufacturerData(const std::vector<uint8_t>& data) {
return setManufacturerData(&data[0], data.size());
} // setManufacturerData
/**
* @brief Set the advertised URI.
* @param [in] uri The URI to advertise.
* @return True if the URI was set successfully.
*/
bool NimBLEAdvertising::setURI(const std::string& uri) {
if (!m_advData.setURI(uri)) {
if (!m_scanData.setURI(uri)) {
return false;
}
}
m_advDataSet = false;
return true;
} // setURI
/**
* @brief Set the service data advertised for the UUID.
* @param [in] uuid The UUID the service data belongs to.
* @param [in] data The data to advertise.
* @param [in] length The length of the data.
* @return True if the service data was set successfully.
* @note If data length is 0 the service data will not be advertised.
*/
bool NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const uint8_t* data, size_t length) {
if (!m_advData.setServiceData(uuid, data, length)) {
if (!m_scanData.setServiceData(uuid, data, length)) {
return false;
}
}
m_advDataSet = false;
return true;
} // setServiceData
/**
* @brief Set the service data advertised for the UUID.
* @param [in] uuid The UUID the service data belongs to.
* @param [in] data The data to advertise.
* @return True if the service data was set successfully.
* @note If data length is 0 the service data will not be advertised.
*/
bool NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const std::vector<uint8_t>& data) {
return setServiceData(uuid, data.data(), data.size());
} // setServiceData
/**
* @brief Set the service data advertised for the UUID.
* @param [in] uuid The UUID the service data belongs to.
* @param [in] data The data to advertise.
* @return True if the service data was set successfully.
* @note If data length is 0 the service data will not be advertised.
*/
bool NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const std::string& data) {
return setServiceData(uuid, reinterpret_cast<const uint8_t*>(data.data()), data.length());
} // setServiceData
#endif // (CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_BROADCASTER) && !MYNEWT_VAL(BLE_EXT_ADV)) || defined(_DOXYGEN_)