diff --git a/host/src/connection_manager.rs b/host/src/connection_manager.rs index 347e272..c3f6086 100644 --- a/host/src/connection_manager.rs +++ b/host/src/connection_manager.rs @@ -139,17 +139,14 @@ impl<'d> ConnectionManager<'d> { None } - pub(crate) fn is_handle_disconnected(&self, h: ConnHandle) -> bool { + pub(crate) fn is_handle_connected(&self, h: ConnHandle) -> bool { let mut state = self.state.borrow_mut(); for storage in state.connections.iter_mut() { match (storage.handle, &storage.state) { - (Some(handle), ConnectionState::DisconnectRequest(_)) if handle == h => { - return true; - } - (Some(handle), ConnectionState::Disconnecting(_)) if handle == h => { + (Some(handle), ConnectionState::Connected) if handle == h => { return true; } - (Some(handle), ConnectionState::Disconnected) if handle == h => { + (Some(handle), ConnectionState::Connecting) if handle == h => { return true; } _ => {} @@ -790,4 +787,31 @@ mod tests { assert!(mgr.poll_disconnecting(None).is_pending()); } + + #[test] + fn nonexisting_handle_is_disconnected() { + let mut storage = [ConnectionStorage::DISCONNECTED; 3]; + let mgr = ConnectionManager::new(&mut storage[..]); + + assert!(!mgr.is_handle_connected(ConnHandle::new(5))); + + unwrap!(mgr.connect( + ConnHandle::new(3), + AddrKind::RANDOM, + BdAddr::new(ADDR_1), + LeConnRole::Peripheral + )); + + assert!(mgr.is_handle_connected(ConnHandle::new(3))); + + let Poll::Ready(handle) = mgr.poll_accept(LeConnRole::Peripheral, &[], None) else { + panic!("expected connection to be accepted"); + }; + + assert!(mgr.is_handle_connected(ConnHandle::new(3))); + + handle.disconnect(); + + assert!(!mgr.is_handle_connected(ConnHandle::new(3))); + } } diff --git a/host/src/host.rs b/host/src/host.rs index 427777d..43eca22 100644 --- a/host/src/host.rs +++ b/host/src/host.rs @@ -823,7 +823,7 @@ where } fn handle_acl(&self, acl: AclPacket<'_>) -> Result<(), Error> { - if self.connections.is_handle_disconnected(acl.handle()) { + if !self.connections.is_handle_connected(acl.handle()) { return Err(Error::Disconnected); } let (header, mut packet) = match acl.boundary_flag() {