use std::time::Duration;
use uuid::Uuid;
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum Filter {
Address(String),
Characteristic(Uuid),
Name(String),
Rssi(i16),
Service(Uuid),
}
#[derive(Default)]
pub struct ScanConfig {
pub(crate) adapter_index: usize,
pub(crate) filters: Vec<Filter>,
pub(crate) address_filter: Option<Box<dyn Fn(&str, &str) -> bool + Send + Sync>>,
pub(crate) name_filter: Option<Box<dyn Fn(&str, &str) -> bool + Send + Sync>>,
pub(crate) rssi_filter: Option<Box<dyn Fn(i16, i16) -> bool + Send + Sync>>,
pub(crate) service_filter: Option<Box<dyn Fn(&Vec<Uuid>, &Uuid) -> bool + Send + Sync>>,
pub(crate) characteristics_filter: Option<Box<dyn Fn(&Vec<Uuid>, &Uuid) -> bool + Send + Sync>>,
pub(crate) max_results: Option<usize>,
pub(crate) timeout: Option<Duration>,
pub(crate) force_disconnect: bool,
}
impl ScanConfig {
#[inline]
pub fn adapter_index(mut self, index: usize) -> Self {
self.adapter_index = index;
self
}
#[inline]
pub fn with_filters(mut self, filters: &[Filter]) -> Self {
self.filters.extend_from_slice(filters);
self
}
#[inline]
pub fn filter_by_address(
mut self,
func: impl Fn(&str, &str) -> bool + Send + Sync + 'static,
) -> Self {
self.address_filter = Some(Box::new(func));
self
}
#[inline]
pub fn filter_by_name(
mut self,
func: impl Fn(&str, &str) -> bool + Send + Sync + 'static,
) -> Self {
self.name_filter = Some(Box::new(func));
self
}
#[inline]
pub fn filter_by_rssi(
mut self,
func: impl Fn(i16, i16) -> bool + Send + Sync + 'static,
) -> Self {
self.rssi_filter = Some(Box::new(func));
self
}
#[inline]
pub fn filter_by_service(
mut self,
func: impl Fn(&Vec<Uuid>, &Uuid) -> bool + Send + Sync + 'static,
) -> Self {
self.service_filter = Some(Box::new(func));
self
}
#[inline]
pub fn filter_by_characteristics(
mut self,
func: impl Fn(&Vec<Uuid>, &Uuid) -> bool + Send + Sync + 'static,
) -> Self {
self.characteristics_filter = Some(Box::new(func));
self
}
#[inline]
pub fn stop_after_matches(mut self, max_results: usize) -> Self {
self.max_results = Some(max_results);
self
}
#[inline]
pub fn stop_after_first_match(self) -> Self {
self.stop_after_matches(1)
}
#[inline]
pub fn stop_after_timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
#[inline]
pub fn force_disconnect(mut self, force_disconnect: bool) -> Self {
self.force_disconnect = force_disconnect;
self
}
#[inline]
pub fn require_name(self) -> Self {
if self.name_filter.is_none() {
self.filter_by_name(|src, _| !src.is_empty())
} else {
self
}
}
}