frenz 0.2.1

Async client for the FRENZ brainband over Bluetooth LE: streams, commands, lossless recording
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! Discovering, connecting to, and commanding a band.

use std::collections::HashMap;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, AtomicI64, Ordering};
use std::sync::{Arc, Mutex, PoisonError};
use std::time::Duration;

use btleplug::api::ValueNotification;
use btleplug::api::{
    Central, CentralEvent, CharPropFlags, Characteristic as BleCharacteristic, Manager as _, Peripheral as _,
    ScanFilter, WriteType,
};
use btleplug::platform::{Adapter, Manager, Peripheral, PeripheralId};
use frenz_proto::{Characteristic, Command, Packet, Response, ResponseBody};
use futures::{Stream, StreamExt};
use tokio::sync::{RwLock, broadcast, mpsc};
use tokio::task::JoinHandle;

use crate::clock::{mono_ns, wall_ns};
use crate::error::Error;
use crate::event::{Decoder, Event, Health, RawRecord};

/// Capacity of the event and raw-record broadcast channels (≈ 3 minutes of all streams).
const CHANNEL_CAPACITY: usize = 1 << 14;

/// A band seen while scanning.
#[derive(Debug, Clone)]
pub struct Discovered {
    /// Advertised name.
    pub name: String,
    /// Signal strength at discovery.
    pub rssi: Option<i16>,
}

/// What to send after subscribing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StartSequence {
    /// The standard start sequence (verified on hardware): firmware query, time sync,
    /// prompt volume 0, periodic heart rate, on-connected, stream start, IMU calibration query.
    #[default]
    Standard,
    /// Send nothing.
    None,
}

/// Connection options.
#[derive(Debug, Clone)]
pub struct ConnectOptions {
    /// How long to scan for the named band.
    pub scan_timeout: Duration,
    /// Commands sent after subscribing.
    pub start: StartSequence,
    /// How long to wait for a command response.
    pub command_timeout: Duration,
    /// After the link is lost, keep trying to reconnect for this long (`None`: do not reconnect).
    /// The band powers itself off about 33 s after being taken off, so a reconnect only succeeds
    /// if it is still on (or is switched on again) within this time.
    pub reconnect: Option<Duration>,
}

impl Default for ConnectOptions {
    fn default() -> Self {
        Self {
            scan_timeout: Duration::from_secs(30),
            start: StartSequence::default(),
            command_timeout: Duration::from_secs(2),
            reconnect: Some(Duration::from_secs(300)),
        }
    }
}

/// Facts about the connected band.
#[derive(Debug, Clone, Default)]
pub struct BandInfo {
    /// Advertised name.
    pub name: String,
    /// Firmware version string (from the start sequence).
    pub firmware: Option<String>,
    /// IMU calibration report `(std, diff)` (from the start sequence).
    pub imu_calibration: Option<([u8; 3], [u16; 3])>,
    /// GATT table at connect: `(service, characteristic, properties)`.
    pub gatt: Vec<(String, String, String)>,
}

async fn first_adapter() -> Result<Adapter, Error> {
    Manager::new()
        .await?
        .adapters()
        .await?
        .into_iter()
        .next()
        .ok_or(Error::NoAdapter)
}

async fn frenz_peripherals(adapter: &Adapter) -> Result<Vec<(Discovered, Peripheral)>, Error> {
    let mut found = Vec::new();
    for p in adapter.peripherals().await? {
        if let Some(props) = p.properties().await?
            && let Some(name) = props.local_name
            && name.to_ascii_uppercase().starts_with("FRENZ")
        {
            found.push((Discovered { name, rssi: props.rssi }, p));
        }
    }
    Ok(found)
}

/// Scans for bands for `duration`, strongest signal first.
pub async fn scan(duration: Duration) -> Result<Vec<Discovered>, Error> {
    let adapter = first_adapter().await?;
    adapter.start_scan(ScanFilter::default()).await?;
    tokio::time::sleep(duration).await;
    let mut found: Vec<_> = frenz_peripherals(&adapter).await?.into_iter().map(|(d, _)| d).collect();
    adapter.stop_scan().await.ok();
    found.sort_by_key(|d| std::cmp::Reverse(d.rssi.unwrap_or(i16::MIN)));
    Ok(found)
}

