async fn handle_peer_request(
        &mut self,
        id: PeerId,
        request: PeerHandlerRequest,
    ) -> Result<(), Error> {
        fx_log_info!("Received Peer Handler request for {:?}: {:?}", id, request);
        match request {
            PeerHandlerRequest::WatchNetworkInformation { responder, .. } => {
                let mut inner = self.inner.lock().await;
                let current_network = inner.manager.network.clone();
                let peer = inner
                    .peers
                    .get_mut(&id)
                    .ok_or_else(|| format_err!("peer removed: {:?}", id))?;

                if Some(&current_network) == peer.reported_network.as_ref() {
                    peer.network_responder = Some(responder);
                } else {
                    responder.send(current_network.clone())?;
                    peer.reported_network = Some(current_network);
                }
            }
            PeerHandlerRequest::WaitForCall { responder, .. } => {
                let this = self.clone();
                let mut inner = self.inner.lock().await;
                let peer = inner
                    .peers
                    .get_mut(&id)
                    .ok_or_else(|| format_err!("peer removed: {:?}", id))?;
                if peer.call_responder.is_none() {
                    peer.call_responder = Some(responder);
                    this.report_calls(id, inner)?;
                } else {
                    let err = format_err!("double hanging get call on PeerHandler::WaitForCall");
                    fx_log_err!("{}", err);
                    *inner = TestCallManagerInner::default();
                    return Err(err);
                }
            }
            PeerHandlerRequest::InitiateOutgoingCall { action: _, responder: _, .. } => {
                unimplemented!();
            }
            PeerHandlerRequest::QueryOperator { responder, .. } => {
                responder.send(Some(&self.inner.lock().await.manager.operator))?;
            }
            PeerHandlerRequest::SubscriberNumberInformation { responder, .. } => {
                responder.send(
                    &mut self
                        .inner
                        .lock()
                        .await
                        .manager
                        .subscriber_numbers
                        .iter()
                        .map(AsRef::as_ref),
                )?;
            }
            PeerHandlerRequest::SetNrecMode { enabled, responder, .. } => {
                let mut inner = self.inner.lock().await;
                if inner.manager.nrec_support {
                    let peer = inner
                        .peers
                        .get_mut(&id)
                        .ok_or_else(|| format_err!("peer removed: {:?}", id))?;
                    peer.nrec_enabled = enabled;
                    responder.send(&mut Ok(()))?;
                } else {
                    responder.send(&mut Err(zx::Status::NOT_SUPPORTED.into_raw()))?;
                }
            }
            PeerHandlerRequest::ReportHeadsetBatteryLevel { level, .. } => {
                self.inner
                    .lock()
                    .await
                    .peers
                    .get_mut(&id)
                    .ok_or_else(|| format_err!("peer removed: {:?}", id))?
                    .battery_level = level;
            }
            PeerHandlerRequest::GainControl { control, .. } => {
                let this = self.clone();
                let proxy = control.into_proxy()?;
                let proxy_ = proxy.clone();
                let task = fasync::Task::spawn(async move {
                    let proxy_ = proxy.clone();
                    let mut speaker_gain_stream =
                        HangingGetStream::new(Box::new(move || Some(proxy_.watch_speaker_gain())));
                    let proxy_ = proxy.clone();
                    let mut microphone_gain_stream = HangingGetStream::new(Box::new(move || {
                        Some(proxy_.watch_microphone_gain())
                    }));

                    loop {
                        futures::select! {
                            gain = speaker_gain_stream.next() => {
                                let mut inner = this.inner.lock().await;
                                let peer = inner.peers.get_mut(&id);
                                match (peer, gain) {
                                    (Some(peer), Some(Ok(gain))) => peer.speaker_gain = gain,
                                    _ => break,
                                }
                            }
                            gain = microphone_gain_stream.next() => {
                                let mut inner = this.inner.lock().await;
                                let peer = inner.peers.get_mut(&id);
                                match (peer, gain) {
                                    (Some(peer), Some(Ok(gain))) => peer.microphone_gain = gain,
                                    _ => break,
                                }
                            }
                        }
                    }
                    fx_log_info!("Headset gain control channel for peer {:?} closed", id);
                });
                let mut inner = self.inner.lock().await;
                let peer = inner
                    .peers
                    .get_mut(&id)
                    .ok_or_else(|| format_err!("peer removed: {:?}", id))?;
                if let Some(requested) = peer.requested_speaker_gain.take() {
                    proxy_.set_speaker_gain(requested)?;
                }
                if let Some(requested) = peer.requested_microphone_gain.take() {
                    proxy_.set_microphone_gain(requested)?;
                }
                peer.gain_control_watcher = Some(task);
                peer.gain_control = Some(proxy_);
            }
        }
        Ok(())
    }
/// Handle a peer `request`. Most requests are handled by immediately responding with the
/// relevant data which is stored in the facade's state.
///
/// Arguments:
///     `id`: The unique id for the peer that initiated the request.
///     `request`: A request made by a client of the fuchsia.bluetooth.hfp.PeerHandler protocol.
Handle a peer `request`. Most requests are handled by immediately responding with the
relevant data which is stored in the facade's state.

