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 "hci/address.h"
21 #include "l2cap/cid.h"
22 #include "l2cap/dynamic_channel.h"
23 #include "l2cap/internal/channel_impl.h"
24 #include "l2cap/internal/ilink.h"
25 #include "l2cap/l2cap_packets.h"
26 #include "l2cap/mtu.h"
27 #include "l2cap/psm.h"
28 #include "os/handler.h"
29 #include "os/log.h"
30 
31 namespace bluetooth {
32 namespace l2cap {
33 namespace internal {
34 
35 class DynamicChannelImpl : public l2cap::internal::ChannelImpl {
36  public:
37   DynamicChannelImpl(Psm psm, Cid cid, Cid remote_cid, l2cap::internal::ILink* link, os::Handler* l2cap_handler);
38 
39   DynamicChannelImpl(const DynamicChannelImpl&) = delete;
40   DynamicChannelImpl& operator=(const DynamicChannelImpl&) = delete;
41 
42   virtual ~DynamicChannelImpl() = default;
43 
44   hci::AddressWithType GetDevice() const;
45 
46   virtual void RegisterOnCloseCallback(DynamicChannel::OnCloseCallback on_close_callback);
47 
48   virtual void Close();
49   virtual void OnClosed(hci::ErrorCode status);
50 
GetQueueUpEnd()51   common::BidiQueueEnd<packet::BasePacketBuilder, packet::PacketView<packet::kLittleEndian>>* GetQueueUpEnd() {
52     return channel_queue_.GetUpEnd();
53   }
54 
GetQueueDownEnd()55   common::BidiQueueEnd<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder>* GetQueueDownEnd() {
56     return channel_queue_.GetDownEnd();
57   }
58 
GetCid()59   virtual Cid GetCid() const {
60     return cid_;
61   }
62 
GetRemoteCid()63   virtual Cid GetRemoteCid() const {
64     return remote_cid_;
65   }
66 
GetPsm()67   virtual Psm GetPsm() const {
68     return psm_;
69   }
70 
SetChannelTxPriority(bool high_priority)71   virtual void SetChannelTxPriority(bool high_priority) {
72     link_->SetChannelTxPriority(cid_, high_priority);
73   }
74 
75   // TODO(cmanton) Do something a little bit better than this
76   bool local_initiated_{false};
77 
78  private:
79   const Psm psm_;
80   const Cid cid_;
81   const Cid remote_cid_;
82   l2cap::internal::ILink* link_;
83   os::Handler* l2cap_handler_;
84   const hci::AddressWithType device_;
85 
86   // User supported states
87   DynamicChannel::OnCloseCallback on_close_callback_{};
88 
89   // Internal states
90   bool closed_ = false;
91   hci::ErrorCode close_reason_ = hci::ErrorCode::SUCCESS;
92   static constexpr size_t kChannelQueueSize = 5;
93   common::BidiQueue<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder> channel_queue_{
94       kChannelQueueSize};
95 };
96 
97 }  // namespace internal
98 }  // namespace l2cap
99 }  // namespace bluetooth
100