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   virtual ~FixedChannelImpl() = default;
40 
GetDevice()41   hci::AddressWithType GetDevice() const {
42     return device_;
43   }
44 
45   /* Return the role we have in the associated link */
46   virtual hci::Role GetRole() const;
47 
48   virtual hci::acl_manager::LeAclConnection* GetAclConnection() const;
49 
50   virtual void RegisterOnCloseCallback(os::Handler* user_handler, FixedChannel::OnCloseCallback on_close_callback);
51 
52   virtual void Acquire();
53 
54   virtual void Release();
55 
IsAcquired()56   virtual bool IsAcquired() const {
57     return acquired_;
58   }
59 
60   Cid GetCid() const override;
61   Cid GetRemoteCid() const override;
62   virtual void OnClosed(hci::ErrorCode status);
63 
ToString()64   virtual std::string ToString() {
65     std::ostringstream ss;
66     ss << "Device " << device_ << " Cid 0x" << std::hex << cid_;
67     return ss.str();
68   }
69 
GetQueueUpEnd()70   common::BidiQueueEnd<packet::BasePacketBuilder, packet::PacketView<packet::kLittleEndian>>* GetQueueUpEnd() {
71     return channel_queue_.GetUpEnd();
72   }
73 
GetQueueDownEnd()74   common::BidiQueueEnd<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder>* GetQueueDownEnd() {
75     return channel_queue_.GetDownEnd();
76   }
77 
78   LinkOptions* GetLinkOptions();
79 
80  private:
81   // Constructor states
82   // For logging purpose only
83   const Cid cid_;
84   // For logging purpose only
85   const hci::AddressWithType device_;
86   // Needed to handle Acquire() and Release()
87   Link* link_;
88   os::Handler* l2cap_handler_;
89 
90   // User supported states
91   os::Handler* user_handler_ = nullptr;
92   FixedChannel::OnCloseCallback on_close_callback_{};
93 
94   // Internal states
95   bool acquired_ = false;
96   bool closed_ = false;
97   hci::ErrorCode close_reason_ = hci::ErrorCode::SUCCESS;
98   static constexpr size_t kChannelQueueSize = 10;
99   common::BidiQueue<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder> channel_queue_{
100       kChannelQueueSize};
101 
102   DISALLOW_COPY_AND_ASSIGN(FixedChannelImpl);
103 };
104 
105 }  // namespace internal
106 }  // namespace le
107 }  // namespace l2cap
108 }  // namespace bluetooth
109