From 2790dbd12e6ad0bd2fc9028f06bfe89df84042a4 Mon Sep 17 00:00:00 2001 From: Patryk Lipinski Date: Thu, 4 Jan 2024 13:02:02 +0100 Subject: [PATCH] applications: Matter Bridge: Use common binding handler This commit removes Matter Bridge specific binding handler, and re-uses the common one. Signed-off-by: Patryk Lipinski --- applications/matter_bridge/CMakeLists.txt | 2 +- .../ble_providers/ble_lbs_data_provider.cpp | 2 +- ...lated_onoff_light_switch_data_provider.cpp | 2 +- .../common/src/bridge/binding_handler.cpp | 137 ------------------ .../common/src/bridge/binding_handler.h | 49 ------- .../common/src/bridge/bridge_manager.cpp | 7 +- .../matter/common/src/bridge/bridge_manager.h | 2 +- .../src/bridge/bridged_device_data_provider.h | 2 +- 8 files changed, 8 insertions(+), 195 deletions(-) delete mode 100644 samples/matter/common/src/bridge/binding_handler.cpp delete mode 100644 samples/matter/common/src/bridge/binding_handler.h diff --git a/applications/matter_bridge/CMakeLists.txt b/applications/matter_bridge/CMakeLists.txt index 0e5b413aca4d..a0f4c4315da3 100644 --- a/applications/matter_bridge/CMakeLists.txt +++ b/applications/matter_bridge/CMakeLists.txt @@ -47,7 +47,7 @@ target_sources(app PRIVATE ${COMMON_ROOT}/src/bridge/matter_bridged_device.cpp ${COMMON_ROOT}/src/bridge/bridge_storage_manager.cpp ${COMMON_ROOT}/src/bridge/bridged_device_data_provider.cpp - ${COMMON_ROOT}/src/bridge/binding_handler.cpp + ${COMMON_ROOT}/src/binding/binding_handler.cpp src/zap-generated/IMClusterCommandHandler.cpp src/zap-generated/callback-stub.cpp ${COMMON_ROOT}/src/led/led_widget.cpp diff --git a/applications/matter_bridge/src/ble_providers/ble_lbs_data_provider.cpp b/applications/matter_bridge/src/ble_providers/ble_lbs_data_provider.cpp index cc4387151bd5..152d29a92046 100644 --- a/applications/matter_bridge/src/ble_providers/ble_lbs_data_provider.cpp +++ b/applications/matter_bridge/src/ble_providers/ble_lbs_data_provider.cpp @@ -7,7 +7,7 @@ #include "ble_lbs_data_provider.h" #ifdef CONFIG_BRIDGE_ONOFF_LIGHT_SWITCH_BRIDGED_DEVICE -#include "binding_handler.h" +#include "binding/binding_handler.h" #endif #include diff --git a/applications/matter_bridge/src/simulated_providers/simulated_onoff_light_switch_data_provider.cpp b/applications/matter_bridge/src/simulated_providers/simulated_onoff_light_switch_data_provider.cpp index 14ccfdbf9774..7946d623be0e 100644 --- a/applications/matter_bridge/src/simulated_providers/simulated_onoff_light_switch_data_provider.cpp +++ b/applications/matter_bridge/src/simulated_providers/simulated_onoff_light_switch_data_provider.cpp @@ -5,7 +5,7 @@ */ #include "simulated_onoff_light_switch_data_provider.h" -#include "binding_handler.h" +#include "binding/binding_handler.h" #include diff --git a/samples/matter/common/src/bridge/binding_handler.cpp b/samples/matter/common/src/bridge/binding_handler.cpp deleted file mode 100644 index 121a813a37f2..000000000000 --- a/samples/matter/common/src/bridge/binding_handler.cpp +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause - */ - -#include "binding_handler.h" - -#include -LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); - -using namespace chip; -using namespace chip::app; - -void BindingHandler::Init() -{ - DeviceLayer::PlatformMgr().ScheduleWork(InitInternal); -} - -void BindingHandler::OnInvokeCommandFailure(BindingData &aBindingData, CHIP_ERROR aError) -{ - CHIP_ERROR error; - - if (aError == CHIP_ERROR_TIMEOUT && !aBindingData.CaseSessionRecovered) { - LOG_INF("Response timeout for invoked command, trying to recover CASE session."); - - /* Set flag to not try recover session multiple times. */ - aBindingData.CaseSessionRecovered = true; - - /* Allocate new object to make sure its life time will be appropriate. */ - BindingHandler::BindingData *data = Platform::New(); - *data = aBindingData; - - /* Establish new CASE session and retrasmit command that was not applied. */ - error = BindingManager::GetInstance().NotifyBoundClusterChanged( - aBindingData.EndpointId, aBindingData.ClusterId, static_cast(data)); - - if (CHIP_NO_ERROR != error) { - LOG_ERR("NotifyBoundClusterChanged failed due to: %" CHIP_ERROR_FORMAT, error.Format()); - return; - } - } else { - LOG_ERR("Binding command was not applied! Reason: %" CHIP_ERROR_FORMAT, aError.Format()); - } -} - -void BindingHandler::DeviceChangedHandler(const EmberBindingTableEntry &binding, OperationalDeviceProxy *deviceProxy, - void *context) -{ - VerifyOrReturn(context != nullptr, LOG_ERR("Invalid context for device handler");); - BindingData *data = static_cast(context); - - if (binding.type == EMBER_MULTICAST_BINDING && data->IsGroup) { - data->InvokeCommandFunc(data->CommandId, binding, nullptr, context); - } else if (binding.type == EMBER_UNICAST_BINDING && !data->IsGroup) { - data->InvokeCommandFunc(data->CommandId, binding, deviceProxy, context); - } -} - -void BindingHandler::DeviceContextReleaseHandler(void *context) -{ - VerifyOrReturn(context != nullptr, LOG_ERR("Invalid context for device context release handler");); - - Platform::Delete(static_cast(context)); -} - -void BindingHandler::InitInternal(intptr_t aArg) -{ - LOG_INF("Initialize binding Handler"); - auto &server = Server::GetInstance(); - if (CHIP_NO_ERROR != - BindingManager::GetInstance().Init( - { &server.GetFabricTable(), server.GetCASESessionManager(), &server.GetPersistentStorage() })) { - LOG_ERR("BindingHandler::InitInternal failed"); - } - - BindingManager::GetInstance().RegisterBoundDeviceChangedHandler(DeviceChangedHandler); - BindingManager::GetInstance().RegisterBoundDeviceContextReleaseHandler(DeviceContextReleaseHandler); - BindingHandler::GetInstance().PrintBindingTable(); -} - -bool BindingHandler::IsGroupBound() -{ - BindingTable &bindingTable = BindingTable::GetInstance(); - - for (auto &entry : bindingTable) { - if (EMBER_MULTICAST_BINDING == entry.type) { - return true; - } - } - return false; -} - -void BindingHandler::PrintBindingTable() -{ - BindingTable &bindingTable = BindingTable::GetInstance(); - - LOG_INF("Binding Table size: [%d]:", bindingTable.Size()); - uint8_t i = 0; - for (auto &entry : bindingTable) { - switch (entry.type) { - case EMBER_UNICAST_BINDING: - LOG_INF("[%d] UNICAST:", i++); - LOG_INF("\t\t+ Fabric: %d\n \ - \t+ LocalEndpoint %d \n \ - \t+ ClusterId %d \n \ - \t+ RemoteEndpointId %d \n \ - \t+ NodeId %d", - (int)entry.fabricIndex, (int)entry.local, (int)entry.clusterId.Value(), - (int)entry.remote, (int)entry.nodeId); - break; - case EMBER_MULTICAST_BINDING: - LOG_INF("[%d] GROUP:", i++); - LOG_INF("\t\t+ Fabric: %d\n \ - \t+ LocalEndpoint %d \n \ - \t+ RemoteEndpointId %d \n \ - \t+ GroupId %d", - (int)entry.fabricIndex, (int)entry.local, (int)entry.remote, (int)entry.groupId); - break; - case EMBER_UNUSED_BINDING: - LOG_INF("[%d] UNUSED", i++); - break; - default: - break; - } - } -} - -void BindingHandler::DeviceWorkerHandler(intptr_t aContext) -{ - VerifyOrReturn(aContext != 0, LOG_ERR("Invalid context data")); - - BindingData *data = reinterpret_cast(aContext); - LOG_INF("Notify Bounded Cluster | endpoint: %d cluster: %d", data->EndpointId, data->ClusterId); - BindingManager::GetInstance().NotifyBoundClusterChanged(data->EndpointId, data->ClusterId, - static_cast(data)); -} diff --git a/samples/matter/common/src/bridge/binding_handler.h b/samples/matter/common/src/bridge/binding_handler.h deleted file mode 100644 index e05a94757a71..000000000000 --- a/samples/matter/common/src/bridge/binding_handler.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause - */ -#pragma once - -#include -#include -#include -#include -#include -#include - -#include - -class BindingHandler { -public: - using InvokeCommand = void (*)(chip::CommandId aCommandId, const EmberBindingTableEntry &aBinding, - chip::OperationalDeviceProxy *aDevice, void *aContext); - - struct BindingData { - chip::EndpointId EndpointId; - chip::CommandId CommandId; - chip::ClusterId ClusterId; - InvokeCommand InvokeCommandFunc; - uint8_t Value; - bool IsGroup{ false }; - bool CaseSessionRecovered{ false }; - }; - - void Init(); - void PrintBindingTable(); - bool IsGroupBound(); - - static void DeviceWorkerHandler(intptr_t); - static void OnInvokeCommandFailure(BindingData &aBindingData, CHIP_ERROR aError); - - static BindingHandler &GetInstance() - { - static BindingHandler sBindingHandler; - return sBindingHandler; - } - -private: - static void DeviceChangedHandler(const EmberBindingTableEntry &, chip::OperationalDeviceProxy *, void *); - static void DeviceContextReleaseHandler(void *context); - static void InitInternal(intptr_t); -}; diff --git a/samples/matter/common/src/bridge/bridge_manager.cpp b/samples/matter/common/src/bridge/bridge_manager.cpp index cb44feffea35..7438241b6fef 100644 --- a/samples/matter/common/src/bridge/bridge_manager.cpp +++ b/samples/matter/common/src/bridge/bridge_manager.cpp @@ -6,7 +6,7 @@ #include "bridge_manager.h" -#include "binding_handler.h" +#include "binding/binding_handler.h" #include #include @@ -41,7 +41,7 @@ CHIP_ERROR BridgeManager::Init(LoadStoredBridgedDevicesCallback loadStoredBridge emberAfEndpointEnableDisable(emberAfEndpointFromIndex(static_cast(emberAfFixedEndpointCount() - 1)), false); - BindingHandler::GetInstance().Init(); + BindingHandler::Init(); /* Invoke the callback to load stored devices in a proper moment. */ CHIP_ERROR err = loadStoredBridgedDevicesCb(); @@ -425,8 +425,7 @@ void BridgeManager::HandleCommand(BridgedDeviceDataProvider &dataProvider, Clust } } - DeviceLayer::PlatformMgr().ScheduleWork(BindingHandler::DeviceWorkerHandler, - reinterpret_cast(bindingData)); + BindingHandler::RunBoundClusterAction(bindingData); } BridgedDeviceDataProvider *BridgeManager::GetProvider(EndpointId endpoint, uint16_t &deviceType) diff --git a/samples/matter/common/src/bridge/bridge_manager.h b/samples/matter/common/src/bridge/bridge_manager.h index 321f6dcaf921..224efdffc5b6 100644 --- a/samples/matter/common/src/bridge/bridge_manager.h +++ b/samples/matter/common/src/bridge/bridge_manager.h @@ -9,7 +9,7 @@ #include "bridge_util.h" #include "bridged_device_data_provider.h" #include "matter_bridged_device.h" -#include "binding_handler.h" +#include "binding/binding_handler.h" class BridgeManager { public: diff --git a/samples/matter/common/src/bridge/bridged_device_data_provider.h b/samples/matter/common/src/bridge/bridged_device_data_provider.h index 55fd6dbddc65..f7f637560f58 100644 --- a/samples/matter/common/src/bridge/bridged_device_data_provider.h +++ b/samples/matter/common/src/bridge/bridged_device_data_provider.h @@ -9,7 +9,7 @@ #include #include #include -#include "binding_handler.h" +#include "binding/binding_handler.h" class BridgedDeviceDataProvider { public: