From f282cce9003296e5d0271fa2e6fe426e2e3a9391 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 3 Aug 2022 03:32:27 +0800 Subject: [PATCH] add a loopback re-queue function to the com the caller also needs to schedule the rx function explicitly, not handled by these functions. --- services/com/src/lib.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/services/com/src/lib.rs b/services/com/src/lib.rs index 132339e10..1f98ef955 100644 --- a/services/com/src/lib.rs +++ b/services/com/src/lib.rs @@ -10,7 +10,8 @@ use com_rs_ref::{DhcpState, LinkState}; use xous::{send_message, Error, CID, Message, msg_scalar_unpack}; use xous_ipc::{String, Buffer}; use num_traits::{ToPrimitive, FromPrimitive}; - +use std::collections::VecDeque; +use std::cell::RefCell; pub use com_rs_ref::serdes::Ipv4Conf; use xous_semver::SemVer; @@ -47,6 +48,9 @@ pub struct Com { battstats_sid: Option, ec_lock_id: Option<[u32; 4]>, ec_acquired: bool, + /// this is a hack to make loopbacks work on smoltcp. Work-around taken from Redox, but tracking this issue as well: + /// https://github.com/smoltcp-rs/smoltcp/issues/50 and https://github.com/smoltcp-rs/smoltcp/issues/55 + loopback_buf: RefCell::>>, } impl Com { pub fn new(xns: &xous_names::XousNames) -> Result { @@ -57,6 +61,7 @@ impl Com { battstats_sid: None, ec_lock_id: None, ec_acquired: false, + loopback_buf: RefCell::new(VecDeque::new()), }) } pub fn conn(&self) -> CID {self.conn} @@ -535,6 +540,19 @@ impl Com { if pkt.len() > NET_MTU { return Err(xous::Error::OutOfMemory) } + { + // this is a hack to make loopbacks work on smoltcp. Work-around taken from Redox, but tracking this issue as well: + // https://github.com/smoltcp-rs/smoltcp/issues/50 and https://github.com/smoltcp-rs/smoltcp/issues/55 + // Inject the loopback packets. loopback packets always take priority, for now. + if let Some(loop_packet) = self.loopback_buf.borrow_mut().pop_front() { + // pkt.len() is almost always not loop_packet.len(), just copy the first bit that can fit and the rest is garbarge... + for (&s, d) in loop_packet.iter().zip(pkt.iter_mut()) { + *d = s; + } + // skips polling the COM -- let's hope there wasn't a race condition on the interrupt + return Ok(()) + } + } let mut prealloc: [u8; NET_MTU] = [0; NET_MTU]; let len_bytes = (pkt.len() as u16).to_be_bytes(); prealloc[0] = len_bytes[0]; @@ -548,6 +566,13 @@ impl Com { Ok(()) } + // this is a hack to make loopbacks work on smoltcp. Work-around taken from Redox, but tracking this issue as well: + // https://github.com/smoltcp-rs/smoltcp/issues/50 and https://github.com/smoltcp-rs/smoltcp/issues/55 + // this function handles enqueuing packets for local injection + pub fn wlan_queue_loopback(&self, pk: &[u8]) { + self.loopback_buf.borrow_mut().push_back(pk.to_vec()); + } + pub fn wlan_send_packet(&self, pkt: &[u8]) -> Result<(), xous::Error> { if pkt.len() > NET_MTU { return Err(xous::Error::OutOfMemory)