Hansong Zhang | 36c81b5 | 2019-11-21 16:42:29 -0800 | [diff] [blame] | 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 "packet/fragmenting_inserter.h" |
| 21 | #include "packet/raw_builder.h" |
| 22 | |
| 23 | namespace bluetooth { |
| 24 | namespace l2cap { |
| 25 | namespace internal { |
| 26 | |
| 27 | LeCreditBasedDataController::LeCreditBasedDataController(Cid cid, Cid remote_cid, UpperQueueDownEnd* channel_queue_end, |
| 28 | os::Handler* handler, Scheduler* scheduler) |
| 29 | : cid_(cid), remote_cid_(remote_cid), enqueue_buffer_(channel_queue_end), handler_(handler), scheduler_(scheduler) { |
| 30 | } |
| 31 | |
| 32 | void LeCreditBasedDataController::OnSdu(std::unique_ptr<packet::BasePacketBuilder> sdu) { |
| 33 | auto sdu_size = sdu->size(); |
| 34 | if (sdu_size == 0) { |
| 35 | LOG_WARN("Received empty SDU"); |
| 36 | return; |
| 37 | } |
| 38 | if (sdu_size > mtu_) { |
| 39 | LOG_WARN("Received sdu_size %d > mtu %d", static_cast<int>(sdu_size), mtu_); |
| 40 | } |
| 41 | std::vector<std::unique_ptr<packet::RawBuilder>> segments; |
| 42 | // TODO: We don't need to waste 2 bytes for continuation segment. |
| 43 | packet::FragmentingInserter fragmenting_inserter(mps_ - 2, std::back_insert_iterator(segments)); |
| 44 | sdu->Serialize(fragmenting_inserter); |
| 45 | fragmenting_inserter.finalize(); |
| 46 | std::unique_ptr<BasicFrameBuilder> builder; |
| 47 | builder = FirstLeInformationFrameBuilder::Create(remote_cid_, sdu_size, std::move(segments[0])); |
| 48 | pdu_queue_.emplace(std::move(builder)); |
| 49 | for (auto i = 1; i < segments.size(); i++) { |
| 50 | builder = BasicFrameBuilder::Create(remote_cid_, std::move(segments[i])); |
| 51 | pdu_queue_.emplace(std::move(builder)); |
| 52 | } |
| 53 | scheduler_->OnPacketsReady(cid_, segments.size()); |
| 54 | } |
| 55 | |
| 56 | void LeCreditBasedDataController::OnPdu(packet::PacketView<true> pdu) { |
| 57 | auto basic_frame_view = BasicFrameView::Create(pdu); |
| 58 | if (!basic_frame_view.IsValid()) { |
| 59 | LOG_WARN("Received invalid frame"); |
| 60 | return; |
| 61 | } |
| 62 | if (basic_frame_view.size() > mps_) { |
| 63 | LOG_WARN("Received frame size %d > mps %d, dropping the packet", static_cast<int>(basic_frame_view.size()), mps_); |
| 64 | return; |
| 65 | } |
| 66 | if (remaining_sdu_continuation_packet_size_ == 0) { |
| 67 | auto start_frame_view = FirstLeInformationFrameView::Create(basic_frame_view); |
| 68 | if (!start_frame_view.IsValid()) { |
| 69 | LOG_WARN("Received invalid frame"); |
| 70 | return; |
| 71 | } |
| 72 | auto payload = start_frame_view.GetPayload(); |
| 73 | auto sdu_size = start_frame_view.GetL2capSduLength(); |
| 74 | remaining_sdu_continuation_packet_size_ = sdu_size - payload.size(); |
| 75 | reassembly_stage_ = payload; |
| 76 | } else { |
| 77 | auto payload = basic_frame_view.GetPayload(); |
| 78 | remaining_sdu_continuation_packet_size_ -= payload.size(); |
| 79 | reassembly_stage_.AppendPacketView(payload); |
| 80 | } |
| 81 | if (remaining_sdu_continuation_packet_size_ == 0) { |
| 82 | enqueue_buffer_.Enqueue(std::make_unique<PacketView<kLittleEndian>>(reassembly_stage_), handler_); |
| 83 | } else if (remaining_sdu_continuation_packet_size_ < 0 || reassembly_stage_.size() > mtu_) { |
| 84 | LOG_WARN("Received larger SDU size than expected"); |
| 85 | reassembly_stage_ = PacketViewForReassembly(std::make_shared<std::vector<uint8_t>>()); |
| 86 | remaining_sdu_continuation_packet_size_ = 0; |
| 87 | // TODO: Close channel |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | std::unique_ptr<packet::BasePacketBuilder> LeCreditBasedDataController::GetNextPacket() { |
| 92 | auto next = std::move(pdu_queue_.front()); |
| 93 | pdu_queue_.pop(); |
| 94 | return next; |
| 95 | } |
| 96 | |
| 97 | void LeCreditBasedDataController::SetMtu(Mtu mtu) { |
| 98 | mtu_ = mtu; |
| 99 | } |
| 100 | |
| 101 | void LeCreditBasedDataController::SetMps(uint16_t mps) { |
| 102 | mps_ = mps; |
| 103 | } |
| 104 | |
| 105 | void LeCreditBasedDataController::OnCredit(uint16_t credits) { |
| 106 | int total_credits = credits_ + credits; |
| 107 | credits_ = total_credits > 0xffff ? 0xffff : total_credits; |
| 108 | } |
| 109 | |
| 110 | } // namespace internal |
| 111 | } // namespace l2cap |
| 112 | } // namespace bluetooth |