• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Mocked implementation of GattCallbacks for use in test
2 
3 use crate::gatt::callbacks::{GattWriteType, TransactionDecision};
4 use crate::gatt::ffi::AttributeBackingType;
5 use crate::gatt::ids::{AttHandle, ConnectionId, TransactionId};
6 use crate::gatt::server::IndicationError;
7 use crate::gatt::GattCallbacks;
8 use tokio::sync::mpsc::{self, unbounded_channel, UnboundedReceiver};
9 
10 /// Routes calls to GattCallbacks into a channel of MockCallbackEvents
11 pub struct MockCallbacks(mpsc::UnboundedSender<MockCallbackEvents>);
12 
13 impl MockCallbacks {
14     /// Constructor. Returns self and the RX side of the associated channel.
new() -> (Self, UnboundedReceiver<MockCallbackEvents>)15     pub fn new() -> (Self, UnboundedReceiver<MockCallbackEvents>) {
16         let (tx, rx) = unbounded_channel();
17         (Self(tx), rx)
18     }
19 }
20 
21 /// Events representing calls to GattCallbacks
22 #[derive(Debug)]
23 pub enum MockCallbackEvents {
24     /// GattCallbacks#on_server_read_characteristic invoked
25     OnServerRead(ConnectionId, TransactionId, AttHandle, AttributeBackingType, u32),
26     /// GattCallbacks#on_server_write_characteristic invoked
27     OnServerWrite(
28         ConnectionId,
29         TransactionId,
30         AttHandle,
31         AttributeBackingType,
32         GattWriteType,
33         Vec<u8>,
34     ),
35     /// GattCallbacks#on_indication_sent_confirmation invoked
36     OnIndicationSentConfirmation(ConnectionId, Result<(), IndicationError>),
37     /// GattCallbacks#on_execute invoked
38     OnExecute(ConnectionId, TransactionId, TransactionDecision),
39 }
40 
41 impl GattCallbacks for MockCallbacks {
on_server_read( &self, conn_id: ConnectionId, trans_id: TransactionId, handle: AttHandle, attr_type: AttributeBackingType, offset: u32, )42     fn on_server_read(
43         &self,
44         conn_id: ConnectionId,
45         trans_id: TransactionId,
46         handle: AttHandle,
47         attr_type: AttributeBackingType,
48         offset: u32,
49     ) {
50         self.0
51             .send(MockCallbackEvents::OnServerRead(conn_id, trans_id, handle, attr_type, offset))
52             .unwrap();
53     }
54 
on_server_write( &self, conn_id: ConnectionId, trans_id: TransactionId, handle: AttHandle, attr_type: AttributeBackingType, write_type: GattWriteType, value: &[u8], )55     fn on_server_write(
56         &self,
57         conn_id: ConnectionId,
58         trans_id: TransactionId,
59         handle: AttHandle,
60         attr_type: AttributeBackingType,
61         write_type: GattWriteType,
62         value: &[u8],
63     ) {
64         self.0
65             .send(MockCallbackEvents::OnServerWrite(
66                 conn_id,
67                 trans_id,
68                 handle,
69                 attr_type,
70                 write_type,
71                 value.to_vec(),
72             ))
73             .unwrap();
74     }
75 
on_indication_sent_confirmation( &self, conn_id: ConnectionId, result: Result<(), IndicationError>, )76     fn on_indication_sent_confirmation(
77         &self,
78         conn_id: ConnectionId,
79         result: Result<(), IndicationError>,
80     ) {
81         self.0.send(MockCallbackEvents::OnIndicationSentConfirmation(conn_id, result)).unwrap();
82     }
83 
on_execute( &self, conn_id: ConnectionId, trans_id: TransactionId, decision: TransactionDecision, )84     fn on_execute(
85         &self,
86         conn_id: ConnectionId,
87         trans_id: TransactionId,
88         decision: TransactionDecision,
89     ) {
90         self.0.send(MockCallbackEvents::OnExecute(conn_id, trans_id, decision)).unwrap()
91     }
92 }
93