rust/bt-bap-unicast: Make manager and client generic over GattTypes - Update BapUnicastClient and BapUnicastManager to be generic over bt_gatt::GattTypes. - Update add_peer to accept T::Client. - Add bt-gatt with test-utils feature to dev-dependencies in Cargo.toml. - Update unit tests to use FakeClient and FakeTypes. Test: unit tests passed Bug: 433288675 Change-Id: I66742aaaa697f03a37f735bc0e70f1266a6a6964 Reviewed-on: https://bluetooth-review.googlesource.com/c/bluetooth/+/3061
diff --git a/rust/bt-bap-unicast/Cargo.toml b/rust/bt-bap-unicast/Cargo.toml index a89c152..7773f3f 100644 --- a/rust/bt-bap-unicast/Cargo.toml +++ b/rust/bt-bap-unicast/Cargo.toml
@@ -11,3 +11,6 @@ bt-pacs.workspace = true futures.workspace = true thiserror.workspace = true + +[dev-dependencies] +bt-gatt = { workspace = true, features = ["test-utils"] }
diff --git a/rust/bt-bap-unicast/src/client.rs b/rust/bt-bap-unicast/src/client.rs index b4ace67..14bd217 100644 --- a/rust/bt-bap-unicast/src/client.rs +++ b/rust/bt-bap-unicast/src/client.rs
@@ -3,17 +3,19 @@ // found in the LICENSE file. use bt_common::PeerId; +use bt_gatt::GattTypes; /// Represents a connection to a single peer and manages its audio streams. -#[derive(Debug)] -pub struct BapUnicastClient { +pub struct BapUnicastClient<T: GattTypes> { peer_id: PeerId, + #[allow(unused)] + gatt_client: T::Client, // TODO: Add ASCS and PACS clients here. } -impl BapUnicastClient { - pub fn new(peer_id: PeerId) -> Self { - Self { peer_id } +impl<T: GattTypes> BapUnicastClient<T> { + pub fn new(peer_id: PeerId, gatt_client: T::Client) -> Self { + Self { peer_id, gatt_client } } pub fn peer_id(&self) -> PeerId {
diff --git a/rust/bt-bap-unicast/src/manager.rs b/rust/bt-bap-unicast/src/manager.rs index b33b427..6912b1e 100644 --- a/rust/bt-bap-unicast/src/manager.rs +++ b/rust/bt-bap-unicast/src/manager.rs
@@ -5,27 +5,28 @@ use crate::client::BapUnicastClient; use crate::types::Error; use bt_common::PeerId; +use bt_gatt::GattTypes; use std::collections::HashMap; /// Manages multiple BAP Unicast peers. -#[derive(Debug, Default)] -pub struct BapUnicastManager { - peers: HashMap<PeerId, BapUnicastClient>, +pub struct BapUnicastManager<T: GattTypes> { + peers: HashMap<PeerId, BapUnicastClient<T>>, } -impl BapUnicastManager { +impl<T: GattTypes> BapUnicastManager<T> { pub fn new() -> Self { - Self::default() + Self { peers: HashMap::new() } } /// Add a peer to the manager. - /// In the future, this will trigger discovery. - pub async fn add_peer(&mut self, peer_id: PeerId) -> Result<(), Error> { + /// Triggers the connection and discovery of PACS and ASCS on the remote + /// peer. + pub async fn add_peer(&mut self, peer_id: PeerId, gatt_client: T::Client) -> Result<(), Error> { if self.peers.contains_key(&peer_id) { return Err(Error::PeerAlreadyExists(peer_id)); } - let client = BapUnicastClient::new(peer_id); + let client = BapUnicastClient::new(peer_id, gatt_client); self.peers.insert(peer_id, client); Ok(()) } @@ -43,14 +44,16 @@ mod tests { use super::*; use bt_common::PeerId; + use bt_gatt::test_utils::{FakeClient, FakeTypes}; #[test] fn test_add_remove_peer() { - let mut manager = BapUnicastManager::new(); + let mut manager = BapUnicastManager::<FakeTypes>::new(); let peer_id = PeerId(1); + let client = FakeClient::new(); - assert!(futures::executor::block_on(manager.add_peer(peer_id)).is_ok()); - assert!(futures::executor::block_on(manager.add_peer(peer_id)).is_err()); // Duplicate + assert!(futures::executor::block_on(manager.add_peer(peer_id, client.clone())).is_ok()); + assert!(futures::executor::block_on(manager.add_peer(peer_id, client.clone())).is_err()); // Duplicate assert!(manager.remove_peer(peer_id).is_ok()); assert!(manager.remove_peer(peer_id).is_err()); // Not found