Skip to content

Commit

Permalink
Rework pwr control and operation state logic. Also add AT command sup…
Browse files Browse the repository at this point in the history
…port to control handle
  • Loading branch information
MathiasKoch committed Jul 17, 2024
1 parent c7a1ef4 commit 5a4572a
Show file tree
Hide file tree
Showing 24 changed files with 960 additions and 861 deletions.
13 changes: 6 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ name = "ublox_cellular"
doctest = false

[dependencies]
atat = { version = "0.22", features = ["derive", "bytes"] }
atat = { version = "0.23", features = ["derive", "bytes"] }
heapless = { version = "^0.8", features = ["serde"] }
serde = { version = "^1", default-features = false, features = ["derive"] }
#ublox-sockets = { version = "0.5", optional = true }
Expand All @@ -37,7 +37,7 @@ embedded-nal = "0.8"
embedded-nal-async = { version = "0.7" }

# embassy-at-cmux = { git = "https://github.com/MathiasKoch/embassy", rev = "5fa7b8f4a", optional = true }
embassy-at-cmux = { path = "../embassy/embassy-at-cmux", optional = true }
embassy-at-cmux = { path = "../embassy/embassy-at-cmux" }
embassy-net-ppp = { version = "0.1", optional = true }
embassy-net = { version = "0.4", features = [
"proto-ipv4",
Expand Down Expand Up @@ -87,13 +87,12 @@ context-mapping-required = []
# sock-set-local-port = []
# fota = []
# uart-power-saving = []
cmux = ["dep:embassy-at-cmux"]
# snr-reported = []
authentication-mode-automatic = []
# lwm2m = []
ucged = []
# http = []
ppp = ["cmux", "dep:embassy-net-ppp", "dep:embassy-net"]
ppp = ["dep:embassy-net-ppp", "dep:embassy-net"]


automatic-apn = []
Expand All @@ -108,11 +107,11 @@ defmt = [
"heapless/defmt-03",
"embassy-time/defmt",
"embassy-sync/defmt",
"embassy-at-cmux/defmt",
"embassy-futures/defmt",
"ublox-sockets?/defmt",
"embassy-net-ppp?/defmt",
"embassy-net?/defmt",
"embassy-at-cmux?/defmt",
]
log = ["dep:log", "ublox-sockets?/log", "atat/log"]

Expand Down Expand Up @@ -153,5 +152,5 @@ embassy-net = { git = "https://github.com/MathiasKoch/embassy", rev = "5fa7b8f4a
#embassy-time = { path = "../embassy/embassy-time" }
#embassy-sync = { path = "../embassy/embassy-sync" }
#embassy-futures = { path = "../embassy/embassy-futures" }

atat = { path = "../atat/atat" }
atat = { git = "https://github.com/BlackbirdHQ/atat", rev = "a466836" }
# atat = { path = "../atat/atat" }
2 changes: 1 addition & 1 deletion examples/embassy-rp2040-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ panic-probe = { version = "0.3.1", features = ["print-defmt"] }

static_cell = { version = "2.0", features = [] }

atat = { version = "0.22", features = ["derive", "bytes", "defmt"] }
atat = { version = "0.23", features = ["derive", "bytes", "defmt"] }
ublox-cellular-rs = { version = "0.4.0", path = "../..", features = [
"lara-r6",
"defmt",
Expand Down
2 changes: 1 addition & 1 deletion examples/embassy-stm32-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ panic-probe = { version = "0.3.1", features = ["print-defmt"] }

static_cell = { version = "2.0", features = []}

atat = { version = "0.21.0", features = ["derive", "bytes", "defmt"] }
atat = { version = "0.23.0", features = ["derive", "bytes", "defmt"] }
ublox-cellular-rs = {version = "0.4.0", path = "../..", features = ["sara-r5", "defmt"]}

[patch.crates-io]
Expand Down
2 changes: 1 addition & 1 deletion examples/tokio-std-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ log = "0.4.21"

static_cell = { version = "2.0" }

atat = { version = "0.22", features = ["derive", "bytes", "log"] }
atat = { version = "0.23", features = ["derive", "bytes", "log"] }
ublox-cellular-rs = { version = "0.4.0", path = "../..", features = [
"lara-r6",
"log",
Expand Down
123 changes: 88 additions & 35 deletions src/asynch/control.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,115 @@
use super::state::{self, LinkState, OperationState};
use atat::{
asynch::{AtatClient, SimpleClient},
AtDigester,
};

use crate::{
command::{
general::{types::FirmwareVersion, GetFirmwareVersion},
gpio::{types::GpioMode, GetGpioConfiguration, SetGpioConfiguration},
network_service::{
responses::{OperatorSelection, SignalQuality},
GetOperatorSelection, GetSignalQuality,
},
Urc,
},
error::Error,
};

use super::{
runner::CMUX_CHANNEL_SIZE,
state::{self, LinkState, OperationState},
};

pub struct Control<'a> {
state_ch: state::Runner<'a>,
at_client:
SimpleClient<'a, embassy_at_cmux::Channel<'a, CMUX_CHANNEL_SIZE>, atat::AtDigester<Urc>>,
}

impl<'a> Control<'a> {
pub(crate) fn new(state_ch: state::Runner<'a>) -> Self {
Self { state_ch }
pub(crate) fn new(
state_ch: state::Runner<'a>,
at_client: SimpleClient<
'a,
embassy_at_cmux::Channel<'a, CMUX_CHANNEL_SIZE>,
atat::AtDigester<Urc>,
>,
) -> Self {
Self {
state_ch,
at_client,
}
}

pub fn link_state(&mut self) -> LinkState {
pub fn link_state(&self) -> LinkState {
self.state_ch.link_state(None)
}

pub fn operation_state(&mut self) -> OperationState {
pub fn operation_state(&self) -> OperationState {
self.state_ch.operation_state(None)
}

pub fn desired_state(&mut self) -> OperationState {
pub fn desired_state(&self) -> OperationState {
self.state_ch.desired_state(None)
}

pub fn set_desired_state(&mut self, ps: OperationState) {
pub fn set_desired_state(&self, ps: OperationState) {
self.state_ch.set_desired_state(ps);
}

pub async fn wait_for_desired_state(&mut self, ps: OperationState) {
pub async fn wait_for_desired_state(&self, ps: OperationState) {
self.state_ch.wait_for_desired_state(ps).await
}

pub async fn wait_for_operation_state(&mut self, ps: OperationState) {
pub async fn wait_for_operation_state(&self, ps: OperationState) {
self.state_ch.wait_for_operation_state(ps).await
}

// pub async fn get_signal_quality(
// &mut self,
// ) -> Result<crate::command::network_service::responses::SignalQuality, Error> {
// self.at
// .send(&crate::command::network_service::GetSignalQuality)
// .await
// .map_err(|e| Error::Atat(e))
// }

// pub async fn get_operator(
// &mut self,
// ) -> Result<crate::command::network_service::responses::OperatorSelection, Error> {
// self.at
// .send(&crate::command::network_service::GetOperatorSelection)
// .await
// .map_err(|e| Error::Atat(e))
// }

// /// Send an AT command to the modem
// /// This is usefull if you have special configuration but might break the drivers functionality if your settings interfere with the drivers settings
// pub async fn send<Cmd: atat::AtatCmd>(
// &mut self,
// cmd: &Cmd,
// ) -> Result<Cmd::Response, atat::Error> {
// self.at.send::<Cmd>(cmd).await
// }
pub async fn get_signal_quality(&mut self) -> Result<SignalQuality, Error> {
if self.operation_state() == OperationState::PowerDown {
return Err(Error::Uninitialized);
}

Ok(self.at_client.send_retry(&GetSignalQuality).await?)
}

pub async fn get_operator(&mut self) -> Result<OperatorSelection, Error> {
if self.operation_state() == OperationState::PowerDown {
return Err(Error::Uninitialized);
}

Ok(self.at_client.send_retry(&GetOperatorSelection).await?)
}

pub async fn get_version(&mut self) -> Result<FirmwareVersion, Error> {
if self.operation_state() == OperationState::PowerDown {
return Err(Error::Uninitialized);
}

let res = self.at_client.send_retry(&GetFirmwareVersion).await?;
Ok(res.version)
}

pub async fn set_gpio_configuration(
&self,
gpio_id: u8,
gpio_mode: GpioMode,
) -> Result<(), Error> {
if self.operation_state() == OperationState::PowerDown {
return Err(Error::Uninitialized);
}

self.at_client
.send_retry(&SetGpioConfiguration { gpio_id, gpio_mode })
.await?;
Ok(())
}

/// Send an AT command to the modem This is usefull if you have special
/// configuration but might break the drivers functionality if your settings
/// interfere with the drivers settings
pub async fn send<Cmd: atat::AtatCmd>(&mut self, cmd: &Cmd) -> Result<Cmd::Response, Error> {
Ok(self.at_client.send_retry::<Cmd>(cmd).await?)
}
}
34 changes: 1 addition & 33 deletions src/asynch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,12 @@
pub mod control;
mod network;
mod pwr;
mod resources;
pub mod runner;
pub mod state;
mod urc_handler;

use embedded_io_async::{BufRead, Error as _, ErrorKind, Read, Write};
pub use resources::Resources;
pub use runner::Runner;
#[cfg(feature = "internal-network-stack")]
pub use state::Device;

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

impl<R, W> embedded_io_async::ErrorType for ReadWriteAdapter<R, W> {
type Error = 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: BufRead, W> BufRead for ReadWriteAdapter<R, W> {
async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
self.0.fill_buf().await.map_err(|e| e.kind())
}

fn consume(&mut self, amt: usize) {
self.0.consume(amt)
}
}

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())
}
}
Loading

0 comments on commit 5a4572a

Please sign in to comment.