From ed9e756ef48e31d88ac2b86fcc3318d63f039834 Mon Sep 17 00:00:00 2001 From: Mathias Date: Wed, 21 Feb 2024 14:57:21 +0100 Subject: [PATCH] Change feature flag naming --- Cargo.toml | 3 +- examples/embassy-rp2040-example/Cargo.toml | 3 + src/asynch/mod.rs | 98 +++++++++++++++++++--- src/asynch/runner.rs | 4 +- src/lib.rs | 3 + 5 files changed, 98 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6bc2214..d66207c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,9 +52,10 @@ atat = { version = "*", features = ["heapless"] } [features] -default = ["socket-udp", "socket-tcp", "async", "ublox-sockets"] +default = ["socket-udp", "socket-tcp", "async", "ppp"] ppp = ["dep:embassy-at-cmux", "dep:embassy-net-ppp", "dep:embassy-net"] +internal-network-stack = ["dep:ublox-sockets"] async = ["dep:embedded-nal-async", "dep:embassy-futures"] diff --git a/examples/embassy-rp2040-example/Cargo.toml b/examples/embassy-rp2040-example/Cargo.toml index a485238..c63b77b 100644 --- a/examples/embassy-rp2040-example/Cargo.toml +++ b/examples/embassy-rp2040-example/Cargo.toml @@ -31,6 +31,9 @@ static_cell = { version = "2.0", features = []} atat = { version = "0.21.0", features = ["derive", "bytes", "defmt"] } ublox-cellular-rs = {version = "0.4.0", path = "../..", features = ["lara-r6", "defmt", "async"]} +[features] +ppp = ["ublox-cellular-rs/ppp"] + [patch.crates-io] ublox-sockets = { git = "https://github.com/BlackbirdHQ/ublox-sockets", branch = "feature/async-borrowed-sockets" } # atat = { git = "https://github.com/BlackbirdHQ/atat", branch = "master" } diff --git a/src/asynch/mod.rs b/src/asynch/mod.rs index c142d50..37eda3b 100644 --- a/src/asynch/mod.rs +++ b/src/asynch/mod.rs @@ -1,6 +1,6 @@ pub mod control; pub mod runner; -#[cfg(feature = "ublox-sockets")] +#[cfg(feature = "internal-network-stack")] pub mod ublox_stack; pub mod state; @@ -9,9 +9,11 @@ use core::mem::MaybeUninit; use crate::{ command::{ + control::{types::FlowControl, SetFlowControl}, + ipc::SetMultiplexing, mobile_control::{ - types::{Functionality, ResetMode}, - SetModuleFunctionality, + types::{Functionality, ResetMode, TerminationErrorMode}, + SetModuleFunctionality, SetReportMobileTerminationError, }, psn::{DeactivatePDPContext, EnterPPP, SetPDPContextDefinition}, Urc, @@ -24,7 +26,8 @@ use atat::{ }; use embassy_sync::{blocking_mutex::raw::NoopRawMutex, mutex::Mutex}; use embassy_time::{Duration, Instant, Timer}; -use embedded_io_async::{BufRead, Write}; +use embedded_io::Error; +use embedded_io_async::{BufRead, Read, Write}; use runner::Runner; use self::control::Control; @@ -42,17 +45,17 @@ pub type Resources< const CMD_BUF_SIZE: usize, const INGRESS_BUF_SIZE: usize, const URC_CAPACITY: usize, -> = Resources< +> = UbxResources< embassy_at_cmux::ChannelTx<'static, 256>, CMD_BUF_SIZE, INGRESS_BUF_SIZE, URC_CAPACITY, >; -// #[cfg(not(feature = "ppp"))] -// pub use self::Resources; +#[cfg(feature = "internal-network-stack")] +pub use self::UbxResources as Resources; -pub struct Resources< +pub struct UbxResources< W: Write, const CMD_BUF_SIZE: usize, const INGRESS_BUF_SIZE: usize, @@ -79,7 +82,7 @@ impl< const CMD_BUF_SIZE: usize, const INGRESS_BUF_SIZE: usize, const URC_CAPACITY: usize, - > Resources + > UbxResources { pub fn new() -> Self { Self { @@ -101,6 +104,7 @@ impl< } } +#[cfg(feature = "internal-network-stack")] pub fn new< 'a, R: embedded_io_async::Read, @@ -167,6 +171,7 @@ pub fn new< (net_device, control, runner) } +#[cfg(feature = "internal-network-stack")] pub struct UbloxRunner< 'a, R: embedded_io_async::Read, @@ -180,6 +185,7 @@ pub struct UbloxRunner< pub reader: R, } +#[cfg(feature = "internal-network-stack")] impl< 'a, R: embedded_io_async::Read, @@ -273,6 +279,28 @@ pub fn new_ppp< (net_device, control, runner) } +pub struct ReadWriteAdapter(pub R, pub W); + +impl embedded_io_async::ErrorType for ReadWriteAdapter { + type Error = embedded_io::ErrorKind; +} + +impl Read for ReadWriteAdapter { + async fn read(&mut self, buf: &mut [u8]) -> Result { + self.0.read(buf).await.map_err(|e| e.kind()) + } +} + +impl Write for ReadWriteAdapter { + async fn write(&mut self, buf: &[u8]) -> Result { + self.1.write(buf).await.map_err(|e| e.kind()) + } + + async fn flush(&mut self) -> Result<(), Self::Error> { + self.1.flush().await.map_err(|e| e.kind()) + } +} + #[cfg(feature = "ppp")] pub struct PPPRunner< 'a, @@ -327,7 +355,45 @@ impl<'a, C: CellularConfig<'a>, const INGRESS_BUF_SIZE: usize, const URC_CAPACIT Ok(()) } - pub async fn run( + async fn init(rx: &mut R, tx: &mut W) -> Result<(), atat::Error> { + let mut buf = [0u8; 64]; + let mut at_client = SimpleClient::new( + ReadWriteAdapter(rx, tx), + atat::AtDigester::::new(), + &mut buf, + atat::Config::default(), + ); + + at_client + .send(&SetReportMobileTerminationError { + n: TerminationErrorMode::Enabled, + }) + .await?; + + at_client + .send(&SetFlowControl { + value: FlowControl::RtsCts, + }) + .await?; + + at_client + .send(&SetMultiplexing { + mode: 0, + subset: None, + port_speed: None, + n1: None, + t1: None, + n2: None, + t2: None, + t3: None, + k: None, + }) + .await?; + + Ok(()) + } + + pub async fn run( &mut self, mut rx: R, mut tx: W, @@ -335,8 +401,20 @@ impl<'a, C: CellularConfig<'a>, const INGRESS_BUF_SIZE: usize, const URC_CAPACIT ) -> ! { loop { // Reset modem + // if self.cellular_runner.init().await.is_err() { + // Timer::after(Duration::from_secs(5)).await; + // continue; + // } + + // Timer::after(Duration::from_secs(5)).await; // Do AT init and enter CMUX mode using interface + if Self::init(&mut rx, &mut tx).await.is_err() { + Timer::after(Duration::from_secs(5)).await; + continue; + }; + + Timer::after(Duration::from_secs(1)).await; let ppp_fut = async { let mut fails = 0; diff --git a/src/asynch/runner.rs b/src/asynch/runner.rs index 9efa713..aff1702 100644 --- a/src/asynch/runner.rs +++ b/src/asynch/runner.rs @@ -74,7 +74,7 @@ impl<'d, AT: AtatClient, C: CellularConfig<'d>, const URC_CAPACITY: usize> self.power_up().await?; }; self.reset().await?; - self.is_alive().await?; + // self.is_alive().await?; Ok(()) } @@ -379,7 +379,7 @@ impl<'d, AT: AtatClient, C: CellularConfig<'d>, const URC_CAPACITY: usize> Timer::after(reset_time()).await; pin.set_high().ok(); Timer::after(boot_time()).await; - self.is_alive().await?; + // self.is_alive().await?; } else { warn!("No reset pin configured"); } diff --git a/src/lib.rs b/src/lib.rs index 312a885..3451008 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,9 @@ // #![cfg_attr(feature = "async", feature(async_fn_in_trait))] // #![cfg_attr(feature = "async", feature(type_alias_impl_trait))] +#[cfg(all(feature = "ppp", feature = "internal-network-stack"))] +compile_error!("You may not enable both `ppp` and `internal-network-stack` features."); + // This mod MUST go first, so that the others see its macros. pub(crate) mod fmt;