1use crate::expect::{expect_call, Status};
6use anyhow::Error;
7use fidl::endpoints::ServerEnd;
8use fidl_fuchsia_bluetooth::PeerId;
9use log::info;
10use zx::MonotonicDuration;
11use {fidl_fuchsia_bluetooth_gatt2 as fidl_gatt2, fidl_fuchsia_bluetooth_le as fidl_le};
12
13pub struct CentralMock {
15 stream: fidl_le::CentralRequestStream,
16 timeout: zx::MonotonicDuration,
17}
18
19impl CentralMock {
20 pub fn new(timeout: MonotonicDuration) -> (fidl_le::CentralProxy, Self) {
21 let (proxy, stream) = fidl::endpoints::create_proxy_and_stream::<fidl_le::CentralMarker>();
22 (proxy, Self { stream, timeout })
23 }
24
25 pub fn from_stream(stream: fidl_le::CentralRequestStream, timeout: MonotonicDuration) -> Self {
26 Self { stream, timeout }
27 }
28
29 pub async fn expect_connect(
30 &mut self,
31 expected_peer_id: Option<PeerId>,
32 ) -> Result<(fidl_le::ConnectionOptions, ServerEnd<fidl_le::ConnectionMarker>), Error> {
33 expect_call(&mut self.stream, self.timeout, move |req| match req {
34 fidl_le::CentralRequest::Connect { id, options, handle, .. } => {
35 if let Some(match_id) = expected_peer_id {
36 assert_eq!(match_id, id);
37 };
38 Ok(Status::Satisfied((options, handle)))
39 }
40 x => {
41 info!("Received unexpected Central Request {x:?}");
42 Ok(Status::Pending)
43 }
44 })
45 .await
46 }
47}
48
49pub struct ConnectionMock {
51 stream: fidl_le::ConnectionRequestStream,
52 timeout: zx::MonotonicDuration,
53}
54
55impl ConnectionMock {
56 pub fn new(timeout: MonotonicDuration) -> (fidl_le::ConnectionProxy, Self) {
57 let (proxy, stream) =
58 fidl::endpoints::create_proxy_and_stream::<fidl_le::ConnectionMarker>();
59 (proxy, Self { stream, timeout })
60 }
61
62 pub fn from_stream(
63 stream: fidl_le::ConnectionRequestStream,
64 timeout: MonotonicDuration,
65 ) -> Self {
66 Self { stream, timeout }
67 }
68
69 pub async fn expect_request_gatt_client(
70 &mut self,
71 ) -> Result<ServerEnd<fidl_gatt2::ClientMarker>, Error> {
72 expect_call(&mut self.stream, self.timeout, move |req| match req {
73 fidl_le::ConnectionRequest::RequestGattClient { client, .. } => {
74 Ok(Status::Satisfied(client))
75 }
76 x => {
77 info!("Received unexpected Connection request: {x:?}");
78 Ok(Status::Pending)
79 }
80 })
81 .await
82 }
83}