bt_gatt/
periodic_advertising.rs

1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Contains traits that are used to synchronize to Periodic Advertisements,
6//! i.e. the GAP Central role role defined in the Bluetooth Core
7//! Specification (5.4, Volume 3 Part C Section 2.2.2)
8//!
9//! These traits should be implemented outside this crate, conforming to the
10//! types and structs here when necessary.
11
12use bt_common::core::Phy;
13use futures::{Future, Stream};
14use thiserror::Error;
15
16use bt_common::PeerId;
17
18#[derive(Error, Debug)]
19#[non_exhaustive]
20pub enum Error {
21    #[error("Periodic Advertising Sync failed to establish")]
22    SyncEstablishFailed,
23    #[error("Periodic Advertising Sync lost")]
24    SyncLost,
25    #[error("I/O error")]
26    Io,
27}
28
29/// A trait for managing a periodic advertising.
30pub trait PeriodicAdvertising {
31    type SyncFut: Future<Output = crate::Result<Self::SyncStream>>;
32    type SyncStream: Stream<Item = crate::Result<SyncReport>>;
33
34    /// Request to sync to periodic advertising resports.
35    /// On success, returns the SyncStream which can be used to receive
36    /// SyncReports.
37    fn sync_to_advertising_reports(
38        peer_id: PeerId,
39        advertising_sid: u8,
40        config: SyncConfiguration,
41    ) -> Self::SyncFut;
42
43    // TODO(b/340885203): Add a method to sync to subevents.
44}
45
46#[derive(Debug, Clone)]
47pub struct SyncConfiguration {
48    /// Filter out duplicate advertising reports.
49    /// Optional.
50    /// Default: true
51    pub filter_duplicates: bool,
52}
53
54#[derive(Debug, Clone)]
55pub struct PeriodicAdvertisingReport {
56    pub rssi: i8,
57    pub data: Vec<u8>,
58    /// The event counter of the event that the advertising packet was received
59    /// in.
60    pub event_counter: Option<u16>,
61    /// The subevent number of the report. Only present if the packet was
62    /// received in a subevent.
63    pub subevent: Option<u8>,
64    pub timestamp: i64,
65}
66
67#[derive(Debug, Clone)]
68pub struct BroadcastIsochronousGroupInfo {
69    /// The number of Broadcast Isochronous Streams in this group.
70    /// The specification calls this "num_bis".
71    pub streams_count: u8,
72    /// The time interval of the periodic SDUs.
73    pub sdu_interval: i64,
74    /// The maximum size of an SDU.
75    pub max_sdu_size: u16,
76    /// The PHY used for transmission of data.
77    pub phy: Phy,
78    /// Indicates whether the BIG is encrypted.
79    pub encryption: bool,
80}
81
82#[derive(Debug, Clone)]
83pub struct BroadcastIsochronousGroupInfoReport {
84    pub info: BroadcastIsochronousGroupInfo,
85    pub timestamp: i64,
86}
87
88#[derive(Debug, Clone)]
89pub enum SyncReport {
90    PeriodicAdvertisingReport(PeriodicAdvertisingReport),
91    BroadcastIsochronousGroupInfoReport(BroadcastIsochronousGroupInfoReport),
92}