Skip to content

Commit 1033744

Browse files
committed
Add PW RPC
1 parent 104841d commit 1033744

13 files changed

+374
-28
lines changed

examples/bridge-app/bridge-common/BUILD.gn

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import("//build_overrides/chip.gni")
1616

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

1920
chip_data_model("bridge-common") {
2021
zap_file = "bridge-app.zap"
@@ -25,3 +26,15 @@ chip_data_model("bridge-common") {
2526
# TODO: the definition of DYNAMIC_ENDPOINT_COUNT needs find a common home!
2627
cflags = [ "-DDYNAMIC_ENDPOINT_COUNT=16" ]
2728
}
29+
30+
if (chip_enable_pw_rpc) {
31+
import("//build_overrides/pigweed.gni")
32+
import("$dir_pw_protobuf_compiler/proto.gni")
33+
34+
pw_proto_library("bridge_service") {
35+
sources = [ "protos/bridge_service.proto" ]
36+
inputs = [ "protos/bridge_service.options" ]
37+
strip_prefix = "protos"
38+
prefix = "bridge_service"
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
chip.rpc.bridge.AddDevice.device_types max_count:32
2+
chip.rpc.bridge.AddDevice.clusters max_count:64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
syntax = "proto3";
2+
3+
package chip.rpc.bridge;
4+
5+
message Empty {}
6+
7+
message RemoveDevice {
8+
uint32 id = 1;
9+
}
10+
11+
message Cluster {
12+
uint32 cluster_id = 1;
13+
}
14+
15+
message DeviceType {
16+
uint32 id = 1;
17+
uint32 version = 2;
18+
}
19+
20+
message AddDevice {
21+
repeated Cluster clusters = 1;
22+
repeated DeviceType device_types = 2;
23+
uint32 parent_endpoint = 3;
24+
}
25+
26+
message AddDeviceResponse {
27+
uint32 id = 1;
28+
}
29+
30+
service Bridge {
31+
rpc Add(AddDevice) returns (AddDeviceResponse){};
32+
rpc Remove(RemoveDevice) returns (Empty){};
33+
}

examples/bridge-app/linux/BUILD.gn

+57-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,23 @@ import("//build_overrides/build.gni")
1616
import("//build_overrides/chip.gni")
1717

1818
import("${chip_root}/build/chip/tools.gni")
19+
import("${chip_root}/src/app/common_flags.gni")
20+
21+
import("${chip_root}/examples/common/pigweed/pigweed_rpcs.gni")
22+
23+
if (chip_enable_pw_rpc) {
24+
import("//build_overrides/pigweed.gni")
25+
import("$dir_pw_build/target_types.gni")
26+
}
1927

2028
assert(chip_build_tools)
2129

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

2837
args = [
2938
"--generator",
@@ -41,6 +50,7 @@ executable("chip-bridge-app") {
4150
sources = [
4251
"${chip_root}/examples/bridge-app/linux/bridged-actions-stub.cpp",
4352
"${chip_root}/examples/tv-app/tv-common/include/CHIPProjectAppConfig.h",
53+
"Backend.cpp",
4454
"Clusters.cpp",
4555
"Device.cpp",
4656
"DynamicDevice.cpp",
@@ -58,13 +68,57 @@ executable("chip-bridge-app") {
5868
"${chip_root}/src/lib",
5969
]
6070

61-
cflags = [ "-Wconversion" ]
62-
6371
include_dirs = [
6472
"include",
6573
target_gen_dir,
6674
]
6775

76+
if (chip_enable_pw_rpc) {
77+
defines = [
78+
"PW_RPC_ENABLED",
79+
"PW_RPC_ATTRIBUTE_SERVICE=1",
80+
"PW_RPC_DESCRIPTOR_SERVICE=1",
81+
"PW_RPC_DEVICE_SERVICE=1",
82+
"PW_RPC_TRACING_SERVICE=1"
83+
]
84+
85+
sources += [
86+
"${chip_root}/examples/platform/linux/Rpc.cpp",
87+
"${chip_root}/examples/platform/linux/system_rpc_server.cc",
88+
"bridge_service.cpp"
89+
]
90+
91+
deps += [
92+
"$dir_pw_hdlc:pw_rpc",
93+
"$dir_pw_hdlc:rpc_channel_output",
94+
"$dir_pw_log",
95+
"$dir_pw_rpc:server",
96+
"$dir_pw_rpc/system_server:facade",
97+
"$dir_pw_stream:socket_stream",
98+
"$dir_pw_stream:sys_io_stream",
99+
"$dir_pw_sync:mutex",
100+
"$dir_pw_trace",
101+
"$dir_pw_trace_tokenized",
102+
"$dir_pw_trace_tokenized:trace_rpc_service",
103+
"${chip_root}/config/linux/lib/pw_rpc:pw_rpc",
104+
"${chip_root}/examples/bridge-app/bridge-common:bridge_service.nanopb_rpc",
105+
"${chip_root}/examples/common/pigweed:attributes_service.nanopb_rpc",
106+
"${chip_root}/examples/common/pigweed:button_service.nanopb_rpc",
107+
"${chip_root}/examples/common/pigweed:descriptor_service.nanopb_rpc",
108+
"${chip_root}/examples/common/pigweed:device_service.nanopb_rpc",
109+
"${chip_root}/examples/common/pigweed:lighting_service.nanopb_rpc",
110+
"${chip_root}/examples/common/pigweed:rpc_services",
111+
]
112+
113+
deps += pw_build_LINK_DEPS
114+
115+
include_dirs += [ "${chip_root}/examples/common" ]
116+
} else {
117+
# The system_rpc_server.cc file is in pigweed and doesn't compile with
118+
# -Wconversion, remove check for RPC build only.
119+
cflags = [ "-Wconversion" ]
120+
}
121+
68122
output_dir = root_out_dir
69123
}
70124

examples/bridge-app/linux/Backend.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
*
3+
* Copyright (c) 2022 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "Backend.h"
20+
21+
#include "main.h"
22+
23+
std::vector<std::unique_ptr<Device>> g_devices;
24+
std::vector<std::unique_ptr<DynamicDeviceImpl>> g_device_impls;
25+
26+
bool RemoveDeviceAt(uint32_t index)
27+
{
28+
if (index >= g_devices.size() || !g_devices[index])
29+
{
30+
return false;
31+
}
32+
33+
RemoveDeviceEndpoint(g_devices[index].get());
34+
35+
for (auto & room : gRooms)
36+
room.RemoveEndpoint(g_devices[index]->GetEndpointId());
37+
38+
g_devices[index] = nullptr;
39+
g_device_impls[index] = nullptr;
40+
41+
return true;
42+
}
43+
44+
int AddDevice(std::unique_ptr<DynamicDeviceImpl> device)
45+
{
46+
auto dev = std::make_unique<Device>(device->CreateDevice());
47+
int ep = AddDeviceEndpoint(dev.get());
48+
if (ep < 0)
49+
{
50+
return -1;
51+
}
52+
53+
size_t index = (size_t) ep;
54+
if (g_devices.size() <= index)
55+
{
56+
g_devices.resize(index + 1);
57+
g_device_impls.resize(index + 1);
58+
}
59+
g_devices[index] = std::move(dev);
60+
g_device_impls[index] = std::move(device);
61+
return ep;
62+
}

examples/bridge-app/linux/Device.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void Device::SetEndpointId(chip::EndpointId id)
4242

4343
const char * Device::GetName()
4444
{
45-
return mDeviceName;
45+
return mDeviceName.c_str();
4646
}
4747

4848
void Device::SetName(const char * name)

examples/bridge-app/linux/UserInputBackend.cpp

+3-22
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
namespace {
1717

18-
std::vector<std::unique_ptr<Device>> g_devices;
19-
std::vector<std::unique_ptr<DynamicDeviceImpl>> g_device_impls;
2018
std::unique_ptr<DynamicDeviceImpl> g_pending;
2119

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

172-
auto dev = std::make_unique<Device>(g_pending->CreateDevice());
173-
int ep = AddDeviceEndpoint(dev.get());
170+
int ep = AddDevice(std::move(g_pending));
171+
174172
if (ep < 0)
175173
{
176174
printf("Failed to add device\n");
177175
}
178176
else
179177
{
180178
printf("Added device at index %d\n", ep);
181-
size_t index = (size_t) ep;
182-
if (g_devices.size() <= index)
183-
{
184-
g_devices.resize(index + 1);
185-
g_device_impls.resize(index + 1);
186-
}
187-
g_devices[index] = std::move(dev);
188-
g_device_impls[index] = std::move(g_pending);
189179
}
190180
}
191181

@@ -198,19 +188,10 @@ void RemoveDevice(const std::vector<std::string> & tokens)
198188
printf("Error: %s.\nExpected index of a device\n", err);
199189
return;
200190
}
201-
if (index >= g_devices.size())
191+
if (!RemoveDeviceAt(index))
202192
{
203193
printf("%d is an invalid index\n", index);
204-
return;
205194
}
206-
207-
RemoveDeviceEndpoint(g_devices[index].get());
208-
209-
for (auto & room : gRooms)
210-
room.RemoveEndpoint(g_devices[index]->GetEndpointId());
211-
212-
g_devices[index] = nullptr;
213-
g_device_impls[index] = nullptr;
214195
}
215196

216197
void AddType(const std::vector<std::string> & tokens)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
*
3+
* Copyright (c) 2022 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "bridge_service.h"
20+
21+
#include "Backend.h"
22+
#include "main.h"
23+
24+
namespace chip {
25+
namespace rpc {
26+
27+
namespace {
28+
const clusters::ClusterInfo * FindCluster(uint32_t id)
29+
{
30+
for (const auto & cluster : clusters::kKnownClusters)
31+
{
32+
if (id == cluster.id)
33+
return &cluster;
34+
}
35+
return nullptr;
36+
}
37+
} // namespace
38+
39+
::pw::Status Bridge::Add(const ::chip_rpc_bridge_AddDevice & request, ::chip_rpc_bridge_AddDeviceResponse & response)
40+
{
41+
std::unique_ptr<DynamicDeviceImpl> pending;
42+
43+
pending = std::make_unique<DynamicDeviceImpl>();
44+
pending->SetParentEndpointId(request.parent_endpoint);
45+
46+
for (pb_size_t i = 0; i < request.clusters_count; i++)
47+
{
48+
const chip_rpc_bridge_Cluster & c = request.clusters[i];
49+
50+
auto cluster = FindCluster(c.cluster_id);
51+
if (!cluster)
52+
{
53+
return pw::Status::InvalidArgument();
54+
}
55+
56+
std::unique_ptr<CommonCluster> obj(cluster->ctor(::operator new(cluster->size)));
57+
DynamicAttributeList dynamic_attrs;
58+
auto attrs = obj->GetAllAttributes();
59+
for (auto & attr : attrs)
60+
dynamic_attrs.Add(attr);
61+
pending->AddCluster(std::move(obj), dynamic_attrs, nullptr, nullptr);
62+
}
63+
64+
for (pb_size_t i = 0; i < request.device_types_count; i++)
65+
{
66+
EmberAfDeviceType devType = { (uint16_t) request.device_types[i].id, (uint8_t) request.device_types[i].version };
67+
pending->AddDeviceType(devType);
68+
}
69+
70+
int ret = AddDevice(std::move(pending));
71+
if (ret < 0)
72+
{
73+
return pw::Status::Aborted();
74+
}
75+
response.id = ret;
76+
return pw::OkStatus();
77+
}
78+
79+
::pw::Status Bridge::Remove(const ::chip_rpc_bridge_RemoveDevice & request, ::chip_rpc_bridge_Empty & response)
80+
{
81+
if (!RemoveDeviceAt(request.id))
82+
{
83+
return pw::Status::NotFound();
84+
}
85+
86+
return pw::OkStatus();
87+
}
88+
89+
} // namespace rpc
90+
} // namespace chip

0 commit comments

Comments
 (0)