diff --git a/.github/.wordlist.txt b/.github/.wordlist.txt index 5cdc3211a85e39..79ecf55feebc8e 100644 --- a/.github/.wordlist.txt +++ b/.github/.wordlist.txt @@ -996,6 +996,7 @@ TransportMgrBase TrustedRootCertificates TSG tsv +TTL tty ttyACM ttyACMx diff --git a/config/ameba/args.gni b/config/ameba/args.gni index d0cea9dfeb220f..fe49e059010256 100755 --- a/config/ameba/args.gni +++ b/config/ameba/args.gni @@ -36,3 +36,4 @@ custom_toolchain = "//third_party/connectedhomeip/config/ameba/toolchain:ameba" pw_build_PIP_CONSTRAINTS = [ "//third_party/connectedhomeip/scripts/constraints.txt" ] +cpp_standard = "c++17" diff --git a/config/ameba/chip.cmake b/config/ameba/chip.cmake index 92a138d0536f7d..9d39d92c72698f 100644 --- a/config/ameba/chip.cmake +++ b/config/ameba/chip.cmake @@ -41,6 +41,8 @@ list( -Wno-deprecated-declarations -Wno-unused-parameter -Wno-format + -Wno-stringop-truncation + -std=c++17 ) list( @@ -98,6 +100,15 @@ string(APPEND CHIP_GN_ARGS "ameba_cc = \"arm-none-eabi-gcc\"\n") string(APPEND CHIP_GN_ARGS "ameba_cxx = \"arm-none-eabi-c++\"\n") string(APPEND CHIP_GN_ARGS "ameba_cpu = \"ameba\"\n") +# Build RPC +#string(APPEND CHIP_GN_ARGS "remove_default_configs = [\"//third_party/connectedhomeip/third_party/pigweed/repo/pw_build:cpp17\"]\n") +string(APPEND CHIP_GN_ARGS "chip_build_pw_rpc_lib = true\n") +string(APPEND CHIP_GN_ARGS "pw_log_BACKEND = \"//third_party/connectedhomeip/third_party/pigweed/repo/pw_log_basic\"\n") +string(APPEND CHIP_GN_ARGS "pw_assert_BACKEND = \"//third_party/connectedhomeip/third_party/pigweed/repo/pw_assert_log\"\n") +string(APPEND CHIP_GN_ARGS "pw_sys_io_BACKEND = \"//third_party/connectedhomeip/examples/platform/ameba/pw_sys_io:pw_sys_io_ameba\"\n") +string(APPEND CHIP_GN_ARGS "dir_pw_third_party_nanopb = \"//third_party/connectedhomeip/third_party/nanopb/repo\"\n") +string(APPEND CHIP_GN_ARGS "pw_build_LINK_DEPS = [\"//third_party/connectedhomeip/third_party/pigweed/repo/pw_assert:impl\", \"//third_party/connectedhomeip/third_party/pigweed/repo/pw_log:impl\"]\n") + file(GENERATE OUTPUT ${CHIP_OUTPUT}/args.gn CONTENT ${CHIP_GN_ARGS}) ExternalProject_Add( @@ -106,9 +117,9 @@ ExternalProject_Add( SOURCE_DIR ${CHIP_ROOT} BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} CONFIGURE_COMMAND gn --root=${CHIP_ROOT}/config/ameba gen --check --fail-on-unused-args ${CHIP_OUTPUT} - BUILD_COMMAND ninja -C ${CHIP_OUTPUT} + BUILD_COMMAND ninja -C ${CHIP_OUTPUT} :ameba INSTALL_COMMAND "" - BUILD_BYPRODUCTS -lCHIP + BUILD_BYPRODUCTS -lCHIP -lPwRpc CONFIGURE_ALWAYS TRUE BUILD_ALWAYS TRUE USES_TERMINAL_CONFIGURE TRUE diff --git a/config/ameba/lib/pw_rpc/BUILD.gn b/config/ameba/lib/pw_rpc/BUILD.gn new file mode 100644 index 00000000000000..4b47ab1d61a16a --- /dev/null +++ b/config/ameba/lib/pw_rpc/BUILD.gn @@ -0,0 +1,38 @@ +# 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. + +import("//build_overrides/chip.gni") +import("//build_overrides/pigweed.gni") +import("$dir_pw_build/target_types.gni") + +static_library("pw_rpc") { + output_name = "libPwRpc" + + public_configs = [ "${dir_pigweed}/pw_hdlc:default_config" ] + deps = [ + "$dir_pw_rpc:server", + "$dir_pw_rpc/nanopb:echo_service", + "${chip_root}/examples/platform/ameba/pw_sys_io:pw_sys_io_ameba", + "${dir_pigweed}/pw_hdlc:pw_rpc", + dir_pw_assert, + dir_pw_hdlc, + dir_pw_log, + ] + + deps += pw_build_LINK_DEPS + + output_dir = "${root_out_dir}/lib" + + complete_static_lib = true +} diff --git a/examples/all-clusters-app/ameba/README.md b/examples/all-clusters-app/ameba/README.md index 69dfd29aa11c4c..ad0f95ecffd8de 100644 --- a/examples/all-clusters-app/ameba/README.md +++ b/examples/all-clusters-app/ameba/README.md @@ -13,6 +13,7 @@ control. - [BLE mode](#ble-mode) - [IP mode](#ip-mode) - [Cluster control](#cluster-control) + - [Running RPC Console](#running-rpc-console) --- @@ -113,3 +114,31 @@ to be On or Off. [Python Controller](https://github.com/project-chip/connectedhomeip/blob/master/docs/guides/python_chip_controller_building.md#step-8-control-application-zcl-clusters) $ chip-device-ctrl > zcl OnOff Toggle 1234 1 0 + +## Running RPC Console + +- Connect a USB-TTL Adapter as shown below + + Ameba USB-TTL + A19 TX + A18 RX + GND GND + +- Build the + [chip-rpc console](https://github.com/project-chip/connectedhomeip/tree/master/examples/common/pigweed/rpc_console) + +- As part of building the example with RPCs enabled the chip_rpc python + interactive console is installed into your venv. The python wheel files are + also created in the output folder: out/debug/chip_rpc_console_wheels. To + install the wheel files without rebuilding: + + $ pip3 install out/debug/chip_rpc_console_wheels/*.whl + +- Launch the chip-rpc console after inputting `ATS$` command + + $ python3 -m chip_rpc.console --device /dev/tty -b 115200 + +- Get and Set lighting directly using the RPC console + + rpcs.chip.rpc.Lighting.Get() + rpcs.chip.rpc.Lighting.Set(on=True, level=128, color=protos.chip.rpc.LightingColor(hue=5, saturation=5)) diff --git a/examples/all-clusters-app/ameba/chip_main.cmake b/examples/all-clusters-app/ameba/chip_main.cmake index 6f83d394ad9f39..9cd9ce4725913b 100755 --- a/examples/all-clusters-app/ameba/chip_main.cmake +++ b/examples/all-clusters-app/ameba/chip_main.cmake @@ -8,8 +8,110 @@ set(dir "${sdk_root}/component/common/api") set(chip_main chip_main) set(list_chip_main_sources chip_main_sources) +if (matter_enable_rpc) +set(pigweed_dir "${chip_dir}/third_party/pigweed/repo") + +include(${pigweed_dir}/pw_build/pigweed.cmake) +include(${pigweed_dir}/pw_protobuf_compiler/proto.cmake) + +set(dir_pw_third_party_nanopb "${chip_dir}/third_party/nanopb/repo" CACHE STRING "" FORCE) + +pw_set_backend(pw_log pw_log_basic) +pw_set_backend(pw_assert pw_assert_log) +pw_set_backend(pw_sys_io pw_sys_io.ameba) +pw_set_backend(pw_trace pw_trace_tokenized) + +add_subdirectory(${chip_dir}/third_party/pigweed/repo ${chip_dir}/examples/all-clusters-app/ameba/out/pigweed) +add_subdirectory(${chip_dir}/third_party/nanopb/repo ${chip_dir}/examples/all-clusters-app/ameba/out/nanopb) +add_subdirectory(${chip_dir}/examples/platform/ameba/pw_sys_io ${chip_dir}/examples/all-clusters-app/ameba/out/pw_sys_io) + +pw_proto_library(attributes_service + SOURCES + ${chip_dir}/examples/common/pigweed/protos/attributes_service.proto + INPUTS + ${chip_dir}/examples/common/pigweed/protos/attributes_service.options + PREFIX + attributes_service + STRIP_PREFIX + ${chip_dir}/examples/common/pigweed/protos + DEPS + pw_protobuf.common_protos +) + +pw_proto_library(button_service + SOURCES + ${chip_dir}/examples/common/pigweed/protos/button_service.proto + PREFIX + button_service + STRIP_PREFIX + ${chip_dir}/examples/common/pigweed/protos + DEPS + pw_protobuf.common_protos +) + +pw_proto_library(device_service + SOURCES + ${chip_dir}/examples/common/pigweed/protos/device_service.proto + INPUTS + ${chip_dir}/examples/common/pigweed/protos/device_service.options + PREFIX + device_service + STRIP_PREFIX + ${chip_dir}/examples/common/pigweed/protos + DEPS + pw_protobuf.common_protos +) + +pw_proto_library(lighting_service + SOURCES + ${chip_dir}/examples/common/pigweed/protos/lighting_service.proto + PREFIX + lighting_service + STRIP_PREFIX + ${chip_dir}/examples/common/pigweed/protos + DEPS + pw_protobuf.common_protos +) + +pw_proto_library(locking_service + SOURCES + ${chip_dir}/examples/common/pigweed/protos/locking_service.proto + PREFIX + locking_service + STRIP_PREFIX + ${chip_dir}/examples/common/pigweed/protos + DEPS + pw_protobuf.common_protos +) + +pw_proto_library(wifi_service + SOURCES + ${chip_dir}/examples/ipv6only-app/common/wifi_service/wifi_service.proto + INPUTS + ${chip_dir}/examples/ipv6only-app/common/wifi_service/wifi_service.options + PREFIX + wifi_service + DEPS + pw_protobuf.common_protos + STRIP_PREFIX + ${chip_dir}/examples/ipv6only-app/common/wifi_service +) + +endif(matter_enable_rpc) + include(${prj_root}/GCC-RELEASE/project_hp/asdk/includepath.cmake) +if (matter_enable_rpc) +list( + APPEND ${list_chip_main_sources} + #rpc + ${chip_dir}/examples/platform/ameba/PigweedLogger.cpp + ${chip_dir}/examples/platform/ameba/Rpc.cpp + ${chip_dir}/examples/common/pigweed/RpcService.cpp + ${chip_dir}/examples/common/pigweed/ameba/PigweedLoggerMutex.cpp +) +endif (matter_enable_rpc) + list( APPEND ${list_chip_main_sources} @@ -40,10 +142,26 @@ chip_configure_data_model(chip_main ZAP_FILE ${matter_example_path}/../all-clusters-common/all-clusters-app.zap ) +if (matter_enable_rpc) target_include_directories( ${chip_main} PUBLIC - ${inc_path} + #rpc + ${chip_dir}/examples/platform/ameba + ${chip_dir}/examples/platform/ameba/pw_sys_io/public + ${chip_dir}/examples/common + ${chip_dir}/examples/common/pigweed + ${chip_dir}/examples/common/pigweed/ameba + ${chip_dir}/src + ${chip_dir}/src/lib/support + ${pigweed_dir}/pw_rpc/nanopb/public +) +endif (matter_enable_rpc) + +target_include_directories( + ${chip_main} + PUBLIC + ${inc_path} ${chip_dir}/zzz_generated/all-clusters-app ${chip_dir}/zzz_generated/all-clusters-app/zap-generated ${chip_dir}/zzz_generated/app-common @@ -63,6 +181,30 @@ target_include_directories( ${chip_dir}/third_party/nlunit-test/repo/src ) +if (matter_enable_rpc) +target_link_libraries(${chip_main} PUBLIC + attributes_service.nanopb_rpc + button_service.nanopb_rpc + device_service.nanopb_rpc + lighting_service.nanopb_rpc + locking_service.nanopb_rpc + wifi_service.nanopb_rpc + pw_checksum + pw_hdlc + pw_log + pw_rpc.server + pw_trace_tokenized + pw_trace_tokenized.trace_buffer + pw_trace_tokenized.rpc_service + pw_trace_tokenized.protos.nanopb_rpc + PwRpc +) + +link_directories( + ${chip_dir_output}/lib +) +endif (matter_enable_rpc) + list( APPEND chip_main_flags @@ -74,6 +216,19 @@ list( -DMBEDTLS_CONFIG_FILE= ) +if (matter_enable_rpc) +list( + APPEND chip_main_flags + + -DPW_RPC_ATTRIBUTE_SERVICE=1 + -DPW_RPC_BUTTON_SERVICE=1 + -DPW_RPC_DEVICE_SERVICE=1 + -DPW_RPC_LIGHTING_SERVICE=1 + -DPW_RPC_LOCKING_SERVICE=1 + -DCONFIG_ENABLE_PW_RPC=1 +) +endif (matter_enable_rpc) + list( APPEND chip_main_cpp_flags @@ -91,3 +246,4 @@ add_custom_command( POST_BUILD COMMAND cp lib${chip_main}.a ${CMAKE_CURRENT_SOURCE_DIR}/lib/application ) + diff --git a/examples/all-clusters-app/ameba/main/chipinterface.cpp b/examples/all-clusters-app/ameba/main/chipinterface.cpp index e49d28aaca6c24..c2f2fcec38388a 100644 --- a/examples/all-clusters-app/ameba/main/chipinterface.cpp +++ b/examples/all-clusters-app/ameba/main/chipinterface.cpp @@ -36,6 +36,10 @@ #include #include +#if CONFIG_ENABLE_PW_RPC +#include "Rpc.h" +#endif + extern "C" { void * __dso_handle = 0; } @@ -190,6 +194,10 @@ extern "C" void ChipTest(void) ChipLogProgress(DeviceLayer, "All Clusters Demo!"); CHIP_ERROR err = CHIP_NO_ERROR; +#if CONFIG_ENABLE_PW_RPC + chip::rpc::Init(); +#endif + initPref(); CHIPDeviceManager & deviceMgr = CHIPDeviceManager::GetInstance(); diff --git a/examples/common/pigweed/ameba/PigweedLoggerMutex.cpp b/examples/common/pigweed/ameba/PigweedLoggerMutex.cpp new file mode 100644 index 00000000000000..5061d53e768a6d --- /dev/null +++ b/examples/common/pigweed/ameba/PigweedLoggerMutex.cpp @@ -0,0 +1,27 @@ +/* + * + * 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 "PigweedLoggerMutex.h" + +namespace chip { +namespace rpc { + +PigweedLoggerMutex logger_mutex; + +} // namespace rpc +} // namespace chip diff --git a/examples/common/pigweed/ameba/PigweedLoggerMutex.h b/examples/common/pigweed/ameba/PigweedLoggerMutex.h new file mode 100644 index 00000000000000..ad0df28f7879eb --- /dev/null +++ b/examples/common/pigweed/ameba/PigweedLoggerMutex.h @@ -0,0 +1,52 @@ +/* + * + * 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 "FreeRTOS.h" +#include "PigweedLogger.h" +#include "RpcService.h" +#include "semphr.h" + +namespace chip { +namespace rpc { +class PigweedLoggerMutex : public chip::rpc::Mutex +{ + +public: + void Lock() override + { + SemaphoreHandle_t * sem = PigweedLogger::getSemaphore(); + if (sem) + { + xSemaphoreTake(*sem, portMAX_DELAY); + } + } + void Unlock() override + { + SemaphoreHandle_t * sem = PigweedLogger::getSemaphore(); + if (sem) + { + xSemaphoreGive(*sem); + } + } +}; + +extern PigweedLoggerMutex logger_mutex; + +} // namespace rpc +} // namespace chip diff --git a/examples/pigweed-app/ameba/README.md b/examples/pigweed-app/ameba/README.md new file mode 100644 index 00000000000000..e84d5b50c5eaa3 --- /dev/null +++ b/examples/pigweed-app/ameba/README.md @@ -0,0 +1,83 @@ +# CHIP Ameba Pigweed Example Application + +This example demonstrates the usage of Pigweed module functionalities in an +application. + +The example is based on [CHIP](https://github.com/project-chip/connectedhomeip), +the [Pigweed](https://pigweed.googlesource.com/pigweed/pigweed) module, which is +a collection of libraries that provide different functionalities for embedded +systems, and the Ameba platform. + +You can use this example as a training ground for making experiments, testing +Pigweed module features and checking what actions are necessary to fully +integrate Pigweed in a CHIP project. + +Pigweed functionalities are being gradually integrated into CHIP. Currently, the +following features are available: + +- **Echo RPC** - Creates a Remote Procedure Call server and allows sending + commands through the serial port to the device, which makes echo and sends + the received commands back. + +--- + +- [CHIP Ameba Pigweed Example Application](#chip-ameba-pigweed-example-application) + - [Building the Example Application](#building-the-example-application) + - [Testing the Example Application](#testing-the-example-application) + +--- + +## Building the Example Application + +- Pull docker image: + + $ docker pull pankore/chip-build-ameba:latest + +- Run docker container: + + $ docker run -it -v ${CHIP_DIR}:/root/chip pankore/chip-build-ameba:latest + +- Setup build environment: + + $ source ./scripts/bootstrap.sh + +- To build the demo application: + + $ ./scripts/build/build_examples.py --target ameba-amebad-pigweed build + + The output image files are stored in `out/ameba-amebad-pigweed/asdk/image` + folder. + + The bootloader image files are stored in + `out/ameba-amebad-pigweed/asdk/bootloader` folder. + +- After building the application, **Ameba Image Tool** is used to flash it to + Ameba board. + +1. Connect your device via USB and open Ameba Image Tool. +2. Select correct serial port and set baudrate as **115200**. +3. Browse and add the corresponding image files in the Flash Download list to + the correct locations +4. Click **Download** button. + +## Testing the Example Application + +Connect a USB-TTL Adapter as shown below + + Ameba USB-TTL + A19 TX + A18 RX + GND GND + +Build the +[chip-rpc console](https://github.com/project-chip/connectedhomeip/tree/master/examples/common/pigweed/rpc_console) + +Input `ATS$` command, then launch the chip-rpc console, where the Echo RPC +commands can be invoked + + python -m pw_hdlc.rpc_console --device /dev/tty -b 115200 + +To send an Echo RPC message, type the following command, where the actual +message is the text in quotation marks after the `msg=` phrase: + + rpcs.pw.rpc.EchoService.Echo(msg="hi") diff --git a/examples/pigweed-app/ameba/chip_main.cmake b/examples/pigweed-app/ameba/chip_main.cmake new file mode 100644 index 00000000000000..95af00c585f1de --- /dev/null +++ b/examples/pigweed-app/ameba/chip_main.cmake @@ -0,0 +1,101 @@ +cmake_minimum_required(VERSION 3.6) + +project(chip_main) + +set(chip_dir "${ameba_matter_root}") +set(chip_dir_output "${matter_output_path}/chip") +set(dir "${sdk_root}/component/common/api") +set(chip_main chip_main) +set(list_chip_main_sources chip_main_sources) +set(pigweed_dir "${chip_dir}/third_party/pigweed/repo") + +include(${prj_root}/GCC-RELEASE/project_hp/asdk/includepath.cmake) + +include(${pigweed_dir}/pw_build/pigweed.cmake) +include(${pigweed_dir}/pw_protobuf_compiler/proto.cmake) + +set(dir_pw_third_party_nanopb "${chip_dir}/third_party/nanopb/repo" CACHE STRING "" FORCE) + +pw_set_backend(pw_log pw_log_basic) +pw_set_backend(pw_assert pw_assert_log) +pw_set_backend(pw_sys_io pw_sys_io.ameba) + +add_subdirectory(${chip_dir}/third_party/pigweed/repo ${chip_dir}/examples/pigweed-app/ameba/out/pigweed) +add_subdirectory(${chip_dir}/third_party/nanopb/repo ${chip_dir}/examples/pigweed-app/ameba/out/nanopb) +add_subdirectory(${chip_dir}/examples/platform/ameba/pw_sys_io ${chip_dir}/examples/pigweed-app/ameba/out/pw_sys_io) + +list( + APPEND ${list_chip_main_sources} + + #chip app + ${chip_dir}/examples/pigweed-app/ameba/main/main.cpp + ${chip_dir}/examples/platform/ameba/PigweedLogger.cpp + ${chip_dir}/examples/common/pigweed/RpcService.cpp + ${chip_dir}/examples/common/pigweed/ameba/PigweedLoggerMutex.cpp +) + +add_library( + ${chip_main} + STATIC + ${chip_main_sources} +) + +target_include_directories( + ${chip_main} + PUBLIC + ${inc_path} + ${chip_dir}/examples/platform/ameba + ${chip_dir}/examples/platform/ameba/pw_sys_io/public + ${chip_dir}/examples/common + ${chip_dir}/examples/common/pigweed + ${chip_dir}/examples/common/pigweed/ameba + ${chip_dir}/src + ${chip_dir}/src/lib/support + ${pigweed_dir}/pw_rpc/nanopb/public + + ${chip_dir_output}/gen/include +) + +target_link_libraries(${chip_main} PUBLIC + pw_checksum + pw_hdlc + pw_log + pw_rpc.nanopb.echo_service + pw_rpc.server + PwRpc +) + +link_directories( + ${chip_dir_output}/lib +) + +list( + APPEND chip_main_flags + + -DINET_CONFIG_ENABLE_IPV4=1 + -DCHIP_PROJECT=1 + -DCHIP_DEVICE_LAYER_TARGET=Ameba + -DUSE_ZAP_CONFIG + -DCHIP_HAVE_CONFIG_H + -DMBEDTLS_CONFIG_FILE= + -DCONFIG_ENABLE_PW_RPC=1 +) + +list( + APPEND chip_main_cpp_flags + + -Wno-unused-parameter + -std=gnu++17 + -fno-rtti + -fno-use-cxa-atexit +) +target_compile_definitions(${chip_main} PRIVATE ${chip_main_flags} ) +target_compile_options(${chip_main} PRIVATE ${chip_main_cpp_flags}) + +# move static library post build command +add_custom_command( + TARGET ${chip_main} + POST_BUILD + COMMAND cp lib${chip_main}.a ${CMAKE_CURRENT_SOURCE_DIR}/lib/application +) + diff --git a/examples/pigweed-app/ameba/main/main.cpp b/examples/pigweed-app/ameba/main/main.cpp new file mode 100644 index 00000000000000..7a215e32a790a3 --- /dev/null +++ b/examples/pigweed-app/ameba/main/main.cpp @@ -0,0 +1,57 @@ +/* + * + * 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. + */ + +#include "pw_rpc/echo_service_nanopb.h" +#include "pw_sys_io/sys_io.h" +#include "pw_sys_io_ameba/init.h" + +#include "PigweedLoggerMutex.h" +#include "pigweed/RpcService.h" +#include +#include + +namespace { +using std::byte; + +#define RPC_TASK_STACK_SIZE 4096 +#define RPC_TASK_PRIORITY 2 +static TaskHandle_t sRpcTaskHandle; + +pw::rpc::EchoService echo_service; + +void RegisterServices(pw::rpc::Server & server) +{ + server.RegisterService(echo_service); +} + +void RunRpcService(void *) +{ + Start(RegisterServices, &::chip::rpc::logger_mutex); +} + +} // namespace + +extern "C" void ChipTest(void) +{ + PigweedLogger::init(); + + printf("***** CHIP ameba pigweed example *****\r\n"); + + // pw_sys_io_Init(); + + xTaskCreate(RunRpcService, "RPC_Task", RPC_TASK_STACK_SIZE / sizeof(StackType_t), nullptr, RPC_TASK_PRIORITY, &sRpcTaskHandle); +} diff --git a/examples/pigweed-app/ameba/third_party/connectedhomeip b/examples/pigweed-app/ameba/third_party/connectedhomeip new file mode 100644 index 00000000000000..9ecdeae07e93e3 --- /dev/null +++ b/examples/pigweed-app/ameba/third_party/connectedhomeip @@ -0,0 +1 @@ +../../../.. diff --git a/examples/platform/ameba/PigweedLogger.cpp b/examples/platform/ameba/PigweedLogger.cpp new file mode 100644 index 00000000000000..625c6c4a901586 --- /dev/null +++ b/examples/platform/ameba/PigweedLogger.cpp @@ -0,0 +1,91 @@ +/* + * + * 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. + */ + +#include "FreeRTOS.h" +#include "pw_sys_io_ameba/init.h" +#include "semphr.h" +#include +#include +#include + +#include +#include + +namespace PigweedLogger { +namespace { + +constexpr uint8_t kLogHdlcAddress = 1; // Send log messages to HDLC address 1 (other than RPC communication) +constexpr size_t kWriteBufferSize = 128; // Buffer for constructing HDLC frames + +SemaphoreHandle_t ameba_log_mutex; + +pw::stream::SysIoWriter sWriter; +size_t sWriteBufferPos; +char sWriteBuffer[kWriteBufferSize]; + +bool uartInitialised; + +void send() +{ + pw::hdlc::WriteUIFrame(kLogHdlcAddress, std::as_bytes(std::span(sWriteBuffer, sWriteBufferPos)), sWriter); + sWriteBufferPos = 0; +} + +} // namespace + +void init() +{ + ameba_log_mutex = xSemaphoreCreateMutex(); + assert(ameba_log_mutex != NULL); + pw_sys_io_Init(); + uartInitialised = true; +} + +int putString(const char * buffer, size_t size) +{ + xSemaphoreTake(ameba_log_mutex, portMAX_DELAY); + + assert(sWriteBufferPos < kWriteBufferSize); + + for (size_t i = 0; i < size; ++i) + { + + if (buffer[i] == '\r') + continue; + + if (buffer[i] == '\n') + { + send(); + continue; + } + + sWriteBuffer[sWriteBufferPos++] = buffer[i]; + + if (sWriteBufferPos == kWriteBufferSize) + send(); + } + + xSemaphoreGive(ameba_log_mutex); + return size; +} + +SemaphoreHandle_t * getSemaphore() +{ + return &ameba_log_mutex; +} + +} // namespace PigweedLogger diff --git a/examples/platform/ameba/PigweedLogger.h b/examples/platform/ameba/PigweedLogger.h new file mode 100644 index 00000000000000..f666fdcf62606e --- /dev/null +++ b/examples/platform/ameba/PigweedLogger.h @@ -0,0 +1,29 @@ +/* + * + * 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 "FreeRTOS.h" +#include "semphr.h" + +namespace PigweedLogger { + +void init(); +int putString(const char * buffer, size_t size); +SemaphoreHandle_t * getSemaphore(); + +} // namespace PigweedLogger diff --git a/examples/platform/ameba/Rpc.cpp b/examples/platform/ameba/Rpc.cpp new file mode 100644 index 00000000000000..c9caba3f504eb0 --- /dev/null +++ b/examples/platform/ameba/Rpc.cpp @@ -0,0 +1,161 @@ +/* + * + * 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. + */ + +#if CONFIG_ENABLE_PW_RPC +#include "FreeRTOS.h" +#include "PigweedLoggerMutex.h" +#include "RpcService.h" +#include "pw_sys_io_ameba/init.h" +#include "sys_api.h" +#include "task.h" +//#include "esp_log.h" +//#include "freertos/FreeRTOS.h" +//#include "freertos/event_groups.h" +//#include "freertos/semphr.h" +//#include "freertos/task.h" +#include "pw_log/log.h" +#include "pw_rpc/server.h" +#include "pw_sys_io/sys_io.h" +#include "support/CodeUtils.h" + +#if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE +#include "pigweed/rpc_services/Attributes.h" +#endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE + +#if defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE +//#include "ScreenManager.h" +#include "pigweed/rpc_services/Button.h" +#endif // defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE + +#if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE +#include "pigweed/rpc_services/Device.h" +#endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE + +#if defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE +#include "pigweed/rpc_services/Lighting.h" +#endif // defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE + +#if defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE +#include "pigweed/rpc_services/Locking.h" +#endif // defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE + +namespace chip { +namespace rpc { + +#if defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE +class AmebaButton final : public Button +{ +public: + pw::Status Event(const chip_rpc_ButtonEvent & request, pw_protobuf_Empty & response) override + { + return pw::Status::Unimplemented(); + } +}; +#endif // defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE + +#if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE +class AmebaDevice final : public Device +{ +public: + pw::Status Reboot(const pw_protobuf_Empty & request, pw_protobuf_Empty & response) override + { + mRebootTimer = xTimerCreateStatic("Reboot", kRebootTimerPeriodTicks, false, nullptr, RebootHandler, &mRebootTimerBuffer); + xTimerStart(mRebootTimer, 0); + return pw::OkStatus(); + } + +private: + static constexpr TickType_t kRebootTimerPeriodTicks = 1000; + TimerHandle_t mRebootTimer; + StaticTimer_t mRebootTimerBuffer; + + static void RebootHandler(TimerHandle_t) { sys_reset(); } +}; +#endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE + +namespace { + +#define RPC_TASK_STACK_SIZE (8 * 1024) +#define RPC_TASK_PRIORITY 1 +static TaskHandle_t sRpcTaskHandle; +StaticTask_t sRpcTaskBuffer; +StackType_t sRpcTaskStack[RPC_TASK_STACK_SIZE]; + +#if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE +Attributes attributes_service; +#endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE + +#if defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE +AmebaButton button_service; +#endif // defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE + +#if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE +AmebaDevice device_service; +#endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE + +#if defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE +Lighting lighting_service; +#endif // defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE + +#if defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE +Locking locking; +#endif // defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE + +void RegisterServices(pw::rpc::Server & server) +{ +#if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE + server.RegisterService(attributes_service); +#endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE + +#if defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE + server.RegisterService(button_service); +#endif // defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE + +#if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE + server.RegisterService(device_service); +#endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE + +#if defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE + server.RegisterService(lighting_service); +#endif // defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE + +#if defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE + server.RegisterService(locking); +#endif // defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE +} + +} // namespace + +void RunRpcService(void *) +{ + Start(RegisterServices, &logger_mutex); +} + +void Init() +{ + PigweedLogger::init(); + + // Start App task. + sRpcTaskHandle = xTaskCreateStatic(RunRpcService, "RPC_TASK", ArraySize(sRpcTaskStack), nullptr, RPC_TASK_PRIORITY, + sRpcTaskStack, &sRpcTaskBuffer); +} + +} // namespace rpc +} // namespace chip + +#endif // CONFIG_ENABLE_PW_RPCC diff --git a/examples/platform/ameba/Rpc.h b/examples/platform/ameba/Rpc.h new file mode 100644 index 00000000000000..9b73e5cafca96b --- /dev/null +++ b/examples/platform/ameba/Rpc.h @@ -0,0 +1,26 @@ +/* + * + * 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 + +namespace chip { +namespace rpc { + +void Init(); + +} // namespace rpc +} // namespace chip diff --git a/examples/platform/ameba/pw_sys_io/BUILD.gn b/examples/platform/ameba/pw_sys_io/BUILD.gn new file mode 100644 index 00000000000000..82b6452095ea52 --- /dev/null +++ b/examples/platform/ameba/pw_sys_io/BUILD.gn @@ -0,0 +1,34 @@ +# 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. + +import("//build_overrides/pigweed.gni") + +import("$dir_pw_build/target_types.gni") + +config("default_config") { + include_dirs = [ "public" ] +} + +pw_source_set("pw_sys_io_ameba") { + sources = [ "sys_io_ameba.cc" ] + + deps = [ + "$dir_pw_sys_io:default_putget_bytes", + "$dir_pw_sys_io:facade", + ] + + cflags = [ "-Wconversion" ] + + public_configs = [ ":default_config" ] +} diff --git a/examples/platform/ameba/pw_sys_io/CMakeLists.txt b/examples/platform/ameba/pw_sys_io/CMakeLists.txt new file mode 100644 index 00000000000000..c0ff32a9f695cb --- /dev/null +++ b/examples/platform/ameba/pw_sys_io/CMakeLists.txt @@ -0,0 +1,11 @@ +include($ENV{PW_ROOT}/pw_build/pigweed.cmake) + +# This is compiled in gn build, just provide headers and let linker sort it out. +pw_add_module_library(pw_sys_io.ameba + SOURCES + PRIVATE_DEPS + pw_sys_io + HEADERS + public +) + diff --git a/examples/platform/ameba/pw_sys_io/public/pw_sys_io_ameba/init.h b/examples/platform/ameba/pw_sys_io/public/pw_sys_io_ameba/init.h new file mode 100644 index 00000000000000..f11f5e928f3e14 --- /dev/null +++ b/examples/platform/ameba/pw_sys_io/public/pw_sys_io_ameba/init.h @@ -0,0 +1,27 @@ +/* + * + * 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 "pw_preprocessor/util.h" + +PW_EXTERN_C_START + +// The actual implement of PreMainInit() in sys_io_BACKEND. +void pw_sys_io_Init(); + +PW_EXTERN_C_END diff --git a/examples/platform/ameba/pw_sys_io/sys_io_ameba.cc b/examples/platform/ameba/pw_sys_io/sys_io_ameba.cc new file mode 100644 index 00000000000000..a57880bc4780f9 --- /dev/null +++ b/examples/platform/ameba/pw_sys_io/sys_io_ameba.cc @@ -0,0 +1,87 @@ +/* + * + * 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. + */ + +#include "pw_sys_io/sys_io.h" +#include +#include +#include +#include + +#include "serial_api.h" + +#define UART_TX PA_18 +#define UART_RX PA_19 + +serial_t sobj; +int console_getchar(uint8_t * chr) +{ + *chr = (uint8_t) serial_getc(&sobj); + return 1; +} + +int console_putchar(const char * chr) +{ + serial_putc(&sobj, *chr); + return 1; +} + +void console_init(void) +{ + serial_init(&sobj, UART_TX, UART_RX); + serial_baud(&sobj, 115200); + serial_format(&sobj, 8, ParityNone, 1); +} + +extern "C" void pw_sys_io_Init() +{ + console_init(); +} + +namespace pw::sys_io { + +Status ReadByte(std::byte * dest) +{ + if (!dest) + return Status::InvalidArgument(); + + int ret = console_getchar(reinterpret_cast(dest)); + return ret < 0 ? Status::FailedPrecondition() : OkStatus(); +} + +Status WriteByte(std::byte b) +{ + int ret = console_putchar(reinterpret_cast(&b)); + return ret < 0 ? Status::FailedPrecondition() : OkStatus(); +} + +// Writes a string using pw::sys_io, and add newline characters at the end. +StatusWithSize WriteLine(const std::string_view & s) +{ + size_t chars_written = 0; + StatusWithSize result = WriteBytes(std::as_bytes(std::span(s))); + if (!result.ok()) + { + return result; + } + chars_written += result.size(); + result = WriteBytes(std::as_bytes(std::span("\r\n", 2))); + chars_written += result.size(); + + return StatusWithSize(result.status(), chars_written); +} + +} // namespace pw::sys_io diff --git a/scripts/build/build/targets.py b/scripts/build/build/targets.py index 043fad9c5b1691..626e4b92b74514 100644 --- a/scripts/build/build/targets.py +++ b/scripts/build/build/targets.py @@ -336,6 +336,7 @@ def AmebaTargets(): yield ameba_target.Extend('amebad-all-clusters', board=AmebaBoard.AMEBAD, app=AmebaApp.ALL_CLUSTERS) yield ameba_target.Extend('amebad-light', board=AmebaBoard.AMEBAD, app=AmebaApp.LIGHT) + yield ameba_target.Extend('amebad-pigweed', board=AmebaBoard.AMEBAD, app=AmebaApp.PIGWEED) def K32WTargets(): diff --git a/scripts/build/builders/ameba.py b/scripts/build/builders/ameba.py index 7f3df30edf14a1..18b394d242e2f9 100644 --- a/scripts/build/builders/ameba.py +++ b/scripts/build/builders/ameba.py @@ -26,6 +26,7 @@ class AmebaBoard(Enum): class AmebaApp(Enum): ALL_CLUSTERS = auto() LIGHT = auto() + PIGWEED = auto() @property def ExampleName(self): @@ -33,6 +34,8 @@ def ExampleName(self): return 'all-clusters-app' elif self == AmebaApp.LIGHT: return 'lighting-app' + elif self == AmebaApp.PIGWEED: + return 'pigweed-app' else: raise Exception('Unknown app type: %r' % self) @@ -42,6 +45,8 @@ def AppNamePrefix(self): return 'chip-ameba-all-clusters-app' elif self == AmebaApp.LIGHT: return 'chip-ameba-lighting-app' + elif self == AmebaApp.PIGWEED: + return 'chip-ameba-pigweed-app' else: raise Exception('Unknown app type: %r' % self) diff --git a/scripts/build/testdata/all_targets_except_host.txt b/scripts/build/testdata/all_targets_except_host.txt index eb986fae869f64..49e67f381a020d 100644 --- a/scripts/build/testdata/all_targets_except_host.txt +++ b/scripts/build/testdata/all_targets_except_host.txt @@ -1,5 +1,6 @@ ameba-amebad-all-clusters ameba-amebad-light +ameba-amebad-pigweed android-androidstudio-arm-chip-tool android-androidstudio-arm64-chip-tool android-androidstudio-x64-chip-tool diff --git a/scripts/build/testdata/build_all_except_host.txt b/scripts/build/testdata/build_all_except_host.txt index e1df7360aa8336..349750e4921865 100644 --- a/scripts/build/testdata/build_all_except_host.txt +++ b/scripts/build/testdata/build_all_except_host.txt @@ -7,6 +7,9 @@ bash -c '$AMEBA_PATH/project/realtek_amebaD_va0_example/GCC-RELEASE/build.sh {ro # Generating ameba-amebad-light bash -c '$AMEBA_PATH/project/realtek_amebaD_va0_example/GCC-RELEASE/build.sh {root} ninja {out}/ameba-amebad-light lighting-app' +# Generating ameba-amebad-pigweed +bash -c '$AMEBA_PATH/project/realtek_amebaD_va0_example/GCC-RELEASE/build.sh {root} ninja {out}/ameba-amebad-pigweed pigweed-app' + # Generating JARs for Java build rules test python3 build/chip/java/tests/generate_jars_for_test.py @@ -629,6 +632,9 @@ ninja -C {out}/ameba-amebad-all-clusters # Building ameba-amebad-light ninja -C {out}/ameba-amebad-light +# Building ameba-amebad-pigweed +ninja -C {out}/ameba-amebad-pigweed + # Building APP android-androidstudio-arm-chip-tool {root}/src/android/CHIPTool/gradlew -p {root}/src/android/CHIPTool -PmatterBuildSrcDir={out}/android-androidstudio-arm-chip-tool -PmatterSdkSourceBuild=true -PmatterSourceBuildAbiFilters=armeabi-v7a assembleDebug diff --git a/scripts/build/testdata/glob_star_targets_except_host.txt b/scripts/build/testdata/glob_star_targets_except_host.txt index b478d6c827af0e..e95ba534e53536 100644 --- a/scripts/build/testdata/glob_star_targets_except_host.txt +++ b/scripts/build/testdata/glob_star_targets_except_host.txt @@ -1,5 +1,6 @@ ameba-amebad-all-clusters ameba-amebad-light +ameba-amebad-pigweed android-androidstudio-arm-chip-tool android-androidstudio-arm64-chip-tool android-androidstudio-x64-chip-tool