@@ -4647,28 +4647,49 @@ static void le_per_adv_sync_established(struct net_buf *buf)
46474647 struct bt_le_per_adv_sync * pending_per_adv_sync ;
46484648 int err ;
46494649
4650- err = bt_le_scan_update (false );
4650+ pending_per_adv_sync = get_pending_per_adv_sync ( );
46514651
4652- if (err ) {
4653- BT_ERR ("Could not stop scan (%d)" , err );
4654- }
4652+ if (pending_per_adv_sync ) {
4653+ atomic_clear_bit (pending_per_adv_sync -> flags ,
4654+ BT_PER_ADV_SYNC_SYNCING );
4655+ err = bt_le_scan_update (false);
46554656
4656- if (evt -> status == BT_HCI_ERR_OP_CANCELLED_BY_HOST ) {
4657- /* Cancelled locally, don't call CB */
4658- return ;
4657+ if (err ) {
4658+ BT_ERR ( "Could not update scan (%d)" , err );
4659+ }
46594660 }
46604661
4661- pending_per_adv_sync = get_pending_per_adv_sync ();
4662-
46634662 if (!pending_per_adv_sync ||
46644663 pending_per_adv_sync -> sid != evt -> sid ||
46654664 bt_addr_le_cmp (& pending_per_adv_sync -> addr , & evt -> adv_addr )) {
4665+ struct bt_le_per_adv_sync_term_info term_info ;
4666+
46664667 BT_ERR ("Unexpected per adv sync established event" );
46674668 per_adv_sync_terminate (sys_le16_to_cpu (evt -> handle ));
4669+
4670+ if (pending_per_adv_sync ) {
4671+ /* Terminate the pending PA sync and notify app */
4672+ term_info .addr = & pending_per_adv_sync -> addr ;
4673+ term_info .sid = pending_per_adv_sync -> sid ;
4674+
4675+ /* Deleting before callback, so the caller will be able
4676+ * to restart sync in the callback.
4677+ */
4678+ per_adv_sync_delete (pending_per_adv_sync );
4679+
4680+ if (pending_per_adv_sync -> cb &&
4681+ pending_per_adv_sync -> cb -> term ) {
4682+ pending_per_adv_sync -> cb -> term (
4683+ pending_per_adv_sync , & term_info );
4684+ }
4685+ }
46684686 return ;
46694687 }
46704688
4671- atomic_clear_bit (pending_per_adv_sync -> flags , BT_PER_ADV_SYNC_SYNCING );
4689+ if (evt -> status == BT_HCI_ERR_OP_CANCELLED_BY_HOST ) {
4690+ /* Cancelled locally, don't call CB */
4691+ return ;
4692+ }
46724693
46734694 atomic_set_bit (pending_per_adv_sync -> flags , BT_PER_ADV_SYNC_SYNCED );
46744695
0 commit comments