1
0
mirror of https://github.com/esphome/esphome.git synced 2025-04-10 12:50:30 +01:00

use new ota structure

This commit is contained in:
Tomasz Duda 2024-05-08 19:54:08 +02:00
parent 0efa32dea9
commit e2cd3775fc
9 changed files with 153 additions and 87 deletions

View File

@ -27,6 +27,12 @@
#define MD5_CTX_TYPE LT_MD5_CTX_T #define MD5_CTX_TYPE LT_MD5_CTX_T
#endif #endif
#if defined(USE_ZEPHYR)
// TODO implement
#include <zephyr/crypto/crypto.h>
#define MD5_CTX_TYPE hash_ctx
#endif
namespace esphome { namespace esphome {
namespace md5 { namespace md5 {

View File

@ -173,22 +173,22 @@ def copy_files():
CORE.data[KEY_ZEPHYR][KEY_OVERLAY], CORE.data[KEY_ZEPHYR][KEY_OVERLAY],
) )
write_file_if_changed( # write_file_if_changed(
CORE.relative_build_path("zephyr/child_image/mcuboot.conf"), # CORE.relative_build_path("zephyr/child_image/mcuboot.conf"),
""" # """
CONFIG_MCUBOOT_SERIAL=y # CONFIG_MCUBOOT_SERIAL=y
CONFIG_BOOT_SERIAL_PIN_RESET=y # CONFIG_BOOT_SERIAL_PIN_RESET=y
CONFIG_UART_CONSOLE=n # CONFIG_UART_CONSOLE=n
CONFIG_BOOT_SERIAL_ENTRANCE_GPIO=n # CONFIG_BOOT_SERIAL_ENTRANCE_GPIO=n
CONFIG_BOOT_SERIAL_CDC_ACM=y # CONFIG_BOOT_SERIAL_CDC_ACM=y
CONFIG_UART_NRFX=n # CONFIG_UART_NRFX=n
CONFIG_LOG=n # CONFIG_LOG=n
CONFIG_ASSERT_VERBOSE=n # CONFIG_ASSERT_VERBOSE=n
CONFIG_BOOT_BANNER=n # CONFIG_BOOT_BANNER=n
CONFIG_PRINTK=n # CONFIG_PRINTK=n
CONFIG_CBPRINTF_LIBC_SUBSTS=n # CONFIG_CBPRINTF_LIBC_SUBSTS=n
""", # """,
) # )
if CORE.data[KEY_ZEPHYR][KEY_BOOTLOADER] == BOOTLOADER_MCUBOOT: if CORE.data[KEY_ZEPHYR][KEY_BOOTLOADER] == BOOTLOADER_MCUBOOT:
fake_board_manifest = """ fake_board_manifest = """

View File

@ -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)

View File

@ -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)

View File

