Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions examples/embassy-net-tcp-client/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions examples/embassy-net-tcp-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ embedded-io-async = { version = "0.6.1", features = ["defmt-03"] }
tinyrlibc = "0.5.0"

# embassy-net branch
nrf-modem = { version = "0.9.0", path = "../../", features = [
"embassy-net",
"nrf9160",
"defmt",
] }
nrf-modem = { path = "../../", features = ["embassy-net", "nrf9160", "defmt"] }

[profile.release]
debug = 2
85 changes: 25 additions & 60 deletions src/embassy_net_modem/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ use core::str::FromStr;

use at_commands::builder::CommandBuilder;
use at_commands::parser::CommandParser;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex};
use embassy_time::{Duration, Timer};
use heapless::Vec;

use crate::embassy_net_modem::CAP_SIZE;
use crate::{embassy_net_modem::CAP_SIZE, Error, LteLink};

/// Provides a higher level API for controlling a given context.
pub struct Control<'a> {
control: super::Control<'a>,
cid: u8,
lte_link: Mutex<CriticalSectionRawMutex, Option<LteLink>>,
}

/// Authentication parameters for the Packet Data Network (PDN).
Expand Down Expand Up @@ -78,24 +80,6 @@ pub enum AuthProt {
Chap = 2,
}

/// Error returned by control.
#[derive(Clone, Copy, PartialEq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
/// Not enough space for command.
BufferTooSmall,
/// Error parsing response from modem.
AtParseError,
/// Error parsing IP addresses.
AddrParseError,
}

impl From<at_commands::parser::ParseError> for Error {
fn from(_: at_commands::parser::ParseError) -> Self {
Self::AtParseError
}
}

