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/fixed_channel_impl.h"
21 #include "l2cap/classic/internal/link.h"
22 #include "os/handler.h"
23 #include "os/log.h"
24
25 namespace bluetooth {
26 namespace l2cap {
27 namespace classic {
28 namespace internal {
29
FixedChannelImpl(Cid cid,Link * link,os::Handler * l2cap_handler)30 FixedChannelImpl::FixedChannelImpl(Cid cid, Link* link, os::Handler* l2cap_handler)
31 : cid_(cid), device_(link->GetDevice()), link_(link), l2cap_handler_(l2cap_handler) {
32 ASSERT_LOG(cid_ >= kFirstFixedChannel && cid_ <= kLastFixedChannel, "Invalid cid: %d", cid_);
33 ASSERT(link_ != nullptr);
34 ASSERT(l2cap_handler_ != nullptr);
35 }
36
RegisterOnCloseCallback(os::Handler * user_handler,FixedChannel::OnCloseCallback on_close_callback)37 void FixedChannelImpl::RegisterOnCloseCallback(os::Handler* user_handler,
38 FixedChannel::OnCloseCallback on_close_callback) {
39 ASSERT_LOG(user_handler_ == nullptr, "OnCloseCallback can only be registered once");
40 // If channel is already closed, call the callback immediately without saving it
41 if (closed_) {
42 user_handler->Post(common::BindOnce(std::move(on_close_callback), close_reason_));
43 return;
44 }
45 user_handler_ = user_handler;
46 on_close_callback_ = std::move(on_close_callback);
47 }
48
OnClosed(hci::ErrorCode status)49 void FixedChannelImpl::OnClosed(hci::ErrorCode status) {
50 ASSERT_LOG(
51 !closed_,
52 "Device %s Cid 0x%x closed twice, old status 0x%x, new status 0x%x",
53 ADDRESS_TO_LOGGABLE_CSTR(device_),
54 cid_,
55 static_cast<int>(close_reason_),
56 static_cast<int>(status));
57 closed_ = true;
58 close_reason_ = status;
59 acquired_ = false;
60 link_ = nullptr;
61 l2cap_handler_ = nullptr;
62 if (user_handler_ == nullptr) {
63 return;
64 }
65 // On close callback can only be called once
66 user_handler_->Post(common::BindOnce(std::move(on_close_callback_), status));
67 user_handler_ = nullptr;
68 on_close_callback_.Reset();
69 }
70
Acquire()71 void FixedChannelImpl::Acquire() {
72 ASSERT_LOG(user_handler_ != nullptr, "Must register OnCloseCallback before calling any methods");
73 if (closed_) {
74 LOG_WARN("%s is already closed", ToLoggableStr(*this).c_str());
75 ASSERT(!acquired_);
76 return;
77 }
78 if (acquired_) {
79 LOG_INFO("%s was already acquired", ToLoggableStr(*this).c_str());
80 return;
81 }
82 acquired_ = true;
83 link_->RefreshRefCount();
84 }
85
Release()86 void FixedChannelImpl::Release() {
87 ASSERT_LOG(user_handler_ != nullptr, "Must register OnCloseCallback before calling any methods");
88 if (closed_) {
89 LOG_WARN("%s is already closed", ToLoggableStr(*this).c_str());
90 ASSERT(!acquired_);
91 return;
92 }
93 if (!acquired_) {
94 LOG_INFO("%s was already released", ToLoggableStr(*this).c_str());
95 return;
96 }
97 acquired_ = false;
98 link_->RefreshRefCount();
99 }
100
101 } // namespace internal
102 } // namespace classic
103 } // namespace l2cap
104 } // namespace bluetooth
105