Skip to content

Commit 1110e52

Browse files
anhmolteivindj-nordic
authored andcommitted
lib: bluetooth: ble_adv: fix directed advertising set configuration
When configuring the advertisement set for direct advertisement with sd_ble_gap_adv_set_configure(), NULL must be passed for the advertising data argument because advertising data is not supported in this mode. The sd_ble_gap_adv_set_configure() would return NRF_ERROR_INVALID_PARAM in this case. When doing directed advertisement, the address of the peer to direct the advertisement towards needed to be specified in the advertisement parameters when calling sd_ble_gap_adv_set_configure(). Set the address in the advertisement parameters to the address supplied by the application calling the ble_adv_peer_addr_reply() function in response to the BLE_ADV_EVT_PEER_ADDR_REQUEST event. Signed-off-by: Andreas Moltumyr <[email protected]>
1 parent e6beeee commit 1110e52

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

doc/nrf-bm/release_notes/release_notes_changelog.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ Libraries
148148
* The ``slave_conn_int`` member in the :c:struct:`ble_adv_data` structure to :c:member:`ble_adv_data.periph_conn_int`.
149149
* The ``CONFIG_BLE_ADV_USE_WHITELIST`` Kconfig option to :kconfig:option:`CONFIG_BLE_ADV_USE_ALLOW_LIST`.
150150

151-
* Fixed an issue with the allow list functionality that made it possible for non-allow-listed devices to connect if advertising was started in either directed or directed high duty mode, but the directed modes had been disabled with Kconfig options.
151+
* Fixed:
152+
153+
* An issue with the allow list functionality that made it possible for non-allow-listed devices to connect if advertising was started in either directed or directed high duty mode, but the directed modes had been disabled with Kconfig options.
154+
* Two minor issues with the directed advertising set configuration that caused directed advertising to not work as intended.
152155

153156
* :ref:`lib_ble_conn_params` library:
154157

lib/bluetooth/ble_adv/ble_adv.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ uint32_t ble_adv_start(struct ble_adv *ble_adv, enum ble_adv_mode mode)
337337
{
338338
uint32_t nrf_err;
339339
struct ble_adv_evt adv_evt;
340+
ble_gap_adv_data_t *adv_data;
341+
340342
if (!ble_adv) {
341343
return NRF_ERROR_NULL;
342344
}
@@ -354,6 +356,8 @@ uint32_t ble_adv_start(struct ble_adv *ble_adv, enum ble_adv_mode mode)
354356
/* Reset peer address */
355357
memset(&ble_adv->peer_address, 0, sizeof(ble_adv->peer_address));
356358

359+
adv_data = &ble_adv->adv_data;
360+
357361
/* If `mode` is initially directed advertising (and that's supported)
358362
* ask the application for a peer address
359363
*/
@@ -362,6 +366,16 @@ uint32_t ble_adv_start(struct ble_adv *ble_adv, enum ble_adv_mode mode)
362366
ble_adv->peer_addr_reply_expected = true;
363367
adv_evt.evt_type = BLE_ADV_EVT_PEER_ADDR_REQUEST;
364368
ble_adv->evt_handler(ble_adv, &adv_evt);
369+
370+
if (ble_adv->peer_addr_reply_expected == false) {
371+
/* Peer address was set by the application. Use it. */
372+
ble_adv->adv_params.p_peer_addr = &ble_adv->peer_address;
373+
adv_data = NULL;
374+
} else {
375+
/* No peer address was supplied. Skip directed advertising. */
376+
ble_adv->peer_addr_reply_expected = false;
377+
mode = BLE_ADV_MODE_FAST;
378+
}
365379
}
366380
}
367381

@@ -436,7 +450,7 @@ uint32_t ble_adv_start(struct ble_adv *ble_adv, enum ble_adv_mode mode)
436450
}
437451

438452
if (mode != BLE_ADV_MODE_IDLE) {
439-
nrf_err = sd_ble_gap_adv_set_configure(&ble_adv->adv_handle, &ble_adv->adv_data,
453+
nrf_err = sd_ble_gap_adv_set_configure(&ble_adv->adv_handle, adv_data,
440454
&ble_adv->adv_params);
441455
if (nrf_err) {
442456
LOG_ERR("Failed to set advertising data, nrf_error %#x", nrf_err);

tests/lib/bluetooth/ble_adv/src/unity_test.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,23 @@
1818
#define MAX_ADV_MODES 5
1919
uint16_t ble_adv_evt_type;
2020

21+
const ble_gap_addr_t test_addr = {
22+
.addr_id_peer = false,
23+
.addr_type = BLE_GAP_ADDR_TYPE_RANDOM_STATIC,
24+
.addr = {0x66, 0x55, 0x44, 0x33, 0x22, 0x11},
25+
};
26+
2127
static void ble_adv_evt_handler(struct ble_adv *adv, const struct ble_adv_evt *adv_evt)
2228
{
2329
ble_adv_evt_type = adv_evt->evt_type;
30+
31+
switch (adv_evt->evt_type) {
32+
case BLE_ADV_EVT_PEER_ADDR_REQUEST:
33+
ble_adv_peer_addr_reply(adv, &test_addr);
34+
break;
35+
default:
36+
break;
37+
}
2438
}
2539

2640
void test_ble_adv_conn_cfg_tag_set(void)
@@ -233,11 +247,11 @@ void test_ble_adv_start(void)
233247
TEST_ASSERT_TRUE(ble_adv_evt_type == BLE_ADV_EVT_IDLE);
234248
}
235249
if (mode[i] == BLE_ADV_MODE_DIRECTED_HIGH_DUTY) {
236-
TEST_ASSERT_TRUE(ble_adv.peer_addr_reply_expected == true);
250+
TEST_ASSERT_TRUE(ble_adv.peer_addr_reply_expected == false);
237251
TEST_ASSERT_TRUE(ble_adv_evt_type == BLE_ADV_EVT_DIRECTED_HIGH_DUTY);
238252
}
239253
if (mode[i] == BLE_ADV_MODE_DIRECTED) {
240-
TEST_ASSERT_TRUE(ble_adv.peer_addr_reply_expected == true);
254+
TEST_ASSERT_TRUE(ble_adv.peer_addr_reply_expected == false);
241255
TEST_ASSERT_TRUE(ble_adv_evt_type == BLE_ADV_EVT_DIRECTED);
242256
}
243257
if (mode[i] == BLE_ADV_MODE_FAST) {

0 commit comments

Comments
 (0)