Skip to content

Commit

Permalink
chore: Update smoltcp to 0.12.0 and embassy-net to 0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyGrondin committed Dec 19, 2024
1 parent f83ab23 commit 9a6afcf
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 96 deletions.
2 changes: 1 addition & 1 deletion esp-wifi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
31 changes: 29 additions & 2 deletions esp-wifi/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2680,15 +2680,42 @@ impl<Dm: Sealed> WifiRxToken<Dm> {

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<R, F>(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<Dm: Sealed> RxToken for WifiRxToken<Dm> {
fn consume<R, F>(self, f: F) -> R
where
F: FnOnce(&mut [u8]) -> R,
F: FnOnce(&[u8]) -> R,
{
self.consume_token(f)
self.consume_token_immutable(f)
}
}

Expand Down
11 changes: 8 additions & 3 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"] }
Expand Down Expand Up @@ -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"}
6 changes: 4 additions & 2 deletions examples/src/bin/wifi_access_point_with_sta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#![no_std]
#![no_main]

use core::net::Ipv4Addr;

use blocking_network_stack::Stack;
use embedded_io::*;
use esp_alloc as _;
Expand All @@ -37,7 +39,7 @@ use esp_wifi::{
};
use smoltcp::{
iface::{SocketSet, SocketStorage},
wire::{IpAddress, Ipv4Address},
wire::IpAddress,
};

const SSID: &str = env!("SSID");
Expand Down Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions examples/src/bin/wifi_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#![no_std]
#![no_main]

use core::net::Ipv4Addr;

use blocking_network_stack::Stack;
use embedded_io::*;
use esp_alloc as _;
Expand All @@ -39,7 +41,7 @@ use esp_wifi::{
};
use smoltcp::{
iface::{SocketSet, SocketStorage},
wire::{DhcpOption, IpAddress, Ipv4Address},
wire::{DhcpOption, IpAddress},
};

const SSID: &str = env!("SSID");
Expand All @@ -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);

Expand Down Expand Up @@ -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...");
Expand Down Expand Up @@ -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...");
Expand Down Expand Up @@ -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...");
Expand Down
6 changes: 4 additions & 2 deletions examples/src/bin/wifi_coex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#![no_std]
#![no_main]

use core::net::Ipv4Addr;

use bleps::{
ad_structure::{
create_advertising_data,
Expand Down Expand Up @@ -44,7 +46,7 @@ use esp_wifi::{
};
use smoltcp::{
iface::{SocketSet, SocketStorage},
wire::{DhcpOption, IpAddress, Ipv4Address},
wire::{DhcpOption, IpAddress},
};

const SSID: &str = env!("SSID");
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions examples/src/bin/wifi_dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#![no_main]

extern crate alloc;
use core::net::Ipv4Addr;

use blocking_network_stack::Stack;
use embedded_io::*;
Expand All @@ -38,7 +39,7 @@ use esp_wifi::{
};
use smoltcp::{
iface::{SocketSet, SocketStorage},
wire::{DhcpOption, IpAddress, Ipv4Address},
wire::{DhcpOption, IpAddress},
};

const SSID: &str = env!("SSID");
Expand Down Expand Up @@ -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
Expand Down
34 changes: 14 additions & 20 deletions examples/src/bin/wifi_embassy_access_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand All @@ -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<WifiDevice<'_, WifiApDevice>>,
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];
Expand All @@ -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...");
Expand Down Expand Up @@ -210,10 +207,7 @@ async fn main(spawner: Spawner) -> ! {
}

#[embassy_executor::task]
async fn run_dhcp(
stack: &'static Stack<WifiDevice<'static, WifiApDevice>>,
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::{
Expand Down Expand Up @@ -279,6 +273,6 @@ async fn connection(mut controller: WifiController<'static>) {
}

#[embassy_executor::task]
async fn net_task(stack: &'static Stack<WifiDevice<'static, WifiApDevice>>) {
stack.run().await
async fn net_task(mut runner: Runner<'static, WifiDevice<'static, WifiApDevice>>) {
runner.run().await
}
Loading

0 comments on commit 9a6afcf

Please sign in to comment.