From 4e56caa71035ab236b11b31af618a7ae45792358 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Tue, 2 Dec 2025 12:30:53 +0100 Subject: chore: refactor api to allow other control commands --- embassy-net-esp-hosted/src/control.rs | 81 +++++++++++++++++++---------------- embassy-net-esp-hosted/src/ioctl.rs | 1 - embassy-net-esp-hosted/src/lib.rs | 3 +- 3 files changed, 47 insertions(+), 38 deletions(-) diff --git a/embassy-net-esp-hosted/src/control.rs b/embassy-net-esp-hosted/src/control.rs index d96a62daf..255ad7045 100644 --- a/embassy-net-esp-hosted/src/control.rs +++ b/embassy-net-esp-hosted/src/control.rs @@ -1,3 +1,4 @@ +use core::marker::PhantomData; use embassy_net_driver_channel as ch; use embassy_net_driver_channel::driver::{HardwareAddress, LinkState}; use heapless::String; @@ -24,9 +25,15 @@ pub struct Control<'a> { shared: &'a Shared, } -/// Handle for managing firmware update. -pub struct UpdateControl<'a, 'd> { - control: &'a mut Control<'d>, +/// Token required for doing an update +pub struct OtaToken { + _d: PhantomData<()>, +} + +impl OtaToken { + fn new() -> Self { + Self { _d: PhantomData } + } } /// WiFi mode. @@ -152,10 +159,43 @@ impl<'a> Control<'a> { } /// Initiate a firmware update. - pub async fn update(&mut self) -> Result, Error> { + /// + /// Returns a token needed for writing and finishing. + pub async fn ota_begin(&mut self) -> Result { let req = proto::CtrlMsg_Req_OTABegin {}; ioctl!(self, ReqOtaBegin, RespOtaBegin, req, resp); - Ok(UpdateControl { control: self }) + Ok(OtaToken::new()) + } + + /// Write slice of firmware to a device. + /// + /// Token is required as proof that ota_begin was called. + /// + /// The slice is split into chunks that can be sent across + /// the ioctl protocol to the wifi adapter. + pub async fn ota_write(&mut self, _token: &OtaToken, data: &[u8]) -> Result<(), Error> { + for chunk in data.chunks(256) { + let req = proto::CtrlMsg_Req_OTAWrite { + ota_data: heapless::Vec::from_slice(chunk).unwrap(), + }; + ioctl!(self, ReqOtaWrite, RespOtaWrite, req, resp); + } + Ok(()) + } + + /// End the OTA session. + /// + /// Token is required as proof that ota_begin was called. + /// + /// NOTE: Will reset the wifi adapter after 5 seconds. + pub async fn ota_end(&mut self, _token: OtaToken) -> Result<(), Error> { + let req = proto::CtrlMsg_Req_OTAEnd {}; + ioctl!(self, ReqOtaEnd, RespOtaEnd, req, resp); + // Ensures that run loop awaits reset + self.shared.ota_done(); + // Wait for re-init + self.init().await?; + Ok(()) } /// duration in seconds, clamped to [10, 3600] @@ -229,37 +269,6 @@ impl<'a> Control<'a> { } } -impl<'a, 'd> UpdateControl<'a, 'd> { - /// Write slice of firmware to a device. - /// - /// The slice is split into chunks that can be sent across - /// the ioctl protocol to the wifi adapter. - pub async fn write(&mut self, data: &[u8]) -> Result<(), Error> { - let this = &mut self.control; - for chunk in data.chunks(256) { - let req = proto::CtrlMsg_Req_OTAWrite { - ota_data: heapless::Vec::from_slice(chunk).unwrap(), - }; - ioctl!(this, ReqOtaWrite, RespOtaWrite, req, resp); - } - Ok(()) - } - - /// End the OTA session. - /// - /// NOTE: Will reset the wifi adapter after 5 seconds. - pub async fn finish(self) -> Result<(), Error> { - let this = self.control; - let req = proto::CtrlMsg_Req_OTAEnd {}; - ioctl!(this, ReqOtaEnd, RespOtaEnd, req, resp); - // Ensures that run loop awaits reset - this.shared.ota_done(); - // Wait for re-init - this.init().await?; - Ok(()) - } -} - // WHY IS THIS A STRING? WHYYYY fn parse_mac(mac: &str) -> Result<[u8; 6], Error> { fn nibble_from_hex(b: u8) -> Result { diff --git a/embassy-net-esp-hosted/src/ioctl.rs b/embassy-net-esp-hosted/src/ioctl.rs index 7f462d528..8b9d582be 100644 --- a/embassy-net-esp-hosted/src/ioctl.rs +++ b/embassy-net-esp-hosted/src/ioctl.rs @@ -106,7 +106,6 @@ impl Shared { let mut this = self.0.borrow_mut(); this.is_ota = true; this.is_init = false; - this.runner_waker.wake(); } // check if ota is in progress diff --git a/embassy-net-esp-hosted/src/lib.rs b/embassy-net-esp-hosted/src/lib.rs index 7236e73e8..f0cf04d04 100644 --- a/embassy-net-esp-hosted/src/lib.rs +++ b/embassy-net-esp-hosted/src/lib.rs @@ -238,8 +238,9 @@ where if self.shared.is_ota() { self.heartbeat_deadline = Instant::now() + HEARTBEAT_MAX_GAP; continue; + } else { + panic!("heartbeat from esp32 stopped") } - panic!("heartbeat from esp32 stopped") } } -- cgit