dfu: download writes partition data

This commit is contained in:
King Kévin 2022-07-23 13:02:21 +02:00
parent 4a53aab483
commit 06025a2b33
1 changed files with 52 additions and 0 deletions

View File

@ -210,6 +210,34 @@ void tud_dfu_download_cb(uint8_t alt, uint16_t block_num, uint8_t const* data, u
return;
}
// get handle for OTA update
if (0 == ota_handle) {
const esp_partition_t *ota_part = esp_ota_get_next_update_partition(NULL);
if (NULL == ota_part) {
ESP_LOGE(TAG, "OTA not found");
tud_dfu_finish_flashing(DFU_STATUS_ERR_PROG);
return;
}
rc = esp_ota_begin(ota_part, OTA_SIZE_UNKNOWN, &ota_handle);
if (ESP_OK != rc) {
ESP_LOGE(TAG, "init OTA failed");
esp_ota_abort(ota_handle);
ota_handle = 0;
tud_dfu_finish_flashing(DFU_STATUS_ERR_PROG);
return;
}
}
// write data to partition
rc = esp_ota_write_with_offset(ota_handle, data, length, block_num * CFG_TUD_DFU_XFER_BUFSIZE);
if (ESP_OK != rc) {
ESP_LOGE(TAG, "writing OTA failed");
esp_ota_abort(ota_handle);
ota_handle = 0;
tud_dfu_finish_flashing(DFU_STATUS_ERR_PROG);
return;
}
// flashing op for download complete without error
tud_dfu_finish_flashing(DFU_STATUS_OK);
}
@ -223,6 +251,30 @@ void tud_dfu_manifest_cb(uint8_t alt)
esp_err_t rc;
ESP_LOGI(TAG, "download completed, enter manifestation");
// finish flashing
if (ota_handle) {
rc = esp_ota_end(ota_handle);
ota_handle = 0;
if (ESP_OK != rc) {
ESP_LOGE(TAG, "close OTA failed");
tud_dfu_finish_flashing(DFU_STATUS_ERR_PROG);
return;
}
}
// switch to OTA app
const esp_partition_t *ota = esp_ota_get_next_update_partition(NULL);
esp_app_desc_t ota_desc;
esp_ota_get_partition_description(ota, &ota_desc);
if (ESP_APP_DESC_MAGIC_WORD == ota_desc.magic_word) {
ESP_LOGI(TAG, "boot set to valid app");
esp_ota_set_boot_partition(ota);
} else {
ESP_LOGI(TAG, "no valid app");
tud_dfu_finish_flashing(DFU_STATUS_ERR_VERIFY);
return;
}
// flashing op for manifest is complete without error
// Application can perform checksum, should it fail, use appropriate status such as errVERIFY.
tud_dfu_finish_flashing(DFU_STATUS_OK);