1 //! Mocked implementation of GattDatabaseCallbacks for use in test
2 
3 use std::ops::RangeInclusive;
4 
5 use crate::{
6     core::shared_box::{WeakBox, WeakBoxRef},
7     gatt::{
8         ids::{AttHandle, TransportIndex},
9         server::{
10             att_server_bearer::AttServerBearer,
11             gatt_database::{AttDatabaseImpl, GattDatabaseCallbacks},
12         },
13     },
14 };
15 use tokio::sync::mpsc::{self, unbounded_channel, UnboundedReceiver};
16 
17 /// Routes calls to GattDatabaseCallbacks into a channel of MockCallbackEvents
18 pub struct MockCallbacks(mpsc::UnboundedSender<MockCallbackEvents>);
19 
20 impl MockCallbacks {
21     /// Constructor. Returns self and the RX side of the associated channel.
new() -> (Self, UnboundedReceiver<MockCallbackEvents>)22     pub fn new() -> (Self, UnboundedReceiver<MockCallbackEvents>) {
23         let (tx, rx) = unbounded_channel();
24         (Self(tx), rx)
25     }
26 }
27 
28 /// Events representing calls to GattCallbacks
29 pub enum MockCallbackEvents {
30     /// GattDatabaseCallbacks#on_le_connect invoked
31     OnLeConnect(TransportIndex, WeakBox<AttServerBearer<AttDatabaseImpl>>),
32     /// GattDatabaseCallbacks#on_le_disconnect invoked
33     OnLeDisconnect(TransportIndex),
34     /// GattDatabaseCallbacks#on_service_change invoked
35     OnServiceChange(RangeInclusive<AttHandle>),
36 }
37 
38 impl GattDatabaseCallbacks for MockCallbacks {
on_le_connect( &self, tcb_idx: TransportIndex, bearer: WeakBoxRef<AttServerBearer<AttDatabaseImpl>>, )39     fn on_le_connect(
40         &self,
41         tcb_idx: TransportIndex,
42         bearer: WeakBoxRef<AttServerBearer<AttDatabaseImpl>>,
43     ) {
44         self.0.send(MockCallbackEvents::OnLeConnect(tcb_idx, bearer.downgrade())).ok().unwrap();
45     }
46 
on_le_disconnect(&self, tcb_idx: TransportIndex)47     fn on_le_disconnect(&self, tcb_idx: TransportIndex) {
48         self.0.send(MockCallbackEvents::OnLeDisconnect(tcb_idx)).ok().unwrap();
49     }
50 
on_service_change(&self, range: RangeInclusive<AttHandle>)51     fn on_service_change(&self, range: RangeInclusive<AttHandle>) {
52         self.0.send(MockCallbackEvents::OnServiceChange(range)).ok().unwrap();
53     }
54 }
55