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 <unordered_map>
18 
19 #include "l2cap/cid.h"
20 #include "l2cap/classic/internal/link.h"
21 #include "l2cap/classic/security_policy.h"
22 #include "l2cap/internal/dynamic_channel_impl.h"
23 #include "l2cap/internal/sender.h"
24 #include "l2cap/psm.h"
25 #include "os/handler.h"
26 #include "os/log.h"
27 
28 namespace bluetooth {
29 namespace l2cap {
30 namespace internal {
31 
DynamicChannelImpl(Psm psm,Cid cid,Cid remote_cid,l2cap::internal::ILink * link,os::Handler * l2cap_handler)32 DynamicChannelImpl::DynamicChannelImpl(Psm psm, Cid cid, Cid remote_cid, l2cap::internal::ILink* link,
33                                        os::Handler* l2cap_handler)
34     : psm_(psm), cid_(cid), remote_cid_(remote_cid), link_(link), l2cap_handler_(l2cap_handler),
35       device_(link->GetDevice()) {
36   ASSERT(cid_ > 0);
37   ASSERT(remote_cid_ > 0);
38   ASSERT(link_ != nullptr);
39   ASSERT(l2cap_handler_ != nullptr);
40 }
41 
GetDevice() const42 hci::AddressWithType DynamicChannelImpl::GetDevice() const {
43   return device_;
44 }
45 
RegisterOnCloseCallback(DynamicChannel::OnCloseCallback on_close_callback)46 void DynamicChannelImpl::RegisterOnCloseCallback(DynamicChannel::OnCloseCallback on_close_callback) {
47   ASSERT_LOG(on_close_callback_.IsEmpty(), "OnCloseCallback can only be registered once");
48   // If channel is already closed, call the callback immediately without saving it
49   if (closed_) {
50     on_close_callback.Invoke(close_reason_);
51     return;
52   }
53   on_close_callback_ = std::move(on_close_callback);
54 }
55 
Close()56 void DynamicChannelImpl::Close() {
57   if (link_ == nullptr) {
58     LOG_ERROR("Channel is already closed");
59     return;
60   }
61   link_->SendDisconnectionRequest(cid_, remote_cid_);
62 }
63 
OnClosed(hci::ErrorCode status)64 void DynamicChannelImpl::OnClosed(hci::ErrorCode status) {
65   ASSERT_LOG(!closed_, "Device %s Cid 0x%x closed twice, old status 0x%x, new status 0x%x", device_.ToString().c_str(),
66              cid_, static_cast<int>(close_reason_), static_cast<int>(status));
67   closed_ = true;
68   close_reason_ = status;
69   link_ = nullptr;
70   l2cap_handler_ = nullptr;
71   on_close_callback_.InvokeIfNotEmpty(close_reason_);
72   on_close_callback_ = {};
73 }
74 
ToString()75 std::string DynamicChannelImpl::ToString() {
76   std::ostringstream ss;
77   ss << "Device " << device_ << "Psm 0x" << std::hex << psm_ << " Cid 0x" << std::hex << cid_;
78   return ss.str();
79 }
80 
81 }  // namespace internal
82 }  // namespace l2cap
83 }  // namespace bluetooth
84