pub fn to_service_found_response(&self) -> Result<ServiceFoundResponse, Error> {
        let peer_id = self.reg_id.ok_or(format_err!("The service has not been registered."))?.0;
        let mut attributes = vec![];

        // 1. Build the (optional) primary Protocol Descriptor List. This is both returned and
        // included in `attributes`.
        let (protocol, attribute_list) = match self.primary_connection {
            Some(Connection::L2cap(psm)) => {
                let prot_list = vec![
                    Some(Box::new(Uuid::new16(bredr::ProtocolIdentifier::L2Cap as u16).into())),
                    Some(Box::new(bredr::DataElement::Uint16(psm.into()))),
                ];
                (Some(build_l2cap_descriptor(psm)), Some(prot_list))
            }
            Some(Connection::Rfcomm(Some(channel_number))) => {
                let prot_list = vec![
                    Some(Box::new(Uuid::new16(bredr::ProtocolIdentifier::L2Cap as u16).into())),
                    Some(Box::new(Uuid::new16(bredr::ProtocolIdentifier::Rfcomm as u16).into())),
                    Some(Box::new(bredr::DataElement::Uint8(channel_number.into()))),
                ];
                let rfcomm_protocol =
                    build_rfcomm_protocol(channel_number).iter().map(|p| p.into()).collect();
                (Some(rfcomm_protocol), Some(prot_list))
            }
            _ => (None, None),
        };
        if let Some(attr) = attribute_list {
            attributes.push(bredr::Attribute {
                id: bredr::ATTR_PROTOCOL_DESCRIPTOR_LIST,
                element: bredr::DataElement::Sequence(vec![Some(Box::new(
                    bredr::DataElement::Sequence(attr),
                ))]),
            });
        }

        // 2. Add the Service Class ID List Attribute. There should always be at least one.
        let svc_ids_list: Vec<bredr::ServiceClassProfileIdentifier> =
            self.svc_ids.iter().cloned().collect();
        let svc_ids_sequence =
            svc_ids_list.into_iter().map(|id| Some(Box::new(Uuid::from(id).into()))).collect();
        attributes.push(bredr::Attribute {
            id: bredr::ATTR_SERVICE_CLASS_ID_LIST,
            element: bredr::DataElement::Sequence(svc_ids_sequence),
        });

        // 3. Add the potential Profile Descriptors.
        if !self.profile_descriptors.is_empty() {
            let mut prof_desc_sequence = vec![];
            for descriptor in &self.profile_descriptors {
                let desc_list = vec![
                    Some(Box::new(Uuid::from(descriptor.profile_id).into())),
                    Some(Box::new(bredr::DataElement::Uint16(u16::from_be_bytes([
                        descriptor.major_version,
                        descriptor.minor_version,
                    ])))),
                ];
                prof_desc_sequence.push(Some(Box::new(bredr::DataElement::Sequence(desc_list))));
            }
            attributes.push(bredr::Attribute {
                id: bredr::ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST,
                element: bredr::DataElement::Sequence(prof_desc_sequence),
            });
        }

        // Add the additional attributes to the response.
        let mut additional_attributes =
            self.additional_attributes.iter().map(|attr| bredr::Attribute::from(attr)).collect();
        attributes.append(&mut additional_attributes);

        Ok(ServiceFoundResponse { id: peer_id, protocol, attributes })
    }
/// Converts the ServiceRecord into a ServiceFoundResponse. Builds the
/// ProtocolDescriptorList from the data in the ServiceRecord.
///
/// Returns an error if the ServiceRecord has not been registered.
// TODO(fxbug.dev/51454): Build the full ServiceFoundResponse. Right now, we just
// build the primary L2CAP Protocol, ServiceClassIdentifiers, and Profile Descriptors.


Returns an error if the ServiceRecord has not been registered.
TODO(fxbug.dev/51454): Build the full ServiceFoundResponse. Right now, we just
build the primary L2CAP Protocol, ServiceClassIdentifiers, and Profile Descriptors.
Returns an error if the ServiceRecord has not been registered . TODO ( fxbug . dev / 51454 ) : Build the full ServiceFoundResponse . Right now we just build the primary L2CAP Protocol ServiceClassIdentifiers and Profile Descriptors .
pub fn to_service_found_response(&self) -> Result<ServiceFoundResponse, Error> {
        let peer_id = self.reg_id.ok_or(format_err!("The service has not been registered."))?.0;
        let mut attributes = vec![];
        // 1. Build the (optional) primary Protocol Descriptor List. This is both returned and
        // included in `attributes`.
        let (protocol, attribute_list) = match self.primary_connection {
            Some(Connection::L2cap(psm)) => {
                let prot_list = vec![
                    Some(Box::new(Uuid::new16(bredr::ProtocolIdentifier::L2Cap as u16).into())),
                    Some(Box::new(bredr::DataElement::Uint16(psm.into()))),
                ];
                (Some(build_l2cap_descriptor(psm)), Some(prot_list))
            }
            Some(Connection::Rfcomm(Some(channel_number))) => {
                let prot_list = vec![
                    Some(Box::new(Uuid::new16(bredr::ProtocolIdentifier::L2Cap as u16).into())),
                    Some(Box::new(Uuid::new16(bredr::ProtocolIdentifier::Rfcomm as u16).into())),
                    Some(Box::new(bredr::DataElement::Uint8(channel_number.into()))),
                ];
                let rfcomm_protocol =
                    build_rfcomm_protocol(channel_number).iter().map(|p| p.into()).collect();
                (Some(rfcomm_protocol), Some(prot_list))
            }
            _ => (None, None),
        };
        if let Some(attr) = attribute_list {
            attributes.push(bredr::Attribute {
                id: bredr::ATTR_PROTOCOL_DESCRIPTOR_LIST,
                element: bredr::DataElement::Sequence(vec![Some(Box::new(
                    bredr::DataElement::Sequence(attr),
                ))]),
            });
        }
        // 2. Add the Service Class ID List Attribute. There should always be at least one.
        let svc_ids_list: Vec<bredr::ServiceClassProfileIdentifier> =
            self.svc_ids.iter().cloned().collect();
        let svc_ids_sequence =
            svc_ids_list.into_iter().map(|id| Some(Box::new(Uuid::from(id).into()))).collect();
        attributes.push(bredr::Attribute {
            id: bredr::ATTR_SERVICE_CLASS_ID_LIST,
            element: bredr::DataElement::Sequence(svc_ids_sequence),
        });
        // 3. Add the potential Profile Descriptors.
        if !self.profile_descriptors.is_empty() {
            let mut prof_desc_sequence = vec![];
            for descriptor in &self.profile_descriptors {
                let desc_list = vec![
                    Some(Box::new(Uuid::from(descriptor.profile_id).into())),
                    Some(Box::new(bredr::DataElement::Uint16(u16::from_be_bytes([
                        descriptor.major_version,
                        descriptor.minor_version,
                    ])))),
                ];
                prof_desc_sequence.push(Some(Box::new(bredr::DataElement::Sequence(desc_list))));
            }
            attributes.push(bredr::Attribute {
                id: bredr::ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST,
                element: bredr::DataElement::Sequence(prof_desc_sequence),
            });
        }
        // Add the additional attributes to the response.
        let mut additional_attributes =
            self.additional_attributes.iter().map(|attr| bredr::Attribute::from(attr)).collect();
        attributes.append(&mut additional_attributes);
        Ok(ServiceFoundResponse { id: peer_id, protocol, attributes })
    }
