Skip to content

Commit

Permalink
Use EspNetifDriver and adjust lifetimes. Currently segfaults.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dane Slattery committed Aug 15, 2024
1 parent 48c6fe1 commit 7801c39
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 64 deletions.
4 changes: 2 additions & 2 deletions examples/lte_modem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down
86 changes: 34 additions & 52 deletions src/modem.rs
Original file line number Diff line number Diff line change
@@ -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<UartDriver<'d>>,
{
serial: T,
base: esp_netif_driver_base_t,
netif: EspNetif,
_d: PhantomData<&'d ()>,
}

impl<'d, T> PppNetif<'d, T>
where
T: BorrowMut<UartDriver<'d>>,
{
pub fn new(serial: T) -> Result<Self, EspError> {
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::<ppp_netif_driver())}
let ifconfig = esp_netif_driver_ifconfig_t{handle}
}
}

pub struct EspModem<'d, T>
where
T: BorrowMut<UartDriver<'d>>,
Expand All @@ -56,7 +23,7 @@ where

impl<'d, T> EspModem<'d, T>
where
T: BorrowMut<UartDriver<'d>>,
T: BorrowMut<UartDriver<'d>> + Send,
{
pub fn new(serial: T) -> Self {
Self {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -142,7 +125,7 @@ where
let cmd = CommandBuilder::create_execute(&mut buff, false)
.named("ATZ0")
.finish()
.unwrap();
.map_err(|_w| EspError::from_infallible::<ESP_FAIL>())?;
self.serial.borrow_mut().write(cmd)?;
let len = self
.serial
Expand All @@ -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::<ESP_FAIL>())?;
Ok(())
}

Expand All @@ -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::<ESP_FAIL>())?;
self.serial.borrow_mut().write(cmd)?;
let len = self
.serial
Expand All @@ -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::<ESP_FAIL>())?;
Ok(())
}

Expand All @@ -183,7 +166,7 @@ where
let cmd = CommandBuilder::create_query(&mut buff, true)
.named("+CGREG")
.finish()
.unwrap();
.map_err(|_w| EspError::from_infallible::<ESP_FAIL>())?;
self.serial.borrow_mut().write(cmd)?;
let len = self
.serial
Expand All @@ -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::<ESP_FAIL>())?;
log::info!(
"CGREG: n: {}stat: {}, lac: {:?}, ci: {:?} ",
n,
Expand All @@ -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::<ESP_FAIL>())?;
self.serial.borrow_mut().write(cmd)?;
let len = self
.serial
Expand All @@ -229,8 +212,7 @@ where
CommandParser::parse(&buff[..len])
.expect_identifier(b"\r\nOK\r\n")
.finish()
.unwrap();

.map_err(|_w| EspError::from_infallible::<ESP_FAIL>())?;
Ok(())
}

Expand All @@ -239,7 +221,7 @@ where
let cmd = CommandBuilder::create_execute(&mut buff, false)
.named("ATD*99#")
.finish()
.unwrap();
.map_err(|_w| EspError::from_infallible::<ESP_FAIL>())?;
self.serial.borrow_mut().write(cmd)?;
let len = self
.serial
Expand All @@ -252,7 +234,7 @@ where
.expect_optional_raw_string()
.expect_identifier(b"\r\n")
.finish()
.unwrap();
.map_err(|_w| EspError::from_infallible::<ESP_FAIL>())?;
log::info!("connect {:?}", connect_parm);
Ok(())
}
Expand Down
25 changes: 15 additions & 10 deletions src/netif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
pub struct EspNetifDriver<'d, T>
where
T: Borrow<EspNetif>,
{
inner: alloc::boxed::Box<EspNetifDriverInner<T>>,
inner: alloc::boxed::Box<EspNetifDriverInner<'d, T>>,
_d: PhantomData<&'d ()>,
}

impl<T> EspNetifDriver<T>
impl<'d, T> EspNetifDriver<'d, T>
where
T: Borrow<EspNetif>,
{
Expand All @@ -900,7 +902,7 @@ mod driver {
#[cfg(esp_idf_lwip_ppp_support)]
pub fn new_ppp<F>(netif: T, tx: F) -> Result<Self, EspError>
where
F: FnMut(&[u8]) -> Result<(), EspError> + Send + 'static,
F: FnMut(&[u8]) -> Result<(), EspError> + Send + 'd,
{
Self::new(
netif,
Expand Down Expand Up @@ -950,7 +952,7 @@ mod driver {
tx: F,
) -> Result<Self, EspError>
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 {
Expand Down Expand Up @@ -1000,7 +1002,10 @@ mod driver {
);
}

Ok(Self { inner })
Ok(Self {
inner,
_d: PhantomData,
})
}

/// Ingest a packet into the driver.
Expand All @@ -1018,7 +1023,7 @@ mod driver {
}
}

impl<T> Drop for EspNetifDriver<T>
impl<'d, T> Drop for EspNetifDriver<'d, T>
where
T: Borrow<EspNetif>,
{
Expand Down Expand Up @@ -1057,7 +1062,7 @@ mod driver {
}

#[repr(C)]
struct EspNetifDriverInner<T>
struct EspNetifDriverInner<'d, T>
where
T: Borrow<EspNetif>,
{
Expand All @@ -1066,12 +1071,12 @@ mod driver {
got_ip_event_id: Option<i32>,
lost_ip_event_id: Option<i32>,
#[allow(clippy::type_complexity)]
tx: alloc::boxed::Box<dyn FnMut(&[u8]) -> Result<(), EspError>>,
tx: alloc::boxed::Box<dyn FnMut(&[u8]) -> Result<(), EspError> + 'd>,
#[allow(clippy::type_complexity)]
post_attach_cfg: alloc::boxed::Box<dyn FnMut(&EspNetif) -> Result<(), EspError>>,
}

impl<T> EspNetifDriverInner<T>
impl<'d, T> EspNetifDriverInner<'d, T>
where
T: Borrow<EspNetif>,
{
Expand Down

0 comments on commit 7801c39

Please sign in to comment.