fix some ota stuff

This commit is contained in:
2025-10-04 03:05:11 +02:00
parent 0ddf6a6886
commit 894be7c373
6 changed files with 66 additions and 91 deletions

View File

@@ -1,13 +1,14 @@
use crate::fat_error::FatError;
use crate::webserver::read_up_to_bytes_from_request;
use crate::BOARD_ACCESS;
use edge_http::io::server::Connection;
use edge_http::Method;
use embedded_io_async::{Read, Write};
use log::info;
use crate::BOARD_ACCESS;
use crate::fat_error::FatError;
pub(crate) async fn ota_operations<T, const N: usize>(
conn: &mut Connection<'_, T, { N }>,
method: Method
method: Method,
) -> Result<Option<u32>, FatError>
where
T: Read + Write,
@@ -23,7 +24,7 @@ where
("Access-Control-Allow-Methods", "*"),
],
)
.await?;
.await?;
Some(200)
}
Method::Post => {
@@ -33,9 +34,8 @@ where
// Erase only a single 4K block right before writing into it.
// The first block will be erased when offset == 0 below.
loop {
let mut buf = [0_u8; 1024];
let to_write = conn.read(&mut buf).await?;
if to_write == 0 {
let buf = read_up_to_bytes_from_request(conn, Some(4096)).await?;
if buf.len() == 0 {
info!("file request for ota finished");
let mut board = BOARD_ACCESS.get().await.lock().await;
board.board_hal.get_esp().finalize_ota().await?;
@@ -44,17 +44,14 @@ where
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)
if offset % 4096 >= offset+to_write % 4096 {
info!("erasing block {} during write between {} with size {}", offset / 4096, offset, to_write);
board.board_hal.get_esp().ota_erase(offset as u32).await?;
}
info!("erasing and writing block 0x{offset:x}");
board
.board_hal
.get_esp()
.write_ota(offset as u32, &buf[0..to_write])
.write_ota(offset as u32, &*buf)
.await?;
}
offset = offset + to_write;
offset = offset + buf.len();
chunk = chunk + 1;
}
BOARD_ACCESS
@@ -74,7 +71,7 @@ where
("Access-Control-Allow-Methods", "*"),
],
)
.await?;
.await?;
Some(200)
}
_ => None,