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 #pragma once
16 
17 #include <lib/fit/function.h>
18 
19 #include <queue>
20 
21 #include "pw_bluetooth_sapphire/internal/host/l2cap/tx_engine.h"
22 
23 namespace bt::l2cap::internal {
24 
25 // A fake TxEngine::TxChannel, useful for testing.
26 class FakeTxChannel : public TxEngine::TxChannel {
27  public:
28   using SendFrameHandler = fit::function<void(ByteBufferPtr)>;
29 
30   ~FakeTxChannel() override = default;
31 
HandleSendFrame(SendFrameHandler handler)32   FakeTxChannel& HandleSendFrame(SendFrameHandler handler) {
33     send_frame_cb_ = std::move(handler);
34     return *this;
35   }
36 
QueueSdu(ByteBufferPtr sdu)37   FakeTxChannel& QueueSdu(ByteBufferPtr sdu) {
38     queue_.push(std::move(sdu));
39     return *this;
40   }
41 
SendFrame(ByteBufferPtr frame)42   void SendFrame(ByteBufferPtr frame) override {
43     if (send_frame_cb_)
44       send_frame_cb_(std::move(frame));
45   }
46 
GetNextQueuedSdu()47   std::optional<ByteBufferPtr> GetNextQueuedSdu() override {
48     if (queue_.empty())
49       return std::nullopt;
50     ByteBufferPtr next = std::move(queue_.front());
51     queue_.pop();
52     return next;
53   }
54 
queue_size()55   size_t queue_size() { return queue_.size(); }
56 
57  private:
58   SendFrameHandler send_frame_cb_;
59   std::queue<ByteBufferPtr> queue_;
60 };
61 
62 }  // namespace bt::l2cap::internal
63