blob: 6912b1e30dc57fbbe4b5c5b5b59ab5affeb9b129 [file]
// Copyright 2026 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
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.
pub struct BapUnicastManager<T: GattTypes> {
peers: HashMap<PeerId, BapUnicastClient<T>>,
}
impl<T: GattTypes> BapUnicastManager<T> {
pub fn new() -> Self {
Self { peers: HashMap::new() }
}
/// Add a peer to the manager.
/// 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, gatt_client);
self.peers.insert(peer_id, client);
Ok(())
}
/// Remove a peer from the manager.
pub fn remove_peer(&mut self, peer_id: PeerId) -> Result<(), Error> {
if self.peers.remove(&peer_id).is_none() {
return Err(Error::PeerNotFound(peer_id));
}
Ok(())
}
}
#[cfg(test)]
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::<FakeTypes>::new();
let peer_id = PeerId(1);
let client = FakeClient::new();
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
}
}