use fidl_fuchsia_bluetooth_bredr as bredr;
use fuchsia_bluetooth::profile::{DataElement, ProtocolDescriptor};
use crate::dlci::ServerChannel;
pub fn build_rfcomm_protocol(server_channel: ServerChannel) -> Vec<ProtocolDescriptor> {
vec![
ProtocolDescriptor { protocol: bredr::ProtocolIdentifier::L2Cap, params: vec![] },
ProtocolDescriptor {
protocol: bredr::ProtocolIdentifier::Rfcomm,
params: vec![DataElement::Uint8(server_channel.into())],
},
]
}
pub fn is_rfcomm_protocol(protocol: &Vec<ProtocolDescriptor>) -> bool {
protocol.iter().any(|descriptor| descriptor.protocol == bredr::ProtocolIdentifier::Rfcomm)
}
pub fn server_channel_from_protocol(protocol: &Vec<ProtocolDescriptor>) -> Option<ServerChannel> {
for descriptor in protocol {
if descriptor.protocol == bredr::ProtocolIdentifier::Rfcomm {
if descriptor.params.len() != 1 {
return None;
}
if let DataElement::Uint8(sc) = descriptor.params[0] {
return ServerChannel::try_from(sc).ok();
}
return None;
}
}
None
}
pub fn rfcomm_connect_parameters(server_channel: ServerChannel) -> bredr::ConnectParameters {
bredr::ConnectParameters::Rfcomm(bredr::RfcommParameters {
channel: Some(server_channel.into()),
..bredr::RfcommParameters::default()
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn server_channel_from_l2cap_is_none() {
let protocol = vec![ProtocolDescriptor {
protocol: bredr::ProtocolIdentifier::L2Cap,
params: vec![DataElement::Uint16(bredr::PSM_AVDTP)],
}];
assert_eq!(server_channel_from_protocol(&protocol), None);
}
#[test]
fn server_channel_from_empty_rfcomm_is_none() {
let protocol = vec![
ProtocolDescriptor { protocol: bredr::ProtocolIdentifier::L2Cap, params: vec![] },
ProtocolDescriptor { protocol: bredr::ProtocolIdentifier::Rfcomm, params: vec![] },
];
assert_eq!(server_channel_from_protocol(&protocol), None);
}
#[test]
fn server_channel_from_invalid_rfcomm_is_none() {
let protocol = vec![
ProtocolDescriptor { protocol: bredr::ProtocolIdentifier::L2Cap, params: vec![] },
ProtocolDescriptor {
protocol: bredr::ProtocolIdentifier::Rfcomm,
params: vec![DataElement::Uint16(100)], },
];
assert_eq!(server_channel_from_protocol(&protocol), None);
}
#[test]
fn invalid_server_channel_number_is_none() {
let protocol = vec![
ProtocolDescriptor { protocol: bredr::ProtocolIdentifier::L2Cap, params: vec![] },
ProtocolDescriptor {
protocol: bredr::ProtocolIdentifier::Rfcomm,
params: vec![DataElement::Uint8(200)], },
];
assert_eq!(server_channel_from_protocol(&protocol), None);
}
#[test]
fn server_channel_from_valid_rfcomm_is_present() {
let sc = 10;
let protocol = vec![
ProtocolDescriptor { protocol: bredr::ProtocolIdentifier::L2Cap, params: vec![] },
ProtocolDescriptor {
protocol: bredr::ProtocolIdentifier::Rfcomm,
params: vec![DataElement::Uint8(sc)],
},
];
let expected = ServerChannel::try_from(sc).ok();
assert_eq!(server_channel_from_protocol(&protocol), expected);
}
#[test]
fn server_channel_from_only_rfcomm_protocol_is_present() {
let sc = 12;
let protocol = vec![ProtocolDescriptor {
protocol: bredr::ProtocolIdentifier::Rfcomm,
params: vec![DataElement::Uint8(sc)],
}];
let expected = ServerChannel::try_from(sc).ok();
assert_eq!(server_channel_from_protocol(&protocol), expected);
}
}