Skip to main content

rs_matter_stack/wireless/
gatt.rs

1use core::future::Future;
2
3use rs_matter::error::Error;
4
5use crate::ble::GattPeripheral;
6
7use super::PreexistingWireless;
8
9/// A trait representing a task that needs access to the BLE GATT peripheral to perform its work
10/// (e.g. the first part of a non-concurrent commissioning flow)
11pub trait GattTask {
12    /// Run the task with the given GATT peripheral
13    async fn run<P>(&mut self, peripheral: P) -> Result<(), Error>
14    where
15        P: GattPeripheral;
16}
17
18impl<T> GattTask for &mut T
19where
20    T: GattTask,
21{
22    fn run<P>(&mut self, peripheral: P) -> impl Future<Output = Result<(), Error>>
23    where
24        P: GattPeripheral,
25    {
26        T::run(*self, peripheral)
27    }
28}
29
30/// A trait for running a task within a context where the BLE peripheral is initialized and operable
31/// (e.g. the first part of a non-concurrent commissioning workflow)
32pub trait Gatt {
33    /// Setup the radio to operate in BLE mode and run the given task.
34    async fn run<T>(&mut self, task: T) -> Result<(), Error>
35    where
36        T: GattTask;
37}
38
39impl<T> Gatt for &mut T
40where
41    T: Gatt,
42{
43    fn run<A>(&mut self, task: A) -> impl Future<Output = Result<(), Error>>
44    where
45        A: GattTask,
46    {
47        T::run(self, task)
48    }
49}
50
51impl<U, N, C, M, P> Gatt for PreexistingWireless<U, N, C, M, P>
52where
53    P: GattPeripheral,
54{
55    async fn run<T>(&mut self, mut task: T) -> Result<(), Error>
56    where
57        T: GattTask,
58    {
59        task.run(&mut self.gatt).await
60    }
61}