|
| 1 | +# |
| 2 | +# Copyright (c) 2021 Project CHIP Authors |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +# |
| 18 | +# Create CMake target for building Matter OTA (Over-the-air update) image. |
| 19 | +# |
| 20 | +# Required arguments: |
| 21 | +# INPUT_FILES file1[, file2...] Binary files to be included in Matter OTA image |
| 22 | +# OUTPUT_FILE file Location of generated Matter OTA image |
| 23 | +# |
| 24 | +function(chip_ota_image TARGET_NAME) |
| 25 | + cmake_parse_arguments(ARG "" "OUTPUT_FILE" "INPUT_FILES" ${ARGN}) |
| 26 | + |
| 27 | + if(NOT ARG_INPUT_FILES OR NOT ARG_OUTPUT_FILE) |
| 28 | + message(FATAL_ERROR "Both INPUT_FILES and OUTPUT_FILE arguments must be specified") |
| 29 | + endif() |
| 30 | + |
| 31 | + sysbuild_get(CONFIG_CHIP_DEVICE_SOFTWARE_VERSION IMAGE ${DEFAULT_IMAGE} VAR CONFIG_CHIP_DEVICE_SOFTWARE_VERSION KCONFIG) |
| 32 | + sysbuild_get(CONFIG_CHIP_DEVICE_SOFTWARE_VERSION_STRING IMAGE ${DEFAULT_IMAGE} VAR CONFIG_CHIP_DEVICE_SOFTWARE_VERSION_STRING KCONFIG) |
| 33 | + sysbuild_get(CONFIG_CHIP_DEVICE_VENDOR_ID IMAGE ${DEFAULT_IMAGE} VAR CONFIG_CHIP_DEVICE_VENDOR_ID KCONFIG) |
| 34 | + sysbuild_get(CONFIG_CHIP_DEVICE_PRODUCT_ID IMAGE ${DEFAULT_IMAGE} VAR CONFIG_CHIP_DEVICE_PRODUCT_ID KCONFIG) |
| 35 | + |
| 36 | + # Prepare ota_image_tool.py argument list |
| 37 | + if(EXISTS ${APP_DIR}/VERSION) |
| 38 | + file(READ ${APP_DIR}/VERSION ver) |
| 39 | + set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${APP_DIR}/VERSION) |
| 40 | + string(REGEX MATCH "VERSION_MAJOR = ([0-9]*)" _ ${ver}) |
| 41 | + set(app_version_major ${CMAKE_MATCH_1}) |
| 42 | + string(REGEX MATCH "VERSION_MINOR = ([0-9]*)" _ ${ver}) |
| 43 | + set(app_version_minor ${CMAKE_MATCH_1}) |
| 44 | + string(REGEX MATCH "PATCHLEVEL = ([0-9]*)" _ ${ver}) |
| 45 | + set(app_version_patchlevel ${CMAKE_MATCH_1}) |
| 46 | + string(REGEX MATCH "VERSION_TWEAK = ([0-9]*)" _ ${ver}) |
| 47 | + set(app_version_tweak ${CMAKE_MATCH_1}) |
| 48 | + |
| 49 | + set(APP_VERSION_STRING "${app_version_major}.${app_version_minor}.${app_version_patchlevel}+${app_version_tweak}") |
| 50 | + math(EXPR APPVERSION "(${app_version_major} << 24) | (${app_version_minor} << 16) | (${app_version_patchlevel} << 8) | ${app_version_tweak}" OUTPUT_FORMAT HEXADECIMAL) |
| 51 | + |
| 52 | + set(OTA_ARGS |
| 53 | + "--vendor-id" |
| 54 | + ${CONFIG_CHIP_DEVICE_VENDOR_ID} |
| 55 | + "--product-id" |
| 56 | + ${CONFIG_CHIP_DEVICE_PRODUCT_ID} |
| 57 | + "--version" |
| 58 | + ${APPVERSION} |
| 59 | + "--version-str" |
| 60 | + ${APP_VERSION_STRING} |
| 61 | + "--digest-algorithm" |
| 62 | + "sha256" |
| 63 | + ) |
| 64 | + else() |
| 65 | + set(OTA_ARGS |
| 66 | + "--vendor-id" |
| 67 | + ${CONFIG_CHIP_DEVICE_VENDOR_ID} |
| 68 | + "--product-id" |
| 69 | + ${CONFIG_CHIP_DEVICE_PRODUCT_ID} |
| 70 | + "--version" |
| 71 | + ${CONFIG_CHIP_DEVICE_SOFTWARE_VERSION} |
| 72 | + "--version-str" |
| 73 | + ${CONFIG_CHIP_DEVICE_SOFTWARE_VERSION_STRING} |
| 74 | + "--digest-algorithm" |
| 75 | + "sha256" |
| 76 | + ) |
| 77 | + endif() |
| 78 | + |
| 79 | + separate_arguments(OTA_EXTRA_ARGS NATIVE_COMMAND "${CHIP_OTA_IMAGE_EXTRA_ARGS}") |
| 80 | + |
| 81 | + list(APPEND OTA_ARGS ${OTA_EXTRA_ARGS}) |
| 82 | + list(APPEND OTA_ARGS ${ARG_INPUT_FILES}) |
| 83 | + list(APPEND OTA_ARGS ${ARG_OUTPUT_FILE}) |
| 84 | + |
| 85 | + # Convert the argument list to multi-line string |
| 86 | + string(REPLACE ";" "\n" OTA_ARGS "${OTA_ARGS}") |
| 87 | + |
| 88 | + # Pass the argument list via file to avoid hitting Windows command-line length limit |
| 89 | + file(GENERATE |
| 90 | + OUTPUT ${ARG_OUTPUT_FILE}.args |
| 91 | + CONTENT ${OTA_ARGS} |
| 92 | + ) |
| 93 | + |
| 94 | + add_custom_command(OUTPUT ${ARG_OUTPUT_FILE} |
| 95 | + COMMAND ${Python3_EXECUTABLE} ${ZEPHYR_CONNECTEDHOMEIP_MODULE_DIR}/src/app/ota_image_tool.py create @${ARG_OUTPUT_FILE}.args |
| 96 | + DEPENDS ${ARG_INPUT_FILES} ${ZEPHYR_CONNECTEDHOMEIP_MODULE_DIR}/src/app/ota_image_tool.py |
| 97 | + ) |
| 98 | + |
| 99 | + add_custom_target(${TARGET_NAME} ALL |
| 100 | + DEPENDS ${ARG_OUTPUT_FILE} |
| 101 | + ) |
| 102 | +endfunction() |
0 commit comments