Skip to content

Commit

Permalink
[nrf noup] Added check to drop handling callbacks for BT central
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
kkasperczyk-no committed Dec 5, 2023
1 parent 791f8cc commit 3b8ba59
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
27 changes: 19 additions & 8 deletions src/platform/Zephyr/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ CHIP_ERROR BLEManagerImpl::_Init()
mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled;
mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART);
mFlags.Set(Flags::kFastAdvertisingEnabled, true);
mGAPConns = 0;
mMatterConnNum = 0;
mTotalConnNum = 0;

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

Expand Down Expand Up @@ -409,15 +410,13 @@ CHIP_ERROR BLEManagerImpl::HandleGAPConnect(const ChipDeviceEvent * event)
if (connEvent->HciResult == BT_HCI_ERR_SUCCESS)
{
ChipLogProgress(DeviceLayer, "BLE connection established (ConnId: 0x%02x)", bt_conn_index(connEvent->BtConn));
mGAPConns++;
mMatterConnNum++;
}
else
{
ChipLogError(DeviceLayer, "BLE connection failed (reason: 0x%02x)", connEvent->HciResult);
}

ChipLogProgress(DeviceLayer, "Current number of connections: %u/%u", NumConnections(), CONFIG_BT_MAX_CONN);

mFlags.Set(Flags::kAdvertisingRefreshNeeded);
PlatformMgr().ScheduleWork(DriveBLEState, 0);

Expand All @@ -432,7 +431,7 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(const ChipDeviceEvent * event)

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

mGAPConns--;
mMatterConnNum--;

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

ChipLogProgress(DeviceLayer, "Current number of connections: %u/%u", NumConnections(), CONFIG_BT_MAX_CONN);

ChipDeviceEvent disconnectEvent;
disconnectEvent.Type = DeviceEventType::kCHIPoBLEConnectionClosed;
ReturnErrorOnFailure(PlatformMgr().PostEvent(&disconnectEvent));
Expand Down Expand Up @@ -615,7 +612,7 @@ void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)

uint16_t BLEManagerImpl::_NumConnections(void)
{
return mGAPConns;
return mMatterConnNum;
}

bool BLEManagerImpl::CloseConnection(BLE_CONNECTION_OBJECT conId)
Expand Down Expand Up @@ -790,9 +787,16 @@ void BLEManagerImpl::HandleTXIndicated(struct bt_conn * conId, bt_gatt_indicate_
void BLEManagerImpl::HandleConnect(struct bt_conn * conId, uint8_t err)
{
ChipDeviceEvent event;
bt_conn_info bt_info;

PlatformMgr().LockChipStack();

sInstance.mTotalConnNum++;
ChipLogProgress(DeviceLayer, "Current number of connections: %u/%u", sInstance.mTotalConnNum, CONFIG_BT_MAX_CONN);

VerifyOrExit(bt_conn_get_info(conId, &bt_info) == 0, );
// Drop all callbacks incoming for the role other than peripheral, required by the Matter accessory
VerifyOrExit(bt_info.role == BT_CONN_ROLE_PERIPHERAL, );
// Don't handle BLE connecting events when it is not related to CHIPoBLE
VerifyOrExit(sInstance.mFlags.Has(Flags::kChipoBleGattServiceRegister), );

Expand All @@ -809,9 +813,16 @@ void BLEManagerImpl::HandleConnect(struct bt_conn * conId, uint8_t err)
void BLEManagerImpl::HandleDisconnect(struct bt_conn * conId, uint8_t reason)
{
ChipDeviceEvent event;
bt_conn_info bt_info;

PlatformMgr().LockChipStack();

sInstance.mTotalConnNum--;
ChipLogProgress(DeviceLayer, "Current number of connections: %u/%u", sInstance.mTotalConnNum, CONFIG_BT_MAX_CONN);

VerifyOrExit(bt_conn_get_info(conId, &bt_info) == 0, );
// Drop all callbacks incoming for the role other than peripheral, required by the Matter accessory
VerifyOrExit(bt_info.role == BT_CONN_ROLE_PERIPHERAL, );
// Don't handle BLE disconnecting events when it is not related to CHIPoBLE
VerifyOrExit(sInstance.mFlags.Has(Flags::kChipoBleGattServiceRegister), );

Expand Down
4 changes: 3 additions & 1 deletion src/platform/Zephyr/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla
struct ServiceData;

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

void DriveBLEState(void);
CHIP_ERROR PrepareAdvertisingRequest();
Expand Down

0 comments on commit 3b8ba59

Please sign in to comment.