use std::io;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender};
use std::sync::Arc;
use std::time::Duration;
pub const BLE_SCHEME: &str = "ble://";
pub(crate) const RX_QUEUE_CAP: usize = 1 << 20;
const MAX_ATTRIBUTE_LEN: usize = 512;
#[cfg_attr(target_os = "android", allow(dead_code))] pub(crate) fn chunk_size(mtu: u16) -> usize {
(usize::from(mtu.max(23)) - 3).min(MAX_ATTRIBUTE_LEN)
}
pub(crate) const WRITE_PACE_EVERY: usize = 8;
#[derive(Debug, Default)]
#[cfg_attr(target_os = "android", allow(dead_code))]
pub(crate) struct WritePacer {
sent: usize,
}
#[cfg_attr(target_os = "android", allow(dead_code))]
impl WritePacer {
pub(crate) fn with_response(&mut self, can_with: bool, can_without: bool) -> bool {
self.sent += 1;
match (can_with, can_without) {
(true, true) => self.sent.is_multiple_of(WRITE_PACE_EVERY),
(_, false) => true,
(false, true) => false,
}
}
}
#[cfg_attr(target_os = "android", allow(dead_code))]
pub(crate) fn target_matches(id: &str, address: &str, name: Option<&str>, target: &str) -> bool {
id.eq_ignore_ascii_case(target)
|| (address != "00:00:00:00:00:00" && address.eq_ignore_ascii_case(target))
|| name == Some(target)
}
pub fn ble_target(path: &str) -> Option<&str> {
path.strip_prefix(BLE_SCHEME)
}
pub(crate) struct RxFeed {
tx: Sender<Vec<u8>>,
queued: Arc<AtomicUsize>,
cap: usize,
dropping: AtomicBool,
}
impl RxFeed {
pub(crate) fn push(&self, chunk: Vec<u8>) -> bool {
let n = chunk.len();
if self.queued.load(Ordering::Acquire) + n > self.cap {
if self.dropping.swap(true, Ordering::Relaxed) {
return false;
}
crate::log_warn!(
"[BLE] RX queue full ({} bytes unread): dropping notifications until it is read",
self.cap
);
return false;
}
self.dropping.store(false, Ordering::Relaxed);
self.queued.fetch_add(n, Ordering::AcqRel);
if self.tx.send(chunk).is_err() {
self.queued.fetch_sub(n, Ordering::AcqRel);
}
true
}
}
pub(crate) fn rx_channel(cap: usize) -> (RxFeed, RxQueue) {
let (tx, rx) = mpsc::channel();
let queued = Arc::new(AtomicUsize::new(0));
let feed = RxFeed {
tx,
queued: queued.clone(),
cap,
dropping: AtomicBool::new(false),
};
let mut q = RxQueue::new(rx);
q.queued = Some(queued);
(feed, q)
}
pub(crate) struct RxQueue {
rx: Receiver<Vec<u8>>,
pending: Vec<u8>,
queued: Option<Arc<AtomicUsize>>,
}
impl RxQueue {
pub(crate) fn new(rx: Receiver<Vec<u8>>) -> Self {
Self {
rx,
pending: Vec::new(),
queued: None,
}
}
fn took(&self, chunk: &[u8]) {
if let Some(q) = &self.queued {
q.fetch_sub(chunk.len(), Ordering::AcqRel);
}
}
pub(crate) fn read(&mut self, buf: &mut [u8], timeout: Duration) -> io::Result<usize> {
if self.pending.is_empty() {
match self.rx.recv_timeout(timeout.max(Duration::from_millis(1))) {
Ok(chunk) => {
self.took(&chunk);
if chunk.is_empty() {
return Err(io::Error::new(io::ErrorKind::TimedOut, "BLE read timeout"));
}
self.pending = chunk;
}
Err(RecvTimeoutError::Timeout) => {
return Err(io::Error::new(io::ErrorKind::TimedOut, "BLE read timeout"))
}
Err(RecvTimeoutError::Disconnected) => {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"BLE device disconnected",
))
}
}
}
let n = buf.len().min(self.pending.len());
buf[..n].copy_from_slice(&self.pending[..n]);
self.pending.drain(..n);
Ok(n)
}
pub(crate) fn clear(&mut self) {
self.pending.clear();
while let Ok(chunk) = self.rx.try_recv() {
self.took(&chunk);
}
}
pub(crate) fn buffered(&self) -> usize {
self.pending.len()
+ self
.queued
.as_ref()
.map_or(0, |q| q.load(Ordering::Acquire))
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::mpsc;
#[test]
fn rx_queue_splits_chunks_and_reports_timeout_and_disconnect() {
let (tx, rx) = mpsc::channel();
let mut q = RxQueue::new(rx);
tx.send(b"hello".to_vec()).unwrap();
let mut buf = [0u8; 3];
assert_eq!(q.read(&mut buf, Duration::from_millis(10)).unwrap(), 3);
assert_eq!(&buf, b"hel");
assert_eq!(q.buffered(), 2);
assert_eq!(q.read(&mut buf, Duration::from_millis(10)).unwrap(), 2);
assert_eq!(&buf[..2], b"lo");
let e = q.read(&mut buf, Duration::from_millis(5)).unwrap_err();
assert_eq!(e.kind(), io::ErrorKind::TimedOut);
drop(tx);
let e = q.read(&mut buf, Duration::from_millis(5)).unwrap_err();
assert_eq!(e.kind(), io::ErrorKind::UnexpectedEof);
q.clear();
}
#[test]
fn empty_notification_is_a_timeout_not_eof() {
let (tx, rx) = mpsc::channel();
let mut q = RxQueue::new(rx);
tx.send(Vec::new()).unwrap();
let mut buf = [0u8; 4];
let e = q.read(&mut buf, Duration::from_millis(10)).unwrap_err();
assert_eq!(e.kind(), io::ErrorKind::TimedOut);
}
#[test]
fn rx_channel_counts_queued_bytes_and_drops_over_cap() {
let (feed, mut q) = rx_channel(8);
assert!(feed.push(b"hello".to_vec()));
assert_eq!(q.buffered(), 5, "queued chunk counts before any read");
assert!(!feed.push(b"world".to_vec()), "5 + 5 > cap 8: dropped");
assert!(feed.push(b"abc".to_vec()));
assert_eq!(q.buffered(), 8);
let mut buf = [0u8; 2];
assert_eq!(q.read(&mut buf, Duration::from_millis(10)).unwrap(), 2);
assert_eq!(q.buffered(), 6, "3 pending + 3 queued");
assert!(feed.push(b"xy".to_vec()), "room again after a read");
q.clear();
assert_eq!(q.buffered(), 0);
let mut all = [0u8; 8];
let e = q.read(&mut all, Duration::from_millis(5)).unwrap_err();
assert_eq!(e.kind(), io::ErrorKind::TimedOut);
drop(feed);
let e = q.read(&mut all, Duration::from_millis(5)).unwrap_err();
assert_eq!(e.kind(), io::ErrorKind::UnexpectedEof);
}
#[test]
fn chunks_follow_mtu_capped_at_the_attribute_size() {
assert_eq!(chunk_size(23), 20);
assert_eq!(chunk_size(0), 20);
assert_eq!(chunk_size(247), 244);
assert_eq!(chunk_size(517), 512);
assert_eq!(chunk_size(527), 512);
}
#[test]
fn every_eighth_chunk_waits_for_a_response_when_rx_allows_both() {
let mut p = WritePacer::default();
let got: Vec<bool> = (0..16).map(|_| p.with_response(true, true)).collect();
let want: Vec<bool> = (1..=16_usize)
.map(|i| i.is_multiple_of(WRITE_PACE_EVERY))
.collect();
assert_eq!(got, want);
let mut p = WritePacer::default();
assert!(
(0..9).all(|_| p.with_response(true, false)),
"no write-without-response"
);
let mut p = WritePacer::default();
assert!(
(0..9).all(|_| !p.with_response(false, true)),
"no write-with-response"
);
}
#[test]
fn target_matches_id_mac_in_any_case_or_name() {
let m = |t| target_matches("hci0/dev_AA_BB", "AA:BB:CC:DD:EE:FF", Some("Nus Board"), t);
assert!(m("hci0/dev_AA_BB"));
assert!(m("aa:bb:cc:dd:ee:ff"));
assert!(m("AA:BB:CC:DD:EE:FF"));
assert!(m("Nus Board"));
assert!(!m("nus board"), "names are exact");
assert!(!m("AA:BB"));
assert!(!target_matches(
"uuid",
"00:00:00:00:00:00",
None,
"00:00:00:00:00:00"
));
assert!(target_matches(
"5D2A-UUID",
"00:00:00:00:00:00",
None,
"5d2a-uuid"
));
}
#[test]
fn ble_target_strips_scheme() {
assert_eq!(ble_target("ble://AA:BB"), Some("AA:BB"));
assert_eq!(ble_target("/dev/ttyUSB0"), None);
}
}