Loading system/gd/rust/topshim/facade/src/hfp_service.rs +29 −19 Original line number Diff line number Diff line //! HFP service facade use bt_topshim::btif::{BluetoothInterface, RawAddress}; use bt_topshim::btif::{BluetoothInterface, RawAddress, ToggleableProfile}; use bt_topshim::profiles::hfp::{Hfp, HfpCallbacks, HfpCallbacksDispatcher}; use bt_topshim_facade_protobuf::empty::Empty; use bt_topshim_facade_protobuf::facade::{ Loading @@ -22,15 +22,19 @@ fn get_hfp_dispatcher( ) -> HfpCallbacksDispatcher { HfpCallbacksDispatcher { dispatch: Box::new(move |cb: HfpCallbacks| { println!("Hfp Callback found {:?}", cb); if let HfpCallbacks::ConnectionState(state, address) = &cb { println!("Hfp Connection state changed to {:?} for address {:?}", state, address); } let guard_tx = tx.lock().unwrap(); if let Some(event_tx) = guard_tx.as_ref() { let txclone = event_tx.clone(); tokio::spawn(async move { if txclone.try_send(cb.clone()).is_err() { println!("Cannot send event {:?}", cb); } /*tokio::spawn(async move { let _ = txclone.send(cb).await; }); });*/ } }), } Loading @@ -52,6 +56,7 @@ impl HfpServiceImpl { let btif_hfp = Arc::new(Mutex::new(Hfp::new(&btif_intf.lock().unwrap()))); let event_tx = Arc::new(Mutex::new(None)); btif_hfp.lock().unwrap().initialize(get_hfp_dispatcher(btif_hfp.clone(), event_tx.clone())); btif_hfp.lock().unwrap().enable(); create_hfp_service(Self { rt, btif_hfp, event_tx }) } } Loading @@ -60,14 +65,15 @@ impl HfpService for HfpServiceImpl { fn start_slc(&mut self, ctx: RpcContext<'_>, req: StartSlcRequest, sink: UnarySink<Empty>) { let hfp = self.btif_hfp.clone(); ctx.spawn(async move { let bt_addr = &req.connection.unwrap().cookie; if let Some(addr) = RawAddress::from_bytes(bt_addr) { let addr_bytes = &req.connection.unwrap().cookie; let bt_addr = from_utf8(addr_bytes).unwrap(); if let Some(addr) = RawAddress::from_string(bt_addr) { hfp.lock().unwrap().connect(addr); sink.success(Empty::default()).await.unwrap(); } else { sink.fail(RpcStatus::with_message( RpcStatusCode::INVALID_ARGUMENT, format!("Invalid Request Address: {}", from_utf8(bt_addr).unwrap()), format!("Invalid Request Address: {}", bt_addr), )) .await .unwrap(); Loading @@ -78,14 +84,15 @@ impl HfpService for HfpServiceImpl { fn stop_slc(&mut self, ctx: RpcContext<'_>, req: StopSlcRequest, sink: UnarySink<Empty>) { let hfp = self.btif_hfp.clone(); ctx.spawn(async move { let bt_addr = &req.connection.unwrap().cookie; if let Some(addr) = RawAddress::from_bytes(bt_addr) { let addr_bytes = &req.connection.unwrap().cookie; let bt_addr = from_utf8(addr_bytes).unwrap(); if let Some(addr) = RawAddress::from_string(bt_addr) { hfp.lock().unwrap().disconnect(addr); sink.success(Empty::default()).await.unwrap(); } else { sink.fail(RpcStatus::with_message( RpcStatusCode::INVALID_ARGUMENT, format!("Invalid Request Address: {}", from_utf8(bt_addr).unwrap()), format!("Invalid Request Address: {}", bt_addr), )) .await .unwrap(); Loading @@ -101,15 +108,16 @@ impl HfpService for HfpServiceImpl { ) { let hfp = self.btif_hfp.clone(); ctx.spawn(async move { let bt_addr = &req.connection.unwrap().cookie; if let Some(addr) = RawAddress::from_bytes(bt_addr) { let addr_bytes = &req.connection.unwrap().cookie; let bt_addr = from_utf8(addr_bytes).unwrap(); if let Some(addr) = RawAddress::from_string(bt_addr) { hfp.lock().unwrap().connect_audio(addr, req.is_sco_offload_enabled, req.force_cvsd); hfp.lock().unwrap().set_active_device(addr); sink.success(Empty::default()).await.unwrap(); } else { sink.fail(RpcStatus::with_message( RpcStatusCode::INVALID_ARGUMENT, format!("Invalid Request Address: {}", from_utf8(bt_addr).unwrap()), format!("Invalid Request Address: {}", bt_addr), )) .await .unwrap(); Loading @@ -125,14 +133,15 @@ impl HfpService for HfpServiceImpl { ) { let hfp = self.btif_hfp.clone(); ctx.spawn(async move { let bt_addr = &req.connection.unwrap().cookie; if let Some(addr) = RawAddress::from_bytes(bt_addr) { let addr_bytes = &req.connection.unwrap().cookie; let bt_addr = from_utf8(addr_bytes).unwrap(); if let Some(addr) = RawAddress::from_string(bt_addr) { hfp.lock().unwrap().disconnect_audio(addr); sink.success(Empty::default()).await.unwrap(); } else { sink.fail(RpcStatus::with_message( RpcStatusCode::INVALID_ARGUMENT, format!("Invalid Request Address: {}", from_utf8(bt_addr).unwrap()), format!("Invalid Request Address: {}", bt_addr), )) .await .unwrap(); Loading @@ -143,8 +152,9 @@ impl HfpService for HfpServiceImpl { fn set_volume(&mut self, ctx: RpcContext<'_>, req: SetVolumeRequest, sink: UnarySink<Empty>) { let hfp = self.btif_hfp.clone(); ctx.spawn(async move { let bt_addr = &req.connection.unwrap().cookie; if let Some(addr) = RawAddress::from_bytes(bt_addr) { let addr_bytes = &req.connection.unwrap().cookie; let bt_addr = from_utf8(addr_bytes).unwrap(); if let Some(addr) = RawAddress::from_string(bt_addr) { // TODO(aritrasen): Consider using TryFrom and cap the maximum volume here // since `as` silently deals with data overflow, which might not be preferred. hfp.lock().unwrap().set_volume(req.volume as i8, addr); Loading @@ -152,7 +162,7 @@ impl HfpService for HfpServiceImpl { } else { sink.fail(RpcStatus::with_message( RpcStatusCode::INVALID_ARGUMENT, format!("Invalid Request Address: {}", from_utf8(bt_addr).unwrap()), format!("Invalid Request Address: {}", bt_addr), )) .await .unwrap(); Loading Loading @@ -189,7 +199,7 @@ impl HfpService for HfpServiceImpl { if let HfpCallbacks::ConnectionState(state, address) = event { let mut rsp = FetchEventsResponse::new(); rsp.event_type = EventType::HFP_CONNECTION_STATE; rsp.data = format!("{:?}, {:?}", state, address); rsp.data = format!("{:?}, {}", state, address.to_string()); sink.send((rsp, WriteFlags::default())).await.unwrap(); } } Loading Loading
system/gd/rust/topshim/facade/src/hfp_service.rs +29 −19 Original line number Diff line number Diff line //! HFP service facade use bt_topshim::btif::{BluetoothInterface, RawAddress}; use bt_topshim::btif::{BluetoothInterface, RawAddress, ToggleableProfile}; use bt_topshim::profiles::hfp::{Hfp, HfpCallbacks, HfpCallbacksDispatcher}; use bt_topshim_facade_protobuf::empty::Empty; use bt_topshim_facade_protobuf::facade::{ Loading @@ -22,15 +22,19 @@ fn get_hfp_dispatcher( ) -> HfpCallbacksDispatcher { HfpCallbacksDispatcher { dispatch: Box::new(move |cb: HfpCallbacks| { println!("Hfp Callback found {:?}", cb); if let HfpCallbacks::ConnectionState(state, address) = &cb { println!("Hfp Connection state changed to {:?} for address {:?}", state, address); } let guard_tx = tx.lock().unwrap(); if let Some(event_tx) = guard_tx.as_ref() { let txclone = event_tx.clone(); tokio::spawn(async move { if txclone.try_send(cb.clone()).is_err() { println!("Cannot send event {:?}", cb); } /*tokio::spawn(async move { let _ = txclone.send(cb).await; }); });*/ } }), } Loading @@ -52,6 +56,7 @@ impl HfpServiceImpl { let btif_hfp = Arc::new(Mutex::new(Hfp::new(&btif_intf.lock().unwrap()))); let event_tx = Arc::new(Mutex::new(None)); btif_hfp.lock().unwrap().initialize(get_hfp_dispatcher(btif_hfp.clone(), event_tx.clone())); btif_hfp.lock().unwrap().enable(); create_hfp_service(Self { rt, btif_hfp, event_tx }) } } Loading @@ -60,14 +65,15 @@ impl HfpService for HfpServiceImpl { fn start_slc(&mut self, ctx: RpcContext<'_>, req: StartSlcRequest, sink: UnarySink<Empty>) { let hfp = self.btif_hfp.clone(); ctx.spawn(async move { let bt_addr = &req.connection.unwrap().cookie; if let Some(addr) = RawAddress::from_bytes(bt_addr) { let addr_bytes = &req.connection.unwrap().cookie; let bt_addr = from_utf8(addr_bytes).unwrap(); if let Some(addr) = RawAddress::from_string(bt_addr) { hfp.lock().unwrap().connect(addr); sink.success(Empty::default()).await.unwrap(); } else { sink.fail(RpcStatus::with_message( RpcStatusCode::INVALID_ARGUMENT, format!("Invalid Request Address: {}", from_utf8(bt_addr).unwrap()), format!("Invalid Request Address: {}", bt_addr), )) .await .unwrap(); Loading @@ -78,14 +84,15 @@ impl HfpService for HfpServiceImpl { fn stop_slc(&mut self, ctx: RpcContext<'_>, req: StopSlcRequest, sink: UnarySink<Empty>) { let hfp = self.btif_hfp.clone(); ctx.spawn(async move { let bt_addr = &req.connection.unwrap().cookie; if let Some(addr) = RawAddress::from_bytes(bt_addr) { let addr_bytes = &req.connection.unwrap().cookie; let bt_addr = from_utf8(addr_bytes).unwrap(); if let Some(addr) = RawAddress::from_string(bt_addr) { hfp.lock().unwrap().disconnect(addr); sink.success(Empty::default()).await.unwrap(); } else { sink.fail(RpcStatus::with_message( RpcStatusCode::INVALID_ARGUMENT, format!("Invalid Request Address: {}", from_utf8(bt_addr).unwrap()), format!("Invalid Request Address: {}", bt_addr), )) .await .unwrap(); Loading @@ -101,15 +108,16 @@ impl HfpService for HfpServiceImpl { ) { let hfp = self.btif_hfp.clone(); ctx.spawn(async move { let bt_addr = &req.connection.unwrap().cookie; if let Some(addr) = RawAddress::from_bytes(bt_addr) { let addr_bytes = &req.connection.unwrap().cookie; let bt_addr = from_utf8(addr_bytes).unwrap(); if let Some(addr) = RawAddress::from_string(bt_addr) { hfp.lock().unwrap().connect_audio(addr, req.is_sco_offload_enabled, req.force_cvsd); hfp.lock().unwrap().set_active_device(addr); sink.success(Empty::default()).await.unwrap(); } else { sink.fail(RpcStatus::with_message( RpcStatusCode::INVALID_ARGUMENT, format!("Invalid Request Address: {}", from_utf8(bt_addr).unwrap()), format!("Invalid Request Address: {}", bt_addr), )) .await .unwrap(); Loading @@ -125,14 +133,15 @@ impl HfpService for HfpServiceImpl { ) { let hfp = self.btif_hfp.clone(); ctx.spawn(async move { let bt_addr = &req.connection.unwrap().cookie; if let Some(addr) = RawAddress::from_bytes(bt_addr) { let addr_bytes = &req.connection.unwrap().cookie; let bt_addr = from_utf8(addr_bytes).unwrap(); if let Some(addr) = RawAddress::from_string(bt_addr) { hfp.lock().unwrap().disconnect_audio(addr); sink.success(Empty::default()).await.unwrap(); } else { sink.fail(RpcStatus::with_message( RpcStatusCode::INVALID_ARGUMENT, format!("Invalid Request Address: {}", from_utf8(bt_addr).unwrap()), format!("Invalid Request Address: {}", bt_addr), )) .await .unwrap(); Loading @@ -143,8 +152,9 @@ impl HfpService for HfpServiceImpl { fn set_volume(&mut self, ctx: RpcContext<'_>, req: SetVolumeRequest, sink: UnarySink<Empty>) { let hfp = self.btif_hfp.clone(); ctx.spawn(async move { let bt_addr = &req.connection.unwrap().cookie; if let Some(addr) = RawAddress::from_bytes(bt_addr) { let addr_bytes = &req.connection.unwrap().cookie; let bt_addr = from_utf8(addr_bytes).unwrap(); if let Some(addr) = RawAddress::from_string(bt_addr) { // TODO(aritrasen): Consider using TryFrom and cap the maximum volume here // since `as` silently deals with data overflow, which might not be preferred. hfp.lock().unwrap().set_volume(req.volume as i8, addr); Loading @@ -152,7 +162,7 @@ impl HfpService for HfpServiceImpl { } else { sink.fail(RpcStatus::with_message( RpcStatusCode::INVALID_ARGUMENT, format!("Invalid Request Address: {}", from_utf8(bt_addr).unwrap()), format!("Invalid Request Address: {}", bt_addr), )) .await .unwrap(); Loading Loading @@ -189,7 +199,7 @@ impl HfpService for HfpServiceImpl { if let HfpCallbacks::ConnectionState(state, address) = event { let mut rsp = FetchEventsResponse::new(); rsp.event_type = EventType::HFP_CONNECTION_STATE; rsp.data = format!("{:?}, {:?}", state, address); rsp.data = format!("{:?}, {}", state, address.to_string()); sink.send((rsp, WriteFlags::default())).await.unwrap(); } } Loading