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