ota is back

This commit is contained in:
2025-10-01 21:56:16 +02:00
parent 9c6dcc465e
commit 27b18df78e
4 changed files with 117 additions and 15 deletions

View File

@@ -6,6 +6,7 @@ mod get_json;
mod get_log;
mod get_static;
mod post_json;
mod ota;
use crate::fat_error::{FatError, FatResult};
use crate::webserver::backup_manager::{backup_config, backup_info, get_backup_config};
@@ -36,7 +37,7 @@ use embassy_net::Stack;
use embassy_time::Instant;
use embedded_io_async::{Read, Write};
use log::info;
use crate::webserver::ota::ota_operations;
// fn ota(
// request: &mut Request<&mut EspHttpConnection>,
// ) -> Result<Option<std::string::String>, anyhow::Error> {
@@ -107,6 +108,8 @@ impl Handler for HTTPRequestRouter {
let prefix = "/file?filename=";
let status = if path.starts_with(prefix) {
file_operations(conn, method, &path, &prefix).await?
} else if path == "/ota" {
ota_operations(conn,method).await?
} else {
match method {
Method::Get => match path {

76
rust/src/webserver/ota.rs Normal file
View File

@@ -0,0 +1,76 @@
use alloc::borrow::ToOwned;
use alloc::format;
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
) -> Result<Option<u32>, FatError>
where
T: Read + Write,
{
Ok(match method {
Method::Options => {
conn.initiate_response(
200,
Some("OK"),
&[
("Access-Control-Allow-Origin", "*"),
("Access-Control-Allow-Headers", "*"),
("Access-Control-Allow-Methods", "*"),
],
)
.await?;
Some(200)
}
Method::Post => {
let mut offset = 0_usize;
let mut chunk = 0;
loop {
let mut buf = [0_u8; 1024];
let to_write = conn.read(&mut buf).await?;
if to_write == 0 {
info!("file request for ota finished");
let mut board = BOARD_ACCESS.get().await.lock().await;
board.board_hal.get_esp().finalize_ota().await?;
break;
} else {
let mut board = BOARD_ACCESS.get().await.lock().await;
board.board_hal.progress(chunk as u32).await;
board
.board_hal
.get_esp()
.write_ota(offset as u32, &buf[0..to_write])
.await?;
}
offset = offset + to_write;
chunk = chunk + 1;
}
BOARD_ACCESS
.get()
.await
.lock()
.await
.board_hal
.clear_progress()
.await;
conn.initiate_response(
200,
Some("OK"),
&[
("Access-Control-Allow-Origin", "*"),
("Access-Control-Allow-Headers", "*"),
("Access-Control-Allow-Methods", "*"),
],
)
.await?;
Some(200)
}
_ => None,
})
}