summaryrefslogtreecommitdiff
path: root/nrf-softdevice
diff options
context:
space:
mode:
authoralexmoon <alex.r.moon@gmail.com>2022-03-25 19:22:00 -0400
committeralexmoon <alex.r.moon@gmail.com>2022-03-25 19:38:06 -0400
commita9af20b6ce1276addd04f8a7681a84b3d569d3e7 (patch)
tree33504ae83df013ac9f89ad7b2394299abb50cb9f /nrf-softdevice
parent6ee09a134d9366029462963650dcd7e3921d6c1d (diff)
downloadnrf-softdevice-a9af20b6ce1276addd04f8a7681a84b3d569d3e7.zip
Add callback to `Softdevice::run()` to receive `SocEvent`s that are not handled by nrf-softdevice.
Diffstat (limited to 'nrf-softdevice')
-rw-r--r--nrf-softdevice/src/events.rs10
-rw-r--r--nrf-softdevice/src/softdevice.rs12
2 files changed, 15 insertions, 7 deletions
diff --git a/nrf-softdevice/src/events.rs b/nrf-softdevice/src/events.rs
index bf7c5d6..f45538e 100644
--- a/nrf-softdevice/src/events.rs
+++ b/nrf-softdevice/src/events.rs
@@ -15,7 +15,7 @@ static SWI2_WAKER: AtomicWaker = AtomicWaker::new();
#[repr(u32)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, IntoPrimitive, TryFromPrimitive)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
-enum SocEvent {
+pub enum SocEvent {
Hfclkstarted = raw::NRF_SOC_EVTS_NRF_EVT_HFCLKSTARTED,
PowerFailureWarning = raw::NRF_SOC_EVTS_NRF_EVT_POWER_FAILURE_WARNING,
FlashOperationSuccess = raw::NRF_SOC_EVTS_NRF_EVT_FLASH_OPERATION_SUCCESS,
@@ -33,7 +33,7 @@ enum SocEvent {
PowerUsbRemoved = raw::NRF_SOC_EVTS_NRF_EVT_POWER_USB_REMOVED,
}
-fn on_soc_evt(evt: u32) {
+fn on_soc_evt<F: FnMut(SocEvent)>(evt: u32, evt_handler: &mut F) {
let evt = match SocEvent::try_from(evt) {
Ok(evt) => evt,
Err(_) => panic!("Unknown soc evt {:?}", evt),
@@ -43,21 +43,21 @@ fn on_soc_evt(evt: u32) {
match evt {
SocEvent::FlashOperationError => crate::flash::on_flash_error(),
SocEvent::FlashOperationSuccess => crate::flash::on_flash_success(),
- _ => {}
+ _ => evt_handler(evt),
}
}
// TODO actually derive this from the headers + the ATT_MTU
const BLE_EVT_MAX_SIZE: u16 = 128;
-pub(crate) async fn run() -> ! {
+pub(crate) async fn run<F: FnMut(SocEvent)>(mut soc_evt_handler: F) -> ! {
poll_fn(|cx| unsafe {
SWI2_WAKER.register(cx.waker());
let mut evt: u32 = 0;
loop {
match RawError::convert(raw::sd_evt_get(&mut evt as _)) {
- Ok(()) => on_soc_evt(evt),
+ Ok(()) => on_soc_evt(evt, &mut soc_evt_handler),
Err(RawError::NotFound) => break,
Err(err) => panic!("sd_evt_get err {:?}", err),
}
diff --git a/nrf-softdevice/src/softdevice.rs b/nrf-softdevice/src/softdevice.rs
index cbcfe8b..af8a0b4 100644
--- a/nrf-softdevice/src/softdevice.rs
+++ b/nrf-softdevice/src/softdevice.rs
@@ -3,9 +3,9 @@ use core::ptr;
use core::sync::atomic::{AtomicBool, Ordering};
use embassy::util::Forever;
-use crate::pac;
use crate::raw;
use crate::RawError;
+use crate::{pac, SocEvent};
unsafe extern "C" fn fault_handler(id: u32, pc: u32, info: u32) {
match (id, info) {
@@ -320,6 +320,14 @@ impl Softdevice {
/// It must be called in its own async task after enabling the softdevice
/// and before doing any operation. Failure to doing so will cause async operations to never finish.
pub async fn run(&self) -> ! {
- crate::events::run().await
+ self.run_with_callback(|_| {}).await
+ }
+
+ /// Runs the softdevice event handling loop.
+ ///
+ /// It must be called in its own async task after enabling the softdevice
+ /// and before doing any operation. Failure to doing so will cause async operations to never finish.
+ pub async fn run_with_callback<F: FnMut(SocEvent)>(&self, f: F) -> ! {
+ crate::events::run(f).await
}
}