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/fake_channel.h"
16
17 #include "pw_bluetooth_sapphire/internal/host/common/host_error.h"
18 #include "pw_bluetooth_sapphire/internal/host/common/log.h"
19
20 namespace bt::l2cap::testing {
21
FakeChannel(ChannelId id,ChannelId remote_id,hci_spec::ConnectionHandle handle,bt::LinkType link_type,ChannelInfo info,uint16_t max_tx_queued)22 FakeChannel::FakeChannel(ChannelId id,
23 ChannelId remote_id,
24 hci_spec::ConnectionHandle handle,
25 bt::LinkType link_type,
26 ChannelInfo info,
27 uint16_t max_tx_queued)
28 : Channel(id, remote_id, link_type, handle, info, max_tx_queued),
29 handle_(handle),
30 fragmenter_(handle),
31 activate_fails_(false),
32 link_error_(false),
33 acl_priority_fails_(false),
34 weak_fake_chan_(this) {}
35
Receive(const ByteBuffer & data)36 void FakeChannel::Receive(const ByteBuffer& data) {
37 auto pdu =
38 fragmenter_.BuildFrame(id(), data, FrameCheckSequenceOption::kNoFcs);
39 auto sdu = std::make_unique<DynamicByteBuffer>(pdu.length());
40 pdu.Copy(sdu.get());
41 if (rx_cb_) {
42 rx_cb_(std::move(sdu));
43 } else {
44 pending_rx_sdus_.push(std::move(sdu));
45 }
46 }
47
SetSendCallback(SendCallback callback)48 void FakeChannel::SetSendCallback(SendCallback callback) {
49 send_cb_ = std::move(callback);
50 }
51
SetSendCallback(SendCallback callback,pw::async::Dispatcher & dispatcher)52 void FakeChannel::SetSendCallback(SendCallback callback,
53 pw::async::Dispatcher& dispatcher) {
54 SetSendCallback(std::move(callback));
55 send_dispatcher_.emplace(dispatcher);
56 }
57
SetLinkErrorCallback(LinkErrorCallback callback)58 void FakeChannel::SetLinkErrorCallback(LinkErrorCallback callback) {
59 link_err_cb_ = std::move(callback);
60 }
61
SetSecurityCallback(SecurityUpgradeCallback callback,pw::async::Dispatcher & dispatcher)62 void FakeChannel::SetSecurityCallback(SecurityUpgradeCallback callback,
63 pw::async::Dispatcher& dispatcher) {
64 security_cb_ = std::move(callback);
65 security_dispatcher_.emplace(dispatcher);
66 }
67
Close()68 void FakeChannel::Close() {
69 if (closed_cb_)
70 closed_cb_();
71 }
72
Activate(RxCallback rx_callback,ClosedCallback closed_callback)73 bool FakeChannel::Activate(RxCallback rx_callback,
74 ClosedCallback closed_callback) {
75 BT_DEBUG_ASSERT(rx_callback);
76 BT_DEBUG_ASSERT(closed_callback);
77 BT_DEBUG_ASSERT(!rx_cb_);
78 BT_DEBUG_ASSERT(!closed_cb_);
79
80 if (activate_fails_)
81 return false;
82
83 closed_cb_ = std::move(closed_callback);
84 rx_cb_ = std::move(rx_callback);
85
86 while (!pending_rx_sdus_.empty()) {
87 rx_cb_(std::move(pending_rx_sdus_.front()));
88 pending_rx_sdus_.pop();
89 }
90
91 return true;
92 }
93
Deactivate()94 void FakeChannel::Deactivate() {
95 closed_cb_ = {};
96 rx_cb_ = {};
97 }
98
SignalLinkError()99 void FakeChannel::SignalLinkError() {
100 if (link_error_) {
101 return;
102 }
103 link_error_ = true;
104
105 if (link_err_cb_) {
106 link_err_cb_();
107 }
108 }
109
Send(ByteBufferPtr sdu)110 bool FakeChannel::Send(ByteBufferPtr sdu) {
111 BT_DEBUG_ASSERT(sdu);
112
113 if (!send_cb_)
114 return false;
115
116 if (sdu->size() > max_tx_sdu_size()) {
117 bt_log(ERROR,
118 "l2cap",
119 "Dropping oversized SDU (sdu->size()=%zu, max_tx_sdu_size()=%u)",
120 sdu->size(),
121 max_tx_sdu_size());
122 return false;
123 }
124
125 if (send_dispatcher_) {
126 (void)send_dispatcher_->Post(
127 [cb = send_cb_.share(), sdu = std::move(sdu)](
128 pw::async::Context /*ctx*/, pw::Status status) mutable {
129 if (status.ok()) {
130 cb(std::move(sdu));
131 }
132 });
133 } else {
134 send_cb_(std::move(sdu));
135 }
136
137 return true;
138 }
139
UpgradeSecurity(sm::SecurityLevel level,sm::ResultFunction<> callback)140 void FakeChannel::UpgradeSecurity(sm::SecurityLevel level,
141 sm::ResultFunction<> callback) {
142 BT_ASSERT(security_dispatcher_);
143 (void)security_dispatcher_->Post(
144 [cb = std::move(callback),
145 f = security_cb_.share(),
146 handle = handle_,
147 level](pw::async::Context /*ctx*/, pw::Status status) mutable {
148 if (status.ok()) {
149 f(handle, level, std::move(cb));
150 }
151 });
152 }
153
RequestAclPriority(pw::bluetooth::AclPriority priority,fit::callback<void (fit::result<fit::failed>)> cb)154 void FakeChannel::RequestAclPriority(
155 pw::bluetooth::AclPriority priority,
156 fit::callback<void(fit::result<fit::failed>)> cb) {
157 if (acl_priority_fails_) {
158 cb(fit::failed());
159 return;
160 }
161 requested_acl_priority_ = priority;
162 cb(fit::ok());
163 }
164
SetBrEdrAutomaticFlushTimeout(pw::chrono::SystemClock::duration flush_timeout,hci::ResultCallback<> callback)165 void FakeChannel::SetBrEdrAutomaticFlushTimeout(
166 pw::chrono::SystemClock::duration flush_timeout,
167 hci::ResultCallback<> callback) {
168 if (!flush_timeout_succeeds_) {
169 callback(ToResult(pw::bluetooth::emboss::StatusCode::UNSPECIFIED_ERROR));
170 return;
171 }
172 info_.flush_timeout = flush_timeout;
173 callback(fit::ok());
174 }
175
StartA2dpOffload(const A2dpOffloadManager::Configuration & config,hci::ResultCallback<> callback)176 void FakeChannel::StartA2dpOffload(
177 const A2dpOffloadManager::Configuration& config,
178 hci::ResultCallback<> callback) {
179 if (a2dp_offload_error_.has_value()) {
180 callback(ToResult(a2dp_offload_error_.value()));
181 return;
182 }
183 callback(fit::ok());
184 }
185
StopA2dpOffload(hci::ResultCallback<> callback)186 void FakeChannel::StopA2dpOffload(hci::ResultCallback<> callback) {
187 if (a2dp_offload_error_.has_value()) {
188 callback(ToResult(a2dp_offload_error_.value()));
189 return;
190 }
191 callback(fit::ok());
192 }
193
194 } // namespace bt::l2cap::testing
195