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