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