1 /*
2  * Copyright 2020 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 <memory>
20 #include <variant>
21 
22 #include "hci/acl_manager/acl_connection.h"
23 #include "hci/acl_manager/le_connection_management_callbacks.h"
24 #include "hci/address_with_type.h"
25 #include "hci/hci_packets.h"
26 #include "hci/le_acl_connection_interface.h"
27 
28 namespace bluetooth {
29 namespace hci {
30 namespace acl_manager {
31 
32 struct DataAsCentral {
33   // the address used when initiating the connection
34   AddressWithType local_address;
35 };
36 
37 struct DataAsPeripheral {
38   // the address of the advertising set that the peer connected to
39   AddressWithType local_address;
40   // the advertising set ID that the peer connected to - in LE, our role is peripheral iff the peer
41   // initiated a connection to our advertisement
42   std::optional<uint8_t> advertising_set_id;
43   // whether the peripheral connected to a discoverable advertisement (this affects the readability
44   // of GAP characteristics)
45   bool connected_to_discoverable;
46 };
47 
48 // when we know it's a peripheral, but we don't yet have all the data about the set it connected to
49 // this state should never remain after the connection is fully populated
50 struct DataAsUninitializedPeripheral {};
51 
52 using RoleSpecificData =
53     std::variant<DataAsUninitializedPeripheral, DataAsCentral, DataAsPeripheral>;
54 
55 class LeAclConnection : public AclConnection {
56  public:
57   LeAclConnection();
58   LeAclConnection(
59       std::shared_ptr<Queue> queue,
60       LeAclConnectionInterface* le_acl_connection_interface,
61       uint16_t handle,
62       RoleSpecificData role_specific_data,
63       AddressWithType remote_address);
64   LeAclConnection(const LeAclConnection&) = delete;
65   LeAclConnection& operator=(const LeAclConnection&) = delete;
66 
67   ~LeAclConnection();
68 
69   virtual AddressWithType GetLocalAddress() const;
70 
71   virtual Role GetRole() const;
72 
73   const RoleSpecificData& GetRoleSpecificData() const;
74 
UpdateRoleSpecificData(RoleSpecificData role_specific_data)75   void UpdateRoleSpecificData(RoleSpecificData role_specific_data) {
76     role_specific_data_ = role_specific_data;
77   }
78 
GetRemoteAddress()79   virtual AddressWithType GetRemoteAddress() const {
80     return remote_address_;
81   }
82 
83   // The peer address and type returned from the Connection Complete Event
84   AddressWithType peer_address_with_type_;
85 
86   // 5.2::7.7.65.10 Connection interval used on this connection.
87   // Range: 0x0006 to 0x0C80
88   // Time = N * 1.25 ms
89   // Time Range: 7.5 ms to 4000 ms.
90   uint16_t interval_;
91   // 5.2::7.7.65.10 Peripheral latency for the connection in number of connection events.
92   // Range: 0x0000 to 0x01F3
93   uint16_t latency_;
94   // 5.2::7.7.65.10 Connection supervision timeout.
95   // Range: 0x000A to 0x0C80
96   // Time = N * 10 ms
97   // Time Range: 100 ms to 32 s
98   uint16_t supervision_timeout_;
99 
100   // True if connection address was in the filter accept list, false otherwise
101   bool in_filter_accept_list_;
IsInFilterAcceptList()102   bool IsInFilterAcceptList() const {
103     return in_filter_accept_list_;
104   }
105 
106   Address local_resolvable_private_address_ = Address::kEmpty;
107   Address peer_resolvable_private_address_ = Address::kEmpty;
108 
GetPeerAddress()109   virtual AddressWithType GetPeerAddress() const {
110     return peer_address_with_type_;
111   }
112 
113   // This function return actual peer address which was used for the connection over the air.
GetPeerOtaAddress()114   virtual AddressWithType GetPeerOtaAddress() const {
115     if (peer_resolvable_private_address_ == Address::kEmpty) {
116       return GetPeerAddress();
117     }
118     return AddressWithType(peer_resolvable_private_address_, AddressType::RANDOM_DEVICE_ADDRESS);
119   }
120 
121   // This function return actual local address which was used for the connection over the air.
GetLocalOtaAddress()122   virtual AddressWithType GetLocalOtaAddress() const {
123     if (local_resolvable_private_address_ == Address::kEmpty) {
124       return GetLocalAddress();
125     }
126 
127     return AddressWithType(local_resolvable_private_address_, AddressType::RANDOM_DEVICE_ADDRESS);
128   }
129 
130   virtual void RegisterCallbacks(LeConnectionManagementCallbacks* callbacks, os::Handler* handler);
131   virtual void Disconnect(DisconnectReason reason);
132 
133   virtual bool LeConnectionUpdate(
134       uint16_t conn_interval_min,
135       uint16_t conn_interval_max,
136       uint16_t conn_latency,
137       uint16_t supervision_timeout,
138       uint16_t min_ce_length,
139       uint16_t max_ce_length);
140 
141   virtual bool ReadRemoteVersionInformation() override;
142   virtual bool LeReadRemoteFeatures();
143 
144   virtual void LeSubrateRequest(
145       uint16_t subrate_min, uint16_t subrate_max, uint16_t max_latency, uint16_t cont_num, uint16_t sup_tout);
146 
147   // TODO implement LeRemoteConnectionParameterRequestReply, LeRemoteConnectionParameterRequestNegativeReply
148 
149   // Called once before passing the connection to the client
150   virtual LeConnectionManagementCallbacks* GetEventCallbacks(std::function<void(uint16_t)> invalidate_callbacks);
151 
152  protected:
153   AddressWithType remote_address_;
154   RoleSpecificData role_specific_data_;
155 
156  private:
157   void OnLeSubrateRequestStatus(CommandStatusView status);
158   virtual bool check_connection_parameters(
159       uint16_t conn_interval_min,
160       uint16_t conn_interval_max,
161       uint16_t expected_conn_latency,
162       uint16_t expected_supervision_timeout);
163   struct impl;
164   struct impl* pimpl_ = nullptr;
165 };
166 
167 }  // namespace acl_manager
168 }  // namespace hci
169 }  // namespace bluetooth
170