aboutsummaryrefslogtreecommitdiff
path: root/embassy-net/src/stack.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-net/src/stack.rs')
-rw-r--r--embassy-net/src/stack.rs54
1 files changed, 29 insertions, 25 deletions
diff --git a/embassy-net/src/stack.rs b/embassy-net/src/stack.rs
index a38f00958..5e795a4fd 100644
--- a/embassy-net/src/stack.rs
+++ b/embassy-net/src/stack.rs
@@ -4,7 +4,7 @@ use core::task::Context;
4use core::task::Poll; 4use core::task::Poll;
5use embassy::time::{Instant, Timer}; 5use embassy::time::{Instant, Timer};
6use embassy::util::ThreadModeMutex; 6use embassy::util::ThreadModeMutex;
7use embassy::util::{Forever, WakerRegistration}; 7use embassy::util::WakerRegistration;
8use futures::pin_mut; 8use futures::pin_mut;
9use smoltcp::iface::InterfaceBuilder; 9use smoltcp::iface::InterfaceBuilder;
10#[cfg(feature = "medium-ethernet")] 10#[cfg(feature = "medium-ethernet")]
@@ -22,23 +22,34 @@ use crate::config::Event;
22use crate::device::{Device, DeviceAdapter, LinkState}; 22use crate::device::{Device, DeviceAdapter, LinkState};
23use crate::{Interface, SocketSet}; 23use crate::{Interface, SocketSet};
24 24
25const ADDRESSES_LEN: usize = 1;
26const NEIGHBOR_CACHE_LEN: usize = 8;
27const SOCKETS_LEN: usize = 2;
28const LOCAL_PORT_MIN: u16 = 1025; 25const LOCAL_PORT_MIN: u16 = 1025;
29const LOCAL_PORT_MAX: u16 = 65535; 26const LOCAL_PORT_MAX: u16 = 65535;
30 27
31struct StackResources { 28pub struct StackResources<const ADDR: usize, const SOCK: usize, const NEIGHBOR: usize> {
32 addresses: [IpCidr; ADDRESSES_LEN], 29 addresses: [IpCidr; ADDR],
33 sockets: [Option<SocketSetItem<'static>>; SOCKETS_LEN], 30 sockets: [Option<SocketSetItem<'static>>; SOCK],
34 31
35 #[cfg(feature = "medium-ethernet")] 32 #[cfg(feature = "medium-ethernet")]
36 routes: [Option<(IpCidr, Route)>; 1], 33 routes: [Option<(IpCidr, Route)>; 1],
37 #[cfg(feature = "medium-ethernet")] 34 #[cfg(feature = "medium-ethernet")]
38 neighbor_cache: [Option<(IpAddress, Neighbor)>; NEIGHBOR_CACHE_LEN], 35 neighbor_cache: [Option<(IpAddress, Neighbor)>; NEIGHBOR],
36}
37
38impl<const ADDR: usize, const SOCK: usize, const NEIGHBOR: usize>
39 StackResources<ADDR, SOCK, NEIGHBOR>
40{
41 pub fn new() -> Self {
42 const NONE_SOCKET: Option<SocketSetItem<'static>> = None;
43
44 Self {
45 addresses: [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 32); ADDR],
46 sockets: [NONE_SOCKET; SOCK],
47 routes: [None; 1],
48 neighbor_cache: [None; NEIGHBOR],
49 }
50 }
39} 51}
40 52
41static STACK_RESOURCES: Forever<StackResources> = Forever::new();
42static STACK: ThreadModeMutex<RefCell<Option<Stack>>> = ThreadModeMutex::new(RefCell::new(None)); 53static STACK: ThreadModeMutex<RefCell<Option<Stack>>> = ThreadModeMutex::new(RefCell::new(None));
43 54
44pub(crate) struct Stack { 55pub(crate) struct Stack {
@@ -164,18 +175,11 @@ fn set_ipv4_addr(iface: &mut Interface, cidr: Ipv4Cidr) {
164 175
165/// Initialize embassy_net. 176/// Initialize embassy_net.
166/// This function must be called from thread mode. 177/// This function must be called from thread mode.
167pub fn init(device: &'static mut dyn Device, configurator: &'static mut dyn Configurator) { 178pub fn init<const ADDR: usize, const SOCK: usize, const NEIGH: usize>(
168 const NONE_SOCKET: Option<SocketSetItem<'static>> = None; 179 device: &'static mut dyn Device,
169 let res = STACK_RESOURCES.put(StackResources { 180 configurator: &'static mut dyn Configurator,
170 addresses: [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 32)], 181 resources: &'static mut StackResources<ADDR, SOCK, NEIGH>,
171 sockets: [NONE_SOCKET; SOCKETS_LEN], 182) {
172
173 #[cfg(feature = "medium-ethernet")]
174 routes: [None; 1],
175 #[cfg(feature = "medium-ethernet")]
176 neighbor_cache: [None; NEIGHBOR_CACHE_LEN],
177 });
178
179 let medium = device.capabilities().medium; 183 let medium = device.capabilities().medium;
180 184
181 #[cfg(feature = "medium-ethernet")] 185 #[cfg(feature = "medium-ethernet")]
@@ -186,18 +190,18 @@ pub fn init(device: &'static mut dyn Device, configurator: &'static mut dyn Conf
186 }; 190 };
187 191
188 let mut b = InterfaceBuilder::new(DeviceAdapter::new(device)); 192 let mut b = InterfaceBuilder::new(DeviceAdapter::new(device));
189 b = b.ip_addrs(&mut res.addresses[..]); 193 b = b.ip_addrs(&mut resources.addresses[..]);
190 194
191 #[cfg(feature = "medium-ethernet")] 195 #[cfg(feature = "medium-ethernet")]
192 if medium == Medium::Ethernet { 196 if medium == Medium::Ethernet {
193 b = b.ethernet_addr(EthernetAddress(ethernet_addr)); 197 b = b.ethernet_addr(EthernetAddress(ethernet_addr));
194 b = b.neighbor_cache(NeighborCache::new(&mut res.neighbor_cache[..])); 198 b = b.neighbor_cache(NeighborCache::new(&mut resources.neighbor_cache[..]));
195 b = b.routes(Routes::new(&mut res.routes[..])); 199 b = b.routes(Routes::new(&mut resources.routes[..]));
196 } 200 }
197 201
198 let iface = b.finalize(); 202 let iface = b.finalize();
199 203
200 let sockets = SocketSet::new(&mut res.sockets[..]); 204 let sockets = SocketSet::new(&mut resources.sockets[..]);
201 205
202 let local_port = loop { 206 let local_port = loop {
203 let mut res = [0u8; 2]; 207 let mut res = [0u8; 2];