Wrap buffers in a single state type

This commit is contained in:
Ulf Lilleengen 2022-08-09 14:43:55 +02:00
parent 18671b94ba
commit 80c1551153
2 changed files with 31 additions and 23 deletions

View File

@ -339,15 +339,16 @@ pub mod client {
use super::*; use super::*;
/// TCP client capable of creating up to N multiple connections with tx and rx buffers according to TX_SZ and RX_SZ.
pub struct TcpClient<'d, D: Device, const N: usize, const TX_SZ: usize = 1024, const RX_SZ: usize = 1024> { pub struct TcpClient<'d, D: Device, const N: usize, const TX_SZ: usize = 1024, const RX_SZ: usize = 1024> {
stack: &'d Stack<D>, stack: &'d Stack<D>,
tx: &'d BufferPool<TX_SZ, N>, state: &'d TcpClientState<N, TX_SZ, RX_SZ>,
rx: &'d BufferPool<RX_SZ, N>,
} }
impl<'d, D: Device, const N: usize, const TX_SZ: usize, const RX_SZ: usize> TcpClient<'d, D, N, TX_SZ, RX_SZ> { impl<'d, D: Device, const N: usize, const TX_SZ: usize, const RX_SZ: usize> TcpClient<'d, D, N, TX_SZ, RX_SZ> {
pub fn new(stack: &'d Stack<D>, tx: &'d BufferPool<TX_SZ, N>, rx: &'d BufferPool<RX_SZ, N>) -> Self { /// Create a new TcpClient
Self { stack, tx, rx } pub fn new(stack: &'d Stack<D>, state: &'d TcpClientState<N, TX_SZ, RX_SZ>) -> Self {
Self { stack, state }
} }
} }
@ -370,7 +371,7 @@ pub mod client {
IpAddr::V6(_) => panic!("ipv6 support not enabled"), IpAddr::V6(_) => panic!("ipv6 support not enabled"),
}; };
let remote_endpoint = (addr, remote.port()); let remote_endpoint = (addr, remote.port());
let mut socket = TcpConnection::new(&self.stack, self.tx, self.rx)?; let mut socket = TcpConnection::new(&self.stack, self.state)?;
socket socket
.socket .socket
.connect(remote_endpoint) .connect(remote_endpoint)
@ -383,26 +384,20 @@ pub mod client {
pub struct TcpConnection<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> { pub struct TcpConnection<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> {
socket: TcpSocket<'d>, socket: TcpSocket<'d>,
tx: &'d BufferPool<TX_SZ, N>, state: &'d TcpClientState<N, TX_SZ, RX_SZ>,
rx: &'d BufferPool<RX_SZ, N>, bufs: NonNull<([u8; TX_SZ], [u8; RX_SZ])>,
txb: NonNull<[u8; TX_SZ]>,
rxb: NonNull<[u8; RX_SZ]>,
} }
impl<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> TcpConnection<'d, N, TX_SZ, RX_SZ> { impl<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> TcpConnection<'d, N, TX_SZ, RX_SZ> {
fn new<D: Device>( fn new<D: Device>(
stack: &'d Stack<D>, stack: &'d Stack<D>,
tx: &'d BufferPool<TX_SZ, N>, state: &'d TcpClientState<N, TX_SZ, RX_SZ>,
rx: &'d BufferPool<RX_SZ, N>,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
let mut txb = tx.alloc().ok_or(Error::ConnectionReset)?; let mut bufs = state.pool.alloc().ok_or(Error::ConnectionReset)?;
let mut rxb = rx.alloc().ok_or(Error::ConnectionReset)?;
Ok(Self { Ok(Self {
socket: unsafe { TcpSocket::new(stack, rxb.as_mut(), txb.as_mut()) }, socket: unsafe { TcpSocket::new(stack, &mut bufs.as_mut().0, &mut bufs.as_mut().1) },
tx, state,
rx, bufs,
txb,
rxb,
}) })
} }
} }
@ -411,8 +406,7 @@ pub mod client {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
self.socket.close(); self.socket.close();
self.rx.free(self.rxb); self.state.pool.free(self.bufs);
self.tx.free(self.txb);
} }
} }
} }
@ -455,9 +449,22 @@ pub mod client {
} }
} }
pub type BufferPool<const BUFSZ: usize, const N: usize> = Pool<[u8; BUFSZ], N>; /// State for TcpClient
pub struct TcpClientState<const N: usize, const TX_SZ: usize, const RX_SZ: usize> {
pool: Pool<([u8; TX_SZ], [u8; RX_SZ]), N>,
}
pub struct Pool<T, const N: usize> { impl<const N: usize, const TX_SZ: usize, const RX_SZ: usize> TcpClientState<N, TX_SZ, RX_SZ> {
pub const fn new() -> Self {
Self {
pool: Pool::new()
}
}
}
unsafe impl<const N: usize, const TX_SZ: usize, const RX_SZ: usize> Sync for TcpClientState<N, TX_SZ, RX_SZ> {}
struct Pool<T, const N: usize> {
used: [AtomicBool; N], used: [AtomicBool; N],
data: [UnsafeCell<MaybeUninit<T>>; N], data: [UnsafeCell<MaybeUninit<T>>; N],
} }
@ -466,7 +473,7 @@ pub mod client {
const VALUE: AtomicBool = AtomicBool::new(false); const VALUE: AtomicBool = AtomicBool::new(false);
const UNINIT: UnsafeCell<MaybeUninit<T>> = UnsafeCell::new(MaybeUninit::uninit()); const UNINIT: UnsafeCell<MaybeUninit<T>> = UnsafeCell::new(MaybeUninit::uninit());
pub const fn new() -> Self { const fn new() -> Self {
Self { Self {
used: [Self::VALUE; N], used: [Self::VALUE; N],
data: [Self::UNINIT; N], data: [Self::UNINIT; N],

View File

@ -18,6 +18,7 @@ cortex-m-rt = "0.7.0"
embedded-hal = "0.2.6" embedded-hal = "0.2.6"
embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" } embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" }
embedded-hal-async = { version = "0.1.0-alpha.1" } embedded-hal-async = { version = "0.1.0-alpha.1" }
embedded-nal-async = "0.2.0"
panic-probe = { version = "0.3", features = ["print-defmt"] } panic-probe = { version = "0.3", features = ["print-defmt"] }
futures = { version = "0.3.17", default-features = false, features = ["async-await"] } futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
heapless = { version = "0.7.5", default-features = false } heapless = { version = "0.7.5", default-features = false }