Skip to content

Commit

Permalink
[Mbed] Add RPC support for locking and lighting examples (#11039)
Browse files Browse the repository at this point in the history
* Add CONFIG_CHIP_PW_RPC flag to lock and lighting apps

* Add more RPC services to Mbed build
Create common RPC service run function
Add RPC support for lock and lighting apps
Add button event handler to lock app task

* Fix Locking RPC service - changed locked variable type to boolen

* Add AppTask reference in Rpc.cpp
Changes restyle

* Fix AppTask.h include

* Remove CHIPDeviceLayer include from RPC.cpp file

* Increase Mbed lock-app and lighting-app build timeout in CI
  • Loading branch information
ATmobica authored and pull[bot] committed Dec 10, 2021
1 parent ee38ff6 commit 1384970
Show file tree
Hide file tree
Showing 15 changed files with 319 additions and 61 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/examples-mbed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
.environment/pigweed-venv/*.log
- name: Build lock-app example
timeout-minutes: 10
timeout-minutes: 12
run: |
scripts/examples/mbed_example.sh -a=lock-app -b=$APP_TARGET -p=$APP_PROFILE
.environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \
Expand All @@ -73,7 +73,7 @@ jobs:
/tmp/bloat_reports/
- name: Build lighting-app example
timeout-minutes: 10
timeout-minutes: 12
run: |
scripts/examples/mbed_example.sh -a=lighting-app -b=$APP_TARGET -p=$APP_PROFILE
.environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \
Expand Down
77 changes: 62 additions & 15 deletions config/mbed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,12 @@ if (CONFIG_CHIP_PW_RPC)
list(APPEND CHIP_LIBRARIES -lPwRpc)
if (${APP_TARGET} MATCHES "pigweed-app")
set(CONFIG_CHIP_PW_RPC_ECHO_PROTO "y")
else()
set(CONFIG_CHIP_PW_RPC_ECHO_PROTO "n")
elseif (${APP_TARGET} MATCHES "lighting-app")
set(CONFIG_CHIP_PW_RPC_COMMON_PROTO "y")
set(CONFIG_CHIP_PW_RPC_LIGHTING_PROTO "y")
elseif (${APP_TARGET} MATCHES "lock-app")
set(CONFIG_CHIP_PW_RPC_COMMON_PROTO "y")
set(CONFIG_CHIP_PW_RPC_LOCKING_PROTO "y")
endif()
endif(CONFIG_CHIP_PW_RPC)

Expand Down Expand Up @@ -271,6 +275,9 @@ chip_gn_arg_bool ("chip_bypass_rendezvous" CONFIG_CHIP_BYPASS_REN
chip_gn_arg_bool ("chip_build_pw_rpc_lib" CONFIG_CHIP_PW_RPC)
if (CONFIG_CHIP_PW_RPC)
chip_gn_arg_bool ("chip_build_pw_rpc_echo_proto" CONFIG_CHIP_PW_RPC_ECHO_PROTO)
chip_gn_arg_bool ("chip_build_pw_rpc_common_proto" CONFIG_CHIP_PW_RPC_COMMON_PROTO)
chip_gn_arg_bool ("chip_build_pw_rpc_lighting_proto" CONFIG_CHIP_PW_RPC_LIGHTING_PROTO)
chip_gn_arg_bool ("chip_build_pw_rpc_locking_proto" CONFIG_CHIP_PW_RPC_LOCKING_PROTO)
endif(CONFIG_CHIP_PW_RPC)

file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/args.gn CONTENT ${CHIP_GN_ARGS})
Expand Down Expand Up @@ -335,26 +342,18 @@ endif()
if (CONFIG_CHIP_PW_RPC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++17" PARENT_SCOPE)
list(APPEND CHIP_DEFINES
CONFIG_CHIP_PW_RPC=1
CHIP_PW_RPC=1
)
endif()

target_include_directories(${APP_TARGET} PRIVATE
${CHIP_INCLUDES}
)

target_compile_definitions(${APP_TARGET} PRIVATE
${CHIP_DEFINES}
)


if (CONFIG_CHIP_PW_RPC)

set(PIGWEED_ROOT "${CHIP_ROOT}/third_party/pigweed/repo")

target_sources(${APP_TARGET} PRIVATE
${CHIP_ROOT}/examples/common/pigweed/RpcService.cpp
${CHIP_ROOT}/examples/common/pigweed/mbed/PigweedLoggerMutex.cpp
${CHIP_ROOT}/examples/common/pigweed/mbed/Rpc.cpp
${MBED_COMMON}/util/PigweedLogger.cpp
)

Expand Down Expand Up @@ -389,16 +388,64 @@ target_include_directories(${APP_TARGET} PRIVATE
${CHIP_ROOT}/third_party/nanopb/repo

${CHIP_ROOT}/examples/common
${CHIP_ROOT}/examples//common/pigweed/mbed
${CHIP_ROOT}/examples/common/pigweed
${CHIP_ROOT}/examples/common/pigweed/mbed
${MBED_COMMON}/pw_sys_io/public

${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/third_party/pigweed/repo/pw_rpc/protos.proto_library/pwpb
${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/third_party/pigweed/repo/pw_protobuf/common_protos.proto_library/nanopb
)

if (CONFIG_CHIP_PW_RPC_ECHO_PROTO)
target_include_directories(${APP_TARGET} PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/third_party/pigweed/repo/pw_rpc/protos.proto_library/nanopb_rpc
${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/third_party/pigweed/repo/pw_rpc/protos.proto_library/nanopb
${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/third_party/pigweed/repo/pw_rpc/protos.proto_library/pwpb
)
list(APPEND CHIP_DEFINES
CHIP_PW_RPC_ECHO_PROTO=1
)
endif(CONFIG_CHIP_PW_RPC_ECHO_PROTO)

endif(CONFIG_CHIP_PW_RPC)
if (CONFIG_CHIP_PW_RPC_COMMON_PROTO)
target_include_directories(${APP_TARGET} PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/examples/common/pigweed/button_service.proto_library/nanopb
${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/examples/common/pigweed/button_service.proto_library/nanopb_rpc

${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/examples/common/pigweed/device_service.proto_library/nanopb
${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/examples/common/pigweed/device_service.proto_library/nanopb_rpc
)
list(APPEND CHIP_DEFINES
CHIP_PW_RPC_COMMON_PROTO=1
)
endif(CONFIG_CHIP_PW_RPC_COMMON_PROTO)

if (CONFIG_CHIP_PW_RPC_LIGHTING_PROTO)
target_include_directories(${APP_TARGET} PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/examples/common/pigweed/lighting_service.proto_library/nanopb
${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/examples/common/pigweed/lighting_service.proto_library/nanopb_rpc
)
list(APPEND CHIP_DEFINES
CHIP_PW_RPC_LIGHTING_PROTO=1
)
endif(CONFIG_CHIP_PW_RPC_LIGHTING_PROTO)

if (CONFIG_CHIP_PW_RPC_LOCKING_PROTO)
target_include_directories(${APP_TARGET} PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/examples/common/pigweed/locking_service.proto_library/nanopb
${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/examples/common/pigweed/locking_service.proto_library/nanopb_rpc
)
list(APPEND CHIP_DEFINES
CHIP_PW_RPC_LOCKING_PROTO=1
)
endif(CONFIG_CHIP_PW_RPC_LOCKING_PROTO)

endif(CONFIG_CHIP_PW_RPC)


target_include_directories(${APP_TARGET} PRIVATE
${CHIP_INCLUDES}
)

target_compile_definitions(${APP_TARGET} PRIVATE
${CHIP_DEFINES}
)
20 changes: 20 additions & 0 deletions config/mbed/chip-gn/lib/pw_rpc/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import("$dir_pw_build/target_types.gni")

declare_args() {
chip_build_pw_rpc_echo_proto = false
chip_build_pw_rpc_common_proto = false
chip_build_pw_rpc_lighting_proto = false
chip_build_pw_rpc_locking_proto = false
}

static_library("pw_rpc") {
Expand All @@ -37,6 +40,23 @@ static_library("pw_rpc") {
deps += [ "$dir_pw_rpc/nanopb:echo_service" ]
}

if (chip_build_pw_rpc_common_proto) {
deps += [
"${chip_root}/examples/common/pigweed:button_service.nanopb_rpc",
"${chip_root}/examples/common/pigweed:device_service.nanopb_rpc",
]
}

if (chip_build_pw_rpc_lighting_proto) {
deps +=
[ "${chip_root}/examples/common/pigweed:lighting_service.nanopb_rpc" ]
}

if (chip_build_pw_rpc_locking_proto) {
deps +=
[ "${chip_root}/examples/common/pigweed:locking_service.nanopb_rpc" ]
}

deps += pw_build_LINK_DEPS

output_dir = "${root_out_dir}/lib"
Expand Down
137 changes: 137 additions & 0 deletions examples/common/pigweed/mbed/Rpc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
*
* Copyright (c) 2021 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 "Rpc.h"
#include "PigweedLogger.h"
#include "PigweedLoggerMutex.h"
#include "pigweed/RpcService.h"

#include "pw_hdlc/rpc_channel.h"
#include "pw_hdlc/rpc_packets.h"
#include "pw_rpc/server.h"
#include "pw_stream/sys_io_stream.h"
#include "pw_sys_io/sys_io.h"
#include "pw_sys_io_mbed/init.h"

#include <support/logging/CHIPLogging.h>

#ifdef CHIP_PW_RPC_ECHO_PROTO
#include "pw_rpc/echo_service_nanopb.h"
#endif

#ifdef CHIP_PW_RPC_COMMON_PROTO
#include "AppTask.h"
#include "rpc_services/Button.h"
#include "rpc_services/Device.h"
#endif

#ifdef CHIP_PW_RPC_LIGHTING_PROTO
#include "rpc_services/Lighting.h"
#endif

#ifdef CHIP_PW_RPC_LOCKING_PROTO
#include "rpc_services/Locking.h"
#endif

using namespace ::chip::DeviceLayer;
using namespace ::rtos;

namespace chip {
namespace rpc {

#ifdef CHIP_PW_RPC_COMMON_PROTO
class MbedButton final : public Button
{
public:
pw::Status Event(ServerContext &, const chip_rpc_ButtonEvent & request, pw_protobuf_Empty & response)
{
GetAppTask().ButtonEventHandler(request.idx, request.pushed);
return pw::OkStatus();
}
};
#endif

namespace {

#define RPC_THREAD_NAME "RPC"
#define RPC_STACK_SIZE (4 * 1024)

rtos::Thread rpcThread{ osPriorityNormal, RPC_STACK_SIZE, /* memory provided */ nullptr, RPC_THREAD_NAME };

