From e611b10fdeacd4b85b2ffe40e239bacfb7994218 Mon Sep 17 00:00:00 2001 From: chao an Date: Mon, 7 Sep 2020 09:55:36 +0800 Subject: [PATCH] Bluetooth: host: free the adv instance if start legacy advertise fail the adv instance should be delete correctly if advertise fail, Otherwise the instance will unable to allocate if start the legacy again Signed-off-by: chao an --- subsys/bluetooth/host/hci_core.c | 44 ++++++++++++++++---------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index dfb254ff406..53eb54571a0 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -7748,7 +7748,8 @@ static int le_adv_start_add_conn(const struct bt_le_ext_adv *adv, return 0; } -int bt_le_adv_start_legacy(const struct bt_le_adv_param *param, +int bt_le_adv_start_legacy(struct bt_le_ext_adv *adv, + const struct bt_le_adv_param *param, const struct bt_data *ad, size_t ad_len, const struct bt_data *sd, size_t sd_len) { @@ -7757,7 +7758,6 @@ int bt_le_adv_start_legacy(const struct bt_le_adv_param *param, struct net_buf *buf; bool dir_adv = (param->peer != NULL), scannable; int err; - struct bt_le_ext_adv *adv; if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) { return -EAGAIN; @@ -7771,6 +7771,10 @@ int bt_le_adv_start_legacy(const struct bt_le_adv_param *param, return -EINVAL; } + if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) { + return -EALREADY; + } + (void)memset(&set_param, 0, sizeof(set_param)); set_param.min_interval = sys_cpu_to_le16(param->interval_min); @@ -7778,11 +7782,6 @@ int bt_le_adv_start_legacy(const struct bt_le_adv_param *param, set_param.channel_map = 0x07; set_param.filter_policy = get_filter_policy(param->options); - adv = adv_new_legacy(); - if (!adv || atomic_test_bit(adv->flags, BT_ADV_ENABLED)) { - return -EALREADY; - } - if (adv->id != param->id) { atomic_clear_bit(bt_dev.flags, BT_DEV_RPA_VALID); } @@ -8122,24 +8121,25 @@ int bt_le_adv_start(const struct bt_le_adv_param *param, const struct bt_data *ad, size_t ad_len, const struct bt_data *sd, size_t sd_len) { - if (IS_ENABLED(CONFIG_BT_EXT_ADV) && - BT_FEAT_LE_EXT_ADV(bt_dev.le.features)) { - struct bt_le_ext_adv *adv = adv_new_legacy(); - int err; + struct bt_le_ext_adv *adv = adv_new_legacy(); + int err; - if (!adv) { - return -ENOMEM; - } - - err = bt_le_adv_start_ext(adv, param, ad, ad_len, sd, sd_len); - if (err) { - adv_delete_legacy(); - } - - return err; + if (!adv) { + return -ENOMEM; } - return bt_le_adv_start_legacy(param, ad, ad_len, sd, sd_len); + if (IS_ENABLED(CONFIG_BT_EXT_ADV) && + BT_FEAT_LE_EXT_ADV(bt_dev.le.features)) { + err = bt_le_adv_start_ext(adv, param, ad, ad_len, sd, sd_len); + } else { + err = bt_le_adv_start_legacy(adv, param, ad, ad_len, sd, sd_len); + } + + if (err) { + adv_delete_legacy(); + } + + return err; } int bt_le_adv_stop(void)