diff options
| -rw-r--r-- | embassy-net-examples/Cargo.toml | 19 | ||||
| -rw-r--r-- | embassy-net-examples/src/main.rs | 102 | ||||
| -rw-r--r-- | embassy-net-examples/src/tuntap.rs | 225 | ||||
| -rw-r--r-- | embassy-net/Cargo.toml | 2 | ||||
| -rw-r--r-- | embassy-net/src/config/mod.rs | 3 | ||||
| -rw-r--r-- | examples/std/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/stm32f4/Cargo.toml | 2 |
7 files changed, 4 insertions, 351 deletions
diff --git a/embassy-net-examples/Cargo.toml b/embassy-net-examples/Cargo.toml deleted file mode 100644 index 413428bf6..000000000 --- a/embassy-net-examples/Cargo.toml +++ /dev/null | |||
| @@ -1,19 +0,0 @@ | |||
| 1 | [package] | ||
| 2 | name = "embassy-net-examples" | ||
| 3 | version = "0.1.0" | ||
| 4 | authors = ["Dario Nieuwenhuis <[email protected]>"] | ||
| 5 | edition = "2018" | ||
| 6 | |||
| 7 | [dependencies] | ||
| 8 | heapless = { version = "0.5.6", default-features = false } | ||
| 9 | embassy = { version = "0.1.0", path = "../embassy", features=["std", "log"] } | ||
| 10 | embassy-std = { version = "0.1.0", path = "../embassy-std" } | ||
| 11 | embassy-net = { version = "0.1.0", path = "../embassy-net", features=["std", "log", "medium-ethernet", "tcp", "dhcpv4"] } | ||
| 12 | env_logger = "0.8.2" | ||
| 13 | log = "0.4.11" | ||
| 14 | futures = "0.3.8" | ||
| 15 | libc = "0.2.81" | ||
| 16 | async-io = "1.3.1" | ||
| 17 | smoltcp = { git = "https://github.com/smoltcp-rs/smoltcp", rev="ec59aba5e10cf91df0c9253d9c2aca4dd143d2ff", default-features = false } | ||
| 18 | clap = { version = "3.0.0-beta.2", features = ["derive"] } | ||
| 19 | rand_core = { version = "0.6.0", features = ["std"] } | ||
diff --git a/embassy-net-examples/src/main.rs b/embassy-net-examples/src/main.rs deleted file mode 100644 index d1c2658dd..000000000 --- a/embassy-net-examples/src/main.rs +++ /dev/null | |||
| @@ -1,102 +0,0 @@ | |||
| 1 | #![feature(type_alias_impl_trait)] | ||
| 2 | #![feature(min_type_alias_impl_trait)] | ||
| 3 | #![feature(impl_trait_in_bindings)] | ||
| 4 | #![allow(incomplete_features)] | ||
| 5 | |||
| 6 | use clap::{AppSettings, Clap}; | ||
| 7 | use embassy::executor::Spawner; | ||
| 8 | use embassy::io::AsyncWriteExt; | ||
| 9 | use embassy::util::Forever; | ||
| 10 | use embassy_net::*; | ||
| 11 | use embassy_std::Executor; | ||
| 12 | use heapless::Vec; | ||
| 13 | use log::*; | ||
| 14 | |||
| 15 | mod tuntap; | ||
| 16 | |||
| 17 | use crate::tuntap::TunTapDevice; | ||
| 18 | |||
| 19 | static DEVICE: Forever<TunTapDevice> = Forever::new(); | ||
| 20 | static CONFIG: Forever<DhcpConfigurator> = Forever::new(); | ||
| 21 | |||
| 22 | #[derive(Clap)] | ||
| 23 | #[clap(version = "1.0")] | ||
| 24 | #[clap(setting = AppSettings::ColoredHelp)] | ||
| 25 | struct Opts { | ||
| 26 | /// TAP device name | ||
| 27 | #[clap(long, default_value = "tap0")] | ||
| 28 | tap: String, | ||
| 29 | } | ||
| 30 | |||
| 31 | #[embassy::task] | ||
| 32 | async fn net_task() { | ||
| 33 | embassy_net::run().await | ||
| 34 | } | ||
| 35 | |||
| 36 | #[embassy::task] | ||
| 37 | async fn main_task(spawner: Spawner) { | ||
| 38 | let opts: Opts = Opts::parse(); | ||
| 39 | |||
| 40 | // Init network device | ||
| 41 | let device = TunTapDevice::new(&opts.tap).unwrap(); | ||
| 42 | |||
| 43 | // Static IP configuration | ||
| 44 | let config = StaticConfigurator::new(Config { | ||
| 45 | address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24), | ||
| 46 | dns_servers: Vec::new(), | ||
| 47 | gateway: Some(Ipv4Address::new(192, 168, 69, 1)), | ||
| 48 | }); | ||
| 49 | |||
| 50 | // DHCP configruation | ||
| 51 | let config = DhcpConfigurator::new(); | ||
| 52 | |||
| 53 | // Init network stack | ||
| 54 | embassy_net::init(DEVICE.put(device), CONFIG.put(config)); | ||
| 55 | |||
| 56 | // Launch network task | ||
| 57 | spawner.spawn(net_task()).unwrap(); | ||
| 58 | |||
| 59 | // Then we can use it! | ||
| 60 | let mut rx_buffer = [0; 4096]; | ||
| 61 | let mut tx_buffer = [0; 4096]; | ||
| 62 | let mut socket = TcpSocket::new(&mut rx_buffer, &mut tx_buffer); | ||
| 63 | |||
| 64 | socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); | ||
| 65 | |||
| 66 | let remote_endpoint = (Ipv4Address::new(192, 168, 69, 74), 8000); | ||
| 67 | info!("connecting to {:?}...", remote_endpoint); | ||
| 68 | let r = socket.connect(remote_endpoint).await; | ||
| 69 | if let Err(e) = r { | ||
| 70 | warn!("connect error: {:?}", e); | ||
| 71 | return; | ||
| 72 | } | ||
| 73 | info!("connected!"); | ||
| 74 | loop { | ||
| 75 | let r = socket.write_all(b"Hello!\n").await; | ||
| 76 | if let Err(e) = r { | ||
| 77 | warn!("write error: {:?}", e); | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | #[no_mangle] | ||
| 84 | fn _embassy_rand(buf: &mut [u8]) { | ||
| 85 | use rand_core::{OsRng, RngCore}; | ||
| 86 | OsRng.fill_bytes(buf); | ||
| 87 | } | ||
| 88 | |||
| 89 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 90 | |||
| 91 | fn main() { | ||
| 92 | env_logger::builder() | ||
| 93 | .filter_level(log::LevelFilter::Debug) | ||
| 94 | .filter_module("async_io", log::LevelFilter::Info) | ||
| 95 | .format_timestamp_nanos() | ||
| 96 | .init(); | ||
| 97 | |||
| 98 | let executor = EXECUTOR.put(Executor::new()); | ||
| 99 | executor.run(|spawner| { | ||
| 100 | spawner.spawn(main_task(spawner)).unwrap(); | ||
| 101 | }); | ||
| 102 | } | ||
diff --git a/embassy-net-examples/src/tuntap.rs b/embassy-net-examples/src/tuntap.rs deleted file mode 100644 index dd453deb3..000000000 --- a/embassy-net-examples/src/tuntap.rs +++ /dev/null | |||
| @@ -1,225 +0,0 @@ | |||
| 1 | use async_io::Async; | ||
| 2 | use libc; | ||
| 3 | use log::*; | ||
| 4 | use smoltcp::wire::EthernetFrame; | ||
| 5 | use std::io; | ||
| 6 | use std::io::{Read, Write}; | ||
| 7 | use std::os::unix::io::{AsRawFd, RawFd}; | ||
| 8 | |||
| 9 | pub const SIOCGIFMTU: libc::c_ulong = 0x8921; | ||
| 10 | pub const SIOCGIFINDEX: libc::c_ulong = 0x8933; | ||
| 11 | pub const ETH_P_ALL: libc::c_short = 0x0003; | ||
| 12 | pub const TUNSETIFF: libc::c_ulong = 0x400454CA; | ||
| 13 | pub const IFF_TUN: libc::c_int = 0x0001; | ||
| 14 | pub const IFF_TAP: libc::c_int = 0x0002; | ||
| 15 | pub const IFF_NO_PI: libc::c_int = 0x1000; | ||
| 16 | |||
| 17 | #[repr(C)] | ||
| 18 | #[derive(Debug)] | ||
| 19 | struct ifreq { | ||
| 20 | ifr_name: [libc::c_char; libc::IF_NAMESIZE], | ||
| 21 | ifr_data: libc::c_int, /* ifr_ifindex or ifr_mtu */ | ||
| 22 | } | ||
| 23 | |||
| 24 | fn ifreq_for(name: &str) -> ifreq { | ||
| 25 | let mut ifreq = ifreq { | ||
| 26 | ifr_name: [0; libc::IF_NAMESIZE], | ||
| 27 | ifr_data: 0, | ||
| 28 | }; | ||
| 29 | for (i, byte) in name.as_bytes().iter().enumerate() { | ||
| 30 | ifreq.ifr_name[i] = *byte as libc::c_char | ||
| 31 | } | ||
| 32 | ifreq | ||
| 33 | } | ||
| 34 | |||
| 35 | fn ifreq_ioctl( | ||
| 36 | lower: libc::c_int, | ||
| 37 | ifreq: &mut ifreq, | ||
| 38 | cmd: libc::c_ulong, | ||
| 39 | ) -> io::Result<libc::c_int> { | ||
| 40 | unsafe { | ||
| 41 | let res = libc::ioctl(lower, cmd as _, ifreq as *mut ifreq); | ||
| 42 | if res == -1 { | ||
| 43 | return Err(io::Error::last_os_error()); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | Ok(ifreq.ifr_data) | ||
| 48 | } | ||
| 49 | |||
| 50 | #[derive(Debug)] | ||
| 51 | pub struct TunTap { | ||
| 52 | fd: libc::c_int, | ||
| 53 | ifreq: ifreq, | ||
| 54 | mtu: usize, | ||
| 55 | } | ||
| 56 | |||
| 57 | impl AsRawFd for TunTap { | ||
| 58 | fn as_raw_fd(&self) -> RawFd { | ||
| 59 | self.fd | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | impl TunTap { | ||
| 64 | pub fn new(name: &str) -> io::Result<TunTap> { | ||
| 65 | unsafe { | ||
| 66 | let fd = libc::open( | ||
| 67 | "/dev/net/tun\0".as_ptr() as *const libc::c_char, | ||
| 68 | libc::O_RDWR | libc::O_NONBLOCK, | ||
| 69 | ); | ||
| 70 | if fd == -1 { | ||
| 71 | return Err(io::Error::last_os_error()); | ||
| 72 | } | ||
| 73 | |||
| 74 | let mut ifreq = ifreq_for(name); | ||
| 75 | ifreq.ifr_data = IFF_TAP | IFF_NO_PI; | ||
| 76 | ifreq_ioctl(fd, &mut ifreq, TUNSETIFF)?; | ||
| 77 | |||
| 78 | let socket = libc::socket(libc::AF_INET, libc::SOCK_DGRAM, libc::IPPROTO_IP); | ||
| 79 | if socket == -1 { | ||
| 80 | return Err(io::Error::last_os_error()); | ||
| 81 | } | ||
| 82 | |||
| 83 | let ip_mtu = ifreq_ioctl(socket, &mut ifreq, SIOCGIFMTU); | ||
| 84 | libc::close(socket); | ||
| 85 | let ip_mtu = ip_mtu? as usize; | ||
| 86 | |||
| 87 | // SIOCGIFMTU returns the IP MTU (typically 1500 bytes.) | ||
| 88 | // smoltcp counts the entire Ethernet packet in the MTU, so add the Ethernet header size to it. | ||
| 89 | let mtu = ip_mtu + EthernetFrame::<&[u8]>::header_len(); | ||
| 90 | |||
| 91 | Ok(TunTap { fd, mtu, ifreq }) | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | impl Drop for TunTap { | ||
| 97 | fn drop(&mut self) { | ||
| 98 | unsafe { | ||
| 99 | libc::close(self.fd); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | impl io::Read for TunTap { | ||
| 105 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||
| 106 | let len = unsafe { libc::read(self.fd, buf.as_mut_ptr() as *mut libc::c_void, buf.len()) }; | ||
| 107 | if len == -1 { | ||
| 108 | Err(io::Error::last_os_error()) | ||
| 109 | } else { | ||
| 110 | Ok(len as usize) | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | impl io::Write for TunTap { | ||
| 116 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { | ||
| 117 | let len = unsafe { libc::write(self.fd, buf.as_ptr() as *mut libc::c_void, buf.len()) }; | ||
| 118 | if len == -1 { | ||
| 119 | Err(io::Error::last_os_error()) | ||
| 120 | } else { | ||
| 121 | Ok(len as usize) | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | fn flush(&mut self) -> io::Result<()> { | ||
| 126 | Ok(()) | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | pub struct TunTapDevice { | ||
| 131 | device: Async<TunTap>, | ||
| 132 | waker: Option<Waker>, | ||
| 133 | } | ||
| 134 | |||
| 135 | impl TunTapDevice { | ||
| 136 | pub fn new(name: &str) -> io::Result<TunTapDevice> { | ||
| 137 | Ok(Self { | ||
| 138 | device: Async::new(TunTap::new(name)?)?, | ||
| 139 | waker: None, | ||
| 140 | }) | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | use core::task::Waker; | ||
| 145 | use embassy_net::{DeviceCapabilities, LinkState, Packet, PacketBox, PacketBoxExt, PacketBuf}; | ||
| 146 | use std::task::Context; | ||
| 147 | |||
| 148 | impl crate::Device for TunTapDevice { | ||
| 149 | fn is_transmit_ready(&mut self) -> bool { | ||
| 150 | true | ||
| 151 | } | ||
| 152 | |||
| 153 | fn transmit(&mut self, pkt: PacketBuf) { | ||
| 154 | // todo handle WouldBlock | ||
| 155 | match self.device.get_mut().write(&pkt) { | ||
| 156 | Ok(_) => {} | ||
| 157 | Err(e) if e.kind() == io::ErrorKind::WouldBlock => { | ||
| 158 | info!("transmit WouldBlock"); | ||
| 159 | } | ||
| 160 | Err(e) => panic!("transmit error: {:?}", e), | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | fn receive(&mut self) -> Option<PacketBuf> { | ||
| 165 | let mut pkt = PacketBox::new(Packet::new()).unwrap(); | ||
| 166 | loop { | ||
| 167 | match self.device.get_mut().read(&mut pkt[..]) { | ||
| 168 | Ok(n) => { | ||
| 169 | return Some(pkt.slice(0..n)); | ||
| 170 | } | ||
| 171 | Err(e) if e.kind() == io::ErrorKind::WouldBlock => { | ||
| 172 | let ready = if let Some(w) = self.waker.as_ref() { | ||
| 173 | let mut cx = Context::from_waker(w); | ||
| 174 | let ready = self.device.poll_readable(&mut cx).is_ready(); | ||
| 175 | ready | ||
| 176 | } else { | ||
| 177 | false | ||
| 178 | }; | ||
| 179 | if !ready { | ||
| 180 | return None; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | Err(e) => panic!("read error: {:?}", e), | ||
| 184 | } | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | fn register_waker(&mut self, w: &Waker) { | ||
| 189 | match self.waker { | ||
| 190 | // Optimization: If both the old and new Wakers wake the same task, we can simply | ||
| 191 | // keep the old waker, skipping the clone. (In most executor implementations, | ||
| 192 | // cloning a waker is somewhat expensive, comparable to cloning an Arc). | ||
| 193 | Some(ref w2) if (w2.will_wake(w)) => {} | ||
| 194 | _ => { | ||
| 195 | // clone the new waker and store it | ||
| 196 | if let Some(old_waker) = core::mem::replace(&mut self.waker, Some(w.clone())) { | ||
| 197 | // We had a waker registered for another task. Wake it, so the other task can | ||
| 198 | // reregister itself if it's still interested. | ||
| 199 | // | ||
| 200 | // If two tasks are waiting on the same thing concurrently, this will cause them | ||
| 201 | // to wake each other in a loop fighting over this WakerRegistration. This wastes | ||
| 202 | // CPU but things will still work. | ||
| 203 | // | ||
| 204 | // If the user wants to have two tasks waiting on the same thing they should use | ||
| 205 | // a more appropriate primitive that can store multiple wakers. | ||
| 206 | old_waker.wake() | ||
| 207 | } | ||
| 208 | } | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | fn capabilities(&mut self) -> DeviceCapabilities { | ||
| 213 | let mut caps = DeviceCapabilities::default(); | ||
| 214 | caps.max_transmission_unit = self.device.get_ref().mtu; | ||
| 215 | caps | ||
| 216 | } | ||
| 217 | |||
| 218 | fn link_state(&mut self) -> LinkState { | ||
| 219 | LinkState::Up | ||
| 220 | } | ||
| 221 | |||
| 222 | fn ethernet_address(&mut self) -> [u8; 6] { | ||
| 223 | [0x02, 0x03, 0x04, 0x05, 0x06, 0x07] | ||
| 224 | } | ||
| 225 | } | ||
diff --git a/embassy-net/Cargo.toml b/embassy-net/Cargo.toml index 30970c371..5d391698c 100644 --- a/embassy-net/Cargo.toml +++ b/embassy-net/Cargo.toml | |||
| @@ -25,7 +25,7 @@ log = { version = "0.4.11", optional = true } | |||
| 25 | embassy = { version = "0.1.0", path = "../embassy" } | 25 | embassy = { version = "0.1.0", path = "../embassy" } |
| 26 | 26 | ||
| 27 | managed = { version = "0.8.0", default-features = false, features = [ "map" ]} | 27 | managed = { version = "0.8.0", default-features = false, features = [ "map" ]} |
| 28 | heapless = { version = "0.5.6", default-features = false } | 28 | heapless = { version = "0.7.1", default-features = false } |
| 29 | as-slice = { version = "0.1.4" } | 29 | as-slice = { version = "0.1.4" } |
| 30 | generic-array = { version = "0.14.4", default-features = false } | 30 | generic-array = { version = "0.14.4", default-features = false } |
| 31 | stable_deref_trait = { version = "1.2.0", default-features = false } | 31 | stable_deref_trait = { version = "1.2.0", default-features = false } |
diff --git a/embassy-net/src/config/mod.rs b/embassy-net/src/config/mod.rs index 16470f7e6..94725dba6 100644 --- a/embassy-net/src/config/mod.rs +++ b/embassy-net/src/config/mod.rs | |||
| @@ -1,4 +1,3 @@ | |||
| 1 | use heapless::consts::*; | ||
| 2 | use heapless::Vec; | 1 | use heapless::Vec; |
| 3 | use smoltcp::time::Instant; | 2 | use smoltcp::time::Instant; |
| 4 | use smoltcp::wire::{Ipv4Address, Ipv4Cidr}; | 3 | use smoltcp::wire::{Ipv4Address, Ipv4Cidr}; |
| @@ -29,7 +28,7 @@ pub enum Event { | |||
| 29 | pub struct Config { | 28 | pub struct Config { |
| 30 | pub address: Ipv4Cidr, | 29 | pub address: Ipv4Cidr, |
| 31 | pub gateway: Option<Ipv4Address>, | 30 | pub gateway: Option<Ipv4Address>, |
| 32 | pub dns_servers: Vec<Ipv4Address, U3>, | 31 | pub dns_servers: Vec<Ipv4Address, 3>, |
| 33 | } | 32 | } |
| 34 | 33 | ||
| 35 | pub trait Configurator { | 34 | pub trait Configurator { |
diff --git a/examples/std/Cargo.toml b/examples/std/Cargo.toml index 04e89269d..22ffc8937 100644 --- a/examples/std/Cargo.toml +++ b/examples/std/Cargo.toml | |||
| @@ -18,4 +18,4 @@ nix = "0.21.0" | |||
| 18 | libc = "0.2.81" | 18 | libc = "0.2.81" |
| 19 | clap = { version = "3.0.0-beta.2", features = ["derive"] } | 19 | clap = { version = "3.0.0-beta.2", features = ["derive"] } |
| 20 | rand_core = { version = "0.6.0", features = ["std"] } | 20 | rand_core = { version = "0.6.0", features = ["std"] } |
| 21 | heapless = { version = "0.5.6", default-features = false } | 21 | heapless = { version = "0.7.1", default-features = false } |
diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index c5c8d9ae6..2c16af5fb 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml | |||
| @@ -32,4 +32,4 @@ embedded-hal = { version = "0.2.4" } | |||
| 32 | panic-probe = { version = "0.2.0", features= ["print-defmt"] } | 32 | panic-probe = { version = "0.2.0", features= ["print-defmt"] } |
| 33 | futures = { version = "0.3.8", default-features = false, features = ["async-await"] } | 33 | futures = { version = "0.3.8", default-features = false, features = ["async-await"] } |
| 34 | rtt-target = { version = "0.3", features = ["cortex-m"] } | 34 | rtt-target = { version = "0.3", features = ["cortex-m"] } |
| 35 | heapless = "0.7" \ No newline at end of file | 35 | heapless = { version = "0.7.1", default-features = false } \ No newline at end of file |
