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 #include "pw_bluetooth_sapphire/internal/host/common/inspect.h"
17 #include "pw_bluetooth_sapphire/internal/host/transport/acl_data_channel.h"
18 #include "pw_bluetooth_sapphire/internal/host/transport/acl_data_packet.h"
19 
20 namespace bt::hci::testing {
21 
22 class MockAclDataChannel final : public AclDataChannel {
23  public:
24   MockAclDataChannel() = default;
25   ~MockAclDataChannel() override = default;
26 
set_bredr_buffer_info(DataBufferInfo info)27   void set_bredr_buffer_info(DataBufferInfo info) { bredr_buffer_info_ = info; }
set_le_buffer_info(DataBufferInfo info)28   void set_le_buffer_info(DataBufferInfo info) { le_buffer_info_ = info; }
29 
30   using SendPacketsCallback =
31       fit::function<bool(std::list<ACLDataPacketPtr> packets)>;
32   // using SendPacketsCallback = fit::function<bool(WeakPtr<ConnectionInterface>
33   // connection)>;
set_send_packets_cb(SendPacketsCallback cb)34   void set_send_packets_cb(SendPacketsCallback cb) {
35     send_packets_cb_ = std::move(cb);
36   }
37 
38   using DropQueuedPacketsCallback =
39       fit::function<void(AclPacketPredicate predicate)>;
set_drop_queued_packets_cb(DropQueuedPacketsCallback cb)40   void set_drop_queued_packets_cb(DropQueuedPacketsCallback cb) {
41     drop_queued_packets_cb_ = std::move(cb);
42   }
43 
44   using RequestAclPriorityCallback = fit::function<void(
45       pw::bluetooth::AclPriority priority,
46       hci_spec::ConnectionHandle handle,
47       fit::callback<void(fit::result<fit::failed>)> callback)>;
set_request_acl_priority_cb(RequestAclPriorityCallback cb)48   void set_request_acl_priority_cb(RequestAclPriorityCallback cb) {
49     request_acl_priority_cb_ = std::move(cb);
50   }
51 
52   void ReceivePacket(std::unique_ptr<ACLDataPacket> packet);
53 
54   // AclDataChannel overrides:
AttachInspect(inspect::Node &,const std::string &)55   void AttachInspect(inspect::Node& /*unused*/,
56                      const std::string& /*unused*/) override {}
57   void SetDataRxHandler(ACLPacketHandler rx_callback) override;
58   void RegisterConnection(WeakPtr<ConnectionInterface> connection) override;
59   void UnregisterConnection(hci_spec::ConnectionHandle handle) override;
60   void OnOutboundPacketAvailable() override;
ClearControllerPacketCount(hci_spec::ConnectionHandle)61   void ClearControllerPacketCount(hci_spec::ConnectionHandle) override {}
62   const DataBufferInfo& GetBufferInfo() const override;
63   const DataBufferInfo& GetLeBufferInfo() const override;
64   void RequestAclPriority(
65       pw::bluetooth::AclPriority priority,
66       hci_spec::ConnectionHandle handle,
67       fit::callback<void(fit::result<fit::failed>)> callback) override;
68 
69  private:
70   using ConnectionMap = std::unordered_map<hci_spec::ConnectionHandle,
71                                            WeakPtr<ConnectionInterface>>;
72 
73   void IncrementRoundRobinIterator(ConnectionMap::iterator& conn_iter,
74                                    bt::LinkType connection_type);
75   void SendPackets();
76 
77   ConnectionMap registered_connections_;
78 
79   DataBufferInfo bredr_buffer_info_;
80   DataBufferInfo le_buffer_info_;
81 
82   ACLPacketHandler data_rx_handler_;
83   SendPacketsCallback send_packets_cb_;
84   DropQueuedPacketsCallback drop_queued_packets_cb_;
85   RequestAclPriorityCallback request_acl_priority_cb_;
86 };
87 
88 }  // namespace bt::hci::testing
89