webbluetooth 0.0.5

The Web Bluetooth API in Rust — Apple platforms, Linux, Android, Windows and the browser
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! End-to-end tests of the Linux backend against a mocked BlueZ.
//!
//! No container has a Bluetooth controller, so `docker/mock-bluez.py` publishes
//! the exact D-Bus interfaces `bluetoothd` would. Everything below is real
//! traffic over a real bus — `GetManagedObjects`, discovery filters, `Connect`,
//! `ReadValue`, `WriteValue`, `StartNotify`, `PropertiesChanged` — against the
//! public API, with nothing stubbed on the Rust side.
//!
//! Skipped unless a bus is configured, so `cargo test` elsewhere still passes.
//! Run them with `./scripts/test-linux.sh`.

// The mock serves org.bluez, so these exercise the BlueZ backend specifically.
// With `linux-hci` the crate talks to a controller instead, which no container
// has.
#![cfg(all(target_os = "linux", not(feature = "linux-hci")))]

use futures_executor::block_on;
use futures_util::StreamExt;
use std::time::Duration;
use webbluetooth::uuid::{characteristics, services, BluetoothUuid};
use webbluetooth::{Bluetooth, DeviceFilter, Error, RequestDeviceOptions, Result};

const MOCK_ADDRESS: &str = "AA:BB:CC:DD:EE:FF";
const CONTROL_POINT: BluetoothUuid = characteristics::HEART_RATE_CONTROL_POINT;

/// There is exactly one mock device, so these tests cannot overlap: one of them
/// disconnects it, which every other connected client sees. Serialising here
/// rather than relying on `--test-threads=1` makes the constraint hold however
/// the suite is invoked.
///
/// The lock is deliberately poison-tolerant — a panicking test should fail on
/// its own terms, not take the rest down with it.
static MOCK_DEVICE: std::sync::Mutex<()> = std::sync::Mutex::new(());

fn exclusive() -> std::sync::MutexGuard<'static, ()> {
    MOCK_DEVICE
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
}

fn have_bus() -> bool {
    std::env::var("WEBBLUETOOTH_DBUS_ADDRESS").is_ok()
        || std::env::var("DBUS_SYSTEM_BUS_ADDRESS").is_ok()
}

#[cfg(feature = "rfcomm")]
#[test]
fn bluez_registers_and_unregisters_a_classic_profile() {
    if !have_bus() {
        return;
    }
    let _mock = exclusive();
    let bluetooth = Bluetooth::new();
    let registration = block_on(bluetooth.register_classic_profile(
        BluetoothUuid::from_u16(0x1101),
        webbluetooth::ClassicSecurity::Authentication,
    ))
    .expect("RegisterProfile failed against the mock BlueZ");
    drop(registration);
}

/// Scan for the mock device and take the grant.
async fn granted() -> Result<(Bluetooth, webbluetooth::BluetoothDevice)> {
    let bluetooth = Bluetooth::new();
    bluetooth
        .availability()
        .await
        .map_err(Error::NotAvailable)?;
    let device = bluetooth
        .request_device(
            RequestDeviceOptions::new()
                .filter(DeviceFilter::new().service(services::HEART_RATE)?)
                .optional_service(services::BATTERY_SERVICE)?,
        )
        .await?;
    Ok((bluetooth, device))
}

#[test]
fn the_adapter_is_reachable() {
    if !have_bus() {
        return;
    }
    let _mock = exclusive();
    let bluetooth = Bluetooth::new();
    assert!(
        block_on(bluetooth.get_availability()),
        "mock adapter not usable"
    );
    assert_eq!(
        webbluetooth::authorization(),
        webbluetooth::Authorization::Allowed
    );
    // BlueZ does support the peripheral role, via a GATT application
    // registered with bluetoothd. `dual_role.rs` checks the constant against
    // the module; here it only has to be true.
    const { assert!(webbluetooth::PERIPHERAL_ROLE) };
}

#[test]
fn a_scan_finds_the_device_and_matches_on_its_service() {
    if !have_bus() {
        return;
    }
    let _mock = exclusive();
    let (_bluetooth, device) = block_on(granted()).expect("request_device failed");
    // On Linux the id is the Bluetooth address — BlueZ exposes it, Apple never
    // does, and this is the one visible API difference between the two.
    assert_eq!(device.id(), MOCK_ADDRESS);
    assert_eq!(device.name().as_deref(), Some("Mock Heart Rate"));
}

