1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
use core::cell::RefCell;
use core::future::Future;
use core::task::Context;
use core::task::Poll;
use embassy::time::{Instant, Timer};
use embassy::util::ThreadModeMutex;
use embassy::util::WakerRegistration;
use futures::pin_mut;
use smoltcp::iface::InterfaceBuilder;
#[cfg(feature = "medium-ethernet")]
use smoltcp::iface::{Neighbor, NeighborCache, Route, Routes};
#[cfg(feature = "medium-ethernet")]
use smoltcp::phy::Device as _;
#[cfg(feature = "medium-ethernet")]
use smoltcp::phy::Medium;
use smoltcp::socket::SocketSetItem;
use smoltcp::time::Instant as SmolInstant;
#[cfg(feature = "medium-ethernet")]
use smoltcp::wire::EthernetAddress;
#[cfg(feature = "medium-ethernet")]
use smoltcp::wire::IpAddress;
use smoltcp::wire::{IpCidr, Ipv4Address, Ipv4Cidr};
use crate::config::Configurator;
use crate::config::Event;
use crate::device::{Device, DeviceAdapter, LinkState};
use crate::{Interface, SocketSet};
const LOCAL_PORT_MIN: u16 = 1025;
const LOCAL_PORT_MAX: u16 = 65535;
pub struct StackResources<const ADDR: usize, const SOCK: usize, const NEIGHBOR: usize> {
addresses: [IpCidr; ADDR],
sockets: [Option<SocketSetItem<'static>>; SOCK],
#[cfg(feature = "medium-ethernet")]
routes: [Option<(IpCidr, Route)>; 1],
#[cfg(feature = "medium-ethernet")]
neighbor_cache: [Option<(IpAddress, Neighbor)>; NEIGHBOR],
}
impl<const ADDR: usize, const SOCK: usize, const NEIGHBOR: usize>
StackResources<ADDR, SOCK, NEIGHBOR>
{
pub fn new() -> Self {
const NONE_SOCKET: Option<SocketSetItem<'static>> = None;
Self {
addresses: [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 32); ADDR],
sockets: [NONE_SOCKET; SOCK],
#[cfg(feature = "medium-ethernet")]
routes: [None; 1],
#[cfg(feature = "medium-ethernet")]
neighbor_cache: [None; NEIGHBOR],
}
}
}
static STACK: ThreadModeMutex<RefCell<Option<Stack>>> = ThreadModeMutex::new(RefCell::new(None));
pub(crate) struct Stack {
iface: Interface,
pub sockets: SocketSet,
link_up: bool,
config_up: bool,
next_local_port: u16,
configurator: &'static mut dyn Configurator,
waker: WakerRegistration,
}
impl Stack {
pub(crate) fn with<R>(f: impl FnOnce(&mut Stack) -> R) -> R {
let mut stack = STACK.borrow().borrow_mut();
let stack = stack.as_mut().unwrap();
f(stack)
}
pub fn get_local_port(&mut self) -> u16 {
let res = self.next_local_port;
self.next_local_port = if res >= LOCAL_PORT_MAX {
LOCAL_PORT_MIN
} else {
res + 1
};
res
}
pub(crate) fn wake(&mut self) {
self.waker.wake()
}
fn poll_configurator(&mut self, timestamp: SmolInstant) {
#[cfg(feature = "medium-ethernet")]
let medium = self.iface.device().capabilities().medium;
match self
.configurator
.poll(&mut self.iface, &mut self.sockets, timestamp)
{
Event::NoChange => {}
Event::Configured(config) => {
debug!("Acquired IP configuration:");
debug!(" IP address: {}", config.address);
set_ipv4_addr(&mut self.iface, config.address);
#[cfg(feature = "medium-ethernet")]
if medium == Medium::Ethernet {
if let Some(gateway) = config.gateway {
debug!(" Default gateway: {}", gateway);
self.iface
.routes_mut()
.add_default_ipv4_route(gateway)
.unwrap();
} else {
debug!(" Default gateway: None");
self.iface.routes_mut().remove_default_ipv4_route();
}
}
for (i, s) in config.dns_servers.iter().enumerate() {
debug!(" DNS server {}: {}", i, s);
}
self.config_up = true;
}
Event::Deconfigured => {
debug!("Lost IP configuration");
set_ipv4_addr(&mut self.iface, Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0));
#[cfg(feature = "medium-ethernet")]
if medium == Medium::Ethernet {
self.iface.routes_mut().remove_default_ipv4_route();
}
self.config_up = false;
}
}
}
fn poll(&mut self, cx: &mut Context<'_>) {
self.iface.device_mut().device.register_waker(cx.waker());
self.waker.register(cx.waker());
let timestamp = instant_to_smoltcp(Instant::now());
if let Err(_) = self.iface.poll(&mut self.sockets, timestamp) {
// If poll() returns error, it may not be done yet, so poll again later.
cx.waker().wake_by_ref();
return;
}
// Update link up
let old_link_up = self.link_up;
self.link_up = self.iface.device_mut().device.link_state() == LinkState::Up;
// Print when changed
if old_link_up != self.link_up {
if self.link_up {
info!("Link up!");
} else {
info!("Link down!");
}
}
if old_link_up || self.link_up {
self.poll_configurator(timestamp)
}
if let Some(poll_at) = self.iface.poll_at(&mut self.sockets, timestamp) {
let t = Timer::at(instant_from_smoltcp(poll_at));
pin_mut!(t);
if t.poll(cx).is_ready() {
cx.waker().wake_by_ref();
}
}
}
}
fn set_ipv4_addr(iface: &mut Interface, cidr: Ipv4Cidr) {
iface.update_ip_addrs(|addrs| {
let dest = addrs.iter_mut().next().unwrap();
*dest = IpCidr::Ipv4(cidr);
});
}
/// Initialize embassy_net.
/// This function must be called from thread mode.
pub fn init<const ADDR: usize, const SOCK: usize, const NEIGH: usize>(
device: &'static mut dyn Device,
configurator: &'static mut dyn Configurator,
resources: &'static mut StackResources<ADDR, SOCK, NEIGH>,
) {
#[cfg(feature = "medium-ethernet")]
let medium = device.capabilities().medium;
#[cfg(feature = "medium-ethernet")]
let ethernet_addr = if medium == Medium::Ethernet {
device.ethernet_address()
} else {
[0, 0, 0, 0, 0, 0]
};
let mut b = InterfaceBuilder::new(DeviceAdapter::new(device));
b = b.ip_addrs(&mut resources.addresses[..]);
#[cfg(feature = "medium-ethernet")]
if medium == Medium::Ethernet {
b = b.ethernet_addr(EthernetAddress(ethernet_addr));
b = b.neighbor_cache(NeighborCache::new(&mut resources.neighbor_cache[..]));
b = b.routes(Routes::new(&mut resources.routes[..]));
}
let iface = b.finalize();
let sockets = SocketSet::new(&mut resources.sockets[..]);
let local_port = loop {
let mut res = [0u8; 2];
rand(&mut res);
let port = u16::from_le_bytes(res);
if port >= LOCAL_PORT_MIN && port <= LOCAL_PORT_MAX {
break port;
}
};
let stack = Stack {
iface,
sockets,
link_up: false,
config_up: false,
configurator,
next_local_port: local_port,
waker: WakerRegistration::new(),
};
*STACK.borrow().borrow_mut() = Some(stack);
}
pub fn is_init() -> bool {
STACK.borrow().borrow().is_some()
}
pub fn is_link_up() -> bool {
STACK.borrow().borrow().as_ref().unwrap().link_up
}
pub fn is_config_up() -> bool {
STACK.borrow().borrow().as_ref().unwrap().config_up
}
pub async fn run() {
futures::future::poll_fn(|cx| {
Stack::with(|stack| stack.poll(cx));
Poll::<()>::Pending
})
.await
}
fn instant_to_smoltcp(instant: Instant) -> SmolInstant {
SmolInstant::from_millis(instant.as_millis() as i64)
}
fn instant_from_smoltcp(instant: SmolInstant) -> Instant {
Instant::from_millis(instant.total_millis() as u64)
}
extern "Rust" {
fn _embassy_rand(buf: &mut [u8]);
}
fn rand(buf: &mut [u8]) {
unsafe { _embassy_rand(buf) }
}
|