Skip to content

Commit

Permalink
applications: Matter Bridge: architectural refactoring
Browse files Browse the repository at this point in the history
- allow to inject Matter and non-Matter devices to be bridged
  into the BridgeManager object
- added devices factory to support bridged devices creation
- implemented a dedicated map facilitating device object life
  time management
- added necessary BridgeManager modifications

Signed-off-by: Marcin Kajor <marcin.kajor@nordicsemi.no>
  • Loading branch information
markaj-nordic authored and rlubos committed Aug 4, 2023
1 parent 969bad4 commit 34eb1ad
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 135 deletions.
3 changes: 2 additions & 1 deletion applications/matter_bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ target_include_directories(app PRIVATE
src/bridged_device_types
${COMMON_ROOT}/src
${COMMON_ROOT}/src/bridge/
${COMMON_ROOT}/src/bridge/util
${NLIO_ROOT}/include
${ZEPHYR_CONNECTEDHOMEIP_MODULE_DIR}/zzz_generated/app-common
)

target_sources(app PRIVATE
src/app_task.cpp
src/main.cpp
src/bridge_shell.cpp
${COMMON_ROOT}/src/bridge/bridge_manager.cpp
${COMMON_ROOT}/src/bridge/bridge_shell.cpp
${COMMON_ROOT}/src/bridge/bridged_device.cpp
src/zap-generated/IMClusterCommandHandler.cpp
src/zap-generated/callback-stub.cpp
Expand Down
3 changes: 3 additions & 0 deletions applications/matter_bridge/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ config BRIDGE_HUMIDITY_SENSOR_MAX_MEASURED_VALUE

endif

config EXPERIMENTAL
default y

source "${ZEPHYR_CONNECTEDHOMEIP_MODULE_DIR}/config/nrfconnect/chip-module/Kconfig.features"
source "${ZEPHYR_CONNECTEDHOMEIP_MODULE_DIR}/config/nrfconnect/chip-module/Kconfig.defaults"
source "${ZEPHYR_BASE}/../nrf/samples/matter/common/src/bridge/Kconfig"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include "bridge_manager.h"
#include "bridged_device_factory.h"

#include <zephyr/logging/log.h>
#include <zephyr/shell/shell.h>

#include "bridge_manager.h"

LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL);

static int add_bridged_device(const struct shell *shell, size_t argc, char **argv)
Expand All @@ -20,8 +21,19 @@ static int add_bridged_device(const struct shell *shell, size_t argc, char **arg
nodeLabel = argv[2];
}

CHIP_ERROR err =
GetBridgeManager().AddBridgedDevice(static_cast<BridgedDevice::DeviceType>(deviceType), nodeLabel);
auto *newBridgedDevice = BridgeFactory::GetBridgedDeviceFactory().Create(
static_cast<BridgedDevice::DeviceType>(deviceType), nodeLabel);

VerifyOrReturnError(newBridgedDevice != nullptr, -EPERM,
shell_fprintf(shell, SHELL_INFO, "Cannot allocate Matter device of given type\n"));

auto *newDataProvider = BridgeFactory::GetDataProviderFactory().Create(
static_cast<BridgedDevice::DeviceType>(deviceType), BridgeManager::HandleUpdate);

VerifyOrReturnError(newDataProvider != nullptr, -EPERM, delete newBridgedDevice,
shell_fprintf(shell, SHELL_INFO, "Cannot allocate data provider of given type\n"));