@ -1,5 +1,5 @@
#ifdef USE_ZEPHYR #ifdef USE_ZEPHYR
#include "ota_component.h" #include "ota_zephyr_mcumgr.h"
#include "esphome/core/defines.h" #include "esphome/core/defines.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/core/hal.h" #include "esphome/core/hal.h"
@ -18,9 +18,10 @@ struct img_mgmt_upload_req {
}; };
namespace esphome { 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 */ #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) { uint16_t *group, bool *abort_more, void *data, size_t data_size) {
if (MGMT_EVT_OP_IMG_MGMT_DFU_CHUNK == event) { if (MGMT_EVT_OP_IMG_MGMT_DFU_CHUNK == event) {
const img_mgmt_upload_check &upload = *static_cast<img_mgmt_upload_check *>(data); const img_mgmt_upload_check &upload = *static_cast<img_mgmt_upload_check *>(data);
static_cast<OTAComponent *>(ota::global_ota_component)->update_chunk(upload); static_cast<OTAComponent *>(global_ota_component)->update_chunk(upload);
} else if (MGMT_EVT_OP_IMG_MGMT_DFU_STARTED == event) { } else if (MGMT_EVT_OP_IMG_MGMT_DFU_STARTED == event) {
static_cast<OTAComponent *>(ota::global_ota_component)->update_started(); static_cast<OTAComponent *>(global_ota_component)->update_started();
} else if (MGMT_EVT_OP_IMG_MGMT_DFU_CHUNK_WRITE_COMPLETE == event) { } else if (MGMT_EVT_OP_IMG_MGMT_DFU_CHUNK_WRITE_COMPLETE == event) {
static_cast<OTAComponent *>(ota::global_ota_component)->update_chunk_wrote(); static_cast<OTAComponent *>(global_ota_component)->update_chunk_wrote();
} else if (MGMT_EVT_OP_IMG_MGMT_DFU_PENDING == event) { } else if (MGMT_EVT_OP_IMG_MGMT_DFU_PENDING == event) {
static_cast<OTAComponent *>(ota::global_ota_component)->update_pending(); static_cast<OTAComponent *>(global_ota_component)->update_pending();
} else { } else {
ESP_LOGD(TAG, "MCUmgr Image Management Event with the %d ID", u32_count_trailing_zeros(MGMT_EVT_GET_ID(event))); 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, .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); } void OTAComponent::setup() { mgmt_callback_register(&IMG_MGMT_CALLBACK); }
@ -114,6 +120,6 @@ void OTAComponent::update_pending() {
#endif #endif
} }
} // namespace zephyr_ota_mcumgr } // namespace zephyr_mcumgr
} // namespace esphome } // namespace esphome
#endif #endif

View File

@ -1,11 +1,11 @@
#pragma once #pragma once
#ifdef USE_ZEPHYR #ifdef USE_ZEPHYR
#include "esphome/components/ota/ota_component.h" #include "esphome/components/ota/ota_backend.h"
struct img_mgmt_upload_check; struct img_mgmt_upload_check;
namespace esphome { namespace esphome {
namespace zephyr_ota_mcumgr { namespace zephyr_mcumgr {
class OTAComponent : public ota::OTAComponent { class OTAComponent : public ota::OTAComponent {
public: public:
@ -24,6 +24,6 @@ class OTAComponent : public ota::OTAComponent {
bool is_confirmed_ = false; bool is_confirmed_ = false;
}; };
} // namespace zephyr_ota_mcumgr } // namespace zephyr_mcumgr
} // namespace esphome } // namespace esphome
#endif #endif

View File

@ -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)

View File

@ -4,7 +4,8 @@ from esphome.components.zephyr import zephyr_add_prj_conf, zephyr_add_overlay
def zephyr_add_cdc_acm(config): def zephyr_add_cdc_acm(config):
zephyr_add_prj_conf("USB_DEVICE_STACK", True) zephyr_add_prj_conf("USB_DEVICE_STACK", True)
zephyr_add_prj_conf("USB_CDC_ACM", 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_prj_conf("USB_DEVICE_REMOTE_WAKEUP", False)
zephyr_add_overlay( zephyr_add_overlay(
""" """

View File

@ -44,30 +44,30 @@ dfu:
reset_output: rest_gpio reset_output: rest_gpio
ota: ota:
safe_mode: true - platform: zephyr_mcumgr
on_begin: on_begin:
then: then:
- logger.log: "OTA start" - logger.log: "OTA start"
on_progress: on_progress:
then: then:
- logger.log: - logger.log:
format: "OTA progress %0.1f%%" format: "OTA progress %0.1f%%"
args: ["x"] args: ["x"]
on_end: on_end:
then: then:
- logger.log: "OTA end" - logger.log: "OTA end"
on_error: on_error:
then: then:
- logger.log: - logger.log:
format: "OTA update error %d" format: "OTA update error %d"
args: ["x"] args: ["x"]
on_state_change: on_state_change:
then: then:
- if: - if:
condition: condition:
lambda: return state == ota::OTA_STARTED; lambda: return state == ota::OTA_STARTED;
then: then:
- logger.log: "OTA start" - logger.log: "OTA start"
zephyr_ble_server: zephyr_ble_server: