From 9a6afcff75dca4baf4b073b1102c149f5137014e Mon Sep 17 00:00:00 2001 From: Anthony Grondin <104731965+AnthonyGrondin@users.noreply.github.com> Date: Thu, 19 Dec 2024 15:34:48 -0500 Subject: [PATCH] chore: Update smoltcp to `0.12.0` and embassy-net to `0.5.0` --- esp-wifi/Cargo.toml | 2 +- esp-wifi/src/wifi/mod.rs | 31 ++++++++++- examples/Cargo.toml | 11 ++-- .../src/bin/wifi_access_point_with_sta.rs | 6 ++- examples/src/bin/wifi_bench.rs | 12 +++-- examples/src/bin/wifi_coex.rs | 6 ++- examples/src/bin/wifi_dhcp.rs | 5 +- examples/src/bin/wifi_embassy_access_point.rs | 34 +++++------- .../bin/wifi_embassy_access_point_with_sta.rs | 53 +++++++++---------- examples/src/bin/wifi_embassy_bench.rs | 31 ++++++----- examples/src/bin/wifi_embassy_dhcp.rs | 27 +++++----- 11 files changed, 122 insertions(+), 96 deletions(-) diff --git a/esp-wifi/Cargo.toml b/esp-wifi/Cargo.toml index 0925bf50377..a96b4035d7c 100644 --- a/esp-wifi/Cargo.toml +++ b/esp-wifi/Cargo.toml @@ -18,7 +18,7 @@ log = { version = "0.4.22", optional = true } document-features = "0.2.10" esp-alloc = { version = "0.5.0", path = "../esp-alloc", optional = true } esp-hal = { version = "0.22.0", path = "../esp-hal", default-features = false } -smoltcp = { version = "0.11.0", default-features = false, features = [ +smoltcp = { version = "0.12.0", default-features = false, features = [ "medium-ethernet", "socket-raw", ], optional = true } diff --git a/esp-wifi/src/wifi/mod.rs b/esp-wifi/src/wifi/mod.rs index df54224506b..f44a5486c7f 100644 --- a/esp-wifi/src/wifi/mod.rs +++ b/esp-wifi/src/wifi/mod.rs @@ -2680,15 +2680,42 @@ impl WifiRxToken { f(buffer) } + + /// Consumes the RX token and applies the callback function to the received + /// data buffer. + /// + /// TODO: https://github.com/embassy-rs/embassy/issues/3670 + pub fn consume_token_immutable(self, f: F) -> R + where + F: FnOnce(&[u8]) -> R, + { + let mut data = self.mode.data_queue_rx().with(|queue| { + unwrap!( + queue.pop_front(), + "unreachable: transmit()/receive() ensures there is a packet to process" + ) + }); + + // We handle the received data outside of the lock because + // EspWifiPacketBuffer::drop must not be called in a critical section. + // Dropping an EspWifiPacketBuffer will call `esp_wifi_internal_free_rx_buffer` + // which will try to lock an internal mutex. If the mutex is already + // taken, the function will try to trigger a context switch, which will + // fail if we are in an interrupt-free context. + let buffer = data.as_slice_mut(); + dump_packet_info(buffer); + + f(buffer) + } } #[cfg(feature = "smoltcp")] impl RxToken for WifiRxToken { fn consume(self, f: F) -> R where - F: FnOnce(&mut [u8]) -> R, + F: FnOnce(&[u8]) -> R, { - self.consume_token(f) + self.consume_token_immutable(f) } } diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 0e521d25db8..6267df3cc60 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -8,13 +8,13 @@ publish = false [dependencies] aligned = { version = "0.4.2", optional = true } bleps = { git = "https://github.com/bjoernQ/bleps", package = "bleps", rev = "a5148d8ae679e021b78f53fd33afb8bb35d0b62e", features = [ "macros", "async"] } -blocking-network-stack = { git = "https://github.com/bjoernQ/blocking-network-stack.git", rev = "1c581661d78e0cf0f17b936297179b993fb149d7" } +blocking-network-stack = { git = "https://github.com/bjoernQ/blocking-network-stack.git", rev = "b3ecefc222d8806edd221f266999ca339c52d34e" } bt-hci = "0.1.1" cfg-if = "1.0.0" critical-section = "1.1.3" embassy-executor = { version = "0.6.0", features = ["task-arena-size-20480"] } embassy-futures = "0.1.1" -embassy-net = { version = "0.4.0", features = [ "tcp", "udp", "dhcpv4", "medium-ethernet"] } +embassy-net = { version = "0.5.0", features = [ "tcp", "udp", "dhcpv4", "medium-ethernet"] } embassy-sync = "0.6.0" embassy-time = "0.3.2" embassy-usb = { version = "0.2.0", default-features = false } @@ -40,7 +40,7 @@ log = "0.4.22" nb = "1.1.0" portable-atomic = { version = "1.9.0", default-features = false } sha2 = { version = "0.10.8", default-features = false } -smoltcp = { version = "0.11.0", default-features = false, features = [ "medium-ethernet", "socket-raw"] } +smoltcp = { version = "0.12.0", default-features = false, features = [ "medium-ethernet", "socket-raw"] } smoltcp-nal = "0.5.1" embedded-time = "=0.12.1" static_cell = { version = "2.1.0", features = ["nightly"] } @@ -74,3 +74,8 @@ incremental = false opt-level = 3 lto = 'fat' overflow-checks = false + +[patch.crates-io] +# Patch until https://github.com/ivmarkov/edge-net/pull/50 is merged +edge-nal = { git = "https://github.com/ivmarkov/edge-net", rev = "5a85bcc8b8726e8b7044e9526f01cdba1fd684da"} +edge-nal-embassy = { git = "https://github.com/ivmarkov/edge-net", rev = "5a85bcc8b8726e8b7044e9526f01cdba1fd684da"} diff --git a/examples/src/bin/wifi_access_point_with_sta.rs b/examples/src/bin/wifi_access_point_with_sta.rs index 30ac04462e6..84d6e3c4a75 100644 --- a/examples/src/bin/wifi_access_point_with_sta.rs +++ b/examples/src/bin/wifi_access_point_with_sta.rs @@ -15,6 +15,8 @@ #![no_std] #![no_main] +use core::net::Ipv4Addr; + use blocking_network_stack::Stack; use embedded_io::*; use esp_alloc as _; @@ -37,7 +39,7 @@ use esp_wifi::{ }; use smoltcp::{ iface::{SocketSet, SocketStorage}, - wire::{IpAddress, Ipv4Address}, + wire::IpAddress, }; const SSID: &str = env!("SSID"); @@ -182,7 +184,7 @@ fn main() -> ! { sta_socket.work(); sta_socket - .open(IpAddress::Ipv4(Ipv4Address::new(142, 250, 185, 115)), 80) + .open(IpAddress::Ipv4(Ipv4Addr::new(142, 250, 185, 115)), 80) .unwrap(); sta_socket diff --git a/examples/src/bin/wifi_bench.rs b/examples/src/bin/wifi_bench.rs index c01f55e0986..9e5e9467d08 100644 --- a/examples/src/bin/wifi_bench.rs +++ b/examples/src/bin/wifi_bench.rs @@ -14,6 +14,8 @@ #![no_std] #![no_main] +use core::net::Ipv4Addr; + use blocking_network_stack::Stack; use embedded_io::*; use esp_alloc as _; @@ -39,7 +41,7 @@ use esp_wifi::{ }; use smoltcp::{ iface::{SocketSet, SocketStorage}, - wire::{DhcpOption, IpAddress, Ipv4Address}, + wire::{DhcpOption, IpAddress}, }; const SSID: &str = env!("SSID"); @@ -62,7 +64,7 @@ fn main() -> ! { esp_alloc::heap_allocator!(72 * 1024); - let server_address: Ipv4Address = HOST_IP.parse().expect("Invalid HOST_IP address"); + let server_address: Ipv4Addr = HOST_IP.parse().expect("Invalid HOST_IP address"); let timg0 = TimerGroup::new(peripherals.TIMG0); @@ -154,7 +156,7 @@ fn main() -> ! { } fn test_download<'a, D: smoltcp::phy::Device>( - server_address: Ipv4Address, + server_address: Ipv4Addr, socket: &mut blocking_network_stack::Socket<'a, 'a, D>, ) { println!("Testing download..."); @@ -188,7 +190,7 @@ fn test_download<'a, D: smoltcp::phy::Device>( } fn test_upload<'a, D: smoltcp::phy::Device>( - server_address: Ipv4Address, + server_address: Ipv4Addr, socket: &mut blocking_network_stack::Socket<'a, 'a, D>, ) { println!("Testing upload..."); @@ -222,7 +224,7 @@ fn test_upload<'a, D: smoltcp::phy::Device>( } fn test_upload_download<'a, D: smoltcp::phy::Device>( - server_address: Ipv4Address, + server_address: Ipv4Addr, socket: &mut blocking_network_stack::Socket<'a, 'a, D>, ) { println!("Testing upload+download..."); diff --git a/examples/src/bin/wifi_coex.rs b/examples/src/bin/wifi_coex.rs index 55734883c2b..214acf0a0d7 100644 --- a/examples/src/bin/wifi_coex.rs +++ b/examples/src/bin/wifi_coex.rs @@ -15,6 +15,8 @@ #![no_std] #![no_main] +use core::net::Ipv4Addr; + use bleps::{ ad_structure::{ create_advertising_data, @@ -44,7 +46,7 @@ use esp_wifi::{ }; use smoltcp::{ iface::{SocketSet, SocketStorage}, - wire::{DhcpOption, IpAddress, Ipv4Address}, + wire::{DhcpOption, IpAddress}, }; const SSID: &str = env!("SSID"); @@ -171,7 +173,7 @@ fn main() -> ! { socket.work(); socket - .open(IpAddress::Ipv4(Ipv4Address::new(142, 250, 185, 115)), 80) + .open(IpAddress::Ipv4(Ipv4Addr::new(142, 250, 185, 115)), 80) .unwrap(); socket diff --git a/examples/src/bin/wifi_dhcp.rs b/examples/src/bin/wifi_dhcp.rs index 2da9243a985..a3d73882f71 100644 --- a/examples/src/bin/wifi_dhcp.rs +++ b/examples/src/bin/wifi_dhcp.rs @@ -13,6 +13,7 @@ #![no_main] extern crate alloc; +use core::net::Ipv4Addr; use blocking_network_stack::Stack; use embedded_io::*; @@ -38,7 +39,7 @@ use esp_wifi::{ }; use smoltcp::{ iface::{SocketSet, SocketStorage}, - wire::{DhcpOption, IpAddress, Ipv4Address}, + wire::{DhcpOption, IpAddress}, }; const SSID: &str = env!("SSID"); @@ -133,7 +134,7 @@ fn main() -> ! { socket.work(); socket - .open(IpAddress::Ipv4(Ipv4Address::new(142, 250, 185, 115)), 80) + .open(IpAddress::Ipv4(Ipv4Addr::new(142, 250, 185, 115)), 80) .unwrap(); socket diff --git a/examples/src/bin/wifi_embassy_access_point.rs b/examples/src/bin/wifi_embassy_access_point.rs index 762f9bb02e0..710e4b84984 100644 --- a/examples/src/bin/wifi_embassy_access_point.rs +++ b/examples/src/bin/wifi_embassy_access_point.rs @@ -15,14 +15,14 @@ #![no_std] #![no_main] -use core::str::FromStr; +use core::{net::Ipv4Addr, str::FromStr}; use embassy_executor::Spawner; use embassy_net::{ tcp::TcpSocket, IpListenEndpoint, - Ipv4Address, Ipv4Cidr, + Runner, Stack, StackResources, StaticConfigV4, @@ -90,7 +90,7 @@ async fn main(spawner: Spawner) -> ! { } let gw_ip_addr_str = GW_IP_ADDR_ENV.unwrap_or("192.168.2.1"); - let gw_ip_addr = Ipv4Address::from_str(gw_ip_addr_str).expect("failed to parse gateway ip"); + let gw_ip_addr = Ipv4Addr::from_str(gw_ip_addr_str).expect("failed to parse gateway ip"); let config = embassy_net::Config::ipv4_static(StaticConfigV4 { address: Ipv4Cidr::new(gw_ip_addr, 24), @@ -101,19 +101,16 @@ async fn main(spawner: Spawner) -> ! { let seed = (rng.random() as u64) << 32 | rng.random() as u64; // Init network stack - let stack = &*mk_static!( - Stack>, - Stack::new( - wifi_interface, - config, - mk_static!(StackResources<3>, StackResources::<3>::new()), - seed - ) + let (stack, runner) = embassy_net::new( + wifi_interface, + config, + mk_static!(StackResources<3>, StackResources::<3>::new()), + seed, ); spawner.spawn(connection(controller)).ok(); - spawner.spawn(net_task(&stack)).ok(); - spawner.spawn(run_dhcp(&stack, gw_ip_addr_str)).ok(); + spawner.spawn(net_task(runner)).ok(); + spawner.spawn(run_dhcp(stack, gw_ip_addr_str)).ok(); let mut rx_buffer = [0; 1536]; let mut tx_buffer = [0; 1536]; @@ -135,7 +132,7 @@ async fn main(spawner: Spawner) -> ! { .config_v4() .inspect(|c| println!("ipv4 config: {c:?}")); - let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer); + let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); socket.set_timeout(Some(embassy_time::Duration::from_secs(10))); loop { println!("Wait for connection..."); @@ -210,10 +207,7 @@ async fn main(spawner: Spawner) -> ! { } #[embassy_executor::task] -async fn run_dhcp( - stack: &'static Stack>, - gw_ip_addr: &'static str, -) { +async fn run_dhcp(stack: Stack<'static>, gw_ip_addr: &'static str) { use core::net::{Ipv4Addr, SocketAddrV4}; use edge_dhcp::{ @@ -279,6 +273,6 @@ async fn connection(mut controller: WifiController<'static>) { } #[embassy_executor::task] -async fn net_task(stack: &'static Stack>) { - stack.run().await +async fn net_task(mut runner: Runner<'static, WifiDevice<'static, WifiApDevice>>) { + runner.run().await } diff --git a/examples/src/bin/wifi_embassy_access_point_with_sta.rs b/examples/src/bin/wifi_embassy_access_point_with_sta.rs index d25ce440077..f140d4a12f9 100644 --- a/examples/src/bin/wifi_embassy_access_point_with_sta.rs +++ b/examples/src/bin/wifi_embassy_access_point_with_sta.rs @@ -18,13 +18,14 @@ #![no_std] #![no_main] +use core::net::Ipv4Addr; + use embassy_executor::Spawner; use embassy_net::{ tcp::TcpSocket, IpListenEndpoint, - Ipv4Address, Ipv4Cidr, - Stack, + Runner, StackResources, StaticConfigV4, }; @@ -94,8 +95,8 @@ async fn main(spawner: Spawner) -> ! { } let ap_config = embassy_net::Config::ipv4_static(StaticConfigV4 { - address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 2, 1), 24), - gateway: Some(Ipv4Address::from_bytes(&[192, 168, 2, 1])), + address: Ipv4Cidr::new(Ipv4Addr::new(192, 168, 2, 1), 24), + gateway: Some(Ipv4Addr::new(192, 168, 2, 1)), dns_servers: Default::default(), }); let sta_config = embassy_net::Config::dhcpv4(Default::default()); @@ -103,23 +104,17 @@ async fn main(spawner: Spawner) -> ! { let seed = (rng.random() as u64) << 32 | rng.random() as u64; // Init network stacks - let ap_stack = &*mk_static!( - Stack>, - Stack::new( - wifi_ap_interface, - ap_config, - mk_static!(StackResources<3>, StackResources::<3>::new()), - seed - ) + let (ap_stack, ap_runner) = embassy_net::new( + wifi_ap_interface, + ap_config, + mk_static!(StackResources<3>, StackResources::<3>::new()), + seed, ); - let sta_stack = &*mk_static!( - Stack>, - Stack::new( - wifi_sta_interface, - sta_config, - mk_static!(StackResources<3>, StackResources::<3>::new()), - seed - ) + let (sta_stack, sta_runner) = embassy_net::new( + wifi_sta_interface, + sta_config, + mk_static!(StackResources<3>, StackResources::<3>::new()), + seed, ); let client_config = Configuration::Mixed( @@ -136,8 +131,8 @@ async fn main(spawner: Spawner) -> ! { controller.set_configuration(&client_config).unwrap(); spawner.spawn(connection(controller)).ok(); - spawner.spawn(ap_task(&ap_stack)).ok(); - spawner.spawn(sta_task(&sta_stack)).ok(); + spawner.spawn(ap_task(ap_runner)).ok(); + spawner.spawn(sta_task(sta_runner)).ok(); loop { if sta_stack.is_link_up() { @@ -158,13 +153,13 @@ async fn main(spawner: Spawner) -> ! { let mut ap_rx_buffer = [0; 1536]; let mut ap_tx_buffer = [0; 1536]; - let mut ap_socket = TcpSocket::new(&ap_stack, &mut ap_rx_buffer, &mut ap_tx_buffer); + let mut ap_socket = TcpSocket::new(ap_stack, &mut ap_rx_buffer, &mut ap_tx_buffer); ap_socket.set_timeout(Some(embassy_time::Duration::from_secs(10))); let mut sta_rx_buffer = [0; 1536]; let mut sta_tx_buffer = [0; 1536]; - let mut sta_socket = TcpSocket::new(&sta_stack, &mut sta_rx_buffer, &mut sta_tx_buffer); + let mut sta_socket = TcpSocket::new(sta_stack, &mut sta_rx_buffer, &mut sta_tx_buffer); sta_socket.set_timeout(Some(embassy_time::Duration::from_secs(10))); loop { @@ -212,7 +207,7 @@ async fn main(spawner: Spawner) -> ! { } if sta_stack.is_link_up() { - let remote_endpoint = (Ipv4Address::new(142, 250, 185, 115), 80); + let remote_endpoint = (Ipv4Addr::new(142, 250, 185, 115), 80); println!("connecting..."); let r = sta_socket.connect(remote_endpoint).await; if let Err(e) = r { @@ -334,11 +329,11 @@ async fn connection(mut controller: WifiController<'static>) { } #[embassy_executor::task] -async fn ap_task(stack: &'static Stack>) { - stack.run().await +async fn ap_task(mut runner: Runner<'static, WifiDevice<'static, WifiApDevice>>) { + runner.run().await } #[embassy_executor::task] -async fn sta_task(stack: &'static Stack>) { - stack.run().await +async fn sta_task(mut runner: Runner<'static, WifiDevice<'static, WifiStaDevice>>) { + runner.run().await } diff --git a/examples/src/bin/wifi_embassy_bench.rs b/examples/src/bin/wifi_embassy_bench.rs index f85b0c7a863..183afb9aa89 100644 --- a/examples/src/bin/wifi_embassy_bench.rs +++ b/examples/src/bin/wifi_embassy_bench.rs @@ -17,9 +17,11 @@ #![no_std] #![no_main] +use core::net::Ipv4Addr; + use embassy_executor::Spawner; use embassy_futures::join::join; -use embassy_net::{tcp::TcpSocket, Ipv4Address, Stack, StackResources}; +use embassy_net::{tcp::TcpSocket, Runner, StackResources}; use embassy_time::{with_timeout, Duration, Timer}; use esp_alloc as _; use esp_backtrace as _; @@ -91,7 +93,7 @@ async fn main(spawner: Spawner) -> ! { )); } - let server_address: Ipv4Address = HOST_IP.parse().expect("Invalid HOST_IP address"); + let server_address: Ipv4Addr = HOST_IP.parse().expect("Invalid HOST_IP address"); let timg0 = TimerGroup::new(peripherals.TIMG0); let mut rng = Rng::new(peripherals.RNG); @@ -121,18 +123,15 @@ async fn main(spawner: Spawner) -> ! { let seed = (rng.random() as u64) << 32 | rng.random() as u64; // Init network stack - let stack = &*mk_static!( - Stack>, - Stack::new( - wifi_interface, - config, - mk_static!(StackResources<3>, StackResources::<3>::new()), - seed - ) + let (stack, runner) = embassy_net::new( + wifi_interface, + config, + mk_static!(StackResources<3>, StackResources::<3>::new()), + seed, ); spawner.spawn(connection(controller)).ok(); - spawner.spawn(net_task(&stack)).ok(); + spawner.spawn(net_task(runner)).ok(); loop { if stack.is_link_up() { @@ -202,11 +201,11 @@ async fn connection(mut controller: WifiController<'static>) { } #[embassy_executor::task] -async fn net_task(stack: &'static Stack>) { - stack.run().await +async fn net_task(mut runner: Runner<'static, WifiDevice<'static, WifiStaDevice>>) { + runner.run().await } -async fn test_download(server_address: Ipv4Address, socket: &mut TcpSocket<'_>) -> usize { +async fn test_download(server_address: Ipv4Addr, socket: &mut TcpSocket<'_>) -> usize { println!("Testing download..."); socket.abort(); @@ -244,7 +243,7 @@ async fn test_download(server_address: Ipv4Address, socket: &mut TcpSocket<'_>) kbps } -async fn test_upload(server_address: Ipv4Address, socket: &mut TcpSocket<'_>) -> usize { +async fn test_upload(server_address: Ipv4Addr, socket: &mut TcpSocket<'_>) -> usize { println!("Testing upload..."); socket.abort(); socket.set_timeout(Some(Duration::from_secs(10))); @@ -281,7 +280,7 @@ async fn test_upload(server_address: Ipv4Address, socket: &mut TcpSocket<'_>) -> kbps } -async fn test_upload_download(server_address: Ipv4Address, socket: &mut TcpSocket<'_>) -> usize { +async fn test_upload_download(server_address: Ipv4Addr, socket: &mut TcpSocket<'_>) -> usize { println!("Testing upload+download..."); socket.abort(); diff --git a/examples/src/bin/wifi_embassy_dhcp.rs b/examples/src/bin/wifi_embassy_dhcp.rs index 5f53e5d70a5..16824805432 100644 --- a/examples/src/bin/wifi_embassy_dhcp.rs +++ b/examples/src/bin/wifi_embassy_dhcp.rs @@ -13,8 +13,10 @@ #![no_std] #![no_main] +use core::net::Ipv4Addr; + use embassy_executor::Spawner; -use embassy_net::{tcp::TcpSocket, Ipv4Address, Stack, StackResources}; +use embassy_net::{tcp::TcpSocket, Runner, StackResources}; use embassy_time::{Duration, Timer}; use esp_alloc as _; use esp_backtrace as _; @@ -83,18 +85,15 @@ async fn main(spawner: Spawner) -> ! { let seed = (rng.random() as u64) << 32 | rng.random() as u64; // Init network stack - let stack = &*mk_static!( - Stack>, - Stack::new( - wifi_interface, - config, - mk_static!(StackResources<3>, StackResources::<3>::new()), - seed - ) + let (stack, runner) = embassy_net::new( + wifi_interface, + config, + mk_static!(StackResources<3>, StackResources::<3>::new()), + seed, ); spawner.spawn(connection(controller)).ok(); - spawner.spawn(net_task(&stack)).ok(); + spawner.spawn(net_task(runner)).ok(); let mut rx_buffer = [0; 4096]; let mut tx_buffer = [0; 4096]; @@ -118,11 +117,11 @@ async fn main(spawner: Spawner) -> ! { loop { Timer::after(Duration::from_millis(1_000)).await; - let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer); + let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); socket.set_timeout(Some(embassy_time::Duration::from_secs(10))); - let remote_endpoint = (Ipv4Address::new(142, 250, 185, 115), 80); + let remote_endpoint = (Ipv4Addr::new(142, 250, 185, 115), 80); println!("connecting..."); let r = socket.connect(remote_endpoint).await; if let Err(e) = r { @@ -194,6 +193,6 @@ async fn connection(mut controller: WifiController<'static>) { } #[embassy_executor::task] -async fn net_task(stack: &'static Stack>) { - stack.run().await +async fn net_task(mut runner: Runner<'static, WifiDevice<'static, WifiStaDevice>>) { + runner.run().await }