/// With no notification at all for this long, the link is checked and treated as lost if down.
const STALL: Duration = Duration::from_secs(10);

type Notifications = Pin<Box<dyn Stream<Item = ValueNotification> + Send>>;
type CentralEvents = Pin<Box<dyn Stream<Item = CentralEvent> + Send>>;

struct Link {
    peripheral: Peripheral,
    characteristics: HashMap<Characteristic, BleCharacteristic>,
}

enum ToDecoder {
    Notification(i64, Characteristic, Arc<[u8]>),
    /// The link was re-established: sequence tracking starts afresh.
    Reset,
}

/// State shared by the handle and its background tasks.
struct Shared {
    name: String,
    options: ConnectOptions,
    events: broadcast::Sender<Event>,
    raw: broadcast::Sender<RawRecord>,
    health: Mutex<Health>,
    last_start_ns: AtomicI64,
    info: Mutex<BandInfo>,
    link: RwLock<Option<Link>>,
    closing: AtomicBool,
    /// Identity of the device first connected to; reconnection accepts only this device.
    device: Mutex<Option<PeripheralId>>,
}

impl Shared {
    fn mark(&self, label: &str) {
        let _ = self.raw.send(RawRecord::Marker {
            mono_ns: mono_ns(),
            wall_ns: wall_ns(),
            label: label.to_owned(),
        });
    }

    fn with_health<R>(&self, f: impl FnOnce(&mut Health) -> R) -> R {
        f(&mut self.health.lock().unwrap_or_else(PoisonError::into_inner))
    }

    /// Scans for the band, connects, and subscribes to its streams.
    async fn establish(&self, scan_timeout: Duration) -> Result<(Notifications, CentralEvents, PeripheralId), Error> {
        let adapter = first_adapter().await?;
        adapter.start_scan(ScanFilter::default()).await?;
        let deadline = tokio::time::Instant::now() + scan_timeout;
        // After the first connection, only the same physical device is accepted: a name can be
        // advertised by anything.
        let pinned = self.device.lock().unwrap_or_else(PoisonError::into_inner).clone();
        let peripheral = loop {
            if let Some((_, p)) = frenz_peripherals(&adapter)
                .await?
                .into_iter()
                .find(|(d, p)| d.name == self.name && pinned.as_ref().is_none_or(|id| *id == p.id()))
            {
                break p;
            }
            if tokio::time::Instant::now() >= deadline {
                adapter.stop_scan().await.ok();
                return Err(Error::NotFound {
                    name: self.name.clone(),
                    timeout: scan_timeout,
                });
            }
            tokio::time::sleep(Duration::from_millis(250)).await;
        };
        adapter.stop_scan().await.ok();
        let connect_timeout = Duration::from_secs(15);
        tokio::time::timeout(connect_timeout, peripheral.connect())
            .await
            .map_err(|_| Error::NotFound {
                name: self.name.clone(),
                timeout: connect_timeout,
            })??;
        peripheral.discover_services().await?;

        let mut characteristics = HashMap::new();
        let mut gatt = Vec::new();
        for c in peripheral.characteristics() {
            gatt.push((
                c.service_uuid.to_string(),
                c.uuid.to_string(),
                format!("{:?}", c.properties),
            ));
            if let Some(known) = Characteristic::from_uuid(c.uuid.as_u128()) {
                characteristics.insert(known, c);
            }
        }
        for required in Characteristic::NOTIFY.into_iter().chain([Characteristic::CommandRx]) {
            if !characteristics.contains_key(&required) {
                return Err(Error::MissingCharacteristic(required.name()));
            }
        }
        // Obtain the notification stream before subscribing so no early notification is missed.
        let notifications = peripheral.notifications().await?;
        let central_events = adapter.events().await?;
        let id = peripheral.id();
        self.device
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .get_or_insert_with(|| id.clone());
        self.info.lock().unwrap_or_else(PoisonError::into_inner).gatt = gatt;
        self.mark("connected");
        // Subscribe only to the band's own notify characteristics; other services drop the link.
        for c in Characteristic::NOTIFY {
            if let Some(ble) = characteristics.get(&c)
                && ble.properties.contains(CharPropFlags::NOTIFY)
            {
                peripheral.subscribe(ble).await?;
            }
        }
        self.mark("subscribed_all");
        *self.link.write().await = Some(Link {
            peripheral,
            characteristics,
        });
        self.with_health(|h| h.connected = true);
        Ok((notifications, central_events, id))
    }

