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/basic_mode_channel_data_controller.h"
18 
19 #include "l2cap/l2cap_packets.h"
20 
21 namespace bluetooth {
22 namespace l2cap {
23 namespace internal {
24 
BasicModeDataController(Cid cid,Cid remote_cid,UpperQueueDownEnd * channel_queue_end,os::Handler * handler,Scheduler * scheduler)25 BasicModeDataController::BasicModeDataController(Cid cid, Cid remote_cid, UpperQueueDownEnd* channel_queue_end,
26                                                  os::Handler* handler, Scheduler* scheduler)
27     : cid_(cid), remote_cid_(remote_cid), enqueue_buffer_(channel_queue_end), handler_(handler), scheduler_(scheduler) {
28 }
29 
~BasicModeDataController()30 BasicModeDataController::~BasicModeDataController() {
31   enqueue_buffer_.Clear();
32 }
33 
OnSdu(std::unique_ptr<packet::BasePacketBuilder> sdu)34 void BasicModeDataController::OnSdu(std::unique_ptr<packet::BasePacketBuilder> sdu) {
35   auto l2cap_information = BasicFrameBuilder::Create(remote_cid_, std::move(sdu));
36   pdu_queue_.emplace(std::move(l2cap_information));
37   scheduler_->OnPacketsReady(cid_, 1);
38 }
39 
OnPdu(packet::PacketView<true> pdu)40 void BasicModeDataController::OnPdu(packet::PacketView<true> pdu) {
41   auto basic_frame_view = BasicFrameView::Create(pdu);
42   if (!basic_frame_view.IsValid()) {
43     LOG_WARN("Received invalid frame");
44     return;
45   }
46   enqueue_buffer_.Enqueue(std::make_unique<PacketView<kLittleEndian>>(basic_frame_view.GetPayload()), handler_);
47 }
48 
GetNextPacket()49 std::unique_ptr<packet::BasePacketBuilder> BasicModeDataController::GetNextPacket() {
50   auto next = std::move(pdu_queue_.front());
51   pdu_queue_.pop();
52   return next;
53 }
54 
55 }  // namespace internal
56 }  // namespace l2cap
57 }  // namespace bluetooth
58