1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_bluetooth_sapphire/internal/host/l2cap/enhanced_retransmission_mode_engines.h"
16
17 namespace bt::l2cap::internal {
18
19 // This factory function ensures that their states are properly linked and
20 // allows for unit tests of their linked behaviors without adding dependencies
21 // between the classes nor tie their lifetimes together. If tying their
22 // lifetimes together is desired, one should perhaps hold reference counts on
23 // the other to preserve the independence.
24 std::pair<std::unique_ptr<EnhancedRetransmissionModeRxEngine>,
25 std::unique_ptr<EnhancedRetransmissionModeTxEngine>>
MakeLinkedEnhancedRetransmissionModeEngines(ChannelId channel_id,uint16_t max_tx_sdu_size,uint8_t max_transmissions,uint8_t n_frames_in_tx_window,EnhancedRetransmissionModeTxEngine::SendFrameCallback send_frame_callback,EnhancedRetransmissionModeTxEngine::ConnectionFailureCallback connection_failure_callback,pw::async::Dispatcher & dispatcher)26 MakeLinkedEnhancedRetransmissionModeEngines(
27 ChannelId channel_id,
28 uint16_t max_tx_sdu_size,
29 uint8_t max_transmissions,
30 uint8_t n_frames_in_tx_window,
31 EnhancedRetransmissionModeTxEngine::SendFrameCallback send_frame_callback,
32 EnhancedRetransmissionModeTxEngine::ConnectionFailureCallback
33 connection_failure_callback,
34 pw::async::Dispatcher& dispatcher) {
35 auto rx_engine = std::make_unique<EnhancedRetransmissionModeRxEngine>(
36 send_frame_callback.share(), connection_failure_callback.share());
37 auto tx_engine = std::make_unique<EnhancedRetransmissionModeTxEngine>(
38 channel_id,
39 max_tx_sdu_size,
40 max_transmissions,
41 n_frames_in_tx_window,
42 send_frame_callback.share(),
43 std::move(connection_failure_callback),
44 dispatcher);
45
46 // The direction swap here is because our acknowledgment sequence is based on
47 // the peer's transmit sequence and vice versa.
48 rx_engine->set_receive_seq_num_callback(
49 fit::bind_member<&EnhancedRetransmissionModeTxEngine::UpdateAckSeq>(
50 tx_engine.get()));
51 rx_engine->set_ack_seq_num_callback(
52 fit::bind_member<&EnhancedRetransmissionModeTxEngine::UpdateReqSeq>(
53 tx_engine.get()));
54 rx_engine->set_remote_busy_set_callback(
55 fit::bind_member<&EnhancedRetransmissionModeTxEngine::SetRemoteBusy>(
56 tx_engine.get()));
57 rx_engine->set_remote_busy_cleared_callback(
58 fit::bind_member<&EnhancedRetransmissionModeTxEngine::ClearRemoteBusy>(
59 tx_engine.get()));
60 rx_engine->set_single_retransmit_set_callback(
61 fit::bind_member<
62 &EnhancedRetransmissionModeTxEngine::SetSingleRetransmit>(
63 tx_engine.get()));
64 rx_engine->set_range_retransmit_set_callback(
65 fit::bind_member<&EnhancedRetransmissionModeTxEngine::SetRangeRetransmit>(
66 tx_engine.get()));
67 return {std::move(rx_engine), std::move(tx_engine)};
68 }
69
70 } // namespace bt::l2cap::internal
71