Skip to content

Commit

Permalink
applications: Fixed duplicated add issue for Matter BT bridge
Browse files Browse the repository at this point in the history
Added check that prevents from adding the same Bluetooth LE
device to the bridge multiple times.

Signed-off-by: Kamil Kasperczyk <[email protected]>
  • Loading branch information
kkasperczyk-no authored and rlubos committed Jan 15, 2024
1 parent 7ca9c73 commit c95907c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,18 @@ BleBridgedDeviceFactory::BleDataProviderFactory &BleBridgedDeviceFactory::GetDat
{
static BleDataProviderFactory sDeviceDataProvider
{
#if defined(CONFIG_BRIDGE_ONOFF_LIGHT_BRIDGED_DEVICE) && (defined(CONFIG_BRIDGE_GENERIC_SWITCH_BRIDGED_DEVICE) || defined(CONFIG_BRIDGE_ONOFF_LIGHT_SWITCH_BRIDGED_DEVICE))
#if defined(CONFIG_BRIDGE_ONOFF_LIGHT_BRIDGED_DEVICE) && (defined(CONFIG_BRIDGE_GENERIC_SWITCH_BRIDGED_DEVICE) || \
defined(CONFIG_BRIDGE_ONOFF_LIGHT_SWITCH_BRIDGED_DEVICE))
{ ServiceUuid::LedButtonService,
[](UpdateAttributeCallback updateClb, InvokeCommandCallback commandClb) { return chip::Platform::New<BleLBSDataProvider>(updateClb, commandClb); } },
[](UpdateAttributeCallback updateClb, InvokeCommandCallback commandClb) {
return chip::Platform::New<BleLBSDataProvider>(updateClb, commandClb);
} },
#endif
#if defined(CONFIG_BRIDGE_TEMPERATURE_SENSOR_BRIDGED_DEVICE) && defined(CONFIG_BRIDGE_HUMIDITY_SENSOR_BRIDGED_DEVICE)
{ ServiceUuid::EnvironmentalSensorService, [](UpdateAttributeCallback updateClb, InvokeCommandCallback commandClb) {
return chip::Platform::New<BleEnvironmentalDataProvider>(updateClb, commandClb);
} },
{ ServiceUuid::EnvironmentalSensorService,
[](UpdateAttributeCallback updateClb, InvokeCommandCallback commandClb) {
return chip::Platform::New<BleEnvironmentalDataProvider>(updateClb, commandClb);
} },
#endif
};
return sDeviceDataProvider;
Expand All @@ -315,8 +319,8 @@ CHIP_ERROR BleBridgedDeviceFactory::CreateDevice(int deviceType, bt_addr_le_t bt
err = MatterDeviceTypeToBleService(static_cast<MatterBridgedDevice::DeviceType>(deviceType), providerType);
VerifyOrExit(err == CHIP_NO_ERROR, );

provider = static_cast<BLEBridgedDeviceProvider *>(
BleBridgedDeviceFactory::GetDataProviderFactory().Create(providerType, BridgeManager::HandleUpdate, BridgeManager::HandleCommand));
provider = static_cast<BLEBridgedDeviceProvider *>(BleBridgedDeviceFactory::GetDataProviderFactory().Create(
providerType, BridgeManager::HandleUpdate, BridgeManager::HandleCommand));
if (!provider) {
return CHIP_ERROR_NO_MEMORY;
}
Expand All @@ -340,11 +344,19 @@ CHIP_ERROR BleBridgedDeviceFactory::CreateDevice(int deviceType, bt_addr_le_t bt
return err;
}

CHIP_ERROR BleBridgedDeviceFactory::CreateDevice(uint16_t uuid, bt_addr_le_t btAddress, const char *nodeLabel, BLEConnectivityManager::ConnectionSecurityRequest * request)
CHIP_ERROR BleBridgedDeviceFactory::CreateDevice(uint16_t uuid, bt_addr_le_t btAddress, const char *nodeLabel,
BLEConnectivityManager::ConnectionSecurityRequest *request)
{
BLEBridgedDeviceProvider *provider =
static_cast<BLEBridgedDeviceProvider *>(BleBridgedDeviceFactory::GetDataProviderFactory().Create(
static_cast<ServiceUuid>(uuid), BridgeManager::HandleUpdate, BridgeManager::HandleCommand));
/* Check if there is already existing provider for given address.
* In case it is, the provider was either connected or recovered before.
*/
BLEBridgedDeviceProvider *provider = BLEConnectivityManager::Instance().FindBLEProvider(btAddress);
if (provider) {
return CHIP_ERROR_INCORRECT_STATE;
}

provider = static_cast<BLEBridgedDeviceProvider *>(BleBridgedDeviceFactory::GetDataProviderFactory().Create(
static_cast<ServiceUuid>(uuid), BridgeManager::HandleUpdate, BridgeManager::HandleCommand));
if (!provider) {
return CHIP_ERROR_NO_MEMORY;
}
Expand Down
8 changes: 6 additions & 2 deletions applications/matter_bridge/src/bridge_shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ static int AddBridgedDeviceHandler(const struct shell *shell, size_t argc, char
}

bt_addr_le_t address;
if (Nrf::BLEConnectivityManager::Instance().GetScannedDeviceAddress(&address, bleDeviceIndex) != CHIP_NO_ERROR) {
if (Nrf::BLEConnectivityManager::Instance().GetScannedDeviceAddress(&address, bleDeviceIndex) !=
CHIP_NO_ERROR) {
shell_fprintf(shell, SHELL_ERROR, "Invalid Bluetooth LE device index.\n");
} else {
shell_fprintf(shell, SHELL_ERROR, "Found device address\n");
Expand Down Expand Up @@ -84,6 +85,8 @@ static int AddBridgedDeviceHandler(const struct shell *shell, size_t argc, char

if (result == CHIP_NO_ERROR) {
shell_fprintf(shell, SHELL_INFO, "Done\n");
} else if (result == CHIP_ERROR_INCORRECT_STATE) {
shell_fprintf(shell, SHELL_INFO, "Device is already added\n");
} else {
shell_fprintf(shell, SHELL_ERROR, "Device add failed\n");
}
Expand Down Expand Up @@ -202,7 +205,8 @@ static int InsertBridgedDevicePincodeHandler(const struct shell *shell, size_t a
unsigned int pincode = strtoul(argv[1], NULL, 0);

bt_addr_le_t address;
if (Nrf::BLEConnectivityManager::Instance().GetScannedDeviceAddress(&address, bleDeviceIndex) != CHIP_NO_ERROR) {
if (Nrf::BLEConnectivityManager::Instance().GetScannedDeviceAddress(&address, bleDeviceIndex) !=
CHIP_NO_ERROR) {
shell_fprintf(shell, SHELL_ERROR, "Invalid Bluetooth LE device index.\n");
} else {
shell_fprintf(shell, SHELL_ERROR, "Found device address\n");
Expand Down

0 comments on commit c95907c

Please sign in to comment.