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