Skip to main content

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};
14
15use crate::central::AdvertisingDatum;
16use thiserror::Error;
17
18use bt_common::PeerId;
19
20#[derive(Error, Debug)]
21#[non_exhaustive]
22pub enum Error {
23    #[error("Periodic Advertising Sync failed to establish")]
24    SyncEstablishFailed,
25    #[error("Periodic Advertising Sync lost")]
26    SyncLost,
27    #[error("I/O error")]
28    Io,
29}
30
31/// A trait for managing a periodic advertising.
32pub trait PeriodicAdvertising {
33    type SyncFut: Future<Output = crate::Result<Self::SyncStream>>;
34    type SyncStream: Stream<Item = crate::Result<SyncReport>>;
35
36    /// Request to sync to periodic advertising resports.
37    /// On success, returns the SyncStream which can be used to receive
38    /// SyncReports.
39    fn sync_to_advertising_reports(
40        &self,
41        peer_id: PeerId,
42        advertising_sid: u8,
43        config: SyncConfiguration,
44    ) -> Self::SyncFut;
45
46    // TODO(b/340885203): Add a method to sync to subevents.
47}
48
49#[derive(Debug, Clone)]
50pub struct SyncConfiguration {
51    /// Filter out duplicate advertising reports.
52    /// Optional.
53    /// Default: true
54    pub filter_duplicates: bool,
55}
56
57#[derive(Debug, Clone)]
58pub struct PeriodicAdvertisingReport {
59    pub rssi: i8,
60    pub data: Vec<AdvertisingDatum>,
61    /// The event counter of the event that the advertising packet was received
62    /// in.
63    pub event_counter: Option<u16>,
64    /// The subevent number of the report. Only present if the packet was
65    /// received in a subevent.
66    pub subevent: Option<u8>,
67    pub timestamp: i64,
68}
69
70#[derive(Debug, Clone)]
71pub struct BroadcastIsochronousGroupInfo {
72    /// The number of Broadcast Isochronous Streams in this group.
73    /// The specification calls this "num_bis".
74    pub streams_count: u8,
75    /// The time interval of the periodic SDUs.
76    pub sdu_interval: i64,
77    /// The maximum size of an SDU.
78    pub max_sdu_size: u16,
79    /// The PHY used for transmission of data.
80    pub phy: Phy,
81    /// Indicates whether the BIG is encrypted.
82    pub encryption: bool,
83}
84
85#[derive(Debug, Clone)]
86pub struct BroadcastIsochronousGroupInfoReport {
87    pub info: BroadcastIsochronousGroupInfo,
88    pub timestamp: i64,
89}
90
91#[derive(Debug, Clone)]
92pub enum SyncReport {
93    PeriodicAdvertisingReport(PeriodicAdvertisingReport),
94    BroadcastIsochronousGroupInfoReport(BroadcastIsochronousGroupInfoReport),
95}