`id`: The unique id for the peer that initiated the request.
`request`: A request made by a client of the fuchsia.bluetooth.hfp.PeerHandler protocol.
Handle a peer ` request ` . Most requests are handled by immediately responding with the relevant data which is stored in the facade ' s state . ` id ` : The unique id for the peer that initiated the request . ` request ` : A request made by a client of the fuchsia . bluetooth . hfp . PeerHandler protocol .
async fn handle_peer_request(
        &mut self,
        id: PeerId,
        request: PeerHandlerRequest,
    ) -> Result<(), Error> {
        fx_log_info!("Received Peer Handler request for {:?}: {:?}", id, request);
        match request {
            PeerHandlerRequest::WatchNetworkInformation { responder, .. } => {
                let mut inner = self.inner.lock().await;
                let current_network = inner.manager.network.clone();
                let peer = inner
                    .peers
                    .get_mut(&id)
                    .ok_or_else(|| format_err!("peer removed: {:?}", id))?;
                if Some(&current_network) == peer.reported_network.as_ref() {
                    peer.network_responder = Some(responder);
                } else {
                    responder.send(current_network.clone())?;
                    peer.reported_network = Some(current_network);
                }
            }
            PeerHandlerRequest::WaitForCall { responder, .. } => {
                let this = self.clone();
                let mut inner = self.inner.lock().await;
                let peer = inner
                    .peers
                    .get_mut(&id)
                    .ok_or_else(|| format_err!("peer removed: {:?}", id))?;
                if peer.call_responder.is_none() {
                    peer.call_responder = Some(responder);
                    this.report_calls(id, inner)?;
                } else {
                    let err = format_err!("double hanging get call on PeerHandler::WaitForCall");
                    fx_log_err!("{}", err);
                    *inner = TestCallManagerInner::default();
                    return Err(err);
                }
            }
            PeerHandlerRequest::InitiateOutgoingCall { action: _, responder: _, .. } => {
                unimplemented!();
            }
            PeerHandlerRequest::QueryOperator { responder, .. } => {
                responder.send(Some(&self.inner.lock().await.manager.operator))?;
            }
            PeerHandlerRequest::SubscriberNumberInformation { responder, .. } => {
                responder.send(
                    &mut self
                        .inner
                        .lock()
                        .await
                        .manager
                        .subscriber_numbers
                        .iter()
                        .map(AsRef::as_ref),
                )?;
            }
            PeerHandlerRequest::SetNrecMode { enabled, responder, .. } => {
                let mut inner = self.inner.lock().await;
                if inner.manager.nrec_support {
                    let peer = inner
                        .peers
                        .get_mut(&id)
                        .ok_or_else(|| format_err!("peer removed: {:?}", id))?;
                    peer.nrec_enabled = enabled;
                    responder.send(&mut Ok(()))?;
                } else {
                    responder.send(&mut Err(zx::Status::NOT_SUPPORTED.into_raw()))?;
                }
            }
            PeerHandlerRequest::ReportHeadsetBatteryLevel { level, .. } => {
                self.inner
                    .lock()
                    .await
                    .peers
                    .get_mut(&id)
                    .ok_or_else(|| format_err!("peer removed: {:?}", id))?
                    .battery_level = level;
            }
            PeerHandlerRequest::GainControl { control, .. } => {
                let this = self.clone();
                let proxy = control.into_proxy()?;
                let proxy_ = proxy.clone();
                let task = fasync::Task::spawn(async move {
                    let proxy_ = proxy.clone();
                    let mut speaker_gain_stream =
                        HangingGetStream::new(Box::new(move || Some(proxy_.watch_speaker_gain())));
                    let proxy_ = proxy.clone();
                    let mut microphone_gain_stream = HangingGetStream::new(Box::new(move || {
                        Some(proxy_.watch_microphone_gain())
                    }));
                    loop {
                        futures::select! {
                            gain = speaker_gain_stream.next() => {
                                let mut inner = this.inner.lock().await;
                                let peer = inner.peers.get_mut(&id);
                                match (peer, gain) {
                                    (Some(peer), Some(Ok(gain))) => peer.speaker_gain = gain,
                                    _ => break,
                                }
                            }
                            gain = microphone_gain_stream.next() => {
                                let mut inner = this.inner.lock().await;
                                let peer = inner.peers.get_mut(&id);
                                match (peer, gain) {
                                    (Some(peer), Some(Ok(gain))) => peer.microphone_gain = gain,
                                    _ => break,
                                }
                            }
                        }
                    }
                    fx_log_info!("Headset gain control channel for peer {:?} closed", id);
                });
                let mut inner = self.inner.lock().await;
                let peer = inner
                    .peers
                    .get_mut(&id)
                    .ok_or_else(|| format_err!("peer removed: {:?}", id))?;
                if let Some(requested) = peer.requested_speaker_gain.take() {
                    proxy_.set_speaker_gain(requested)?;
                }
                if let Some(requested) = peer.requested_microphone_gain.take() {
                    proxy_.set_microphone_gain(requested)?;
                }
                peer.gain_control_watcher = Some(task);
                peer.gain_control = Some(proxy_);
            }
        }
        Ok(())
    }
