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 <cstdint>
20 
21 #include "common/bidi_queue.h"
22 #include "l2cap/cid.h"
23 #include "l2cap/classic/dynamic_channel_configuration_option.h"
24 #include "l2cap/internal/channel_impl.h"
25 #include "l2cap/internal/data_controller.h"
26 #include "l2cap/internal/sender.h"
27 #include "l2cap/l2cap_packets.h"
28 #include "packet/base_packet_builder.h"
29 #include "packet/packet_view.h"
30 
31 namespace bluetooth {
32 namespace l2cap {
33 namespace internal {
34 
35 /**
36  * Handle the scheduling of packets through the l2cap stack.
37  * For each attached channel, dequeue its outgoing packets and enqueue it to the
38  * given LinkQueueUpEnd, according to some policy (cid).
39  *
40  * Note: If a channel cannot dequeue from ChannelQueueDownEnd so that the buffer
41  * for incoming packet is full, further incoming packets will be dropped.
42  */
43 class Scheduler {
44  public:
45   using UpperEnqueue = packet::PacketView<packet::kLittleEndian>;
46   using UpperDequeue = packet::BasePacketBuilder;
47   using UpperQueueDownEnd = common::BidiQueueEnd<UpperEnqueue, UpperDequeue>;
48   using LowerEnqueue = UpperDequeue;
49   using LowerDequeue = UpperEnqueue;
50   using LowerQueueUpEnd = common::BidiQueueEnd<LowerEnqueue, LowerDequeue>;
51 
52   /**
53    * Callback from the sender to indicate that the scheduler could dequeue
54    * number_packets from it
55    */
OnPacketsReady(Cid,int)56   virtual void OnPacketsReady(Cid /* cid */, int /* number_packets */) {}
57 
58   /**
59    * Let the scheduler send the specified cid first.
60    * Used by A2dp software encoding.
61    */
SetChannelTxPriority(Cid,bool)62   virtual void SetChannelTxPriority(Cid /* cid */, bool /* high_priority */) {}
63 
64   /**
65    * Called by data controller to indicate that a channel is closed and packets
66    * should be dropped
67    */
RemoveChannel(Cid)68   virtual void RemoveChannel(Cid /* cid */) {}
69 
70   virtual ~Scheduler() = default;
71 };
72 
73 }  // namespace internal
74 }  // namespace l2cap
75 }  // namespace bluetooth
76