    async fn send(&self, command: Command) -> Result<(), Error> {
        let link = self.link.read().await;
        let link = link.as_ref().ok_or(Error::Disconnected)?;
        let rx = link
            .characteristics
            .get(&Characteristic::CommandRx)
            .ok_or(Error::MissingCharacteristic("command_rx"))?;
        let encoded = command.encode();
        if matches!(command, Command::StreamControl { enable: true, .. }) {
            self.last_start_ns.store(mono_ns(), Ordering::Relaxed);
        }
        let result = link
            .peripheral
            .write(rx, encoded.as_bytes(), WriteType::WithoutResponse)
            .await;
        let _ = self.raw.send(RawRecord::Write {
            mono_ns: mono_ns(),
            wall_ns: wall_ns(),
            label: format!("{command:?}"),
            bytes: encoded.as_bytes().into(),
            error: result.as_ref().err().map(ToString::to_string),
        });
        Ok(result?)
    }

    async fn request(&self, command: Command) -> Result<Response, Error> {
        let mut events = self.events.subscribe();
        self.send(command).await?;
        let opcode = command.opcode();
        let timeout = self.options.command_timeout;
        let response = tokio::time::timeout(timeout, async {
            loop {
                match events.recv().await {
                    Ok(Event::Packet { packet, .. }) => {
                        if let Packet::Response(r) = packet.as_ref()
                            && r.opcode == opcode
                        {
                            return Ok(r.clone());
                        }
                    }
                    Ok(Event::Disconnected { .. }) | Err(broadcast::error::RecvError::Closed) => {
                        return Err(Error::Disconnected);
                    }
                    Ok(_) | Err(broadcast::error::RecvError::Lagged(_)) => {}
                }
            }
        })
        .await
        .map_err(|_| Error::Timeout { opcode, timeout })??;
        if response.ok() {
            Ok(response)
        } else {
            Err(Error::CommandFailed {
                opcode,
                status: response.status,
            })
        }
    }

    async fn run_start_sequence(&self) -> Result<(), Error> {
        let now = u32::try_from(wall_ns() / 1_000_000_000).unwrap_or(u32::MAX);
        let pause = Duration::from_millis(100);
        match self.options.start {
            StartSequence::None => return Ok(()),
            StartSequence::Standard => {
                if let Ok(r) = self.request(Command::GetFirmwareVersion).await
                    && let Ok(ResponseBody::FirmwareVersion(v)) = r.body()
                {
                    self.info.lock().unwrap_or_else(PoisonError::into_inner).firmware = Some(v.to_owned());
                }
                self.send(Command::TimeSync(now)).await?;
                self.send(Command::SetPromptVolume(0)).await?;
                tokio::time::sleep(pause).await;
                self.send(Command::HeartRatePeriodic(true)).await?;
                tokio::time::sleep(pause).await;
                self.send(Command::OnConnected).await?;
                self.send(Command::StreamControl {
                    mask: 0xff,
                    enable: true,
                })
                .await?;
                tokio::time::sleep(pause).await;
            }
        }
        if let Ok(r) = self.request(Command::GetImuCalibration).await
            && let Ok(ResponseBody::ImuCalibration { std, diff }) = r.body()
        {
            self.info.lock().unwrap_or_else(PoisonError::into_inner).imu_calibration = Some((std, diff));
        }
        self.mark("start_sequence_sent");
        Ok(())
    }
}

