Skip to content

Commit

Permalink
Fix and clean up npl
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 27, 2023
1 parent 163776e commit 2f4c51c
Showing 1 changed file with 51 additions and 55 deletions.
106 changes: 51 additions & 55 deletions esp-wifi/src/ble/npl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use core::cell::RefCell;
use core::{
cell::RefCell,
ptr::{addr_of, addr_of_mut},
};

use critical_section::Mutex;

Expand Down Expand Up @@ -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<Callout>; 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,
}

Expand Down Expand Up @@ -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(
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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));
}
}

Expand Down

0 comments on commit 2f4c51c

Please sign in to comment.