Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bridge #21975

Merged
merged 18 commits into from
Oct 3, 2022
Merged

Bridge #21975

Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/bridge-app/bridge-common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import("//build_overrides/chip.gni")

import("${chip_root}/src/app/chip_data_model.gni")
import("${chip_root}/examples/common/pigweed/pigweed_rpcs.gni")

chip_data_model("bridge-common") {
zap_file = "bridge-app.zap"
Expand All @@ -25,3 +26,15 @@ chip_data_model("bridge-common") {
# TODO: the definition of DYNAMIC_ENDPOINT_COUNT needs find a common home!
cflags = [ "-DDYNAMIC_ENDPOINT_COUNT=16" ]
}

if (chip_enable_pw_rpc) {
import("//build_overrides/pigweed.gni")
import("$dir_pw_protobuf_compiler/proto.gni")

pw_proto_library("bridge_service") {
sources = [ "protos/bridge_service.proto" ]
inputs = [ "protos/bridge_service.options" ]
strip_prefix = "protos"
prefix = "bridge_service"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
chip.rpc.bridge.AddDevice.device_types max_count:32
chip.rpc.bridge.AddDevice.clusters max_count:64
33 changes: 33 additions & 0 deletions examples/bridge-app/bridge-common/protos/bridge_service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
syntax = "proto3";

package chip.rpc.bridge;

message Empty {}

message RemoveDevice {
uint32 id = 1;
}

message Cluster {
uint32 cluster_id = 1;
}

message DeviceType {
uint32 id = 1;
uint32 version = 2;
}

message AddDevice {
repeated Cluster clusters = 1;
repeated DeviceType device_types = 2;
uint32 parent_endpoint = 3;
}

message AddDeviceResponse {
uint32 id = 1;
}

service Bridge {
rpc Add(AddDevice) returns (AddDeviceResponse){};
rpc Remove(RemoveDevice) returns (Empty){};
}
89 changes: 87 additions & 2 deletions examples/bridge-app/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,120 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")

import("${chip_root}/build/chip/tools.gni")
import("${chip_root}/src/app/common_flags.gni")

import("${chip_root}/examples/common/pigweed/pigweed_rpcs.gni")

if (chip_enable_pw_rpc) {
import("//build_overrides/pigweed.gni")
import("$dir_pw_build/target_types.gni")
}

assert(chip_build_tools)

action("chip-bridge-codegen") {
script = "${chip_root}/scripts/codegen.py"
sources =
[ "${chip_root}/examples/bridge-app/bridge-common/bridge-app.matter" ]
# Also several other files, but this is sufficient for dependency purposes.
outputs = [ "$target_gen_dir/bridge/BridgeClustersImpl.h" ]

args = [
"--generator",
"bridge",
"--output-dir",
rebase_path(target_gen_dir, root_build_dir),
rebase_path(
"${chip_root}/examples/bridge-app/bridge-common/bridge-app.matter",
root_build_dir),
]
deps = [ "${chip_root}/scripts/idl" ]
}

executable("chip-bridge-app") {
sources = [
"${chip_root}/examples/bridge-app/linux/bridged-actions-stub.cpp",
"${chip_root}/examples/tv-app/tv-common/include/CHIPProjectAppConfig.h",
"Backend.cpp",
"Clusters.cpp",
"Device.cpp",
"DynamicDevice.cpp",
"UserInputBackend.cpp",
"include/Device.h",
"include/main.h",
"main.cpp",
]

deps = [
":chip-bridge-codegen",
"${chip_root}/examples/bridge-app/bridge-common",
"${chip_root}/examples/platform/linux:app-main",
"${chip_root}/src/app/tests/suites/credentials:dac_provider",
"${chip_root}/src/lib",
]

cflags = [ "-Wconversion" ]
include_dirs = [
"include",
target_gen_dir,
]

if (chip_enable_pw_rpc) {
defines = [
"PW_RPC_ENABLED",
"PW_RPC_ATTRIBUTE_SERVICE=1",
"PW_RPC_DESCRIPTOR_SERVICE=1",
"PW_RPC_DEVICE_SERVICE=1",
"PW_RPC_TRACING_SERVICE=1"
]

sources += [
"${chip_root}/examples/platform/linux/Rpc.cpp",
"${chip_root}/examples/platform/linux/system_rpc_server.cc",
"bridge_service.cpp"
]

deps += [
"$dir_pw_hdlc:pw_rpc",
"$dir_pw_hdlc:rpc_channel_output",
"$dir_pw_log",
"$dir_pw_rpc:server",
"$dir_pw_rpc/system_server:facade",
"$dir_pw_stream:socket_stream",
"$dir_pw_stream:sys_io_stream",
"$dir_pw_sync:mutex",
"$dir_pw_trace",
"$dir_pw_trace_tokenized",
"$dir_pw_trace_tokenized:trace_rpc_service",
"${chip_root}/config/linux/lib/pw_rpc:pw_rpc",
"${chip_root}/examples/bridge-app/bridge-common:bridge_service.nanopb_rpc",
"${chip_root}/examples/common/pigweed:attributes_service.nanopb_rpc",
"${chip_root}/examples/common/pigweed:button_service.nanopb_rpc",
"${chip_root}/examples/common/pigweed:descriptor_service.nanopb_rpc",
"${chip_root}/examples/common/pigweed:device_service.nanopb_rpc",
"${chip_root}/examples/common/pigweed:lighting_service.nanopb_rpc",
"${chip_root}/examples/common/pigweed:rpc_services",
]

include_dirs = [ "include" ]
deps += pw_build_LINK_DEPS

include_dirs += [ "${chip_root}/examples/common" ]
} else {
# The system_rpc_server.cc file is in pigweed and doesn't compile with
# -Wconversion, remove check for RPC build only.
cflags = [ "-Wconversion" ]
}

output_dir = root_out_dir
}

group("linux") {
deps = [ ":chip-bridge-app" ]
}

group("default") {
deps = [ ":linux" ]
}
62 changes: 62 additions & 0 deletions examples/bridge-app/linux/Backend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
*
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "Backend.h"

#include "main.h"

std::vector<std::unique_ptr<Device>> g_devices;
std::vector<std::unique_ptr<DynamicDeviceImpl>> g_device_impls;

bool RemoveDeviceAt(uint32_t index)
{
if (index >= g_devices.size() || !g_devices[index])
{
return false;
}

RemoveDeviceEndpoint(g_devices[index].get());

for (auto & room : gRooms)
room.RemoveEndpoint(g_devices[index]->GetEndpointId());

g_devices[index] = nullptr;
g_device_impls[index] = nullptr;

return true;
}

int AddDevice(std::unique_ptr<DynamicDeviceImpl> device)
{
auto dev = std::make_unique<Device>(device->CreateDevice());
int ep = AddDeviceEndpoint(dev.get());
if (ep < 0)
{
return -1;
}

size_t index = (size_t) ep;
if (g_devices.size() <= index)
{
g_devices.resize(index + 1);
g_device_impls.resize(index + 1);
}
g_devices[index] = std::move(dev);
g_device_impls[index] = std::move(device);
return ep;
}
75 changes: 75 additions & 0 deletions examples/bridge-app/linux/Clusters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "GeneratedClusters.h"

#include "Device.h"

CommonCluster * CommonAttributeAccessInterface::FindCluster(const chip::app::ConcreteClusterPath & path)
{
Device * dev = FindDeviceEndpoint(path.mEndpointId);
if (dev)
{
for (auto c : dev->clusters())
{
if (c->GetClusterId() == path.mClusterId)
return static_cast<CommonCluster *>(c);
}
}
return nullptr;
}

void CommonCluster::SetEndpointId(chip::EndpointId id)
{
mEndpoint = id;
}
chip::EndpointId CommonCluster::GetEndpointId() const
{
return mEndpoint;
}

void CommonCluster::SetCallback(PropagateWriteCB * cb)
{
mCallback = cb;
}

bool CommonCluster::active() const
{
return mEndpoint < 0xFFFF;
}

CHIP_ERROR CommonCluster::ForwardWriteToBridge(const chip::app::ConcreteDataAttributePath & aPath,
chip::app::AttributeValueDecoder & aDecoder)
{
if (mCallback)
return (*mCallback)(this, aPath, aDecoder);
return WriteFromBridge(aPath, aDecoder);
}

void CommonCluster::OnUpdated(chip::AttributeId attr)
{
if (active())
MatterReportingAttributeChangeCallback(GetEndpointId(), GetClusterId(), attr);
}

bool CommonCluster::Push(chip::AttributeId attr, chip::TLV::TLVReader & reader)
{
chip::app::AttributeValueDecoder decoder(reader, chip::Access::SubjectDescriptor());
if (WriteFromBridge(chip::app::ConcreteDataAttributePath(GetEndpointId(), GetClusterId(), attr), decoder) != CHIP_NO_ERROR)
return false;
OnUpdated(attr);
return true;
}

namespace clusters {
BridgedDeviceBasicCluster::BridgedDeviceBasicCluster() {}

CHIP_ERROR BridgedDeviceBasicCluster::WriteFromBridge(const chip::app::ConcreteDataAttributePath & aPath,
chip::app::AttributeValueDecoder & aDecoder)
{
switch (aPath.mAttributeId)
{
case ZCL_REACHABLE_ATTRIBUTE_ID:
return mReachable.Write(aPath, aDecoder);
}
return CHIP_ERROR_NOT_IMPLEMENTED;
}

} // namespace clusters
Loading