Skip to content

Commit c4f467b

Browse files
yufengwangcaandy31415
authored andcommitted
[Fabric-Sync] Add RPC method RemoveSynchronizedDevice (project-chip#34121)
* Add RPC method RemoveSynchronizedDevice * Update examples/fabric-admin/rpc/RpcClient.cpp Co-authored-by: Andrei Litvin <andy314@gmail.com> * Update examples/fabric-bridge-app/linux/RpcServer.cpp Co-authored-by: Andrei Litvin <andy314@gmail.com> * Update comments for RemoveSynchronizedDevice --------- Co-authored-by: Andrei Litvin <andy314@gmail.com>
1 parent badca57 commit c4f467b

File tree

10 files changed

+98
-9
lines changed

10 files changed

+98
-9
lines changed

.github/workflows/examples-linux-standalone.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -202,21 +202,21 @@ jobs:
202202
run: |
203203
./scripts/run_in_build_env.sh \
204204
"./scripts/build/build_examples.py \
205-
--target linux-x64-fabric-admin \
205+
--target linux-x64-fabric-admin-rpc \
206206
build"
207207
.environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \
208208
linux debug fabric-admin \
209-
out/linux-x64-fabric-admin/fabric-admin \
209+
out/linux-x64-fabric-admin-rpc/fabric-admin \
210210
/tmp/bloat_reports/
211211
- name: Build example Fabric Bridge App
212212
run: |
213213
./scripts/run_in_build_env.sh \
214214
"./scripts/build/build_examples.py \
215-
--target linux-x64-fabric-bridge \
215+
--target linux-x64-fabric-bridge-no-ble-rpc \
216216
build"
217217
.environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \
218218
linux debug fabric-bridge-app \
219-
out/linux-x64-fabric-bridge/fabric-bridge-app \
219+
out/linux-x64-fabric-bridge-no-ble-rpc/fabric-bridge-app \
220220
/tmp/bloat_reports/
221221
- name: Uploading Size Reports
222222
uses: ./.github/actions/upload-size-reports

examples/common/pigweed/protos/fabric_bridge_service.proto

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ message SynchronizedDevice {
1111

1212
service FabricBridge {
1313
rpc AddSynchronizedDevice(SynchronizedDevice) returns (pw.protobuf.Empty){}
14+
rpc RemoveSynchronizedDevice(SynchronizedDevice) returns (pw.protobuf.Empty){}
1415
}
1516

examples/common/pigweed/rpc_services/FabricBridge.h

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class FabricBridge : public pw_rpc::nanopb::FabricBridge::Service<FabricBridge>
3838
{
3939
return pw::Status::Unimplemented();
4040
}
41+
42+
virtual pw::Status RemoveSynchronizedDevice(const chip_rpc_SynchronizedDevice & request, pw_protobuf_Empty & response)
43+
{
44+
return pw::Status::Unimplemented();
45+
}
4146
};
4247

4348
} // namespace rpc

examples/fabric-admin/BUILD.gn

+4-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ config("config") {
4646
defines += [ "CONFIG_USE_LOCAL_STORAGE" ]
4747
}
4848

49-
cflags = [ "-Wconversion" ]
50-
5149
if (chip_enable_pw_rpc) {
5250
defines += [ "PW_RPC_ENABLED" ]
5351
}
@@ -144,6 +142,10 @@ static_library("fabric-admin-utils") {
144142
]
145143

146144
deps += pw_build_LINK_DEPS
145+
} else {
146+
# The system_rpc_server.cc file is in pigweed and doesn't compile with
147+
# -Wconversion, remove check for RPC build only.
148+
cflags = [ "-Wconversion" ]
147149
}
148150

