add image confirm

This commit is contained in:
Tomasz Duda 2024-02-13 19:47:08 +01:00
parent 9019416a05
commit 84a84b04a6
2 changed files with 37 additions and 1 deletions

View File

@ -4,6 +4,7 @@
#include "esphome/core/hal.h"
#include <zephyr/mgmt/mcumgr/mgmt/callbacks.h>
#include <zephyr/sys/math_extras.h>
#include <zephyr/dfu/mcuboot.h>
struct img_mgmt_upload_action {
/** The total size of the image. */
@ -20,6 +21,8 @@ namespace zephyr_ota_mcumgr {
static const char *const TAG = "zephyr_ota_mcumgr";
#define IMAGE_HASH_LEN 32 /* Size of SHA256 TLV hash */
static enum mgmt_cb_return mcumgr_img_mgmt_cb(uint32_t event, enum mgmt_cb_return prev_status, int32_t *rc,
uint16_t *group, bool *abort_more, void *data, size_t data_size) {
if (MGMT_EVT_OP_IMG_MGMT_DFU_CHUNK == event) {
@ -36,7 +39,7 @@ static enum mgmt_cb_return mcumgr_img_mgmt_cb(uint32_t event, enum mgmt_cb_retur
}
return MGMT_CB_OK;
}
static struct mgmt_callback img_mgmt_callback = {
.callback = mcumgr_img_mgmt_cb,
.event_id = MGMT_EVT_OP_IMG_MGMT_ALL,
@ -46,8 +49,39 @@ OTAComponent::OTAComponent() { ota::global_ota_component = this; }
void OTAComponent::setup() { mgmt_callback_register(&img_mgmt_callback); }
void OTAComponent::loop() {
if (false == _is_confirmed){
_is_confirmed = boot_is_img_confirmed();
if (false == _is_confirmed){
if(boot_write_img_confirmed()){
ESP_LOGD(TAG, "Unable to confirm image");
//TODO reboot
}
}
}
}
static const char *swap_type_str(uint8_t type) {
switch (type) {
case BOOT_SWAP_TYPE_NONE:
return "none";
case BOOT_SWAP_TYPE_TEST:
return "test";
case BOOT_SWAP_TYPE_PERM:
return "perm";
case BOOT_SWAP_TYPE_REVERT:
return "revert";
case BOOT_SWAP_TYPE_FAIL:
return "fail";
}
return "unknown";
}
void OTAComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Over-The-Air Updates:");
ESP_LOGCONFIG(TAG, " swap type after reboot: %s", swap_type_str(mcuboot_swap_type()));
ESP_LOGCONFIG(TAG, " image confirmed: %s", YESNO(boot_is_img_confirmed()));
}
void OTAComponent::update_chunk(const img_mgmt_upload_check &upload) {

View File

@ -11,6 +11,7 @@ class OTAComponent : public ota::OTAComponent {
public:
OTAComponent();
void setup() override;
void loop() override;
void dump_config() override;
void update_chunk(const img_mgmt_upload_check &upload);
void update_started();
@ -20,6 +21,7 @@ class OTAComponent : public ota::OTAComponent {
protected:
uint32_t _last_progress = 0;
float _percentage = 0;
bool _is_confirmed = false;
};
} // namespace zephyr_ota_mcumgr