#[test]
fn a_filter_that_matches_nothing_finds_nothing() {
    if !have_bus() {
        return;
    }
    let _mock = exclusive();
    let bluetooth = Bluetooth::with_chooser(webbluetooth::chooser::FirstMatch::with_timeout(
        Duration::from_millis(600),
    ));
    let result = block_on(async {
        bluetooth
            .request_device(
                RequestDeviceOptions::new()
                    .filter(DeviceFilter::new().name_prefix("Nothing Called This")),
            )
            .await
    });
    assert!(matches!(result, Err(Error::NotFound(_))), "got {result:?}");
}

#[test]
fn connect_resolves_services_and_reads_a_characteristic() {
    if !have_bus() {
        return;
    }
    let _mock = exclusive();
    block_on(async {
        let (_bluetooth, device) = granted().await.expect("request_device failed");
        let gatt = device.gatt();
        gatt.connect().await.expect("connect failed");
        assert!(gatt.connected());

        let service = gatt
            .get_primary_service(services::HEART_RATE)
            .await
            .expect("no heart rate service");
        assert_eq!(
            service.uuid().as_str(),
            "0000180d-0000-1000-8000-00805f9b34fb"
        );
        assert!(service.is_primary());

        let measurement = service
            .get_characteristic(characteristics::HEART_RATE_MEASUREMENT)
            .await
            .expect("no measurement characteristic");

        // BlueZ reports properties as flag strings; they must arrive as bits.
        let props = measurement.properties();
        assert!(props.read(), "read flag lost in translation");
        assert!(props.notify(), "notify flag lost in translation");
        assert!(!props.write());

        let value = measurement.read_value().await.expect("ReadValue failed");
        assert_eq!(value, vec![0x00, 72], "wrong bytes off the wire");

        // A second characteristic, to prove discovery is not returning one thing.
        let location = service
            .get_characteristic(characteristics::BODY_SENSOR_LOCATION)
            .await
            .expect("no body sensor location");
        assert_eq!(
            location.read_value().await.expect("ReadValue failed"),
            vec![0x02]
        );
    });
}

#[test]
fn reading_a_write_only_characteristic_is_refused_locally() {
    if !have_bus() {
        return;
    }
    let _mock = exclusive();
    block_on(async {
        let (_bluetooth, device) = granted().await.expect("request_device failed");
        device.gatt().connect().await.expect("connect failed");
        let service = device
            .gatt()
            .get_primary_service(services::HEART_RATE)
            .await
            .unwrap();
        let control = service
            .get_characteristic(CONTROL_POINT)
            .await
            .expect("no control point");

        // The peer is never asked: the properties say it cannot be read.
        let result = control.read_value().await;
        assert!(
            matches!(result, Err(Error::NotSupported(_))),
            "got {result:?}"
        );
    });
}

#[test]
fn a_write_reaches_the_peer() {
    if !have_bus() {
        return;
    }
    let _mock = exclusive();
    block_on(async {
        let (_bluetooth, device) = granted().await.expect("request_device failed");
        device.gatt().connect().await.expect("connect failed");
        let service = device
            .gatt()
            .get_primary_service(services::HEART_RATE)
            .await
            .unwrap();
        let control = service
            .get_characteristic(CONTROL_POINT)
            .await
            .expect("no control point");

        control
            .write_value_with_response(&[0x01])
            .await
            .expect("WriteValue failed");
        // The mock stores what it was given, so reading the property back
        // proves the bytes crossed the bus intact.
        assert_eq!(control.value(), Some(vec![0x01]));

        control
            .write_value_without_response(&[0x02])
            .await
            .expect("command write failed");
    });
}