149151
if (chip_enable_transport_trace) {

examples/fabric-admin/args.gni

+1
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ matter_log_json_payload_decode_full = true
3232
# make fabric-admin very strict by default
3333
chip_tlv_validate_char_string_on_read = true
3434
chip_tlv_validate_char_string_on_write = true
35+
chip_enable_ble = true

examples/fabric-admin/rpc/RpcClient.cpp

+45-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ constexpr uint32_t kDefaultChannelId = 1;
4141
// Fabric Bridge Client
4242
rpc::pw_rpc::nanopb::FabricBridge::Client fabricBridgeClient(rpc::client::GetDefaultRpcClient(), kDefaultChannelId);
4343
pw::rpc::NanopbUnaryReceiver<::pw_protobuf_Empty> addSynchronizedDeviceCall;
44+
pw::rpc::NanopbUnaryReceiver<::pw_protobuf_Empty> removeSynchronizedDeviceCall;
4445

4546
// Callback function to be called when the RPC response is received
4647
void OnAddDeviceResponseCompleted(const pw_protobuf_Empty & response, pw::Status status)
@@ -55,6 +56,19 @@ void OnAddDeviceResponseCompleted(const pw_protobuf_Empty & response, pw::Status
5556
}
5657
}
5758

59+
// Callback function to be called when the RPC response is received
60+
void OnRemoveDeviceResponseCompleted(const pw_protobuf_Empty & response, pw::Status status)
61+
{
62+
if (status.ok())
63+
{
64+
ChipLogProgress(NotSpecified, "RemoveSynchronizedDevice RPC call succeeded!");
65+
}
66+
else
67+
{
68+
ChipLogProgress(NotSpecified, "RemoveSynchronizedDevice RPC call failed with status: %d", status.code());
69+
}
70+
}
71+
5872
} // namespace
5973

6074
CHIP_ERROR InitRpcClient(uint16_t rpcServerPort)
@@ -76,11 +90,41 @@ CHIP_ERROR AddSynchronizedDevice(chip::NodeId nodeId)
7690
chip_rpc_SynchronizedDevice device;
7791
device.node_id = nodeId;
7892

79-
// The RPC will remain active as long as `addSynchronizedDeviceCall` is alive.
93+
// By assigning the returned call to the global 'addSynchronizedDeviceCall', the RPC
94+
// call is kept alive until it completes. When a response is received, it
95+
// will be logged by the handler function and the call will complete.
8096
addSynchronizedDeviceCall = fabricBridgeClient.AddSynchronizedDevice(device, OnAddDeviceResponseCompleted);
8197

