use std::fmt::Debug;
use std::io;
use futures_core::future::BoxFuture;
pub use burble_crypto::NumCompare;
pub(self) use cmd::*;
pub use {consts::*, peripheral::*, secdb::*};
use crate::l2cap;
mod cmd;
mod consts;
mod peripheral;
mod secdb;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error(transparent)]
L2cap(#[from] l2cap::Error),
#[error("local failure: {0}")]
Local(Reason),
#[error("remote failure: {0}")]
Remote(Reason),
#[error("io error: {0}")]
Io(#[from] io::Error),
#[error("pairing timeout")]
Timeout,
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
#[must_use]
pub struct Device {
display: Option<Box<dyn Display>>,
confirm: Option<Box<dyn Confirm>>,
}
impl Device {
#[inline(always)]
pub fn new() -> Self {
Self {
display: None,
confirm: None,
}
}
#[inline(always)]
pub fn with_display(mut self, d: Box<dyn Display>) -> Self {
self.display = Some(d);
self
}
#[inline(always)]
pub fn with_confirm(mut self, c: Box<dyn Confirm>) -> Self {
self.confirm = Some(c);
self
}
const fn io_cap(&self) -> IoCap {
let inp = match self.confirm {
Some(_) => InputCap::YesNo,
_ => InputCap::None,
};
let out = match self.display {
Some(_) => OutputCap::Numeric,
None => OutputCap::None,
};
IoCap::new(inp, out)
}
}
impl Default for Device {
#[inline(always)]
fn default() -> Self {
Self::new()
}
}
pub trait Display: Debug + Send + Sync {
fn show(&mut self, n: NumCompare) -> BoxFuture<bool>;
}
pub trait Confirm: Debug + Send + Sync {
fn confirm(&mut self) -> BoxFuture<bool>;
}