From 7f020a6124dd07e8bd92dd195497f0595bd81f36 Mon Sep 17 00:00:00 2001 From: Szymon Janc Date: Mon, 18 Jun 2018 14:25:33 +0200 Subject: [PATCH] nimble/ll: Return error for unconfigured advertising instances Trying to enable, remove, set advertising data or scan response to set that was previously not configured should result in "Unknown Advertising Identifier (0x42)" error. --- .../include/controller/ble_ll_hci.h | 3 ++ nimble/controller/src/ble_ll_adv.c | 41 ++++++++++++++++--- nimble/controller/src/ble_ll_hci.c | 5 +++ 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/nimble/controller/include/controller/ble_ll_hci.h b/nimble/controller/include/controller/ble_ll_hci.h index 3eb7f60d8..2eeb643c0 100644 --- a/nimble/controller/include/controller/ble_ll_hci.h +++ b/nimble/controller/include/controller/ble_ll_hci.h @@ -57,6 +57,9 @@ int ble_ll_hci_send_noop(void); /* Checks the preferref phy masks from set default phy and set phy commands */ int ble_ll_hci_chk_phy_masks(uint8_t *cmdbuf, uint8_t *txphy, uint8_t *rxphy); +/* Returns true if Extended Advertising HCI commands are in use */ +bool ble_ll_hci_adv_mode_ext(void); + #ifdef __cplusplus } #endif diff --git a/nimble/controller/src/ble_ll_adv.c b/nimble/controller/src/ble_ll_adv.c index b662e9731..ead9ad7e1 100644 --- a/nimble/controller/src/ble_ll_adv.c +++ b/nimble/controller/src/ble_ll_adv.c @@ -134,7 +134,7 @@ struct ble_ll_adv_sm #define BLE_LL_ADV_SM_FLAG_CONN_RSP_TXD 0x0008 #define BLE_LL_ADV_SM_FLAG_ACTIVE_CHANSET_MASK 0x0030 /* use helpers! */ #define BLE_LL_ADV_SM_FLAG_ADV_DATA_INCOMPLETE 0x0040 -#define BLE_LL_ADV_SM_FLAG_ADV_EXT_HCI 0x0080 +#define BLE_LL_ADV_SM_FLAG_CONFIGURED 0x0080 #define BLE_LL_ADV_SM_FLAG_ADV_RPA_TMO 0x0100 #define ADV_DATA_LEN(_advsm) \ @@ -1588,7 +1588,7 @@ static void ble_ll_adv_sm_stop_timeout(struct ble_ll_adv_sm *advsm) { #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV) - if (advsm->flags & BLE_LL_ADV_SM_FLAG_ADV_EXT_HCI) { + if (ble_ll_hci_adv_mode_ext()) { ble_ll_hci_ev_send_adv_set_terminated(BLE_ERR_DIR_ADV_TMO, advsm->adv_instance, 0, advsm->events); @@ -1896,6 +1896,19 @@ done: *omp = om; } +static bool +instance_configured(struct ble_ll_adv_sm *advsm) +{ +#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV) + if (ble_ll_hci_adv_mode_ext()) { + return advsm->flags & BLE_LL_ADV_SM_FLAG_CONFIGURED; + } +#endif + + /* legacy HCI instance is always configured */ + return true; +} + /** * Set the scan response data that the controller will send. * @@ -1918,6 +1931,10 @@ ble_ll_adv_set_scan_rsp_data(uint8_t *cmd, uint8_t instance, uint8_t operation) advsm = &g_ble_ll_adv_sm[instance]; datalen = cmd[0]; + if (!instance_configured(advsm)) { + return BLE_ERR_UNK_ADV_INDENT; + } + /* check if type of advertising support scan rsp */ if (!(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE)) { if (!(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY)) { @@ -2014,6 +2031,10 @@ ble_ll_adv_set_adv_data(uint8_t *cmd, uint8_t instance, uint8_t operation) advsm = &g_ble_ll_adv_sm[instance]; datalen = cmd[0]; + if (!instance_configured(advsm)) { + return BLE_ERR_UNK_ADV_INDENT; + } + /* check if type of advertising support adv data */ if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) { if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_DIRECTED) { @@ -2313,7 +2334,7 @@ ble_ll_adv_ext_set_param(uint8_t *cmdbuf, uint8_t *rspbuf, uint8_t *rsplen) advsm->flags &= ~BLE_LL_ADV_SM_FLAG_SCAN_REQ_NOTIF; } - advsm->flags |= BLE_LL_ADV_SM_FLAG_ADV_EXT_HCI; + advsm->flags |= BLE_LL_ADV_SM_FLAG_CONFIGURED; done: /* Update TX power */ @@ -2416,9 +2437,13 @@ ble_ll_adv_ext_set_enable(uint8_t *cmd, uint8_t len) } } - if (enable) { - advsm = &g_ble_ll_adv_sm[set->handle]; + advsm = &g_ble_ll_adv_sm[set->handle]; + if (!instance_configured(advsm)) { + return BLE_ERR_UNK_ADV_INDENT; + } + + if (enable) { if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_HD_DIRECTED) { if (set->duration == 0 || le16toh(set->duration) > 128) { return BLE_ERR_INV_HCI_CMD_PARMS; @@ -2489,6 +2514,10 @@ ble_ll_adv_remove(uint8_t instance) advsm = &g_ble_ll_adv_sm[instance]; + if (!instance_configured(advsm)) { + return BLE_ERR_UNK_ADV_INDENT; + } + if (advsm->adv_enabled) { return BLE_ERR_CMD_DISALLOWED; } @@ -3337,7 +3366,7 @@ ble_ll_adv_send_conn_comp_ev(struct ble_ll_conn_sm *connsm, #endif #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV) - if (advsm->flags & BLE_LL_ADV_SM_FLAG_ADV_EXT_HCI) { + if (ble_ll_hci_adv_mode_ext()) { ble_ll_hci_ev_send_adv_set_terminated(0, advsm->adv_instance, connsm->conn_handle, advsm->events); } diff --git a/nimble/controller/src/ble_ll_hci.c b/nimble/controller/src/ble_ll_hci.c index 564e040db..2e246839f 100644 --- a/nimble/controller/src/ble_ll_hci.c +++ b/nimble/controller/src/ble_ll_hci.c @@ -54,6 +54,11 @@ static enum { ADV_MODE_LEGACY, ADV_MODE_EXT, } hci_adv_mode; + +bool ble_ll_hci_adv_mode_ext(void) +{ + return hci_adv_mode == ADV_MODE_EXT; +} #endif /**