Skip to content

Commit

Permalink
Rethink WiFi API (#3027)
Browse files Browse the repository at this point in the history
* Rethink WiFi API

* Fix
  • Loading branch information
bjoernQ authored Feb 14, 2025
1 parent 1e58278 commit 0138d46
Show file tree
Hide file tree
Showing 36 changed files with 1,022 additions and 1,108 deletions.
2 changes: 2 additions & 0 deletions esp-wifi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `set_power_saving` is now also available when the `coex` feature is activated (#3081)

- Network interfaces and the controller are now more separated (#3027)

### Fixed

- Fixed a problem using BLE on ESP32-C6 when connected via Serial-JTAG (#2981)
Expand Down
3 changes: 0 additions & 3 deletions esp-wifi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@ csi = []
## Provide implementations of smoltcp traits
smoltcp = ["dep:smoltcp"]

## Provide utilities for smoltcp initialization. Adds smoltcp dependency
utils = ["smoltcp"]

## Use builtin scheduler
builtin-scheduler = []

Expand Down
37 changes: 37 additions & 0 deletions esp-wifi/MIGRATING-0.11.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,40 @@ As part of limiting public API changes due to config options, the `csi_enabled`
-esp-wifi = { version = "0.12.0", features = ["wifi"] }
+esp-wifi = { version = "0.12.0", features = ["wifi", "csi"] }
```

## Changed the way to get the WiFi controller and interfaces

The network interfaces and the controller are now more separated. This way you can change between STA, AP and AP_STA mode easily without reconstructing the networking stacks.

There is no convenience utility to create a `smoltcp` interface needed by blocking networking stacks anymore. You need your own implementation.

Please note that networking stacks _might_ need to be reset when connecting to a different network interface (i.e. get a new IP address and routings) - `embassy-net` should manage to do that automatically.

```diff
- let (iface, device, mut controller) =
- create_network_interface(&init, peripherals.WIFI, WifiStaDevice).unwrap();
+ let (mut controller, interfaces) =
+ esp_wifi::wifi::new(&init, peripherals.WIFI).unwrap();
+ let mut device = interfaces.sta;
+ let iface = create_interface(&mut device);
...
+ fn timestamp() -> smoltcp::time::Instant {
+ smoltcp::time::Instant::from_micros(
+ esp_hal::time::Instant::now()
+ .duration_since_epoch()
+ .as_micros() as i64,
+ )
+ }
+
+ pub fn create_interface(device: &mut esp_wifi::wifi::WifiDevice) -> smoltcp::iface::Interface {
+ // users could create multiple instances but since they only have one WifiDevice
+ // they probably can't do anything bad with that
+ smoltcp::iface::Interface::new(
+ smoltcp::iface::Config::new(smoltcp::wire::HardwareAddress::Ethernet(
+ smoltcp::wire::EthernetAddress::from_bytes(&device.mac_address()),
+ )),
+ device,
+ timestamp(),
+ )
+ }
```
6 changes: 3 additions & 3 deletions esp-wifi/src/ble/btdm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::boxed::Box;
use core::ptr::{addr_of, addr_of_mut};

use esp_wifi_sys::c_types::c_void;
use esp_wifi_sys::c_types::{c_char, c_void};
use portable_atomic::{AtomicBool, Ordering};

use super::ReceivedPacket;
Expand Down Expand Up @@ -36,7 +36,7 @@ struct VhciHostCallbacks {

extern "C" {
fn btdm_osi_funcs_register(osi_funcs: *const osi_funcs_s) -> i32;
fn btdm_controller_get_compile_version() -> *const u8;
fn btdm_controller_get_compile_version() -> *const c_char;

#[cfg(any(esp32c3, esp32s3))]
fn btdm_controller_init(config_opts: *const esp_bt_controller_config_t) -> i32;
Expand Down Expand Up @@ -208,7 +208,7 @@ unsafe extern "C" fn queue_recv_from_isr(

unsafe extern "C" fn task_create(
func: *mut crate::binary::c_types::c_void,
name: *const u8,
name: *const c_char,
stack_depth: u32,
param: *mut crate::binary::c_types::c_void,
prio: u32,
Expand Down
4 changes: 2 additions & 2 deletions esp-wifi/src/ble/npl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ unsafe extern "C" fn task_create(
task_handle: *const c_void,
core_id: u32,
) -> i32 {
let name_str = str_from_c(name as *const _);
let name_str = str_from_c(name);
trace!(
"task_create {:?} {} {} {:?} {} {:?} {}",
task_func,
Expand Down Expand Up @@ -421,7 +421,7 @@ unsafe extern "C" fn task_delete(task: *const c_void) {
}

unsafe extern "C" fn osi_assert(ln: u32, fn_name: *const c_void, param1: u32, param2: u32) {
let name_str = str_from_c(fn_name as *const u8);
let name_str = str_from_c(fn_name as _);
panic!("ASSERT {}:{} {} {}", name_str, ln, param1, param2);
}

Expand Down
2 changes: 1 addition & 1 deletion esp-wifi/src/ble/os_adapter_esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(super) struct osi_funcs_s {
task_create: Option<
unsafe extern "C" fn(
*mut crate::binary::c_types::c_void,
*const u8,
*const crate::binary::c_types::c_char,
u32,
*mut crate::binary::c_types::c_void,
u32,
Expand Down
7 changes: 5 additions & 2 deletions esp-wifi/src/ble/os_adapter_esp32c3.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::*;
use crate::hal::{interrupt, peripherals::Interrupt};
use crate::{
binary::c_types::c_char,
hal::{interrupt, peripherals::Interrupt},
};

pub(crate) static mut BT_INTERRUPT_FUNCTION5: (
*mut crate::binary::c_types::c_void,
Expand Down Expand Up @@ -43,7 +46,7 @@ pub(super) struct osi_funcs_s {
task_create: Option<
unsafe extern "C" fn(
*mut crate::binary::c_types::c_void,
*const u8,
*const c_char,
u32,
*mut crate::binary::c_types::c_void,
u32,
Expand Down
2 changes: 1 addition & 1 deletion esp-wifi/src/ble/os_adapter_esp32s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub(super) struct osi_funcs_s {
task_create: Option<
unsafe extern "C" fn(
*mut crate::binary::c_types::c_void,
*const u8,
*const crate::binary::c_types::c_char,
u32,
*mut crate::binary::c_types::c_void,
u32,
Expand Down
2 changes: 1 addition & 1 deletion esp-wifi/src/common_adapter/common_adapter_esp32c2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(crate) unsafe fn phy_enable() {
[0u8; core::mem::size_of::<esp_phy_calibration_data_t>()];

let phy_version = get_phy_version_str();
trace!("phy_version {}", str_from_c(phy_version as *const u8));
trace!("phy_version {}", str_from_c(phy_version));

let init_data = &PHY_INIT_DATA_DEFAULT;

Expand Down
2 changes: 1 addition & 1 deletion esp-wifi/src/common_adapter/common_adapter_esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub(crate) unsafe fn phy_enable() {
[0u8; core::mem::size_of::<esp_phy_calibration_data_t>()];

let phy_version = get_phy_version_str();
trace!("phy_version {}", str_from_c(phy_version as *const u8));
trace!("phy_version {}", str_from_c(phy_version));

let init_data = &PHY_INIT_DATA_DEFAULT;

Expand Down
2 changes: 1 addition & 1 deletion esp-wifi/src/common_adapter/common_adapter_esp32c6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(crate) unsafe fn phy_enable() {
[0u8; core::mem::size_of::<esp_phy_calibration_data_t>()];

let phy_version = get_phy_version_str();
trace!("phy_version {}", str_from_c(phy_version as *const u8));
trace!("phy_version {}", str_from_c(phy_version));

let init_data = &PHY_INIT_DATA_DEFAULT;

Expand Down
2 changes: 1 addition & 1 deletion esp-wifi/src/common_adapter/common_adapter_esp32h2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub(crate) unsafe fn phy_enable() {
[0u8; core::mem::size_of::<esp_phy_calibration_data_t>()];

let phy_version = get_phy_version_str();
trace!("phy_version {}", str_from_c(phy_version as *const u8));
trace!("phy_version {}", str_from_c(phy_version));

let init_data = &PHY_INIT_DATA_DEFAULT;

Expand Down
10 changes: 5 additions & 5 deletions esp-wifi/src/common_adapter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use esp_wifi_sys::include::timeval;
use esp_wifi_sys::{c_types::c_char, include::timeval};
use portable_atomic::{AtomicU32, Ordering};

use crate::{
Expand Down Expand Up @@ -193,7 +193,7 @@ pub(crate) unsafe extern "C" fn semphr_give_from_isr(sem: *const (), hptw: *cons

// other functions
#[no_mangle]
pub unsafe extern "C" fn puts(s: *const u8) {
pub unsafe extern "C" fn puts(s: *const c_char) {
let cstr = str_from_c(s);
info!("{}", cstr);
}
Expand All @@ -205,10 +205,10 @@ static mut WIFI_EVENT: esp_event_base_t = c"WIFI_EVENT".as_ptr();
// stuff needed by wpa-supplicant
#[no_mangle]
pub unsafe extern "C" fn __assert_func(
file: *const u8,
file: *const c_char,
line: u32,
func: *const u8,
failed_expr: *const u8,
func: *const c_char,
failed_expr: *const c_char,
) {
let file = str_from_c(file);
let (func_pre, func) = if func.is_null() {
Expand Down
6 changes: 3 additions & 3 deletions esp-wifi/src/compat/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::{
ptr::{self, addr_of, addr_of_mut},
};

use esp_wifi_sys::include::malloc;
use esp_wifi_sys::{c_types::c_char, include::malloc};

use super::malloc::free;
use crate::{
Expand Down Expand Up @@ -164,13 +164,13 @@ impl RawQueue {
}
}

pub unsafe fn str_from_c<'a>(s: *const u8) -> &'a str {
pub unsafe fn str_from_c<'a>(s: *const c_char) -> &'a str {
let c_str = core::ffi::CStr::from_ptr(s.cast());
core::str::from_utf8_unchecked(c_str.to_bytes())
}

#[no_mangle]
unsafe extern "C" fn strnlen(chars: *const u8, maxlen: usize) -> usize {
unsafe extern "C" fn strnlen(chars: *const c_char, maxlen: usize) -> usize {
let mut len = 0;
loop {
if chars.offset(len).read_volatile() == 0 {
Expand Down
Loading

0 comments on commit 0138d46

Please sign in to comment.