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/receiver.h"
18
19 #include <bluetooth/log.h>
20
21 #include "common/bidi_queue.h"
22 #include "l2cap/cid.h"
23 #include "l2cap/internal/data_pipeline_manager.h"
24 #include "l2cap/l2cap_packets.h"
25 #include "packet/packet_view.h"
26
27 namespace bluetooth {
28 namespace l2cap {
29 namespace internal {
Receiver(LowerQueueUpEnd * link_queue_up_end,os::Handler * handler,DataPipelineManager * data_pipeline_manager_)30 Receiver::Receiver(
31 LowerQueueUpEnd* link_queue_up_end, os::Handler* handler, DataPipelineManager* data_pipeline_manager_)
32 : link_queue_up_end_(link_queue_up_end),
33 handler_(handler),
34 buffer_timer_(handler),
35 data_pipeline_manager_(data_pipeline_manager_) {
36 log::assert_that(
37 link_queue_up_end_ != nullptr && handler_ != nullptr,
38 "assert failed: link_queue_up_end_ != nullptr && handler_ != nullptr");
39 link_queue_up_end_->RegisterDequeue(handler_,
40 common::Bind(&Receiver::link_queue_dequeue_callback, common::Unretained(this)));
41 }
42
43 // Invoked from external handler/thread (ModuleRegistry)
~Receiver()44 Receiver::~Receiver() {
45 link_queue_up_end_->UnregisterDequeue();
46 }
47
48 // Invoked from external (Queue Reactable)
link_queue_dequeue_callback()49 void Receiver::link_queue_dequeue_callback() {
50 auto packet = link_queue_up_end_->TryDequeue();
51 auto basic_frame_view = BasicFrameView::Create(*packet);
52 if (!basic_frame_view.IsValid()) {
53 log::warn("Received an invalid basic frame");
54 return;
55 }
56 Cid cid = static_cast<Cid>(basic_frame_view.GetChannelId());
57 auto* data_controller = data_pipeline_manager_->GetDataController(cid);
58 if (data_controller == nullptr) {
59 // TODO(b/150170271): Buffer a few packets before data controller is attached
60 log::warn("Received a packet without data controller. cid: {}", cid);
61 buffered_packets_.emplace(*packet);
62 log::warn("Enqueued the unexpected packet. Current queue size: {}", buffered_packets_.size());
63 buffer_timer_.Schedule(
64 common::BindOnce(&Receiver::check_buffered_packets, common::Unretained(this)), std::chrono::milliseconds(500));
65
66 return;
67 }
68 data_controller->OnPdu(*packet);
69 }
70
check_buffered_packets()71 void Receiver::check_buffered_packets() {
72 while (!buffered_packets_.empty()) {
73 auto packet = buffered_packets_.front();
74 buffered_packets_.pop();
75 auto basic_frame_view = BasicFrameView::Create(packet);
76 if (!basic_frame_view.IsValid()) {
77 log::warn("Received an invalid basic frame");
78 return;
79 }
80 Cid cid = static_cast<Cid>(basic_frame_view.GetChannelId());
81 auto* data_controller = data_pipeline_manager_->GetDataController(cid);
82 if (data_controller == nullptr) {
83 log::error("Dropping a packet with invalid cid: {}", cid);
84 } else {
85 data_controller->OnPdu(packet);
86 }
87 }
88 }
89
90 } // namespace internal
91 } // namespace l2cap
92 } // namespace bluetooth
93