Skip to main content

bluetooth_host/
probe.rs

1//! Naming a controller without starting a host: reset it and read who made
2//! it and what it can do. Bounded and harmless: it sends only Reset and
3//! read commands, and closes the transport when done.
4
5use std::time::Duration;
6
7use serde::Serialize;
8
9use crate::error::{Error, Result};
10use crate::host::{Host, HostConfig};
11use crate::transport::TransportSpec;
12
13/// What a probe found.
14#[derive(Debug, Clone, Serialize)]
15pub struct ProbeResult {
16    /// The controller's public address, if it has one.
17    pub address: Option<String>,
18    /// Manufacturer company identifier.
19    pub manufacturer: u16,
20    /// Its name, from the assigned numbers.
21    pub manufacturer_name: Option<&'static str>,
22    /// HCI version number.
23    pub hci_version: u8,
24    /// The Core Specification that version is.
25    pub version_name: Option<&'static str>,
26    /// LMP/LL subversion (identifies the firmware build).
27    pub lmp_subversion: u16,
28    /// LE features by name.
29    pub le_features: Vec<&'static str>,
30    /// Advertising sets.
31    pub adv_sets: u8,
32}
33
34/// Probes the controller at `spec`, giving up after `timeout`.
35pub async fn probe(spec: &TransportSpec, timeout: Duration) -> Result<ProbeResult> {
36    let host = tokio::time::timeout(timeout, Host::open(spec, HostConfig::default()))
37        .await
38        .map_err(|_| Error::Timeout("the controller to answer a probe"))??;
39    let c = host.capabilities();
40    Ok(ProbeResult {
41        address: (c.public_address != [0; 6])
42            .then(|| crate::Address::public(c.public_address).to_string()),
43        manufacturer: c.manufacturer,
44        manufacturer_name: c.manufacturer_name(),
45        hci_version: c.hci_version,
46        version_name: c.version_name(),
47        lmp_subversion: c.lmp_subversion,
48        le_features: c.le_feature_names(),
49        adv_sets: c.adv_sets,
50    })
51}