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   next_to_dequeue_and_num_packets.push(std::make_pair(cid, number_packets));
47   try_register_link_queue_enqueue();
48 }
49 
50 // Invoked from some external Queue Reactable context
link_queue_enqueue_callback()51 std::unique_ptr<Fifo::UpperDequeue> Fifo::link_queue_enqueue_callback() {
52   ASSERT(!next_to_dequeue_and_num_packets.empty());
53   auto& channel_id_and_number_packets = next_to_dequeue_and_num_packets.front();
54   auto channel_id = channel_id_and_number_packets.first;
55   channel_id_and_number_packets.second--;
56   if (channel_id_and_number_packets.second == 0) {
57     next_to_dequeue_and_num_packets.pop();
58   }
59   auto packet = data_pipeline_manager_->GetDataController(channel_id)->GetNextPacket();
60 
61   data_pipeline_manager_->OnPacketSent(channel_id);
62   if (next_to_dequeue_and_num_packets.empty() && link_queue_enqueue_registered_.exchange(false)) {
63     link_queue_up_end_->UnregisterEnqueue();
64   }
65   return packet;
66 }
67 
try_register_link_queue_enqueue()68 void Fifo::try_register_link_queue_enqueue() {
69   if (link_queue_enqueue_registered_.exchange(true)) {
70     return;
71   }
72   link_queue_up_end_->RegisterEnqueue(handler_,
73                                       common::Bind(&Fifo::link_queue_enqueue_callback, common::Unretained(this)));
74 }
75 
76 }  // namespace internal
77 }  // namespace l2cap
78 }  // namespace bluetooth
79