#[test]
fn notifications_arrive_as_property_changes() {
    if !have_bus() {
        return;
    }
    let _mock = exclusive();
    block_on(async {
        let (_bluetooth, device) = granted().await.expect("request_device failed");
        device.gatt().connect().await.expect("connect failed");
        let service = device
            .gatt()
            .get_primary_service(services::HEART_RATE)
            .await
            .unwrap();
        let measurement = service
            .get_characteristic(characteristics::HEART_RATE_MEASUREMENT)
            .await
            .unwrap();

        let mut notifications = measurement
            .start_notifications()
            .await
            .expect("StartNotify failed");

        // The mock pushes one value when notifications are enabled.
        let received = webbluetooth::timeout(Duration::from_secs(5), notifications.next()).await;
        let value = received
            .expect("timed out waiting for a notification")
            .expect("stream ended");
        assert_eq!(value, vec![0x00, 77]);
        assert!(measurement.is_notifying());

        measurement
            .stop_notifications()
            .await
            .expect("StopNotify failed");
    });
}

#[test]
fn a_descriptor_reads() {
    if !have_bus() {
        return;
    }
    let _mock = exclusive();
    block_on(async {
        let (_bluetooth, device) = granted().await.expect("request_device failed");
        device.gatt().connect().await.expect("connect failed");
        let service = device
            .gatt()
            .get_primary_service(services::HEART_RATE)
            .await
            .unwrap();
        let measurement = service
            .get_characteristic(characteristics::HEART_RATE_MEASUREMENT)
            .await
            .unwrap();

        // The CCCD is readable but write-blocklisted; reading it must work.
        let descriptors = measurement.get_descriptors().await.expect("no descriptors");
        assert!(!descriptors.is_empty(), "the CCCD should be discoverable");

        let cccd = measurement
            .get_descriptor(webbluetooth::uuid::descriptors::CLIENT_CHARACTERISTIC_CONFIGURATION)
            .await
            .expect("no CCCD");
        assert_eq!(
            cccd.read_value().await.expect("descriptor read failed"),
            vec![0, 0]
        );

        // …and writing it is refused before any traffic, because that would
        // bypass start_notifications().
        let refused = cccd.write_value(&[0x01, 0x00]).await;
        assert!(
            matches!(refused, Err(Error::Security(_))),
            "got {refused:?}"
        );
    });
}

#[test]
fn the_service_allowlist_is_enforced() {
    if !have_bus() {
        return;
    }
    let _mock = exclusive();
    block_on(async {
        let (_bluetooth, device) = granted().await.expect("request_device failed");
        device.gatt().connect().await.expect("connect failed");

        // Generic Attribute was never requested, so it is a SecurityError even
        // though the device might well offer it.
        let refused = device
            .gatt()
            .get_primary_service(services::GENERIC_ATTRIBUTE)
            .await;
        assert!(
            matches!(refused, Err(Error::Security(_))),
            "got {refused:?}"
        );

        // The blocklist is enforced on the same path.
        let blocked = device
            .gatt()
            .get_primary_service(services::HUMAN_INTERFACE_DEVICE)
            .await;
        assert!(
            matches!(blocked, Err(Error::Security(_))),
            "got {blocked:?}"
        );
    });
}

#[test]
fn handles_go_stale_after_a_disconnect() {
    if !have_bus() {
        return;
    }
    let _mock = exclusive();
    block_on(async {
        let (_bluetooth, device) = granted().await.expect("request_device failed");
        let gatt = device.gatt();
        gatt.connect().await.expect("connect failed");
        let service = gatt
            .get_primary_service(services::HEART_RATE)
            .await
            .unwrap();
        let measurement = service
            .get_characteristic(characteristics::HEART_RATE_MEASUREMENT)
            .await
            .unwrap();
        assert!(measurement.read_value().await.is_ok());

        let mut disconnected = device.watch_disconnect();
        gatt.disconnect();
        let _ = webbluetooth::timeout(Duration::from_secs(5), disconnected.next()).await;

        // Every handle taken before the disconnect is now invalid. The
        // specification checks `gatt.connected` first, so the reason given is
        // the lost connection rather than the stale handle.
        let stale = measurement.read_value().await;
        assert!(matches!(stale, Err(Error::Network(_))), "got {stale:?}");
    });
}

#[test]
fn granted_devices_are_listed_and_can_be_forgotten() {
    if !have_bus() {
        return;
    }
    let _mock = exclusive();
    block_on(async {
        let (bluetooth, device) = granted().await.expect("request_device failed");
        let id = device.id().to_owned();
        assert!(bluetooth.get_devices().iter().any(|d| d.id() == id));

        device.forget();
        assert!(!bluetooth.get_devices().iter().any(|d| d.id() == id));
    });
}