new ota logic

This commit is contained in:
2026-03-15 19:57:19 +01:00
parent 07ab69075a
commit c61a586595
15 changed files with 313 additions and 400 deletions

View File

@@ -4,7 +4,8 @@ use crate::BOARD_ACCESS;
use edge_http::io::server::Connection;
use edge_http::Method;
use edge_nal::io::{Read, Write};
use log::info;
use esp_hal_ota::OtaError;
use log::{error, info};
pub(crate) async fn ota_operations<T, const N: usize>(
conn: &mut Connection<'_, T, { N }>,
@@ -28,27 +29,45 @@ where
Some(200)
}
Method::Post => {
let mut offset = 0_usize;
let size = read_up_to_bytes_from_request(conn, Some(4)).await?;
let flash_size = u32::from_le_bytes(size[..4].try_into().unwrap());
info!("flash size: {flash_size}");
let crc32 = read_up_to_bytes_from_request(conn, Some(4)).await?;
let target_crc = u32::from_le_bytes(crc32[..4].try_into().unwrap());
info!("crc32: {target_crc}");
BOARD_ACCESS
.get()
.await
.lock()
.await
.board_hal
.get_esp()
.ota
.ota_begin(flash_size, target_crc)?;
let mut chunk = 0;
loop {
let buf = read_up_to_bytes_from_request(conn, Some(4096)).await?;
if buf.is_empty() {
info!("file request for ota finished");
let mut board = BOARD_ACCESS.get().await.lock().await;
board.board_hal.get_esp().finalize_ota().await?;
break;
error!("Upload finished, but no enough data received.");
return Err(OtaError::WrongCRC)?;
} else {
let mut board = BOARD_ACCESS.get().await.lock().await;
board.board_hal.progress(chunk as u32).await;
// Erase next block if we are at a 4K boundary (including the first block at offset 0)
info!("erasing and writing block 0x{offset:x}");
board
.board_hal
.get_esp()
.write_ota(offset as u32, &buf)
.await?;
let finsihed = board.board_hal.get_esp().ota.ota_write_chunk(&buf)?;
if finsihed {
board.board_hal.get_esp().ota.ota_flush(true, true)?;
board.board_hal.get_esp().set_restart_to_conf(true);
break;
}
info!(
"Progress: {}%",
(board.board_hal.get_esp().ota.get_ota_progress() * 100.0) as u8
);
}
offset += buf.len();
chunk += 1;
}
BOARD_ACCESS