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; pub(crate) async fn ota_operations( conn: &mut Connection<'_, T, { N }>, method: Method, ) -> Result, 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 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?; break; } 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?; } offset = offset + buf.len(); 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, }) }