ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
ota_zephyr_mcumgr.cpp
Go to the documentation of this file.
1#ifdef USE_ZEPHYR
2#include "ota_zephyr_mcumgr.h"
3#include "esphome/core/log.h"
4#include "esphome/core/hal.h"
5#include <zephyr/sys/math_extras.h>
7#include <zephyr/dfu/mcuboot.h>
8
9// It should be from below header but there is problem with internal includes.
10// #include <zephyr/mgmt/mcumgr/grp/img_mgmt/img_mgmt.h>
11// NOLINTBEGIN(readability-identifier-naming,google-runtime-int)
12struct img_mgmt_upload_action {
14 unsigned long long size;
15};
16
17struct img_mgmt_upload_req {
18 uint32_t image; /* 0 by default */
19 size_t off; /* SIZE_MAX if unspecified */
20};
21// NOLINTEND(readability-identifier-naming,google-runtime-int)
22
24
25static_assert(sizeof(struct img_mgmt_upload_action) == 8, "ABI mismatch");
26static_assert(sizeof(struct img_mgmt_upload_req) == 8, "ABI mismatch");
27static_assert(offsetof(struct img_mgmt_upload_req, image) == 0, "ABI mismatch");
28static_assert(offsetof(struct img_mgmt_upload_req, off) == 4, "ABI mismatch");
29
30ESPHOME_LOG_TAG(TAG, "zephyr_mcumgr");
31static OTAComponent *global_ota_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
32
33static enum mgmt_cb_return mcumgr_img_mgmt_cb(uint32_t event, enum mgmt_cb_return prev_status, int32_t *rc,
34 uint16_t *group, bool *abort_more, void *data, size_t data_size) {
35 if (MGMT_EVT_OP_IMG_MGMT_DFU_CHUNK == event) {
36 const img_mgmt_upload_check &upload = *static_cast<img_mgmt_upload_check *>(data);
37 global_ota_component->update_chunk(upload);
38 } else if (MGMT_EVT_OP_IMG_MGMT_DFU_STARTED == event) {
39 global_ota_component->update_started();
40 } else if (MGMT_EVT_OP_IMG_MGMT_DFU_CHUNK_WRITE_COMPLETE == event) {
41 global_ota_component->update_chunk_wrote();
42 } else if (MGMT_EVT_OP_IMG_MGMT_DFU_PENDING == event) {
43 global_ota_component->update_pending();
44 } else if (MGMT_EVT_OP_IMG_MGMT_DFU_STOPPED == event) {
45 global_ota_component->update_stopped();
46 } else {
47 ESP_LOGD(TAG, "MCUmgr Image Management Event with the %d ID", u32_count_trailing_zeros(MGMT_EVT_GET_ID(event)));
48 }
49 return MGMT_CB_OK;
50}
51
52OTAComponent::OTAComponent() { global_ota_component = this; }
53
55 this->img_mgmt_callback_.callback = mcumgr_img_mgmt_cb;
56 this->img_mgmt_callback_.event_id = MGMT_EVT_OP_IMG_MGMT_ALL;
57 mgmt_callback_register(&this->img_mgmt_callback_);
58#if defined(CONFIG_USB_DEVICE_STACK) || defined(CONFIG_USB_DEVICE_STACK_NEXT)
59 if (int err = zephyr::usb_device_enable(); err != 0)
60 ESP_LOGE(TAG, "USB device setup failed: %d", err);
61#endif
62// Handle OTA rollback: mark partition valid immediately unless USE_OTA_ROLLBACK is enabled,
63// in which case safe_mode will mark it valid after confirming successful boot.
64#ifndef USE_OTA_ROLLBACK
65 if (!boot_is_img_confirmed()) {
66 boot_write_img_confirmed();
67 }
68#endif
69}
70
71#ifdef ESPHOME_LOG_HAS_CONFIG
72static const char *swap_type_str(uint8_t type) {
73 switch (type) {
74 case BOOT_SWAP_TYPE_NONE:
75 return "none";
76 case BOOT_SWAP_TYPE_TEST:
77 return "test";
78 case BOOT_SWAP_TYPE_PERM:
79 return "perm";
80 case BOOT_SWAP_TYPE_REVERT:
81 return "revert";
82 case BOOT_SWAP_TYPE_FAIL:
83 return "fail";
84 default:
85 break;
86 }
87
88 return "unknown";
89}
90#endif
91
93 ESP_LOGCONFIG(TAG,
94 "Over-The-Air Updates:\n"
95 " swap type after reboot: %s\n"
96 " image confirmed: %s",
97 swap_type_str(mcuboot_swap_type()), YESNO(boot_is_img_confirmed()));
98}
99
100void OTAComponent::update_chunk(const img_mgmt_upload_check &upload) {
101 float percentage = (upload.req->off * 100.0f) / upload.action->size;
102 this->defer([this, percentage]() { this->percentage_ = percentage; });
103}
104
106 this->defer([this]() {
107 ESP_LOGD(TAG, "Starting update");
108#ifdef USE_OTA_STATE_LISTENER
109 this->notify_state_(ota::OTA_STARTED, 0.0f, 0);
110#endif
111 });
112}
113
115 uint32_t now = millis();
116 if (now - this->last_progress_ > 1000) {
117 this->last_progress_ = now;
118 this->defer([this]() {
119 ESP_LOGD(TAG, "OTA in progress: %0.1f%%", this->percentage_);
120#ifdef USE_OTA_STATE_LISTENER
122#endif
123 });
124 }
125}
126
128 this->defer([this]() {
129 ESP_LOGD(TAG, "OTA pending");
130#ifdef USE_OTA_STATE_LISTENER
131 this->notify_state_(ota::OTA_COMPLETED, 100.0f, 0);
132#endif
133 });
134}
135
137 this->defer([this]() {
138 ESP_LOGD(TAG, "OTA stopped");
139#ifdef USE_OTA_STATE_LISTENER
140 this->notify_state_(ota::OTA_ERROR, 0.0f, static_cast<uint8_t>(ota::OTA_RESPONSE_ERROR_UNKNOWN));
141#endif
142 });
143}
144
145} // namespace esphome::zephyr_mcumgr
146#endif
void defer(const char *name, SchedulerCallback &&f)
Defer a callback to the next loop() call with a const char* name.
void notify_state_(OTAState state, float progress, uint8_t error)
void update_chunk(const img_mgmt_upload_check &upload)
uint8_t type
@ OTA_RESPONSE_ERROR_UNKNOWN
Definition ota_backend.h:55
ESPHOME_LOG_TAG(TAG, "zephyr_mcumgr")
int usb_device_enable()
Start the USB device on whichever Zephyr USB device stack the build uses (the legacy one or device_ne...
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t