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 "common/bidi_queue.h"
20 #include "l2cap/cid.h"
21 #include "l2cap/internal/data_pipeline_manager.h"
22 #include "l2cap/l2cap_packets.h"
23 #include "packet/packet_view.h"
24 
25 namespace bluetooth {
26 namespace l2cap {
27 namespace internal {
Receiver(LowerQueueUpEnd * link_queue_up_end,os::Handler * handler,DataPipelineManager * data_pipeline_manager_)28 Receiver::Receiver(LowerQueueUpEnd* link_queue_up_end, os::Handler* handler,
29                    DataPipelineManager* data_pipeline_manager_)
30     : link_queue_up_end_(link_queue_up_end), handler_(handler), data_pipeline_manager_(data_pipeline_manager_) {
31   ASSERT(link_queue_up_end_ != nullptr && handler_ != nullptr);
32   link_queue_up_end_->RegisterDequeue(handler_,
33                                       common::Bind(&Receiver::link_queue_dequeue_callback, common::Unretained(this)));
34 }
35 
36 // Invoked from external handler/thread (ModuleRegistry)
~Receiver()37 Receiver::~Receiver() {
38   link_queue_up_end_->UnregisterDequeue();
39 }
40 
41 // Invoked from external (Queue Reactable)
link_queue_dequeue_callback()42 void Receiver::link_queue_dequeue_callback() {
43   auto packet = link_queue_up_end_->TryDequeue();
44   auto basic_frame_view = BasicFrameView::Create(*packet);
45   if (!basic_frame_view.IsValid()) {
46     LOG_WARN("Received an invalid basic frame");
47     return;
48   }
49   Cid cid = static_cast<Cid>(basic_frame_view.GetChannelId());
50   auto* data_controller = data_pipeline_manager_->GetDataController(cid);
51   if (data_controller == nullptr) {
52     // TODO(b/150170271): Buffer a few packets before data controller is attached
53     LOG_WARN("Received a packet with invalid cid: %d", cid);
54     return;
55   }
56   data_controller->OnPdu(*packet);
57 }
58 
59 }  // namespace internal
60 }  // namespace l2cap
61 }  // namespace bluetooth
62