summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralexmoon <alex.r.moon@gmail.com>2022-07-11 15:05:36 -0400
committerGitHub <noreply@github.com>2022-07-11 15:05:36 -0400
commit96dbdc838ac2d2a0997eebeda77cead2154d5d22 (patch)
tree2725a1aecd68b205dd94af21c34b133da481dcdc
parent92139b7c647b6ca2242b0b304b6685e0b2b0dba2 (diff)
parent61d50a279f38987bbc125082d1775e0f348f2e1f (diff)
downloadnrf-softdevice-96dbdc838ac2d2a0997eebeda77cead2154d5d22.zip
Merge pull request #115 from alexmoon/connection-iter
Add a Connection iterator
-rw-r--r--nrf-softdevice/src/ble/connection.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/nrf-softdevice/src/ble/connection.rs b/nrf-softdevice/src/ble/connection.rs
index 1ed33d5..79e0f8f 100644
--- a/nrf-softdevice/src/ble/connection.rs
+++ b/nrf-softdevice/src/ble/connection.rs
@@ -1,5 +1,6 @@
use core::cell::Cell;
use core::cell::UnsafeCell;
+use core::iter::FusedIterator;
use raw::ble_gap_conn_params_t;
@@ -300,8 +301,46 @@ impl Connection {
pub(crate) fn with_state<T>(&self, f: impl FnOnce(&mut ConnectionState) -> T) -> T {
with_state(self.index, f)
}
+
+ pub fn iter() -> ConnectionIter {
+ ConnectionIter(0)
+ }
}
+pub struct ConnectionIter(u8);
+
+impl Iterator for ConnectionIter {
+ type Item = Connection;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ let n = usize::from(self.0);
+ if n < CONNS_MAX {
+ unsafe {
+ for (i, s) in STATES[n..].iter().enumerate() {
+ let state = &mut *s.get();
+ if state.conn_handle.is_some() {
+ let index = (n + i) as u8;
+ state.refcount = unwrap!(
+ state.refcount.checked_add(1),
+ "Too many references to same connection"
+ );
+ self.0 = index + 1;
+ return Some(Connection { index });
+ }
+ }
+ }
+ self.0 = CONNS_MAX as u8;
+ }
+ None
+ }
+
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ (0, Some(CONNS_MAX - usize::from(self.0)))
+ }
+}
+
+impl FusedIterator for ConnectionIter {}
+
// ConnectionStates by index.
const DUMMY_STATE: UnsafeCell<ConnectionState> = UnsafeCell::new(ConnectionState::dummy());
static mut STATES: [UnsafeCell<ConnectionState>; CONNS_MAX] = [DUMMY_STATE; CONNS_MAX];