diff options
Diffstat (limited to 'embassy-net/src')
| -rw-r--r-- | embassy-net/src/config/dhcp.rs | 59 | ||||
| -rw-r--r-- | embassy-net/src/config/mod.rs | 38 | ||||
| -rw-r--r-- | embassy-net/src/config/statik.rs | 35 | ||||
| -rw-r--r-- | embassy-net/src/device.rs | 105 | ||||
| -rw-r--r-- | embassy-net/src/fmt.rs | 118 | ||||
| -rw-r--r-- | embassy-net/src/lib.rs | 31 | ||||
| -rw-r--r-- | embassy-net/src/packet_pool.rs | 92 | ||||
| -rw-r--r-- | embassy-net/src/stack.rs | 259 | ||||
| -rw-r--r-- | embassy-net/src/tcp_socket.rs | 177 |
9 files changed, 914 insertions, 0 deletions
diff --git a/embassy-net/src/config/dhcp.rs b/embassy-net/src/config/dhcp.rs new file mode 100644 index 000000000..f0c144321 --- /dev/null +++ b/embassy-net/src/config/dhcp.rs | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | use heapless::Vec; | ||
| 2 | use smoltcp::socket::{Dhcpv4Event, Dhcpv4Socket, SocketHandle}; | ||
| 3 | use smoltcp::time::Instant; | ||
| 4 | |||
| 5 | use super::*; | ||
| 6 | use crate::device::LinkState; | ||
| 7 | use crate::fmt::*; | ||
| 8 | use crate::{Interface, SocketSet}; | ||
| 9 | |||
| 10 | pub struct DhcpConfigurator { | ||
| 11 | handle: Option<SocketHandle>, | ||
| 12 | } | ||
| 13 | |||
| 14 | impl DhcpConfigurator { | ||
| 15 | pub fn new() -> Self { | ||
| 16 | Self { handle: None } | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | impl Configurator for DhcpConfigurator { | ||
| 21 | fn poll( | ||
| 22 | &mut self, | ||
| 23 | iface: &mut Interface, | ||
| 24 | sockets: &mut SocketSet, | ||
| 25 | _timestamp: Instant, | ||
| 26 | ) -> Event { | ||
| 27 | if self.handle.is_none() { | ||
| 28 | let handle = sockets.add(Dhcpv4Socket::new()); | ||
| 29 | self.handle = Some(handle) | ||
| 30 | } | ||
| 31 | |||
| 32 | let mut socket = sockets.get::<Dhcpv4Socket>(self.handle.unwrap()); | ||
| 33 | |||
| 34 | let link_up = iface.device_mut().device.link_state() == LinkState::Up; | ||
| 35 | if !link_up { | ||
| 36 | socket.reset(); | ||
| 37 | return Event::Deconfigured; | ||
| 38 | } | ||
| 39 | |||
| 40 | match socket.poll() { | ||
| 41 | None => Event::NoChange, | ||
| 42 | Some(Dhcpv4Event::Deconfigured) => Event::Deconfigured, | ||
| 43 | Some(Dhcpv4Event::Configured(config)) => { | ||
| 44 | let mut dns_servers = Vec::new(); | ||
| 45 | for s in &config.dns_servers { | ||
| 46 | if let Some(addr) = s { | ||
| 47 | dns_servers.push(addr.clone()).unwrap(); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | Event::Configured(Config { | ||
| 52 | address: config.address, | ||
| 53 | gateway: config.router, | ||
| 54 | dns_servers, | ||
| 55 | }) | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
diff --git a/embassy-net/src/config/mod.rs b/embassy-net/src/config/mod.rs new file mode 100644 index 000000000..16470f7e6 --- /dev/null +++ b/embassy-net/src/config/mod.rs | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | use heapless::consts::*; | ||
| 2 | use heapless::Vec; | ||
| 3 | use smoltcp::time::Instant; | ||
| 4 | use smoltcp::wire::{Ipv4Address, Ipv4Cidr}; | ||
| 5 | |||
| 6 | use crate::fmt::*; | ||
| 7 | use crate::{Interface, SocketSet}; | ||
| 8 | |||
| 9 | mod statik; | ||
| 10 | pub use statik::StaticConfigurator; | ||
| 11 | |||
| 12 | #[cfg(feature = "dhcpv4")] | ||
| 13 | mod dhcp; | ||
| 14 | #[cfg(feature = "dhcpv4")] | ||
| 15 | pub use dhcp::DhcpConfigurator; | ||
| 16 | |||
| 17 | /// Return value for the `Configurator::poll` function | ||
| 18 | #[derive(Debug, Clone)] | ||
| 19 | pub enum Event { | ||
| 20 | /// No change has occured to the configuration. | ||
| 21 | NoChange, | ||
| 22 | /// Configuration has been lost (for example, DHCP lease has expired) | ||
| 23 | Deconfigured, | ||
| 24 | /// Configuration has been newly acquired, or modified. | ||
| 25 | Configured(Config), | ||
| 26 | } | ||
| 27 | |||
| 28 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
| 29 | pub struct Config { | ||
| 30 | pub address: Ipv4Cidr, | ||
| 31 | pub gateway: Option<Ipv4Address>, | ||
| 32 | pub dns_servers: Vec<Ipv4Address, U3>, | ||
| 33 | } | ||
| 34 | |||
| 35 | pub trait Configurator { | ||
| 36 | fn poll(&mut self, iface: &mut Interface, sockets: &mut SocketSet, timestamp: Instant) | ||
| 37 | -> Event; | ||
| 38 | } | ||
diff --git a/embassy-net/src/config/statik.rs b/embassy-net/src/config/statik.rs new file mode 100644 index 000000000..912143bff --- /dev/null +++ b/embassy-net/src/config/statik.rs | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | use smoltcp::time::Instant; | ||
| 2 | |||
| 3 | use super::*; | ||
| 4 | use crate::fmt::*; | ||
| 5 | use crate::{Interface, SocketSet}; | ||
| 6 | |||
| 7 | pub struct StaticConfigurator { | ||
| 8 | config: Config, | ||
| 9 | returned: bool, | ||
| 10 | } | ||
| 11 | |||
| 12 | impl StaticConfigurator { | ||
| 13 | pub fn new(config: Config) -> Self { | ||
| 14 | Self { | ||
| 15 | config, | ||
| 16 | returned: false, | ||
| 17 | } | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | impl Configurator for StaticConfigurator { | ||
| 22 | fn poll( | ||
| 23 | &mut self, | ||
| 24 | _iface: &mut Interface, | ||
| 25 | _sockets: &mut SocketSet, | ||
| 26 | _timestamp: Instant, | ||
| 27 | ) -> Event { | ||
| 28 | if self.returned { | ||
| 29 | Event::NoChange | ||
| 30 | } else { | ||
| 31 | self.returned = true; | ||
| 32 | Event::Configured(self.config.clone()) | ||
| 33 | } | ||
| 34 | } | ||
| 35 | } | ||
diff --git a/embassy-net/src/device.rs b/embassy-net/src/device.rs new file mode 100644 index 000000000..6c06b0605 --- /dev/null +++ b/embassy-net/src/device.rs | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | use core::task::Waker; | ||
| 2 | use smoltcp::phy::Device as SmolDevice; | ||
| 3 | use smoltcp::phy::DeviceCapabilities; | ||
| 4 | use smoltcp::time::Instant as SmolInstant; | ||
| 5 | |||
| 6 | use crate::fmt::*; | ||
| 7 | use crate::packet_pool::PacketBoxExt; | ||
| 8 | use crate::Result; | ||
| 9 | use crate::{Packet, PacketBox, PacketBuf}; | ||
| 10 | |||
| 11 | #[derive(PartialEq, Eq, Clone, Copy)] | ||
| 12 | pub enum LinkState { | ||
| 13 | Down, | ||
| 14 | Up, | ||
| 15 | } | ||
| 16 | |||
| 17 | pub trait Device { | ||
| 18 | fn is_transmit_ready(&mut self) -> bool; | ||
| 19 | fn transmit(&mut self, pkt: PacketBuf); | ||
| 20 | fn receive(&mut self) -> Option<PacketBuf>; | ||
| 21 | |||
| 22 | fn register_waker(&mut self, waker: &Waker); | ||
| 23 | fn capabilities(&mut self) -> DeviceCapabilities; | ||
| 24 | fn link_state(&mut self) -> LinkState; | ||
| 25 | fn ethernet_address(&mut self) -> [u8; 6]; | ||
| 26 | } | ||
| 27 | |||
| 28 | pub struct DeviceAdapter { | ||
| 29 | pub device: &'static mut dyn Device, | ||
| 30 | caps: DeviceCapabilities, | ||
| 31 | } | ||
| 32 | |||
| 33 | impl DeviceAdapter { | ||
| 34 | pub(crate) fn new(device: &'static mut dyn Device) -> Self { | ||
| 35 | Self { | ||
| 36 | caps: device.capabilities(), | ||
| 37 | device, | ||
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | impl<'a> SmolDevice<'a> for DeviceAdapter { | ||
| 43 | type RxToken = RxToken; | ||
| 44 | type TxToken = TxToken<'a>; | ||
| 45 | |||
| 46 | fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> { | ||
| 47 | let rx_pkt = self.device.receive()?; | ||
| 48 | let tx_pkt = PacketBox::new(Packet::new()).unwrap(); // TODO: not sure about unwrap | ||
| 49 | let rx_token = RxToken { pkt: rx_pkt }; | ||
| 50 | let tx_token = TxToken { | ||
| 51 | device: self.device, | ||
| 52 | pkt: tx_pkt, | ||
| 53 | }; | ||
| 54 | |||
| 55 | Some((rx_token, tx_token)) | ||
| 56 | } | ||
| 57 | |||
| 58 | /// Construct a transmit token. | ||
| 59 | fn transmit(&'a mut self) -> Option<Self::TxToken> { | ||
| 60 | if !self.device.is_transmit_ready() { | ||
| 61 | return None; | ||
| 62 | } | ||
| 63 | |||
| 64 | let tx_pkt = PacketBox::new(Packet::new())?; | ||
| 65 | Some(TxToken { | ||
| 66 | device: self.device, | ||
| 67 | pkt: tx_pkt, | ||
| 68 | }) | ||
| 69 | } | ||
| 70 | |||
| 71 | /// Get a description of device capabilities. | ||
| 72 | fn capabilities(&self) -> DeviceCapabilities { | ||
| 73 | self.caps.clone() | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | pub struct RxToken { | ||
| 78 | pkt: PacketBuf, | ||
| 79 | } | ||
| 80 | |||
| 81 | impl smoltcp::phy::RxToken for RxToken { | ||
| 82 | fn consume<R, F>(mut self, _timestamp: SmolInstant, f: F) -> Result<R> | ||
| 83 | where | ||
| 84 | F: FnOnce(&mut [u8]) -> Result<R>, | ||
| 85 | { | ||
| 86 | f(&mut self.pkt) | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | pub struct TxToken<'a> { | ||
| 91 | device: &'a mut dyn Device, | ||
| 92 | pkt: PacketBox, | ||
| 93 | } | ||
| 94 | |||
| 95 | impl<'a> smoltcp::phy::TxToken for TxToken<'a> { | ||
| 96 | fn consume<R, F>(self, _timestamp: SmolInstant, len: usize, f: F) -> Result<R> | ||
| 97 | where | ||
| 98 | F: FnOnce(&mut [u8]) -> Result<R>, | ||
| 99 | { | ||
| 100 | let mut buf = self.pkt.slice(0..len); | ||
| 101 | let r = f(&mut buf)?; | ||
| 102 | self.device.transmit(buf); | ||
| 103 | Ok(r) | ||
| 104 | } | ||
| 105 | } | ||
diff --git a/embassy-net/src/fmt.rs b/embassy-net/src/fmt.rs new file mode 100644 index 000000000..4da69766c --- /dev/null +++ b/embassy-net/src/fmt.rs | |||
| @@ -0,0 +1,118 @@ | |||
| 1 | #![macro_use] | ||
| 2 | |||
| 3 | #[cfg(all(feature = "defmt", feature = "log"))] | ||
| 4 | compile_error!("You may not enable both `defmt` and `log` features."); | ||
| 5 | |||
| 6 | pub use fmt::*; | ||
| 7 | |||
| 8 | #[cfg(feature = "defmt")] | ||
| 9 | mod fmt { | ||
| 10 | pub use defmt::{ | ||
| 11 | assert, assert_eq, assert_ne, debug, debug_assert, debug_assert_eq, debug_assert_ne, error, | ||
| 12 | info, panic, todo, trace, unreachable, unwrap, warn, | ||
| 13 | }; | ||
| 14 | } | ||
| 15 | |||
| 16 | #[cfg(feature = "log")] | ||
| 17 | mod fmt { | ||
| 18 | pub use core::{ | ||
| 19 | assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo, | ||
| 20 | unreachable, | ||
| 21 | }; | ||
| 22 | pub use log::{debug, error, info, trace, warn}; | ||
| 23 | } | ||
| 24 | |||
| 25 | #[cfg(not(any(feature = "defmt", feature = "log")))] | ||
| 26 | mod fmt { | ||
| 27 | #![macro_use] | ||
| 28 | |||
| 29 | pub use core::{ | ||
| 30 | assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo, | ||
| 31 | unreachable, | ||
| 32 | }; | ||
| 33 | |||
| 34 | #[macro_export] | ||
| 35 | macro_rules! trace { | ||
| 36 | ($($msg:expr),+ $(,)?) => { | ||
| 37 | () | ||
| 38 | }; | ||
| 39 | } | ||
| 40 | |||
| 41 | #[macro_export] | ||
| 42 | macro_rules! debug { | ||
| 43 | ($($msg:expr),+ $(,)?) => { | ||
| 44 | () | ||
| 45 | }; | ||
| 46 | } | ||
| 47 | |||
| 48 | #[macro_export] | ||
| 49 | macro_rules! info { | ||
| 50 | ($($msg:expr),+ $(,)?) => { | ||
| 51 | () | ||
| 52 | }; | ||
| 53 | } | ||
| 54 | |||
| 55 | #[macro_export] | ||
| 56 | macro_rules! warn { | ||
| 57 | ($($msg:expr),+ $(,)?) => { | ||
| 58 | () | ||
| 59 | }; | ||
| 60 | } | ||
| 61 | |||
| 62 | #[macro_export] | ||
| 63 | macro_rules! error { | ||
| 64 | ($($msg:expr),+ $(,)?) => { | ||
| 65 | () | ||
| 66 | }; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | #[cfg(not(feature = "defmt"))] | ||
| 71 | #[macro_export] | ||
| 72 | macro_rules! unwrap { | ||
| 73 | ($arg:expr) => { | ||
| 74 | match $crate::fmt::Try::into_result($arg) { | ||
| 75 | ::core::result::Result::Ok(t) => t, | ||
| 76 | ::core::result::Result::Err(e) => { | ||
| 77 | ::core::panic!("unwrap of `{}` failed: {:?}", ::core::stringify!($arg), e); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | }; | ||
| 81 | ($arg:expr, $($msg:expr),+ $(,)? ) => { | ||
| 82 | match $crate::fmt::Try::into_result($arg) { | ||
| 83 | ::core::result::Result::Ok(t) => t, | ||
| 84 | ::core::result::Result::Err(e) => { | ||
| 85 | ::core::panic!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] | ||
| 92 | pub struct NoneError; | ||
| 93 | |||
| 94 | pub trait Try { | ||
| 95 | type Ok; | ||
| 96 | type Error; | ||
| 97 | fn into_result(self) -> Result<Self::Ok, Self::Error>; | ||
| 98 | } | ||
| 99 | |||
| 100 | impl<T> Try for Option<T> { | ||
| 101 | type Ok = T; | ||
| 102 | type Error = NoneError; | ||
| 103 | |||
| 104 | #[inline] | ||
| 105 | fn into_result(self) -> Result<T, NoneError> { | ||
| 106 | self.ok_or(NoneError) | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | impl<T, E> Try for Result<T, E> { | ||
| 111 | type Ok = T; | ||
| 112 | type Error = E; | ||
| 113 | |||
| 114 | #[inline] | ||
| 115 | fn into_result(self) -> Self { | ||
| 116 | self | ||
| 117 | } | ||
| 118 | } | ||
diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs new file mode 100644 index 000000000..88dcf0aa5 --- /dev/null +++ b/embassy-net/src/lib.rs | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | #![cfg_attr(not(feature = "std"), no_std)] | ||
| 2 | |||
| 3 | // This mod MUST go first, so that the others see its macros. | ||
| 4 | pub(crate) mod fmt; | ||
| 5 | |||
| 6 | mod config; | ||
| 7 | mod device; | ||
| 8 | mod packet_pool; | ||
| 9 | mod stack; | ||
| 10 | |||
| 11 | #[cfg(feature = "dhcpv4")] | ||
| 12 | pub use config::DhcpConfigurator; | ||
| 13 | pub use config::{Config, Configurator, Event as ConfigEvent, StaticConfigurator}; | ||
| 14 | |||
| 15 | pub use device::{Device, LinkState}; | ||
| 16 | pub use packet_pool::{Packet, PacketBox, PacketBoxExt, PacketBuf}; | ||
| 17 | pub use stack::{init, is_config_up, is_init, is_link_up, run}; | ||
| 18 | |||
| 19 | #[cfg(feature = "tcp")] | ||
| 20 | mod tcp_socket; | ||
| 21 | #[cfg(feature = "tcp")] | ||
| 22 | pub use tcp_socket::TcpSocket; | ||
| 23 | |||
| 24 | // smoltcp reexports | ||
| 25 | pub use smoltcp::phy::{DeviceCapabilities, Medium}; | ||
| 26 | pub use smoltcp::time::Duration as SmolDuration; | ||
| 27 | pub use smoltcp::time::Instant as SmolInstant; | ||
| 28 | pub use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; | ||
| 29 | pub type Interface = smoltcp::iface::Interface<'static, device::DeviceAdapter>; | ||
| 30 | pub type SocketSet = smoltcp::socket::SocketSet<'static>; | ||
| 31 | pub use smoltcp::{Error, Result}; | ||
diff --git a/embassy-net/src/packet_pool.rs b/embassy-net/src/packet_pool.rs new file mode 100644 index 000000000..2c27d4013 --- /dev/null +++ b/embassy-net/src/packet_pool.rs | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | use as_slice::{AsMutSlice, AsSlice}; | ||
| 2 | use core::ops::{Deref, DerefMut, Range}; | ||
| 3 | |||
| 4 | use atomic_pool::{pool, Box}; | ||
| 5 | |||
| 6 | pub const MTU: usize = 1514; | ||
| 7 | pub const PACKET_POOL_SIZE: usize = 4; | ||
| 8 | |||
| 9 | pool!(pub PacketPool: [Packet; PACKET_POOL_SIZE]); | ||
| 10 | pub type PacketBox = Box<PacketPool>; | ||
| 11 | |||
| 12 | pub struct Packet(pub [u8; MTU]); | ||
| 13 | |||
| 14 | impl Packet { | ||
| 15 | pub const fn new() -> Self { | ||
| 16 | Self([0; MTU]) | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | pub trait PacketBoxExt { | ||
| 21 | fn slice(self, range: Range<usize>) -> PacketBuf; | ||
| 22 | } | ||
| 23 | |||
| 24 | impl PacketBoxExt for PacketBox { | ||
| 25 | fn slice(self, range: Range<usize>) -> PacketBuf { | ||
| 26 | PacketBuf { | ||
| 27 | packet: self, | ||
| 28 | range, | ||
| 29 | } | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | impl AsSlice for Packet { | ||
| 34 | type Element = u8; | ||
| 35 | |||
| 36 | fn as_slice(&self) -> &[Self::Element] { | ||
| 37 | &self.deref()[..] | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | impl AsMutSlice for Packet { | ||
| 42 | fn as_mut_slice(&mut self) -> &mut [Self::Element] { | ||
| 43 | &mut self.deref_mut()[..] | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | impl Deref for Packet { | ||
| 48 | type Target = [u8; MTU]; | ||
| 49 | |||
| 50 | fn deref(&self) -> &[u8; MTU] { | ||
| 51 | &self.0 | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | impl DerefMut for Packet { | ||
| 56 | fn deref_mut(&mut self) -> &mut [u8; MTU] { | ||
| 57 | &mut self.0 | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | pub struct PacketBuf { | ||
| 62 | packet: PacketBox, | ||
| 63 | range: Range<usize>, | ||
| 64 | } | ||
| 65 | |||
| 66 | impl AsSlice for PacketBuf { | ||
| 67 | type Element = u8; | ||
| 68 | |||
| 69 | fn as_slice(&self) -> &[Self::Element] { | ||
| 70 | &self.packet[self.range.clone()] | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | impl AsMutSlice for PacketBuf { | ||
| 75 | fn as_mut_slice(&mut self) -> &mut [Self::Element] { | ||
| 76 | &mut self.packet[self.range.clone()] | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | impl Deref for PacketBuf { | ||
| 81 | type Target = [u8]; | ||
| 82 | |||
| 83 | fn deref(&self) -> &[u8] { | ||
| 84 | &self.packet[self.range.clone()] | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | impl DerefMut for PacketBuf { | ||
| 89 | fn deref_mut(&mut self) -> &mut [u8] { | ||
| 90 | &mut self.packet[self.range.clone()] | ||
| 91 | } | ||
| 92 | } | ||
diff --git a/embassy-net/src/stack.rs b/embassy-net/src/stack.rs new file mode 100644 index 000000000..e436beb1e --- /dev/null +++ b/embassy-net/src/stack.rs | |||
| @@ -0,0 +1,259 @@ | |||
| 1 | use core::cell::RefCell; | ||
| 2 | use core::future::Future; | ||
| 3 | use core::task::Context; | ||
| 4 | use core::task::Poll; | ||
| 5 | use embassy::time::{Instant, Timer}; | ||
| 6 | use embassy::util::ThreadModeMutex; | ||
| 7 | use embassy::util::{Forever, WakerRegistration}; | ||
| 8 | use futures::pin_mut; | ||
| 9 | use smoltcp::iface::InterfaceBuilder; | ||
| 10 | #[cfg(feature = "medium-ethernet")] | ||
| 11 | use smoltcp::iface::{Neighbor, NeighborCache, Route, Routes}; | ||
| 12 | use smoltcp::phy::Device as _; | ||
| 13 | use smoltcp::phy::Medium; | ||
| 14 | use smoltcp::socket::SocketSetItem; | ||
| 15 | use smoltcp::time::Instant as SmolInstant; | ||
| 16 | #[cfg(feature = "medium-ethernet")] | ||
| 17 | use smoltcp::wire::EthernetAddress; | ||
| 18 | use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; | ||
| 19 | |||
| 20 | use crate::config::Configurator; | ||
| 21 | use crate::config::Event; | ||
| 22 | use crate::device::{Device, DeviceAdapter, LinkState}; | ||
| 23 | use crate::fmt::*; | ||
| 24 | use crate::{Interface, SocketSet}; | ||
| 25 | |||
| 26 | const ADDRESSES_LEN: usize = 1; | ||
| 27 | const NEIGHBOR_CACHE_LEN: usize = 8; | ||
| 28 | const SOCKETS_LEN: usize = 2; | ||
| 29 | const LOCAL_PORT_MIN: u16 = 1025; | ||
| 30 | const LOCAL_PORT_MAX: u16 = 65535; | ||
| 31 | |||
| 32 | struct StackResources { | ||
| 33 | addresses: [IpCidr; ADDRESSES_LEN], | ||
| 34 | sockets: [Option<SocketSetItem<'static>>; SOCKETS_LEN], | ||
| 35 | |||
| 36 | #[cfg(feature = "medium-ethernet")] | ||
| 37 | routes: [Option<(IpCidr, Route)>; 1], | ||
| 38 | #[cfg(feature = "medium-ethernet")] | ||
| 39 | neighbor_cache: [Option<(IpAddress, Neighbor)>; NEIGHBOR_CACHE_LEN], | ||
| 40 | } | ||
| 41 | |||
| 42 | static STACK_RESOURCES: Forever<StackResources> = Forever::new(); | ||
| 43 | static STACK: ThreadModeMutex<RefCell<Option<Stack>>> = ThreadModeMutex::new(RefCell::new(None)); | ||
| 44 | |||
| 45 | pub(crate) struct Stack { | ||
| 46 | iface: Interface, | ||
| 47 | pub sockets: SocketSet, | ||
| 48 | link_up: bool, | ||
| 49 | config_up: bool, | ||
| 50 | next_local_port: u16, | ||
| 51 | configurator: &'static mut dyn Configurator, | ||
| 52 | waker: WakerRegistration, | ||
| 53 | } | ||
| 54 | |||
| 55 | impl Stack { | ||
| 56 | pub(crate) fn with<R>(f: impl FnOnce(&mut Stack) -> R) -> R { | ||
| 57 | let mut stack = STACK.borrow().borrow_mut(); | ||
| 58 | let stack = stack.as_mut().unwrap(); | ||
| 59 | f(stack) | ||
| 60 | } | ||
| 61 | |||
| 62 | pub fn get_local_port(&mut self) -> u16 { | ||
| 63 | let res = self.next_local_port; | ||
| 64 | self.next_local_port = if res >= LOCAL_PORT_MAX { | ||
| 65 | LOCAL_PORT_MIN | ||
| 66 | } else { | ||
| 67 | res + 1 | ||
| 68 | }; | ||
| 69 | res | ||
| 70 | } | ||
| 71 | |||
| 72 | pub(crate) fn wake(&mut self) { | ||
| 73 | self.waker.wake() | ||
| 74 | } | ||
| 75 | |||
| 76 | fn poll_configurator(&mut self, timestamp: SmolInstant) { | ||
| 77 | let medium = self.iface.device().capabilities().medium; | ||
| 78 | |||
| 79 | match self | ||
| 80 | .configurator | ||
| 81 | .poll(&mut self.iface, &mut self.sockets, timestamp) | ||
| 82 | { | ||
| 83 | Event::NoChange => {} | ||
| 84 | Event::Configured(config) => { | ||
| 85 | debug!("Acquired IP configuration:"); | ||
| 86 | |||
| 87 | debug!(" IP address: {}", config.address); | ||
| 88 | set_ipv4_addr(&mut self.iface, config.address); | ||
| 89 | |||
| 90 | #[cfg(feature = "medium-ethernet")] | ||
| 91 | if medium == Medium::Ethernet { | ||
| 92 | if let Some(gateway) = config.gateway { | ||
| 93 | debug!(" Default gateway: {}", gateway); | ||
| 94 | self.iface | ||
| 95 | .routes_mut() | ||
| 96 | .add_default_ipv4_route(gateway) | ||
| 97 | .unwrap(); | ||
| 98 | } else { | ||
| 99 | debug!(" Default gateway: None"); | ||
| 100 | self.iface.routes_mut().remove_default_ipv4_route(); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | for (i, s) in config.dns_servers.iter().enumerate() { | ||
| 104 | debug!(" DNS server {}: {}", i, s); | ||
| 105 | } | ||
| 106 | |||
| 107 | self.config_up = true; | ||
| 108 | } | ||
| 109 | Event::Deconfigured => { | ||
| 110 | debug!("Lost IP configuration"); | ||
| 111 | set_ipv4_addr(&mut self.iface, Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0)); | ||
| 112 | #[cfg(feature = "medium-ethernet")] | ||
| 113 | if medium == Medium::Ethernet { | ||
| 114 | self.iface.routes_mut().remove_default_ipv4_route(); | ||
| 115 | } | ||
| 116 | self.config_up = false; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | fn poll(&mut self, cx: &mut Context<'_>) { | ||
| 122 | self.iface.device_mut().device.register_waker(cx.waker()); | ||
| 123 | self.waker.register(cx.waker()); | ||
| 124 | |||
| 125 | let timestamp = instant_to_smoltcp(Instant::now()); | ||
| 126 | if let Err(_) = self.iface.poll(&mut self.sockets, timestamp) { | ||
| 127 | // If poll() returns error, it may not be done yet, so poll again later. | ||
| 128 | cx.waker().wake_by_ref(); | ||
| 129 | return; | ||
| 130 | } | ||
| 131 | |||
| 132 | // Update link up | ||
| 133 | let old_link_up = self.link_up; | ||
| 134 | self.link_up = self.iface.device_mut().device.link_state() == LinkState::Up; | ||
| 135 | |||
| 136 | // Print when changed | ||
| 137 | if old_link_up != self.link_up { | ||
| 138 | if self.link_up { | ||
| 139 | info!("Link up!"); | ||
| 140 | } else { | ||
| 141 | info!("Link down!"); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | if old_link_up || self.link_up { | ||
| 146 | self.poll_configurator(timestamp) | ||
| 147 | } | ||
| 148 | |||
| 149 | if let Some(poll_at) = self.iface.poll_at(&mut self.sockets, timestamp) { | ||
| 150 | let t = Timer::at(instant_from_smoltcp(poll_at)); | ||
| 151 | pin_mut!(t); | ||
| 152 | if t.poll(cx).is_ready() { | ||
| 153 | cx.waker().wake_by_ref(); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | fn set_ipv4_addr(iface: &mut Interface, cidr: Ipv4Cidr) { | ||
| 160 | iface.update_ip_addrs(|addrs| { | ||
| 161 | let dest = addrs.iter_mut().next().unwrap(); | ||
| 162 | *dest = IpCidr::Ipv4(cidr); | ||
| 163 | }); | ||
| 164 | } | ||
| 165 | |||
| 166 | /// Initialize embassy_net. | ||
| 167 | /// This function must be called from thread mode. | ||
| 168 | pub fn init(device: &'static mut dyn Device, configurator: &'static mut dyn Configurator) { | ||
| 169 | const NONE_SOCKET: Option<SocketSetItem<'static>> = None; | ||
| 170 | let res = STACK_RESOURCES.put(StackResources { | ||
| 171 | addresses: [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 32)], | ||
| 172 | sockets: [NONE_SOCKET; SOCKETS_LEN], | ||
| 173 | |||
| 174 | #[cfg(feature = "medium-ethernet")] | ||
| 175 | routes: [None; 1], | ||
| 176 | #[cfg(feature = "medium-ethernet")] | ||
| 177 | neighbor_cache: [None; NEIGHBOR_CACHE_LEN], | ||
| 178 | }); | ||
| 179 | |||
| 180 | let medium = device.capabilities().medium; | ||
| 181 | |||
| 182 | #[cfg(feature = "medium-ethernet")] | ||
| 183 | let ethernet_addr = if medium == Medium::Ethernet { | ||
| 184 | device.ethernet_address() | ||
| 185 | } else { | ||
| 186 | [0, 0, 0, 0, 0, 0] | ||
| 187 | }; | ||
| 188 | |||
| 189 | let mut b = InterfaceBuilder::new(DeviceAdapter::new(device)); | ||
| 190 | b = b.ip_addrs(&mut res.addresses[..]); | ||
| 191 | |||
| 192 | #[cfg(feature = "medium-ethernet")] | ||
| 193 | if medium == Medium::Ethernet { | ||
| 194 | b = b.ethernet_addr(EthernetAddress(ethernet_addr)); | ||
| 195 | b = b.neighbor_cache(NeighborCache::new(&mut res.neighbor_cache[..])); | ||
| 196 | b = b.routes(Routes::new(&mut res.routes[..])); | ||
| 197 | } | ||
| 198 | |||
| 199 | let iface = b.finalize(); | ||
| 200 | |||
| 201 | let sockets = SocketSet::new(&mut res.sockets[..]); | ||
| 202 | |||
| 203 | let local_port = loop { | ||
| 204 | let mut res = [0u8; 2]; | ||
| 205 | rand(&mut res); | ||
| 206 | let port = u16::from_le_bytes(res); | ||
| 207 | if port >= LOCAL_PORT_MIN && port <= LOCAL_PORT_MAX { | ||
| 208 | break port; | ||
| 209 | } | ||
| 210 | }; | ||
| 211 | |||
| 212 | let stack = Stack { | ||
| 213 | iface, | ||
| 214 | sockets, | ||
| 215 | link_up: false, | ||
| 216 | config_up: false, | ||
| 217 | configurator, | ||
| 218 | next_local_port: local_port, | ||
| 219 | waker: WakerRegistration::new(), | ||
| 220 | }; | ||
| 221 | |||
| 222 | *STACK.borrow().borrow_mut() = Some(stack); | ||
| 223 | } | ||
| 224 | |||
| 225 | pub fn is_init() -> bool { | ||
| 226 | STACK.borrow().borrow().is_some() | ||
| 227 | } | ||
| 228 | |||
| 229 | pub fn is_link_up() -> bool { | ||
| 230 | STACK.borrow().borrow().as_ref().unwrap().link_up | ||
| 231 | } | ||
| 232 | |||
| 233 | pub fn is_config_up() -> bool { | ||
| 234 | STACK.borrow().borrow().as_ref().unwrap().config_up | ||
| 235 | } | ||
| 236 | |||
| 237 | pub async fn run() { | ||
| 238 | futures::future::poll_fn(|cx| { | ||
| 239 | Stack::with(|stack| stack.poll(cx)); | ||
| 240 | Poll::<()>::Pending | ||
| 241 | }) | ||
| 242 | .await | ||
| 243 | } | ||
| 244 | |||
| 245 | fn instant_to_smoltcp(instant: Instant) -> SmolInstant { | ||
| 246 | SmolInstant::from_millis(instant.as_millis() as i64) | ||
| 247 | } | ||
| 248 | |||
| 249 | fn instant_from_smoltcp(instant: SmolInstant) -> Instant { | ||
| 250 | Instant::from_millis(instant.total_millis() as u64) | ||
| 251 | } | ||
| 252 | |||
| 253 | extern "Rust" { | ||
| 254 | fn _embassy_rand(buf: &mut [u8]); | ||
| 255 | } | ||
| 256 | |||
| 257 | fn rand(buf: &mut [u8]) { | ||
| 258 | unsafe { _embassy_rand(buf) } | ||
| 259 | } | ||
diff --git a/embassy-net/src/tcp_socket.rs b/embassy-net/src/tcp_socket.rs new file mode 100644 index 000000000..4f43bc611 --- /dev/null +++ b/embassy-net/src/tcp_socket.rs | |||
| @@ -0,0 +1,177 @@ | |||
| 1 | use core::marker::PhantomData; | ||
| 2 | use core::mem; | ||
| 3 | use core::pin::Pin; | ||
| 4 | use core::task::{Context, Poll}; | ||
| 5 | use embassy::io; | ||
| 6 | use embassy::io::{AsyncBufRead, AsyncWrite}; | ||
| 7 | use smoltcp::socket::SocketHandle; | ||
| 8 | use smoltcp::socket::TcpSocket as SyncTcpSocket; | ||
| 9 | use smoltcp::socket::{TcpSocketBuffer, TcpState}; | ||
| 10 | use smoltcp::time::Duration; | ||
| 11 | use smoltcp::wire::IpEndpoint; | ||
| 12 | |||
| 13 | use super::stack::Stack; | ||
| 14 | use crate::fmt::*; | ||
| 15 | use crate::{Error, Result}; | ||
| 16 | |||
| 17 | pub struct TcpSocket<'a> { | ||
| 18 | handle: SocketHandle, | ||
| 19 | ghost: PhantomData<&'a mut [u8]>, | ||
| 20 | } | ||
| 21 | |||
| 22 | impl<'a> Unpin for TcpSocket<'a> {} | ||
| 23 | |||
| 24 | impl<'a> TcpSocket<'a> { | ||
| 25 | pub fn new(rx_buffer: &'a mut [u8], tx_buffer: &'a mut [u8]) -> Self { | ||
| 26 | let handle = Stack::with(|stack| { | ||
| 27 | let rx_buffer: &'static mut [u8] = unsafe { mem::transmute(rx_buffer) }; | ||
| 28 | let tx_buffer: &'static mut [u8] = unsafe { mem::transmute(tx_buffer) }; | ||
| 29 | stack.sockets.add(SyncTcpSocket::new( | ||
| 30 | TcpSocketBuffer::new(rx_buffer), | ||
| 31 | TcpSocketBuffer::new(tx_buffer), | ||
| 32 | )) | ||
| 33 | }); | ||
| 34 | |||
| 35 | Self { | ||
| 36 | handle, | ||
| 37 | ghost: PhantomData, | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | pub async fn connect<T>(&mut self, remote_endpoint: T) -> Result<()> | ||
| 42 | where | ||
| 43 | T: Into<IpEndpoint>, | ||
| 44 | { | ||
| 45 | let local_port = Stack::with(|stack| stack.get_local_port()); | ||
| 46 | self.with(|s| s.connect(remote_endpoint, local_port))?; | ||
| 47 | |||
| 48 | futures::future::poll_fn(|cx| { | ||
| 49 | self.with(|s| match s.state() { | ||
| 50 | TcpState::Closed | TcpState::TimeWait => Poll::Ready(Err(Error::Unaddressable)), | ||
| 51 | TcpState::Listen => Poll::Ready(Err(Error::Illegal)), | ||
| 52 | TcpState::SynSent | TcpState::SynReceived => { | ||
| 53 | s.register_send_waker(cx.waker()); | ||
| 54 | Poll::Pending | ||
| 55 | } | ||
| 56 | _ => Poll::Ready(Ok(())), | ||
| 57 | }) | ||
| 58 | }) | ||
| 59 | .await | ||
| 60 | } | ||
| 61 | |||
| 62 | pub fn set_timeout(&mut self, duration: Option<Duration>) { | ||
| 63 | self.with(|s| s.set_timeout(duration)) | ||
| 64 | } | ||
| 65 | |||
| 66 | pub fn set_keep_alive(&mut self, interval: Option<Duration>) { | ||
| 67 | self.with(|s| s.set_keep_alive(interval)) | ||
| 68 | } | ||
| 69 | |||
| 70 | pub fn set_hop_limit(&mut self, hop_limit: Option<u8>) { | ||
| 71 | self.with(|s| s.set_hop_limit(hop_limit)) | ||
| 72 | } | ||
| 73 | |||
| 74 | pub fn local_endpoint(&self) -> IpEndpoint { | ||
| 75 | self.with(|s| s.local_endpoint()) | ||
| 76 | } | ||
| 77 | |||
| 78 | pub fn remote_endpoint(&self) -> IpEndpoint { | ||
| 79 | self.with(|s| s.remote_endpoint()) | ||
| 80 | } | ||
| 81 | |||
| 82 | pub fn state(&self) -> TcpState { | ||
| 83 | self.with(|s| s.state()) | ||
| 84 | } | ||
| 85 | |||
| 86 | pub fn close(&mut self) { | ||
| 87 | self.with(|s| s.close()) | ||
| 88 | } | ||
| 89 | |||
| 90 | pub fn abort(&mut self) { | ||
| 91 | self.with(|s| s.abort()) | ||
| 92 | } | ||
| 93 | |||
| 94 | pub fn may_send(&self) -> bool { | ||
| 95 | self.with(|s| s.may_send()) | ||
| 96 | } | ||
| 97 | |||
| 98 | pub fn may_recv(&self) -> bool { | ||
| 99 | self.with(|s| s.may_recv()) | ||
| 100 | } | ||
| 101 | |||
| 102 | fn with<R>(&self, f: impl FnOnce(&mut SyncTcpSocket) -> R) -> R { | ||
| 103 | Stack::with(|stack| { | ||
| 104 | let res = { | ||
| 105 | let mut s = stack.sockets.get::<SyncTcpSocket>(self.handle); | ||
| 106 | f(&mut *s) | ||
| 107 | }; | ||
| 108 | stack.wake(); | ||
| 109 | res | ||
| 110 | }) | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | fn to_ioerr(_err: Error) -> io::Error { | ||
| 115 | // todo | ||
| 116 | io::Error::Other | ||
| 117 | } | ||
| 118 | |||
| 119 | impl<'a> Drop for TcpSocket<'a> { | ||
| 120 | fn drop(&mut self) { | ||
| 121 | Stack::with(|stack| { | ||
| 122 | stack.sockets.remove(self.handle); | ||
| 123 | }) | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | impl<'a> AsyncBufRead for TcpSocket<'a> { | ||
| 128 | fn poll_fill_buf<'z>( | ||
| 129 | self: Pin<&'z mut Self>, | ||
| 130 | cx: &mut Context<'_>, | ||
| 131 | ) -> Poll<io::Result<&'z [u8]>> { | ||
| 132 | self.with(|socket| match socket.peek(1 << 30) { | ||
| 133 | // No data ready | ||
| 134 | Ok(buf) if buf.len() == 0 => { | ||
| 135 | socket.register_recv_waker(cx.waker()); | ||
| 136 | Poll::Pending | ||
| 137 | } | ||
| 138 | // Data ready! | ||
| 139 | Ok(buf) => { | ||
| 140 | // Safety: | ||
| 141 | // - User can't touch the inner TcpSocket directly at all. | ||
| 142 | // - The socket itself won't touch these bytes until consume() is called, which | ||
| 143 | // requires the user to release this borrow. | ||
| 144 | let buf: &'z [u8] = unsafe { core::mem::transmute(&*buf) }; | ||
| 145 | Poll::Ready(Ok(buf)) | ||
| 146 | } | ||
| 147 | // EOF | ||
| 148 | Err(Error::Finished) => Poll::Ready(Ok(&[][..])), | ||
| 149 | // Error | ||
| 150 | Err(e) => Poll::Ready(Err(to_ioerr(e))), | ||
| 151 | }) | ||
| 152 | } | ||
| 153 | |||
| 154 | fn consume(self: Pin<&mut Self>, amt: usize) { | ||
| 155 | self.with(|s| s.recv(|_| (amt, ()))).unwrap() | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | impl<'a> AsyncWrite for TcpSocket<'a> { | ||
| 160 | fn poll_write( | ||
| 161 | self: Pin<&mut Self>, | ||
| 162 | cx: &mut Context<'_>, | ||
| 163 | buf: &[u8], | ||
| 164 | ) -> Poll<io::Result<usize>> { | ||
| 165 | self.with(|s| match s.send_slice(buf) { | ||
| 166 | // Not ready to send (no space in the tx buffer) | ||
| 167 | Ok(0) => { | ||
| 168 | s.register_send_waker(cx.waker()); | ||
| 169 | Poll::Pending | ||
| 170 | } | ||
| 171 | // Some data sent | ||
| 172 | Ok(n) => Poll::Ready(Ok(n)), | ||
| 173 | // Error | ||
| 174 | Err(e) => Poll::Ready(Err(to_ioerr(e))), | ||
| 175 | }) | ||
| 176 | } | ||
| 177 | } | ||
