1 //! This trait represents the lower-level operations
2 //! made available to the connection manager. In particular,
3 //! we can add devices to either the "direct" or "background"
4 //! connect list, which are in turn mapped to an appropriate choice
5 //! of scan parameters / the filter accept list.
6 //!
7 //! Note that the ACL manager is unaware of address resolution,
8 //! so this must be handled by the connection manager. Conversely, the connection
9 //! manager does not need to consider the HCI state machine, and can send requests
10 //! at any time.
11 //!
12 //! In addition to the supplied API, when a connection completes to a peer device,
13 //! it is removed from the "direct" connect list (based on exact address match).
14 
15 use std::fmt::Debug;
16 
17 use crate::core::address::AddressWithType;
18 
19 use super::LeConnection;
20 
21 /// An HCI Error Code from the controller
22 #[derive(Copy, Clone, Eq, PartialEq, Debug)]
23 pub struct ErrorCode(pub u8);
24 
25 impl ErrorCode {
26     /// Operation completed successfully
27     pub const SUCCESS: Self = ErrorCode(0);
28 }
29 
30 /// The LeAclManager before callbacks are registered
31 pub trait InactiveLeAclManager {
32     /// The type implementing LeAclManager once callbacks are registered
33     type ActiveManager: LeAclManager + 'static;
34 
35     /// Register callbacks for connection events, and produuce an ActiveManager
register_callbacks( self, callbacks: impl LeAclManagerConnectionCallbacks + 'static, ) -> Self::ActiveManager36     fn register_callbacks(
37         self,
38         callbacks: impl LeAclManagerConnectionCallbacks + 'static,
39     ) -> Self::ActiveManager;
40 }
41 
42 /// The operations provided by GD AclManager to the connection manager
43 pub trait LeAclManager: Debug {
44     /// Adds an address to the direct connect list, if not already connected.
45     /// WARNING: the connection timeout is set the FIRST time the address is added, and is
46     /// NOT RESET! TODO(aryarahul): remove connection timeout from le_impl since it belongs here instead
47     /// Precondition: Must NOT be currently connected to this adddress (if connected due to race, is a no-op)
add_to_direct_list(&self, address: AddressWithType)48     fn add_to_direct_list(&self, address: AddressWithType); // CreateLeConnection(is_direct=true)
49     /// Adds an address to the background connect list
add_to_background_list(&self, address: AddressWithType)50     fn add_to_background_list(&self, address: AddressWithType); // CreateLeConnection(is_direct=false)
51     /// Removes address from both the direct + background connect lists
52     /// Due to races, it is possible to call this, and THEN get a connection complete with us as central
remove_from_all_lists(&self, address: AddressWithType)53     fn remove_from_all_lists(&self, address: AddressWithType); // CancelLeConnect
54 }
55 
56 /// The callbacks invoked by the LeAclManager in response to events from the controller
57 pub trait LeAclManagerConnectionCallbacks {
58     /// Invoked when an LE connection to a given address completes
on_le_connect(&self, address: AddressWithType, result: Result<LeConnection, ErrorCode>)59     fn on_le_connect(&self, address: AddressWithType, result: Result<LeConnection, ErrorCode>);
60     /// Invoked when a peer device disconnects from us. The address must match the address
61     /// supplied on the initial connection.
on_disconnect(&self, address: AddressWithType)62     fn on_disconnect(&self, address: AddressWithType);
63 }
64