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 <memory>
20 #include <queue>
21 #include <unordered_map>
22 #include <utility>
23 
24 #include "common/bidi_queue.h"
25 #include "l2cap/cid.h"
26 #include "l2cap/internal/channel_impl.h"
27 #include "l2cap/internal/scheduler.h"
28 #include "l2cap/l2cap_packets.h"
29 #include "l2cap/mtu.h"
30 #include "os/alarm.h"
31 #include "os/queue.h"
32 #include "packet/base_packet_builder.h"
33 #include "packet/packet_view.h"
34 
35 namespace bluetooth {
36 namespace l2cap {
37 namespace internal {
38 
39 class DataPipelineManager;
40 
41 /**
42  * Handle receiving L2CAP PDUs from link queue and distribute them into into channel data controllers.
43  * Dequeue incoming packets from LinkQueueUpEnd, and enqueue it to ChannelQueueDownEnd. Note: If a channel
44  * cannot dequeue from ChannelQueueDownEnd so that the buffer for incoming packet is full, further incoming packets will
45  * be dropped.
46  * The Reassembler keeps the reference to ChannelImpl objects, because it needs to check channel mode and parameters.
47  * The Reassembler also keeps the reference to Scheduler, to get Segmenter and send signals (Tx, Rx seq) to it.
48  */
49 class Receiver {
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 
58   Receiver(LowerQueueUpEnd* link_queue_up_end, os::Handler* handler, DataPipelineManager* data_pipeline_manager);
59   ~Receiver();
60 
61  private:
62   LowerQueueUpEnd* link_queue_up_end_;
63   os::Handler* handler_;
64   os::Alarm buffer_timer_;
65   std::queue<LowerDequeue> buffered_packets_;
66   DataPipelineManager* data_pipeline_manager_;
67 
68   void link_queue_dequeue_callback();
69   void check_buffered_packets();
70 };
71 
72 }  // namespace internal
73 }  // namespace l2cap
74 }  // namespace bluetooth
75