Skip to main content

bt_channel_test_support/
lib.rs

1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use fidl::endpoints::create_proxy_and_stream;
6use fidl_fuchsia_bluetooth as fidl_bt;
7use fuchsia_bluetooth::types::Channel;
8
9#[derive(Debug, Clone, Copy)]
10pub enum Transport {
11    Socket,
12    Fidl,
13}
14
15/// Creates a pair of channels for testing.
16///
17/// Returns `(local, remote)` where:
18/// - When `transport` is `Transport::Fidl`, the first element (`local`) is a FIDL client channel
19///   (wraps a `ChannelProxy`) and the second element (`remote`) is a FIDL server channel
20///   (wraps a `ChannelRequestStream`).
21/// - When `transport` is `Transport::Socket`, the elements represent a symmetric socket pair
22///   and are not distinguished as client or server.
23pub fn create_test_channels(transport: Transport) -> (Channel, Channel) {
24    create_test_channels_with_max_tx(transport, Channel::DEFAULT_MAX_TX)
25}
26
27/// Creates a pair of channels for testing, specifying the maximum TX size.
28///
29/// Returns `(local, remote)` where:
30/// - When `transport` is `Transport::Fidl`, the first element (`local`) is a FIDL client channel
31///   (wraps a `ChannelProxy`) and is typically used as the "local" channel by the component under test.
32/// - The second element (`remote`) is a FIDL server channel (wraps a `ChannelRequestStream`)
33///   and is typically used as the "remote" channel in unit tests to simulate the remote peer.
34/// - When `transport` is `Transport::Socket`, the elements represent a symmetric socket pair
35///   and are not distinguished as client or server.
36pub fn create_test_channels_with_max_tx(
37    transport: Transport,
38    max_tx_size: usize,
39) -> (Channel, Channel) {
40    match transport {
41        Transport::Socket => Channel::create_socket_pair_with_max_tx(max_tx_size),
42        Transport::Fidl => {
43            let (proxy, stream) = create_proxy_and_stream::<fidl_bt::ChannelMarker>();
44            let client = Channel::from_fidl_client(proxy, max_tx_size);
45            let server = Channel::from_fidl_server(stream, max_tx_size);
46            (client, server)
47        }
48    }
49}