Skip to content

Commit 140858f

Browse files
[zephyr] Added check to drop handling callbacks for BT central
Matter BLEManager handles all BT connect and disconnect callbacks no matter if these are Matter related ones or not. It collides with other not Matter-related services that trigger Matter CHIPoBLE service advertising changes. Added role check that allows to at least drop all callbacks related to BT central role. Signed-off-by: Kamil Kasperczyk <kamil.kasperczyk@nordicsemi.no>
1 parent bf94bcc commit 140858f

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/platform/Zephyr/BLEManagerImpl.cpp

+19-8
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ CHIP_ERROR BLEManagerImpl::_Init()
167167
mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled;
168168
mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART);
169169
mFlags.Set(Flags::kFastAdvertisingEnabled, true);
170-
mGAPConns = 0;
170+
mMatterConnNum = 0;
171+
mTotalConnNum = 0;
171172

172173
memset(mSubscribedConns, 0, sizeof(mSubscribedConns));
173174

@@ -493,15 +494,13 @@ CHIP_ERROR BLEManagerImpl::HandleGAPConnect(const ChipDeviceEvent * event)
493494
if (connEvent->HciResult == BT_HCI_ERR_SUCCESS)
494495
{
495496
ChipLogProgress(DeviceLayer, "BLE connection established (ConnId: 0x%02x)", bt_conn_index(connEvent->BtConn));
496-
mGAPConns++;
497+
mMatterConnNum++;
497498
}
498499
else
499500
{
500501
ChipLogError(DeviceLayer, "BLE connection failed (reason: 0x%02x)", connEvent->HciResult);
501502
}
502503

503-
ChipLogProgress(DeviceLayer, "Current number of connections: %u/%u", NumConnections(), CONFIG_BT_MAX_CONN);
504-
505504
mFlags.Set(Flags::kAdvertisingRefreshNeeded);
506505
PlatformMgr().ScheduleWork(DriveBLEState, 0);
507506

@@ -516,7 +515,7 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(const ChipDeviceEvent * event)
516515

517516
ChipLogProgress(DeviceLayer, "BLE GAP connection terminated (reason 0x%02x)", connEvent->HciResult);
518517

519-
mGAPConns--;
518+
mMatterConnNum--;
520519

521520
// If indications were enabled for this connection, record that they are now disabled and
522521
// notify the BLE Layer of a disconnect.
@@ -544,8 +543,6 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(const ChipDeviceEvent * event)
544543
// Unref bt_conn before scheduling DriveBLEState.
545544
bt_conn_unref(connEvent->BtConn);
546545

547-
ChipLogProgress(DeviceLayer, "Current number of connections: %u/%u", NumConnections(), CONFIG_BT_MAX_CONN);
548-
549546
ChipDeviceEvent disconnectEvent;
550547
disconnectEvent.Type = DeviceEventType::kCHIPoBLEConnectionClosed;
551548
ReturnErrorOnFailure(PlatformMgr().PostEvent(&disconnectEvent));
@@ -705,7 +702,7 @@ void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
705702

706703
uint16_t BLEManagerImpl::_NumConnections(void)
707704
{
708-
return mGAPConns;
705+
return mMatterConnNum;
709706
}
710707

711708
bool BLEManagerImpl::CloseConnection(BLE_CONNECTION_OBJECT conId)
@@ -880,9 +877,16 @@ void BLEManagerImpl::HandleTXIndicated(struct bt_conn * conId, bt_gatt_indicate_
880877
void BLEManagerImpl::HandleConnect(struct bt_conn * conId, uint8_t err)
881878
{
882879
ChipDeviceEvent event;
880+
bt_conn_info bt_info;
883881

884882
PlatformMgr().LockChipStack();
885883

884+
sInstance.mTotalConnNum++;
885+
ChipLogProgress(DeviceLayer, "Current number of connections: %u/%u", sInstance.mTotalConnNum, CONFIG_BT_MAX_CONN);
886+
887+
VerifyOrExit(bt_conn_get_info(conId, &bt_info) == 0, );
888+
// Drop all callbacks incoming for the role other than peripheral, required by the Matter accessory
889+
VerifyOrExit(bt_info.role == BT_CONN_ROLE_PERIPHERAL, );
886890
// Don't handle BLE connecting events when it is not related to CHIPoBLE
887891
VerifyOrExit(sInstance.mFlags.Has(Flags::kChipoBleGattServiceRegister), );
888892

@@ -899,9 +903,16 @@ void BLEManagerImpl::HandleConnect(struct bt_conn * conId, uint8_t err)
899903
void BLEManagerImpl::HandleDisconnect(struct bt_conn * conId, uint8_t reason)
900904
{
901905
ChipDeviceEvent event;
906+
bt_conn_info bt_info;
902907

903908
PlatformMgr().LockChipStack();
904909

910+
sInstance.mTotalConnNum--;
911+
ChipLogProgress(DeviceLayer, "Current number of connections: %u/%u", sInstance.mTotalConnNum, CONFIG_BT_MAX_CONN);
912+
913+
VerifyOrExit(bt_conn_get_info(conId, &bt_info) == 0, );
914+
// Drop all callbacks incoming for the role other than peripheral, required by the Matter accessory
915+
VerifyOrExit(bt_info.role == BT_CONN_ROLE_PERIPHERAL, );
905916
// Don't handle BLE disconnecting events when it is not related to CHIPoBLE
906917
VerifyOrExit(sInstance.mFlags.Has(Flags::kChipoBleGattServiceRegister), );
907918

src/platform/Zephyr/BLEManagerImpl.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla
9797
struct ServiceData;
9898

9999
BitFlags<Flags> mFlags;
100-
uint16_t mGAPConns;
100+
uint16_t mMatterConnNum;
101101
CHIPoBLEServiceMode mServiceMode;
102102
bool mSubscribedConns[CONFIG_BT_MAX_CONN];
103103
bt_gatt_indicate_params mIndicateParams[CONFIG_BT_MAX_CONN];
@@ -106,6 +106,8 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla
106106
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
107107
PacketBufferHandle c3CharDataBufferHandle;
108108
#endif
109+
// The summarized number of Bluetooth LE connections related to the device (including these not related to Matter service).
110+
uint16_t mTotalConnNum;
109111

110112
void DriveBLEState(void);
111113
CHIP_ERROR PrepareAdvertisingRequest();

0 commit comments

Comments
 (0)