• 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 #include "l2cap/internal/scheduler_fifo.h"
18 
19 #include "dynamic_channel_impl.h"
20 #include "l2cap/internal/data_pipeline_manager.h"
21 #include "l2cap/l2cap_packets.h"
22 #include "os/log.h"
23 
24 namespace bluetooth {
25 namespace l2cap {
26 namespace internal {
27 
Fifo(DataPipelineManager * data_pipeline_manager,LowerQueueUpEnd * link_queue_up_end,os::Handler * handler)28 Fifo::Fifo(DataPipelineManager* data_pipeline_manager, LowerQueueUpEnd* link_queue_up_end, os::Handler* handler)
29     : data_pipeline_manager_(data_pipeline_manager), link_queue_up_end_(link_queue_up_end), handler_(handler) {
30   ASSERT(link_queue_up_end_ != nullptr && handler_ != nullptr);
31 }
32 
33 // Invoked from some external Handler context
~Fifo()34 Fifo::~Fifo() {
35   // TODO(hsz): notify Sender don't send callback to me
36   if (link_queue_enqueue_registered_.exchange(false)) {
37     link_queue_up_end_->UnregisterEnqueue();
38   }
39 }
40 
41 // Invoked within L2CAP Handler context
OnPacketsReady(Cid cid,int number_packets)42 void Fifo::OnPacketsReady(Cid cid, int number_packets) {
43   if (number_packets == 0) {
44     return;
45   }
46   int priority = high_priority_cids_.count(cid) != 0;
47   next_to_dequeue_and_num_packets.push(std::make_pair(cid, number_packets), priority);
48   try_register_link_queue_enqueue();
49 }
50 
51 // Invoked within L2CAP Handler context
SetChannelTxPriority(Cid cid,bool high_priority)52 void Fifo::SetChannelTxPriority(Cid cid, bool high_priority) {
53   if (high_priority) {
54     high_priority_cids_.emplace(cid);
55   } else {
56     high_priority_cids_.erase(cid);
57   }
58 }
59 
RemoveChannel(Cid cid)60 void Fifo::RemoveChannel(Cid cid) {
61   for (int i = 0; i < next_to_dequeue_and_num_packets.size(); i++) {
62     auto& channel_id_and_number_packets = next_to_dequeue_and_num_packets.front();
63     if (channel_id_and_number_packets.second != cid) {
64       next_to_dequeue_and_num_packets.push(channel_id_and_number_packets);
65     }
66     next_to_dequeue_and_num_packets.pop();
67   }
68   if (next_to_dequeue_and_num_packets.empty() && link_queue_enqueue_registered_.exchange(false)) {
69     link_queue_up_end_->UnregisterEnqueue();
70   }
71 }
72 
73 // Invoked from some external Queue Reactable context
link_queue_enqueue_callback()74 std::unique_ptr<Fifo::UpperDequeue> Fifo::link_queue_enqueue_callback() {
75   ASSERT(!next_to_dequeue_and_num_packets.empty());
76   auto& channel_id_and_number_packets = next_to_dequeue_and_num_packets.front();
77   auto channel_id = channel_id_and_number_packets.first;
78   channel_id_and_number_packets.second--;
79   if (channel_id_and_number_packets.second == 0) {
80     next_to_dequeue_and_num_packets.pop();
81   }
82   auto packet = data_pipeline_manager_->GetDataController(channel_id)->GetNextPacket();
83 
84   data_pipeline_manager_->OnPacketSent(channel_id);
85   if (next_to_dequeue_and_num_packets.empty() && link_queue_enqueue_registered_.exchange(false)) {
86     link_queue_up_end_->UnregisterEnqueue();
87   }
88   return packet;
89 }
90 
try_register_link_queue_enqueue()91 void Fifo::try_register_link_queue_enqueue() {
92   if (link_queue_enqueue_registered_.exchange(true)) {
93     return;
94   }
95   link_queue_up_end_->RegisterEnqueue(handler_,
96                                       common::Bind(&Fifo::link_queue_enqueue_callback, common::Unretained(this)));
97 }
98 
99 }  // namespace internal
100 }  // namespace l2cap
101 }  // namespace bluetooth
102