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