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   virtual ~DynamicChannelImpl() = default;
40 
41   hci::AddressWithType GetDevice() const;
42 
43   virtual void RegisterOnCloseCallback(DynamicChannel::OnCloseCallback on_close_callback);
44 
45   virtual void Close();
46   virtual void OnClosed(hci::ErrorCode status);
47   virtual std::string ToString();
48 
GetQueueUpEnd()49   common::BidiQueueEnd<packet::BasePacketBuilder, packet::PacketView<packet::kLittleEndian>>* GetQueueUpEnd() {
50     return channel_queue_.GetUpEnd();
51   }
52 
GetQueueDownEnd()53   common::BidiQueueEnd<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder>* GetQueueDownEnd() {
54     return channel_queue_.GetDownEnd();
55   }
56 
GetCid()57   virtual Cid GetCid() const {
58     return cid_;
59   }
60 
GetRemoteCid()61   virtual Cid GetRemoteCid() const {
62     return remote_cid_;
63   }
64 
GetPsm()65   virtual Psm GetPsm() const {
66     return psm_;
67   }
68 
69   // TODO(cmanton) Do something a little bit better than this
70   bool local_initiated_{false};
71 
72  private:
73   const Psm psm_;
74   const Cid cid_;
75   const Cid remote_cid_;
76   l2cap::internal::ILink* link_;
77   os::Handler* l2cap_handler_;
78   const hci::AddressWithType device_;
79 
80   // User supported states
81   DynamicChannel::OnCloseCallback on_close_callback_{};
82 
83   // Internal states
84   bool closed_ = false;
85   hci::ErrorCode close_reason_ = hci::ErrorCode::SUCCESS;
86   static constexpr size_t kChannelQueueSize = 5;
87   common::BidiQueue<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder> channel_queue_{
88       kChannelQueueSize};
89 
90   DISALLOW_COPY_AND_ASSIGN(DynamicChannelImpl);
91 };
92 
93 }  // namespace internal
94 }  // namespace l2cap
95 }  // namespace bluetooth
96