/// Status of a given context.
#[derive(PartialEq, Debug)]
pub struct Status {
Expand Down Expand Up @@ -131,7 +115,11 @@ impl<'a> Control<'a> {
///
/// `cid` indicates which PDP context to use, range 0-10.
pub async fn new(control: super::Control<'a>, cid: u8) -> Self {
Self { control, cid }
Self {
control,
cid,
lte_link: Mutex::new(None),
}
}

/// Perform a raw AT command
Expand All @@ -148,15 +136,9 @@ impl<'a> Control<'a> {
pub async fn configure(&self, config: &PdConfig<'_>, pin: Option<&[u8]>) -> Result<(), Error> {
let mut cmd: [u8; 256] = [0; 256];

let op = CommandBuilder::create_set(&mut cmd, true)
.named("+CFUN")
.with_int_parameter(0)
.finish()
.map_err(|_| Error::BufferTooSmall)?;
let n = self.control.at_command(op).await;
CommandParser::parse(n.as_bytes())
.expect_identifier(b"OK")
.finish()?;
if let Some(link) = self.lte_link.lock().await.take() {
link.deactivate().await?;
}

let mut op = CommandBuilder::create_set(&mut cmd, true)
.named("+CGDCONT")
Expand All @@ -165,7 +147,7 @@ impl<'a> Control<'a> {
if let Some(apn) = config.apn {
op = op.with_string_parameter(apn);
}
let op = op.finish().map_err(|_| Error::BufferTooSmall)?;
let op = op.finish().map_err(|s| Error::BufferTooSmall(Some(s)))?;

let n = self.control.at_command(op).await;
// info!("RES1: {}", unsafe { core::str::from_utf8_unchecked(&buf[..n]) });
Expand All @@ -183,7 +165,7 @@ impl<'a> Control<'a> {
.with_string_parameter(username)
.with_string_parameter(password);
}
let op = op.finish().map_err(|_| Error::BufferTooSmall)?;
let op = op.finish().map_err(|s| Error::BufferTooSmall(Some(s)))?;

let n = self.control.at_command(op).await;
// info!("RES2: {}", unsafe { core::str::from_utf8_unchecked(&buf[..n]) });
Expand All @@ -197,7 +179,7 @@ impl<'a> Control<'a> {
.named("+CPIN")
.with_string_parameter(pin)
.finish()
.map_err(|_| Error::BufferTooSmall)?;
.map_err(|s| Error::BufferTooSmall(Some(s)))?;
let _ = self.control.at_command(op).await;
// Ignore ERROR which means no pin required
}
Expand All @@ -212,7 +194,7 @@ impl<'a> Control<'a> {
.named("+CGATT")
.with_int_parameter(1)
.finish()
.map_err(|_| Error::BufferTooSmall)?;
.map_err(|s| Error::BufferTooSmall(Some(s)))?;
let n = self.control.at_command(op).await;
CommandParser::parse(n.as_bytes())
.expect_identifier(b"OK")
Expand All @@ -227,7 +209,7 @@ impl<'a> Control<'a> {
.named("+CGATT")
.with_int_parameter(0)
.finish()
.map_err(|_| Error::BufferTooSmall)?;
.map_err(|s| Error::BufferTooSmall(Some(s)))?;
let n = self.control.at_command(op).await;
CommandParser::parse(n.as_bytes())
.expect_identifier(b"OK")
Expand All @@ -241,7 +223,7 @@ impl<'a> Control<'a> {
let op = CommandBuilder::create_query(&mut cmd, true)
.named("+CGATT")
.finish()
.map_err(|_| Error::BufferTooSmall)?;
.map_err(|s| Error::BufferTooSmall(Some(s)))?;
let n = self.control.at_command(op).await;
let (res,) = CommandParser::parse(n.as_bytes())
.expect_identifier(b"+CGATT: ")
Expand All @@ -258,7 +240,7 @@ impl<'a> Control<'a> {
let op = CommandBuilder::create_query(&mut cmd, true)
.named("+CGATT")
.finish()
.map_err(|_| Error::BufferTooSmall)?;
.map_err(|s| Error::BufferTooSmall(Some(s)))?;
let n = self.control.at_command(op).await;
let (res,) = CommandParser::parse(n.as_bytes())
.expect_identifier(b"+CGATT: ")
Expand All @@ -280,7 +262,7 @@ impl<'a> Control<'a> {
.named("+CGPADDR")
.with_int_parameter(self.cid)
.finish()
.map_err(|_| Error::BufferTooSmall)?;
.map_err(|s| Error::BufferTooSmall(Some(s)))?;
let n = self.control.at_command(op).await;
let (_, ip1, ip2) = CommandParser::parse(n.as_bytes())
.expect_identifier(b"+CGPADDR: ")
Expand Down Expand Up @@ -308,7 +290,7 @@ impl<'a> Control<'a> {
.named("+CGCONTRDP")
.with_int_parameter(self.cid)
.finish()
.map_err(|_| Error::BufferTooSmall)?;
.map_err(|s| Error::BufferTooSmall(Some(s)))?;
let n = self.control.at_command(op).await;
let (_cid, _bid, _apn, _mask, gateway, dns1, dns2, _, _, _, _, _mtu) =
CommandParser::parse(n.as_bytes())
Expand Down Expand Up @@ -368,41 +350,24 @@ impl<'a> Control<'a> {

/// Disable modem
pub async fn disable(&self) -> Result<(), Error> {
let mut cmd: [u8; 256] = [0; 256];

let op = CommandBuilder::create_set(&mut cmd, true)
.named("+CFUN")
.with_int_parameter(0)
.finish()
.map_err(|_| Error::BufferTooSmall)?;
let n = self.control.at_command(op).await;
CommandParser::parse(n.as_bytes())
.expect_identifier(b"OK")
.finish()?;

if let Some(link) = self.lte_link.lock().await.take() {
link.deactivate().await?;
};
Ok(())
}

/// Enable modem
pub async fn enable(&self) -> Result<(), Error> {
let mut cmd: [u8; 256] = [0; 256];

let op = CommandBuilder::create_set(&mut cmd, true)
.named("+CFUN")
.with_int_parameter(1)
.finish()
.map_err(|_| Error::BufferTooSmall)?;
let n = self.control.at_command(op).await;
CommandParser::parse(n.as_bytes())
.expect_identifier(b"OK")
.finish()?;
self.lte_link.lock().await.replace(LteLink::new().await?);

// Make modem survive PDN detaches
let op = CommandBuilder::create_set(&mut cmd, true)
.named("%XPDNCFG")
.with_int_parameter(1)
.finish()
.map_err(|_| Error::BufferTooSmall)?;
.map_err(|s| Error::BufferTooSmall(Some(s)))?;
let n = self.control.at_command(op).await;
CommandParser::parse(n.as_bytes())
.expect_identifier(b"OK")
Expand Down
5 changes: 5 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ pub enum Error {
DnsParseFailed,
#[cfg(feature = "embedded-nal-async")]
ReverseDnsLookupNotSupported,
/// The address resturned by the modem couldn't be parsed
#[cfg(feature = "embassy-net")]
AddrParseError,
}

impl Display for Error {
Expand Down Expand Up @@ -115,6 +118,8 @@ impl embedded_io_async::Error for Error {
Error::DnsParseFailed => embedded_io_async::ErrorKind::Other,
#[cfg(feature = "embedded-nal-async")]
Error::ReverseDnsLookupNotSupported => embedded_io_async::ErrorKind::Unsupported,
#[cfg(feature = "embassy-net")]
Error::AddrParseError => embedded_io_async::ErrorKind::InvalidInput,
}
}
}
Expand Down
Loading