/// Forwards notifications until the link is lost; then, if enabled, reconnects and carries on.
async fn supervise(
    shared: Arc<Shared>,
    mut notifications: Notifications,
    mut central_events: CentralEvents,
    mut id: PeripheralId,
    to_decoder: mpsc::UnboundedSender<ToDecoder>,
) {
    loop {
        // Arrival path: timestamp, copy, hand off. No parsing, no blocking I/O.
        loop {
            tokio::select! {
                n = notifications.next() => {
                    let Some(n) = n else { break };
                    let (mono, wall) = (mono_ns(), wall_ns());
                    let Some(characteristic) = Characteristic::from_uuid(n.uuid.as_u128()) else { continue };
                    let bytes: Arc<[u8]> = n.value.into();
                    let _ = shared.raw.send(RawRecord::Notification { mono_ns: mono, wall_ns: wall, characteristic, bytes: bytes.clone() });
                    if to_decoder.send(ToDecoder::Notification(mono, characteristic, bytes)).is_err() {
                        return;
                    }
                }
                e = central_events.next() => match e {
                    Some(CentralEvent::DeviceDisconnected(d)) if d == id => break,
                    Some(_) => {}
                    None => break,
                },
                () = tokio::time::sleep(STALL) => {
                    let link = shared.link.read().await;
                    let alive = match link.as_ref() {
                        Some(l) => l.peripheral.is_connected().await.unwrap_or(false),
                        None => false,
                    };
                    if !alive { break }
                }
            }
        }
        if let Some(link) = shared.link.write().await.take() {
            link.peripheral.disconnect().await.ok();
        }
        shared.with_health(|h| h.connected = false);
        shared.mark("disconnected");
        let _ = shared.events.send(Event::Disconnected { mono_ns: mono_ns() });
        let Some(window) = shared
            .options
            .reconnect
            .filter(|_| !shared.closing.load(Ordering::Relaxed))
        else {
            return;
        };

        let deadline = tokio::time::Instant::now() + window;
        let mut attempt = 0u32;
        loop {
            if shared.closing.load(Ordering::Relaxed) {
                return;
            }
            let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
            if remaining.is_zero() {
                shared.mark("reconnect_gave_up");
                let _ = shared.events.send(Event::ReconnectFailed { mono_ns: mono_ns() });
                return;
            }
            attempt += 1;
            let _ = shared.events.send(Event::Reconnecting {
                mono_ns: mono_ns(),
                attempt,
            });
            match shared.establish(remaining.min(Duration::from_secs(10))).await {
                Ok((n, c, i)) => {
                    notifications = n;
                    central_events = c;
                    id = i;
                    break;
                }
                Err(_) => {
                    let backoff = Duration::from_secs(u64::from(attempt.min(5)));
                    tokio::time::sleep(backoff.min(deadline.saturating_duration_since(tokio::time::Instant::now())))
                        .await;
                }
            }
        }
        let _ = to_decoder.send(ToDecoder::Reset);
        shared.with_health(|h| h.reconnects += 1);
        shared.mark("reconnected");
        // The start sequence waits for responses, which this task delivers, so run it alongside.
        let again = shared.clone();
        tokio::spawn(async move {
            again.run_start_sequence().await.ok();
            let _ = again.events.send(Event::Reconnected { mono_ns: mono_ns() });
        });
    }
}

/// A connected band.
///
/// If the link drops, the band is reconnected automatically (see [`ConnectOptions::reconnect`]);
/// subscribers, recorders and this handle keep working across the interruption. Dropping a `Band`
/// stops its background tasks but does not send the stop sequence; call [`Band::stop`] for an
/// orderly end.
pub struct Band {
    shared: Arc<Shared>,
    tasks: Vec<JoinHandle<()>>,
    /// Receiver created before subscribing, so the first recorder sees everything since connect.
    initial_raw: Mutex<Option<broadcast::Receiver<RawRecord>>>,
}

