From b490959cd959c0dc4542fadf0c2a00ae283c6335 Mon Sep 17 00:00:00 2001 From: Kamil Kasperczyk Date: Thu, 17 Aug 2023 19:49:53 +0200 Subject: [PATCH] applications: Enabled using persistent storage in Matter bridge Added storing bridged devices in the persistent storage after creation, removing them from the storage after removal and loading them from the storage after boot. Signed-off-by: Kamil Kasperczyk --- applications/matter_bridge/src/app_task.cpp | 57 +++++- applications/matter_bridge/src/app_task.h | 1 + .../src/bridged_devices_creator.cpp | 169 ++++++++++++++++-- .../src/bridged_devices_creator.h | 28 ++- .../common/src/bridge/ble_bridged_device.h | 37 ++-- .../common/src/bridge/bridge_manager.cpp | 19 +- .../matter/common/src/bridge/bridge_manager.h | 89 ++++++++- 7 files changed, 370 insertions(+), 30 deletions(-) diff --git a/applications/matter_bridge/src/app_task.cpp b/applications/matter_bridge/src/app_task.cpp index b6b55bb82a2e..5c361779d532 100644 --- a/applications/matter_bridge/src/app_task.cpp +++ b/applications/matter_bridge/src/app_task.cpp @@ -7,6 +7,8 @@ #include "app_task.h" #include "app_config.h" #include "bridge_manager.h" +#include "bridge_storage_manager.h" +#include "bridged_devices_creator.h" #include "led_util.h" #ifdef CONFIG_BRIDGED_DEVICE_BT @@ -149,7 +151,7 @@ CHIP_ERROR AppTask::Init() PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); /* Initialize bridge manager */ - BridgeManager::Instance().Init(); + BridgeManager::Instance().Init(RestoreBridgedDevices); #ifdef CONFIG_BRIDGED_DEVICE_BT BLEConnectivityManager::Instance().Init(sUuidServices, kUuidServicesNumber); @@ -354,3 +356,56 @@ void AppTask::DispatchEvent(const AppEvent &event) LOG_INF("Event received with no handler. Dropping event."); } } + +CHIP_ERROR AppTask::RestoreBridgedDevices() +{ + uint8_t count; + uint8_t indexes[BridgeManager::kMaxBridgedDevices] = { 0 }; + size_t indexesCount = 0; + + if (!BridgeStorageManager::Instance().LoadBridgedDevicesCount(count)) { + LOG_INF("No bridged devices to load from the storage."); + return CHIP_NO_ERROR; + } + + if (!BridgeStorageManager::Instance().LoadBridgedDevicesIndexes(indexes, BridgeManager::kMaxBridgedDevices, + indexesCount)) { + return CHIP_ERROR_NOT_FOUND; + } + + /* Load all devices based on the read count number. */ + for (auto i = 0; i < static_cast(indexesCount); i++) { + uint16_t endpointId; + char label[BridgedDevice::kNodeLabelSize] = { 0 }; + size_t labelSize; + uint16_t deviceType; + + if (!BridgeStorageManager::Instance().LoadBridgedDeviceEndpointId(endpointId, indexes[i])) { + return CHIP_ERROR_NOT_FOUND; + } + + /* Ignore an error, as node label is optional, so it may not be found. */ + BridgeStorageManager::Instance().LoadBridgedDeviceNodeLabel(label, sizeof(label), labelSize, + indexes[i]); + + if (!BridgeStorageManager::Instance().LoadBridgedDeviceType(deviceType, indexes[i])) { + return CHIP_ERROR_NOT_FOUND; + } + +#ifdef CONFIG_BRIDGED_DEVICE_BT + bt_addr_le_t addr; + + if (!BridgeStorageManager::Instance().LoadBtAddress(addr, indexes[i])) { + return CHIP_ERROR_NOT_FOUND; + } + + /* TODO: Add creating a device once BLE re-connection mechanism will be in place*/ +#else + LOG_INF("Loaded bridged device on endpoint id %d from the storage", endpointId); + + BridgedDeviceCreator::CreateDevice(deviceType, label, chip::Optional(indexes[i]), + chip::Optional(endpointId)); +#endif + } + return CHIP_NO_ERROR; +} diff --git a/applications/matter_bridge/src/app_task.h b/applications/matter_bridge/src/app_task.h index 1454aa500625..ec799f800b2a 100644 --- a/applications/matter_bridge/src/app_task.h +++ b/applications/matter_bridge/src/app_task.h @@ -48,6 +48,7 @@ class AppTask { static void LEDStateUpdateHandler(LEDWidget &ledWidget); static void FunctionTimerTimeoutCallback(k_timer *timer); static void UpdateStatusLED(); + static CHIP_ERROR RestoreBridgedDevices(); FunctionEvent mFunction = FunctionEvent::NoneSelected; bool mFunctionTimerActive = false; diff --git a/applications/matter_bridge/src/bridged_devices_creator.cpp b/applications/matter_bridge/src/bridged_devices_creator.cpp index 3d4fa81b2ee1..09b79ca1fe8d 100644 --- a/applications/matter_bridge/src/bridged_devices_creator.cpp +++ b/applications/matter_bridge/src/bridged_devices_creator.cpp @@ -15,7 +15,70 @@ LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); namespace { -CHIP_ERROR AddDevice(int deviceType, const char *nodeLabel, BridgedDeviceDataProvider *provider) +CHIP_ERROR StoreDevice(BridgedDevice *device, BridgedDeviceDataProvider *provider, uint8_t index) +{ + uint16_t endpointId; + uint8_t count = 0; + uint8_t indexes[BridgeManager::kMaxBridgedDevices] = { 0 }; + size_t indexesCount = 0; + bool deviceRefresh = false; + + /* Check if a device is already present in the storage. */ + if (BridgeStorageManager::Instance().LoadBridgedDeviceEndpointId(endpointId, index)) { + deviceRefresh = true; + } + + if (!BridgeStorageManager::Instance().StoreBridgedDevice(device, index)) { + LOG_ERR("Failed to store bridged device"); + return CHIP_ERROR_INTERNAL; + } + +/* Store additional information that are not present for every generic BridgedDevice. */ +#ifdef CONFIG_BRIDGED_DEVICE_BT + BLEBridgedDeviceProvider *bleProvider = static_cast(provider); + + bt_addr_le_t addr; + if (!bleProvider->GetBtAddress(&addr)) { + return CHIP_ERROR_INTERNAL; + } + + if (!BridgeStorageManager::Instance().StoreBtAddress(addr, index)) { + LOG_ERR("Failed to store bridged device's Bluetooth address"); + return CHIP_ERROR_INTERNAL; + } +#endif + + /* If a device was not present in the storage before, put new index on the end of list and increment the count + * number of stored devices. */ + if (!deviceRefresh) { + BridgeStorageManager::Instance().LoadBridgedDevicesIndexes(indexes, BridgeManager::kMaxBridgedDevices, + indexesCount); + + if (indexesCount >= BridgeManager::kMaxBridgedDevices) { + return CHIP_ERROR_BUFFER_TOO_SMALL; + } + + indexes[indexesCount] = index; + indexesCount++; + + if (!BridgeStorageManager::Instance().StoreBridgedDevicesIndexes(indexes, indexesCount)) { + LOG_ERR("Failed to store bridged devices indexes."); + return CHIP_ERROR_INTERNAL; + } + + BridgeStorageManager::Instance().LoadBridgedDevicesCount(count); + + if (!BridgeStorageManager::Instance().StoreBridgedDevicesCount(count + 1)) { + LOG_ERR("Failed to store bridged devices count."); + return CHIP_ERROR_INTERNAL; + } + } + + return CHIP_NO_ERROR; +} + +CHIP_ERROR AddDevice(int deviceType, const char *nodeLabel, BridgedDeviceDataProvider *provider, + chip::Optional index, chip::Optional endpointId) { VerifyOrReturnError(provider != nullptr, CHIP_ERROR_INVALID_ARGUMENT, LOG_ERR("Cannot allocate data provider of given type")); @@ -29,10 +92,19 @@ CHIP_ERROR AddDevice(int deviceType, const char *nodeLabel, BridgedDeviceDataPro return CHIP_ERROR_INTERNAL; } - uint8_t index = 0; - CHIP_ERROR err = BridgeManager::Instance().AddBridgedDevices(newBridgedDevice, provider, index); + CHIP_ERROR err; + uint8_t deviceIndex = 0; + + if (index.HasValue() && endpointId.HasValue()) { + deviceIndex = index.Value(); + err = BridgeManager::Instance().AddBridgedDevices(newBridgedDevice, provider, deviceIndex, + endpointId.Value()); + } else { + err = BridgeManager::Instance().AddBridgedDevices(newBridgedDevice, provider, deviceIndex); + } if (err == CHIP_NO_ERROR) { + err = StoreDevice(newBridgedDevice, provider, deviceIndex); } else if (err == CHIP_ERROR_INVALID_STRING_LENGTH) { LOG_ERR("Error: too long node label (max %d)", BridgedDevice::kNodeLabelSize); } else if (err == CHIP_ERROR_NO_MEMORY) { @@ -59,6 +131,8 @@ struct BluetoothConnectionContext { int deviceType; char nodeLabel[BridgedDevice::kNodeLabelSize]; BLEBridgedDeviceProvider *provider; + chip::Optional index; + chip::Optional endpointId; }; void BluetoothDeviceConnected(BLEBridgedDevice *device, bt_gatt_dm *discoveredData, bool discoverySucceeded, @@ -76,11 +150,12 @@ void BluetoothDeviceConnected(BLEBridgedDevice *device, bt_gatt_dm *discoveredDa return; } - AddDevice(ctx->deviceType, ctx->nodeLabel, ctx->provider); + AddDevice(ctx->deviceType, ctx->nodeLabel, ctx->provider, ctx->index, ctx->endpointId); } } -BridgedDeviceDataProvider *CreateBleProvider(int deviceType, const char *nodeLabel, int bleDeviceIndex) +BridgedDeviceDataProvider *CreateBleProvider(int deviceType, const char *nodeLabel, int bleDeviceIndex, + chip::Optional index, chip::Optional endpointId) { /* The device object can be created once the Bluetooth LE connection will be established. */ BluetoothConnectionContext *context = chip::Platform::New(); @@ -104,6 +179,8 @@ BridgedDeviceDataProvider *CreateBleProvider(int deviceType, const char *nodeLab } contextPtr->provider = static_cast(provider); + contextPtr->index = index; + contextPtr->endpointId = endpointId; CHIP_ERROR err = BLEConnectivityManager::Instance().Connect( bleDeviceIndex, BluetoothDeviceConnected, contextPtr.get(), contextPtr->provider->GetServiceUuid()); @@ -122,13 +199,14 @@ CHIP_ERROR BridgedDeviceCreator::CreateDevice(int deviceType, const char *nodeLa , int bleDeviceIndex #endif -) + , + chip::Optional index, chip::Optional endpointId) { BridgedDeviceDataProvider *provider = nullptr; #if defined(CONFIG_BRIDGED_DEVICE_BT) /* The device cannot be created in line, it has to wait for connected callback. */ - provider = CreateBleProvider(deviceType, nodeLabel, bleDeviceIndex); + provider = CreateBleProvider(deviceType, nodeLabel, bleDeviceIndex, index, endpointId); if (!provider) { return CHIP_ERROR_INTERNAL; @@ -137,15 +215,86 @@ CHIP_ERROR BridgedDeviceCreator::CreateDevice(int deviceType, const char *nodeLa #elif defined(CONFIG_BRIDGED_DEVICE_SIMULATED) provider = CreateSimulatedProvider(deviceType); /* The device is simulated, so it can be added immediately. */ - return AddDevice(deviceType, nodeLabel, provider); + return AddDevice(deviceType, nodeLabel, provider, index, endpointId); #else return CHIP_ERROR_NOT_IMPLEMENTED; #endif + + return CHIP_NO_ERROR; } CHIP_ERROR BridgedDeviceCreator::RemoveDevice(int endpointId) { - uint8_t index = 0; + uint8_t index; + uint8_t count = 0; + uint8_t indexes[BridgeManager::kMaxBridgedDevices] = { 0 }; + size_t indexesCount = 0; + CHIP_ERROR err = BridgeManager::Instance().RemoveBridgedDevice(endpointId, index); + + if (CHIP_NO_ERROR != err) { + LOG_ERR("Failed to remove bridged device"); + return err; + } + + if (!BridgeStorageManager::Instance().LoadBridgedDevicesIndexes(indexes, BridgeManager::kMaxBridgedDevices, + indexesCount)) { + LOG_ERR("Failed to load stored bridged device indexes"); + return CHIP_ERROR_INTERNAL; + } + + /* Find the required index on the list, remove it and move all following indexes one position earlier. */ + bool indexFound = false; + for (auto i = 0; i < static_cast(indexesCount); i++) { + if (indexes[i] == index) { + indexFound = true; + } + + if (indexFound && ((i + 1) < BridgeManager::kMaxBridgedDevices)) { + indexes[i] = indexes[i + 1]; + } + } + + if (indexFound) { + indexesCount--; + } else { + return CHIP_ERROR_NOT_FOUND; + } + + /* Update the current indexes list. */ + if (!BridgeStorageManager::Instance().StoreBridgedDevicesIndexes(indexes, indexesCount)) { + LOG_ERR("Failed to store bridged devices indexes."); + return CHIP_ERROR_INTERNAL; + } + + if (!BridgeStorageManager::Instance().LoadBridgedDevicesCount(count)) { + LOG_ERR("Failed to load bridged devices count."); + return CHIP_ERROR_INTERNAL; + } + + if (!BridgeStorageManager::Instance().StoreBridgedDevicesCount(count - 1)) { + LOG_ERR("Failed to store bridged devices count."); + return CHIP_ERROR_INTERNAL; + } + + if (!BridgeStorageManager::Instance().RemoveBridgedDeviceEndpointId(index)) { + LOG_ERR("Failed to remove bridged device endpoint id."); + return CHIP_ERROR_INTERNAL; + } + + /* Ignore error, as node label may not be present in the storage. */ + BridgeStorageManager::Instance().RemoveBridgedDeviceNodeLabel(index); + + if (!BridgeStorageManager::Instance().RemoveBridgedDeviceType(index)) { + LOG_ERR("Failed to remove bridged device type."); + return CHIP_ERROR_INTERNAL; + } + +#ifdef CONFIG_BRIDGED_DEVICE_BT + if (!BridgeStorageManager::Instance().RemoveBtAddress(index)) { + LOG_ERR("Failed to remove bridged device Bluetooth address."); + return CHIP_ERROR_INTERNAL; + } +#endif - return BridgeManager::Instance().RemoveBridgedDevice(endpointId, index); + return CHIP_NO_ERROR; } diff --git a/applications/matter_bridge/src/bridged_devices_creator.h b/applications/matter_bridge/src/bridged_devices_creator.h index 60e43220416e..66785cbbd2cb 100644 --- a/applications/matter_bridge/src/bridged_devices_creator.h +++ b/applications/matter_bridge/src/bridged_devices_creator.h @@ -7,14 +7,40 @@ #pragma once #include +#include namespace BridgedDeviceCreator { +/** + * @brief Create a bridged device. + * + * @param deviceType the Matter device type of a bridged device to be created + * @param nodeLabel node label of a Matter device to be created + * @param bleDeviceIndex index of Bluetooth LE device on the scanned devices list to be bridged with Matter bridged + * device + * @param index optional index object that shall have a valid value set if the value is meant + * to be used to index assignment, or shall not have a value set if the default index assignment should be used. + * @param endpointId optional endpoint id object that shall have a valid value set if the value is meant + * to be used to endpoint id assignment, or shall not have a value set if the default endpoint id assignment should be + * used. + * @return CHIP_NO_ERROR on success + * @return other error code on failure + */ CHIP_ERROR CreateDevice(int deviceType, const char *nodeLabel #ifdef CONFIG_BRIDGED_DEVICE_BT , int bleDeviceIndex #endif -); + , + chip::Optional index = chip::Optional(), + chip::Optional endpointId = chip::Optional()); + +/** + * @brief Remove bridged device. + * + * @param endpoint value of endpoint id specifying the bridged device to be removed + * @return CHIP_NO_ERROR on success + * @return other error code on failure + */ CHIP_ERROR RemoveDevice(int endpointId); } /* namespace BridgedDeviceCreator */ diff --git a/samples/matter/common/src/bridge/ble_bridged_device.h b/samples/matter/common/src/bridge/ble_bridged_device.h index 0f3e56bfd5ef..e5ff0e82d2ad 100644 --- a/samples/matter/common/src/bridge/ble_bridged_device.h +++ b/samples/matter/common/src/bridge/ble_bridged_device.h @@ -13,7 +13,19 @@ #include #include -struct BLEBridgedDevice; +struct BLEBridgedDeviceProvider; + +struct BLEBridgedDevice { + using DeviceConnectedCallback = void (*)(BLEBridgedDevice *device, bt_gatt_dm *discoveredData, + bool discoverySucceeded, void *context); + + bt_addr_le_t mAddr; + DeviceConnectedCallback mConnectedCallback; + void *mConnectedCallbackContext; + bt_uuid *mServiceUuid; + bt_conn *mConn; + BLEBridgedDeviceProvider *mProvider; +}; class BLEBridgedDeviceProvider : public BridgedDeviceDataProvider { public: @@ -24,18 +36,17 @@ class BLEBridgedDeviceProvider : public BridgedDeviceDataProvider { virtual int MatchBleDevice(BLEBridgedDevice *device) = 0; virtual int ParseDiscoveredData(bt_gatt_dm *discoveredData) = 0; -protected: - BLEBridgedDevice *mDevice{ nullptr }; -}; + bool GetBtAddress(bt_addr_le_t *addr) + { + if (mDevice == nullptr || !addr) { + return false; + } -struct BLEBridgedDevice { - using DeviceConnectedCallback = void (*)(BLEBridgedDevice *device, bt_gatt_dm *discoveredData, - bool discoverySucceeded, void *context); + memcpy(addr, &mDevice->mAddr, sizeof(mDevice->mAddr)); - bt_addr_le_t mAddr; - DeviceConnectedCallback mConnectedCallback; - void *mConnectedCallbackContext; - bt_uuid *mServiceUuid; - bt_conn *mConn; - BLEBridgedDeviceProvider *mProvider; + return true; + } + +protected: + BLEBridgedDevice *mDevice{ nullptr }; }; diff --git a/samples/matter/common/src/bridge/bridge_manager.cpp b/samples/matter/common/src/bridge/bridge_manager.cpp index 9b81ce33f78f..8de8e275d591 100644 --- a/samples/matter/common/src/bridge/bridge_manager.cpp +++ b/samples/matter/common/src/bridge/bridge_manager.cpp @@ -18,8 +18,19 @@ LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); using namespace ::chip; using namespace ::chip::app; -void BridgeManager::Init() +CHIP_ERROR BridgeManager::Init(LoadStoredBridgedDevices loadStoredBridgedDevicesCb) { + if (!loadStoredBridgedDevicesCb) { + return CHIP_ERROR_INVALID_ARGUMENT; + } + + if (mIsInitialized) { + LOG_INF("BridgeManager is already initialized."); + return CHIP_ERROR_INCORRECT_STATE; + } + + mIsInitialized = true; + /* The first dynamic endpoint is the last fixed endpoint + 1. */ mFirstDynamicEndpointId = static_cast( static_cast(emberAfEndpointFromIndex(static_cast(emberAfFixedEndpointCount() - 1))) + 1); @@ -29,6 +40,9 @@ void BridgeManager::Init() /* Disable the placeholder endpoint */ emberAfEndpointEnableDisable(emberAfEndpointFromIndex(static_cast(emberAfFixedEndpointCount() - 1)), false); + + /* Invoke the callback to load stored devices in a proper moment. */ + return loadStoredBridgedDevicesCb(); } CHIP_ERROR BridgeManager::AddBridgedDevices(BridgedDevice *device, BridgedDeviceDataProvider *dataProvider, @@ -148,7 +162,6 @@ CHIP_ERROR BridgeManager::AddDevices(BridgedDevice *aDevice, BridgedDeviceDataPr */ if (devicesPairIndex.HasValue()) { index = devicesPairIndex.Value(); - /* The requested index is already used. */ if (mDevicesMap.Contains(index)) { return CHIP_ERROR_INTERNAL; @@ -162,7 +175,7 @@ CHIP_ERROR BridgeManager::AddDevices(BridgedDevice *aDevice, BridgedDeviceDataPr /* Make sure that the following endpoint id assignments will be monotonically continued from the * biggest assigned number. */ mCurrentDynamicEndpointId = - mCurrentDynamicEndpointId > endpointId ? mCurrentDynamicEndpointId : endpointId; + mCurrentDynamicEndpointId > endpointId ? mCurrentDynamicEndpointId : endpointId + 1; } return err; diff --git a/samples/matter/common/src/bridge/bridge_manager.h b/samples/matter/common/src/bridge/bridge_manager.h index b3a4f4dad6ec..0952179dcc71 100644 --- a/samples/matter/common/src/bridge/bridge_manager.h +++ b/samples/matter/common/src/bridge/bridge_manager.h @@ -12,12 +12,58 @@ class BridgeManager { public: - void Init(); + static constexpr uint8_t kMaxBridgedDevices = CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT; + + using LoadStoredBridgedDevices = CHIP_ERROR (*)(); + + /** + * @brief Initialize BridgeManager instance. + * + * @param loadStoredBridgedDevicesCb callback to method capable of loading and adding bridged devices stored in + * persistent storage + * @return CHIP_NO_ERROR on success + * @return other error code on failure + */ + CHIP_ERROR Init(LoadStoredBridgedDevices loadStoredBridgedDevicesCb); + + /** + * @brief Add pair of bridged device and its data provider using default index and endpoint id assignment. + * + * @param device address of valid bridged device object + * @param dataProvider address of valid data provider object + * @param devicesPairIndex reference to the index object that will be filled with pair's index assigned by the + * bridge + * @return CHIP_NO_ERROR on success + * @return other error code on failure + */ CHIP_ERROR AddBridgedDevices(BridgedDevice *device, BridgedDeviceDataProvider *dataProvider, uint8_t &devicesPairIndex); + + /** + * @brief Add pair of bridged device and its data provider using specific index and endpoint id. + * + * @param device address of valid bridged device object + * @param dataProvider address of valid data provider object + * @param devicesPairIndex reference to the index object that contains index value required to be assigned and + * that will be filled with pair's index finally assigned by the bridge + * @param endpointId value of endpoint id required to be assigned + * @return CHIP_NO_ERROR on success + * @return other error code on failure + */ CHIP_ERROR AddBridgedDevices(BridgedDevice *device, BridgedDeviceDataProvider *dataProvider, uint8_t &devicesPairIndex, uint16_t endpointId); + + /** + * @brief Remove bridged device. + * + * @param endpoint value of endpoint id specifying the bridged device to be removed + * @param devicesPairIndex reference to the index object that will be filled with pair's index obtained by the + * bridge + * @return CHIP_NO_ERROR on success + * @return other error code on failure + */ CHIP_ERROR RemoveBridgedDevice(uint16_t endpoint, uint8_t &devicesPairIndex); + static CHIP_ERROR HandleRead(uint16_t index, chip::ClusterId clusterId, const EmberAfAttributeMetadata *attributeMetadata, uint8_t *buffer, uint16_t maxReadLength); @@ -33,7 +79,6 @@ class BridgeManager { } private: - static constexpr uint8_t kMaxBridgedDevices = CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT; static constexpr uint8_t kMaxDataProviders = CONFIG_BRIDGE_MAX_BRIDGED_DEVICES_NUMBER; using DevicePtr = chip::Platform::UniquePtr; @@ -57,10 +102,49 @@ class BridgeManager { }; using DeviceMap = FiniteMap; + /** + * @brief Add pair of bridged device and its data provider using optional index and endpoint id. This is a + * wrapper method invoked by public AddBridgedDevices methods that maps integer indexes to optionals are assigns + * output index values. + * + * @param device address of valid bridged device object + * @param dataProvider address of valid data provider object + * @param devicesPairIndex reference to the index object that will be filled with pair's index assigned by the + * bridge + * @param endpointId value of endpoint id required to be assigned + * @param index reference to the optional index object that shall have a valid value set if the value is meant + * to be used to index assignment, or shall not have a value set if the default index assignment should be used. + * @return CHIP_NO_ERROR on success + * @return other error code on failure + */ CHIP_ERROR AddBridgedDevices(BridgedDevice *device, BridgedDeviceDataProvider *dataProvider, uint8_t &devicesPairIndex, uint16_t endpointId, chip::Optional &index); + + /** + * @brief Add pair of bridged device and its data provider using optional index and endpoint id. The method + * creates a map entry, matches the bridged device object with the data provider object and creates Matter + * dynamic endpoint. + * + * @param device address of valid bridged device object + * @param dataProvider address of valid data provider object + * @param devicesPairIndex reference to the optional index object that shall have a valid value set if the value + * is meant to be used to index assignment, or shall not have a value set if the default index assignment should + * be used. + * @param endpointId value of endpoint id required to be assigned + * @return CHIP_NO_ERROR on success + * @return other error code on failure + */ CHIP_ERROR AddDevices(BridgedDevice *aDevice, BridgedDeviceDataProvider *aDataProvider, chip::Optional &devicesPairIndex, uint16_t endpointId); + + /** + * @brief Create Matter dynamic endpoint. + * + * @param index index in Matter Data Model's (ember) array to store the endpoint + * @param endpointId value of endpoint id to be created + * @return CHIP_NO_ERROR on success + * @return other error code on failure + */ CHIP_ERROR CreateEndpoint(uint8_t index, uint16_t endpointId); DeviceMap mDevicesMap; @@ -68,4 +152,5 @@ class BridgeManager { chip::EndpointId mFirstDynamicEndpointId; chip::EndpointId mCurrentDynamicEndpointId; + bool mIsInitialized = false; };