From e2cd3775fcc2286f9444e4982ac25a2761ea553f Mon Sep 17 00:00:00 2001 From: Tomasz Duda Date: Wed, 8 May 2024 19:54:08 +0200 Subject: [PATCH] use new ota structure --- esphome/components/md5/md5.h | 6 ++ esphome/components/zephyr/__init__.py | 32 +++---- esphome/components/zephyr_mcumgr/__init__.py | 20 ----- .../components/zephyr_mcumgr/ota/__init__.py | 87 +++++++++++++++++++ .../ota_zephyr_mcumgr.cpp} | 24 +++-- .../ota_zephyr_mcumgr.h} | 6 +- .../components/zephyr_ota_mcumgr/__init__.py | 14 --- esphome/components/zephyr_uart/__init__.py | 3 +- tests/test12.2.yaml | 48 +++++----- 9 files changed, 153 insertions(+), 87 deletions(-) delete mode 100644 esphome/components/zephyr_mcumgr/__init__.py create mode 100644 esphome/components/zephyr_mcumgr/ota/__init__.py rename esphome/components/{zephyr_ota_mcumgr/ota_component.cpp => zephyr_mcumgr/ota_zephyr_mcumgr.cpp} (83%) rename esphome/components/{zephyr_ota_mcumgr/ota_component.h => zephyr_mcumgr/ota_zephyr_mcumgr.h} (81%) delete mode 100644 esphome/components/zephyr_ota_mcumgr/__init__.py diff --git a/esphome/components/md5/md5.h b/esphome/components/md5/md5.h index 4ec8a8a12c..adba21e5df 100644 --- a/esphome/components/md5/md5.h +++ b/esphome/components/md5/md5.h @@ -27,6 +27,12 @@ #define MD5_CTX_TYPE LT_MD5_CTX_T #endif +#if defined(USE_ZEPHYR) +// TODO implement +#include +#define MD5_CTX_TYPE hash_ctx +#endif + namespace esphome { namespace md5 { diff --git a/esphome/components/zephyr/__init__.py b/esphome/components/zephyr/__init__.py index c59bd1839d..ce4a106ec4 100644 --- a/esphome/components/zephyr/__init__.py +++ b/esphome/components/zephyr/__init__.py @@ -173,22 +173,22 @@ def copy_files(): CORE.data[KEY_ZEPHYR][KEY_OVERLAY], ) - write_file_if_changed( - CORE.relative_build_path("zephyr/child_image/mcuboot.conf"), - """ -CONFIG_MCUBOOT_SERIAL=y -CONFIG_BOOT_SERIAL_PIN_RESET=y -CONFIG_UART_CONSOLE=n -CONFIG_BOOT_SERIAL_ENTRANCE_GPIO=n -CONFIG_BOOT_SERIAL_CDC_ACM=y -CONFIG_UART_NRFX=n -CONFIG_LOG=n -CONFIG_ASSERT_VERBOSE=n -CONFIG_BOOT_BANNER=n -CONFIG_PRINTK=n -CONFIG_CBPRINTF_LIBC_SUBSTS=n -""", - ) + # write_file_if_changed( + # CORE.relative_build_path("zephyr/child_image/mcuboot.conf"), + # """ + # CONFIG_MCUBOOT_SERIAL=y + # CONFIG_BOOT_SERIAL_PIN_RESET=y + # CONFIG_UART_CONSOLE=n + # CONFIG_BOOT_SERIAL_ENTRANCE_GPIO=n + # CONFIG_BOOT_SERIAL_CDC_ACM=y + # CONFIG_UART_NRFX=n + # CONFIG_LOG=n + # CONFIG_ASSERT_VERBOSE=n + # CONFIG_BOOT_BANNER=n + # CONFIG_PRINTK=n + # CONFIG_CBPRINTF_LIBC_SUBSTS=n + # """, + # ) if CORE.data[KEY_ZEPHYR][KEY_BOOTLOADER] == BOOTLOADER_MCUBOOT: fake_board_manifest = """ diff --git a/esphome/components/zephyr_mcumgr/__init__.py b/esphome/components/zephyr_mcumgr/__init__.py deleted file mode 100644 index eeb96c7748..0000000000 --- a/esphome/components/zephyr_mcumgr/__init__.py +++ /dev/null @@ -1,20 +0,0 @@ -from esphome.components.zephyr import zephyr_add_prj_conf - - -async def to_code(config): - zephyr_add_prj_conf("NET_BUF", True) - zephyr_add_prj_conf("ZCBOR", True) - zephyr_add_prj_conf("MCUMGR", True) - - zephyr_add_prj_conf("MCUMGR_GRP_IMG", True) - - zephyr_add_prj_conf("IMG_MANAGER", True) - zephyr_add_prj_conf("STREAM_FLASH", True) - zephyr_add_prj_conf("FLASH_MAP", True) - zephyr_add_prj_conf("FLASH", True) - - zephyr_add_prj_conf("BOOTLOADER_MCUBOOT", True) - - zephyr_add_prj_conf("MCUMGR_MGMT_NOTIFICATION_HOOKS", True) - zephyr_add_prj_conf("MCUMGR_GRP_IMG_STATUS_HOOKS", True) - zephyr_add_prj_conf("MCUMGR_GRP_IMG_UPLOAD_CHECK_HOOK", True) diff --git a/esphome/components/zephyr_mcumgr/ota/__init__.py b/esphome/components/zephyr_mcumgr/ota/__init__.py new file mode 100644 index 0000000000..90fc4d4a1d --- /dev/null +++ b/esphome/components/zephyr_mcumgr/ota/__init__.py @@ -0,0 +1,87 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components.ota import BASE_OTA_SCHEMA, ota_to_code +from esphome.const import ( + CONF_ID, + CONF_NUM_ATTEMPTS, + CONF_OTA, + CONF_REBOOT_TIMEOUT, +) +from esphome.core import CORE, coroutine_with_priority +import esphome.final_validate as fv +from esphome.components.zephyr.const import BOOTLOADER_MCUBOOT +from esphome.components.zephyr import zephyr_add_prj_conf + +DEPENDENCIES = ["zephyr_ble_server"] +AUTO_LOAD = ["zephyr_mcumgr"] + +esphome = cg.esphome_ns.namespace("esphome") +ZephyrMcumgrOTAComponent = cg.esphome_ns.namespace("zephyr_mcumgr").class_( + "OTAComponent", cg.Component +) + +CONFIG_SCHEMA = ( + cv.Schema( + { + cv.GenerateID(): cv.declare_id(ZephyrMcumgrOTAComponent), + cv.Optional( + CONF_REBOOT_TIMEOUT, default="5min" + ): cv.positive_time_period_milliseconds, + cv.Optional(CONF_NUM_ATTEMPTS, default="10"): cv.positive_not_null_int, + } + ) + .extend(BASE_OTA_SCHEMA) + .extend(cv.COMPONENT_SCHEMA) +) + + +def _validate_mcumgr(config): + if CORE.using_zephyr: + fconf = fv.full_config.get() + try: + bootloader = fconf.get_config_for_path(["nrf52", "bootloader"]) + if bootloader != BOOTLOADER_MCUBOOT: + raise cv.Invalid(f"'{bootloader}' bootloader does not support OTA") + except KeyError: + pass + + +FINAL_VALIDATE_SCHEMA = _validate_mcumgr + +# TODO cdc ota, check if ble server is enabled + + +@coroutine_with_priority(50.0) +async def to_code(config): + CORE.data[CONF_OTA] = {} + + var = cg.new_Pvariable(config[CONF_ID]) + await ota_to_code(var, config) + cg.add_define("USE_OTA") + + await cg.register_component(var, config) + # mcumgr begin + zephyr_add_prj_conf("NET_BUF", True) + zephyr_add_prj_conf("ZCBOR", True) + zephyr_add_prj_conf("MCUMGR", True) + + zephyr_add_prj_conf("MCUMGR_GRP_IMG", True) + + zephyr_add_prj_conf("IMG_MANAGER", True) + zephyr_add_prj_conf("STREAM_FLASH", True) + zephyr_add_prj_conf("FLASH_MAP", True) + zephyr_add_prj_conf("FLASH", True) + + zephyr_add_prj_conf("BOOTLOADER_MCUBOOT", True) + + zephyr_add_prj_conf("MCUMGR_MGMT_NOTIFICATION_HOOKS", True) + zephyr_add_prj_conf("MCUMGR_GRP_IMG_STATUS_HOOKS", True) + zephyr_add_prj_conf("MCUMGR_GRP_IMG_UPLOAD_CHECK_HOOK", True) + # mcumgr ble + zephyr_add_prj_conf("MCUMGR_TRANSPORT_BT", True) + zephyr_add_prj_conf("MCUMGR_TRANSPORT_BT_REASSEMBLY", True) + + zephyr_add_prj_conf("MCUMGR_GRP_OS", True) + zephyr_add_prj_conf("MCUMGR_GRP_OS_MCUMGR_PARAMS", True) + + zephyr_add_prj_conf("NCS_SAMPLE_MCUMGR_BT_OTA_DFU_SPEEDUP", True) diff --git a/esphome/components/zephyr_ota_mcumgr/ota_component.cpp b/esphome/components/zephyr_mcumgr/ota_zephyr_mcumgr.cpp similarity index 83% rename from esphome/components/zephyr_ota_mcumgr/ota_component.cpp rename to esphome/components/zephyr_mcumgr/ota_zephyr_mcumgr.cpp index 2fa9c447fa..3169701544 100644 --- a/esphome/components/zephyr_ota_mcumgr/ota_component.cpp +++ b/esphome/components/zephyr_mcumgr/ota_zephyr_mcumgr.cpp @@ -1,5 +1,5 @@ #ifdef USE_ZEPHYR -#include "ota_component.h" +#include "ota_zephyr_mcumgr.h" #include "esphome/core/defines.h" #include "esphome/core/log.h" #include "esphome/core/hal.h" @@ -18,9 +18,10 @@ struct img_mgmt_upload_req { }; namespace esphome { -namespace zephyr_ota_mcumgr { +namespace zephyr_mcumgr { -static const char *const TAG = "zephyr_ota_mcumgr"; +static const char *const TAG = "zephyr_mcumgr"; +static OTAComponent *global_ota_component = nullptr; #define IMAGE_HASH_LEN 32 /* Size of SHA256 TLV hash */ @@ -28,13 +29,13 @@ static enum mgmt_cb_return mcumgr_img_mgmt_cb(uint32_t event, enum mgmt_cb_retur uint16_t *group, bool *abort_more, void *data, size_t data_size) { if (MGMT_EVT_OP_IMG_MGMT_DFU_CHUNK == event) { const img_mgmt_upload_check &upload = *static_cast(data); - static_cast(ota::global_ota_component)->update_chunk(upload); + static_cast(global_ota_component)->update_chunk(upload); } else if (MGMT_EVT_OP_IMG_MGMT_DFU_STARTED == event) { - static_cast(ota::global_ota_component)->update_started(); + static_cast(global_ota_component)->update_started(); } else if (MGMT_EVT_OP_IMG_MGMT_DFU_CHUNK_WRITE_COMPLETE == event) { - static_cast(ota::global_ota_component)->update_chunk_wrote(); + static_cast(global_ota_component)->update_chunk_wrote(); } else if (MGMT_EVT_OP_IMG_MGMT_DFU_PENDING == event) { - static_cast(ota::global_ota_component)->update_pending(); + static_cast(global_ota_component)->update_pending(); } else { ESP_LOGD(TAG, "MCUmgr Image Management Event with the %d ID", u32_count_trailing_zeros(MGMT_EVT_GET_ID(event))); } @@ -46,7 +47,12 @@ static struct mgmt_callback IMG_MGMT_CALLBACK = { .event_id = MGMT_EVT_OP_IMG_MGMT_ALL, }; -OTAComponent::OTAComponent() { ota::global_ota_component = this; } +OTAComponent::OTAComponent() { + global_ota_component = this; +#ifdef USE_OTA_STATE_CALLBACK + ota::register_ota_platform(this); +#endif +} void OTAComponent::setup() { mgmt_callback_register(&IMG_MGMT_CALLBACK); } @@ -114,6 +120,6 @@ void OTAComponent::update_pending() { #endif } -} // namespace zephyr_ota_mcumgr +} // namespace zephyr_mcumgr } // namespace esphome #endif diff --git a/esphome/components/zephyr_ota_mcumgr/ota_component.h b/esphome/components/zephyr_mcumgr/ota_zephyr_mcumgr.h similarity index 81% rename from esphome/components/zephyr_ota_mcumgr/ota_component.h rename to esphome/components/zephyr_mcumgr/ota_zephyr_mcumgr.h index 65babfebf5..72f12f674d 100644 --- a/esphome/components/zephyr_ota_mcumgr/ota_component.h +++ b/esphome/components/zephyr_mcumgr/ota_zephyr_mcumgr.h @@ -1,11 +1,11 @@ #pragma once #ifdef USE_ZEPHYR -#include "esphome/components/ota/ota_component.h" +#include "esphome/components/ota/ota_backend.h" struct img_mgmt_upload_check; namespace esphome { -namespace zephyr_ota_mcumgr { +namespace zephyr_mcumgr { class OTAComponent : public ota::OTAComponent { public: @@ -24,6 +24,6 @@ class OTAComponent : public ota::OTAComponent { bool is_confirmed_ = false; }; -} // namespace zephyr_ota_mcumgr +} // namespace zephyr_mcumgr } // namespace esphome #endif diff --git a/esphome/components/zephyr_ota_mcumgr/__init__.py b/esphome/components/zephyr_ota_mcumgr/__init__.py deleted file mode 100644 index 5a2ade9872..0000000000 --- a/esphome/components/zephyr_ota_mcumgr/__init__.py +++ /dev/null @@ -1,14 +0,0 @@ -from esphome.components.zephyr import zephyr_add_prj_conf - -DEPENDENCIES = ["zephyr_ble_server"] -AUTO_LOAD = ["zephyr_mcumgr"] - - -async def to_code(config): - zephyr_add_prj_conf("MCUMGR_TRANSPORT_BT", True) - zephyr_add_prj_conf("MCUMGR_TRANSPORT_BT_REASSEMBLY", True) - - zephyr_add_prj_conf("MCUMGR_GRP_OS", True) - zephyr_add_prj_conf("MCUMGR_GRP_OS_MCUMGR_PARAMS", True) - - zephyr_add_prj_conf("NCS_SAMPLE_MCUMGR_BT_OTA_DFU_SPEEDUP", True) diff --git a/esphome/components/zephyr_uart/__init__.py b/esphome/components/zephyr_uart/__init__.py index de08e334b1..a57079ba25 100644 --- a/esphome/components/zephyr_uart/__init__.py +++ b/esphome/components/zephyr_uart/__init__.py @@ -4,7 +4,8 @@ from esphome.components.zephyr import zephyr_add_prj_conf, zephyr_add_overlay def zephyr_add_cdc_acm(config): zephyr_add_prj_conf("USB_DEVICE_STACK", True) zephyr_add_prj_conf("USB_CDC_ACM", True) - # prevent device to go to susspend + # prevent device to go to susspend, without this communication stop working in python + # there should be a way to solve it zephyr_add_prj_conf("USB_DEVICE_REMOTE_WAKEUP", False) zephyr_add_overlay( """ diff --git a/tests/test12.2.yaml b/tests/test12.2.yaml index 70424ede98..5bfe0dbc4f 100644 --- a/tests/test12.2.yaml +++ b/tests/test12.2.yaml @@ -44,30 +44,30 @@ dfu: reset_output: rest_gpio ota: - safe_mode: true - on_begin: - then: - - logger.log: "OTA start" - on_progress: - then: - - logger.log: - format: "OTA progress %0.1f%%" - args: ["x"] - on_end: - then: - - logger.log: "OTA end" - on_error: - then: - - logger.log: - format: "OTA update error %d" - args: ["x"] - on_state_change: - then: - - if: - condition: - lambda: return state == ota::OTA_STARTED; - then: - - logger.log: "OTA start" + - platform: zephyr_mcumgr + on_begin: + then: + - logger.log: "OTA start" + on_progress: + then: + - logger.log: + format: "OTA progress %0.1f%%" + args: ["x"] + on_end: + then: + - logger.log: "OTA end" + on_error: + then: + - logger.log: + format: "OTA update error %d" + args: ["x"] + on_state_change: + then: + - if: + condition: + lambda: return state == ota::OTA_STARTED; + then: + - logger.log: "OTA start" zephyr_ble_server: