diff --git a/applications/matter_bridge/src/ble_providers/ble_bridged_device_factory.cpp b/applications/matter_bridge/src/ble_providers/ble_bridged_device_factory.cpp index 50f8271a6d0..a4e7a40354e 100644 --- a/applications/matter_bridge/src/ble_providers/ble_bridged_device_factory.cpp +++ b/applications/matter_bridge/src/ble_providers/ble_bridged_device_factory.cpp @@ -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(updateClb, commandClb); } }, + [](UpdateAttributeCallback updateClb, InvokeCommandCallback commandClb) { + return chip::Platform::New(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(updateClb, commandClb); - } }, + { ServiceUuid::EnvironmentalSensorService, + [](UpdateAttributeCallback updateClb, InvokeCommandCallback commandClb) { + return chip::Platform::New(updateClb, commandClb); + } }, #endif }; return sDeviceDataProvider; @@ -315,8 +319,8 @@ CHIP_ERROR BleBridgedDeviceFactory::CreateDevice(int deviceType, bt_addr_le_t bt err = MatterDeviceTypeToBleService(static_cast(deviceType), providerType); VerifyOrExit(err == CHIP_NO_ERROR, ); - provider = static_cast( - BleBridgedDeviceFactory::GetDataProviderFactory().Create(providerType, BridgeManager::HandleUpdate, BridgeManager::HandleCommand)); + provider = static_cast(BleBridgedDeviceFactory::GetDataProviderFactory().Create( + providerType, BridgeManager::HandleUpdate, BridgeManager::HandleCommand)); if (!provider) { return CHIP_ERROR_NO_MEMORY; } @@ -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(BleBridgedDeviceFactory::GetDataProviderFactory().Create( - static_cast(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(BleBridgedDeviceFactory::GetDataProviderFactory().Create( + static_cast(uuid), BridgeManager::HandleUpdate, BridgeManager::HandleCommand)); if (!provider) { return CHIP_ERROR_NO_MEMORY; } diff --git a/applications/matter_bridge/src/bridge_shell.cpp b/applications/matter_bridge/src/bridge_shell.cpp index 5a59529a728..7c2724b93d2 100644 --- a/applications/matter_bridge/src/bridge_shell.cpp +++ b/applications/matter_bridge/src/bridge_shell.cpp @@ -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"); @@ -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"); } @@ -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");