Skip to content

Commit 8e6b591

Browse files
authored
esp-radio: Wrap C types that are needed fot pub API (#4148)
1 parent 90382c9 commit 8e6b591

File tree

2 files changed

+83
-7
lines changed

2 files changed

+83
-7
lines changed

esp-radio/src/wifi/event.rs

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,22 @@ impl StaDisconnected<'_> {
281281
}
282282
}
283283

284+
/// All AP credentials received from WPS handshake.
285+
#[repr(transparent)]
286+
pub struct ApCredential(wifi_event_sta_wps_er_success_t__bindgen_ty_1);
287+
288+
impl ApCredential {
289+
/// Get the SSID of an AP.
290+
pub fn ssid(&self) -> &[u8] {
291+
&self.0.ssid
292+
}
293+
294+
/// Get passphrase for the AP.
295+
pub fn passphrase(&self) -> &[u8] {
296+
&self.0.passphrase
297+
}
298+
}
299+
284300
impl StaAuthmodeChange<'_> {
285301
/// Get the old authentication mode.
286302
pub fn old_mode(&self) -> u32 {
@@ -300,8 +316,12 @@ impl StaWpsErSuccess<'_> {
300316
}
301317

302318
/// Get all AP credentials received.
303-
pub fn ap_cred(&self) -> &[wifi_event_sta_wps_er_success_t__bindgen_ty_1] {
304-
&self.0.ap_cred
319+
pub fn ap_cred(&self) -> &[ApCredential] {
320+
let array_ref: &[ApCredential; 3] =
321+
// cast reference of fixed-size array to wrapper type
322+
unsafe { &*(&self.0.ap_cred as *const _ as *const [ApCredential; 3]) };
323+
324+
&array_ref[..]
305325
}
306326
}
307327

@@ -312,6 +332,47 @@ impl StaWpsErPin<'_> {
312332
}
313333
}
314334

335+
/// A safe, read-only wrapper for a single FTM report entry.
336+
#[repr(transparent)]
337+
pub struct FtmReportEntry<'a>(&'a wifi_ftm_report_entry_t);
338+
339+
impl FtmReportEntry<'_> {
340+
/// Gets the Dialog Token of the FTM frame.
341+
pub fn dialog_token(&self) -> u8 {
342+
self.0.dlog_token
343+
}
344+
345+
/// Gets the Received Signal Strength Indicator (RSSI) of the FTM frame.
346+
pub fn rssi(&self) -> i8 {
347+
self.0.rssi
348+
}
349+
350+
/// Gets the Round Trip Time (RTT) in picoseconds.
351+
pub fn rtt(&self) -> u32 {
352+
self.0.rtt
353+
}
354+
355+
/// Gets T1: Time of departure of the FTM frame from the Responder (in picoseconds).
356+
pub fn t1(&self) -> u64 {
357+
self.0.t1
358+
}
359+
360+
/// Gets T2: Time of arrival of the FTM frame at the Initiator (in picoseconds).
361+
pub fn t2(&self) -> u64 {
362+
self.0.t2
363+
}
364+
365+
/// Gets T3: Time of departure of the ACK from the Initiator (in picoseconds).
366+
pub fn t3(&self) -> u64 {
367+
self.0.t3
368+
}
369+
370+
/// Gets T4: Time of arrival of the ACK at the Responder (in picoseconds).
371+
pub fn t4(&self) -> u64 {
372+
self.0.t4
373+
}
374+
}
375+
315376
impl FtmReport<'_> {
316377
/// Get the MAC address of the FTM peer.
317378
pub fn peer_mac(&self) -> &[u8] {
@@ -338,15 +399,27 @@ impl FtmReport<'_> {
338399
self.0.dist_est
339400
}
340401

341-
/// Get Pointer to FTM Report, should be freed after use.
342-
pub fn report_data(&self) -> *mut wifi_ftm_report_entry_t {
343-
self.0.ftm_report_data
344-
}
345-
346402
/// Get the number of entries in the FTM report data.
347403
pub fn report_num_entries(&self) -> u8 {
348404
self.0.ftm_report_num_entries
349405
}
406+
407+
/// Returns an iterator over the detailed FTM report entries.
408+
pub fn entries(&self) -> impl Iterator<Item = FtmReportEntry<'_>> + '_ {
409+
let ptr = self.0.ftm_report_data;
410+
let len = self.0.ftm_report_num_entries as usize;
411+
412+
// Return an empty slice when there are no entries.
413+
let entries_slice = if ptr.is_null() || len == 0 {
414+
&[]
415+
} else {
416+
// Otherwise, it's the slice from the data.
417+
// Can we trust the C API to provide a valid pointer and length?
418+
unsafe { core::slice::from_raw_parts(ptr, len) }
419+
};
420+
421+
entries_slice.iter().map(FtmReportEntry)
422+
}
350423
}
351424

352425
impl ApProbeReqReceived<'_> {

esp-radio/src/wifi/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,7 @@ impl TryFrom<&Config> for WifiMode {
909909
}
910910
}
911911

912+
#[doc(hidden)]
912913
impl TryFrom<wifi_mode_t> for WifiMode {
913914
type Error = WifiError;
914915

@@ -924,6 +925,7 @@ impl TryFrom<wifi_mode_t> for WifiMode {
924925
}
925926
}
926927

928+
#[doc(hidden)]
927929
impl From<WifiMode> for wifi_mode_t {
928930
fn from(val: WifiMode) -> Self {
929931
#[allow(non_upper_case_globals)]
@@ -1048,6 +1050,7 @@ impl Default for CsiConfig {
10481050
}
10491051
}
10501052

1053+
#[doc(hidden)]
10511054
#[cfg(feature = "csi")]
10521055
impl From<CsiConfig> for wifi_csi_config_t {
10531056
fn from(config: CsiConfig) -> Self {

0 commit comments

Comments
 (0)