8298
if (!addSynchronizedDeviceCall.active())
8399
{
100+
// The RPC call was not sent. This could occur due to, for example, an invalid channel ID. Handle if necessary.
101+
return CHIP_ERROR_INTERNAL;
102+
}
103+
104+
return CHIP_NO_ERROR;
105+
}
106+
107+
CHIP_ERROR RemoveSynchronizedDevice(chip::NodeId nodeId)
108+
{
109+
ChipLogProgress(NotSpecified, "RemoveSynchronizedDevice");
110+
111+
if (removeSynchronizedDeviceCall.active())
112+
{
113+
ChipLogError(NotSpecified, "Remove Synchronized Device operation is in progress\n");
114+
return CHIP_ERROR_BUSY;
115+
}
116+
117+
chip_rpc_SynchronizedDevice device;
118+
device.node_id = nodeId;
119+
120+
// By assigning the returned call to the global 'removeSynchronizedDeviceCall', the RPC
121+
// call is kept alive until it completes. When a response is received, it
122+
// will be logged by the handler function and the call will complete.
123+
removeSynchronizedDeviceCall = fabricBridgeClient.RemoveSynchronizedDevice(device, OnRemoveDeviceResponseCompleted);
124+
125+
if (!removeSynchronizedDeviceCall.active())
126+
{
127+
// The RPC call was not sent. This could occur due to, for example, an invalid channel ID. Handle if necessary.
84128
return CHIP_ERROR_INTERNAL;
85129
}
86130

examples/fabric-admin/rpc/RpcClient.h

+15
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,18 @@ CHIP_ERROR InitRpcClient(uint16_t rpcServerPort);
4646
* - CHIP_ERROR_INTERNAL: An internal error occurred while activating the RPC call.
4747
*/
4848
CHIP_ERROR AddSynchronizedDevice(chip::NodeId nodeId);
49+
50+
/**
51+
* @brief Removes a synchronized device from the RPC client.
52+
*
53+
* This function attempts to remove a device identified by its `nodeId` from the synchronized device list.
54+
* It logs the progress and checks if a `RemoveSynchronizedDevice` operation is already in progress.
55+
* If an operation is in progress, it returns `CHIP_ERROR_BUSY`.
56+
*
57+
* @param nodeId The Node ID of the device to be removed.
58+
* @return CHIP_ERROR An error code indicating the success or failure of the operation.
59+
* - CHIP_NO_ERROR: The RPC command was successfully sent.
60+
* - CHIP_ERROR_BUSY: Another operation is currently in progress.
61+
* - CHIP_ERROR_INTERNAL: An internal error occurred while activating the RPC call.
62+
*/
63+
CHIP_ERROR RemoveSynchronizedDevice(chip::NodeId nodeId);

examples/fabric-bridge-app/linux/BUILD.gn

+4-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ executable("fabric-bridge-app") {
4646
"${chip_root}/src/lib",
4747
]
4848

49-
cflags = [ "-Wconversion" ]
50-
5149
include_dirs = [ "include" ]
5250

5351
if (bridge_enable_pw_rpc) {
@@ -87,6 +85,10 @@ executable("fabric-bridge-app") {
8785
"${chip_root}/examples/common",
8886
"${chip_root}/examples/platform/linux",
8987
]
88+
} else {
89+
# The system_rpc_server.cc file is in pigweed and doesn't compile with
90+
# -Wconversion, remove check for RPC build only.
91+
cflags = [ "-Wconversion" ]
9092
}
9193

9294
output_dir = root_out_dir

examples/fabric-bridge-app/linux/RpcServer.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class FabricBridge final : public chip::rpc::FabricBridge
4343
{
4444
public:
4545
pw::Status AddSynchronizedDevice(const chip_rpc_SynchronizedDevice & request, pw_protobuf_Empty & response) override;
46+
pw::Status RemoveSynchronizedDevice(const chip_rpc_SynchronizedDevice & request, pw_protobuf_Empty & response) override;
4647
};
4748

4849
pw::Status FabricBridge::AddSynchronizedDevice(const chip_rpc_SynchronizedDevice & request, pw_protobuf_Empty & response)
@@ -64,6 +65,21 @@ pw::Status FabricBridge::AddSynchronizedDevice(const chip_rpc_SynchronizedDevice
6465
return pw::OkStatus();
6566
}
6667

68+
pw::Status FabricBridge::RemoveSynchronizedDevice(const chip_rpc_SynchronizedDevice & request, pw_protobuf_Empty & response)
69+
{
70+
NodeId nodeId = request.node_id;
71+
ChipLogProgress(NotSpecified, "Received RemoveSynchronizedDevice: " ChipLogFormatX64, ChipLogValueX64(nodeId));
72+
73+
int removed_idx = DeviceMgr().RemoveDeviceByNodeId(nodeId);
74+
if (removed_idx < 0)
75+
{
76+
ChipLogError(NotSpecified, "Failed to remove device with nodeId=0x" ChipLogFormatX64, ChipLogValueX64(nodeId));
77+
return pw::Status::NotFound();
78+
}
79+
80+
return pw::OkStatus();
81+
}
82+
6783
FabricBridge fabric_bridge_service;
6884
#endif // defined(PW_RPC_FABRIC_BRIDGE_SERVICE) && PW_RPC_FABRIC_BRIDGE_SERVICE
6985

examples/fabric-bridge-app/linux/main.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ using namespace chip::app::Clusters::AdministratorCommissioning;
4747
namespace {
4848

4949
constexpr uint16_t kPollIntervalMs = 100;
50+
51+
#if defined(PW_RPC_FABRIC_BRIDGE_SERVICE) && PW_RPC_FABRIC_BRIDGE_SERVICE
5052
constexpr uint16_t kRetryIntervalS = 3;
53+
#endif
5154

5255
bool KeyboardHit()
5356
{

0 commit comments

Comments
 (0)