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