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,4 +1,5 @@
use crate::fat_error::{FatError, FatResult};
use crate::webserver::read_up_to_bytes_from_request;
use crate::BOARD_ACCESS;
use alloc::borrow::ToOwned;
use alloc::format;
@@ -118,9 +119,8 @@ where
let mut offset = 0_usize;
let mut chunk = 0;
loop {
let mut buf = [0_u8; 4096];
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 {} finished", filename);
break;
} else {
@@ -129,10 +129,10 @@ where
board
.board_hal
.get_esp()
.write_file(filename.to_owned(), offset as u32, &buf[0..to_write])
.write_file(filename.to_owned(), offset as u32, &buf)
.await?;
}
offset = offset + to_write;
offset = offset + buf.len();
chunk = chunk + 1;
}
BOARD_ACCESS

View File

@@ -5,8 +5,8 @@ mod file_manager;
mod get_json;
mod get_log;
mod get_static;
mod post_json;
mod ota;
mod post_json;
use crate::fat_error::{FatError, FatResult};
use crate::webserver::backup_manager::{backup_config, backup_info, get_backup_config};
@@ -17,6 +17,7 @@ use crate::webserver::get_json::{
};
use crate::webserver::get_log::get_log;
use crate::webserver::get_static::{serve_bundle, serve_favicon, serve_index};
use crate::webserver::ota::ota_operations;
use crate::webserver::post_json::{
board_test, night_lamp_test, pump_test, set_config, wifi_scan, write_time,
};
@@ -37,7 +38,6 @@ use embassy_net::Stack;
use embassy_time::Instant;
use embedded_io_async::{Read, Write};
use log::{error, info};
use crate::webserver::ota::ota_operations;
// fn ota(
// request: &mut Request<&mut EspHttpConnection>,
// ) -> Result<Option<std::string::String>, anyhow::Error> {
@@ -109,11 +109,10 @@ impl Handler for HTTPRequestRouter {
let status = if path.starts_with(prefix) {
file_operations(conn, method, &path, &prefix).await?
} else if path == "/ota" {
ota_operations(conn,method).await.map_err(|e| {
ota_operations(conn, method).await.map_err(|e| {
error!("Error handling ota: {}", e);
e
}
)?
e
})?
} else {
match method {
Method::Get => match path {
@@ -202,12 +201,18 @@ where
let mut data_store = Vec::new();
let mut total_read = 0;
loop {
let left = max_read - total_read;
let mut buf = [0_u8; 64];
let read = request.read(&mut buf).await?;
let s_buf = if buf.len() <= left {
&mut buf
} else {
&mut buf[0..left]
};
let read = request.read(s_buf).await?;
if read == 0 {
break;
}
let actual_data = &buf[0..read];
let actual_data = &s_buf[0..read];
total_read += read;
if total_read > max_read {
bail!("Request too large {total_read} > {max_read}");

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,