From 3ffd066e0e438e397bc859b47cb2c75b38c0e3f2 Mon Sep 17 00:00:00 2001 From: Jandy Gou Date: Tue, 14 Jun 2022 22:02:05 +0800 Subject: [PATCH] make platform zephyr stand alone (#19504) --- build_overrides/zephyr.gni | 15 + config/zephyr/README.md | 10 + .../app/copy-flashbundle-firmware.cmake | 25 ++ config/zephyr/app/enable-gnu-std.cmake | 6 + config/zephyr/app/flashing.cmake | 55 ++++ config/zephyr/app/overlay-usb_support.conf | 24 ++ config/zephyr/chip-gn/.gn | 29 ++ config/zephyr/chip-gn/BUILD.gn | 40 +++ config/zephyr/chip-gn/args.gni | 27 ++ config/zephyr/chip-gn/toolchain/BUILD.gn | 33 +++ config/zephyr/chip-module/CMakeLists.txt | 257 ++++++++++++++++++ config/zephyr/chip-module/Kconfig | 17 ++ config/zephyr/chip-module/Kconfig.defaults | 225 +++++++++++++++ config/zephyr/chip-module/Kconfig.features | 68 +++++ .../chip-module/Kconfig.mcuboot.defaults | 89 ++++++ .../zephyr/chip-module/Kconfig.mcuboot.root | 21 ++ .../Kconfig.multiprotocol_rpmsg.defaults | 91 +++++++ .../Kconfig.multiprotocol_rpmsg.root | 21 ++ config/zephyr/chip-module/make_gn_args.py | 73 +++++ config/zephyr/chip-module/zephyr/module.yml | 22 ++ src/platform/BUILD.gn | 7 + src/platform/Zephyr/BUILD.gn | 79 ++++++ src/platform/Zephyr/BlePlatformConfig.h | 36 +++ .../Zephyr/CHIPDevicePlatformConfig.h | 132 +++++++++ src/platform/Zephyr/CHIPDevicePlatformEvent.h | 2 +- src/platform/Zephyr/CHIPPlatformConfig.h | 55 ++++ .../Zephyr/ConfigurationManagerImpl.h | 2 +- .../Zephyr/ConnectivityManagerImpl.cpp | 61 +++++ src/platform/Zephyr/ConnectivityManagerImpl.h | 103 +++++++ .../DeviceNetworkProvisioningDelegateImpl.cpp | 44 +++ .../DeviceNetworkProvisioningDelegateImpl.h | 43 +++ .../Zephyr/DiagnosticDataProviderImpl.cpp | 4 +- src/platform/Zephyr/InetPlatformConfig.h | 46 ++++ src/platform/Zephyr/SystemPlatformConfig.h | 58 ++++ src/platform/Zephyr/args.gni | 5 + src/platform/device.gni | 9 +- 36 files changed, 1828 insertions(+), 6 deletions(-) create mode 100644 build_overrides/zephyr.gni create mode 100644 config/zephyr/README.md create mode 100644 config/zephyr/app/copy-flashbundle-firmware.cmake create mode 100644 config/zephyr/app/enable-gnu-std.cmake create mode 100644 config/zephyr/app/flashing.cmake create mode 100644 config/zephyr/app/overlay-usb_support.conf create mode 100644 config/zephyr/chip-gn/.gn create mode 100644 config/zephyr/chip-gn/BUILD.gn create mode 100644 config/zephyr/chip-gn/args.gni create mode 100644 config/zephyr/chip-gn/toolchain/BUILD.gn create mode 100644 config/zephyr/chip-module/CMakeLists.txt create mode 100644 config/zephyr/chip-module/Kconfig create mode 100644 config/zephyr/chip-module/Kconfig.defaults create mode 100644 config/zephyr/chip-module/Kconfig.features create mode 100644 config/zephyr/chip-module/Kconfig.mcuboot.defaults create mode 100644 config/zephyr/chip-module/Kconfig.mcuboot.root create mode 100644 config/zephyr/chip-module/Kconfig.multiprotocol_rpmsg.defaults create mode 100644 config/zephyr/chip-module/Kconfig.multiprotocol_rpmsg.root create mode 100644 config/zephyr/chip-module/make_gn_args.py create mode 100644 config/zephyr/chip-module/zephyr/module.yml create mode 100644 src/platform/Zephyr/BUILD.gn create mode 100644 src/platform/Zephyr/BlePlatformConfig.h create mode 100644 src/platform/Zephyr/CHIPDevicePlatformConfig.h create mode 100644 src/platform/Zephyr/CHIPPlatformConfig.h create mode 100644 src/platform/Zephyr/ConnectivityManagerImpl.cpp create mode 100644 src/platform/Zephyr/ConnectivityManagerImpl.h create mode 100644 src/platform/Zephyr/DeviceNetworkProvisioningDelegateImpl.cpp create mode 100644 src/platform/Zephyr/DeviceNetworkProvisioningDelegateImpl.h create mode 100644 src/platform/Zephyr/InetPlatformConfig.h create mode 100644 src/platform/Zephyr/SystemPlatformConfig.h create mode 100644 src/platform/Zephyr/args.gni diff --git a/build_overrides/zephyr.gni b/build_overrides/zephyr.gni new file mode 100644 index 00000000000000..9ad63cf8d53544 --- /dev/null +++ b/build_overrides/zephyr.gni @@ -0,0 +1,15 @@ +# 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. + +# place holder diff --git a/config/zephyr/README.md b/config/zephyr/README.md new file mode 100644 index 00000000000000..bb780288a34606 --- /dev/null +++ b/config/zephyr/README.md @@ -0,0 +1,10 @@ +# Zephyr build and configuration files + +This directory contains build scripts and common configuration files used by +CHIP Zephyr applications. It is structured as follows: + +| File/Folder | Contents | +| ----------- | ----------------------------------------------------------------------------------------------------------------------------------- | +| chip-gn | GN project used to build selected CHIP libraries with the _Zephyr_ platform integration layer | +| chip-module | CMake wrapper for the GN project defined in `chip-gn` directory, and other components that allow one to use CHIP as a Zephyr module | +| app | Common and optional Kconfig configuration files that can be used in Zephyr applications | diff --git a/config/zephyr/app/copy-flashbundle-firmware.cmake b/config/zephyr/app/copy-flashbundle-firmware.cmake new file mode 100644 index 00000000000000..93464f7aceb837 --- /dev/null +++ b/config/zephyr/app/copy-flashbundle-firmware.cmake @@ -0,0 +1,25 @@ +# +# Copyright (c) 2022 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. +# + +# Copy the merged.hex file if it exists, otherwise copy zephyr.hex + +set(HEX_NAME merged.hex) + +if(EXISTS "zephyr/${HEX_NAME}") + configure_file("zephyr/${HEX_NAME}" "${FLASHBUNDLE_FIRMWARE}" COPYONLY) +else() + configure_file("zephyr/${KERNEL_HEX_NAME}" "${FLASHBUNDLE_FIRMWARE}" COPYONLY) +endif() diff --git a/config/zephyr/app/enable-gnu-std.cmake b/config/zephyr/app/enable-gnu-std.cmake new file mode 100644 index 00000000000000..5f31e1a5c62ab8 --- /dev/null +++ b/config/zephyr/app/enable-gnu-std.cmake @@ -0,0 +1,6 @@ +add_library(gnu17 INTERFACE) +target_compile_options(gnu17 + INTERFACE + $<$:-std=gnu++17> + -D_SYS__PTHREADTYPES_H_) +target_link_libraries(app PRIVATE gnu17) diff --git a/config/zephyr/app/flashing.cmake b/config/zephyr/app/flashing.cmake new file mode 100644 index 00000000000000..b70e166ff28cfe --- /dev/null +++ b/config/zephyr/app/flashing.cmake @@ -0,0 +1,55 @@ +# +# 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. +# + +# Adds rules to generate a flashing wrapper script for an nrfconnect firmware. + +set(FLASHBUNDLE_FLASHER_COMMON ${CMAKE_PROJECT_NAME}/firmware_utils.py) +set(FLASHBUNDLE_FLASHER_PLATFORM ${CMAKE_PROJECT_NAME}/nrfconnect_firmware_utils.py) +set(FLASHBUNDLE_FLASHER_WRAPPER ${CMAKE_PROJECT_NAME}/${CMAKE_PROJECT_NAME}.flash.py) +set(FLASHBUNDLE_FIRMWARE ${CMAKE_PROJECT_NAME}/${CMAKE_PROJECT_NAME}.hex) +set(FLASHBUNDLE_FILES ${FLASHBUNDLE_FLASHER_COMMON} ${FLASHBUNDLE_FLASHER_PLATFORM} ${FLASHBUNDLE_FLASHER_WRAPPER} ${FLASHBUNDLE_FIRMWARE}) + +get_filename_component(FLASHBUNDLE_FIRMWARE_BASENAME ${FLASHBUNDLE_FIRMWARE} NAME) + +list(JOIN FLASHBUNDLE_FILES "\n" FLASHBUNDLE_MANIFEST) + +file(GENERATE + OUTPUT ${CMAKE_PROJECT_NAME}.flashbundle.txt + CONTENT "${FLASHBUNDLE_MANIFEST}\n" + ) + +add_custom_command(OUTPUT "${FLASHBUNDLE_FLASHER_COMMON}" + COMMAND ${CMAKE_COMMAND} ARGS -E copy "${PROJECT_SOURCE_DIR}/third_party/connectedhomeip/scripts/flashing/firmware_utils.py" "${FLASHBUNDLE_FLASHER_COMMON}" + VERBATIM) + +add_custom_command(OUTPUT "${FLASHBUNDLE_FLASHER_PLATFORM}" + COMMAND ${CMAKE_COMMAND} ARGS -E copy "${PROJECT_SOURCE_DIR}/third_party/connectedhomeip/scripts/flashing/nrfconnect_firmware_utils.py" "${FLASHBUNDLE_FLASHER_PLATFORM}" + VERBATIM) + +add_custom_command(OUTPUT "${FLASHBUNDLE_FIRMWARE}" + DEPENDS zephyr/${KERNEL_HEX_NAME} + COMMAND ${CMAKE_COMMAND} ARGS -DFLASHBUNDLE_FIRMWARE=${FLASHBUNDLE_FIRMWARE} -DKERNEL_HEX_NAME=${KERNEL_HEX_NAME} -P "${CHIP_ROOT}/config/nrfconnect/app/copy-flashbundle-firmware.cmake" + VERBATIM) + +add_custom_command(OUTPUT "${CMAKE_PROJECT_NAME}/${CMAKE_PROJECT_NAME}.flash.py" + COMMAND ${python} + "${PROJECT_SOURCE_DIR}/third_party/connectedhomeip/scripts/flashing/gen_flashing_script.py" nrfconnect + --output "${FLASHBUNDLE_FLASHER_WRAPPER}" + --application "${FLASHBUNDLE_FIRMWARE_BASENAME}" + COMMENT "To flash ${FLASHBUNDLE_FIRMWARE_BASENAME} run ${FLASHBUNDLE_FLASHER_WRAPPER}" + VERBATIM) + +add_custom_target(flashing_script DEPENDS ${FLASHBUNDLE_FILES}) diff --git a/config/zephyr/app/overlay-usb_support.conf b/config/zephyr/app/overlay-usb_support.conf new file mode 100644 index 00000000000000..1b4a2d56fe8490 --- /dev/null +++ b/config/zephyr/app/overlay-usb_support.conf @@ -0,0 +1,24 @@ +# +# Copyright (c) 2020 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. +# + +CONFIG_GPIO=y + +CONFIG_USB=y +CONFIG_USB_DEVICE_STACK=y +CONFIG_USB_UART_CONSOLE=y + +CONFIG_UART_INTERRUPT_DRIVEN=y +CONFIG_UART_LINE_CTRL=y diff --git a/config/zephyr/chip-gn/.gn b/config/zephyr/chip-gn/.gn new file mode 100644 index 00000000000000..2024e95fed6079 --- /dev/null +++ b/config/zephyr/chip-gn/.gn @@ -0,0 +1,29 @@ +# Copyright (c) 2020 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/build.gni") +import("//build_overrides/chip.gni") + +# The location of the build configuration file. +buildconfig = "${build_root}/config/BUILDCONFIG.gn" + +# CHIP uses angle bracket includes. +check_system_includes = true + +default_args = { + target_cpu = "" + target_os = "zephyr" + + import("${chip_root}/config/zephyr/chip-gn/args.gni") +} diff --git a/config/zephyr/chip-gn/BUILD.gn b/config/zephyr/chip-gn/BUILD.gn new file mode 100644 index 00000000000000..85288992c84812 --- /dev/null +++ b/config/zephyr/chip-gn/BUILD.gn @@ -0,0 +1,40 @@ +# Copyright (c) 2020 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/build.gni") +import("//build_overrides/chip.gni") + +import("${chip_root}/build/chip/tests.gni") + +assert(current_os == "zephyr") + +declare_args() { + chip_build_example_providers = false +} + +group("zephyr") { + deps = [ "${chip_root}/src/lib" ] + + if (chip_build_tests) { + deps += [ "${chip_root}/src:tests" ] + } + + if (chip_build_example_providers) { + deps += [ "${chip_root}/examples/providers:device_info_provider" ] + } +} + +group("default") { + deps = [ ":zephyr" ] +} diff --git a/config/zephyr/chip-gn/args.gni b/config/zephyr/chip-gn/args.gni new file mode 100644 index 00000000000000..e5d5aedf5bc17d --- /dev/null +++ b/config/zephyr/chip-gn/args.gni @@ -0,0 +1,27 @@ +# Copyright (c) 2020 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") + +chip_device_platform = "zephyr" + +chip_build_tests = false + +chip_project_config_include = "" +chip_system_project_config_include = "" +chip_ble_project_config_include = "" + +custom_toolchain = "${chip_root}/config/zephyr/chip-gn/toolchain:zephyr" + +pw_build_PIP_CONSTRAINTS = [ "${chip_root}/scripts/constraints.txt" ] diff --git a/config/zephyr/chip-gn/toolchain/BUILD.gn b/config/zephyr/chip-gn/toolchain/BUILD.gn new file mode 100644 index 00000000000000..28fbe6d8e939b0 --- /dev/null +++ b/config/zephyr/chip-gn/toolchain/BUILD.gn @@ -0,0 +1,33 @@ +# Copyright (c) 2020 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/toolchain/gcc_toolchain.gni") +import("//build_overrides/build.gni") + +declare_args() { + zephyr_ar = "" + zephyr_cc = "" + zephyr_cxx = "" +} + +gcc_toolchain("zephyr") { + ar = zephyr_ar + cc = zephyr_cc + cxx = zephyr_cxx + + toolchain_args = { + current_os = "zephyr" + is_clang = false + } +} diff --git a/config/zephyr/chip-module/CMakeLists.txt b/config/zephyr/chip-module/CMakeLists.txt new file mode 100644 index 00000000000000..ba25a316f57a2e --- /dev/null +++ b/config/zephyr/chip-module/CMakeLists.txt @@ -0,0 +1,257 @@ +# +# Copyright (c) 2022 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. +# + +# +# @file +# CMake sub-project defining 'chip' target which represents CHIP library +# Since CHIP doesn't provide native CMake support, ExternalProject +# module is used to build the required artifacts with GN meta-build +# system. It is assumed that find_package(Zephyr) has been called before +# including this file. +# + +if (CONFIG_CHIP) + +include(ExternalProject) +include(../zephyr-util.cmake) + +# ============================================================================== +# Declare configuration variables and define constants +# ============================================================================== + +# C/C++ compiler flags passed to CHIP build system +list(APPEND CHIP_CFLAGS) + +# C compiler flags passed to CHIP build system +list(APPEND CHIP_CFLAGS_C) + +# C++ compiler flags passed to CHIP build system +list(APPEND CHIP_CFLAGS_CC) + +# CHIP libraries that the application should be linked with +list(APPEND CHIP_LIBRARIES) + +# GN meta-build system arguments in the form of 'key1 = value1\nkey2 = value2...' string +string(APPEND CHIP_GN_ARGS) + +# C/C++ compiler flags which should not be forwarded to CHIP +# build system (e.g. because CHIP configures them on its own) +set(CHIP_CFLAG_EXCLUDES + "-fno-asynchronous-unwind-tables" + "-fno-common" + "-fno-defer-pop" + "-fno-reorder-functions" + "-ffunction-sections" + "-fdata-sections" + "-g*" + "-O*" + "-W*" +) + +# ============================================================================== +# Helper macros +# ============================================================================== + +macro(chip_gn_arg_import FILE) + string(APPEND CHIP_GN_ARGS "import(\"${FILE}\")\n") +endmacro() + +macro(chip_gn_arg_string ARG STRING) + string(APPEND CHIP_GN_ARGS "${ARG} = \"${STRING}\"\n") +endmacro() + +macro(chip_gn_arg_bool ARG BOOLEAN) + if (${BOOLEAN}) + string(APPEND CHIP_GN_ARGS "${ARG} = true\n") + else() + string(APPEND CHIP_GN_ARGS "${ARG} = false\n") + endif() +endmacro() + +macro(chip_gn_arg_cflags ARG CFLAGS) + set(CFLAG_EXCLUDES "[") + foreach(cflag ${CHIP_CFLAG_EXCLUDES}) + string(APPEND CFLAG_EXCLUDES "\"${cflag}\", ") + endforeach() + string(APPEND CFLAG_EXCLUDES "]") + string(APPEND CHIP_GN_ARGS "${ARG} = filter_exclude(string_split(\"${CFLAGS}\"), ${CFLAG_EXCLUDES})\n") +endmacro() + +# ============================================================================== +# Prepare CHIP configuration based on the project Kconfig configuration +# ============================================================================== + +if (NOT CHIP_ROOT) + get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../.. REALPATH) +endif() + +set(GN_ROOT_TARGET ${CHIP_ROOT}/config/zephyr/chip-gn) + +if (CONFIG_POSIX_API) + list(APPEND CHIP_CFLAGS + -D_SYS__PTHREADTYPES_H_ + -isystem${ZEPHYR_BASE}/include/zephyr/posix + ) +endif() + +list(APPEND CHIP_CFLAGS -isystem${ZEPHYR_BASE}/../modules/crypto/mbedtls/include/) + +zephyr_get_compile_flags(CHIP_CFLAGS_C C) +zephyr_get_compile_flags(CHIP_CFLAGS_CC CXX) +zephyr_get_gnu_cpp_standard(CHIP_CFLAGS_CC) + +convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS CHIP_CFLAGS) +convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS_C CHIP_CFLAGS_C) +convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS_CC CHIP_CFLAGS_CC) + +# Prepare CHIP libraries that the application should be linked with + +if (NOT CHIP_LIBRARIES) + set(CHIP_LIBRARIES -lCHIP) +endif() + +if (CONFIG_CHIP_LIB_SHELL) + list(APPEND CHIP_LIBRARIES -lCHIPShell) +endif() + +if (CONFIG_CHIP_PW_RPC) + list(APPEND CHIP_LIBRARIES -lPwRpc) +endif() + + +# Set up CHIP project configuration file + +if (CONFIG_CHIP_PROJECT_CONFIG) + get_filename_component(CHIP_PROJECT_CONFIG + ${CONFIG_CHIP_PROJECT_CONFIG} + REALPATH + BASE_DIR ${CMAKE_SOURCE_DIR} + ) + set(CHIP_PROJECT_CONFIG "<${CHIP_PROJECT_CONFIG}>") +else() + set(CHIP_PROJECT_CONFIG "") +endif() + +# Set up custom OpenThread configuration + +if (CONFIG_CHIP_OPENTHREAD_CONFIG) + get_filename_component(CHIP_OPENTHREAD_CONFIG + ${CONFIG_CHIP_OPENTHREAD_CONFIG} + REALPATH + BASE_DIR ${CMAKE_SOURCE_DIR} + ) + zephyr_set_openthread_config(${CHIP_OPENTHREAD_CONFIG}) +endif() + +# Find required programs + +find_program(GN_EXECUTABLE gn) +if (${GN_EXECUTABLE} STREQUAL GN_EXECUTABLE-NOTFOUND) + message(FATAL_ERROR "The 'gn' command was not found. Make sure you have GN installed.") +else() + # Parse the 'gn --version' output to find the installed version. + set(MIN_GN_VERSION 1000) + execute_process( + COMMAND + ${GN_EXECUTABLE} --version + OUTPUT_VARIABLE gn_version_output + ERROR_VARIABLE gn_error_output + RESULT_VARIABLE gn_status + ) + + if(${gn_status} EQUAL 0) + if(gn_version_output VERSION_LESS ${MIN_GN_VERSION}) + message(FATAL_ERROR "Found unsuitable version of 'gn'. Required is at least ${MIN_GN_VERSION}") + endif() + else() + message(FATAL_ERROR "Could NOT find working gn: Found gn (${GN_EXECUTABLE}), but failed to load with:\n ${gn_error_output}") + endif() +endif() + +find_package(Python3 REQUIRED) + +# ============================================================================== +# Generate configuration for CHIP GN build system +# ============================================================================== +chip_gn_arg_cflags("target_cflags" ${CHIP_CFLAGS}) +chip_gn_arg_cflags("target_cflags_c" ${CHIP_CFLAGS_C}) +chip_gn_arg_cflags("target_cflags_cc" ${CHIP_CFLAGS_CC}) +chip_gn_arg_string("zephyr_ar" ${CMAKE_AR}) +chip_gn_arg_string("zephyr_cc" ${CMAKE_C_COMPILER}) +chip_gn_arg_string("zephyr_cxx" ${CMAKE_CXX_COMPILER}) +chip_gn_arg_string("chip_project_config_include" "${CHIP_PROJECT_CONFIG}") +chip_gn_arg_string("chip_system_project_config_include" "${CHIP_PROJECT_CONFIG}") +chip_gn_arg_bool ("is_debug" CONFIG_DEBUG) +chip_gn_arg_bool ("chip_enable_openthread" CONFIG_NET_L2_OPENTHREAD) +chip_gn_arg_bool ("chip_openthread_ftd" CONFIG_OPENTHREAD_FTD) +chip_gn_arg_bool ("chip_inet_config_enable_ipv4" CONFIG_NET_IPV4) +chip_gn_arg_bool ("chip_build_tests" CONFIG_CHIP_BUILD_TESTS) +chip_gn_arg_bool ("chip_inet_config_enable_tcp_endpoint" CONFIG_CHIP_BUILD_TESTS) +chip_gn_arg_bool ("chip_build_libshell" CONFIG_CHIP_LIB_SHELL) + +if (CONFIG_CHIP_ENABLE_DNSSD_SRP) + chip_gn_arg_string("chip_mdns" "platform") +endif() + +file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/args.gn CONTENT ${CHIP_GN_ARGS}) + +# ============================================================================== +# Define 'chip-gn' target that builds CHIP library(ies) with GN build system +# ============================================================================== + +ExternalProject_Add( + chip-gn + PREFIX ${CMAKE_CURRENT_BINARY_DIR} + SOURCE_DIR ${CHIP_ROOT} + BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} + CONFIGURE_COMMAND ${GN_EXECUTABLE} + --root=${CHIP_ROOT} + --root-target=${GN_ROOT_TARGET} + --dotfile=${GN_ROOT_TARGET}/.gn + --script-executable=${Python3_EXECUTABLE} + gen --check --fail-on-unused-args ${CMAKE_CURRENT_BINARY_DIR} + BUILD_COMMAND ninja + INSTALL_COMMAND "" + BUILD_BYPRODUCTS ${CHIP_LIBRARIES} + BUILD_ALWAYS TRUE + USES_TERMINAL_CONFIGURE TRUE + USES_TERMINAL_BUILD TRUE +) +add_dependencies(chip-gn kernel) + +# ============================================================================== +# Define 'chip' target that exposes CHIP headers & libraries to the application +# ============================================================================== + +zephyr_interface_library_named(chip) +target_compile_definitions(chip INTERFACE CHIP_HAVE_CONFIG_H) +target_include_directories(chip INTERFACE + ${CHIP_ROOT}/src + ${CHIP_ROOT}/src/include + ${CHIP_ROOT}/src/lib + ${CHIP_ROOT}/third_party/nlassert/repo/include + ${CMAKE_CURRENT_BINARY_DIR}/gen/include +) +target_link_directories(chip INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/lib) + +if (CONFIG_CHIP_LIB_SHELL) + target_link_options(chip INTERFACE -Wl,--whole-archive -lCHIPShell -Wl,--no-whole-archive) +endif() + +target_link_libraries(chip INTERFACE -Wl,--start-group ${CHIP_LIBRARIES} -Wl,--end-group) +add_dependencies(chip chip-gn) + +endif() # CONFIG_CHIP \ No newline at end of file diff --git a/config/zephyr/chip-module/Kconfig b/config/zephyr/chip-module/Kconfig new file mode 100644 index 00000000000000..90c7d73f470b8f --- /dev/null +++ b/config/zephyr/chip-module/Kconfig @@ -0,0 +1,17 @@ +# +# 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. +# + +rsource "../Kconfig" \ No newline at end of file diff --git a/config/zephyr/chip-module/Kconfig.defaults b/config/zephyr/chip-module/Kconfig.defaults new file mode 100644 index 00000000000000..4cf4edea17a939 --- /dev/null +++ b/config/zephyr/chip-module/Kconfig.defaults @@ -0,0 +1,225 @@ +# +# Copyright (c) 2022 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. +# + +# The purpose of this file is to define new default values of settings used when building Matter samples. +# This file only changes defaults and thus all symbols here must be promptless +# and safeguarded so that they only are applied when building Matter. + +if CHIP + +config LOG + bool + default y + +if LOG + +choice LOG_MODE + default LOG_MODE_MINIMAL +endchoice + +choice MATTER_LOG_LEVEL_CHOICE + default MATTER_LOG_LEVEL_DBG +endchoice + +config LOG_DEFAULT_LEVEL + int + default 2 + +endif + +config PRINTK_SYNC + bool + default y + +config ASSERT + bool + default y + +config HW_STACK_PROTECTION + bool + default y + +config FPU + bool + default y + +config SHELL + bool + default y + +# Enable getting reboot reasons information +config HWINFO + bool + default y + +# Generic networking options +config NET_SOCKETS_POSIX_NAMES + bool + default n + +# Application stack size +config MAIN_STACK_SIZE + int + default 8192 + +config INIT_STACKS + bool + default y + +config CHIP_DEVICE_VENDOR_ID + int + default 65521 + +config CHIP_DEVICE_PRODUCT_ID + int + default 32768 + +# Disable certain parts of Zephyr IPv6 stack +config NET_IPV6_NBR_CACHE + bool + default n + +config NET_IPV6_MLD + bool + default y + +config NET_IF_MCAST_IPV6_ADDR_COUNT + int + default 14 + +# Network buffers +config NET_PKT_RX_COUNT + int + default 16 + +config NET_PKT_TX_COUNT + int + default 16 + +config NET_BUF_RX_COUNT + int + default 80 + +config NET_BUF_TX_COUNT + int + default 80 + +# Bluetooth Low Energy configs +config BT + bool + default y + +config BT_PERIPHERAL + bool + default y + +config BT_PERIPHERAL_PREF_MIN_INT + int + default 36 + +config BT_PERIPHERAL_PREF_MAX_INT + int + default 36 + +config BT_GAP_AUTO_UPDATE_CONN_PARAMS + bool + default y + +config BT_GATT_DYNAMIC_DB + bool + default y + +config BT_DEVICE_NAME_DYNAMIC + bool + default y + +config BT_DEVICE_NAME_MAX + int + default 15 + +config BT_MAX_CONN + int + default 1 + +config BT_L2CAP_TX_MTU + int + default 247 + +config BT_BUF_ACL_RX_SIZE + int + default 251 + +config BT_BUF_ACL_TX_SIZE + int + default 251 + +config BT_RX_STACK_SIZE + int + default 1200 + +config CHIP_OTA_REQUESTOR + bool + default y + +# Enable extended discovery +config CHIP_EXTENDED_DISCOVERY + bool + default y + +config NVS_LOOKUP_CACHE + bool + default y + +# Enable OpenThread +config NET_L2_OPENTHREAD + bool + default y + +# Enable mbedTLS +config MBEDTLS_ENABLE_HEAP + bool + default y + +config MBEDTLS_HEAP_SIZE + int + default 15360 + +# Disable not used shell modules +config SENSOR_SHELL + bool + default n + +config DEVICE_SHELL + bool + default n + +config DATE_SHELL + bool + default n + +config DEVMEM_SHELL + bool + default n + +config MCUBOOT_SHELL + bool + default n + +config FLASH_SHELL + bool + default n + +endif # CHIP diff --git a/config/zephyr/chip-module/Kconfig.features b/config/zephyr/chip-module/Kconfig.features new file mode 100644 index 00000000000000..bc800095dc7ce0 --- /dev/null +++ b/config/zephyr/chip-module/Kconfig.features @@ -0,0 +1,68 @@ +# +# Copyright (c) 2022 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. +# + +# The purpose of this file is to define Kconfig options enabling specific features. +# Feature options will define defaults for settings that are related to the specific feature. + +if CHIP + +# MCU Manager and SMP configuration +config MCUMGR + bool + default y + +config MCUMGR_CMD_IMG_MGMT + bool + default y + +config MCUMGR_CMD_OS_MGMT + bool + default y + +config MCUMGR_SMP_BT + bool + default y + +config MCUMGR_SMP_BT_AUTHEN + bool + default n + +config MCUMGR_BUF_COUNT + int + default 6 + +# Increase BT MTU and RX buffer for big size DFU messages +config BT_L2CAP_TX_MTU + int + default 498 + +config BT_BUF_ACL_RX_SIZE + int + default 502 + +# Increase MCUMGR_BUF_SIZE, as it must be big enough to fit MAX MTU + overhead and for single-image DFU default is 384 B +config MCUMGR_BUF_SIZE + int + default 1024 + +# Increase system workqueue size, as SMP is processed within it +config SYSTEM_WORKQUEUE_STACK_SIZE + int + default 2800 + +endif # CHIP_DFU_OVER_BT_SMP + +endif # CHIP diff --git a/config/zephyr/chip-module/Kconfig.mcuboot.defaults b/config/zephyr/chip-module/Kconfig.mcuboot.defaults new file mode 100644 index 00000000000000..4d5979330000f4 --- /dev/null +++ b/config/zephyr/chip-module/Kconfig.mcuboot.defaults @@ -0,0 +1,89 @@ +# +# Copyright (c) 2022 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. +# + +# The purpose of this file is to define new default values of settings used when building mcuboot child image for Matter samples. + +config MAIN_STACK_SIZE + int + default 10240 + +config BOOT_SWAP_SAVE_ENCTLV + bool + default n + +config BOOT_ENCRYPT_RSA + bool + default n + +config BOOT_ENCRYPT_EC256 + bool + default n + +config BOOT_ENCRYPT_X25519 + bool + default n + +config BOOT_BOOTSTRAP + bool + default n + +config PM + bool + default n + +config FLASH + bool + default y + +config FPROTECT + bool + default y + +config BOOT_MAX_IMG_SECTORS + int + default 256 + +config LOG + bool + default n + +config CONSOLE_HANDLER + bool + default n + +config BOOT_BANNER + bool + default n + +config TIMESLICING + bool + default n + +config RESET_ON_FATAL_ERROR + bool + default n + +config MULTITHREADING + bool + default n + +config TICKLESS_KERNEL + bool + default n + +config TIMEOUT_64BIT + bool + default n diff --git a/config/zephyr/chip-module/Kconfig.mcuboot.root b/config/zephyr/chip-module/Kconfig.mcuboot.root new file mode 100644 index 00000000000000..e8756636dfd66d --- /dev/null +++ b/config/zephyr/chip-module/Kconfig.mcuboot.root @@ -0,0 +1,21 @@ +# +# Copyright (c) 2022 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. +# + +# The purpose of this file is to create a wrapper Kconfig file that will be set as +# mcuboot_KCONFIG_ROOT and processed before any other Kconfig for mcuboot child image. + +rsource "Kconfig.mcuboot.defaults" +source "${ZEPHYR_BASE}/../bootloader/mcuboot/boot/zephyr/Kconfig" diff --git a/config/zephyr/chip-module/Kconfig.multiprotocol_rpmsg.defaults b/config/zephyr/chip-module/Kconfig.multiprotocol_rpmsg.defaults new file mode 100644 index 00000000000000..dea52bae56e2f4 --- /dev/null +++ b/config/zephyr/chip-module/Kconfig.multiprotocol_rpmsg.defaults @@ -0,0 +1,91 @@ +# +# Copyright (c) 2022 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. +# + +# The purpose of this file is to define new default values of settings used when building multiprotocol_rpmsg child image for Matter samples. + +config LOG + bool + default n + +config HEAP_MEM_POOL_SIZE + int + default 8192 + +config MAIN_STACK_SIZE + int + default 2048 + +config SYSTEM_WORKQUEUE_STACK_SIZE + int + default 2048 + +config BT + bool + default y + +config BT_HCI_RAW + bool + default y + +config BT_MAX_CONN + int + default 1 + +config BT_PERIPHERAL + bool + default y + +config BT_CENTRAL + bool + default n + +config BT_BUF_ACL_RX_SIZE + int + default 502 + +config BT_BUF_ACL_TX_SIZE + int + default 251 + +config BT_CTLR_DATA_LENGTH_MAX + int + default 251 + +config BT_CTLR_ASSERT_HANDLER + bool + default y + +config BT_HCI_RAW_RESERVE + int + default 1 + +# Workaround: Unable to allocate command buffer when using K_NO_WAIT since +# Host number of completed commands does not follow normal flow control. +config BT_BUF_CMD_TX_COUNT + int + default 10 + +config ASSERT + bool + default y + +config DEBUG_INFO + bool + default y + +config EXCEPTION_STACK_TRACE + bool + default y diff --git a/config/zephyr/chip-module/Kconfig.multiprotocol_rpmsg.root b/config/zephyr/chip-module/Kconfig.multiprotocol_rpmsg.root new file mode 100644 index 00000000000000..b34c82243585b4 --- /dev/null +++ b/config/zephyr/chip-module/Kconfig.multiprotocol_rpmsg.root @@ -0,0 +1,21 @@ +# +# Copyright (c) 2022 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. +# + +# The purpose of this file is to create a wrapper Kconfig file that will be set as +# multiprotocol_rpmsg_KCONFIG_ROOT and processed before any other Kconfig for multiprotocol_rpmsg child image. + +rsource "Kconfig.multiprotocol_rpmsg.defaults" +source "Kconfig.zephyr" diff --git a/config/zephyr/chip-module/make_gn_args.py b/config/zephyr/chip-module/make_gn_args.py new file mode 100644 index 00000000000000..f30cd459a6e7d4 --- /dev/null +++ b/config/zephyr/chip-module/make_gn_args.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 + +# +# 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. +# + +import argparse +import re +import sys + +GN_SPECIAL_CHARACTERS = r'(["$\\])' +GN_CFLAG_EXCLUDES = [ + '-fno-asynchronous-unwind-tables', + '-fno-common', + '-fno-defer-pop', + '-fno-reorder-functions', + '-ffunction-sections', + '-fdata-sections', + '-g*', + '-O*', + '-W*', +] + + +def escape_strings(gn_args): + return [[key, re.sub(GN_SPECIAL_CHARACTERS, r'\\\1', value)] for key, value in gn_args] + + +def write_gn_args(args): + if args.module: + sys.stdout.write('import("{}")\n'.format(args.module)) + + for key, value in args.arg: + sys.stdout.write('{} = {}\n'.format(key, value)) + + for key, value in args.arg_string: + sys.stdout.write('{} = "{}"\n'.format(key, value)) + + cflag_excludes = ', '.join(['"{}"'.format(exclude) + for exclude in GN_CFLAG_EXCLUDES]) + + for key, value in args.arg_cflags: + sys.stdout.write('{} = filter_exclude(string_split("{}"), [{}])\n'.format( + key, value, cflag_excludes)) + + +def main(): + parser = argparse.ArgumentParser(fromfile_prefix_chars='@') + parser.add_argument('--module', action='store') + parser.add_argument('--arg', action='append', nargs=2, default=[]) + parser.add_argument('--arg-string', action='append', nargs=2, default=[]) + parser.add_argument('--arg-cflags', action='append', nargs=2, default=[]) + args = parser.parse_args() + args.arg_string = escape_strings(args.arg_string) + args.arg_cflags = escape_strings(args.arg_cflags) + write_gn_args(args) + + +if __name__ == "__main__": + main() diff --git a/config/zephyr/chip-module/zephyr/module.yml b/config/zephyr/chip-module/zephyr/module.yml new file mode 100644 index 00000000000000..c09c9ea59e218a --- /dev/null +++ b/config/zephyr/chip-module/zephyr/module.yml @@ -0,0 +1,22 @@ +# +# 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. +# + +name: connectedhomeip +build: + cmake: . + kconfig: Kconfig + depends: + - openthread diff --git a/src/platform/BUILD.gn b/src/platform/BUILD.gn index 3dde9e09081332..773fa52d93fa05 100644 --- a/src/platform/BUILD.gn +++ b/src/platform/BUILD.gn @@ -258,6 +258,11 @@ if (chip_device_platform != "none" && chip_device_platform != "external") { ] defines -= [ "CHIP_DEVICE_CONFIG_ENABLE_WPA=${chip_device_config_enable_wpa}" ] + } else if (chip_device_platform == "zephyr") { + defines += [ + "CHIP_DEVICE_LAYER_TARGET_ZEPHYR=1", + "CHIP_DEVICE_LAYER_TARGET=Zephyr", + ] } } } else if (chip_device_platform == "none") { @@ -413,6 +418,8 @@ if (chip_device_platform != "none") { _platform_target = "fake" } else if (chip_device_platform == "cyw30739") { _platform_target = "CYW30739" + } else if (chip_device_platform == "zephyr") { + _platform_target = "Zephyr" } else { assert(false, "Unknown chip_device_platform: ${chip_device_platform}") } diff --git a/src/platform/Zephyr/BUILD.gn b/src/platform/Zephyr/BUILD.gn new file mode 100644 index 00000000000000..c4f2820b4da8df --- /dev/null +++ b/src/platform/Zephyr/BUILD.gn @@ -0,0 +1,79 @@ +# 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/chip.gni") +import("//build_overrides/zephyr.gni") + +import("${chip_root}/src/platform/Zephyr/args.gni") +import("${chip_root}/src/platform/device.gni") + +assert(chip_device_platform == "zephyr") + +static_library("Zephyr") { + sources = [ + "../SingletonConfigurationManager.cpp", + "BLEManagerImpl.cpp", + "BLEManagerImpl.h", + "BlePlatformConfig.h", + "CHIPDevicePlatformConfig.h", + "CHIPDevicePlatformEvent.h", + "CHIPPlatformConfig.h", + "ConfigurationManagerImpl.cpp", + "ConfigurationManagerImpl.h", + "ConnectivityManagerImpl.cpp", + "ConnectivityManagerImpl.h", + "DeviceNetworkProvisioningDelegateImpl.cpp", + "DeviceNetworkProvisioningDelegateImpl.h", + "DiagnosticDataProviderImpl.cpp", + "DiagnosticDataProviderImpl.h", + "InetPlatformConfig.h", + "KeyValueStoreManagerImpl.cpp", + "KeyValueStoreManagerImpl.h", + "Logging.cpp", + "PlatformManagerImpl.cpp", + "PlatformManagerImpl.h", + "SysHeapMalloc.h", + "SystemPlatformConfig.h", + "SystemTimeSupport.cpp", + "ZephyrConfig.cpp", + "ZephyrConfig.h", + ] + + public_deps = [ "${chip_root}/src/platform:platform_base" ] + + if (chip_enable_openthread) { + sources += [ + "../OpenThread/OpenThreadUtils.cpp", + "ThreadStackManagerImpl.cpp", + "ThreadStackManagerImpl.h", + ] + + if (chip_mdns == "platform") { + sources += [ "../OpenThread/DnssdImpl.cpp" ] + deps = [ "${chip_root}/src/lib/dnssd:platform_header" ] + } + } + + if (chip_enable_nfc) { + sources += [ + "NFCManagerImpl.cpp", + "NFCManagerImpl.h", + ] + } + + if (chip_malloc_sys_heap) { + sources += [ "SysHeapMalloc.cpp" ] + } +} diff --git a/src/platform/Zephyr/BlePlatformConfig.h b/src/platform/Zephyr/BlePlatformConfig.h new file mode 100644 index 00000000000000..9f49cd73dcfec3 --- /dev/null +++ b/src/platform/Zephyr/BlePlatformConfig.h @@ -0,0 +1,36 @@ +/* + * + * Copyright (c) 2020 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. + */ + +/** + * @file + * Platform-specific configuration overrides for the CHIP BLE + * Layer on Zephyr platform. + * + */ + +#pragma once + +// ==================== Platform Adaptations ==================== + +struct bt_conn; +#define BLE_CONNECTION_OBJECT bt_conn * +#define BLE_CONNECTION_UNINITIALIZED nullptr +#define BLE_MAX_RECEIVE_WINDOW_SIZE 5 + +// ========== Platform-specific Configuration Overrides ========= + +/* none so far */ diff --git a/src/platform/Zephyr/CHIPDevicePlatformConfig.h b/src/platform/Zephyr/CHIPDevicePlatformConfig.h new file mode 100644 index 00000000000000..c591a51af4c070 --- /dev/null +++ b/src/platform/Zephyr/CHIPDevicePlatformConfig.h @@ -0,0 +1,132 @@ +/* + * + * Copyright (c) 2022 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. + */ + +/** + * @file + * Platform-specific configuration overrides for the chip Device Layer + * on Zephyr platform. + */ + +#pragma once + +#include "autoconf.h" + +// ==================== Platform Adaptations ==================== + +#ifndef CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID +#define CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID CONFIG_CHIP_DEVICE_VENDOR_ID +#endif + +#ifndef CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID +#define CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID CONFIG_CHIP_DEVICE_PRODUCT_ID +#endif + +#ifndef CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION +#define CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION CONFIG_CHIP_DEVICE_SOFTWARE_VERSION +#endif + +#ifndef CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING +#define CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING CONFIG_CHIP_DEVICE_SOFTWARE_VERSION_STRING +#endif + +#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION 0 +#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP 0 + +#define CHIP_DEVICE_CONFIG_ENABLE_THREAD CONFIG_NET_L2_OPENTHREAD + +#define CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE CONFIG_BT + +#define CHIP_DEVICE_CONFIG_ENABLE_CHIP_TIME_SERVICE_TIME_SYNC 0 + +// ========== Platform-specific Configuration ========= + +// These are configuration options that are unique to Zephyr platforms. +// These can be overridden by the application as needed. + +#ifndef CHIP_DEVICE_CONFIG_SETTINGS_KEY +/// Key for all Matter persistent data stored using the Zephyr Settings API +#define CHIP_DEVICE_CONFIG_SETTINGS_KEY "mt" +#endif // CHIP_DEVICE_CONFIG_SETTINGS_KEY + +#ifndef CHIP_DEVICE_CONFIG_OTA_REQUESTOR_REBOOT_DELAY_MS +/// Delay between completing a firmware update download and reboot to apply the update +#define CHIP_DEVICE_CONFIG_OTA_REQUESTOR_REBOOT_DELAY_MS 1000 +#endif // CHIP_DEVICE_CONFIG_OTA_REQUESTOR_REBOOT_DELAY_MS + +#ifndef CHIP_DEVICE_CONFIG_SERVER_SHUTDOWN_ACTIONS_SLEEP_MS +/// Time to sleep after running server shutdown actions to let lower layers complete the actions. +/// This may include transmitting packets created by the actions. +#define CHIP_DEVICE_CONFIG_SERVER_SHUTDOWN_ACTIONS_SLEEP_MS 500 +#endif // CHIP_DEVICE_CONFIG_SERVER_SHUTDOWN_ACTIONS_SLEEP_MS + +#ifndef CHIP_DEVICE_CONFIG_HEAP_STATISTICS_MALLINFO +#if !defined(CONFIG_CHIP_MALLOC_SYS_HEAP) && defined(CONFIG_NEWLIB_LIBC) +/// Use mallinfo() to obtain the heap usage statistics exposed by SoftwareDiagnostics cluster attributes. +#define CHIP_DEVICE_CONFIG_HEAP_STATISTICS_MALLINFO 1 +#endif // !defined(CONFIG_CHIP_MALLOC_SYS_HEAP) && defined(CONFIG_NEWLIB_LIBC) +#endif // CHIP_DEVICE_CONFIG_HEAP_STATISTICS_MALLINFO + +// ========== Platform-specific Configuration Overrides ========= + +#ifndef CHIP_DEVICE_CONFIG_CHIP_TASK_PRIORITY +#define CHIP_DEVICE_CONFIG_CHIP_TASK_PRIORITY (K_PRIO_PREEMPT(1)) +#endif // CHIP_DEVICE_CONFIG_CHIP_TASK_PRIORITY + +#ifndef CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE +#define CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE 8192 +#endif // CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE + +#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_TELEMETRY 0 +#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_TELEMETRY 0 +#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_TELEMETRY_FULL 0 +#define CHIP_DEVICE_CONFIG_LOG_PROVISIONING_HASH 0 + +#define CHIP_DEVICE_CONFIG_CHIPOBLE_DISABLE_ADVERTISING_WHEN_PROVISIONED CONFIG_CHIP_DISABLE_CHIPOBLE_ADVERTISING_WHEN_PROVISIONED + +#define CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART 0 + +#ifndef CONFIG_CHIP_ENABLE_PAIRING_AUTOSTART +#define CHIP_DEVICE_CONFIG_ENABLE_PAIRING_AUTOSTART 0 +#else +#define CHIP_DEVICE_CONFIG_ENABLE_PAIRING_AUTOSTART CONFIG_CHIP_ENABLE_PAIRING_AUTOSTART +#endif // CONFIG_CHIP_ENABLE_PAIRING_AUTOSTART + +#ifdef CONFIG_CHIP_ENABLE_DNSSD_SRP +#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT 1 +#define CHIP_DEVICE_CONFIG_THREAD_SRP_MAX_SERVICES (CHIP_CONFIG_MAX_FABRICS + 1) +#ifdef CONFIG_CHIP_ENABLE_DNS_CLIENT +#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT 1 +#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_COMMISSIONABLE_DISCOVERY 1 +#endif // CONFIG_CHIP_ENABLE_DNS_CLIENT +#endif // CONFIG_CHIP_ENABLE_DNSSD_SRP + +#ifdef CONFIG_CHIP_ENABLE_SLEEPY_END_DEVICE_SUPPORT +#define CHIP_DEVICE_CONFIG_ENABLE_SED 1 +#define CHIP_DEVICE_CONFIG_THREAD_SSED CONFIG_CHIP_THREAD_SSED +#endif // CONFIG_CHIP_ENABLE_SLEEPY_END_DEVICE_SUPPORT + +#ifdef CONFIG_CHIP_COMMISSIONABLE_DEVICE_TYPE +#define CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DEVICE_TYPE 1 +#endif // CONFIG_CHIP_COMMISSIONABLE_DEVICE_TYPE + +#ifdef CONFIG_CHIP_DEVICE_TYPE +#define CHIP_DEVICE_CONFIG_DEVICE_TYPE CONFIG_CHIP_DEVICE_TYPE +#endif // CONFIG_CHIP_DEVICE_TYPE + +#ifdef CONFIG_CHIP_EXTENDED_DISCOVERY +#define CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY 1 +#endif // CONFIG_CHIP_EXTENDED_DISCOVERY diff --git a/src/platform/Zephyr/CHIPDevicePlatformEvent.h b/src/platform/Zephyr/CHIPDevicePlatformEvent.h index ba29c164e49eef..5687945287c2b1 100644 --- a/src/platform/Zephyr/CHIPDevicePlatformEvent.h +++ b/src/platform/Zephyr/CHIPDevicePlatformEvent.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2020 Project CHIP Authors + * Copyright (c) 2022 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. diff --git a/src/platform/Zephyr/CHIPPlatformConfig.h b/src/platform/Zephyr/CHIPPlatformConfig.h new file mode 100644 index 00000000000000..c463fb21da88f2 --- /dev/null +++ b/src/platform/Zephyr/CHIPPlatformConfig.h @@ -0,0 +1,55 @@ +/* + * + * Copyright (c) 2020-2022 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. + */ + +/** + * @file + * Platform-specific configuration overrides for CHIP on + * Zephyr platform. + */ + +#pragma once + +// ==================== General Platform Adaptations ==================== + +#define CHIP_CONFIG_ABORT() abort() + +#define CHIP_CONFIG_PERSISTED_STORAGE_KEY_TYPE const char * +#define CHIP_CONFIG_PERSISTED_STORAGE_MAX_KEY_LENGTH 2 + +#define CHIP_CONFIG_LIFETIIME_PERSISTED_COUNTER_KEY "rc" + +// ==================== General Configuration Overrides ==================== + +#ifndef CHIP_CONFIG_MAX_UNSOLICITED_MESSAGE_HANDLERS +#define CHIP_CONFIG_MAX_UNSOLICITED_MESSAGE_HANDLERS 8 +#endif // CHIP_CONFIG_MAX_UNSOLICITED_MESSAGE_HANDLERS + +#ifndef CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS +#define CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS 8 +#endif // CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS + +#ifndef CHIP_LOG_FILTERING +#define CHIP_LOG_FILTERING 0 +#endif // CHIP_LOG_FILTERING + +#ifndef CHIP_CONFIG_BDX_MAX_NUM_TRANSFERS +#define CHIP_CONFIG_BDX_MAX_NUM_TRANSFERS 1 +#endif // CHIP_CONFIG_BDX_MAX_NUM_TRANSFERS + +#ifndef CHIP_CONFIG_MAX_FABRICS +#define CHIP_CONFIG_MAX_FABRICS 5 +#endif diff --git a/src/platform/Zephyr/ConfigurationManagerImpl.h b/src/platform/Zephyr/ConfigurationManagerImpl.h index e678b5a5146868..5f331d14d81897 100644 --- a/src/platform/Zephyr/ConfigurationManagerImpl.h +++ b/src/platform/Zephyr/ConfigurationManagerImpl.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2020 Project CHIP Authors + * Copyright (c) 2022 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. diff --git a/src/platform/Zephyr/ConnectivityManagerImpl.cpp b/src/platform/Zephyr/ConnectivityManagerImpl.cpp new file mode 100644 index 00000000000000..c09181db994e8d --- /dev/null +++ b/src/platform/Zephyr/ConnectivityManagerImpl.cpp @@ -0,0 +1,61 @@ +/* + * + * Copyright (c) 2020 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 + +#include +#include + +#include +#include + +#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE +#include +#endif + +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD +#include +#endif + +using namespace ::chip; +using namespace ::chip::TLV; +using namespace ::chip::DeviceLayer::Internal; + +namespace chip { +namespace DeviceLayer { + +ConnectivityManagerImpl ConnectivityManagerImpl::sInstance; + +CHIP_ERROR ConnectivityManagerImpl::_Init() +{ +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD + GenericConnectivityManagerImpl_Thread::_Init(); +#endif + + return CHIP_NO_ERROR; +} + +void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) +{ + // Forward the event to the generic base classes as needed. +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD + GenericConnectivityManagerImpl_Thread::_OnPlatformEvent(event); +#endif +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Zephyr/ConnectivityManagerImpl.h b/src/platform/Zephyr/ConnectivityManagerImpl.h new file mode 100644 index 00000000000000..16e39cdb1b63db --- /dev/null +++ b/src/platform/Zephyr/ConnectivityManagerImpl.h @@ -0,0 +1,103 @@ +/* + * + * Copyright (c) 2020 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 +#include +#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE +#include +#else +#include +#endif +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD +#include +#else +#include +#endif +#include + +#include + +namespace chip { +namespace Inet { +class IPAddress; +} // namespace Inet +} // namespace chip + +namespace chip { +namespace DeviceLayer { + +/** + * Concrete implementation of the ConnectivityManager singleton object for Zephyr platforms. + */ +class ConnectivityManagerImpl final : public ConnectivityManager, + public Internal::GenericConnectivityManagerImpl, +#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE + public Internal::GenericConnectivityManagerImpl_BLE, +#else + public Internal::GenericConnectivityManagerImpl_NoBLE, +#endif +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD + public Internal::GenericConnectivityManagerImpl_Thread, +#else + public Internal::GenericConnectivityManagerImpl_NoThread, +#endif + public Internal::GenericConnectivityManagerImpl_NoWiFi +{ + // Allow the ConnectivityManager interface class to delegate method calls to + // the implementation methods provided by this class. + friend class ConnectivityManager; + +private: + // ===== Members that implement the ConnectivityManager abstract interface. + + CHIP_ERROR _Init(void); + void _OnPlatformEvent(const ChipDeviceEvent * event); + + // ===== Members for internal use by the following friends. + + friend ConnectivityManager & ConnectivityMgr(void); + friend ConnectivityManagerImpl & ConnectivityMgrImpl(void); + + static ConnectivityManagerImpl sInstance; +}; + +/** + * Returns the public interface of the ConnectivityManager singleton object. + * + * chip applications should use this to access features of the ConnectivityManager object + * that are common to all platforms. + */ +inline ConnectivityManager & ConnectivityMgr(void) +{ + return ConnectivityManagerImpl::sInstance; +} + +/** + * Returns the platform-specific implementation of the ConnectivityManager singleton object. + * + * chip applications can use this to gain access to features of the ConnectivityManager + * that are specific to the ESP32 platform. + */ +inline ConnectivityManagerImpl & ConnectivityMgrImpl(void) +{ + return ConnectivityManagerImpl::sInstance; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Zephyr/DeviceNetworkProvisioningDelegateImpl.cpp b/src/platform/Zephyr/DeviceNetworkProvisioningDelegateImpl.cpp new file mode 100644 index 00000000000000..2e75cfc0022518 --- /dev/null +++ b/src/platform/Zephyr/DeviceNetworkProvisioningDelegateImpl.cpp @@ -0,0 +1,44 @@ +/* + * + * Copyright (c) 2022 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 "DeviceNetworkProvisioningDelegateImpl.h" + +#if CHIP_ENABLE_OPENTHREAD +#include +#endif + +namespace chip { +namespace DeviceLayer { + +CHIP_ERROR +DeviceNetworkProvisioningDelegateImpl::_ProvisionThreadNetwork(ByteSpan threadData) +{ +#if CHIP_ENABLE_OPENTHREAD + CHIP_ERROR error = CHIP_NO_ERROR; + + SuccessOrExit(error = ThreadStackMgr().SetThreadEnabled(false)); + SuccessOrExit(error = ThreadStackMgr().SetThreadProvision(threadData)); + SuccessOrExit(error = ThreadStackMgr().SetThreadEnabled(true)); +exit: + return error; +#else + return CHIP_ERROR_NOT_IMPLEMENTED; +#endif // CHIP_ENABLE_OPENTHREAD +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Zephyr/DeviceNetworkProvisioningDelegateImpl.h b/src/platform/Zephyr/DeviceNetworkProvisioningDelegateImpl.h new file mode 100644 index 00000000000000..e799358e4b7536 --- /dev/null +++ b/src/platform/Zephyr/DeviceNetworkProvisioningDelegateImpl.h @@ -0,0 +1,43 @@ +/* + * + * Copyright (c) 2020 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 + +namespace chip { +namespace DeviceLayer { + +namespace Internal { + +template +class GenericDeviceNetworkProvisioningDelegateImpl; + +} // namespace Internal + +class DeviceNetworkProvisioningDelegateImpl final + : public Internal::GenericDeviceNetworkProvisioningDelegateImpl +{ + friend class GenericDeviceNetworkProvisioningDelegateImpl; + +private: + CHIP_ERROR _ProvisionWiFiNetwork(const char * ssid, const char * passwd) { return CHIP_ERROR_NOT_IMPLEMENTED; } + CHIP_ERROR _ProvisionThreadNetwork(ByteSpan threadData); +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Zephyr/DiagnosticDataProviderImpl.cpp b/src/platform/Zephyr/DiagnosticDataProviderImpl.cpp index 162a0d3ea19820..e3cee6927ea74b 100644 --- a/src/platform/Zephyr/DiagnosticDataProviderImpl.cpp +++ b/src/platform/Zephyr/DiagnosticDataProviderImpl.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2021 Project CHIP Authors + * Copyright (c) 2022 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. @@ -18,7 +18,7 @@ /** * @file * Provides an implementation of the DiagnosticDataProvider object - * for Zephy platform. + * for Zephyr platform. */ #include diff --git a/src/platform/Zephyr/InetPlatformConfig.h b/src/platform/Zephyr/InetPlatformConfig.h new file mode 100644 index 00000000000000..a5d03a9d0614d5 --- /dev/null +++ b/src/platform/Zephyr/InetPlatformConfig.h @@ -0,0 +1,46 @@ +/* + * + * Copyright (c) 2020 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. + */ + +/** + * @file + * Platform-specific configuration overrides for the CHIP Inet + * Layer on Zephyr platform. + * + */ + +#pragma once + +// ==================== Platform Adaptations ==================== + +#ifndef INET_CONFIG_ENABLE_IPV4 +#error Inet IPv4 configuration should be configured at build generation time +#endif + +// ========== Platform-specific Configuration Overrides ========= + +#ifndef INET_CONFIG_NUM_TCP_ENDPOINTS +#define INET_CONFIG_NUM_TCP_ENDPOINTS 4 +#endif // INET_CONFIG_NUM_TCP_ENDPOINTS + +#ifndef INET_CONFIG_NUM_UDP_ENDPOINTS +#define INET_CONFIG_NUM_UDP_ENDPOINTS 4 +#endif // INET_CONFIG_NUM_UDP_ENDPOINTS + +// Although Zephyr defines SO_BINDTODEVICE, it's currently unimplemented +#ifdef HAVE_SO_BINDTODEVICE +#undef HAVE_SO_BINDTODEVICE +#endif // HAVE_SO_BINDTODEVICE diff --git a/src/platform/Zephyr/SystemPlatformConfig.h b/src/platform/Zephyr/SystemPlatformConfig.h new file mode 100644 index 00000000000000..6fa52c1728c093 --- /dev/null +++ b/src/platform/Zephyr/SystemPlatformConfig.h @@ -0,0 +1,58 @@ +/* + * + * Copyright (c) 2020 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. + */ + +/** + * @file + * Platform-specific configuration overrides for the CHIP System + * Layer on Zephyr platform. + * + */ + +#pragma once + +#include + +namespace chip { +namespace DeviceLayer { +struct ChipDeviceEvent; +} // namespace DeviceLayer +} // namespace chip + +// ==================== Platform Adaptations ==================== + +#define CHIP_SYSTEM_CONFIG_POSIX_LOCKING 1 +#define CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING 0 +#define CHIP_SYSTEM_CONFIG_NO_LOCKING 0 + +#ifndef CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS +#define CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS 0 +#endif // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS + +#if CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS +#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME 0 +#else // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS +#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME 1 +#endif // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS + +#define CHIP_SYSTEM_CONFIG_USE_LWIP 0 +#define CHIP_SYSTEM_CONFIG_USE_SOCKETS 1 + +// ========== Platform-specific Configuration Overrides ========= + +#ifndef CHIP_SYSTEM_CONFIG_NUM_TIMERS +#define CHIP_SYSTEM_CONFIG_NUM_TIMERS 16 +#endif // CHIP_SYSTEM_CONFIG_NUM_TIMERS diff --git a/src/platform/Zephyr/args.gni b/src/platform/Zephyr/args.gni new file mode 100644 index 00000000000000..cc9de74cddd561 --- /dev/null +++ b/src/platform/Zephyr/args.gni @@ -0,0 +1,5 @@ +chip_device_platform = "zephyr" + +declare_args() { + chip_malloc_sys_heap = false +} diff --git a/src/platform/device.gni b/src/platform/device.gni index 17f2551def2b43..ec758393b98664 100755 --- a/src/platform/device.gni +++ b/src/platform/device.gni @@ -16,7 +16,7 @@ import("//build_overrides/chip.gni") import("${chip_root}/src/ble/ble.gni") declare_args() { - # Device platform layer: cc13x2_26x2, darwin, efr32, esp32, external, freertos, linux, nrfconnect, k32w0, qpg, tizen, cyw30739, bl602, none. + # Device platform layer: cc13x2_26x2, darwin, efr32, esp32, external, freertos, linux, nrfconnect, k32w0, qpg, tizen, cyw30739, bl602, zephyr, none. chip_device_platform = "auto" chip_platform_target = "" @@ -38,6 +38,8 @@ if (chip_device_platform == "auto") { chip_device_platform = "android" } else if (current_os == "webos") { chip_device_platform = "webos" + } else if (current_os == "zephyr") { + chip_device_platform = "zephyr" } else { chip_device_platform = "none" } @@ -125,6 +127,8 @@ if (chip_device_platform == "cc13x2_26x2") { _chip_device_layer = "CYW30739" } else if (chip_device_platform == "webos") { _chip_device_layer = "webos" +} else if (chip_device_platform == "zephyr") { + _chip_device_layer = "Zephyr" } if (chip_device_platform != "external") { @@ -182,5 +186,6 @@ assert( chip_device_platform == "telink" || chip_device_platform == "mbed" || chip_device_platform == "p6" || chip_device_platform == "android" || chip_device_platform == "ameba" || chip_device_platform == "cyw30739" || - chip_device_platform == "webos" || chip_device_platform == "bl602", + chip_device_platform == "webos" || chip_device_platform == "bl602" || + chip_device_platform == "zephyr", "Please select a valid value for chip_device_platform")