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