Skip to content

Commit 9b39b1d

Browse files
author
rgoliver
authored
RPC: Cleanup and add thread service (project-chip#14843)
* RPC: Refactor wifi RPC as common service - Move WiFi into a common rpc service like others. - Add WiFi RPC service to the all-clusters app * RPC: Add thread rpc service to efr and nrf - Add a Thread RPC service similar to others. - Add to NRF and EFR32 platforms - Add to efr/lighting RPC example app - Add to efr/lock RPC example app - Add to nfr/lighting RPC example app * RPC: Add get/set pairing state * RPC: Add OTCLI to EFR32 and NRF32 - Add the openthread cli as a new RPC service. - Add service to EFR32 and NRF32 platforms - Add to lighting/efr32 example - Add to lighting/nrf example * RPC: Refactor linux RPC into platform. Move common linux Rpc.* files into common platform to match refactoring done for other platforms. * RPC: Wake m5 display on RPC button press
1 parent dc95b77 commit 9b39b1d

File tree

28 files changed

+691
-114
lines changed

28 files changed

+691
-114
lines changed

examples/all-clusters-app/esp32/main/CMakeLists.txt

+5-4
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,15 @@ pw_proto_library(locking_service
198198

199199
pw_proto_library(wifi_service
200200
SOURCES
201-
${CHIP_ROOT}/examples/ipv6only-app/common/wifi_service/wifi_service.proto
201+
${CHIP_ROOT}/examples/common/pigweed/protos/wifi_service.proto
202202
INPUTS
203-
${CHIP_ROOT}/examples/ipv6only-app/common/wifi_service/wifi_service.options
203+
${CHIP_ROOT}/examples/common/pigweed/protos/wifi_service.options
204204
PREFIX
205205
wifi_service
206206
DEPS
207207
pw_protobuf.common_protos
208208
STRIP_PREFIX
209-
${CHIP_ROOT}/examples/ipv6only-app/common/wifi_service
209+
${CHIP_ROOT}/examples/common/pigweed/protos
210210
)
211211

212212
target_link_libraries(${COMPONENT_LIB} PUBLIC
@@ -237,6 +237,7 @@ target_compile_options(${COMPONENT_LIB} PRIVATE
237237
"-DPW_RPC_DEVICE_SERVICE=1"
238238
"-DPW_RPC_LIGHTING_SERVICE=1"
239239
"-DPW_RPC_LOCKING_SERVICE=1"
240-
"-DPW_RPC_TRACING_SERVICE=1")
240+
"-DPW_RPC_TRACING_SERVICE=1"
241+
"-DPW_RPC_WIFI_SERVICE=1")
241242

242243
endif (CONFIG_ENABLE_PW_RPC)

examples/common/pigweed/BUILD.gn

+23
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,29 @@ if (chip_enable_pw_rpc) {
7272
strip_prefix = "protos"
7373
prefix = "locking_service"
7474
}
75+
76+
pw_proto_library("ot_cli_service") {
77+
sources = [ "protos/ot_cli_service.proto" ]
78+
inputs = [ "protos/ot_cli_service.options" ]
79+
deps = [ "$dir_pw_protobuf:common_protos" ]
80+
strip_prefix = "protos"
81+
prefix = "ot_cli_service"
82+
}
83+
84+
pw_proto_library("thread_service") {
85+
sources = [ "protos/thread_service.proto" ]
86+
inputs = [ "protos/thread_service.options" ]
87+
deps = [ "$dir_pw_protobuf:common_protos" ]
88+
strip_prefix = "protos"
89+
prefix = "thread_service"
90+
}
91+
92+
pw_proto_library("wifi_service") {
93+
sources = [ "protos/wifi_service.proto" ]
94+
deps = [ "$dir_pw_protobuf:common_protos" ]
95+
strip_prefix = "protos"
96+
prefix = "wifi_service"
97+
}
7598
}
7699

77100
pw_source_set("rpc_services") {

examples/common/pigweed/protos/device_service.proto

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ message DeviceState {
2828
repeated FabricInfo fabric_info = 2;
2929
}
3030

31+
message PairingState {
32+
bool pairing_enabled = 1;
33+
}
34+
3135
service Device {
3236
rpc FactoryReset(pw.protobuf.Empty) returns (pw.protobuf.Empty){}
3337
rpc Reboot(pw.protobuf.Empty) returns (pw.protobuf.Empty){}
3438
rpc TriggerOta(pw.protobuf.Empty) returns (pw.protobuf.Empty){}
3539
rpc GetDeviceInfo(pw.protobuf.Empty) returns (DeviceInfo){}
3640
rpc GetDeviceState(pw.protobuf.Empty) returns (DeviceState){}
37-
rpc SetPairingInfo(PairingInfo) returns (pw.protobuf.Empty){}
41+
rpc SetPairingState(PairingState) returns (pw.protobuf.Empty){}
42+
rpc GetPairingState(pw.protobuf.Empty) returns (PairingState){}
3843
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
chip.rpc.OtCliResponse.response max_size:128
2+
chip.rpc.OtCliCommand.command max_size:128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
syntax = "proto3";
2+
3+
package chip.rpc;
4+
5+
import 'pw_protobuf_protos/common.proto';
6+
7+
message OtCliResponse {
8+
string response = 1;
9+
}
10+
11+
message OtCliCommand {
12+
string command = 1;
13+
}
14+
15+
service OtCli {
16+
// Handle the command using the Open Thread CLI, and stream the reponses back.
17+
rpc Command(OtCliCommand) returns (stream OtCliResponse){}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
chip.rpc.ThreadNetworkInfo.network_name max_size:17 // Maximum size of the Thread Network Name field + 1 for null
2+
chip.rpc.ThreadNetworkInfo.extended_pan_id max_size:8 // Size of extended pan id in bytes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
syntax = "proto3";
2+
3+
package chip.rpc;
4+
5+
import 'pw_protobuf_protos/common.proto';
6+
7+
enum ThreadDeviceType {
8+
DEVICE_TYPE_NOT_SUPPORTED = 0;
9+
DEVICE_TYPE_ROUTER = 1;
10+
DEVICE_TYPE_FULL_END_DEVICE = 2;
11+
DEVICE_TYPE_MINIMAL_END_DEVICE = 3;
12+
DEVICE_TYPE_SLEEPY_END_DEVICE = 4;
13+
}
14+
15+
message ThreadState {
16+
bool is_provisioned = 1;
17+
bool is_enabled = 2;
18+
bool is_attached = 3;
19+
ThreadDeviceType device_type = 4;
20+
}
21+
22+
message ThreadNetworkInfo {
23+
uint32 pan_id = 1;
24+
string network_name = 2;
25+
uint32 channel = 3;
26+
bytes extended_pan_id = 4;
27+
}
28+
29+
service Thread {
30+
rpc GetState(pw.protobuf.Empty) returns (ThreadState){}
31+
rpc GetNetworkInfo(pw.protobuf.Empty) returns (ThreadNetworkInfo){}
32+
}

examples/common/pigweed/rpc_console/py/BUILD.gn

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ pw_python_package("chip_rpc") {
4242
"${chip_root}/examples/common/pigweed:echo_service.python",
4343
"${chip_root}/examples/common/pigweed:lighting_service.python",
4444
"${chip_root}/examples/common/pigweed:locking_service.python",
45-
"${chip_root}/examples/ipv6only-app/common:wifi_service.python",
45+
"${chip_root}/examples/common/pigweed:ot_cli_service.python",
46+
"${chip_root}/examples/common/pigweed:thread_service.python",
47+
"${chip_root}/examples/common/pigweed:wifi_service.python",
4648
]
4749
}
4850

examples/common/pigweed/rpc_console/py/chip_rpc/console.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@
5757
from attributes_service import attributes_service_pb2
5858
from button_service import button_service_pb2
5959
from device_service import device_service_pb2
60+
from echo_service import echo_pb2
6061
from lighting_service import lighting_service_pb2
6162
from locking_service import locking_service_pb2
63+
from ot_cli_service import ot_cli_service_pb2
64+
from thread_service import thread_service_pb2
6265
from wifi_service import wifi_service_pb2
63-
from echo_service import echo_pb2
6466

6567
_LOG = logging.getLogger(__name__)
6668
_DEVICE_LOG = logging.getLogger('rpc_device')
@@ -69,13 +71,15 @@
6971
SOCKET_SERVER = 'localhost'
7072
SOCKET_PORT = 33000
7173

72-
PROTOS = [button_service_pb2,
73-
attributes_service_pb2,
74+
PROTOS = [attributes_service_pb2,
75+
button_service_pb2,
76+
device_service_pb2,
77+
echo_pb2,
7478
lighting_service_pb2,
7579
locking_service_pb2,
76-
wifi_service_pb2,
77-
device_service_pb2,
78-
echo_pb2]
80+
ot_cli_service_pb2,
81+
thread_service_pb2,
82+
wifi_service_pb2]
7983

8084

8185
def _parse_args():

examples/common/pigweed/rpc_services/Device.h

+20
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ class Device : public pw_rpc::nanopb::Device::Service<Device>
5252
return pw::Status::Unimplemented();
5353
}
5454

55+
virtual pw::Status SetPairingState(const chip_rpc_PairingState & request, pw_protobuf_Empty & response)
56+
{
57+
if (request.pairing_enabled)
58+
{
59+
DeviceLayer::ConnectivityMgr().SetBLEAdvertisingEnabled(true);
60+
DeviceLayer::ConnectivityMgr().SetBLEAdvertisingMode(DeviceLayer::ConnectivityMgr().kFastAdvertising);
61+
}
62+
else
63+
{
64+
DeviceLayer::ConnectivityMgr().SetBLEAdvertisingEnabled(false);
65+
}
66+
return pw::OkStatus();
67+
}
68+
69+
virtual pw::Status GetPairingState(const pw_protobuf_Empty & request, chip_rpc_PairingState & response)
70+
{
71+
response.pairing_enabled = DeviceLayer::ConnectivityMgr().IsBLEAdvertisingEnabled();
72+
return pw::OkStatus();
73+
}
74+
5575
virtual pw::Status GetDeviceState(const pw_protobuf_Empty & request, chip_rpc_DeviceState & response)
5676
{
5777
uint64_t time_since_boot_sec;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
#pragma once
20+
21+
#if CHIP_ENABLE_OPENTHREAD
22+
#include <openthread/cli.h>
23+
#include <openthread/instance.h>
24+
25+
static_assert(OPENTHREAD_API_VERSION >= 85, "Invalid Open Thread version");
26+
#endif // CHIP_ENABLE_OPENTHREAD
27+
28+
#include "ot_cli_service/ot_cli_service.rpc.pb.h"
29+
#include "platform/ConfigurationManager.h"
30+
#include "pw_log/log.h"
31+
32+
namespace chip {
33+
namespace rpc {
34+
35+
class OtCli : public pw_rpc::nanopb::OtCli::Service<OtCli>
36+
{
37+
public:
38+
virtual ~OtCli() = default;
39+
40+
virtual void Command(const ::chip_rpc_OtCliCommand & request, ServerWriter<::chip_rpc_OtCliResponse> & writer)
41+
{
42+
#if CHIP_ENABLE_OPENTHREAD
43+
LazyInit();
44+
rpc_writer = &writer;
45+
otCliInputLine(const_cast<char *>(request.command));
46+
rpc_writer->Finish();
47+
rpc_writer = nullptr;
48+
#endif // CHIP_ENABLE_OPENTHREAD
49+
}
50+
51+
private:
52+
ServerWriter<::chip_rpc_OtCliResponse> * rpc_writer = nullptr;
53+
54+
#if CHIP_ENABLE_OPENTHREAD
55+
bool mInitComplete = false;
56+
57+
void LazyInit()
58+
{
59+
if (mInitComplete)
60+
{
61+
return;
62+
}
63+
otCliInit(otInstanceInitSingle(), &OnOtCliOutput, this);
64+
mInitComplete = true;
65+
}
66+
67+
static int OnOtCliOutput(void * context, const char * format, va_list arguments)
68+
{
69+
OtCli * ot_cli = reinterpret_cast<OtCli *>(context);
70+
if (ot_cli->rpc_writer)
71+
{
72+
chip_rpc_OtCliResponse out;
73+
int rval = vsnprintf(out.response, sizeof(out.response), format, arguments);
74+
ot_cli->rpc_writer->Write(out);
75+
return rval;
76+
}
77+
return 0;
78+
}
79+
#endif // CHIP_ENABLE_OPENTHREAD
80+
};
81+
82+
} // namespace rpc
83+
} // namespace chip
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
#pragma once
20+
21+
#include <platform/CHIPDeviceConfig.h>
22+
23+
#include "platform/ConnectivityManager.h"
24+
#include "thread_service/thread_service.rpc.pb.h"
25+
26+
#if CHIP_ENABLE_OPENTHREAD
27+
#include <openthread/dataset.h>
28+
#include <platform/ThreadStackManager.h>
29+
#endif
30+
31+
namespace chip {
32+
namespace rpc {
33+
34+
// Implementation class for chip.rpc.Thread.
35+
class Thread : public pw_rpc::nanopb::Thread::Service<Thread>
36+
{
37+
public:
38+
::pw::Status GetState(const pw_protobuf_Empty & request, chip_rpc_ThreadState & response)
39+
{
40+
response.is_provisioned = DeviceLayer::ConnectivityMgr().IsThreadProvisioned();
41+
response.is_enabled = DeviceLayer::ConnectivityMgr().IsThreadEnabled();
42+
response.is_attached = DeviceLayer::ConnectivityMgr().IsThreadAttached();
43+
response.device_type = static_cast<chip_rpc_ThreadDeviceType>(DeviceLayer::ConnectivityMgr().GetThreadDeviceType());
44+
return pw::OkStatus();
45+
}
46+
47+
::pw::Status GetNetworkInfo(const pw_protobuf_Empty & request, chip_rpc_ThreadNetworkInfo & response)
48+
{
49+
#if CHIP_ENABLE_OPENTHREAD
50+
otInstance * otInst = DeviceLayer::ThreadStackMgrImpl().OTInstance();
51+
otOperationalDataset data;
52+
if (OT_ERROR_NONE != otDatasetGetActive(otInst, &data))
53+
{
54+
return pw::Status::NotFound();
55+
}
56+
57+
if (data.mComponents.mIsPanIdPresent)
58+
{
59+
response.pan_id = data.mPanId;
60+
}
61+
if (data.mComponents.mIsNetworkNamePresent)
62+
{
63+
snprintf(response.network_name, sizeof(response.network_name), "%s", data.mNetworkName.m8);
64+
}
65+
if (data.mComponents.mIsChannelPresent)
66+
{
67+
response.channel = data.mChannel;
68+
}
69+
if (data.mComponents.mIsExtendedPanIdPresent)
70+
{
71+
size_t size = std::min(sizeof(response.extended_pan_id.bytes), sizeof(data.mExtendedPanId));
72+
memcpy(response.extended_pan_id.bytes, data.mExtendedPanId.m8, size);
73+
response.extended_pan_id.size = size;
74+
}
75+
return pw::OkStatus();
76+
#else // CHIP_ENABLE_OPENTHREAD
77+
return ::pw::Status::Unimplemented();
78+
#endif // CHIP_ENABLE_OPENTHREAD
79+
}
80+
};
81+
82+
} // namespace rpc
83+
} // namespace chip

0 commit comments

Comments
 (0)