use crate::att::{self, Characteristic, Service};
use crate::gatt::{self, Connection, NotificationSink};
use crate::hci::{Advertisement as HciAdvertisement, HciSocket};
use std::collections::{BTreeSet, HashMap};
use std::sync::atomic::{AtomicBool, AtomicU16, Ordering};
use std::sync::{Arc, Mutex, Weak};
use webbluetooth_core::chooser::DeviceChooser;
use webbluetooth_core::error::{Error, Result};
use webbluetooth_core::filter::{Advertisement, RequestDeviceOptions};
use webbluetooth_core::gatt::{CharacteristicProperties, WriteType};
use webbluetooth_core::registry::DeviceRegistry;
use webbluetooth_core::scan::{self, ScanHub};
use webbluetooth_core::state::ManagerState;
use webbluetooth_core::uuid::BluetoothUuid;
#[derive(Clone, Debug)]
pub struct Handle(Arc<HandleData>);
#[derive(Debug)]
struct HandleData {
handle: u16,
uuid: BluetoothUuid,
properties: u8,
range: (u16, u16),
}
pub fn attribute_uuid(handle: &Handle) -> Result<BluetoothUuid> {
Ok(handle.0.uuid)
}
fn to_uuid(u: &att::Uuid) -> Result<BluetoothUuid> {
BluetoothUuid::parse(&u.to_canonical())
}
#[allow(dead_code)]
#[derive(Debug, Default, Clone)]
pub struct RestoredScan {
pub device_ids: Vec<String>,
pub scan_services: Vec<BluetoothUuid>,
pub scan_allow_duplicates: bool,
}
#[derive(Clone)]
struct Sighting {
address: String,
random_address: bool,
name: Option<String>,
}
struct DeviceData {
address: String,
random_address: bool,
connection: Option<Arc<Connection>>,
service_changed: Option<u16>,
services: Vec<Service>,
characteristics: HashMap<u16, Vec<Characteristic>>,
}
type Subscribers = HashMap<(String, u16), Vec<webbluetooth_core::backlog::Sender<Vec<u8>>>>;
pub struct Inner {
devices: DeviceRegistry<DeviceData>,
notifications: Mutex<Subscribers>,
cached: Mutex<HashMap<(String, u16), Vec<u8>>>,
adapter_present: bool,
controller: AtomicU16,
hub: ScanHub,
sightings: Mutex<HashMap<String, Sighting>>,
scan_stop: Mutex<Option<(Arc<AtomicBool>, std::thread::JoinHandle<()>)>>,
}
struct Notifications {
inner: Weak<Inner>,
device_id: String,
}
impl NotificationSink for Notifications {
fn on_notification(&self, handle: u16, value: Vec<u8>) {
let Some(inner) = self.inner.upgrade() else {
return;
};
let service_changed = inner
.devices
.get(&self.device_id, |d| d.inner.service_changed)
.ok()
.flatten();
if service_changed == Some(handle) {
inner.devices.mark_services_changed(&self.device_id);
return;
}
inner
.cached
.lock()
.unwrap()
.insert((self.device_id.clone(), handle), value.clone());
let mut subscribers = inner.notifications.lock().unwrap();
let key = (self.device_id.clone(), handle);
if let Some(list) = subscribers.get_mut(&key) {
list.retain(|tx| tx.send(value.clone()).is_ok());
if list.is_empty() {
subscribers.remove(&key);
}
}
}
fn on_disconnected(&self) {
let Some(inner) = self.inner.upgrade() else {
return;
};
inner.devices.update(&self.device_id, |d| {
d.inner.connection = None;
d.inner.services.clear();
d.inner.characteristics.clear();
});
inner.devices.mark_disconnected(&self.device_id);
}
}
impl Inner {
pub fn new(show_power_alert: bool) -> Arc<Self> {
Self::with_restoration(show_power_alert, None, BTreeSet::new())
}
pub fn with_restoration(
_show_power_alert: bool,
_restore_identifier: Option<&str>,
_restore_allowed: BTreeSet<BluetoothUuid>,
) -> Arc<Self> {
let adapter_present = HciSocket::open(DEFAULT_CONTROLLER).is_ok();
Arc::new(Inner {
devices: DeviceRegistry::default(),
notifications: Mutex::new(HashMap::new()),
cached: Mutex::new(HashMap::new()),
controller: AtomicU16::new(DEFAULT_CONTROLLER),
adapter_present,
hub: ScanHub::new(),
sightings: Mutex::new(HashMap::new()),
scan_stop: Mutex::new(None),
})
}
pub fn state(&self) -> ManagerState {
if self.adapter_present {
ManagerState::PoweredOn
} else {
ManagerState::Unauthorized
}
}
pub async fn settled_state(&self) -> ManagerState {
self.state()
}
pub async fn require_powered_on(&self) -> Result<()> {
self.state().require_powered_on()
}
pub fn restored(&self) -> Option<RestoredScan> {
None
}
pub fn scan_hub(&self) -> &ScanHub {
&self.hub
}
pub fn set_radio_scanning(self: &Arc<Self>, on: bool) -> Result<()> {
let mut running = self.scan_stop.lock().unwrap();
if !on {
let Some((stop, thread)) = running.take() else {
return Ok(());
};
drop(running);
stop.store(true, Ordering::Release);
if thread.thread().id() != std::thread::current().id() {
let _ = thread.join();
}
return Ok(());
}
if running.is_some() {
return Ok(());
}
let stop = Arc::new(AtomicBool::new(false));
let engine = Arc::downgrade(self);
let thread_stop = stop.clone();
let controller = self.controller.load(Ordering::Relaxed);
let thread = std::thread::Builder::new()
.name("webbluetooth-hci-scan".into())
.spawn(move || {
let Ok(socket) = HciSocket::open(controller) else {
if let Some(engine) = engine.upgrade() {
engine.hub.close_all();
}
return;
};
let mut accumulated: HashMap<String, Advertisement> = HashMap::new();
let _ = socket.scan(thread_stop, |report| {
let Some(engine) = engine.upgrade() else {
return;
};
if !engine.hub.is_watching() {
return;
}
let entry = accumulated.entry(report.address.clone()).or_default();
entry.merge(advertisement_from(&report));
let advertisement = entry.clone();
let name = advertisement.local_name.clone();
engine.sightings.lock().unwrap().insert(
report.address.clone(),
Sighting {
address: report.address.clone(),
random_address: report.random_address,
name: name.clone(),
},
);
engine
.hub
.publish(&report.address, name.as_deref(), &advertisement);
});
})
.map_err(|e| Error::Network(format!("could not start scanning: {e}")))?;
*running = Some((stop, thread));
Ok(())
}
pub async fn request_device(
self: &Arc<Self>,
options: RequestDeviceOptions,
chooser: &dyn DeviceChooser,
) -> Result<String> {
options.validate()?;
self.require_powered_on().await?;
let allowed = options.grant();
let id = scan::choose(
&self.hub,
options,
chooser,
|on| self.set_radio_scanning(on),
|| {},
)
.await?;
let Some(sighting) = self.sightings.lock().unwrap().get(&id).cloned() else {
return Err(Error::NotFound(format!(
"the chooser returned {id:?}, which was not among the devices it was offered"
)));
};
self.devices.insert(
&id,
sighting.name,
allowed,
false,
DeviceData {
address: sighting.address,
random_address: sighting.random_address,
connection: None,
service_changed: None,
services: Vec::new(),
characteristics: HashMap::new(),
},
);
Ok(id)
}
pub async fn adopt_device(
&self,
id: &str,
allowed: webbluetooth_core::registry::Grant,
) -> Result<String> {
if !webbluetooth_core::address::is_bluetooth_address(id) {
return Err(Error::NotFound(format!(
"{id:?} is not a Bluetooth address"
)));
}
let known = self.sightings.lock().unwrap().get(id).cloned();
self.devices.insert(
id,
known.as_ref().and_then(|s| s.name.clone()),
allowed,
false,
DeviceData {
address: id.to_owned(),
service_changed: None,
random_address: known.map(|s| s.random_address).unwrap_or(false),
connection: None,
services: Vec::new(),
characteristics: HashMap::new(),
},
);
Ok(id.to_owned())
}
fn connection(&self, id: &str) -> Result<Arc<Connection>> {
self.devices
.get(id, |d| d.inner.connection.clone())?
.ok_or_else(|| Error::InvalidState("the device is not connected".into()))
}
fn peer(&self, id: &str) -> Result<(String, bool)> {
self.devices
.get(id, |d| (d.inner.address.clone(), d.inner.random_address))
}
webbluetooth_core::registry::forward_to_registry!();
pub async fn phy(&self, id: &str) -> Result<webbluetooth_core::ConnectionPhy> {
let _ = self.is_connected(id);
Err(Error::NotSupported("the connection handle belongs to the kernel; PHY control would need an HCI monitor socket".into()))
}
pub async fn set_preferred_phy(
&self,
id: &str,
_tx: webbluetooth_core::Phy,
_rx: webbluetooth_core::Phy,
) -> Result<webbluetooth_core::ConnectionPhy> {
let _ = self.is_connected(id);
Err(Error::NotSupported("the connection handle belongs to the kernel; PHY control would need an HCI monitor socket".into()))
}
pub async fn pair(&self, id: &str) -> Result<webbluetooth_core::Pairing> {
let _ = self.is_connected(id);
Err(Error::NotSupported(
"pairing needs a Security Manager, which lives in bluetoothd; use the \
BlueZ backend for a device that requires encryption"
.into(),
))
}
pub async fn is_paired(&self, id: &str) -> Result<bool> {
let _ = self.is_connected(id);
Ok(false)
}
pub async fn adapters(&self) -> Result<Vec<webbluetooth_core::AdapterInfo>> {
let current = self.controller.load(Ordering::Relaxed);
let found = crate::hci::devices().map_err(Error::NotSupported)?;
Ok(found
.into_iter()
.map(|d| webbluetooth_core::AdapterInfo {
is_default: d.id == current,
id: d.name.clone(),
name: Some(d.name),
address: Some(d.address),
powered: d.powered,
})
.collect())
}
pub async fn adapter(&self) -> Result<webbluetooth_core::AdapterInfo> {
let adapters = self.adapters().await?;
adapters
.into_iter()
.find(webbluetooth_core::AdapterInfo::is_default)
.ok_or(Error::NotAvailable(
webbluetooth_core::Availability::Unsupported,
))
}
pub async fn select_adapter(&self, id: &str) -> Result<()> {
let adapters = self.adapters().await?;
let Some(found) = adapters.iter().find(|a| a.id == id) else {
return Err(Error::NotFound(format!(
"no controller called {id:?}; this system has {}",
adapters
.iter()
.map(|a| a.id.as_str())
.collect::<Vec<_>>()
.join(", ")
)));
};
let index = found
.id
.strip_prefix("hci")
.and_then(|n| n.parse::<u16>().ok())
.ok_or_else(|| Error::NotFound(format!("{id:?} is not an hciN controller name")))?;
self.controller.store(index, Ordering::Relaxed);
Ok(())
}
pub async fn connection_parameters(
&self,
id: &str,
) -> Result<webbluetooth_core::ConnectionParameters> {
let _ = self.is_connected(id);
Err(Error::NotSupported(
"the kernel owns the LE link; negotiated parameters are only in the \
HCI connection-complete event"
.into(),
))
}
async fn gatt_lock(&self, id: &str) -> Result<futures_util::lock::OwnedMutexGuard<()>> {
self.devices.gatt_lock(id).await
}
pub fn forget(&self, id: &str) {
if let Some(device) = self.devices.remove(id) {
if let Some(connection) = device.inner.connection {
connection.close();
}
}
}
pub fn service_is_primary(&self, _handle: &Handle) -> bool {
true
}
pub fn characteristic_properties(&self, handle: &Handle) -> CharacteristicProperties {
CharacteristicProperties(handle.0.properties as u32)
}
pub fn cached_value(&self, handle: &Handle) -> Option<Vec<u8>> {
let key = handle.0.handle;
self.cached
.lock()
.unwrap()
.iter()
.find(|((_, h), _)| *h == key)
.map(|(_, v)| v.clone())
}
pub fn is_notifying(&self, handle: &Handle) -> bool {
let key = handle.0.handle;
self.notifications
.lock()
.unwrap()
.keys()
.any(|(_, h)| *h == key)
}
pub async fn connect(self: &Arc<Self>, id: &str) -> Result<()> {
self.require_powered_on().await?;
if self.is_connected(id) {
return Ok(());
}
let (address, random) = self.peer(id)?;
let sink = Arc::new(Notifications {
inner: Arc::downgrade(self),
device_id: id.to_owned(),
});
let connection =
Connection::open(&address, random, sink).map_err(|e| Error::Network(e.to_string()))?;
let connection = Arc::new(connection);
let services = connection
.discover_services()
.map_err(|e| Error::Network(format!("service discovery failed: {e}")))?;
let service_changed = subscribe_to_service_changed(&connection, &services);
self.devices.update(id, |d| {
d.connected = true;
d.inner.connection = Some(connection);
d.inner.services = services;
d.inner.characteristics.clear();
d.inner.service_changed = service_changed;
});
Ok(())
}
pub fn disconnect(&self, id: &str) {
if let Ok(Some(connection)) = self.devices.get(id, |d| d.inner.connection.clone()) {
connection.close();
}
}
pub async fn discover_services(
&self,
id: &str,
uuid: Option<&BluetoothUuid>,
) -> Result<Vec<Handle>> {
let _guard = self.gatt_lock(id).await?;
if !self.is_connected(id) {
return Err(Error::InvalidState("the device is not connected".into()));
}
let services = self.devices.get(id, |d| d.inner.services.clone())?;
Ok(services
.into_iter()
.filter_map(|s| {
let found = to_uuid(&s.uuid).ok()?;
match uuid {
Some(want) if *want != found => None,
_ => Some(Handle(Arc::new(HandleData {
handle: s.start_handle,
uuid: found,
properties: 0,
range: (s.start_handle, s.end_handle),
}))),
}
})
.collect())
}
pub async fn discover_included_services(
&self,
id: &str,
service: &Handle,
uuid: Option<&BluetoothUuid>,
) -> Result<Vec<Handle>> {
let _guard = self.gatt_lock(id).await?;
let connection = self.connection(id)?;
let (start, end) = service.0.range;
let declared = Service {
start_handle: start,
end_handle: end,
uuid: att::Uuid::Short(0),
};
let found = connection
.discover_included_services(&declared)
.map_err(|e| Error::Network(format!("included service discovery failed: {e}")))?;
let mut out = Vec::new();
for included in found {
let Ok(u) = to_uuid(&included.uuid) else {
continue;
};
if uuid.is_some_and(|want| *want != u) {
continue;
}
out.push(Handle(Arc::new(HandleData {
handle: included.start_handle,
uuid: u,
properties: 0,
range: (included.start_handle, included.end_handle),
})));
}
Ok(out)
}
pub async fn discover_characteristics(
&self,
id: &str,
service: &Handle,
uuid: Option<&BluetoothUuid>,
) -> Result<Vec<Handle>> {
let _guard = self.gatt_lock(id).await?;
let connection = self.connection(id)?;
let (start, end) = service.0.range;
let cached = self
.devices
.get(id, |d| d.inner.characteristics.get(&start).cloned())?;
let characteristics = match cached {
Some(c) => c,
None => {
let service = Service {
start_handle: start,
end_handle: end,
uuid: att::Uuid::Short(0),
};
let found = connection
.discover_characteristics(&service)
.map_err(|e| Error::Network(format!("characteristic discovery failed: {e}")))?;
self.devices.update(id, |d| {
d.inner.characteristics.insert(start, found.clone());
});
found
}
};
let mut out = Vec::new();
for (i, c) in characteristics.iter().enumerate() {
let owns_until = characteristics
.get(i + 1)
.map(|next| next.handle - 1)
.unwrap_or(end);
let Ok(found) = to_uuid(&c.uuid) else {
continue;
};
if let Some(want) = uuid {
if *want != found {
continue;
}
}
out.push(Handle(Arc::new(HandleData {
handle: c.value_handle,
uuid: found,
properties: c.properties,
range: (c.value_handle + 1, owns_until),
})));
}
Ok(out)
}
pub async fn discover_descriptors(
&self,
id: &str,
characteristic: &Handle,
uuid: Option<&BluetoothUuid>,
) -> Result<Vec<Handle>> {
let _guard = self.gatt_lock(id).await?;
let connection = self.connection(id)?;
let (start, end) = characteristic.0.range;
let descriptors = connection
.discover_descriptors(start, end)
.map_err(|e| Error::Network(format!("descriptor discovery failed: {e}")))?;
Ok(descriptors
.into_iter()
.filter_map(|d| {
let found = to_uuid(&d.uuid).ok()?;
match uuid {
Some(want) if *want != found => None,
_ => Some(Handle(Arc::new(HandleData {
handle: d.handle,
uuid: found,
properties: 0,
range: (d.handle, d.handle),
}))),
}
})
.collect())
}
pub async fn read_characteristic(&self, id: &str, handle: &Handle) -> Result<Vec<u8>> {
let _guard = self.gatt_lock(id).await?;
let connection = self.connection(id)?;
let value = connection
.read(handle.0.handle)
.map_err(|e| map_error("read", e))?;
self.cached
.lock()
.unwrap()
.insert((id.to_owned(), handle.0.handle), value.clone());
Ok(value)
}
pub async fn write_characteristic(
&self,
id: &str,
handle: &Handle,
value: &[u8],
write_type: WriteType,
) -> Result<()> {
let _guard = match write_type {
WriteType::WithResponse => Some(self.gatt_lock(id).await?),
WriteType::WithoutResponse => None,
};
let connection = self.connection(id)?;
match write_type {
WriteType::WithResponse => connection.write(handle.0.handle, value),
WriteType::WithoutResponse => connection.write_command(handle.0.handle, value),
}
.map_err(|e| map_error("write", e))
}
pub async fn set_notify(&self, id: &str, handle: &Handle, enabled: bool) -> Result<bool> {
let _guard = self.gatt_lock(id).await?;
let connection = self.connection(id)?;
let (start, end) = handle.0.range;
let descriptors = connection
.discover_descriptors(start, end)
.map_err(|e| map_error("descriptor discovery", e))?;
let cccd = crate::gatt::find_cccd(&descriptors).ok_or_else(|| {
Error::NotSupported(
"this characteristic has no client characteristic configuration descriptor".into(),
)
})?;
let properties = CharacteristicProperties(handle.0.properties as u32);
connection
.set_notify(
cccd,
enabled && properties.notify(),
enabled && properties.indicate(),
)
.map_err(|e| map_error("subscribe", e))?;
Ok(enabled)
}
pub fn subscribe(&self, handle: &Handle) -> webbluetooth_core::backlog::Receiver<Vec<u8>> {
let (tx, rx) = webbluetooth_core::backlog::channel();
let mut subscribers = self.notifications.lock().unwrap();
for id in self.devices.ids() {
subscribers
.entry((id, handle.0.handle))
.or_default()
.push(tx.clone());
}
rx
}
pub async fn read_descriptor(&self, id: &str, handle: &Handle) -> Result<Vec<u8>> {
let _guard = self.gatt_lock(id).await?;
let connection = self.connection(id)?;
connection
.read(handle.0.handle)
.map_err(|e| map_error("descriptor read", e))
}
pub async fn write_descriptor(&self, id: &str, handle: &Handle, value: &[u8]) -> Result<()> {
let _guard = self.gatt_lock(id).await?;
let connection = self.connection(id)?;
connection
.write(handle.0.handle, value)
.map_err(|e| map_error("descriptor write", e))
}
pub async fn read_rssi(&self, _id: &str) -> Result<i32> {
Err(Error::NotSupported(
"connected RSSI is not available without bluetoothd; the scan reports it instead"
.into(),
))
}
pub async fn request_connection_priority(
&self,
_id: &str,
_priority: webbluetooth_core::ConnectionPriority,
) -> Result<()> {
Err(Error::NotSupported(
"the kernel owns the ACL link, so an LE Connection Update sent \
behind its back would desynchronise it"
.into(),
))
}
pub async fn request_mtu(&self, _id: &str, _mtu: u16) -> Result<()> {
Err(Error::NotSupported(
"the raw HCI backend negotiates ATT MTU during connection setup".into(),
))
}
pub fn max_write_len(&self, id: &str, _write_type: WriteType) -> Result<usize> {
let connection = self.connection(id)?;
Ok(connection.mtu().saturating_sub(3) as usize)
}
pub async fn l2cap_target(&self, id: &str, _psm: u16) -> Result<(String, String)> {
let (address, random) = self.peer(id)?;
let kind = if random { "random" } else { "public" };
Ok((address, kind.to_string()))
}
}
fn map_error(what: &str, e: crate::gatt::Error) -> Error {
use crate::gatt::Error as G;
match &e {
G::Att(code) => match code.0 {
att::AttError::READ_NOT_PERMITTED | att::AttError::WRITE_NOT_PERMITTED => {
Error::Security(format!("{what} refused: {code}"))
}
att::AttError::REQUEST_NOT_SUPPORTED | att::AttError::ATTRIBUTE_NOT_LONG => {
Error::NotSupported(format!("{what}: {code}"))
}
att::AttError::INVALID_ATTRIBUTE_VALUE_LENGTH => {
Error::InvalidModification(format!("{what}: {code}"))
}
att::AttError::INSUFFICIENT_AUTHENTICATION
| att::AttError::INSUFFICIENT_ENCRYPTION
| att::AttError::INSUFFICIENT_AUTHORIZATION => Error::Security(format!(
"{what} needs an encrypted link, and pairing needs bluetoothd — \
build without the linux-hci feature to use it"
)),
_ => Error::Network(format!("{what} failed: {code}")),
},
G::Timeout => Error::Timeout(format!("{what} timed out")),
G::Disconnected => Error::InvalidState("the link dropped".into()),
other => Error::Network(format!("{what} failed: {other}")),
}
}
fn advertisement_from(report: &HciAdvertisement) -> Advertisement {
let mut manufacturer_data = HashMap::new();
if let Some((company, payload)) = &report.manufacturer_data {
manufacturer_data.insert(*company, payload.clone());
}
Advertisement {
#[cfg(feature = "classic")]
class_of_device: None,
local_name: report.local_name.clone(),
tx_power: report.tx_power.map(i16::from),
appearance: report.appearance,
is_connectable: Some(report.connectable),
service_uuids: report
.service_uuids
.iter()
.filter_map(|u| BluetoothUuid::parse(u).ok())
.collect(),
overflow_service_uuids: Vec::new(),
solicited_service_uuids: Vec::new(),
manufacturer_data,
service_data: report
.service_data
.iter()
.filter_map(|(u, d)| Some((BluetoothUuid::parse(u).ok()?, d.clone())))
.collect(),
rssi: report.rssi as i32,
raw: Some(report.raw.clone()),
}
}
fn subscribe_to_service_changed(connection: &Connection, services: &[Service]) -> Option<u16> {
const GENERIC_ATTRIBUTE: u16 = 0x1801;
const SERVICE_CHANGED: u16 = 0x2A05;
let service = services
.iter()
.find(|s| s.uuid == att::Uuid::Short(GENERIC_ATTRIBUTE))?;
let characteristic = connection
.discover_characteristics(service)
.ok()?
.into_iter()
.find(|c| c.uuid == att::Uuid::Short(SERVICE_CHANGED))?;
let cccd = gatt::find_cccd(
&connection
.discover_descriptors(characteristic.value_handle + 1, service.end_handle)
.ok()?,
)?;
connection.set_notify(cccd, false, true).ok()?;
Some(characteristic.value_handle)
}
const DEFAULT_CONTROLLER: u16 = 0;
pub fn authorization() -> webbluetooth_core::Authorization {
match HciSocket::open(DEFAULT_CONTROLLER) {
Ok(_) => webbluetooth_core::Authorization::Allowed,
Err(_) => webbluetooth_core::Authorization::Denied,
}
}