use std::time::Duration;
use bt_hci::cmd::le::{
LeAddDeviceToFilterAcceptList, LeClearFilterAcceptList, LeCreateConn, LeCreateConnCancel,
LeExtCreateConn, LeRemoveDeviceFromFilterAcceptList,
};
use bt_hci::param::{AddrKind, BdAddr, Duration as HciDuration, InitiatingPhy, PhyParams};
use serde::{Deserialize, Serialize};
use tokio::sync::oneshot;
use crate::address::Address;
use crate::connection::{Connection, ParamsRequest, Phy};
use crate::error::{Error, Result};
use crate::host::Host;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct ConnectParams {
pub scan_interval_ms: u32,
pub scan_window_ms: u32,
pub params: ParamsRequest,
pub phys: Vec<Phy>,
pub timeout_ms: u32,
}
impl Default for ConnectParams {
fn default() -> Self {
ConnectParams {
scan_interval_ms: 60,
scan_window_ms: 30,
params: ParamsRequest::default(),
phys: vec![Phy::Le1m],
timeout_ms: 10_000,
}
}
}
fn u625(ms: u32) -> HciDuration<625> {
HciDuration::from_u16(
u16::try_from((u64::from(ms) * 1000 / 625).clamp(4, 0x4000)).unwrap_or(0x4000),
)
}
impl Host {
pub async fn connect(&self, peer: Address, p: &ConnectParams) -> Result<Connection> {
p.params.check()?;
if p.scan_window_ms > p.scan_interval_ms {
return Err(Error::InvalidParameter(
"the scan window must not exceed the interval".into(),
));
}
let (tx, rx) = oneshot::channel();
{
let mut w = self
.inner
.connect_waiter
.lock()
.unwrap_or_else(|e| e.into_inner());
if w.is_some() {
return Err(Error::InvalidParameter(
"a connection is already being made".into(),
));
}
*w = Some(tx);
}
let r = self.initiate(peer, p).await;
if let Err(e) = r {
self.inner
.connect_waiter
.lock()
.unwrap_or_else(|e| e.into_inner())
.take();
return Err(e);
}
let mut rx = rx;
let state =
match tokio::time::timeout(Duration::from_millis(u64::from(p.timeout_ms)), &mut rx)
.await
{
Ok(Ok(r)) => r?,
Ok(Err(_)) => return Err(Error::Stopped),
Err(_) => {
let _ = self.inner.hci.exec(&LeCreateConnCancel::new()).await;
match tokio::time::timeout(Duration::from_secs(3), rx).await {
Ok(Ok(Ok(s))) => s,
_ => {
self.inner
.connect_waiter
.lock()
.unwrap_or_else(|e| e.into_inner())
.take();
return Err(Error::Timeout("the device to accept the connection"));
}
}
}
};
Ok(Connection::new(self.clone(), state))
}
async fn initiate(&self, peer: Address, p: &ConnectParams) -> Result<()> {
let caps = self.inner.caps();
let own = AddrKind::new(self.address().hci_type());
let c = &p.params;
if caps.extended_advertising() {
let phy = InitiatingPhy {
scan_interval: u625(p.scan_interval_ms),
scan_window: u625(p.scan_window_ms),
conn_interval_min: HciDuration::from_u16(c.interval_min),
conn_interval_max: HciDuration::from_u16(c.interval_max),
max_latency: c.latency,
supervision_timeout: HciDuration::from_u16(c.timeout),
min_ce_len: HciDuration::from_u16(0),
max_ce_len: HciDuration::from_u16(0),
};
let want = |x: Phy| p.phys.contains(&x);
let phys = PhyParams {
le_1m_phy: (want(Phy::Le1m) || p.phys.is_empty()).then_some(phy),
le_2m_phy: (want(Phy::Le2m) && caps.phy_2m()).then_some(phy),
le_coded_phy: (want(Phy::LeCoded) && caps.coded_phy()).then_some(phy),
};
self.inner
.hci
.exec_async(&LeExtCreateConn::new(
false,
own,
AddrKind::new(peer.hci_type()),
BdAddr::new(peer.bytes),
phys,
))
.await
} else {
self.inner
.hci
.exec_async(&LeCreateConn::new(
u625(p.scan_interval_ms),
u625(p.scan_window_ms),
false,
AddrKind::new(peer.hci_type()),
BdAddr::new(peer.bytes),
own,
HciDuration::from_u16(c.interval_min),
HciDuration::from_u16(c.interval_max),
c.latency,
HciDuration::from_u16(c.timeout),
HciDuration::from_u16(0),
HciDuration::from_u16(0),
))
.await
}
}
pub async fn accept_list_add(&self, a: Address) -> Result<()> {
self.inner
.hci
.exec(&LeAddDeviceToFilterAcceptList::new(
AddrKind::new(a.hci_type()),
BdAddr::new(a.bytes),
))
.await
}
pub async fn accept_list_remove(&self, a: Address) -> Result<()> {
self.inner
.hci
.exec(&LeRemoveDeviceFromFilterAcceptList::new(
AddrKind::new(a.hci_type()),
BdAddr::new(a.bytes),
))
.await
}
pub async fn accept_list_clear(&self) -> Result<()> {
self.inner.hci.exec(&LeClearFilterAcceptList::new()).await
}
pub fn listen_channel(
&self,
spsm: u16,
mtu: u16,
) -> tokio::sync::mpsc::Receiver<crate::l2cap::Channel> {
crate::l2cap::listen(self, spsm, mtu)
}
}