Document public API and fix warnings
This commit is contained in:
parent
b76b7ca9f5
commit
5e27a3e64f
@ -1,38 +1,46 @@
|
|||||||
|
//! Helper utility to configure a specific modem context.
|
||||||
use core::net::IpAddr;
|
use core::net::IpAddr;
|
||||||
use heapless::String;
|
|
||||||
use core::str::FromStr;
|
use core::str::FromStr;
|
||||||
use core::fmt::Write;
|
|
||||||
use heapless::Vec;
|
use heapless::Vec;
|
||||||
use at_commands::{builder::CommandBuilder, parser::CommandParser};
|
use at_commands::{builder::CommandBuilder, parser::CommandParser};
|
||||||
|
|
||||||
/// Provides a higher level API for configuring and reading information for a given
|
/// Provides a higher level API for controlling a given context.
|
||||||
/// context id.
|
|
||||||
pub struct Control<'a> {
|
pub struct Control<'a> {
|
||||||
control: crate::Control<'a>,
|
control: crate::Control<'a>,
|
||||||
cid: u8,
|
cid: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configuration for a given context
|
||||||
pub struct Config<'a> {
|
pub struct Config<'a> {
|
||||||
pub gateway: &'a str,
|
/// Desired APN address.
|
||||||
|
pub apn: &'a str,
|
||||||
|
/// Desired authentication protocol.
|
||||||
pub auth_prot: AuthProt,
|
pub auth_prot: AuthProt,
|
||||||
|
/// Credentials.
|
||||||
pub auth: Option<(&'a str, &'a str)>,
|
pub auth: Option<(&'a str, &'a str)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Authentication protocol.
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum AuthProt {
|
pub enum AuthProt {
|
||||||
|
/// No authentication.
|
||||||
None = 0,
|
None = 0,
|
||||||
|
/// PAP authentication.
|
||||||
Pap = 1,
|
Pap = 1,
|
||||||
|
/// CHAP authentication.
|
||||||
Chap = 2,
|
Chap = 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Error returned by control.
|
||||||
#[derive(Clone, Copy, PartialEq, Debug)]
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
/// Not enough space for command.
|
||||||
BufferTooSmall,
|
BufferTooSmall,
|
||||||
AtCommand,
|
/// Error parsing response from modem.
|
||||||
AtParseError,
|
AtParseError,
|
||||||
|
/// Error parsing IP addresses.
|
||||||
AddrParseError,
|
AddrParseError,
|
||||||
Format,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<at_commands::parser::ParseError> for Error {
|
impl From<at_commands::parser::ParseError> for Error {
|
||||||
@ -41,17 +49,16 @@ impl From<at_commands::parser::ParseError> for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<core::fmt::Error> for Error {
|
/// Status of a given context.
|
||||||
fn from(_: core::fmt::Error) -> Self {
|
|
||||||
Self::Format
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
pub struct Status {
|
pub struct Status {
|
||||||
|
/// Attached to APN or not.
|
||||||
pub attached: bool,
|
pub attached: bool,
|
||||||
|
/// IP if assigned.
|
||||||
pub ip: Option<IpAddr>,
|
pub ip: Option<IpAddr>,
|
||||||
|
/// Gateway if assigned.
|
||||||
pub gateway: Option<IpAddr>,
|
pub gateway: Option<IpAddr>,
|
||||||
|
/// DNS servers if assigned.
|
||||||
pub dns: Vec<IpAddr, 2>,
|
pub dns: Vec<IpAddr, 2>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,16 +73,14 @@ impl defmt::Format for Status {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Control<'a> {
|
impl<'a> Control<'a> {
|
||||||
|
/// Create a new instance of a control handle for a given context.
|
||||||
|
///
|
||||||
|
/// Will wait for the modem to be initialized if not.
|
||||||
pub async fn new(control: crate::Control<'a>, cid: u8) -> Self {
|
pub async fn new(control: crate::Control<'a>, cid: u8) -> Self {
|
||||||
control.wait_init().await;
|
control.wait_init().await;
|
||||||
Self { control, cid }
|
Self { control, cid }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bypass modem configurator
|
|
||||||
pub async fn at_command(&self, req: &[u8], resp: &mut [u8]) -> usize {
|
|
||||||
self.control.at_command(req, resp).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Configures the modem with the provided config.
|
/// Configures the modem with the provided config.
|
||||||
pub async fn configure(&self, config: Config<'_>) -> Result<(), Error> {
|
pub async fn configure(&self, config: Config<'_>) -> Result<(), Error> {
|
||||||
let mut cmd: [u8; 256] = [0; 256];
|
let mut cmd: [u8; 256] = [0; 256];
|
||||||
@ -85,7 +90,7 @@ impl<'a> Control<'a> {
|
|||||||
.named("+CGDCONT")
|
.named("+CGDCONT")
|
||||||
.with_int_parameter(self.cid)
|
.with_int_parameter(self.cid)
|
||||||
.with_string_parameter("IP")
|
.with_string_parameter("IP")
|
||||||
.with_string_parameter(config.gateway)
|
.with_string_parameter(config.apn)
|
||||||
.finish().map_err(|_| Error::BufferTooSmall)?;
|
.finish().map_err(|_| Error::BufferTooSmall)?;
|
||||||
let n = self.control.at_command(op, &mut buf).await;
|
let n = self.control.at_command(op, &mut buf).await;
|
||||||
CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?;
|
CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?;
|
||||||
@ -112,6 +117,7 @@ impl<'a> Control<'a> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Read current connectivity status for modem.
|
||||||
pub async fn status(&self) -> Result<Status, Error> {
|
pub async fn status(&self) -> Result<Status, Error> {
|
||||||
let mut cmd: [u8; 256] = [0; 256];
|
let mut cmd: [u8; 256] = [0; 256];
|
||||||
let mut buf: [u8; 256] = [0; 256];
|
let mut buf: [u8; 256] = [0; 256];
|
||||||
@ -134,7 +140,7 @@ impl<'a> Control<'a> {
|
|||||||
.with_int_parameter(self.cid)
|
.with_int_parameter(self.cid)
|
||||||
.finish().map_err(|_| Error::BufferTooSmall)?;
|
.finish().map_err(|_| Error::BufferTooSmall)?;
|
||||||
let n = self.control.at_command(op, &mut buf).await;
|
let n = self.control.at_command(op, &mut buf).await;
|
||||||
let (_, ip1, ip2, ) = CommandParser::parse(&buf[..n])
|
let (_, ip1, _ip2, ) = CommandParser::parse(&buf[..n])
|
||||||
.expect_identifier(b"+CGPADDR: ")
|
.expect_identifier(b"+CGPADDR: ")
|
||||||
.expect_int_parameter()
|
.expect_int_parameter()
|
||||||
.expect_optional_string_parameter()
|
.expect_optional_string_parameter()
|
||||||
@ -154,7 +160,7 @@ impl<'a> Control<'a> {
|
|||||||
.with_int_parameter(self.cid)
|
.with_int_parameter(self.cid)
|
||||||
.finish().map_err(|_| Error::BufferTooSmall)?;
|
.finish().map_err(|_| Error::BufferTooSmall)?;
|
||||||
let n = self.control.at_command(op, &mut buf).await;
|
let n = self.control.at_command(op, &mut buf).await;
|
||||||
let (_cid, _bid, _apn, _mask, gateway, dns1, dns2, _, _, _, _, mtu) = CommandParser::parse(&buf[..n])
|
let (_cid, _bid, _apn, _mask, gateway, dns1, dns2, _, _, _, _, _mtu) = CommandParser::parse(&buf[..n])
|
||||||
.expect_identifier(b"+CGCONTRDP: ")
|
.expect_identifier(b"+CGCONTRDP: ")
|
||||||
.expect_int_parameter()
|
.expect_int_parameter()
|
||||||
.expect_optional_int_parameter()
|
.expect_optional_int_parameter()
|
||||||
|
|||||||
@ -7,7 +7,7 @@ use core::ptr::addr_of_mut;
|
|||||||
use core::str::FromStr;
|
use core::str::FromStr;
|
||||||
use core::slice;
|
use core::slice;
|
||||||
|
|
||||||
use defmt::{assert, info, warn, unwrap};
|
use defmt::{info, warn, unwrap};
|
||||||
use heapless::Vec;
|
use heapless::Vec;
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources};
|
use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources};
|
||||||
@ -93,7 +93,7 @@ async fn main(spawner: Spawner) {
|
|||||||
|
|
||||||
static mut TRACE_BUF: [u8; 4096] = [0u8; 4096];
|
static mut TRACE_BUF: [u8; 4096] = [0u8; 4096];
|
||||||
let mut config = uarte::Config::default();
|
let mut config = uarte::Config::default();
|
||||||
config.baudrate = Baudrate::BAUD1M;
|
config.baudrate = Baudrate::BAUD115200;
|
||||||
let trace_writer = TraceWriter(BufferedUarteTx::new(
|
let trace_writer = TraceWriter(BufferedUarteTx::new(
|
||||||
//let trace_uart = BufferedUarteTx::new(
|
//let trace_uart = BufferedUarteTx::new(
|
||||||
unsafe { peripherals::SERIAL0::steal() },
|
unsafe { peripherals::SERIAL0::steal() },
|
||||||
@ -128,7 +128,7 @@ async fn main(spawner: Spawner) {
|
|||||||
let control = context::Control::new(control, 0).await;
|
let control = context::Control::new(control, 0).await;
|
||||||
|
|
||||||
unwrap!(control.configure(context::Config {
|
unwrap!(control.configure(context::Config {
|
||||||
gateway: "iot.nat.es",
|
apn: "iot.nat.es",
|
||||||
auth_prot: context::AuthProt::Pap,
|
auth_prot: context::AuthProt::Pap,
|
||||||
auth: Some(("orange", "orange")),
|
auth: Some(("orange", "orange")),
|
||||||
}).await);
|
}).await);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user