pub fn to_service_found_response ( & self ) -> Result < ServiceFoundResponse , Error > { let peer_id = self . reg_id . ok_or ( format_err ! ( " " ) ) ? . 0 ; let mut attributes = vec ! [ ] ; // 1. Build the (optional) primary Protocol Descriptor List. This is both returned and // included in `attributes`. let ( protocol , attribute_list ) = match self . primary_connection { Some ( Connection :: L2cap ( psm ) ) => { let prot_list = vec ! [ Some ( Box new ( Uuid new16 ( bredr ProtocolIdentifier L2Cap as u16 ) into ( ) ) ) Some ( Box new ( bredr DataElement Uint16 ( psm into ( ) ) ) ) ] ; ( Some ( build_l2cap_descriptor ( psm ) ) , Some ( prot_list ) ) } Some ( Connection :: Rfcomm ( Some ( channel_number ) ) ) => { let prot_list = vec ! [ Some ( Box new ( Uuid new16 ( bredr ProtocolIdentifier L2Cap as u16 ) into ( ) ) ) Some ( Box new ( Uuid new16 ( bredr ProtocolIdentifier Rfcomm as u16 ) into ( ) ) ) Some ( Box new ( bredr DataElement Uint8 ( channel_number into ( ) ) ) ) ] ; let rfcomm_protocol = build_rfcomm_protocol ( channel_number ) . iter ( ) . map ( | p | p . into ( ) ) . collect ( ) ; ( Some ( rfcomm_protocol ) , Some ( prot_list ) ) } _ => ( None , None ) , } ; if let Some ( attr ) = attribute_list { attributes . push ( bredr :: Attribute { id : bredr :: ATTR_PROTOCOL_DESCRIPTOR_LIST , element : bredr :: DataElement :: Sequence ( vec ! [ Some ( Box new ( bredr DataElement Sequence ( attr ) ) ) ] ) , } ) ; } // 2. Add the Service Class ID List Attribute. There should always be at least one. let svc_ids_list : Vec < bredr :: ServiceClassProfileIdentifier > = self . svc_ids . iter ( ) . cloned ( ) . collect ( ) ; let svc_ids_sequence = svc_ids_list . into_iter ( ) . map ( | id | Some ( Box :: new ( Uuid :: from ( id ) . into ( ) ) ) ) . collect ( ) ; attributes . push ( bredr :: Attribute { id : bredr :: ATTR_SERVICE_CLASS_ID_LIST , element : bredr :: DataElement :: Sequence ( svc_ids_sequence ) , } ) ; // 3. Add the potential Profile Descriptors. if ! self . profile_descriptors . is_empty ( ) { let mut prof_desc_sequence = vec ! [ ] ; for descriptor in & self . profile_descriptors { let desc_list = vec ! [ Some ( Box new ( Uuid from ( descriptor profile_id ) into ( ) ) ) Some ( Box new ( bredr DataElement Uint16 ( u16 from_be_bytes ( [ descriptor major_version descriptor minor_version ] ) ) ) ) ] ; prof_desc_sequence . push ( Some ( Box :: new ( bredr :: DataElement :: Sequence ( desc_list ) ) ) ) ; } attributes . push ( bredr :: Attribute { id : bredr :: ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST , element : bredr :: DataElement :: Sequence ( prof_desc_sequence ) , } ) ; } // Add the additional attributes to the response. let mut additional_attributes = self . additional_attributes . iter ( ) . map ( | attr | bredr :: Attribute :: from ( attr ) ) . collect ( ) ; attributes . append ( & mut additional_attributes ) ; Ok ( ServiceFoundResponse { id : peer_id , protocol , attributes } ) }
Converts the ServiceRecord into a ServiceFoundResponse.
Converts the ServiceRecord into a ServiceFoundResponse .
&self
&self