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