Skip to content

Commit

Permalink
Get rid of dyn trait for connection manager
Browse files Browse the repository at this point in the history
The dyn trait is no longer needed after the central/peripheral split
refactoring.
  • Loading branch information
lulf committed Nov 15, 2024
1 parent d96ffb4 commit 32278e9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 59 deletions.
2 changes: 1 addition & 1 deletion host/src/central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<'d, C: Controller> Central<'d, C> {
}

/// Attempt to create a connection with the provided config.
pub async fn connect(&mut self, config: &ConnectConfig<'_>) -> Result<Connection<'_>, BleHostError<C::Error>>
pub async fn connect(&mut self, config: &ConnectConfig<'_>) -> Result<Connection<'d>, BleHostError<C::Error>>
where
C: ControllerCmdSync<LeClearFilterAcceptList>
+ ControllerCmdSync<LeAddDeviceToFilterAcceptList>
Expand Down
8 changes: 4 additions & 4 deletions host/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bt_hci::controller::{ControllerCmdAsync, ControllerCmdSync};
use bt_hci::param::{BdAddr, ConnHandle, DisconnectReason, LeConnRole};
use embassy_time::Duration;

use crate::connection_manager::DynamicConnectionManager;
use crate::connection_manager::ConnectionManager;
use crate::scan::ScanConfig;
use crate::{BleHostError, Stack};

Expand Down Expand Up @@ -48,7 +48,7 @@ impl Default for ConnectParams {
/// When the last reference to a connection is dropped, the connection is automatically disconnected.
pub struct Connection<'d> {
index: u8,
manager: &'d dyn DynamicConnectionManager,
manager: &'d ConnectionManager<'d>,
}

impl<'d> Clone for Connection<'d> {
Expand All @@ -65,7 +65,7 @@ impl<'d> Drop for Connection<'d> {
}

impl<'d> Connection<'d> {
pub(crate) fn new(index: u8, manager: &'d dyn DynamicConnectionManager) -> Self {
pub(crate) fn new(index: u8, manager: &'d ConnectionManager<'d>) -> Self {
Self { index, manager }
}

Expand Down Expand Up @@ -96,7 +96,7 @@ impl<'d> Connection<'d> {
/// Request connection to be disconnected.
pub fn disconnect(&self) {
self.manager
.disconnect(self.index, DisconnectReason::RemoteUserTerminatedConn);
.request_disconnect(self.index, DisconnectReason::RemoteUserTerminatedConn);
}

/// The RSSI value for this connection.
Expand Down
58 changes: 7 additions & 51 deletions host/src/connection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl<'d> ConnectionManager<'d> {
Poll::Pending
}

pub(crate) fn get_connected_handle(&self, h: ConnHandle) -> Option<Connection<'_>> {
pub(crate) fn get_connected_handle(&'d self, h: ConnHandle) -> Option<Connection<'d>> {
let mut state = self.state.borrow_mut();
for (index, storage) in state.connections.iter().enumerate() {
match (storage.handle, &storage.state) {
Expand Down Expand Up @@ -222,11 +222,11 @@ impl<'d> ConnectionManager<'d> {
}

pub(crate) fn poll_accept(
&self,
&'d self,
role: LeConnRole,
peers: &[(AddrKind, &BdAddr)],
cx: Option<&mut Context<'_>>,
) -> Poll<Connection<'_>> {
) -> Poll<Connection<'d>> {
let mut state = self.state.borrow_mut();
if let Some(cx) = cx {
match role {
Expand Down Expand Up @@ -305,7 +305,7 @@ impl<'d> ConnectionManager<'d> {
});
}

pub(crate) async fn accept(&self, role: LeConnRole, peers: &[(AddrKind, &BdAddr)]) -> Connection<'_> {
pub(crate) async fn accept(&'d self, role: LeConnRole, peers: &[(AddrKind, &BdAddr)]) -> Connection<'d> {
poll_fn(|cx| self.poll_accept(role, peers, Some(cx))).await
}

Expand Down Expand Up @@ -370,49 +370,8 @@ impl<'d> ConnectionManager<'d> {
trace!("[link][pool_request_to_send] connection {:?} not found", handle);
Poll::Ready(Err(Error::NotFound))
}
}

pub(crate) trait DynamicConnectionManager {
fn role(&self, index: u8) -> LeConnRole;
fn is_connected(&self, index: u8) -> bool;
fn handle(&self, index: u8) -> ConnHandle;
fn peer_address(&self, index: u8) -> BdAddr;
fn set_att_mtu(&self, index: u8, mtu: u16);
fn inc_ref(&self, index: u8);
fn dec_ref(&self, index: u8);
fn disconnect(&self, index: u8, reason: DisconnectReason);
fn get_att_mtu(&self, conn: ConnHandle) -> u16;
fn exchange_att_mtu(&self, conn: ConnHandle, mtu: u16) -> u16;
fn get_connected_handle(&self, h: ConnHandle) -> Option<Connection<'_>>;
}

impl<'d> DynamicConnectionManager for ConnectionManager<'d> {
fn role(&self, index: u8) -> LeConnRole {
ConnectionManager::role(self, index)
}
fn handle(&self, index: u8) -> ConnHandle {
ConnectionManager::handle(self, index)
}
fn is_connected(&self, index: u8) -> bool {
ConnectionManager::is_connected(self, index)
}
fn peer_address(&self, index: u8) -> BdAddr {
ConnectionManager::peer_address(self, index)
}
fn inc_ref(&self, index: u8) {
ConnectionManager::inc_ref(self, index)
}
fn dec_ref(&self, index: u8) {
ConnectionManager::dec_ref(self, index)
}
fn set_att_mtu(&self, index: u8, mtu: u16) {
ConnectionManager::set_att_mtu(self, index, mtu);
}
fn disconnect(&self, index: u8, reason: DisconnectReason) {
ConnectionManager::request_disconnect(self, index, reason)
}

fn get_att_mtu(&self, conn: ConnHandle) -> u16 {
pub(crate) fn get_att_mtu(&self, conn: ConnHandle) -> u16 {
let mut state = self.state.borrow_mut();
for storage in state.connections.iter_mut() {
match storage.state {
Expand All @@ -424,7 +383,8 @@ impl<'d> DynamicConnectionManager for ConnectionManager<'d> {
}
state.default_att_mtu
}
fn exchange_att_mtu(&self, conn: ConnHandle, mtu: u16) -> u16 {

pub(crate) fn exchange_att_mtu(&self, conn: ConnHandle, mtu: u16) -> u16 {
let mut state = self.state.borrow_mut();
for storage in state.connections.iter_mut() {
match storage.state {
Expand All @@ -437,10 +397,6 @@ impl<'d> DynamicConnectionManager for ConnectionManager<'d> {
}
mtu
}

fn get_connected_handle(&self, h: ConnHandle) -> Option<Connection<'_>> {
ConnectionManager::get_connected_handle(self, h)
}
}

pub struct DisconnectRequest<'a, 'd> {
Expand Down
4 changes: 2 additions & 2 deletions host/src/gatt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::attribute::{
};
use crate::attribute_server::AttributeServer;
use crate::connection::Connection;
use crate::connection_manager::DynamicConnectionManager;
use crate::connection_manager::ConnectionManager;
use crate::cursor::{ReadCursor, WriteCursor};
use crate::pdu::Pdu;
use crate::types::gatt_traits::GattValue;
Expand All @@ -30,7 +30,7 @@ pub struct GattServer<'reference, 'values, C: Controller, M: RawMutex, const MAX
server: AttributeServer<'values, M, MAX>,
tx: DynamicSender<'reference, (ConnHandle, Pdu<'reference>)>,
rx: DynamicReceiver<'reference, (ConnHandle, Pdu<'reference>)>,
connections: &'reference dyn DynamicConnectionManager,
connections: &'reference ConnectionManager<'reference>,
}

impl<'reference, 'values, C: Controller, M: RawMutex, const MAX: usize, const L2CAP_MTU: usize>
Expand Down
2 changes: 1 addition & 1 deletion host/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use futures::pin_mut;

use crate::channel_manager::{ChannelManager, ChannelStorage, PacketChannel};
use crate::command::CommandState;
use crate::connection_manager::{ConnectionManager, ConnectionStorage, DynamicConnectionManager, PacketGrant};
use crate::connection_manager::{ConnectionManager, ConnectionStorage, PacketGrant};
use crate::cursor::WriteCursor;
use crate::l2cap::sar::{PacketReassembly, SarType};
use crate::packet_pool::{AllocId, GlobalPacketPool};
Expand Down

0 comments on commit 32278e9

Please sign in to comment.