From 6295c5f9da84830faa27f52ab832c74a31401f6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Quentin?= Date: Mon, 9 Sep 2024 12:15:52 +0200 Subject: [PATCH] Esp wifi/fix wifi logs (#2117) * Fix `wifi-logs` feature * Some clarifications in the esp-wifi migration guide * CHANGELOG.md * Review comments * Check more features --- esp-wifi/CHANGELOG.md | 1 + esp-wifi/MIGRATING-0.9.md | 28 ++++++++++++++++++++++++++++ esp-wifi/src/compat/syslog.rs | 12 ++++++++---- esp-wifi/src/wifi/mod.rs | 1 + esp-wifi/src/wifi/os_adapter.rs | 5 ++++- xtask/src/main.rs | 7 ++++--- 6 files changed, 46 insertions(+), 8 deletions(-) diff --git a/esp-wifi/CHANGELOG.md b/esp-wifi/CHANGELOG.md index b85f9612d..0b3a39694 100644 --- a/esp-wifi/CHANGELOG.md +++ b/esp-wifi/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - esp-wifi now allocates memory from the global allocator provided by `esp-alloc` (#2099) ### Fixed +- Feature `wifi-logs` doesn't break the build anymore (#2117) ### Removed diff --git a/esp-wifi/MIGRATING-0.9.md b/esp-wifi/MIGRATING-0.9.md index c33374dcf..b05b54f3b 100644 --- a/esp-wifi/MIGRATING-0.9.md +++ b/esp-wifi/MIGRATING-0.9.md @@ -60,3 +60,31 @@ You now need to have a global allocator provided by `esp-alloc` providing alloca // ... ``` + +The size of the heap depends on what you are going to use esp-wifi for and if you are using the heap for your own allocations or not. + +E.g. when using `coex` you need around 92k. If not using `coex`, going lower than 72k you will observe some failed allocations but it might still work. Going even lower will make things fail. + +### Using your own allocator + +You can also use your own allocator instead of using `esp-alloc`. To do that you need to opt-out of the default feature `esp-alloc` and provide two functions: + +```rust +#[no_mangle] +pub extern "C" fn esp_wifi_free_internal_heap() -> usize { + // return size of free allocatable RAM +} + +#[no_mangle] +pub extern "C" fn esp_wifi_allocate_from_internal_ram(size: usize) -> *mut u8 { + // allocate memory of size `size` from internal memory + unsafe { + core::alloc::alloc( + esp_alloc::MemoryCapability::Internal.into(), + core::alloc::Layout::from_size_align_unchecked(size, 4), + ) + } +} +``` + +It's important to allocate from internal memory (i.e. not PSRAM) diff --git a/esp-wifi/src/compat/syslog.rs b/esp-wifi/src/compat/syslog.rs index ef44bd846..6bf140a7c 100644 --- a/esp-wifi/src/compat/syslog.rs +++ b/esp-wifi/src/compat/syslog.rs @@ -6,15 +6,19 @@ pub unsafe extern "C" fn syslog(_priority: u32, _format: *const u8, _args: VaLis cfg_if::cfg_if! { if #[cfg(any(target_arch = "riscv32", all(target_arch = "xtensa", xtensa_has_vaarg)))] { + extern "C" { + fn vsnprintf(buffer: *mut u8, len: usize, fmt: *const u8, args: VaListImpl); + } + let mut buf = [0u8; 512]; vsnprintf(&mut buf as *mut u8, 512, _format, _args); - let res_str = str_from_c(&buf as *const u8); - info!("{}", res_str); + let res_str = core::ffi::CStr::from_ptr(&buf as *const _ as *const i8); + info!("{}", res_str.to_str().unwrap()); } else { - let res_str = str_from_c(_format); - info!("{}", res_str); + let res_str = core::ffi::CStr::from_ptr(_format as *const i8); + info!("{}", res_str.to_str().unwrap()); } } } diff --git a/esp-wifi/src/wifi/mod.rs b/esp-wifi/src/wifi/mod.rs index f049cedbe..fd394da07 100644 --- a/esp-wifi/src/wifi/mod.rs +++ b/esp-wifi/src/wifi/mod.rs @@ -2044,6 +2044,7 @@ impl PromiscuousPkt<'_> { } #[cfg(feature = "sniffer")] +#[allow(clippy::type_complexity)] static SNIFFER_CB: Mutex>> = Mutex::new(RefCell::new(None)); #[cfg(feature = "sniffer")] diff --git a/esp-wifi/src/wifi/os_adapter.rs b/esp-wifi/src/wifi/os_adapter.rs index b45a2276d..6735bd075 100644 --- a/esp-wifi/src/wifi/os_adapter.rs +++ b/esp-wifi/src/wifi/os_adapter.rs @@ -1449,7 +1449,7 @@ pub unsafe extern "C" fn log_write( format: *const crate::binary::c_types::c_char, args: ... ) { - let args = core::mem::transmute(args); + let args = core::mem::transmute::, core::ffi::VaListImpl<'_>>(args); crate::compat::syslog::syslog(level, format as *const u8, args); } @@ -1477,6 +1477,9 @@ pub unsafe extern "C" fn log_writev( format: *const crate::binary::c_types::c_char, args: esp_wifi_sys::include::va_list, ) { + // annotations on transmute here would require different types for RISC-V and + // Xtensa - so let's allow `missing_transmute_annotations` in this case + #[allow(clippy::missing_transmute_annotations)] let args = core::mem::transmute(args); crate::compat::syslog::syslog(level, format as *const u8, args); } diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 6514c2c80..a4ffc2bff 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -652,11 +652,12 @@ fn lint_packages(workspace: &Path, args: LintPackagesArgs) -> Result<()> { } Package::EspWifi => { - let mut features = format!("--features={chip},async,ps-min-modem,defmt"); + let mut features = format!( + "--features={chip},async,ps-min-modem,defmt,dump-packets,wifi-logs" + ); if device.contains("wifi") { - features - .push_str(",wifi-default,esp-now,embedded-svc,embassy-net,dump-packets") + features.push_str(",wifi-default,esp-now,embedded-svc,embassy-net,sniffer") } if device.contains("bt") { features.push_str(",ble")