async fn handle_peer_request ( & mut self , id : PeerId , request : PeerHandlerRequest , ) -> Result < ( ) , Error > { fx_log_info ! ( " " id request ) ; match request { PeerHandlerRequest :: WatchNetworkInformation { responder , .. } => { let mut inner = self . inner . lock ( ) . await ; let current_network = inner . manager . network . clone ( ) ; let peer = inner . peers . get_mut ( & id ) . ok_or_else ( | | format_err ! ( " " id ) ) ? ; if Some ( & current_network ) == peer . reported_network . as_ref ( ) { peer . network_responder = Some ( responder ) ; } else { responder . send ( current_network . clone ( ) ) ? ; peer . reported_network = Some ( current_network ) ; } } PeerHandlerRequest :: WaitForCall { responder , .. } => { let this = self . clone ( ) ; let mut inner = self . inner . lock ( ) . await ; let peer = inner . peers . get_mut ( & id ) . ok_or_else ( | | format_err ! ( " " id ) ) ? ; if peer . call_responder . is_none ( ) { peer . call_responder = Some ( responder ) ; this . report_calls ( id , inner ) ? ; } else { let err = format_err ! ( " " ) ; fx_log_err ! ( " " err ) ; * inner = TestCallManagerInner :: default ( ) ; return Err ( err ) ; } } PeerHandlerRequest :: InitiateOutgoingCall { action : _ , responder : _ , .. } => { unimplemented ! ( ) ; } PeerHandlerRequest :: QueryOperator { responder , .. } => { responder . send ( Some ( & self . inner . lock ( ) . await . manager . operator ) ) ? ; } PeerHandlerRequest :: SubscriberNumberInformation { responder , .. } => { responder . send ( & mut self . inner . lock ( ) . await . manager . subscriber_numbers . iter ( ) . map ( AsRef :: as_ref ) , ) ? ; } PeerHandlerRequest :: SetNrecMode { enabled , responder , .. } => { let mut inner = self . inner . lock ( ) . await ; if inner . manager . nrec_support { let peer = inner . peers . get_mut ( & id ) . ok_or_else ( | | format_err ! ( " " id ) ) ? ; peer . nrec_enabled = enabled ; responder . send ( & mut Ok ( ( ) ) ) ? ; } else { responder . send ( & mut Err ( zx :: Status :: NOT_SUPPORTED . into_raw ( ) ) ) ? ; } } PeerHandlerRequest :: ReportHeadsetBatteryLevel { level , .. } => { self . inner . lock ( ) . await . peers . get_mut ( & id ) . ok_or_else ( | | format_err ! ( " " id ) ) ? . battery_level = level ; } PeerHandlerRequest :: GainControl { control , .. } => { let this = self . clone ( ) ; let proxy = control . into_proxy ( ) ? ; let proxy_ = proxy . clone ( ) ; let task = fasync :: Task :: spawn ( async move { let proxy_ = proxy . clone ( ) ; let mut speaker_gain_stream = HangingGetStream :: new ( Box :: new ( move | | Some ( proxy_ . watch_speaker_gain ( ) ) ) ) ; let proxy_ = proxy . clone ( ) ; let mut microphone_gain_stream = HangingGetStream :: new ( Box :: new ( move | | { Some ( proxy_ . watch_microphone_gain ( ) ) } ) ) ; loop { futures :: select ! { gain speaker_gain_stream next ( ) { let mut inner this inner lock ( ) await let peer inner peers get_mut ( id ) match ( peer gain ) { ( Some ( peer ) Some ( Ok ( gain ) ) ) peer speaker_gain gain break } } gain microphone_gain_stream next ( ) { let mut inner this inner lock ( ) await let peer inner peers get_mut ( id ) match ( peer gain ) { ( Some ( peer ) Some ( Ok ( gain ) ) ) peer microphone_gain gain break } } } } fx_log_info ! ( " " id ) ; } ) ; let mut inner = self . inner . lock ( ) . await ; let peer = inner . peers . get_mut ( & id ) . ok_or_else ( | | format_err ! ( " " id ) ) ? ; if let Some ( requested ) = peer . requested_speaker_gain . take ( ) { proxy_ . set_speaker_gain ( requested ) ? ; } if let Some ( requested ) = peer . requested_microphone_gain . take ( ) { proxy_ . set_microphone_gain ( requested ) ? ; } peer . gain_control_watcher = Some ( task ) ; peer . gain_control = Some ( proxy_ ) ; } } Ok ( ( ) ) }
Handle a peer `request`.
Handle a peer ` request ` .
&mut self
id
PeerId
request
PeerHandlerRequest
&mut self
id
PeerId
request
PeerHandlerRequest