Skip to content

Commit

Permalink
Add PW RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
achaulk-goog committed Aug 31, 2022
1 parent 104841d commit 1033744
Show file tree
Hide file tree
Showing 13 changed files with 374 additions and 28 deletions.
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){};
}
60 changes: 57 additions & 3 deletions examples/bridge-app/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@ 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" ]
outputs = [ "$target_gen_dir/cpp/BridgeClustersImpl.h" ]
# Also several other files, but this is sufficient for dependency purposes.
outputs = [ "$target_gen_dir/bridge/BridgeClustersImpl.h" ]

args = [
"--generator",
Expand All @@ -41,6 +50,7 @@ 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",
Expand All @@ -58,13 +68,57 @@ executable("chip-bridge-app") {
"${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",
]

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
}

Expand Down
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;
}
2 changes: 1 addition & 1 deletion examples/bridge-app/linux/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void Device::SetEndpointId(chip::EndpointId id)

const char * Device::GetName()
{
return mDeviceName;
return mDeviceName.c_str();
}

void Device::SetName(const char * name)
Expand Down
25 changes: 3 additions & 22 deletions examples/bridge-app/linux/UserInputBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

namespace {

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

// Pseudo-index representing the device being built.
Expand Down Expand Up @@ -169,23 +167,15 @@ void FinishDevice(const std::vector<std::string> & tokens)
for (auto * c : g_pending->clusters())
c->SetCallback(&g_write_cb);

auto dev = std::make_unique<Device>(g_pending->CreateDevice());
int ep = AddDeviceEndpoint(dev.get());
int ep = AddDevice(std::move(g_pending));

if (ep < 0)
{
printf("Failed to add device\n");
}
else
{
printf("Added device at index %d\n", ep);
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(g_pending);
}
}

Expand All @@ -198,19 +188,10 @@ void RemoveDevice(const std::vector<std::string> & tokens)
printf("Error: %s.\nExpected index of a device\n", err);
return;
}
if (index >= g_devices.size())
if (!RemoveDeviceAt(index))
{
printf("%d is an invalid index\n", index);
return;
}

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

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

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

void AddType(const std::vector<std::string> & tokens)
Expand Down
90 changes: 90 additions & 0 deletions examples/bridge-app/linux/bridge_service.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
*
* 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 "bridge_service.h"

#include "Backend.h"
#include "main.h"

namespace chip {
namespace rpc {

namespace {
const clusters::ClusterInfo * FindCluster(uint32_t id)
{
for (const auto & cluster : clusters::kKnownClusters)
{
if (id == cluster.id)
return &cluster;
}
return nullptr;
}
} // namespace

::pw::Status Bridge::Add(const ::chip_rpc_bridge_AddDevice & request, ::chip_rpc_bridge_AddDeviceResponse & response)
{
std::unique_ptr<DynamicDeviceImpl> pending;

pending = std::make_unique<DynamicDeviceImpl>();
pending->SetParentEndpointId(request.parent_endpoint);

for (pb_size_t i = 0; i < request.clusters_count; i++)
{
const chip_rpc_bridge_Cluster & c = request.clusters[i];

auto cluster = FindCluster(c.cluster_id);
if (!cluster)
{
return pw::Status::InvalidArgument();
}

std::unique_ptr<CommonCluster> obj(cluster->ctor(::operator new(cluster->size)));
DynamicAttributeList dynamic_attrs;
auto attrs = obj->GetAllAttributes();
for (auto & attr : attrs)
dynamic_attrs.Add(attr);
pending->AddCluster(std::move(obj), dynamic_attrs, nullptr, nullptr);
}

for (pb_size_t i = 0; i < request.device_types_count; i++)
{
EmberAfDeviceType devType = { (uint16_t) request.device_types[i].id, (uint8_t) request.device_types[i].version };
pending->AddDeviceType(devType);
}

int ret = AddDevice(std::move(pending));
if (ret < 0)
{
return pw::Status::Aborted();
}
response.id = ret;
return pw::OkStatus();
}

::pw::Status Bridge::Remove(const ::chip_rpc_bridge_RemoveDevice & request, ::chip_rpc_bridge_Empty & response)
{
if (!RemoveDeviceAt(request.id))
{
return pw::Status::NotFound();
}

return pw::OkStatus();
}

} // namespace rpc
} // namespace chip
Loading

0 comments on commit 1033744

Please sign in to comment.