Skip to content

Commit

Permalink
make subscriber count a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
tarfu committed Dec 4, 2023
1 parent d78c607 commit 032393a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 37 deletions.
3 changes: 1 addition & 2 deletions examples/embassy-stm32-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ bind_interrupts!(struct Irqs {
const INGRESS_BUF_SIZE: usize = 1024;
const URC_CAPACITY: usize = 2;
const URC_SUBSCRIBERS: usize = 2;
const MAX_DESIRED_STATE_LISTENERS: usize = 5;

struct MyCelullarConfig {
reset_pin: Option<Output<'static, AnyPin>>,
Expand Down Expand Up @@ -213,7 +212,7 @@ async fn ingress_task(

#[embassy_executor::task]
async fn cellular_task(
runner: Runner<'static, atat::asynch::Client<'_, BufferedUartTx<'static, UART8>, {INGRESS_BUF_SIZE}>, MyCelullarConfig, {URC_CAPACITY}, {MAX_DESIRED_STATE_LISTENERS}>,
runner: Runner<'static, atat::asynch::Client<'_, BufferedUartTx<'static, UART8>, {INGRESS_BUF_SIZE}>, MyCelullarConfig, {URC_CAPACITY}>,
) -> ! {
runner.run().await
}
Expand Down
8 changes: 4 additions & 4 deletions src/asynch/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use crate::error::Error;
use super::state::{LinkState, PowerState};
use super::{state, AtHandle};

pub struct Control<'a, AT: AtatClient, const MAX_STATE_LISTENERS: usize> {
state_ch: state::StateRunner<'a, MAX_STATE_LISTENERS>,
pub struct Control<'a, AT: AtatClient> {
state_ch: state::StateRunner<'a>,
at: AtHandle<'a, AT>,
}

impl<'a, AT: AtatClient, const MAX_STATE_LISTENERS: usize> Control<'a, AT, MAX_STATE_LISTENERS> {
impl<'a, AT: AtatClient> Control<'a, AT> {
pub(crate) fn new(
state_ch: state::StateRunner<'a, MAX_STATE_LISTENERS>,
state_ch: state::StateRunner<'a>,
at: AtHandle<'a, AT>,
) -> Self {
Self { state_ch, at }
Expand Down
16 changes: 8 additions & 8 deletions src/asynch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ impl<'d, AT: AtatClient> AtHandle<'d, AT> {
}
}

pub struct State<AT: AtatClient, const MAX_STATE_LISTENERS: usize> {
ch: state::State<MAX_STATE_LISTENERS>,
pub struct State<AT: AtatClient> {
ch: state::State,
at_handle: Mutex<NoopRawMutex, AT>,
}

impl<AT: AtatClient, const MAX_STATE_LISTENERS: usize> State<AT, MAX_STATE_LISTENERS> {
impl<AT: AtatClient> State<AT> {
pub fn new(at_handle: AT) -> Self {
Self {
ch: state::State::new(),
Expand All @@ -44,15 +44,15 @@ pub async fn new<
SUB: AtatUrcChannel<Urc, URC_CAPACITY, 2>,
C: CellularConfig,
const URC_CAPACITY: usize,
const MAX_STATE_LISTENERS: usize,

>(
state: &'a mut State<AT, MAX_STATE_LISTENERS>,
state: &'a mut State<AT>,
subscriber: &'a SUB,
config: C,
) -> (
Device<'a, AT, URC_CAPACITY, MAX_STATE_LISTENERS>,
Control<'a, AT, MAX_STATE_LISTENERS>,
Runner<'a, AT, C, URC_CAPACITY, MAX_STATE_LISTENERS>,
Device<'a, AT, URC_CAPACITY>,
Control<'a, AT>,
Runner<'a, AT, C, URC_CAPACITY>,
) {
let (ch_runner, net_device) = state::new(
&mut state.ch,
Expand Down
10 changes: 5 additions & 5 deletions src/asynch/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ pub struct Runner<
AT: AtatClient,
C: CellularConfig,
const URC_CAPACITY: usize,
const MAX_STATE_LISTENERS: usize,

> {
ch: state::Runner<'d, MAX_STATE_LISTENERS>,
ch: state::Runner<'d>,
at: AtHandle<'d, AT>,
config: C,
urc_subscription: UrcSubscription<'d, Urc, URC_CAPACITY, 2>,
Expand All @@ -55,11 +55,11 @@ impl<
AT: AtatClient,
C: CellularConfig,
const URC_CAPACITY: usize,
const MAX_STATE_LISTENERS: usize,
> Runner<'d, AT, C, URC_CAPACITY, MAX_STATE_LISTENERS>

> Runner<'d, AT, C, URC_CAPACITY>
{
pub(crate) fn new(
ch: state::Runner<'d, MAX_STATE_LISTENERS>,
ch: state::Runner<'d>,
at: AtHandle<'d, AT>,
config: C,
urc_subscription: UrcSubscription<'d, Urc, URC_CAPACITY, 2>,
Expand Down
39 changes: 21 additions & 18 deletions src/asynch/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use embassy_sync::blocking_mutex::Mutex;
use embassy_sync::pubsub::PubSubChannel;
use embassy_sync::waitqueue::WakerRegistration;


const MAX_STATE_LISTENERS: usize = 5;

/// The link state of a network device.
#[derive(PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
Expand Down Expand Up @@ -38,19 +41,19 @@ use crate::error::Error;

use super::AtHandle;

pub struct State<const MAX_STATE_LISTENERS: usize> {
inner: MaybeUninit<StateInner<MAX_STATE_LISTENERS>>,
pub struct State {
inner: MaybeUninit<StateInner>,
}

impl<const MAX_STATE_LISTENERS: usize> State<MAX_STATE_LISTENERS> {
impl State {
pub const fn new() -> Self {
Self {
inner: MaybeUninit::uninit(),
}
}
}

struct StateInner<const MAX_STATE_LISTENERS: usize> {
struct StateInner {
shared: Mutex<NoopRawMutex, RefCell<Shared>>,
desired_state_pub_sub: PubSubChannel<NoopRawMutex, PowerState, 1, MAX_STATE_LISTENERS, 1>,
}
Expand All @@ -63,19 +66,19 @@ pub struct Shared {
waker: WakerRegistration,
}

pub struct Runner<'d, const MAX_STATE_LISTENERS: usize> {
pub struct Runner<'d> {
shared: &'d Mutex<NoopRawMutex, RefCell<Shared>>,
desired_state_pub_sub: &'d PubSubChannel<NoopRawMutex, PowerState, 1, MAX_STATE_LISTENERS, 1>,
}

#[derive(Clone, Copy)]
pub struct StateRunner<'d, const MAX_STATE_LISTENERS: usize> {
pub struct StateRunner<'d> {
shared: &'d Mutex<NoopRawMutex, RefCell<Shared>>,
desired_state_pub_sub: &'d PubSubChannel<NoopRawMutex, PowerState, 1, MAX_STATE_LISTENERS, 1>,
}

impl<'d, const MAX_STATE_LISTENERS: usize> Runner<'d, MAX_STATE_LISTENERS> {
pub fn state_runner(&self) -> StateRunner<'d, MAX_STATE_LISTENERS> {
impl<'d> Runner<'d> {
pub fn state_runner(&self) -> StateRunner<'d> {
StateRunner {
shared: self.shared,
desired_state_pub_sub: self.desired_state_pub_sub,
Expand Down Expand Up @@ -110,7 +113,7 @@ impl<'d, const MAX_STATE_LISTENERS: usize> Runner<'d, MAX_STATE_LISTENERS> {
}
}

impl<'d, const MAX_STATE_LISTENERS: usize> StateRunner<'d, MAX_STATE_LISTENERS> {
impl<'d> StateRunner<'d> {
pub fn set_link_state(&self, state: LinkState) {
self.shared.lock(|s| {
let s = &mut *s.borrow_mut();
Expand Down Expand Up @@ -201,19 +204,19 @@ impl<'d, const MAX_STATE_LISTENERS: usize> StateRunner<'d, MAX_STATE_LISTENERS>
}
}

pub fn new<'d, AT: AtatClient, const URC_CAPACITY: usize, const MAX_STATE_LISTENERS: usize>(
state: &'d mut State<MAX_STATE_LISTENERS>,
pub fn new<'d, AT: AtatClient, const URC_CAPACITY: usize>(
state: &'d mut State,
at: AtHandle<'d, AT>,
urc_subscription: UrcSubscription<'d, Urc, URC_CAPACITY, 2>,
) -> (
Runner<'d, MAX_STATE_LISTENERS>,
Device<'d, AT, URC_CAPACITY, MAX_STATE_LISTENERS>,
Runner<'d>,
Device<'d, AT, URC_CAPACITY>,
) {
// safety: this is a self-referential struct, however:
// - it can't move while the `'d` borrow is active.
// - when the borrow ends, the dangling references inside the MaybeUninit will never be used again.
let state_uninit: *mut MaybeUninit<StateInner<MAX_STATE_LISTENERS>> =
(&mut state.inner as *mut MaybeUninit<StateInner<MAX_STATE_LISTENERS>>).cast();
let state_uninit: *mut MaybeUninit<StateInner> =
(&mut state.inner as *mut MaybeUninit<StateInner>).cast();

let state = unsafe { &mut *state_uninit }.write(StateInner {
shared: Mutex::new(RefCell::new(Shared {
Expand All @@ -240,16 +243,16 @@ pub fn new<'d, AT: AtatClient, const URC_CAPACITY: usize, const MAX_STATE_LISTEN
)
}

pub struct Device<'d, AT: AtatClient, const URC_CAPACITY: usize, const MAX_STATE_LISTENERS: usize> {
pub struct Device<'d, AT: AtatClient, const URC_CAPACITY: usize> {
pub(crate) shared: &'d Mutex<NoopRawMutex, RefCell<Shared>>,
pub(crate) desired_state_pub_sub:
&'d PubSubChannel<NoopRawMutex, PowerState, 1, MAX_STATE_LISTENERS, 1>,
pub(crate) at: AtHandle<'d, AT>,
pub(crate) urc_subscription: UrcSubscription<'d, Urc, URC_CAPACITY, 2>,
}

impl<'d, AT: AtatClient, const URC_CAPACITY: usize, const MAX_STATE_LISTENERS: usize>
Device<'d, AT, URC_CAPACITY, MAX_STATE_LISTENERS>
impl<'d, AT: AtatClient, const URC_CAPACITY: usize>
Device<'d, AT, URC_CAPACITY>
{
pub fn link_state_poll_fn(&mut self, cx: &mut Context) -> LinkState {
self.shared.lock(|s| {
Expand Down

0 comments on commit 032393a

Please sign in to comment.