From 7801c3948cc88416934e6e550eecfb34bf35f60d Mon Sep 17 00:00:00 2001 From: Dane Slattery Date: Thu, 15 Aug 2024 17:03:36 +0200 Subject: [PATCH] Use `EspNetifDriver` and adjust lifetimes. Currently segfaults. --- examples/lte_modem.rs | 4 +- src/modem.rs | 86 +++++++++++++++++-------------------------- src/netif.rs | 25 ++++++++----- 3 files changed, 51 insertions(+), 64 deletions(-) diff --git a/examples/lte_modem.rs b/examples/lte_modem.rs index df1ca3bf87b..1aafc4ee150 100644 --- a/examples/lte_modem.rs +++ b/examples/lte_modem.rs @@ -29,7 +29,7 @@ fn main() -> anyhow::Result<()> { let nvs = EspDefaultNvsPartition::take()?; let serial = peripherals.uart2; let tx = peripherals.pins.gpio17; - let rx = peripherals.pins.gpio16; + let rx = peripherals.pins.gpio18; let mut serial = UartDriver::new( serial, tx, @@ -42,7 +42,7 @@ fn main() -> anyhow::Result<()> { }, )?; log::error!("Hello"); - let mut modem = EspModem::new(&mut serial)?; + let mut modem = EspModem::new(&mut serial); match modem.setup_data_mode() { Err(x) => log::error!("Error: {:?}", x), diff --git a/src/modem.rs b/src/modem.rs index d6831d154d2..bebb1295038 100644 --- a/src/modem.rs +++ b/src/modem.rs @@ -1,50 +1,17 @@ use at_commands::{builder::CommandBuilder, parser::CommandParser}; use atat::{self, AtatCmd}; use core::{borrow::BorrowMut, ffi::c_void, marker::PhantomData}; -use esp_idf_hal::{delay::TickType, uart::UartDriver}; +use esp_idf_hal::{ + delay::{TickType, BLOCK}, + uart::{UartDriver, UartTxDriver}, +}; use crate::{ handle::RawHandle, - netif::{EspNetif, NetifStack}, + netif::{EspNetif, EspNetifDriver, NetifStack}, sys::*, }; -pub struct PppNetif<'d, T> -where - T: BorrowMut>, -{ - serial: T, - base: esp_netif_driver_base_t, - netif: EspNetif, - _d: PhantomData<&'d ()>, -} - -impl<'d, T> PppNetif<'d, T> -where - T: BorrowMut>, -{ - pub fn new(serial: T) -> Result { - let netif = EspNetif::new(NetifStack::Ppp)?; - let mut base = esp_netif_driver_base_t { - netif: netif.handle(), - post_attach: Some(Self::post_attach), - }; - let base_ptr: *mut c_void = &mut base as *mut _ as *mut c_void; - esp!(unsafe { esp_netif_attach(netif.handle(), base_ptr) })?; - Ok(Self { - serial, - netif, - base, - _d: PhantomData, - }) - } - - unsafe extern "C" fn post_attach(netif: *mut esp_netif_obj, args: *mut c_void) -> i32 { - let driver= unsafe{std::ptr::slice_from_raw_parts_mut(args, size_of:: where T: BorrowMut>, @@ -56,7 +23,7 @@ where impl<'d, T> EspModem<'d, T> where - T: BorrowMut>, + T: BorrowMut> + Send, { pub fn new(serial: T) -> Self { Self { @@ -107,11 +74,27 @@ where self.set_data_mode()?; // now in ppp mode. - // self.netif. + let netif = EspNetif::new(NetifStack::Ppp)?; + + let (mut tx, rx) = self.serial.borrow_mut().split(); + let driver = EspNetifDriver::new_ppp(netif, move |x| Self::tx(&mut tx, x))?; + + let mut buff = [0u8; 64]; + loop { + let len = rx.read(&mut buff, BLOCK)?; + if len > 0 { + driver.rx(&buff)?; + } + } Ok(()) } + fn tx(writer: &mut UartTxDriver, data: &[u8]) -> Result<(), EspError> { + writer.write(data)?; + Ok(()) + } + fn get_signal_quality(&mut self) -> Result<(), EspError> { let mut buff = [0u8; 64]; let cmd = CommandBuilder::create_execute(&mut buff, true) @@ -142,7 +125,7 @@ where let cmd = CommandBuilder::create_execute(&mut buff, false) .named("ATZ0") .finish() - .unwrap(); + .map_err(|_w| EspError::from_infallible::())?; self.serial.borrow_mut().write(cmd)?; let len = self .serial @@ -153,7 +136,7 @@ where .expect_identifier(b"ATZ0\r") .expect_identifier(b"\r\nOK\r\n") .finish() - .unwrap(); + .map_err(|_w| EspError::from_infallible::())?; Ok(()) } @@ -162,7 +145,7 @@ where let cmd = CommandBuilder::create_execute(&mut buff, false) .named(format!("ATE{}", i32::from(echo))) .finish() - .unwrap(); + .map_err(|_w| EspError::from_infallible::())?; self.serial.borrow_mut().write(cmd)?; let len = self .serial @@ -174,7 +157,7 @@ where .expect_identifier(b"ATE0\r") .expect_identifier(b"\r\nOK\r\n") .finish() - .unwrap(); + .map_err(|_w| EspError::from_infallible::())?; Ok(()) } @@ -183,7 +166,7 @@ where let cmd = CommandBuilder::create_query(&mut buff, true) .named("+CGREG") .finish() - .unwrap(); + .map_err(|_w| EspError::from_infallible::())?; self.serial.borrow_mut().write(cmd)?; let len = self .serial @@ -199,7 +182,7 @@ where .expect_optional_int_parameter() .expect_identifier(b"\r\n\r\nOK\r\n") .finish() - .unwrap(); + .map_err(|_w| EspError::from_infallible::())?; log::info!( "CGREG: n: {}stat: {}, lac: {:?}, ci: {:?} ", n, @@ -216,9 +199,9 @@ where .named("+CGDCONT") .with_int_parameter(1) // context id .with_string_parameter("IP") // pdp type - .with_string_parameter("internet") + .with_string_parameter("flowlive.net") // apn .finish() - .unwrap(); + .map_err(|_w| EspError::from_infallible::())?; self.serial.borrow_mut().write(cmd)?; let len = self .serial @@ -229,8 +212,7 @@ where CommandParser::parse(&buff[..len]) .expect_identifier(b"\r\nOK\r\n") .finish() - .unwrap(); - + .map_err(|_w| EspError::from_infallible::())?; Ok(()) } @@ -239,7 +221,7 @@ where let cmd = CommandBuilder::create_execute(&mut buff, false) .named("ATD*99#") .finish() - .unwrap(); + .map_err(|_w| EspError::from_infallible::())?; self.serial.borrow_mut().write(cmd)?; let len = self .serial @@ -252,7 +234,7 @@ where .expect_optional_raw_string() .expect_identifier(b"\r\n") .finish() - .unwrap(); + .map_err(|_w| EspError::from_infallible::())?; log::info!("connect {:?}", connect_parm); Ok(()) } diff --git a/src/netif.rs b/src/netif.rs index 96fcffb7b19..34145fe6afa 100644 --- a/src/netif.rs +++ b/src/netif.rs @@ -873,20 +873,22 @@ where #[cfg(feature = "alloc")] mod driver { use core::borrow::Borrow; + use core::marker::PhantomData; use crate::handle::RawHandle; use crate::sys::*; use super::EspNetif; - pub struct EspNetifDriver + pub struct EspNetifDriver<'d, T> where T: Borrow, { - inner: alloc::boxed::Box>, + inner: alloc::boxed::Box>, + _d: PhantomData<&'d ()>, } - impl EspNetifDriver + impl<'d, T> EspNetifDriver<'d, T> where T: Borrow, { @@ -900,7 +902,7 @@ mod driver { #[cfg(esp_idf_lwip_ppp_support)] pub fn new_ppp(netif: T, tx: F) -> Result where - F: FnMut(&[u8]) -> Result<(), EspError> + Send + 'static, + F: FnMut(&[u8]) -> Result<(), EspError> + Send + 'd, { Self::new( netif, @@ -950,7 +952,7 @@ mod driver { tx: F, ) -> Result where - F: FnMut(&[u8]) -> Result<(), EspError> + Send + 'static, + F: FnMut(&[u8]) -> Result<(), EspError> + Send + 'd, P: FnMut(&EspNetif) -> Result<(), EspError> + Send + 'static, { let mut inner = alloc::boxed::Box::new(EspNetifDriverInner { @@ -1000,7 +1002,10 @@ mod driver { ); } - Ok(Self { inner }) + Ok(Self { + inner, + _d: PhantomData, + }) } /// Ingest a packet into the driver. @@ -1018,7 +1023,7 @@ mod driver { } } - impl Drop for EspNetifDriver + impl<'d, T> Drop for EspNetifDriver<'d, T> where T: Borrow, { @@ -1057,7 +1062,7 @@ mod driver { } #[repr(C)] - struct EspNetifDriverInner + struct EspNetifDriverInner<'d, T> where T: Borrow, { @@ -1066,12 +1071,12 @@ mod driver { got_ip_event_id: Option, lost_ip_event_id: Option, #[allow(clippy::type_complexity)] - tx: alloc::boxed::Box Result<(), EspError>>, + tx: alloc::boxed::Box Result<(), EspError> + 'd>, #[allow(clippy::type_complexity)] post_attach_cfg: alloc::boxed::Box Result<(), EspError>>, } - impl EspNetifDriverInner + impl<'d, T> EspNetifDriverInner<'d, T> where T: Borrow, {