Skip to content

Commit

Permalink
nimble/host: Fix legacy advertising API with EXT_ADV enabled
Browse files Browse the repository at this point in the history
Configured advertisign instance is required in order to set advertising
data when extended advertising HCI is used. Since when legacy PDU are
in use we can set both AD and SCAN_RSP we can easily pre-configure
selected instance just for this purpose and re-configure it before
starting advertising without affecting set AD/SCAN_RSP.
  • Loading branch information
sjanc committed May 17, 2024
1 parent 7ec17b6 commit 04fea98
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions nimble/host/src/ble_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ struct ble_gap_slave_state {

static bssnz_t struct ble_gap_slave_state ble_gap_slave[BLE_ADV_INSTANCES];

#if MYNEWT_VAL(BLE_EXT_ADV)
static bool ext_adv_legacy_configured = false;
#endif

struct ble_gap_update_entry {
SLIST_ENTRY(ble_gap_update_entry) next;
struct ble_gap_upd_params params;
Expand Down Expand Up @@ -1471,6 +1475,8 @@ ble_gap_reset_state(int reason)
ble_gap_adv_finished(i, reason, 0, 0);
}
}

ext_adv_legacy_configured = false;
#else
if (ble_gap_adv_active_instance(0)) {
/* Indicate to application that advertising has stopped. */
Expand Down Expand Up @@ -2552,7 +2558,13 @@ ble_gap_adv_stop(void)
return rc;
}

return ble_gap_ext_adv_remove(MYNEWT_VAL(BLE_HS_EXT_ADV_LEGACY_INSTANCE));
rc = ble_gap_ext_adv_remove(MYNEWT_VAL(BLE_HS_EXT_ADV_LEGACY_INSTANCE));
if (rc) {
return rc;
}

ext_adv_legacy_configured = false;
return 0;
#else
int rc;

Expand Down Expand Up @@ -2804,13 +2816,15 @@ ble_gap_adv_start(uint8_t own_addr_type, const ble_addr_t *direct_addr,
ext_params.filter_policy = adv_params->filter_policy;
ext_params.high_duty_directed = adv_params->high_duty_cycle;

/* configure instance 0 */
/* configure legacy instance */
rc = ble_gap_ext_adv_configure(MYNEWT_VAL(BLE_HS_EXT_ADV_LEGACY_INSTANCE),
&ext_params, NULL, cb, cb_arg);
if (rc) {
return rc;
}

ext_adv_legacy_configured = true;

if (duration_ms == BLE_HS_FOREVER) {
duration = 0;
} else {
Expand Down Expand Up @@ -2900,6 +2914,38 @@ ble_gap_adv_start(uint8_t own_addr_type, const ble_addr_t *direct_addr,
#endif
}

#if MYNEWT_VAL(BLE_EXT_ADV)
static int
ble_gap_ext_adv_legacy_preconfigure(void)
{
struct ble_gap_ext_adv_params ext_params;
int rc;

if (ext_adv_legacy_configured) {
return 0;
}

memset(&ext_params, 0, sizeof(ext_params));
ext_params.own_addr_type = BLE_ADDR_RANDOM;
ext_params.primary_phy = BLE_HCI_LE_PHY_1M;
ext_params.secondary_phy = BLE_HCI_LE_PHY_1M;
ext_params.tx_power = 127;
ext_params.sid = 0;
ext_params.legacy_pdu = 1;
ext_params.scannable = 1;
ext_params.connectable = 1;

rc = ble_gap_ext_adv_configure(MYNEWT_VAL(BLE_HS_EXT_ADV_LEGACY_INSTANCE),
&ext_params, NULL, NULL, NULL);
if (rc) {
return rc;
}

ext_adv_legacy_configured = true;
return 0;
}
#endif

int
ble_gap_adv_set_data(const uint8_t *data, int data_len)
{
Expand All @@ -2913,6 +2959,11 @@ ble_gap_adv_set_data(const uint8_t *data, int data_len)
return BLE_HS_EINVAL;
}

rc = ble_gap_ext_adv_legacy_preconfigure();
if (rc) {
return rc;
}

mbuf = os_msys_get_pkthdr(data_len, 0);
if (!mbuf) {
return BLE_HS_ENOMEM;
Expand Down Expand Up @@ -2966,6 +3017,11 @@ ble_gap_adv_rsp_set_data(const uint8_t *data, int data_len)
return BLE_HS_EINVAL;
}

rc = ble_gap_ext_adv_legacy_preconfigure();
if (rc) {
return rc;
}

mbuf = os_msys_get_pkthdr(data_len, 0);
if (!mbuf) {
return BLE_HS_ENOMEM;
Expand Down

0 comments on commit 04fea98

Please sign in to comment.