• 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/acl_manager/le_acl_connection.h"
21 #include "l2cap/cid.h"
22 #include "l2cap/internal/channel_impl.h"
23 #include "l2cap/le/fixed_channel.h"
24 #include "l2cap/le/link_options.h"
25 #include "os/handler.h"
26 #include "os/log.h"
27 
28 namespace bluetooth {
29 namespace l2cap {
30 namespace le {
31 namespace internal {
32 
33 class Link;
34 
35 class FixedChannelImpl : public l2cap::internal::ChannelImpl {
36  public:
37   FixedChannelImpl(Cid cid, Link* link, os::Handler* l2cap_handler);
38 
39   FixedChannelImpl(const FixedChannelImpl&) = delete;
40   FixedChannelImpl& operator=(const FixedChannelImpl&) = delete;
41 
42   virtual ~FixedChannelImpl() = default;
43 
GetDevice()44   hci::AddressWithType GetDevice() const {
45     return device_;
46   }
47 
48   /* Return the role we have in the associated link */
49   virtual hci::Role GetRole() const;
50 
51   virtual hci::acl_manager::LeAclConnection* GetAclConnection() const;
52 
53   virtual void RegisterOnCloseCallback(os::Handler* user_handler, FixedChannel::OnCloseCallback on_close_callback);
54 
55   virtual void Acquire();
56 
57   virtual void Release();
58 
IsAcquired()59   virtual bool IsAcquired() const {
60     return acquired_;
61   }
62 
63   Cid GetCid() const override;
64   Cid GetRemoteCid() const override;
65   virtual void OnClosed(hci::ErrorCode status);
66 
ToString()67   virtual std::string ToString() {
68     std::ostringstream ss;
69     ss << "Device " << device_ << " 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 
81   LinkOptions* GetLinkOptions();
82 
83  private:
84   // Constructor states
85   // For logging purpose only
86   const Cid cid_;
87   // For logging purpose only
88   const hci::AddressWithType device_;
89   // Needed to handle Acquire() and Release()
90   Link* link_;
91   os::Handler* l2cap_handler_;
92 
93   // User supported states
94   os::Handler* user_handler_ = nullptr;
95   FixedChannel::OnCloseCallback on_close_callback_{};
96 
97   // Internal states
98   bool acquired_ = false;
99   bool closed_ = false;
100   hci::ErrorCode close_reason_ = hci::ErrorCode::SUCCESS;
101   static constexpr size_t kChannelQueueSize = 10;
102   common::BidiQueue<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder> channel_queue_{
103       kChannelQueueSize};
104 };
105 
106 }  // namespace internal
107 }  // namespace le
108 }  // namespace l2cap
109 }  // namespace bluetooth
110