Skip to content

Commit

Permalink
Smoltcp 0.9 upgrade (esp-rs#124)
Browse files Browse the repository at this point in the history
* switch to heapless for SimpleQueue

* Merge Network into WifiStack

- Merge the Network and Wifi structs into WifiStack
- Get it building & running with just TCP socket for now

* get dhcp example building with changes and no warnings

* fixup error checking

* Fix other examples

* fmt

* Remove redundant comment

* fix unused results

* bump BT_INTERNAL_QUEUE to 10

* add back static ip configuration in work()
  • Loading branch information
MabezDev authored and bjoernQ committed May 23, 2024
1 parent 77b547e commit aa85d75
Show file tree
Hide file tree
Showing 14 changed files with 500 additions and 723 deletions.
2 changes: 1 addition & 1 deletion esp-wifi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ riscv-rt = { version = "0.11.0", optional = true }
riscv = { version = "0.10.1", optional = true }
xtensa-lx-rt = { version = "0.14.0", optional = true }
xtensa-lx = { version = "0.7.0", optional = true }
smoltcp = { version = "0.8.2", default-features=false, features = ["proto-igmp", "proto-ipv4", "socket-tcp", "socket-icmp", "socket-udp", "medium-ethernet", "proto-dhcpv4", "socket-raw", "socket-dhcpv4"] }
smoltcp = { version = "0.9.1", default-features=false, features = ["proto-igmp", "proto-ipv4", "socket-tcp", "socket-icmp", "socket-udp", "medium-ethernet", "proto-dhcpv4", "socket-raw", "socket-dhcpv4"] }
critical-section = "1.1.1"
atomic-polyfill = "1.0.1"
log = "0.4.17"
Expand Down
12 changes: 3 additions & 9 deletions esp-wifi/examples/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@

use bleps::{
ad_structure::{
create_advertising_data,
AdStructure,
BR_EDR_NOT_SUPPORTED,
LE_GENERAL_DISCOVERABLE,
create_advertising_data, AdStructure, BR_EDR_NOT_SUPPORTED, LE_GENERAL_DISCOVERABLE,
},
attribute_server::{AttributeServer, NotificationData, WorkResult},
Ble,
HciConnector,
Ble, HciConnector,
};
use bleps_macros::gatt;
#[cfg(feature = "esp32")]
Expand All @@ -32,9 +28,7 @@ use hal::{
clock::{ClockControl, CpuClock},
peripherals::*,
prelude::*,
Rng,
Rtc,
IO,
Rng, Rtc, IO,
};
#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use riscv_rt::entry;
Expand Down
39 changes: 17 additions & 22 deletions esp-wifi/examples/coex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use bleps::{
Ble, HciConnector,
};

use esp_wifi::{ble::controller::BleConnector, current_millis, wifi_interface::Network};
use esp_wifi::{ble::controller::BleConnector, current_millis, wifi_interface::WifiStack};

use embedded_io::blocking::*;
use embedded_svc::ipv4::Interface;
Expand All @@ -27,14 +27,13 @@ use embedded_svc::wifi::{AccessPointInfo, ClientConfiguration, Configuration, Wi
use esp_backtrace as _;
use esp_println::{logger::init_logger, print, println};
use esp_wifi::initialize;
use esp_wifi::wifi::{WifiError, utils::create_network_interface};
use esp_wifi::{create_network_stack_storage, network_stack_storage};
use esp_wifi::wifi::{utils::create_network_interface, WifiError};
use hal::{
clock::{ClockControl, CpuClock},
Rng,
};
use hal::{peripherals::Peripherals, prelude::*, Rtc};
use smoltcp::wire::Ipv4Address;
use smoltcp::{iface::SocketStorage, wire::Ipv4Address};

#[cfg(feature = "esp32c3")]
use hal::system::SystemExt;
Expand Down Expand Up @@ -72,9 +71,9 @@ fn main() -> ! {

rtc.rwdt.disable();

let mut storage = create_network_stack_storage!(3, 8, 1, 1);
let ethernet = create_network_interface(network_stack_storage!(storage));
let mut wifi_interface = esp_wifi::wifi_interface::Wifi::new(ethernet);
let mut socket_set_entries: [SocketStorage; 3] = Default::default();
let (iface, device, sockets) = create_network_interface(&mut socket_set_entries);
let mut wifi_stack = WifiStack::new(iface, device, sockets, current_millis);

#[cfg(feature = "esp32c3")]
{
Expand All @@ -89,11 +88,10 @@ fn main() -> ! {
initialize(timg1.timer0, Rng::new(peripherals.RNG), &clocks).unwrap();
}

println!("is wifi started: {:?}", wifi_interface.is_started());
println!("is wifi started: {:?}", wifi_stack.is_started());

println!("Start Wifi Scan");
let res: Result<(heapless::Vec<AccessPointInfo, 10>, usize), WifiError> =
wifi_interface.scan_n();
let res: Result<(heapless::Vec<AccessPointInfo, 10>, usize), WifiError> = wifi_stack.scan_n();
if let Ok((res, _count)) = res {
for ap in res {
println!("{:?}", ap);
Expand All @@ -106,16 +104,16 @@ fn main() -> ! {
password: PASSWORD.into(),
..Default::default()
});
let res = wifi_interface.set_configuration(&client_config);
let res = wifi_stack.set_configuration(&client_config);
println!("wifi_connect returned {:?}", res);

println!("{:?}", wifi_interface.get_capabilities());
println!("wifi_connect {:?}", wifi_interface.connect());
println!("{:?}", wifi_stack.get_capabilities());
println!("wifi_connect {:?}", wifi_stack.connect());

// wait to get connected
println!("Wait to get connected");
loop {
let res = wifi_interface.is_connected();
let res = wifi_stack.is_connected();
match res {
Ok(connected) => {
if connected {
Expand All @@ -128,18 +126,15 @@ fn main() -> ! {
}
}
}
println!("{:?}", wifi_interface.is_connected());
println!("{:?}", wifi_stack.is_connected());

// wait for getting an ip address
println!("Wait to get an ip address");
let network = Network::new(wifi_interface, current_millis);
loop {
network.poll_dhcp().unwrap();
wifi_stack.work();

network.work();

if network.is_iface_up() {
println!("got ip {:?}", network.get_ip_info());
if wifi_stack.is_iface_up() {
println!("got ip {:?}", wifi_stack.get_ip_info());
break;
}
}
Expand Down Expand Up @@ -171,7 +166,7 @@ fn main() -> ! {

let mut rx_buffer = [0u8; 1536];
let mut tx_buffer = [0u8; 1536];
let mut socket = network.get_socket(&mut rx_buffer, &mut tx_buffer);
let mut socket = wifi_stack.get_socket(&mut rx_buffer, &mut tx_buffer);

loop {
println!("Making HTTP request");
Expand Down
39 changes: 18 additions & 21 deletions esp-wifi/examples/dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ use embedded_svc::wifi::{AccessPointInfo, ClientConfiguration, Configuration, Wi
use esp_backtrace as _;
use esp_println::logger::init_logger;
use esp_println::{print, println};
use esp_wifi::wifi::{WifiError, utils::create_network_interface};
use esp_wifi::wifi_interface::Network;
use esp_wifi::{create_network_stack_storage, network_stack_storage};
use esp_wifi::wifi::utils::create_network_interface;
use esp_wifi::wifi::WifiError;
use esp_wifi::wifi_interface::WifiStack;
use esp_wifi::{current_millis, initialize};
use hal::clock::{ClockControl, CpuClock};
use hal::Rng;
use hal::{peripherals::Peripherals, prelude::*, Rtc};
use smoltcp::iface::SocketStorage;
use smoltcp::wire::Ipv4Address;

#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
Expand Down Expand Up @@ -68,9 +69,9 @@ fn main() -> ! {

rtc.rwdt.disable();

let mut storage = create_network_stack_storage!(3, 8, 1, 1);
let ethernet = create_network_interface(network_stack_storage!(storage));
let mut wifi_interface = esp_wifi::wifi_interface::Wifi::new(ethernet);
let mut socket_set_entries: [SocketStorage; 3] = Default::default();
let (iface, device, sockets) = create_network_interface(&mut socket_set_entries);
let mut wifi_stack = WifiStack::new(iface, device, sockets, current_millis);

#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
{
Expand All @@ -85,11 +86,10 @@ fn main() -> ! {
initialize(timg1.timer0, Rng::new(peripherals.RNG), &clocks).unwrap();
}

println!("is wifi started: {:?}", wifi_interface.is_started());
println!("is wifi started: {:?}", wifi_stack.is_started());

println!("Start Wifi Scan");
let res: Result<(heapless::Vec<AccessPointInfo, 10>, usize), WifiError> =
wifi_interface.scan_n();
let res: Result<(heapless::Vec<AccessPointInfo, 10>, usize), WifiError> = wifi_stack.scan_n();
if let Ok((res, _count)) = res {
for ap in res {
println!("{:?}", ap);
Expand All @@ -102,16 +102,16 @@ fn main() -> ! {
password: PASSWORD.into(),
..Default::default()
});
let res = wifi_interface.set_configuration(&client_config);
let res = wifi_stack.set_configuration(&client_config);
println!("wifi_set_configuration returned {:?}", res);

println!("{:?}", wifi_interface.get_capabilities());
println!("wifi_connect {:?}", wifi_interface.connect());
println!("{:?}", wifi_stack.get_capabilities());
println!("wifi_connect {:?}", wifi_stack.connect());

// wait to get connected
println!("Wait to get connected");
loop {
let res = wifi_interface.is_connected();
let res = wifi_stack.is_connected();
match res {
Ok(connected) => {
if connected {
Expand All @@ -124,18 +124,15 @@ fn main() -> ! {
}
}
}
println!("{:?}", wifi_interface.is_connected());
println!("{:?}", wifi_stack.is_connected());

// wait for getting an ip address
println!("Wait to get an ip address");
let network = Network::new(wifi_interface, current_millis);
loop {
network.poll_dhcp().unwrap();
wifi_stack.work();

network.work();

if network.is_iface_up() {
println!("got ip {:?}", network.get_ip_info());
if wifi_stack.is_iface_up() {
println!("got ip {:?}", wifi_stack.get_ip_info());
break;
}
}
Expand All @@ -144,7 +141,7 @@ fn main() -> ! {

let mut rx_buffer = [0u8; 1536];
let mut tx_buffer = [0u8; 1536];
let mut socket = network.get_socket(&mut rx_buffer, &mut tx_buffer);
let mut socket = wifi_stack.get_socket(&mut rx_buffer, &mut tx_buffer);

loop {
println!("Making HTTP request");
Expand Down
36 changes: 18 additions & 18 deletions esp-wifi/examples/static_ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ use embedded_svc::wifi::{AccessPointInfo, ClientConfiguration, Configuration, Wi
use esp_backtrace as _;
use esp_println::logger::init_logger;
use esp_println::{print, println};
use esp_wifi::wifi::{WifiError, utils::create_network_interface};
use esp_wifi::wifi_interface::Network;
use esp_wifi::{create_network_stack_storage, network_stack_storage};
use esp_wifi::{current_millis, initialize};
use esp_wifi::current_millis;
use esp_wifi::initialize;
use esp_wifi::wifi::{utils::create_network_interface, WifiError};
use esp_wifi::wifi_interface::WifiStack;
use hal::clock::{ClockControl, CpuClock};
use hal::Rng;
use hal::{peripherals::Peripherals, prelude::*, Rtc};
Expand All @@ -34,6 +34,7 @@ use hal::system::SystemExt;

#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use riscv_rt::entry;
use smoltcp::iface::SocketStorage;
#[cfg(any(feature = "esp32", feature = "esp32s3", feature = "esp32s2"))]
use xtensa_lx_rt::entry;

Expand Down Expand Up @@ -69,9 +70,9 @@ fn main() -> ! {

rtc.rwdt.disable();

let mut storage = create_network_stack_storage!(3, 8, 1, 1);
let ethernet = create_network_interface(network_stack_storage!(storage));
let mut wifi_interface = esp_wifi::wifi_interface::Wifi::new(ethernet);
let mut socket_set_entries: [SocketStorage; 3] = Default::default();
let (iface, device, sockets) = create_network_interface(&mut socket_set_entries);
let mut wifi_stack = WifiStack::new(iface, device, sockets, current_millis);

#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
{
Expand All @@ -86,11 +87,10 @@ fn main() -> ! {
initialize(timg1.timer0, Rng::new(peripherals.RNG), &clocks).unwrap();
}

println!("is wifi started: {:?}", wifi_interface.is_started());
println!("is wifi started: {:?}", wifi_stack.is_started());

println!("Start Wifi Scan");
let res: Result<(heapless::Vec<AccessPointInfo, 10>, usize), WifiError> =
wifi_interface.scan_n();
let res: Result<(heapless::Vec<AccessPointInfo, 10>, usize), WifiError> = wifi_stack.scan_n();
if let Ok((res, _count)) = res {
for ap in res {
println!("{:?}", ap);
Expand All @@ -103,16 +103,16 @@ fn main() -> ! {
password: PASSWORD.into(),
..Default::default()
});
let res = wifi_interface.set_configuration(&client_config);
let res = wifi_stack.set_configuration(&client_config);
println!("wifi_set_configuration returned {:?}", res);

println!("{:?}", wifi_interface.get_capabilities());
println!("wifi_connect {:?}", wifi_interface.connect());
println!("{:?}", wifi_stack.get_capabilities());
println!("wifi_connect {:?}", wifi_stack.connect());

// wait to get connected
println!("Wait to get connected");
loop {
let res = wifi_interface.is_connected();
let res = wifi_stack.is_connected();
match res {
Ok(connected) => {
if connected {
Expand All @@ -125,11 +125,11 @@ fn main() -> ! {
}
}
}
println!("{:?}", wifi_interface.is_connected());
println!("{:?}", wifi_stack.is_connected());

println!("Setting static IP {}", STATIC_IP);
let mut network = Network::new(wifi_interface, current_millis);
network

wifi_stack
.set_iface_configuration(&embedded_svc::ipv4::Configuration::Client(
embedded_svc::ipv4::ClientConfiguration::Fixed(embedded_svc::ipv4::ClientSettings {
ip: embedded_svc::ipv4::Ipv4Addr::from(parse_ip(STATIC_IP)),
Expand All @@ -150,7 +150,7 @@ fn main() -> ! {

let mut rx_buffer = [0u8; 1536];
let mut tx_buffer = [0u8; 1536];
let mut socket = network.get_socket(&mut rx_buffer, &mut tx_buffer);
let mut socket = wifi_stack.get_socket(&mut rx_buffer, &mut tx_buffer);

socket.listen(8080).unwrap();

Expand Down
6 changes: 3 additions & 3 deletions esp-wifi/src/ble/btdm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct ReceivedPacket {
pub data: [u8; 256],
}

static BT_INTERNAL_QUEUE: Mutex<RefCell<SimpleQueue<[u8; 8], 5>>> =
static BT_INTERNAL_QUEUE: Mutex<RefCell<SimpleQueue<[u8; 8], 10>>> =
Mutex::new(RefCell::new(SimpleQueue::new()));

#[repr(C)]
Expand Down Expand Up @@ -91,7 +91,7 @@ extern "C" fn notify_host_recv(data: *mut u8, len: u16) -> i32 {

critical_section::with(|cs| {
let mut queue = BT_RECEIVE_QUEUE.borrow_ref_mut(cs);
queue.enqueue(packet);
queue.enqueue(packet).unwrap();
});
}

Expand Down Expand Up @@ -189,7 +189,7 @@ unsafe extern "C" fn queue_send(queue: *const (), item: *const (), _block_time_m

critical_section::with(|cs| {
let mut queue = BT_INTERNAL_QUEUE.borrow_ref_mut(cs);
queue.enqueue(data);
queue.enqueue(data).unwrap();
});
memory_fence();
});
Expand Down
20 changes: 12 additions & 8 deletions esp-wifi/src/ble/npl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,10 +1226,12 @@ unsafe extern "C" fn ble_hs_hci_rx_evt(cmd: *const u8, arg: *const c_void) {
data[2] = len as u8;
data[3..][..len].copy_from_slice(payload);

queue.enqueue(ReceivedPacket {
len: (len + 3) as u8,
data,
});
queue
.enqueue(ReceivedPacket {
len: (len + 3) as u8,
data,
})
.unwrap();
});
}

Expand All @@ -1247,10 +1249,12 @@ unsafe extern "C" fn ble_hs_rx_data(om: *const OsMbuf, arg: *const c_void) {
data[0] = 0x02; // ACL
data[1..][..data_slice.len()].copy_from_slice(data_slice);

queue.enqueue(ReceivedPacket {
len: (len + 1) as u8,
data,
});
queue
.enqueue(ReceivedPacket {
len: (len + 1) as u8,
data,
})
.unwrap();
});
}

Expand Down
Loading

0 comments on commit aa85d75

Please sign in to comment.