1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <string>
20 #include <unordered_map>
21 
22 #include "common/bidi_queue.h"
23 #include "common/bind.h"
24 #include "data_controller.h"
25 #include "l2cap/cid.h"
26 #include "l2cap/classic/internal/channel_configuration_state.h"
27 #include "l2cap/internal/channel_impl.h"
28 #include "l2cap/internal/receiver.h"
29 #include "l2cap/internal/scheduler.h"
30 #include "l2cap/internal/scheduler_fifo.h"
31 #include "l2cap/l2cap_packets.h"
32 #include "l2cap/mtu.h"
33 #include "os/handler.h"
34 #include "os/log.h"
35 #include "os/queue.h"
36 #include "packet/base_packet_builder.h"
37 #include "packet/packet_view.h"
38 
39 namespace bluetooth {
40 namespace l2cap {
41 namespace internal {
42 class ILink;
43 
44 /**
45  * Manages data pipeline from channel queue end to link queue end, per link.
46  * Contains a Scheduler and Receiver per link.
47  * Contains a Sender and its corresponding DataController per attached channel.
48  */
49 class DataPipelineManager {
50  public:
51   using UpperEnqueue = packet::PacketView<packet::kLittleEndian>;
52   using UpperDequeue = packet::BasePacketBuilder;
53   using UpperQueueDownEnd = common::BidiQueueEnd<UpperEnqueue, UpperDequeue>;
54   using LowerEnqueue = UpperDequeue;
55   using LowerDequeue = UpperEnqueue;
56   using LowerQueueUpEnd = common::BidiQueueEnd<LowerEnqueue, LowerDequeue>;
57 
DataPipelineManager(os::Handler * handler,ILink * link,LowerQueueUpEnd * link_queue_up_end)58   DataPipelineManager(os::Handler* handler, ILink* link, LowerQueueUpEnd* link_queue_up_end)
59       : handler_(handler), link_(link), scheduler_(std::make_unique<Fifo>(this, link_queue_up_end, handler)),
60         receiver_(link_queue_up_end, handler, this) {}
61 
62   using ChannelMode = Sender::ChannelMode;
63 
64   virtual void AttachChannel(Cid cid, std::shared_ptr<ChannelImpl> channel, ChannelMode mode);
65   virtual void DetachChannel(Cid cid);
66   virtual DataController* GetDataController(Cid cid);
67   virtual void OnPacketSent(Cid cid);
68   virtual void UpdateClassicConfiguration(Cid cid, classic::internal::ChannelConfigurationState config);
69   virtual ~DataPipelineManager() = default;
70 
71  private:
72   os::Handler* handler_;
73   ILink* link_;
74   std::unordered_map<Cid, Sender> sender_map_;
75   std::unique_ptr<Scheduler> scheduler_;
76   Receiver receiver_;
77 };
78 }  // namespace internal
79 }  // namespace l2cap
80 }  // namespace bluetooth
81