Skip to content

Commit

Permalink
Move ppp and internal-stack to separate modules
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasKoch committed Feb 21, 2024
1 parent 6a7a031 commit 25e398a
Show file tree
Hide file tree
Showing 8 changed files with 539 additions and 524 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ atat = { version = "*", features = ["heapless"] }


[features]
default = ["socket-udp", "socket-tcp", "async"]
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"]
Expand Down
4 changes: 0 additions & 4 deletions src/asynch/control.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use core::future::poll_fn;
use core::task::Poll;

use atat::asynch::AtatClient;
use embassy_time::{with_timeout, Duration};

use crate::error::Error;

Expand Down
111 changes: 111 additions & 0 deletions src/asynch/internal_stack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// pub mod ublox_stack;

use core::mem::MaybeUninit;

use atat::{asynch::Client, AtatIngress};
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, mutex::Mutex};
use embedded_io_async::{Read, Write};

use crate::{command::Urc, config::CellularConfig};

pub use super::resources::UbxResources as Resources;

use super::{control::Control, runner::Runner, state, AtHandle};

pub fn new_internal<
'a,
R: Read,
W: Write,
C: CellularConfig<'a>,
const CMD_BUF_SIZE: usize,
const INGRESS_BUF_SIZE: usize,
const URC_CAPACITY: usize,
>(
reader: R,
writer: W,
resources: &'a mut Resources<W, CMD_BUF_SIZE, INGRESS_BUF_SIZE, URC_CAPACITY>,
config: C,
) -> (
state::Device<'a, Client<'a, W, INGRESS_BUF_SIZE>, URC_CAPACITY>,
Control<'a, Client<'a, W, INGRESS_BUF_SIZE>>,
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.
// - when the borrow ends, the dangling references inside the MaybeUninit will never be used again.
let at_client_uninit: *mut MaybeUninit<Mutex<NoopRawMutex, Client<'a, W, INGRESS_BUF_SIZE>>> =
(&mut resources.at_client
as *mut MaybeUninit<Mutex<NoopRawMutex, Client<'static, W, INGRESS_BUF_SIZE>>>)
.cast();

unsafe { &mut *at_client_uninit }.write(Mutex::new(Client::new(
writer,
&resources.res_slot,
&mut resources.cmd_buf,
atat::Config::default(),
)));

let at_client = unsafe { (&*at_client_uninit).assume_init_ref() };

let (ch_runner, net_device) = state::new(
&mut resources.ch,
AtHandle(at_client),
resources.urc_channel.subscribe().unwrap(),
);

let control = Control::new(ch_runner.state_runner(), AtHandle(at_client));

let runner = Runner::new(
ch_runner,
AtHandle(at_client),
config,
resources.urc_channel.subscribe().unwrap(),
);

let ingress = atat::Ingress::new(
atat::AtDigester::<Urc>::new(),
&mut resources.ingress_buf,
&resources.res_slot,
&resources.urc_channel,
);

let runner = InternalRunner {
cellular_runner: runner,
ingress,
reader,
};

(net_device, control, runner)
}

pub struct InternalRunner<
'a,
R: Read,
W: Write,
C: CellularConfig<'a>,
const INGRESS_BUF_SIZE: usize,
const URC_CAPACITY: usize,
> {
pub cellular_runner: Runner<'a, Client<'a, W, INGRESS_BUF_SIZE>, C, URC_CAPACITY>,
pub ingress: atat::Ingress<'a, atat::AtDigester<Urc>, Urc, INGRESS_BUF_SIZE, URC_CAPACITY, 2>,
pub reader: R,
}

impl<
'a,
R: Read,
W: Write,
C: CellularConfig<'a>,
const INGRESS_BUF_SIZE: usize,
const URC_CAPACITY: usize,
> InternalRunner<'a, R, W, C, INGRESS_BUF_SIZE, URC_CAPACITY>
{
pub async fn run(&mut self) -> ! {
embassy_futures::join::join(
self.ingress.read_from(&mut self.reader),
self.cellular_runner.run(),
)
.await;
core::unreachable!()
}
}
Loading

0 comments on commit 25e398a

Please sign in to comment.