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 <atomic>
20 #include <string>
21 #include <unordered_map>
22 
23 #include "common/bidi_queue.h"
24 #include "common/bind.h"
25 #include "data_controller.h"
26 #include "l2cap/cid.h"
27 #include "l2cap/classic/internal/channel_configuration_state.h"
28 #include "l2cap/internal/channel_impl.h"
29 #include "l2cap/internal/data_controller.h"
30 #include "l2cap/l2cap_packets.h"
31 #include "l2cap/mtu.h"
32 #include "os/handler.h"
33 #include "os/queue.h"
34 #include "packet/base_packet_builder.h"
35 #include "packet/packet_view.h"
36 
37 namespace bluetooth {
38 namespace l2cap {
39 namespace internal {
40 class Scheduler;
41 class ILink;
42 
43 /**
44  * A middle layer between L2CAP channel and outgoing packet scheduler.
45  * Fetches data (SDU) from an L2CAP channel queue end, handles L2CAP segmentation, and gives data to L2CAP scheduler.
46  */
47 class Sender {
48  public:
49   using UpperEnqueue = packet::PacketView<packet::kLittleEndian>;
50   using UpperDequeue = packet::BasePacketBuilder;
51   using UpperQueueDownEnd = common::BidiQueueEnd<UpperEnqueue, UpperDequeue>;
52 
53   enum class ChannelMode {
54     BASIC = 0,
55     ERTM = 3,
56     LE_CREDIT_BASED = 10,
57   };
58 
59   Sender(os::Handler* handler, ILink* link, Scheduler* scheduler, std::shared_ptr<ChannelImpl> channel);
60   Sender(os::Handler* handler, ILink* link, Scheduler* scheduler, std::shared_ptr<ChannelImpl> channel,
61          ChannelMode mode);
62   ~Sender();
63 
64   /**
65    * Callback from scheduler to indicate that scheduler already dequeued a packet from sender's queue.
66    * Segmenter can continue dequeuing from channel queue end.
67    */
68   void OnPacketSent();
69 
70   /**
71    * Called by the scheduler to return the next PDU to be sent
72    */
73   std::unique_ptr<UpperDequeue> GetNextPacket();
74 
75   void UpdateClassicConfiguration(classic::internal::ChannelConfigurationState config);
76   DataController* GetDataController();
77 
78  private:
79   os::Handler* handler_;
80   ILink* link_;
81   UpperQueueDownEnd* queue_end_;
82   Scheduler* scheduler_;
83   const Cid channel_id_;
84   const Cid remote_channel_id_;
85   std::atomic_bool is_dequeue_registered_ = false;
86   RetransmissionAndFlowControlModeOption mode_ = RetransmissionAndFlowControlModeOption::L2CAP_BASIC;
87   std::unique_ptr<DataController> data_controller_;
88 
89   void try_register_dequeue();
90   void dequeue_callback();
91 };
92 }  // namespace internal
93 }  // namespace l2cap
94 }  // namespace bluetooth
95