CHIP_ERROR err = GetBridgeManager().AddBridgedDevices(newBridgedDevice, newDataProvider);
if (err == CHIP_NO_ERROR) {
shell_fprintf(shell, SHELL_INFO, "Done\n");
} else if (err == CHIP_ERROR_INVALID_STRING_LENGTH) {
Expand Down
98 changes: 98 additions & 0 deletions applications/matter_bridge/src/bridged_device_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#pragma once

#include "bridge_util.h"
#include "bridged_device.h"
#include "bridged_device_data_provider.h"
#include <lib/support/CHIPMem.h>

#ifdef CONFIG_BRIDGE_HUMIDITY_SENSOR_BRIDGED_DEVICE
#include "humidity_sensor.h"
#include "humidity_sensor_data_provider.h"
#endif

#ifdef CONFIG_BRIDGE_ONOFF_LIGHT_BRIDGED_DEVICE
#include "onoff_light.h"
#include "onoff_light_data_provider.h"
#endif

#ifdef CONFIG_BRIDGE_TEMPERATURE_SENSOR_BRIDGED_DEVICE
#include "temperature_sensor.h"
#include "temperature_sensor_data_provider.h"
#endif

namespace BridgeFactory
{

using UpdateAttributeCallback = BridgedDeviceDataProvider::UpdateAttributeCallback;
using DeviceType = BridgedDevice::DeviceType;
using BridgedDeviceFactory = DeviceFactory<BridgedDevice, const char *>;
using DataProviderFactory = DeviceFactory<BridgedDeviceDataProvider, UpdateAttributeCallback>;

auto checkLabel = [](const char *nodeLabel) {
/* If node label is provided it must fit the maximum defined length */
if (!nodeLabel || (nodeLabel && (strlen(nodeLabel) < BridgedDevice::kNodeLabelSize))) {
return true;
}
return false;
};

inline BridgedDeviceFactory &GetBridgedDeviceFactory()
{
static BridgedDeviceFactory sBridgedDeviceFactory{
#ifdef CONFIG_BRIDGE_HUMIDITY_SENSOR_BRIDGED_DEVICE
{ DeviceType::HumiditySensor,
[](const char *nodeLabel) -> BridgedDevice * {
if (!checkLabel(nodeLabel)) {
return nullptr;
}
return chip::Platform::New<HumiditySensorDevice>(nodeLabel);
} },
#endif
#ifdef CONFIG_BRIDGE_ONOFF_LIGHT_BRIDGED_DEVICE
{ DeviceType::OnOffLight,
[](const char *nodeLabel) -> BridgedDevice * {
if (!checkLabel(nodeLabel)) {
return nullptr;
}
return chip::Platform::New<OnOffLightDevice>(nodeLabel);
} },
#endif
#ifdef CONFIG_BRIDGE_TEMPERATURE_SENSOR_BRIDGED_DEVICE
{ BridgedDevice::DeviceType::TemperatureSensor,
[](const char *nodeLabel) -> BridgedDevice * {
if (!checkLabel(nodeLabel)) {
return nullptr;
}
return chip::Platform::New<TemperatureSensorDevice>(nodeLabel);
} },
#endif
};
return sBridgedDeviceFactory;
}

inline DataProviderFactory &GetDataProviderFactory()
{
static DataProviderFactory sDeviceDataProvider{
#ifdef CONFIG_BRIDGE_HUMIDITY_SENSOR_BRIDGED_DEVICE
{ DeviceType::HumiditySensor,
[](UpdateAttributeCallback clb) { return chip::Platform::New<HumiditySensorDataProvider>(clb); } },
#endif
#ifdef CONFIG_BRIDGE_ONOFF_LIGHT_BRIDGED_DEVICE
{ DeviceType::OnOffLight,
[](UpdateAttributeCallback clb) { return chip::Platform::New<OnOffLightDataProvider>(clb); } },
#endif
#ifdef CONFIG_BRIDGE_TEMPERATURE_SENSOR_BRIDGED_DEVICE
{ DeviceType::TemperatureSensor,
[](UpdateAttributeCallback clb) { return chip::Platform::New<TemperatureSensorDataProvider>(clb); } },
#endif
};
return sDeviceDataProvider;
}

} // namespace BridgeFactory
Loading

0 comments on commit 34eb1ad

Please sign in to comment.