use std::str::FromStr;
use std::time::Duration;
use async_trait::async_trait;
use btleplug::api::{AddressType, BDAddr};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::time::Instant;
use tracing::{debug, error, warn};
use super::data_channel::PxpDataChannel;
use crate::error::PxpError;
const LENGTH_PREFIX_LEN: usize = 4;
const MTU_READY_TIMEOUT: Duration = Duration::from_secs(2);
const MTU_POLL_INTERVAL: Duration = Duration::from_millis(50);
pub(crate) struct L2capDataChannel {
stream: bluer::l2cap::Stream,
read_buf: Vec<u8>,
}
impl L2capDataChannel {
pub(crate) async fn connect(
addr: BDAddr,
addr_type: Option<AddressType>,
psm: u16,
) -> Result<Self, PxpError> {
let (addr, addr_type) = bdaddr_to_bluer(addr, addr_type)?;
let stream =
bluer::l2cap::Stream::connect(bluer::l2cap::SocketAddr::new(addr, addr_type, psm))
.await
.map_err(|e| {
warn!({ ?e, psm }, "Failed to connect L2CAP CoC");
PxpError::from(e)
})?;
await_send_mtu(&stream).await;
Ok(Self {
stream,
read_buf: Vec::new(),
})
}
}
async fn await_send_mtu(stream: &bluer::l2cap::Stream) {
let deadline = Instant::now() + MTU_READY_TIMEOUT;
loop {
if let Ok(mtu) = stream.as_ref().send_mtu() {
if mtu > 0 {
debug!(mtu, "L2CAP send MTU is available");
return;
}
}
if Instant::now() >= deadline {
warn!(
timeout = ?MTU_READY_TIMEOUT,
"L2CAP send MTU did not become available; first writes may be capped at 16 bytes"
);
return;
}
tokio::time::sleep(MTU_POLL_INTERVAL).await;
}
}
#[async_trait]
impl PxpDataChannel for L2capDataChannel {
async fn send(&mut self, message: &[u8]) -> Result<(), PxpError> {
let len = u32::try_from(message.len()).map_err(|_| {
error!(
len = message.len(),
"L2CAP message too large for length prefix"
);
PxpError::InvalidFraming
})?;
let mut frame = Vec::with_capacity(LENGTH_PREFIX_LEN + message.len());
frame.extend_from_slice(&len.to_be_bytes());
frame.extend_from_slice(message);
self.stream.write_all(&frame).await.map_err(|e| {
warn!(?e, "Failed to write L2CAP message");
PxpError::from(e)
})?;
Ok(())
}
async fn recv(&mut self) -> Result<Option<Vec<u8>>, PxpError> {
loop {
if let Some(message) = split_next_message(&mut self.read_buf) {
return Ok(Some(message));
}
let mut chunk = [0u8; 1024];
let n = match self.stream.read(&mut chunk).await {
Ok(n) => n,
Err(e)
if matches!(
e.kind(),
std::io::ErrorKind::ConnectionReset
| std::io::ErrorKind::BrokenPipe
| std::io::ErrorKind::UnexpectedEof
) =>
{
0
}
Err(e) => {
warn!(?e, "Failed to read L2CAP message");
return Err(PxpError::from(e));
}
};
if n == 0 {
if self.read_buf.is_empty() {
return Ok(None);
}
warn!(buffered = self.read_buf.len(), "L2CAP closed mid-message");
return Err(PxpError::ConnectionLost);
}
self.read_buf
.extend_from_slice(chunk.get(..n).unwrap_or(&[]));
}
}
}
fn split_next_message(buf: &mut Vec<u8>) -> Option<Vec<u8>> {
let prefix: [u8; LENGTH_PREFIX_LEN] = buf.get(..LENGTH_PREFIX_LEN)?.try_into().ok()?;
let len = usize::try_from(u32::from_be_bytes(prefix)).ok()?;
let end = LENGTH_PREFIX_LEN.checked_add(len)?;
let message = buf.get(LENGTH_PREFIX_LEN..end)?.to_vec();
buf.drain(..end);
Some(message)
}
fn bdaddr_to_bluer(
addr: BDAddr,
addr_type: Option<AddressType>,
) -> Result<(bluer::Address, bluer::AddressType), PxpError> {
let addr = bluer::Address::from_str(&addr.to_string()).map_err(|_| {
warn!("Failed to parse Bluetooth address");
PxpError::InvalidEndpoint
})?;
let addr_type = match addr_type {
Some(AddressType::Public) => bluer::AddressType::LePublic,
Some(AddressType::Random) => bluer::AddressType::LeRandom,
None => {
warn!("Peer address type unknown; defaulting to LE public");
bluer::AddressType::LePublic
}
};
Ok((addr, addr_type))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn split_message_across_reads() {
let mut buf = b"\0\0\0".to_vec();
assert_eq!(split_next_message(&mut buf), None);
assert_eq!(buf, b"\0\0\0");
buf.extend_from_slice(b"\x05hel");
assert_eq!(split_next_message(&mut buf), None);
assert_eq!(buf, b"\0\0\0\x05hel");
buf.extend_from_slice(b"lo");
assert_eq!(split_next_message(&mut buf), Some(b"hello".to_vec()));
assert!(buf.is_empty());
}
#[test]
fn split_multiple_buffered_messages() {
let mut buf = b"\0\0\0\x03one\0\0\0\x03two\0\0\0\x03th".to_vec();
assert_eq!(split_next_message(&mut buf), Some(b"one".to_vec()));
assert_eq!(split_next_message(&mut buf), Some(b"two".to_vec()));
assert_eq!(split_next_message(&mut buf), None);
assert_eq!(buf, b"\0\0\0\x03th");
}
#[test]
fn split_nothing_buffered() {
let mut buf = Vec::new();
assert_eq!(split_next_message(&mut buf), None);
assert!(buf.is_empty());
}
#[test]
fn split_empty_message() {
let mut buf = b"\0\0\0\0rest".to_vec();
assert_eq!(split_next_message(&mut buf), Some(Vec::new()));
assert_eq!(buf, b"rest");
}
#[test]
fn split_handles_binary_payload() {
let mut buf = vec![0x00, 0x00, 0x00, 0x04, 0x00, 0xFF, 0x0D, 0x0A];
assert_eq!(
split_next_message(&mut buf),
Some(vec![0x00, 0xFF, 0x0D, 0x0A])
);
assert!(buf.is_empty());
}
#[test]
fn length_prefix_is_big_endian() {
let mut buf = vec![0x00, 0x00, 0x01, 0x00];
buf.extend_from_slice(&[0xAB; 0x100]);
assert_eq!(split_next_message(&mut buf), Some(vec![0xAB; 0x100]));
assert!(buf.is_empty());
}
#[test]
fn address_round_trip_public() {
let bd = BDAddr::from([0x1F, 0x2A, 0x00, 0xCC, 0x22, 0xF1]);
let (addr, addr_type) = bdaddr_to_bluer(bd, Some(AddressType::Public)).unwrap();
assert_eq!(
addr,
bluer::Address::new([0x1F, 0x2A, 0x00, 0xCC, 0x22, 0xF1])
);
assert_eq!(addr_type, bluer::AddressType::LePublic);
}
#[test]
fn address_round_trip_random() {
let bd = BDAddr::from([0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF]);
let (addr, addr_type) = bdaddr_to_bluer(bd, Some(AddressType::Random)).unwrap();
assert_eq!(
addr,
bluer::Address::new([0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF])
);
assert_eq!(addr_type, bluer::AddressType::LeRandom);
}
#[test]
fn address_type_defaults_to_public() {
let bd = BDAddr::from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
let (_, addr_type) = bdaddr_to_bluer(bd, None).unwrap();
assert_eq!(addr_type, bluer::AddressType::LePublic);
}
}