From 7534606ab6a4f84b9d15c34528b6423b82900803 Mon Sep 17 00:00:00 2001 From: Juraj Sadel Date: Tue, 19 Aug 2025 15:43:39 +0200 Subject: [PATCH] esp-radio: Hide C-types from public API (#3907) * Hide C-types from public API * Add accessories for all events --- esp-radio/src/wifi/event.rs | 554 ++++++++++++++++++++++++- examples/wifi/access_point/src/main.rs | 7 +- 2 files changed, 556 insertions(+), 5 deletions(-) diff --git a/esp-radio/src/wifi/event.rs b/esp-radio/src/wifi/event.rs index bb7424eca..db0b11197 100644 --- a/esp-radio/src/wifi/event.rs +++ b/esp-radio/src/wifi/event.rs @@ -3,6 +3,10 @@ use alloc::boxed::Box; use esp_hal::sync::Locked; use super::WifiEvent; +use crate::wifi::include::{ + wifi_event_sta_wps_er_success_t__bindgen_ty_1, + wifi_ftm_report_entry_t, +}; pub(crate) mod sealed { use super::*; @@ -89,10 +93,10 @@ macro_rules! impl_wifi_event { }; // data ($newtype:ident, $data:ident) => { - pub use esp_wifi_sys::include::$data; + use esp_wifi_sys::include::$data; /// See [`WifiEvent`]. #[derive(Copy, Clone)] - pub struct $newtype(pub $data); + pub struct $newtype($data); impl sealed::Event for $newtype { unsafe fn from_raw_event_data(ptr: *mut crate::binary::c_types::c_void) -> Self { Self(unsafe { *ptr.cast() }) @@ -163,6 +167,552 @@ impl_wifi_event!(NdpTerminated, wifi_event_ndp_terminated_t); impl_wifi_event!(HomeChannelChange, wifi_event_home_channel_change_t); impl_wifi_event!(StaNeighborRep, wifi_event_neighbor_report_t); +impl ApStaconnected { + /// Get the MAC address of the connected station. + pub fn mac(&self) -> &[u8] { + &self.0.mac + } + + /// Get the AID (Association Identifier) of the connected station. + pub fn aid(&self) -> u8 { + self.0.aid + } +} + +impl ApStadisconnected { + /// Get the MAC address of the disconnected station. + pub fn mac(&self) -> &[u8] { + &self.0.mac + } + + /// Get the reason for the disconnection. + pub fn reason(&self) -> u16 { + self.0.reason + } +} + +impl ScanDone { + /// Get the status of the scan operation. + pub fn status(&self) -> u32 { + self.0.status + } + + /// Get the number of found APs. + pub fn number(&self) -> u8 { + self.0.number + } + + /// Get the scan ID associated with this scan operation. + pub fn id(&self) -> u8 { + self.0.scan_id + } +} + +impl StaConnected { + /// Get the SSID of the connected station. + pub fn ssid(&self) -> &[u8] { + &self.0.ssid + } + + /// Get the length of the SSID. + pub fn ssid_len(&self) -> u8 { + self.0.ssid_len + } + + /// Get the BSSID (MAC address) of the connected station. + pub fn bssid(&self) -> &[u8] { + &self.0.bssid + } + + /// Get the channel on which the station is connected. + pub fn channel(&self) -> u8 { + self.0.channel + } + + /// Get the authentication mode used for the connection. + pub fn authmode(&self) -> u32 { + self.0.authmode + } + + /// Get the AID (Association Identifier) of the connected station. + pub fn aid(&self) -> u16 { + self.0.aid + } +} + +impl StaDisconnected { + /// Get the SSID of the disconnected station. + pub fn ssid(&self) -> &[u8] { + &self.0.ssid + } + + /// Get the length of the SSID. + pub fn ssid_len(&self) -> u8 { + self.0.ssid_len + } + + /// Get the BSSID (MAC address) of the disconnected station. + pub fn bssid(&self) -> &[u8] { + &self.0.bssid + } + + /// Get the reason for the disconnection. + pub fn reason(&self) -> u8 { + self.0.reason + } + + /// Get the authentication mode used for the disconnection. + pub fn rssi(&self) -> i8 { + self.0.rssi + } +} + +impl StaAuthmodeChange { + /// Get the old authentication mode. + pub fn old_mode(&self) -> u32 { + self.0.old_mode + } + + /// Get the new authentication mode. + pub fn new_mode(&self) -> u32 { + self.0.new_mode + } +} + +impl StaWpsErSuccess { + /// Get number of AP credentials received. + pub fn ap_cred_cnt(&self) -> u8 { + self.0.ap_cred_cnt + } + + /// Get all AP credentials received. + pub fn ap_cred(&self) -> &[wifi_event_sta_wps_er_success_t__bindgen_ty_1] { + &self.0.ap_cred + } +} + +impl StaWpsErPin { + /// Get the PIN code received from the WPS. + pub fn pin(&self) -> &[u8] { + &self.0.pin_code + } +} + +impl FtmReport { + /// Get the MAC address of the FTM peer. + pub fn peer_mac(&self) -> &[u8] { + &self.0.peer_mac + } + + /// Get the status of the FTM operation. + pub fn status(&self) -> u32 { + self.0.status + } + + /// Get the raw round-trip time (RTT) in nanoseconds. + pub fn rtt_raw(&self) -> u32 { + self.0.rtt_raw + } + + /// Get the estimated round-trip time (RTT) in nanoseconds. + pub fn rtt_est(&self) -> u32 { + self.0.rtt_est + } + + /// Get the distance estimate in centimeters. + pub fn dist_est(&self) -> u32 { + self.0.dist_est + } + + /// Get Pointer to FTM Report, should be freed after use. + pub fn report_data(&self) -> *mut wifi_ftm_report_entry_t { + self.0.ftm_report_data + } + + /// Get the number of entries in the FTM report data. + pub fn report_num_entries(&self) -> u8 { + self.0.ftm_report_num_entries + } +} + +impl ApProbereqrecved { + /// Get received probe request SSID. + pub fn rssi(&self) -> i32 { + self.0.rssi + } + + /// Get the MAC address of the station which send probe request. + pub fn mac(&self) -> &[u8] { + &self.0.mac + } +} + +impl StaBssRssiLow { + /// Get received probe request SSID of bss. + pub fn rssi(&self) -> i32 { + self.0.rssi + } +} + +impl ActionTxStatus { + /// Get WiFi interface to send request to. + pub fn ifx(&self) -> u32 { + self.0.ifx + } + + /// Get context to identify the request. + pub fn context(&self) -> u32 { + self.0.context + } + + /// Get destination MAC address. + pub fn da(&self) -> &[u8] { + &self.0.da + } + + /// Get the status of the operation. + pub fn status(&self) -> u8 { + self.0.status + } +} + +impl RocDone { + /// Get context to identify the request. + pub fn context(&self) -> u32 { + self.0.context + } +} + +impl ApWpsRgSuccess { + /// Gegt enrollee mac address. + pub fn peer_mac(&self) -> &[u8] { + &self.0.peer_macaddr + } +} + +impl ApWpsRgFailed { + /// Get WPS failure reason. + pub fn reason(&self) -> u32 { + self.0.reason + } + + /// Gegt enrollee mac address. + pub fn peer_macaddr(&self) -> &[u8; 6] { + &self.0.peer_macaddr + } +} + +impl ApWpsRgPin { + /// Get the PIN code of station in enrollee mode. + pub fn pin_code(&self) -> &[u8] { + &self.0.pin_code + } +} + +cfg_if::cfg_if! { + if #[cfg(wifi_has_wifi6)] { + use crate::wifi::include::wifi_twt_setup_config_t; + + impl ItwtSetup { + /// Get the itwt setup config, this value is determined by the AP. + pub fn config(&self) -> &wifi_twt_setup_config_t { + &self.0.config + } + + /// Get the itwt setup status, 1 indicates success, others indicate setup failure. + pub fn status(&self) -> i32 { + self.0.status + } + + /// Get the itwt setup frame tx fail reason. + pub fn reason(&self) -> u8 { + self.0.reason + } + + /// Get TWT SP start time. + pub fn target_wake_time(&self) -> u64 { + self.0.target_wake_time + } + } + + impl ItwtTeardown { + /// Get flow id. + pub fn flow_id(&self) -> u8 { + self.0.flow_id + } + + /// Get itwt teardown status. + pub fn status(&self) -> u32 { + self.0.status + } + } + + impl ItwtProbe { + /// Get probe status. + pub fn status(&self) -> u32 { + self.0.status + } + + /// Get failure reason. + pub fn reason(&self) -> u8 { + self.0.reason + } + } + + impl ItwtSuspend { + /// Get suspend status. + pub fn status(&self) -> i32 { + self.0.status + } + + /// Get bitmap of the suspended flow id. + pub fn flow_id_bitmap(&self) -> u8 { + self.0.flow_id_bitmap + } + + /// Get the actual suspend time for each flow id in milliseconds. + pub fn actual_suspend_time_ms(&self) -> &[u32] { + &self.0.actual_suspend_time_ms + } + } + + impl BtwtSetup { + /// Get the btwt setup status. + pub fn status(&self) -> u32 { + self.0.status + } + + /// Get the type of TWT command. + pub fn cmd(&self) -> u32 { + self.0.setup_cmd + } + + /// Get the TWT id. + pub fn btwt_id(&self) -> u8 { + self.0.btwt_id + } + + /// Get the Nominal Minimum Wake Duration, indicates the minimum amount of time, + /// in units of 256 µs, that the TWT requesting STA expects that it needs to be awake. + pub fn min_wake_dura(&self) -> u8 { + self.0.min_wake_dura + } + + /// Get the TWT Wake Interval Exponent. + pub fn wake_invl_expn(&self) -> u8 { + self.0.wake_invl_expn + } + + /// Get the TWT Wake Interval Mantissa. + pub fn wake_invl_mant(&self) -> u16 { + self.0.wake_invl_mant + } + + /// Get whether this is a trigger-enabled TWT (true) or a non-trigger-enabled TWT (false). + pub fn trigger(&self) -> bool { + self.0.trigger + } + + /// Get the TWT flow type, an announced TWT (true) or an unannounced TWT (false). + pub fn flow_type(&self) -> u8 { + self.0.flow_type + } + + /// Get the btwt setup frame tx fail reason. + pub fn reason(&self) -> u8 { + self.0.reason + } + + /// Get the TWT SP start time. + pub fn target_wake_time(&self) -> u64 { + self.0.target_wake_time + } + } + + impl BtwtTeardown { + /// Get the TWT id. + pub fn id(&self) -> u8 { + self.0.btwt_id + } + + /// Get the btwt teardown status. + pub fn status(&self) -> u32 { + self.0.status + } + } + } +} + +impl NanSvcMatch { + /// Get the Subscribe Service id. + pub fn subscribe_id(&self) -> u8 { + self.0.subscribe_id + } + + /// Get the Publish Service id. + pub fn publish_id(&self) -> u8 { + self.0.publish_id + } + + /// Get the NAN Interface MAC of the Publisher. + pub fn pub_if_mac(&self) -> &[u8] { + &self.0.pub_if_mac + } + + /// Indicates whether publisher’s service ID needs to be updated. + pub fn update_pub_id(&self) -> bool { + self.0.update_pub_id + } +} + +impl NanReplied { + /// Get the Subscribe Service id. + pub fn subscribe_id(&self) -> u8 { + self.0.subscribe_id + } + + /// Get the Publish Service id. + pub fn publish_id(&self) -> u8 { + self.0.publish_id + } + + /// Get the NAN Interface MAC of the Subscriber. + pub fn sub_if_mac(&self) -> &[u8] { + &self.0.sub_if_mac + } +} + +impl NanReceive { + /// Get Our Service Identifier. + pub fn inst_id(&self) -> u8 { + self.0.inst_id + } + + /// Get Peer's Service Identifier. + pub fn peer_inst_id(&self) -> u8 { + self.0.peer_inst_id + } + + /// Get Peer’s NAN Interface MAC + pub fn peer_if_mac(&self) -> &[u8; 6] { + &self.0.peer_if_mac + } + + /// Get Peer Service Info. + pub fn peer_svc_info(&self) -> &[u8; 64] { + &self.0.peer_svc_info + } +} + +impl NdpIndication { + /// Get Publish Id for NAN Service. + pub fn publish_id(&self) -> u8 { + self.0.publish_id + } + + /// Get NDF instance id. + pub fn ndp_id(&self) -> u8 { + self.0.ndp_id + } + + /// Get Peer’s NAN Interface MAC. + pub fn peer_nmi(&self) -> &[u8; 6] { + &self.0.peer_nmi + } + + /// Get Peer’s NAN Data Interface MAC. + pub fn peer_ndi(&self) -> &[u8; 6] { + &self.0.peer_ndi + } + + /// Get Service Specific Info. + pub fn svc_info(&self) -> &[u8; 64] { + &self.0.svc_info + } +} + +impl NdpConfirm { + /// Get NDP status code. + pub fn status(&self) -> u8 { + self.0.status + } + + /// Get NDP instance id. + pub fn id(&self) -> u8 { + self.0.ndp_id + } + + /// Get Peer’s NAN Management Interface MAC. + pub fn peer_nmi(&self) -> &[u8; 6] { + &self.0.peer_nmi + } + + /// Get Peer’s NAN Data Interface MAC. + pub fn peer_ndi(&self) -> &[u8; 6] { + &self.0.peer_ndi + } + + /// Get Own NAN Data Interface MAC. + pub fn own_ndi(&self) -> &[u8; 6] { + &self.0.own_ndi + } + + /// Get Service Specific Info. + pub fn svc_info(&self) -> &[u8; 64] { + &self.0.svc_info + } +} + +impl NdpTerminated { + /// Get termination reason code. + pub fn reason(&self) -> u8 { + self.0.reason + } + + /// Get NDP instance id. + pub fn id(&self) -> u8 { + self.0.ndp_id + } + + /// Get Initiator’s NAN Data Interface MAC + pub fn init_ndi(&self) -> &[u8; 6] { + &self.0.init_ndi + } +} + +impl HomeChannelChange { + /// Get the old home channel of the device. + pub fn old_chan(&self) -> u8 { + self.0.old_chan + } + + /// Get the old second channel of the device. + pub fn old_snd(&self) -> u32 { + self.0.old_snd + } + + /// Get the new home channel of the device. + pub fn new_chan(&self) -> u8 { + self.0.new_chan + } + + /// Get the new second channel of the device. + pub fn new_snd(&self) -> u32 { + self.0.new_snd + } +} + +impl StaNeighborRep { + /// Get the Neighbor Report received from the AP. + pub fn report(&self) -> &[u8] { + &self.0.report[..self.0.report_len as usize] + } + + /// Get the length of report. + pub fn report_len(&self) -> u16 { + self.0.report_len + } +} /// Handle the given event using the registered event handlers. #[instability::unstable] pub fn handle(event_data: &Event) -> bool { diff --git a/examples/wifi/access_point/src/main.rs b/examples/wifi/access_point/src/main.rs index 5a722898d..4bb9c13a8 100644 --- a/examples/wifi/access_point/src/main.rs +++ b/examples/wifi/access_point/src/main.rs @@ -50,15 +50,16 @@ fn main() -> ! { _ = event::ApStart::replace_handler(|_| println!("ap start event")); event::ApStaconnected::update_handler(move |event| { connections += 1; - println!("connected {}, mac: {:?}", connections, event.0.mac); + esp_println::println!("connected {}, mac: {:?}", connections, event.mac()); }); event::ApStaconnected::update_handler(|event| { - println!("connected aid: {}", event.0.aid); + esp_println::println!("connected aid: {}", event.aid()); }); event::ApStadisconnected::update_handler(|event| { println!( "disconnected mac: {:?}, reason: {:?}", - event.0.mac, event.0.reason + event.mac(), + event.reason() ); });