Skip to content

Commit

Permalink
apps/bttester: Fix use of extended advertising interface
Browse files Browse the repository at this point in the history
This shoudl allow to run GAP and GATT tests with BLE_EXT_ADV enabled.
  • Loading branch information
sjanc committed Mar 22, 2024
1 parent b1ecc2d commit 3d525a2
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions apps/bttester/src/btp_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ controller_info(const void *cmd, uint16_t cmd_len,
}

#if MYNEWT_VAL(BLE_EXT_ADV)
int adv_duration = 0;
static struct ble_gap_ext_adv_params adv_params = {
.primary_phy = BLE_HCI_LE_PHY_1M,
.secondary_phy = BLE_HCI_LE_PHY_1M,
Expand Down Expand Up @@ -350,7 +349,6 @@ static uint8_t
set_discoverable(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
#if !MYNEWT_VAL(BLE_EXT_ADV)
const struct btp_gap_set_discoverable_cmd *cp = cmd;
struct btp_gap_set_discoverable_rp *rp = rsp;

Expand All @@ -359,19 +357,25 @@ set_discoverable(const void *cmd, uint16_t cmd_len,
switch (cp->discoverable) {
case BTP_GAP_NON_DISCOVERABLE:
ad_flags &= ~(BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_DISC_LTD);
#if !MYNEWT_VAL(BLE_EXT_ADV)
adv_params.disc_mode = BLE_GAP_DISC_MODE_NON;
#endif
current_settings &= ~BIT(BTP_GAP_SETTINGS_DISCOVERABLE);
break;
case BTP_GAP_GENERAL_DISCOVERABLE:
ad_flags &= ~BLE_HS_ADV_F_DISC_LTD;
ad_flags |= BLE_HS_ADV_F_DISC_GEN;
#if !MYNEWT_VAL(BLE_EXT_ADV)
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
#endif
current_settings |= BIT(BTP_GAP_SETTINGS_DISCOVERABLE);
break;
case BTP_GAP_LIMITED_DISCOVERABLE:
ad_flags &= ~BLE_HS_ADV_F_DISC_GEN;
ad_flags |= BLE_HS_ADV_F_DISC_LTD;
#if !MYNEWT_VAL(BLE_EXT_ADV)
adv_params.disc_mode = BLE_GAP_DISC_MODE_LTD;
#endif
current_settings |= BIT(BTP_GAP_SETTINGS_DISCOVERABLE);
break;
default:
Expand All @@ -383,9 +387,6 @@ set_discoverable(const void *cmd, uint16_t cmd_len,
*rsp_len = sizeof(*rp);

return BTP_STATUS_SUCCESS;
#else
return BTP_STATUS_FAILED;
#endif
}

static uint8_t
Expand Down Expand Up @@ -438,16 +439,19 @@ start_advertising(const void *cmd, uint16_t cmd_len,
{
const struct btp_gap_start_advertising_cmd *cp = cmd;
struct btp_gap_start_advertising_rp *rp = rsp;
int32_t duration_ms = BLE_HS_FOREVER;
uint8_t buf[BLE_HS_ADV_MAX_SZ];
uint8_t buf_len = 0;
uint8_t adv_len, sd_len;
uint8_t addr_type;
uint32_t duration;

int err;

int i;
#if MYNEWT_VAL(BLE_EXT_ADV)
struct os_mbuf *ad_buf;
int duration_ms = 0;
#else
int32_t duration_ms = BLE_HS_FOREVER;
#endif

SYS_LOG_DBG("");

Expand Down Expand Up @@ -498,8 +502,6 @@ start_advertising(const void *cmd, uint16_t cmd_len,
}

#if MYNEWT_VAL(BLE_EXT_ADV)
struct os_mbuf *ad_buf;

adv_params.own_addr_type = own_addr_type;
err = ble_gap_ext_adv_configure(0, &adv_params, NULL, gap_event_cb, NULL);
if (err) {
Expand All @@ -513,7 +515,6 @@ start_advertising(const void *cmd, uint16_t cmd_len,
os_mbuf_free_chain(ad_buf);
return BTP_STATUS_FAILED;
}

err = ble_gap_ext_adv_set_data(0, ad_buf);
#else
err = ble_gap_adv_set_data(buf, buf_len);
Expand All @@ -528,20 +529,28 @@ start_advertising(const void *cmd, uint16_t cmd_len,
return BTP_STATUS_FAILED;
}

#if MYNEWT_VAL(BLE_EXT_ADV)
ad_buf = os_msys_get_pkthdr(BLE_HS_ADV_MAX_SZ, 0);

if (os_mbuf_append(ad_buf, buf, buf_len)) {
os_mbuf_free_chain(ad_buf);
return BTP_STATUS_FAILED;
}
err = ble_gap_ext_adv_rsp_set_data(0, ad_buf);
#else
err = ble_gap_adv_rsp_set_data(buf, buf_len);
#endif
if (err != 0) {
SYS_LOG_ERR("Advertising failed: err %d", err);
return BTP_STATUS_FAILED;
}
}

#if !MYNEWT_VAL(BLE_EXT_ADV)
if (adv_params.disc_mode == BLE_GAP_DISC_MODE_LTD) {
duration_ms = MYNEWT_VAL(BTTESTER_LTD_ADV_TIMEOUT);
if (current_settings & BIT(BTP_GAP_SETTINGS_DISCOVERABLE)) {
if (ad_flags & BLE_HS_ADV_F_DISC_LTD) {
duration_ms = MYNEWT_VAL(BTTESTER_LTD_ADV_TIMEOUT);
}
}
#else
adv_duration = MYNEWT_VAL(BTTESTER_LTD_ADV_TIMEOUT);
#endif

/* In NimBLE, own_addr_type is configured in `controller_info` function.
* Let's just verify restrictions for Privacy options.
Expand Down Expand Up @@ -576,10 +585,10 @@ start_advertising(const void *cmd, uint16_t cmd_len,
#endif

#if MYNEWT_VAL(BLE_EXT_ADV)
err = ble_gap_ext_adv_start(0, duration_ms, 0);
err = ble_gap_ext_adv_start(0, duration_ms / 10, 0);
#else
err = ble_gap_adv_start(own_addr_type, NULL, duration_ms,
&adv_params, gap_event_cb, NULL);
&adv_params, gap_event_cb, NULL);
#endif
if (err) {
SYS_LOG_ERR("Advertising failed: err %d", err);
Expand Down

0 comments on commit 3d525a2

Please sign in to comment.