#ifdef CHIP_PW_RPC_ECHO_PROTO
pw::rpc::EchoService echo_service;
#endif

#ifdef CHIP_PW_RPC_COMMON_PROTO
chip::rpc::MbedButton button_service;
chip::rpc::Device device_service;
#endif

#ifdef CHIP_PW_RPC_LIGHTING_PROTO
chip::rpc::Lighting lighting_service;
#endif

#ifdef CHIP_PW_RPC_LOCKING_PROTO
chip::rpc::Locking locking_service;
#endif

void RegisterServices(pw::rpc::Server & server)
{
#ifdef CHIP_PW_RPC_ECHO_PROTO
server.RegisterService(echo_service);
#endif

#ifdef CHIP_PW_RPC_COMMON_PROTO
server.RegisterService(button_service);
server.RegisterService(device_service);
#endif

#ifdef CHIP_PW_RPC_LIGHTING_PROTO
server.RegisterService(lighting_service);
#endif

#ifdef CHIP_PW_RPC_LOCKING_PROTO
server.RegisterService(locking_service);
#endif
}

} // namespace

void RunRpcService()
{
Start(RegisterServices, &logger_mutex);
}

Thread * Init()
{
pw_sys_io_Init();

ChipLogProgress(NotSpecified, "RPC service starting...\r\n");

auto error = rpcThread.start(RunRpcService);
if (error != osOK)
{
ChipLogError(NotSpecified, "Run RPC service failed[%d]", error);
return NULL;
}

return &rpcThread;
}

} // namespace rpc
} // namespace chip
30 changes: 30 additions & 0 deletions examples/common/pigweed/mbed/Rpc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* 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.
*/

