From dab30f957aca8f124bdf01fe77f2589dfef7e860 Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Mon, 7 Feb 2022 17:48:10 +0530 Subject: [PATCH] [ESP32] Automatically build OTA image based on flag --- config/esp32/components/chip/CMakeLists.txt | 12 ++++ config/esp32/components/chip/Kconfig | 18 +++++- config/esp32/components/chip/ota-image.cmake | 65 ++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 config/esp32/components/chip/ota-image.cmake diff --git a/config/esp32/components/chip/CMakeLists.txt b/config/esp32/components/chip/CMakeLists.txt index 2fcfd0a899cf58..1f59be4cfe7f08 100644 --- a/config/esp32/components/chip/CMakeLists.txt +++ b/config/esp32/components/chip/CMakeLists.txt @@ -29,6 +29,8 @@ if(NOT CHIP_ROOT) get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../.. REALPATH) endif() +include(${CMAKE_CURRENT_LIST_DIR}/ota-image.cmake) + set(CHIP_REQURIE_COMPONENTS freertos lwip bt mdns mbedtls fatfs app_update console openthread) if (NOT CMAKE_BUILD_EARLY_EXPANSION) @@ -246,3 +248,13 @@ if(CONFIG_ENABLE_PW_RPC) set(WRAP_FUNCTIONS esp_log_write) target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=${WRAP_FUNCTIONS}") endif() + +# Build Matter OTA image +if (CONFIG_CHIP_OTA_IMAGE_BUILD) + chip_ota_image(chip-ota-image + INPUT_FILES ${BUILD_DIR}/${CMAKE_PROJECT_NAME}.bin + OUTPUT_FILE ${BUILD_DIR}/${CMAKE_PROJECT_NAME}-ota.bin + ) + # Adding dependecy as app target so that this runs after images are ready + add_dependencies(chip-ota-image app) +endif() diff --git a/config/esp32/components/chip/Kconfig b/config/esp32/components/chip/Kconfig index 4fb3fe4fe63a48..63535664264835 100644 --- a/config/esp32/components/chip/Kconfig +++ b/config/esp32/components/chip/Kconfig @@ -287,7 +287,7 @@ menu "CHIP Device Layer" config DEVICE_SOFTWARE_VERSION string "Device Software Version String" - default "" + default "v1.0" help A string identifying the software version running on the device. @@ -768,4 +768,20 @@ menu "CHIP Device Layer" endmenu + menu "Matter OTA Image" + + config CHIP_OTA_IMAGE_BUILD + bool "Generate Matter OTA image" + help + Enable building OTA (Over-the-air update) image which includes Matter OTA header. + + config CHIP_OTA_IMAGE_EXTRA_ARGS + string "OTA image creator extra arguments" + depends on CHIP_OTA_IMAGE_BUILD + help + This option allows one to pass optional arguments to the ota_image_tool.py script, + used for building OTA image with Matter OTA header. + + endmenu + endmenu diff --git a/config/esp32/components/chip/ota-image.cmake b/config/esp32/components/chip/ota-image.cmake new file mode 100644 index 00000000000000..59646edcab08a6 --- /dev/null +++ b/config/esp32/components/chip/ota-image.cmake @@ -0,0 +1,65 @@ +# +# 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. +# + +find_package(Python3 REQUIRED) + +# +# Create CMake target for building Matter OTA (Over-the-air update) image. +# Required arguments: +# INPUT_FILES file1, [file2...] - binary files which Matter OTA image will be composed of +# OUTPUT_FILE file - where to store newly created Matter OTA image +# +function(chip_ota_image TARGET_NAME) + cmake_parse_arguments(ARG "" "OUTPUT_FILE" "INPUT_FILES" ${ARGN}) + + if (NOT ARG_INPUT_FILES OR NOT ARG_OUTPUT_FILE) + message(FATAL_ERROR "Both INPUT_FILES and OUTPUT_FILE arguments must be specified") + endif() + + # Prepare ota_image_tool.py argument list + set(OTA_ARGS + "--vendor-id" + ${CONFIG_DEVICE_VENDOR_ID} + "--product-id" + ${CONFIG_DEVICE_PRODUCT_ID} + "--version" + ${CONFIG_DEVICE_SOFTWARE_VERSION_NUMBER} + "--version-str" + ${CONFIG_DEVICE_SOFTWARE_VERSION} + "--digest-algorithm" + "sha256" + ) + + separate_arguments(OTA_EXTRA_ARGS NATIVE_COMMAND "${CHIP_OTA_IMAGE_EXTRA_ARGS}") + + list(APPEND OTA_ARGS ${OTA_EXTRA_ARGS}) + list(APPEND OTA_ARGS ${ARG_INPUT_FILES}) + list(APPEND OTA_ARGS ${ARG_OUTPUT_FILE}) + + # Convert the argument list to multi-line string + string(REPLACE ";" "\n" OTA_ARGS "${OTA_ARGS}") + + # Pass the argument list via file to avoid hitting Windows command-line length limit + file(GENERATE + OUTPUT ${BUILD_DIR}/args-ota-image.tmp + CONTENT ${OTA_ARGS} + ) + + add_custom_target(${TARGET_NAME} ALL + COMMAND ${Python3_EXECUTABLE} ${CHIP_ROOT}/src/app/ota_image_tool.py create @${BUILD_DIR}/args-ota-image.tmp + COMMAND ${CMAKE_COMMAND} -E remove ${BUILD_DIR}/args-ota-image.tmp + ) +endfunction()