diff options
| -rw-r--r-- | embassy-net/Cargo.toml | 1 | ||||
| -rw-r--r-- | embassy-net/src/lib.rs | 5 | ||||
| -rw-r--r-- | embassy-net/src/udp.rs | 157 | ||||
| -rw-r--r-- | examples/std/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/std/src/bin/net_udp.rs | 109 |
5 files changed, 273 insertions, 1 deletions
diff --git a/embassy-net/Cargo.toml b/embassy-net/Cargo.toml index fface207b..e4d8c2c27 100644 --- a/embassy-net/Cargo.toml +++ b/embassy-net/Cargo.toml | |||
| @@ -18,6 +18,7 @@ std = [] | |||
| 18 | 18 | ||
| 19 | defmt = ["dep:defmt", "smoltcp/defmt"] | 19 | defmt = ["dep:defmt", "smoltcp/defmt"] |
| 20 | 20 | ||
| 21 | udp = ["smoltcp/socket-udp"] | ||
| 21 | tcp = ["smoltcp/socket-tcp"] | 22 | tcp = ["smoltcp/socket-tcp"] |
| 22 | dns = ["smoltcp/socket-dns"] | 23 | dns = ["smoltcp/socket-dns"] |
| 23 | dhcpv4 = ["medium-ethernet", "smoltcp/socket-dhcpv4"] | 24 | dhcpv4 = ["medium-ethernet", "smoltcp/socket-dhcpv4"] |
diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs index 1c5ba103a..83d364715 100644 --- a/embassy-net/src/lib.rs +++ b/embassy-net/src/lib.rs | |||
| @@ -16,6 +16,9 @@ pub use stack::{Config, ConfigStrategy, Stack, StackResources}; | |||
| 16 | #[cfg(feature = "tcp")] | 16 | #[cfg(feature = "tcp")] |
| 17 | pub mod tcp; | 17 | pub mod tcp; |
| 18 | 18 | ||
| 19 | #[cfg(feature = "udp")] | ||
| 20 | pub mod udp; | ||
| 21 | |||
| 19 | // smoltcp reexports | 22 | // smoltcp reexports |
| 20 | pub use smoltcp::phy::{DeviceCapabilities, Medium}; | 23 | pub use smoltcp::phy::{DeviceCapabilities, Medium}; |
| 21 | pub use smoltcp::time::{Duration as SmolDuration, Instant as SmolInstant}; | 24 | pub use smoltcp::time::{Duration as SmolDuration, Instant as SmolInstant}; |
| @@ -24,3 +27,5 @@ pub use smoltcp::wire::{EthernetAddress, HardwareAddress}; | |||
| 24 | pub use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; | 27 | pub use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; |
| 25 | #[cfg(feature = "proto-ipv6")] | 28 | #[cfg(feature = "proto-ipv6")] |
| 26 | pub use smoltcp::wire::{Ipv6Address, Ipv6Cidr}; | 29 | pub use smoltcp::wire::{Ipv6Address, Ipv6Cidr}; |
| 30 | #[cfg(feature = "udp")] | ||
| 31 | pub use smoltcp::{socket::udp::PacketMetadata, wire::IpListenEndpoint}; | ||
diff --git a/embassy-net/src/udp.rs b/embassy-net/src/udp.rs new file mode 100644 index 000000000..78b09a492 --- /dev/null +++ b/embassy-net/src/udp.rs | |||
| @@ -0,0 +1,157 @@ | |||
| 1 | use core::cell::UnsafeCell; | ||
| 2 | use core::mem; | ||
| 3 | use core::task::Poll; | ||
| 4 | |||
| 5 | use futures::future::poll_fn; | ||
| 6 | use smoltcp::iface::{Interface, SocketHandle}; | ||
| 7 | use smoltcp::socket::udp::{self, PacketMetadata}; | ||
| 8 | use smoltcp::wire::{IpEndpoint, IpListenEndpoint}; | ||
| 9 | |||
| 10 | use super::stack::SocketStack; | ||
| 11 | use crate::{Device, Stack}; | ||
| 12 | |||
| 13 | #[derive(PartialEq, Eq, Clone, Copy, Debug)] | ||
| 14 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 15 | pub enum BindError { | ||
| 16 | /// The socket was already open. | ||
| 17 | InvalidState, | ||
| 18 | /// No route to host. | ||
| 19 | NoRoute, | ||
| 20 | } | ||
| 21 | |||
| 22 | #[derive(PartialEq, Eq, Clone, Copy, Debug)] | ||
| 23 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 24 | pub enum Error { | ||
| 25 | /// No route to host. | ||
| 26 | NoRoute, | ||
| 27 | } | ||
| 28 | |||
| 29 | pub struct UdpSocket<'a> { | ||
| 30 | stack: &'a UnsafeCell<SocketStack>, | ||
| 31 | handle: SocketHandle, | ||
| 32 | } | ||
| 33 | |||
| 34 | impl<'a> UdpSocket<'a> { | ||
| 35 | pub fn new<D: Device>( | ||
| 36 | stack: &'a Stack<D>, | ||
| 37 | rx_meta: &'a mut [PacketMetadata], | ||
| 38 | rx_buffer: &'a mut [u8], | ||
| 39 | tx_meta: &'a mut [PacketMetadata], | ||
| 40 | tx_buffer: &'a mut [u8], | ||
| 41 | ) -> Self { | ||
| 42 | // safety: not accessed reentrantly. | ||
| 43 | let s = unsafe { &mut *stack.socket.get() }; | ||
| 44 | |||
| 45 | let rx_meta: &'static mut [PacketMetadata] = unsafe { mem::transmute(rx_meta) }; | ||
| 46 | let rx_buffer: &'static mut [u8] = unsafe { mem::transmute(rx_buffer) }; | ||
| 47 | let tx_meta: &'static mut [PacketMetadata] = unsafe { mem::transmute(tx_meta) }; | ||
| 48 | let tx_buffer: &'static mut [u8] = unsafe { mem::transmute(tx_buffer) }; | ||
| 49 | let handle = s.sockets.add(udp::Socket::new( | ||
| 50 | udp::PacketBuffer::new(rx_meta, rx_buffer), | ||
| 51 | udp::PacketBuffer::new(tx_meta, tx_buffer), | ||
| 52 | )); | ||
| 53 | |||
| 54 | Self { | ||
| 55 | stack: &stack.socket, | ||
| 56 | handle, | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | pub fn bind<T>(&mut self, endpoint: T) -> Result<(), BindError> | ||
| 61 | where | ||
| 62 | T: Into<IpListenEndpoint>, | ||
| 63 | { | ||
| 64 | let mut endpoint = endpoint.into(); | ||
| 65 | |||
| 66 | // safety: not accessed reentrantly. | ||
| 67 | if endpoint.port == 0 { | ||
| 68 | // If user didn't specify port allocate a dynamic port. | ||
| 69 | endpoint.port = unsafe { &mut *self.stack.get() }.get_local_port(); | ||
| 70 | } | ||
| 71 | |||
| 72 | // safety: not accessed reentrantly. | ||
| 73 | match unsafe { self.with_mut(|s, _| s.bind(endpoint)) } { | ||
| 74 | Ok(()) => Ok(()), | ||
| 75 | Err(udp::BindError::InvalidState) => Err(BindError::InvalidState), | ||
| 76 | Err(udp::BindError::Unaddressable) => Err(BindError::NoRoute), | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | /// SAFETY: must not call reentrantly. | ||
| 81 | unsafe fn with<R>(&self, f: impl FnOnce(&udp::Socket, &Interface) -> R) -> R { | ||
| 82 | let s = &*self.stack.get(); | ||
| 83 | let socket = s.sockets.get::<udp::Socket>(self.handle); | ||
| 84 | f(socket, &s.iface) | ||
| 85 | } | ||
| 86 | |||
| 87 | /// SAFETY: must not call reentrantly. | ||
| 88 | unsafe fn with_mut<R>(&self, f: impl FnOnce(&mut udp::Socket, &mut Interface) -> R) -> R { | ||
| 89 | let s = &mut *self.stack.get(); | ||
| 90 | let socket = s.sockets.get_mut::<udp::Socket>(self.handle); | ||
| 91 | let res = f(socket, &mut s.iface); | ||
| 92 | s.waker.wake(); | ||
| 93 | res | ||
| 94 | } | ||
| 95 | |||
| 96 | pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> { | ||
| 97 | poll_fn(move |cx| unsafe { | ||
| 98 | self.with_mut(|s, _| match s.recv_slice(buf) { | ||
| 99 | Ok(x) => Poll::Ready(Ok(x)), | ||
| 100 | // No data ready | ||
| 101 | Err(udp::RecvError::Exhausted) => { | ||
| 102 | //s.register_recv_waker(cx.waker()); | ||
| 103 | cx.waker().wake_by_ref(); | ||
| 104 | Poll::Pending | ||
| 105 | } | ||
| 106 | }) | ||
| 107 | }) | ||
| 108 | .await | ||
| 109 | } | ||
| 110 | |||
| 111 | pub async fn send_to<T>(&self, buf: &[u8], remote_endpoint: T) -> Result<(), Error> | ||
| 112 | where | ||
| 113 | T: Into<IpEndpoint>, | ||
| 114 | { | ||
| 115 | let remote_endpoint = remote_endpoint.into(); | ||
| 116 | poll_fn(move |cx| unsafe { | ||
| 117 | self.with_mut(|s, _| match s.send_slice(buf, remote_endpoint) { | ||
| 118 | // Entire datagram has been sent | ||
| 119 | Ok(()) => Poll::Ready(Ok(())), | ||
| 120 | Err(udp::SendError::BufferFull) => { | ||
| 121 | s.register_send_waker(cx.waker()); | ||
| 122 | Poll::Pending | ||
| 123 | } | ||
| 124 | Err(udp::SendError::Unaddressable) => Poll::Ready(Err(Error::NoRoute)), | ||
| 125 | }) | ||
| 126 | }) | ||
| 127 | .await | ||
| 128 | } | ||
| 129 | |||
| 130 | pub fn endpoint(&self) -> IpListenEndpoint { | ||
| 131 | unsafe { self.with(|s, _| s.endpoint()) } | ||
| 132 | } | ||
| 133 | |||
| 134 | pub fn is_open(&self) -> bool { | ||
| 135 | unsafe { self.with(|s, _| s.is_open()) } | ||
| 136 | } | ||
| 137 | |||
| 138 | pub fn close(&mut self) { | ||
| 139 | unsafe { self.with_mut(|s, _| s.close()) } | ||
| 140 | } | ||
| 141 | |||
| 142 | pub fn may_send(&self) -> bool { | ||
| 143 | unsafe { self.with(|s, _| s.can_send()) } | ||
| 144 | } | ||
| 145 | |||
| 146 | pub fn may_recv(&self) -> bool { | ||
| 147 | unsafe { self.with(|s, _| s.can_recv()) } | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | impl Drop for UdpSocket<'_> { | ||
| 152 | fn drop(&mut self) { | ||
| 153 | // safety: not accessed reentrantly. | ||
| 154 | let s = unsafe { &mut *self.stack.get() }; | ||
| 155 | s.sockets.remove(self.handle); | ||
| 156 | } | ||
| 157 | } | ||
diff --git a/examples/std/Cargo.toml b/examples/std/Cargo.toml index 54499796b..427b93438 100644 --- a/examples/std/Cargo.toml +++ b/examples/std/Cargo.toml | |||
| @@ -6,7 +6,7 @@ version = "0.1.0" | |||
| 6 | [dependencies] | 6 | [dependencies] |
| 7 | embassy-util = { version = "0.1.0", path = "../../embassy-util", features = ["log"] } | 7 | embassy-util = { version = "0.1.0", path = "../../embassy-util", features = ["log"] } |
| 8 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["log", "std", "time", "nightly"] } | 8 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["log", "std", "time", "nightly"] } |
| 9 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features=[ "std", "log", "medium-ethernet", "tcp", "dhcpv4", "pool-16"] } | 9 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features=[ "std", "log", "medium-ethernet", "tcp", "udp", "dhcpv4", "pool-16"] } |
| 10 | embedded-io = { version = "0.3.0", features = ["async", "std", "futures"] } | 10 | embedded-io = { version = "0.3.0", features = ["async", "std", "futures"] } |
| 11 | 11 | ||
| 12 | async-io = "1.6.0" | 12 | async-io = "1.6.0" |
diff --git a/examples/std/src/bin/net_udp.rs b/examples/std/src/bin/net_udp.rs new file mode 100644 index 000000000..7fe36e233 --- /dev/null +++ b/examples/std/src/bin/net_udp.rs | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | #![feature(type_alias_impl_trait)] | ||
| 2 | |||
| 3 | use clap::Parser; | ||
| 4 | use embassy_executor::executor::{Executor, Spawner}; | ||
| 5 | use embassy_net::udp::UdpSocket; | ||
| 6 | use embassy_net::{ConfigStrategy, Ipv4Address, Ipv4Cidr, PacketMetadata, Stack, StackResources}; | ||
| 7 | use embassy_util::Forever; | ||
| 8 | use heapless::Vec; | ||
| 9 | use log::*; | ||
| 10 | use rand_core::{OsRng, RngCore}; | ||
| 11 | |||
| 12 | #[path = "../tuntap.rs"] | ||
| 13 | mod tuntap; | ||
| 14 | |||
| 15 | use crate::tuntap::TunTapDevice; | ||
| 16 | |||
| 17 | macro_rules! forever { | ||
| 18 | ($val:expr) => {{ | ||
| 19 | type T = impl Sized; | ||
| 20 | static FOREVER: Forever<T> = Forever::new(); | ||
| 21 | FOREVER.put_with(move || $val) | ||
| 22 | }}; | ||
| 23 | } | ||
| 24 | |||
| 25 | #[derive(Parser)] | ||
| 26 | #[clap(version = "1.0")] | ||
| 27 | struct Opts { | ||
| 28 | /// TAP device name | ||
| 29 | #[clap(long, default_value = "tap0")] | ||
| 30 | tap: String, | ||
| 31 | /// use a static IP instead of DHCP | ||
| 32 | #[clap(long)] | ||
| 33 | static_ip: bool, | ||
| 34 | } | ||
| 35 | |||
| 36 | #[embassy_executor::task] | ||
| 37 | async fn net_task(stack: &'static Stack<TunTapDevice>) -> ! { | ||
| 38 | stack.run().await | ||
| 39 | } | ||
| 40 | |||
| 41 | #[embassy_executor::task] | ||
| 42 | async fn main_task(spawner: Spawner) { | ||
| 43 | let opts: Opts = Opts::parse(); | ||
| 44 | |||
| 45 | // Init network device | ||
| 46 | let device = TunTapDevice::new(&opts.tap).unwrap(); | ||
| 47 | |||
| 48 | // Choose between dhcp or static ip | ||
| 49 | let config = if opts.static_ip { | ||
| 50 | ConfigStrategy::Static(embassy_net::Config { | ||
| 51 | address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24), | ||
| 52 | dns_servers: Vec::new(), | ||
| 53 | gateway: Some(Ipv4Address::new(192, 168, 69, 1)), | ||
| 54 | }) | ||
| 55 | } else { | ||
| 56 | ConfigStrategy::Dhcp | ||
| 57 | }; | ||
| 58 | |||
| 59 | // Generate random seed | ||
| 60 | let mut seed = [0; 8]; | ||
| 61 | OsRng.fill_bytes(&mut seed); | ||
| 62 | let seed = u64::from_le_bytes(seed); | ||
| 63 | |||
| 64 | // Init network stack | ||
| 65 | let stack = &*forever!(Stack::new( | ||
| 66 | device, | ||
| 67 | config, | ||
| 68 | forever!(StackResources::<1, 2, 8>::new()), | ||
| 69 | seed | ||
| 70 | )); | ||
| 71 | |||
| 72 | // Launch network task | ||
| 73 | spawner.spawn(net_task(stack)).unwrap(); | ||
| 74 | |||
| 75 | // Then we can use it! | ||
| 76 | let mut rx_meta = [PacketMetadata::EMPTY; 16]; | ||
| 77 | let mut rx_buffer = [0; 4096]; | ||
| 78 | let mut tx_meta = [PacketMetadata::EMPTY; 16]; | ||
| 79 | let mut tx_buffer = [0; 4096]; | ||
| 80 | let mut buf = [0; 4096]; | ||
| 81 | |||
| 82 | let mut socket = UdpSocket::new(stack, &mut rx_meta, &mut rx_buffer, &mut tx_meta, &mut tx_buffer); | ||
| 83 | socket.bind(9400).unwrap(); | ||
| 84 | |||
| 85 | loop { | ||
| 86 | let (n, ep) = socket.recv_from(&mut buf).await.unwrap(); | ||
| 87 | if let Ok(s) = core::str::from_utf8(&buf[..n]) { | ||
| 88 | info!("ECHO (to {}): {}", ep, s); | ||
| 89 | } else { | ||
| 90 | info!("ECHO (to {}): bytearray len {}", ep, n); | ||
| 91 | } | ||
| 92 | socket.send_to(&buf[..n], ep).await.unwrap(); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 97 | |||
| 98 | fn main() { | ||
| 99 | env_logger::builder() | ||
| 100 | .filter_level(log::LevelFilter::Debug) | ||
| 101 | .filter_module("async_io", log::LevelFilter::Info) | ||
| 102 | .format_timestamp_nanos() | ||
| 103 | .init(); | ||
| 104 | |||
| 105 | let executor = EXECUTOR.put(Executor::new()); | ||
| 106 | executor.run(|spawner| { | ||
| 107 | spawner.spawn(main_task(spawner)).unwrap(); | ||
| 108 | }); | ||
| 109 | } | ||