#pragma once

#include "rtos/Thread.h"

namespace chip {
namespace rpc {

void RunRpcService();

rtos::Thread * Init();

} // namespace rpc
} // namespace chip
4 changes: 2 additions & 2 deletions examples/common/pigweed/rpc_services/Locking.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ class Locking final : public generated::Locking<Locking>

virtual pw::Status Set(ServerContext &, const chip_rpc_LockingState & request, pw_protobuf_Empty & response)
{
uint8_t locked = request.locked;
bool locked = request.locked;
RETURN_STATUS_IF_NOT_OK(app::Clusters::OnOff::Attributes::OnOff::Set(kEndpoint, locked));
return pw::OkStatus();
}

virtual pw::Status Get(ServerContext &, const pw_protobuf_Empty & request, chip_rpc_LockingState & response)
{
uint8_t locked;
bool locked;
RETURN_STATUS_IF_NOT_OK(app::Clusters::OnOff::Attributes::OnOff::Get(kEndpoint, &locked));
response.locked = locked;
return pw::OkStatus();
Expand Down
3 changes: 2 additions & 1 deletion examples/lighting-app/mbed/config.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ CONFIG_CHIP_BUILD_TESTS=n
CONFIG_CHIP_WITH_EXTERNAL_MBEDTLS=y
CONFIG_CHIP_PROJECT_CONFIG=main/include/CHIPProjectConfig.h
CONFIG_CHIP_BYPASS_RENDEZVOUS=n
CONFIG_MBED_BSD_SOCKET_TRACE=n
CONFIG_MBED_BSD_SOCKET_TRACE=n
CONFIG_CHIP_PW_RPC=y
1 change: 0 additions & 1 deletion examples/lighting-app/mbed/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ static bool sHaveBLEConnections = false;

static mbed::Timeout sFunctionTimer;

// TODO: change EventQueue default event size
static events::EventQueue sAppEventQueue;

using namespace ::chip::Credentials;
Expand Down
3 changes: 2 additions & 1 deletion examples/lighting-app/mbed/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class AppTask
void PostEvent(AppEvent * aEvent);
void UpdateClusterState(void);

void ButtonEventHandler(uint32_t id, bool pushed);

private:
friend AppTask & GetAppTask(void);

Expand All @@ -50,7 +52,6 @@ class AppTask
void LightingButtonPressEventHandler(void);
void FunctionButtonPressEventHandler(void);
void FunctionButtonReleaseEventHandler(void);
void ButtonEventHandler(uint32_t id, bool pushed);
void SliderEventHandler(int slider_pos);
void TimerEventHandler(void);

Expand Down
Loading

0 comments on commit 1384970

Please sign in to comment.