use std::collections::HashMap;
use std::sync::Arc;
use async_trait::async_trait;
use btleplug::api::{Characteristic, Peripheral as _, WriteType};
use btleplug::platform::Peripheral;
use tokio::sync::RwLock;
use uuid::Uuid;
use crate::error::{Error, Result};
use crate::transport::Transport;
pub struct BtleplugTransport {
peripheral: Peripheral,
char_cache: Arc<RwLock<HashMap<Uuid, Characteristic>>>,
}
impl BtleplugTransport {
pub async fn new(peripheral: Peripheral) -> Result<Self> {
if !peripheral.is_connected().await? {
return Err(Error::NotConnected);
}
let transport = Self {
peripheral,
char_cache: Arc::new(RwLock::new(HashMap::new())),
};
transport.populate_cache().await?;
Ok(transport)
}
pub async fn connect_and_discover(peripheral: Peripheral) -> Result<Self> {
if !peripheral.is_connected().await? {
peripheral.connect().await?;
}
peripheral.discover_services().await?;
Self::new(peripheral).await
}
#[must_use]
pub fn peripheral(&self) -> &Peripheral {
&self.peripheral
}
async fn populate_cache(&self) -> Result<()> {
let services = self.peripheral.services();
let mut cache = self.char_cache.write().await;
for service in services {
for char in service.characteristics {
cache.insert(char.uuid, char);
}
}
Ok(())
}
async fn find_characteristic(&self, uuid: Uuid) -> Result<Characteristic> {
{
let cache = self.char_cache.read().await;
if let Some(char) = cache.get(&uuid) {
return Ok(char.clone());
}
}
self.populate_cache().await?;
let cache = self.char_cache.read().await;
cache
.get(&uuid)
.cloned()
.ok_or(Error::CharacteristicNotFound(uuid_to_name(uuid)))
}
}
#[async_trait]
impl Transport for BtleplugTransport {
async fn read(&self, char_uuid: Uuid) -> Result<Vec<u8>> {
let char = self.find_characteristic(char_uuid).await?;
self.peripheral
.read(&char)
.await
.map_err(|e| map_btleplug_error(e, char_uuid))
}
async fn write(&self, char_uuid: Uuid, data: &[u8]) -> Result<()> {
let char = self.find_characteristic(char_uuid).await?;
self.peripheral
.write(&char, data, WriteType::WithResponse)
.await
.map_err(|e| map_btleplug_error(e, char_uuid))
}
async fn write_without_response(&self, char_uuid: Uuid, data: &[u8]) -> Result<()> {
let char = self.find_characteristic(char_uuid).await?;
self.peripheral
.write(&char, data, WriteType::WithoutResponse)
.await
.map_err(|e| map_btleplug_error(e, char_uuid))
}
async fn subscribe(&self, char_uuid: Uuid) -> Result<()> {
let char = self.find_characteristic(char_uuid).await?;
self.peripheral
.subscribe(&char)
.await
.map_err(|e| map_btleplug_error(e, char_uuid))
}
async fn unsubscribe(&self, char_uuid: Uuid) -> Result<()> {
let char = self.find_characteristic(char_uuid).await?;
self.peripheral
.unsubscribe(&char)
.await
.map_err(|e| map_btleplug_error(e, char_uuid))
}
fn is_connected(&self) -> bool {
true
}
fn local_name(&self) -> Option<String> {
futures::executor::block_on(async {
self.peripheral
.properties()
.await
.ok()
.flatten()
.and_then(|p| p.local_name)
})
}
}
fn map_btleplug_error(err: btleplug::Error, _char_uuid: Uuid) -> Error {
let err_str = err.to_string().to_lowercase();
if err_str.contains("authentication") || err_str.contains("0x05") {
return Error::InsufficientAuthentication;
}
Error::Btleplug(err)
}
fn uuid_to_name(uuid: Uuid) -> &'static str {
use crate::protocol::{
BATTERY_LEVEL_CHAR_UUID, COMMAND_CHAR_UUID, CONFIG_CHAR_UUID, CONTROL_CHAR_UUID,
DEVICE_INFO_CHAR_UUID, DEVICE_NAME_CHAR_UUID, EXTENDED_INFO_CHAR_UUID,
FIRMWARE_REVISION_CHAR_UUID, LOCK_POSITION_CHAR_UUID, LOCK_STATE_CHAR_UUID,
STATUS_CHAR_UUID,
};
match uuid {
_ if uuid == LOCK_STATE_CHAR_UUID => "lock_state",
_ if uuid == STATUS_CHAR_UUID => "status",
_ if uuid == LOCK_POSITION_CHAR_UUID => "lock_position",
_ if uuid == COMMAND_CHAR_UUID => "command",
_ if uuid == DEVICE_INFO_CHAR_UUID => "device_info",
_ if uuid == EXTENDED_INFO_CHAR_UUID => "extended_info",
_ if uuid == CONFIG_CHAR_UUID => "config",
_ if uuid == CONTROL_CHAR_UUID => "control",
_ if uuid == BATTERY_LEVEL_CHAR_UUID => "battery_level",
_ if uuid == FIRMWARE_REVISION_CHAR_UUID => "firmware_revision",
_ if uuid == DEVICE_NAME_CHAR_UUID => "device_name",
_ => "unknown",
}
}
pub async fn scan_for_devices(
adapter: &btleplug::platform::Adapter,
timeout: std::time::Duration,
) -> Result<Vec<Peripheral>> {
use btleplug::api::{Central, ScanFilter};
adapter.start_scan(ScanFilter::default()).await?;
tokio::time::sleep(timeout).await;
adapter.stop_scan().await?;
let peripherals = adapter.peripherals().await?;
let mut locks = Vec::new();
for peripheral in peripherals {
if let Some(props) = peripheral.properties().await?
&& let Some(name) = props.local_name
&& name == crate::protocol::DEVICE_NAME
{
locks.push(peripheral);
}
}
Ok(locks)
}