use std::fmt::{Display, Formatter};
use crate::util::validation::validate_bluetooth_address;
use super::device::DeviceState;
use super::error::ConnectionError;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BluetoothNetworkRole {
PanU, Dun, }
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct BluetoothIdentity {
pub bdaddr: String,
pub bt_device_type: BluetoothNetworkRole,
}
impl BluetoothIdentity {
pub fn new(
bdaddr: String,
bt_device_type: BluetoothNetworkRole,
) -> Result<Self, ConnectionError> {
validate_bluetooth_address(&bdaddr)?;
Ok(Self {
bdaddr,
bt_device_type,
})
}
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct BluetoothDevice {
pub bdaddr: String,
pub name: Option<String>,
pub alias: Option<String>,
pub bt_caps: u32,
pub state: DeviceState,
}
impl BluetoothDevice {
#[must_use]
pub fn new(
bdaddr: String,
name: Option<String>,
alias: Option<String>,
bt_caps: u32,
state: DeviceState,
) -> Self {
Self {
bdaddr,
name,
alias,
bt_caps,
state,
}
}
}
impl Display for BluetoothDevice {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let role = BluetoothNetworkRole::from(self.bt_caps);
write!(
f,
"{} ({}) [{}]",
self.alias.as_deref().unwrap_or("unknown"),
role,
self.bdaddr
)
}
}
impl Display for BluetoothNetworkRole {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
BluetoothNetworkRole::Dun => write!(f, "DUN"),
BluetoothNetworkRole::PanU => write!(f, "PANU"),
}
}
}
impl From<u32> for BluetoothNetworkRole {
fn from(value: u32) -> Self {
match value {
0 => Self::PanU,
1 => Self::Dun,
_ => Self::PanU,
}
}
}