diff --git a/include/bluetooth/bluetooth.h b/include/bluetooth/bluetooth.h index c3aa204309f..7fc459d3006 100644 --- a/include/bluetooth/bluetooth.h +++ b/include/bluetooth/bluetooth.h @@ -1153,6 +1153,9 @@ struct bt_le_per_adv_sync_term_info { /** Advertiser SID */ uint8_t sid; + + /** Cause of periodic advertising termination */ + uint8_t reason; }; struct bt_le_per_adv_sync_recv_info { diff --git a/subsys/bluetooth/host/scan.c b/subsys/bluetooth/host/scan.c index d029c0a4011..2bb3bf5f997 100644 --- a/subsys/bluetooth/host/scan.c +++ b/subsys/bluetooth/host/scan.c @@ -686,6 +686,7 @@ void bt_hci_le_per_adv_sync_established(struct net_buf *buf) struct bt_le_per_adv_sync_synced_info sync_info; struct bt_le_per_adv_sync *pending_per_adv_sync; struct bt_le_per_adv_sync_cb *listener; + bool unexpected_evt; int err; pending_per_adv_sync = get_pending_per_adv_sync(); @@ -716,22 +717,29 @@ void bt_hci_le_per_adv_sync_established(struct net_buf *buf) BT_PER_ADV_SYNC_SYNCING_USE_LIST) && ((pending_per_adv_sync->sid != evt->sid) || bt_addr_le_cmp(&pending_per_adv_sync->addr, &evt->adv_addr)))) { - struct bt_le_per_adv_sync_term_info term_info; - BT_ERR("Unexpected per adv sync established event"); + /* Request terminate of pending periodic advertising in controller */ per_adv_sync_terminate(sys_le16_to_cpu(evt->handle)); + unexpected_evt = true; + } else { + unexpected_evt = false; + } + + if (unexpected_evt || evt->status != BT_HCI_ERR_SUCCESS) { if (pending_per_adv_sync) { + struct bt_le_per_adv_sync_term_info term_info; + /* Terminate the pending PA sync and notify app */ term_info.addr = &pending_per_adv_sync->addr; term_info.sid = pending_per_adv_sync->sid; + term_info.reason = unexpected_evt ? BT_HCI_ERR_UNSPECIFIED : evt->status; /* Deleting before callback, so the caller will be able * to restart sync in the callback. */ per_adv_sync_delete(pending_per_adv_sync); - SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) { @@ -787,6 +795,8 @@ void bt_hci_le_per_adv_sync_lost(struct net_buf *buf) term_info.addr = &per_adv_sync->addr; term_info.sid = per_adv_sync->sid; + /* There is no status in the per. adv. sync lost event */ + term_info.reason = BT_HCI_ERR_UNSPECIFIED; /* Deleting before callback, so the caller will be able to restart * sync in the callback