From 2f4c51c35df2708cf7a41e47acfff597ed0e8c85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Fri, 27 Oct 2023 19:58:00 +0200 Subject: [PATCH] Fix and clean up npl --- esp-wifi/src/ble/npl.rs | 106 +++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 55 deletions(-) diff --git a/esp-wifi/src/ble/npl.rs b/esp-wifi/src/ble/npl.rs index b2d8a5c8..81e0349a 100644 --- a/esp-wifi/src/ble/npl.rs +++ b/esp-wifi/src/ble/npl.rs @@ -1,4 +1,7 @@ -use core::cell::RefCell; +use core::{ + cell::RefCell, + ptr::{addr_of, addr_of_mut}, +}; use critical_section::Mutex; @@ -28,29 +31,21 @@ const BLE_HCI_TRANS_BUF_CMD: i32 = 3; /* ACL_DATA_MBUF_LEADINGSPCAE: The leadingspace in user info header for ACL data */ const ACL_DATA_MBUF_LEADINGSPACE: usize = 4; -#[derive(Copy, Clone, Default)] +#[derive(Copy, Clone)] struct Callout { - _callout_id: u32, - eventq_id: u32, - event_id: u32, - timer_handle: u32, + _callout: *const ble_npl_callout, + eventq: *const ble_npl_eventq, + timer_handle: ets_timer, + events: ble_npl_event, } static mut CALLOUTS: [Option; 12] = [None; 12]; -static mut CALLOUT_TIMERS: [ets_timer; 12] = [ets_timer { - next: core::ptr::null_mut(), - expire: 0, - period: 0, - func: None, - priv_: core::ptr::null_mut(), -}; 12]; -static mut CALLOUT_EVENTS: [u32; 12] = [0u32; 12]; - -#[derive(Copy, Clone, Default)] + +#[derive(Copy, Clone)] struct Event { - event_id: u32, - event_fn_ptr: u32, - ev_arg_ptr: u32, + event: *const ble_npl_event, + event_fn_ptr: *const ble_npl_event_fn, + ev_arg_ptr: *const c_void, queued: bool, } @@ -694,7 +689,7 @@ unsafe extern "C" fn ble_npl_callout_stop(callout: *const ble_npl_callout) { let co = unwrap!(CALLOUTS[((*callout).dummy - 1) as usize].as_mut()); // stop timer - compat::timer_compat::compat_timer_disarm(co.timer_handle as *mut c_void); + compat::timer_compat::compat_timer_disarm(addr_of_mut!(co.timer_handle)); } unsafe extern "C" fn ble_npl_callout_reset( @@ -706,7 +701,7 @@ unsafe extern "C" fn ble_npl_callout_reset( let co = unwrap!(CALLOUTS[((*callout).dummy - 1) as usize].as_mut()); // start timer - compat::timer_compat::compat_timer_arm(co.timer_handle as *mut c_void, time, false); + compat::timer_compat::compat_timer_arm(addr_of_mut!(co.timer_handle), time, false); 0 } @@ -755,7 +750,7 @@ unsafe extern "C" fn ble_npl_event_set_arg(event: *const ble_npl_event, arg: *co panic!("Call set_arg on uninitialized event"); } - unwrap!(EVENTS[((*event).dummy - 1) as usize].as_mut()).ev_arg_ptr = arg as u32; + unwrap!(EVENTS[((*event).dummy - 1) as usize].as_mut()).ev_arg_ptr = arg; } unsafe extern "C" fn ble_npl_event_get_arg(event: *const ble_npl_event) -> *const c_void { @@ -764,12 +759,11 @@ unsafe extern "C" fn ble_npl_event_get_arg(event: *const ble_npl_event) -> *cons panic!("Call get_arg on uninitialized event"); } - trace!( - "returning arg {:x}", - unwrap!(EVENTS[((*event).dummy - 1) as usize].as_mut()).ev_arg_ptr - ); + let arg_ptr = unwrap!(EVENTS[((*event).dummy - 1) as usize].as_mut()).ev_arg_ptr; - unwrap!(EVENTS[((*event).dummy - 1) as usize].as_mut()).ev_arg_ptr as *const c_void + trace!("returning arg {:x}", arg_ptr as usize); + + arg_ptr } unsafe extern "C" fn ble_npl_event_is_queued(event: *const ble_npl_event) -> bool { @@ -803,16 +797,16 @@ unsafe extern "C" fn ble_npl_event_init( ) { trace!("ble_npl_event_init {:?} {:?} {:?}", event, func, arg); - let event = event as *mut ble_npl_event; - if (*event).dummy == 0 { let idx = unwrap!(EVENTS.iter().position(|item| item.is_none())); EVENTS[idx] = Some(Event { - event_id: event as u32, - event_fn_ptr: func as u32, - ev_arg_ptr: arg as u32, + event, + event_fn_ptr: func, + ev_arg_ptr: arg, queued: false, }); + + let event = event.cast_mut(); (*event).dummy = (idx + 1) as i32; } } @@ -835,7 +829,7 @@ unsafe extern "C" fn ble_npl_event_run(event: *const ble_npl_event) { panic!("Trying to run an uninitialized event"); } else { let ev = unwrap!(EVENTS[((*event).dummy - 1) as usize].as_mut()); - trace!("info {:x} with arg {:x}", ev.event_fn_ptr, event as u32); + trace!("info {:?} with arg {:x}", ev.event_fn_ptr, event as u32); let func: unsafe extern "C" fn(u32) = core::mem::transmute(ev.event_fn_ptr); func(event as u32); } @@ -891,10 +885,10 @@ unsafe extern "C" fn ble_npl_eventq_get( if let Some(event_idx) = dequeued { let evt = unwrap!(EVENTS[event_idx - 1].as_mut()); - if evt.queued == true { - trace!("got {:x}", evt.event_id); + if evt.queued { + trace!("got {:x}", evt.event as usize); evt.queued = false; - return evt.event_id as *const ble_npl_event; + return evt.event; } } @@ -923,26 +917,31 @@ unsafe extern "C" fn ble_npl_callout_init( args ); - let callout = callout as *mut ble_npl_callout; - if (*callout).dummy == 0 { + let callout = callout.cast_mut(); let idx = unwrap!(CALLOUTS.iter().position(|item| item.is_none())); - let timer = &CALLOUT_TIMERS[idx]; + let new_callout = CALLOUTS[idx].insert(Callout { + _callout: callout, + eventq, + timer_handle: ets_timer { + next: core::ptr::null_mut(), + expire: 0, + period: 0, + func: None, + priv_: core::ptr::null_mut(), + }, + events: ble_npl_event { dummy: 0 }, + }); + + ble_npl_event_init(addr_of_mut!(new_callout.events), func, args); + crate::compat::timer_compat::compat_timer_setfn( - core::mem::transmute(timer), - callout_timer_callback_wrapper as *mut c_void, + addr_of_mut!(new_callout.timer_handle), + callout_timer_callback_wrapper, idx as *mut c_void, ); - ble_npl_event_init(core::mem::transmute(&CALLOUT_EVENTS), func, args); - - CALLOUTS[idx] = Some(Callout { - _callout_id: callout as u32, - eventq_id: eventq as u32, - event_id: core::mem::transmute(&CALLOUT_EVENTS), - timer_handle: timer as *const _ as u32, - }); (*callout).dummy = (idx + 1) as i32; } @@ -953,13 +952,10 @@ unsafe extern "C" fn callout_timer_callback_wrapper(arg: *mut c_void) { info!("callout_timer_callback_wrapper {:?}", arg); let co = unwrap!(CALLOUTS[arg as usize].as_mut()); - if co.eventq_id == 0 { - ble_npl_eventq_put( - co.event_id as *const ble_npl_eventq, - co.event_id as *const ble_npl_event, - ); + if co.eventq.is_null() { + ble_npl_eventq_put(addr_of!(co.events).cast(), addr_of!(co.events)); } else { - ble_npl_event_run(co.event_id as *const ble_npl_event); + ble_npl_event_run(addr_of!(co.events)); } }