• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SetChannelTxPriority(bool high_priority)69   virtual void SetChannelTxPriority(bool high_priority) {
70     link_->SetChannelTxPriority(cid_, high_priority);
71   }
72 
73   // TODO(cmanton) Do something a little bit better than this
74   bool local_initiated_{false};
75 
76  private:
77   const Psm psm_;
78   const Cid cid_;
79   const Cid remote_cid_;
80   l2cap::internal::ILink* link_;
81   os::Handler* l2cap_handler_;
82   const hci::AddressWithType device_;
83 
84   // User supported states
85   DynamicChannel::OnCloseCallback on_close_callback_{};
86 
87   // Internal states
88   bool closed_ = false;
89   hci::ErrorCode close_reason_ = hci::ErrorCode::SUCCESS;
90   static constexpr size_t kChannelQueueSize = 5;
91   common::BidiQueue<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder> channel_queue_{
92       kChannelQueueSize};
93 
94   DISALLOW_COPY_AND_ASSIGN(DynamicChannelImpl);
95 };
96 
97 }  // namespace internal
98 }  // namespace l2cap
99 }  // namespace bluetooth
100