bt_gatt/
lib.rs

1// Copyright 2023 The Sapphire 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
5pub mod types;
6pub use types::{Characteristic, Descriptor, Result};
7
8pub mod server;
9pub use server::Server;
10
11pub mod client;
12pub use client::Client;
13
14pub mod central;
15pub use central::Central;
16
17pub mod periodic_advertising;
18pub mod pii;
19
20#[cfg(any(test, feature = "test-utils"))]
21pub mod test_utils;
22
23#[cfg(test)]
24mod tests;
25
26use futures::{Future, Stream};
27
28use crate::periodic_advertising::PeriodicAdvertising;
29
30/// Implementors implement traits with respect to GattTypes.
31/// Implementation crates provide an object which relates a constellation of
32/// types to each other, used to provide concrete types for library crates to
33/// paramaterize on.
34pub trait GattTypes: Sized {
35    // Types related to finding and connecting to peers
36    type Central: Central<Self>;
37    type ScanResultStream: Stream<Item = Result<central::ScanResult>> + 'static;
38    type Client: Client<Self>;
39    type ConnectFuture: Future<Output = Result<Self::Client>>;
40
41    // Types related to finding and connecting to services
42    type PeerServiceHandle: client::PeerServiceHandle<Self>;
43    type FindServicesFut: Future<Output = Result<Vec<Self::PeerServiceHandle>>> + 'static;
44    type PeerService: client::PeerService<Self>;
45    type ServiceConnectFut: Future<Output = Result<Self::PeerService>>;
46
47    // Types related to interacting with services
48    /// Future returned by PeerService::discover_characteristics,
49    /// delivering the set of characteristics discovered within a service
50    type CharacteristicDiscoveryFut: Future<Output = Result<Vec<Characteristic>>>;
51    /// A stream of notifications, delivering updates to a characteristic value
52    /// that has been subscribed to.  See [`client::PeerService::subscribe`]
53    type NotificationStream: Stream<Item = Result<client::CharacteristicNotification>> + 'static;
54    /// Future resolving when a characteristic or descriptor has been read.
55    /// Resolves to a number of bytes read along with a boolean indicating if
56    /// the value was possibly truncated, or an Error if the value could not
57    /// be read.
58    type ReadFut<'a>: Future<Output = Result<(usize, bool)>> + 'a;
59    /// Future resolving when a characteristic or descriptor has been written.
60    /// Returns an error if the value could not be written.
61    type WriteFut<'a>: Future<Output = Result<()>> + 'a;
62    /// The implementation of periodic advertising for this GATT implementation.
63    type PeriodicAdvertising: PeriodicAdvertising;
64}
65
66/// Servers and services are defined with respect to ServerTypes.
67/// Implementation crates provide an object which relates this constellation of
68/// types to each other, used to provide concrete types for library crates to
69/// paramaterize on.
70pub trait ServerTypes: Sized {
71    type Server: Server<Self>;
72    type LocalService: server::LocalService<Self>;
73    type LocalServiceFut: Future<Output = Result<Self::LocalService>>;
74    type ServiceEventStream: Stream<Item = Result<server::ServiceEvent<Self>>>;
75    type ServiceWriteType: ToOwned<Owned = Vec<u8>>;
76    type ReadResponder: server::ReadResponder;
77    type WriteResponder: server::WriteResponder;
78
79    type IndicateConfirmationStream: Stream<Item = Result<server::ConfirmationEvent>>;
80}