Skip to content

Commit

Permalink
Change feature flag naming
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasKoch committed Feb 21, 2024
1 parent 942b93e commit 67adb83
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 29 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ atat = { version = "*", features = ["heapless"] }


[features]
default = ["socket-udp", "socket-tcp", "async", "ublox-sockets"]
default = ["socket-udp", "socket-tcp", "async"]

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"]

Expand Down
4 changes: 4 additions & 0 deletions examples/embassy-rp2040-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ 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"]
internal-network-stack = ["ublox-cellular-rs/internal-network-stack"]

[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" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use embassy_rp::uart::BufferedUartTx;
use embassy_rp::{bind_interrupts, peripherals::UART0, uart::BufferedInterruptHandler};
use embassy_time::{Duration, Timer};
use static_cell::StaticCell;
use ublox_cellular::asynch::InternalRunner;
use ublox_cellular::asynch::Resources;
use ublox_cellular::asynch::UbloxRunner;
use {defmt_rtt as _, panic_probe as _};

use ublox_cellular::config::{Apn, CellularConfig};
Expand Down Expand Up @@ -95,7 +95,7 @@ async fn main(spawner: Spawner) {
Resources<BufferedUartTx<UART0>, CMD_BUF_SIZE, INGRESS_BUF_SIZE, URC_CAPACITY>,
> = StaticCell::new();

let (_net_device, mut control, runner) = ublox_cellular::asynch::new(
let (_net_device, mut control, runner) = ublox_cellular::asynch::new_internal(
uart_rx,
uart_tx,
RESOURCES.init(Resources::new()),
Expand Down Expand Up @@ -175,7 +175,7 @@ async fn main(spawner: Spawner) {

#[embassy_executor::task]
async fn cell_task(
mut runner: UbloxRunner<
mut runner: InternalRunner<
'static,
BufferedUartRx<'static, UART0>,
BufferedUartTx<'static, UART0>,
Expand Down
108 changes: 93 additions & 15 deletions src/asynch/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -79,7 +82,7 @@ impl<
const CMD_BUF_SIZE: usize,
const INGRESS_BUF_SIZE: usize,
const URC_CAPACITY: usize,
> Resources<W, CMD_BUF_SIZE, INGRESS_BUF_SIZE, URC_CAPACITY>
> UbxResources<W, CMD_BUF_SIZE, INGRESS_BUF_SIZE, URC_CAPACITY>
{
pub fn new() -> Self {
Self {
Expand All @@ -101,7 +104,8 @@ impl<
}
}

pub fn new<
#[cfg(feature = "internal-network-stack")]
pub fn new_internal<
'a,
R: embedded_io_async::Read,
W: embedded_io_async::Write,
Expand All @@ -117,7 +121,7 @@ pub fn new<
) -> (
state::Device<'a, Client<'a, W, INGRESS_BUF_SIZE>, URC_CAPACITY>,
Control<'a, Client<'a, W, INGRESS_BUF_SIZE>>,
UbloxRunner<'a, R, W, C, INGRESS_BUF_SIZE, URC_CAPACITY>,
InternalRunner<'a, R, W, C, INGRESS_BUF_SIZE, URC_CAPACITY>,
) {
// safety: this is a self-referential struct, however:
// - it can't move while the `'a` borrow is active.
Expand Down Expand Up @@ -158,7 +162,7 @@ pub fn new<
&resources.urc_channel,
);

let runner = UbloxRunner {
let runner = InternalRunner {
cellular_runner: runner,
ingress,
reader,
Expand All @@ -167,7 +171,8 @@ pub fn new<
(net_device, control, runner)
}

pub struct UbloxRunner<
#[cfg(feature = "internal-network-stack")]
pub struct InternalRunner<
'a,
R: embedded_io_async::Read,
W: embedded_io_async::Write,
Expand All @@ -180,14 +185,15 @@ pub struct UbloxRunner<
pub reader: R,
}

#[cfg(feature = "internal-network-stack")]
impl<
'a,
R: embedded_io_async::Read,
W: embedded_io_async::Write,
C: CellularConfig<'a>,
const INGRESS_BUF_SIZE: usize,
const URC_CAPACITY: usize,
> UbloxRunner<'a, R, W, C, INGRESS_BUF_SIZE, URC_CAPACITY>
> InternalRunner<'a, R, W, C, INGRESS_BUF_SIZE, URC_CAPACITY>
{
pub async fn run(&mut self) -> ! {
embassy_futures::join::join(
Expand Down Expand Up @@ -273,6 +279,28 @@ pub fn new_ppp<
(net_device, control, runner)
}

pub struct ReadWriteAdapter<R, W>(pub R, pub W);

impl<R, W> embedded_io_async::ErrorType for ReadWriteAdapter<R, W> {
type Error = embedded_io::ErrorKind;
}

impl<R: Read, W> Read for ReadWriteAdapter<R, W> {
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
self.0.read(buf).await.map_err(|e| e.kind())
}
}

impl<R, W: Write> Write for ReadWriteAdapter<R, W> {
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
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,
Expand Down Expand Up @@ -327,16 +355,66 @@ impl<'a, C: CellularConfig<'a>, const INGRESS_BUF_SIZE: usize, const URC_CAPACIT
Ok(())
}

pub async fn run<R: BufRead, W: Write>(
async fn init<R: Read, W: Write>(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::<Urc>::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<R: BufRead + Read, W: Write>(
&mut self,
mut rx: R,
mut tx: W,
stack: &embassy_net::Stack<embassy_net_ppp::Device<'a>>,
) -> ! {
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;
Expand Down
18 changes: 10 additions & 8 deletions src/asynch/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use crate::command::device_lock::GetPinStatus;
use crate::command::general::{GetCCID, GetFirmwareVersion, GetModelId};
use crate::command::gpio::types::{GpioInPull, GpioMode, GpioOutValue};
use crate::command::gpio::SetGpioConfiguration;
use crate::command::ip_transport_layer::types::HexMode;
use crate::command::ip_transport_layer::SetHexMode;
use crate::command::mobile_control::types::{Functionality, ResetMode, TerminationErrorMode};
use crate::command::mobile_control::{SetModuleFunctionality, SetReportMobileTerminationError};
use crate::command::psn::responses::GPRSAttached;
Expand Down Expand Up @@ -74,7 +72,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(())
}
Expand Down Expand Up @@ -205,16 +203,17 @@ impl<'d, AT: AtatClient, C: CellularConfig<'d>, const URC_CAPACITY: usize>
})
.await?;

#[cfg(feature = "internal-network-stack")]
if C::HEX_MODE {
self.at
.send(&SetHexMode {
hex_mode_disable: HexMode::Enabled,
.send(&crate::command::ip_transport_layer::SetHexMode {
hex_mode_disable: crate::command::ip_transport_layer::types::HexMode::Enabled,
})
.await?;
} else {
self.at
.send(&SetHexMode {
hex_mode_disable: HexMode::Disabled,
.send(&crate::command::ip_transport_layer::SetHexMode {
hex_mode_disable: crate::command::ip_transport_layer::types::HexMode::Disabled,
})
.await?;
}
Expand Down Expand Up @@ -379,7 +378,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");
}
Expand Down Expand Up @@ -617,10 +616,13 @@ impl<'d, AT: AtatClient, C: CellularConfig<'d>, const URC_CAPACITY: usize>
Urc::MobileStationDeactivate => warn!("Mobile station deactivated"),
Urc::NetworkPDNDeactivate => warn!("Network PDN deactivated"),
Urc::MobileStationPDNDeactivate => warn!("Mobile station PDN deactivated"),
#[cfg(feature = "internal-network-stack")]
Urc::SocketDataAvailable(_) => warn!("Socket data available"),
#[cfg(feature = "internal-network-stack")]
Urc::SocketDataAvailableUDP(_) => warn!("Socket data available UDP"),
Urc::DataConnectionActivated(_) => warn!("Data connection activated"),
Urc::DataConnectionDeactivated(_) => warn!("Data connection deactivated"),
#[cfg(feature = "internal-network-stack")]
Urc::SocketClosed(_) => warn!("Socket closed"),
Urc::MessageWaitingIndication(_) => warn!("Message waiting indication"),
Urc::ExtendedPSNetworkRegistration(_) => warn!("Extended PS network registration"),
Expand Down
9 changes: 7 additions & 2 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod file_system;
pub mod general;
pub mod gpio;
pub mod http;
#[cfg(feature = "internal-network-stack")]
pub mod ip_transport_layer;
pub mod ipc;
pub mod mobile_control;
Expand Down Expand Up @@ -42,16 +43,20 @@ pub enum Urc {
#[at_urc("+CGEV: ME PDN DEACT")]
MobileStationPDNDeactivate,

#[cfg(feature = "internal-network-stack")]
#[at_urc("+UUSORD")]
SocketDataAvailable(ip_transport_layer::urc::SocketDataAvailable),
#[cfg(feature = "internal-network-stack")]
#[at_urc("+UUSORF")]
SocketDataAvailableUDP(ip_transport_layer::urc::SocketDataAvailable),
#[cfg(feature = "internal-network-stack")]
#[at_urc("+UUSOCL")]
SocketClosed(ip_transport_layer::urc::SocketClosed),

#[at_urc("+UUPSDA")]
DataConnectionActivated(psn::urc::DataConnectionActivated),
#[at_urc("+UUPSDD")]
DataConnectionDeactivated(psn::urc::DataConnectionDeactivated),
#[at_urc("+UUSOCL")]
SocketClosed(ip_transport_layer::urc::SocketClosed),

#[at_urc("+UMWI")]
MessageWaitingIndication(sms::urc::MessageWaitingIndication),
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ pub trait CellularConfig<'a> {
// const URC_CAPACITY: usize;

const FLOW_CONTROL: bool = false;

#[cfg(feature = "internal-network-stack")]
const HEX_MODE: bool = true;

const OPERATOR_FORMAT: OperatorFormat = OperatorFormat::Long;

const PROFILE_ID: ProfileId = ProfileId(1);
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 67adb83

Please sign in to comment.