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 #pragma once
18 
19 #include "common/bidi_queue.h"
20 #include "common/interfaces/ILoggable.h"
21 #include "l2cap/cid.h"
22 #include "l2cap/classic/fixed_channel.h"
23 #include "l2cap/internal/channel_impl.h"
24 #include "l2cap/l2cap_packets.h"
25 #include "os/handler.h"
26 #include "os/log.h"
27 
28 namespace bluetooth {
29 namespace l2cap {
30 namespace classic {
31 namespace internal {
32 
33 class Link;
34 
35 class FixedChannelImpl : public l2cap::internal::ChannelImpl,
36                          public bluetooth::common::IRedactableLoggable {
37   public:
38   FixedChannelImpl(Cid cid, Link* link, os::Handler* l2cap_handler);
39 
40   FixedChannelImpl(const FixedChannelImpl&) = delete;
41   FixedChannelImpl& operator=(const FixedChannelImpl&) = delete;
42 
43   virtual ~FixedChannelImpl() = default;
44 
GetDevice()45   hci::Address GetDevice() const {
46     return device_.GetAddress();
47   }
48 
49   virtual void RegisterOnCloseCallback(os::Handler* user_handler, FixedChannel::OnCloseCallback on_close_callback);
50 
51   virtual void Acquire();
52 
53   virtual void Release();
54 
IsAcquired()55   virtual bool IsAcquired() const {
56     return acquired_;
57   }
58 
59   virtual void OnClosed(hci::ErrorCode status);
60 
ToStringForLogging()61   std::string ToStringForLogging() const override {
62     std::ostringstream ss;
63     ss << "Device " << device_.ToStringForLogging() << " Cid 0x" << std::hex << cid_;
64     return ss.str();
65   }
66 
ToRedactedStringForLogging()67   std::string ToRedactedStringForLogging() const override {
68     std::ostringstream ss;
69     ss << "Device " << device_.ToRedactedStringForLogging() << " Cid 0x" << std::hex << cid_;
70     return ss.str();
71   }
72 
GetQueueUpEnd()73   common::BidiQueueEnd<packet::BasePacketBuilder, packet::PacketView<packet::kLittleEndian>>* GetQueueUpEnd() {
74     return channel_queue_.GetUpEnd();
75   }
76 
GetQueueDownEnd()77   common::BidiQueueEnd<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder>* GetQueueDownEnd() {
78     return channel_queue_.GetDownEnd();
79   }
80 
GetCid()81   Cid GetCid() const {
82     return cid_;
83   }
84 
GetRemoteCid()85   Cid GetRemoteCid() const {
86     return cid_;
87   }
88 
89  private:
90   // Constructor states
91   // For logging purpose only
92   const Cid cid_;
93   // For logging purpose only
94   const hci::AddressWithType device_;
95   // Needed to handle Acquire() and Release()
96   Link* link_;
97   os::Handler* l2cap_handler_;
98 
99   // User supported states
100   os::Handler* user_handler_ = nullptr;
101   FixedChannel::OnCloseCallback on_close_callback_{};
102 
103   // Internal states
104   bool acquired_ = false;
105   bool closed_ = false;
106   hci::ErrorCode close_reason_ = hci::ErrorCode::SUCCESS;
107   static constexpr size_t kChannelQueueSize = 10;
108   common::BidiQueue<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder> channel_queue_{
109       kChannelQueueSize};
110 };
111 
112 }  // namespace internal
113 }  // namespace classic
114 }  // namespace l2cap
115 }  // namespace bluetooth
116