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(!closed_, "Device %s Cid 0x%x closed twice, old status 0x%x, new status 0x%x", device_.ToString().c_str(),
51              cid_, static_cast<int>(close_reason_), static_cast<int>(status));
52   closed_ = true;
53   close_reason_ = status;
54   acquired_ = false;
55   link_ = nullptr;
56   l2cap_handler_ = nullptr;
57   if (user_handler_ == nullptr) {
58     return;
59   }
60   // On close callback can only be called once
61   user_handler_->Post(common::BindOnce(std::move(on_close_callback_), status));
62   user_handler_ = nullptr;
63   on_close_callback_.Reset();
64 }
65 
Acquire()66 void FixedChannelImpl::Acquire() {
67   ASSERT_LOG(user_handler_ != nullptr, "Must register OnCloseCallback before calling any methods");
68   if (closed_) {
69     LOG_WARN("%s is already closed", ToString().c_str());
70     ASSERT(!acquired_);
71     return;
72   }
73   if (acquired_) {
74     LOG_DEBUG("%s was already acquired", ToString().c_str());
75     return;
76   }
77   acquired_ = true;
78   link_->RefreshRefCount();
79 }
80 
Release()81 void FixedChannelImpl::Release() {
82   ASSERT_LOG(user_handler_ != nullptr, "Must register OnCloseCallback before calling any methods");
83   if (closed_) {
84     LOG_WARN("%s is already closed", ToString().c_str());
85     ASSERT(!acquired_);
86     return;
87   }
88   if (!acquired_) {
89     LOG_DEBUG("%s was already released", ToString().c_str());
90     return;
91   }
92   acquired_ = false;
93   link_->RefreshRefCount();
94 }
95 
96 }  // namespace internal
97 }  // namespace classic
98 }  // namespace l2cap
99 }  // namespace bluetooth
100