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