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