• 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/le_credit_based_channel_data_controller.h"
18 
19 #include "l2cap/l2cap_packets.h"
20 #include "l2cap/le/internal/link.h"
21 #include "packet/fragmenting_inserter.h"
22 #include "packet/raw_builder.h"
23 
24 namespace bluetooth {
25 namespace l2cap {
26 namespace internal {
27 
LeCreditBasedDataController(ILink * link,Cid cid,Cid remote_cid,UpperQueueDownEnd * channel_queue_end,os::Handler * handler,Scheduler * scheduler)28 LeCreditBasedDataController::LeCreditBasedDataController(ILink* link, Cid cid, Cid remote_cid,
29                                                          UpperQueueDownEnd* channel_queue_end, os::Handler* handler,
30                                                          Scheduler* scheduler)
31     : cid_(cid), remote_cid_(remote_cid), enqueue_buffer_(channel_queue_end), handler_(handler), scheduler_(scheduler),
32       link_(link) {}
33 
OnSdu(std::unique_ptr<packet::BasePacketBuilder> sdu)34 void LeCreditBasedDataController::OnSdu(std::unique_ptr<packet::BasePacketBuilder> sdu) {
35   auto sdu_size = sdu->size();
36   if (sdu_size == 0) {
37     LOG_WARN("Received empty SDU");
38     return;
39   }
40   if (sdu_size > mtu_) {
41     LOG_WARN("Received sdu_size %d > mtu %d", static_cast<int>(sdu_size), mtu_);
42   }
43   std::vector<std::unique_ptr<packet::RawBuilder>> segments;
44   // TODO: We don't need to waste 2 bytes for continuation segment.
45   packet::FragmentingInserter fragmenting_inserter(mps_ - 2, std::back_insert_iterator(segments));
46   sdu->Serialize(fragmenting_inserter);
47   fragmenting_inserter.finalize();
48   std::unique_ptr<BasicFrameBuilder> builder;
49   builder = FirstLeInformationFrameBuilder::Create(remote_cid_, sdu_size, std::move(segments[0]));
50   pdu_queue_.emplace(std::move(builder));
51   for (auto i = 1; i < segments.size(); i++) {
52     builder = BasicFrameBuilder::Create(remote_cid_, std::move(segments[i]));
53     pdu_queue_.emplace(std::move(builder));
54   }
55   if (credits_ >= segments.size()) {
56     scheduler_->OnPacketsReady(cid_, segments.size());
57     credits_ -= segments.size();
58   } else if (credits_ > 0) {
59     scheduler_->OnPacketsReady(cid_, credits_);
60     pending_frames_count_ += (segments.size() - credits_);
61     credits_ = 0;
62   } else {
63     pending_frames_count_ += segments.size();
64   }
65 }
66 
OnPdu(packet::PacketView<true> pdu)67 void LeCreditBasedDataController::OnPdu(packet::PacketView<true> pdu) {
68   auto basic_frame_view = BasicFrameView::Create(pdu);
69   if (!basic_frame_view.IsValid()) {
70     LOG_WARN("Received invalid frame");
71     return;
72   }
73   if (basic_frame_view.size() > mps_) {
74     LOG_WARN("Received frame size %d > mps %d, dropping the packet", static_cast<int>(basic_frame_view.size()), mps_);
75     return;
76   }
77   if (remaining_sdu_continuation_packet_size_ == 0) {
78     auto start_frame_view = FirstLeInformationFrameView::Create(basic_frame_view);
79     if (!start_frame_view.IsValid()) {
80       LOG_WARN("Received invalid frame");
81       return;
82     }
83     auto payload = start_frame_view.GetPayload();
84     auto sdu_size = start_frame_view.GetL2capSduLength();
85     remaining_sdu_continuation_packet_size_ = sdu_size - payload.size();
86     reassembly_stage_ = payload;
87   } else {
88     auto payload = basic_frame_view.GetPayload();
89     remaining_sdu_continuation_packet_size_ -= payload.size();
90     reassembly_stage_.AppendPacketView(payload);
91   }
92   if (remaining_sdu_continuation_packet_size_ == 0) {
93     enqueue_buffer_.Enqueue(std::make_unique<PacketView<kLittleEndian>>(reassembly_stage_), handler_);
94   } else if (remaining_sdu_continuation_packet_size_ < 0 || reassembly_stage_.size() > mtu_) {
95     LOG_WARN("Received larger SDU size than expected");
96     reassembly_stage_ = PacketViewForReassembly(PacketView<kLittleEndian>(std::make_shared<std::vector<uint8_t>>()));
97     remaining_sdu_continuation_packet_size_ = 0;
98     link_->SendDisconnectionRequest(cid_, remote_cid_);
99   }
100   // TODO: Improve the logic by sending credit only after user dequeued the SDU
101   link_->SendLeCredit(cid_, 1);
102 }
103 
GetNextPacket()104 std::unique_ptr<packet::BasePacketBuilder> LeCreditBasedDataController::GetNextPacket() {
105   ASSERT(!pdu_queue_.empty());
106   auto next = std::move(pdu_queue_.front());
107   pdu_queue_.pop();
108   return next;
109 }
110 
SetMtu(Mtu mtu)111 void LeCreditBasedDataController::SetMtu(Mtu mtu) {
112   mtu_ = mtu;
113 }
114 
SetMps(uint16_t mps)115 void LeCreditBasedDataController::SetMps(uint16_t mps) {
116   mps_ = mps;
117 }
118 
OnCredit(uint16_t credits)119 void LeCreditBasedDataController::OnCredit(uint16_t credits) {
120   int total_credits = credits_ + credits;
121   if (total_credits > 0xffff) {
122     link_->SendDisconnectionRequest(cid_, remote_cid_);
123   }
124   credits_ = total_credits;
125   if (pending_frames_count_ > 0 && credits_ >= pending_frames_count_) {
126     scheduler_->OnPacketsReady(cid_, pending_frames_count_);
127     pending_frames_count_ = 0;
128     credits_ -= pending_frames_count_;
129   } else if (pending_frames_count_ > 0) {
130     scheduler_->OnPacketsReady(cid_, credits_);
131     pending_frames_count_ -= credits_;
132     credits_ = 0;
133   }
134 }
135 
136 }  // namespace internal
137 }  // namespace l2cap
138 }  // namespace bluetooth
139