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