sl4f_lib/bluetooth/
a2dp_facade.rs1use crate::common_utils::common::macros::{fx_err_and_bail, with_line};
6use anyhow::Error;
7use fidl_fuchsia_bluetooth_a2dp::{AudioModeMarker, AudioModeProxy, Role};
8use fuchsia_component::client;
9use fuchsia_sync::Mutex;
10use log::info;
11use std::ops::DerefMut;
12
13#[derive(Debug)]
14pub struct A2dpFacade {
15 audio_mode_proxy: Mutex<Option<AudioModeProxy>>,
16}
17
18impl A2dpFacade {
20 pub fn new() -> A2dpFacade {
21 A2dpFacade { audio_mode_proxy: Mutex::new(None) }
22 }
23
24 pub async fn init_audio_mode_proxy(&self) -> Result<(), Error> {
26 let tag = "A2dpFacade::init_audio_mode_proxy";
27 let mut proxy_locked = self.audio_mode_proxy.lock();
28 if proxy_locked.is_some() {
29 info!(
30 tag = &with_line!(tag);
31 "Current A2DP AudioMode proxy: {0:?}", self.audio_mode_proxy
32 );
33 return Ok(());
34 }
35 match client::connect_to_protocol::<AudioModeMarker>() {
36 Ok(proxy) => {
37 *proxy_locked = Some(proxy);
38 Ok(())
39 }
40 Err(err) => {
41 fx_err_and_bail!(
42 &with_line!(tag),
43 format_err!("Failed to create A2DP AudioMode proxy: {err}")
44 );
45 }
46 }
47 }
48
49 pub async fn set_role(&self, role: Role) -> Result<(), Error> {
54 let tag = "A2dpFacade::set_role";
55 let mut proxy_locked = self.audio_mode_proxy.lock();
56 match proxy_locked.deref_mut() {
57 Some(proxy) => {
58 proxy.set_role(role).await?;
59 info!("new A2DP audio mode set: {:?}", role);
60 Ok(())
61 }
62 None => {
63 fx_err_and_bail!(&with_line!(tag), "no A2DP Audio Mode Proxy detected");
64 }
65 }
66 }
67}