impl Band {
    /// Scans for a band advertising `name`, connects, subscribes to all of its streams and runs the
    /// configured start sequence.
    pub async fn connect(name: &str, options: ConnectOptions) -> Result<Self, Error> {
        let (events, _) = broadcast::channel(CHANNEL_CAPACITY);
        let (raw, initial_raw) = broadcast::channel(CHANNEL_CAPACITY);
        let scan_timeout = options.scan_timeout;
        let shared = Arc::new(Shared {
            name: name.to_owned(),
            options,
            events,
            raw,
            health: Mutex::new(Health::default()),
            last_start_ns: AtomicI64::new(i64::MIN / 2),
            info: Mutex::new(BandInfo {
                name: name.to_owned(),
                ..BandInfo::default()
            }),
            link: RwLock::new(None),
            closing: AtomicBool::new(false),
            device: Mutex::new(None),
        });
        let (notifications, central_events, id) = shared.establish(scan_timeout).await?;

        let (to_decoder, mut decoder_rx) = mpsc::unbounded_channel::<ToDecoder>();
        let mut tasks = Vec::new();
        let decoding = shared.clone();
        tasks.push(tokio::spawn(async move {
            let mut decoder = Decoder::default();
            let mut out = Vec::new();
            while let Some(message) = decoder_rx.recv().await {
                match message {
                    ToDecoder::Reset => decoder.reset(),
                    ToDecoder::Notification(mono, characteristic, bytes) => {
                        let last_start = decoding.last_start_ns.load(Ordering::Relaxed);
                        decoding.with_health(|h| decoder.handle(mono, characteristic, &bytes, last_start, h, &mut out));
                        for event in out.drain(..) {
                            let _ = decoding.events.send(event);
                        }
                    }
                }
            }
        }));
        tasks.push(tokio::spawn(supervise(
            shared.clone(),
            notifications,
            central_events,
            id,
            to_decoder,
        )));
        let band = Self {
            shared,
            tasks,
            initial_raw: Mutex::new(Some(initial_raw)),
        };
        band.shared.run_start_sequence().await?;
        Ok(band)
    }

    /// Facts about the band gathered at connect.
    pub fn info(&self) -> BandInfo {
        self.shared.info.lock().unwrap_or_else(PoisonError::into_inner).clone()
    }

    /// Snapshot of the connection's counters.
    pub fn health(&self) -> Health {
        self.shared.with_health(|h| h.clone())
    }

    /// Subscribes to decoded events. A subscriber that falls more than ~16k events behind receives
    /// `RecvError::Lagged` with the number of events it missed.
    pub fn subscribe(&self) -> broadcast::Receiver<Event> {
        self.shared.events.subscribe()
    }

    /// Subscribes to raw records (notifications, writes, markers) in arrival order, from now on.
    pub fn subscribe_raw(&self) -> broadcast::Receiver<RawRecord> {
        self.shared.raw.subscribe()
    }

    /// Like [`Band::subscribe_raw`], but the first call receives every record since the connection
    /// was set up (the connect markers, start sequence and first notifications). Later calls
    /// subscribe from now on. Recorders use this so a recording is complete.
    pub fn subscribe_raw_from_start(&self) -> broadcast::Receiver<RawRecord> {
        self.initial_raw
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .take()
            .unwrap_or_else(|| self.shared.raw.subscribe())
    }

    /// Adds a marker to the raw record stream.
    pub fn mark(&self, label: &str) {
        self.shared.mark(label);
    }

    /// Writes a command without waiting for a response. Fails with [`Error::Disconnected`] while
    /// the link is down.
    pub async fn send(&self, command: Command) -> Result<(), Error> {
        self.shared.send(command).await
    }

    /// Writes a command and waits for the response with the same opcode.
    pub async fn request(&self, command: Command) -> Result<Response, Error> {
        self.shared.request(command).await
    }

    /// Queries the battery percentage.
    pub async fn battery(&self) -> Result<u8, Error> {
        match self.request(Command::GetBattery).await?.body() {
            Ok(ResponseBody::Battery(p)) => Ok(p),
            _ => Err(Error::CommandFailed {
                opcode: 0x02,
                status: 0xff,
            }),
        }
    }

    /// Stops streaming, sends the session-end notice, and disconnects. No reconnection follows.
    pub async fn stop(mut self) -> Result<Health, Error> {
        self.shared.closing.store(true, Ordering::Relaxed);
        if self.health().connected {
            self.send(Command::StreamControl {
                mask: 0xff,
                enable: false,
            })
            .await
            .ok();
            tokio::time::sleep(Duration::from_millis(100)).await;
            self.send(Command::EndSession).await.ok();
            tokio::time::sleep(Duration::from_millis(500)).await;
            self.mark("stop_sequence_sent");
            if let Some(link) = self.shared.link.write().await.take() {
                link.peripheral.disconnect().await.ok();
            }
        }
        tokio::time::sleep(Duration::from_millis(200)).await;
        for task in self.tasks.drain(..) {
            task.abort();
        }
        Ok(self.health())
    }
}

impl Drop for Band {
    fn drop(&mut self) {
        self.shared.closing.store(true, Ordering::Relaxed);
        for task in &self.tasks {
            task.abort();
        }
    }
}