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 #pragma once 17 18 #include <bluetooth/log.h> 19 20 #include <map> 21 #include <variant> 22 23 #include "common/callback.h" 24 #include "hci/address_with_type.h" 25 #include "hci/controller.h" 26 #include "hci/octets.h" 27 #include "os/alarm.h" 28 29 namespace bluetooth { 30 namespace hci { 31 32 constexpr std::chrono::milliseconds kUnregisterSyncTimeoutInMs = std::chrono::milliseconds(10); 33 34 class LeAddressManagerCallback { 35 public: 36 virtual ~LeAddressManagerCallback() = default; 37 virtual void OnPause() = 0; 38 virtual void OnResume() = 0; NotifyOnIRKChange()39 virtual void NotifyOnIRKChange() {} 40 }; 41 42 struct PrivateAddressIntervalRange { 43 std::chrono::milliseconds min; 44 std::chrono::milliseconds max; 45 }; 46 47 class LeAddressManager { 48 public: 49 LeAddressManager(common::Callback<void(std::unique_ptr<CommandBuilder>)> enqueue_command, 50 os::Handler* handler, Address public_address, uint8_t accept_list_size, 51 uint8_t resolving_list_size, Controller* controller); 52 virtual ~LeAddressManager(); 53 54 enum AddressPolicy { 55 POLICY_NOT_SET, 56 USE_PUBLIC_ADDRESS, 57 USE_STATIC_ADDRESS, 58 USE_NON_RESOLVABLE_ADDRESS, 59 USE_RESOLVABLE_ADDRESS 60 }; 61 62 // Aborts if called more than once 63 void SetPrivacyPolicyForInitiatorAddress(AddressPolicy address_policy, 64 AddressWithType fixed_address, Octet16 rotation_irk, 65 bool supports_ble_privacy, 66 std::chrono::milliseconds minimum_rotation_time, 67 std::chrono::milliseconds maximum_rotation_time); 68 // TODO(jpawlowski): remove once we have config file abstraction in cert tests 69 void SetPrivacyPolicyForInitiatorAddressForTest(AddressPolicy address_policy, 70 AddressWithType fixed_address, 71 Octet16 rotation_irk, 72 std::chrono::milliseconds minimum_rotation_time, 73 std::chrono::milliseconds maximum_rotation_time); 74 AddressPolicy GetAddressPolicy(); 75 bool RotatingAddress(); 76 virtual void AckPause(LeAddressManagerCallback* callback); 77 virtual void AckResume(LeAddressManagerCallback* callback); 78 virtual AddressPolicy Register(LeAddressManagerCallback* callback); 79 virtual void Unregister(LeAddressManagerCallback* callback); 80 virtual bool UnregisterSync(LeAddressManagerCallback* callback, 81 std::chrono::milliseconds timeout = kUnregisterSyncTimeoutInMs); 82 virtual AddressWithType GetInitiatorAddress(); // What was set in SetRandomAddress() 83 virtual AddressWithType NewResolvableAddress(); // A new random address without rotating. 84 virtual AddressWithType NewNonResolvableAddress(); // A new non-resolvable address 85 86 uint8_t GetFilterAcceptListSize(); 87 uint8_t GetResolvingListSize(); 88 void AddDeviceToFilterAcceptList(FilterAcceptListAddressType accept_list_address_type, 89 Address address); 90 void AddDeviceToResolvingList(PeerAddressType peer_identity_address_type, 91 Address peer_identity_address, 92 const std::array<uint8_t, 16>& peer_irk, 93 const std::array<uint8_t, 16>& local_irk); 94 void RemoveDeviceFromFilterAcceptList(FilterAcceptListAddressType accept_list_address_type, 95 Address address); 96 void RemoveDeviceFromResolvingList(PeerAddressType peer_identity_address_type, 97 Address peer_identity_address); 98 void ClearFilterAcceptList(); 99 void ClearResolvingList(); 100 void OnCommandComplete(CommandCompleteView view); 101 std::chrono::milliseconds GetNextPrivateAddressIntervalMs(); 102 PrivateAddressIntervalRange GetNextPrivateAddressIntervalRange(const std::string& client_name); 103 void CheckAddressRotationHappenedInExpectedTimeInterval( 104 const std::chrono::time_point<std::chrono::system_clock>& interval_min, 105 const std::chrono::time_point<std::chrono::system_clock>& interval_max, 106 const std::chrono::time_point<std::chrono::system_clock>& event_time, 107 const std::string& client_name); 108 109 // Unsynchronized check for testing purposes NumberCachedCommands()110 size_t NumberCachedCommands() const { return cached_commands_.size(); } 111 112 protected: 113 AddressPolicy address_policy_ = AddressPolicy::POLICY_NOT_SET; 114 std::chrono::milliseconds minimum_rotation_time_; 115 std::chrono::milliseconds maximum_rotation_time_; 116 117 private: 118 enum class ClientState; 119 std::string ClientStateText(const ClientState cs); 120 121 enum CommandType { 122 ROTATE_RANDOM_ADDRESS, 123 ADD_DEVICE_TO_ACCEPT_LIST, 124 REMOVE_DEVICE_FROM_ACCEPT_LIST, 125 CLEAR_ACCEPT_LIST, 126 ADD_DEVICE_TO_RESOLVING_LIST, 127 REMOVE_DEVICE_FROM_RESOLVING_LIST, 128 CLEAR_RESOLVING_LIST, 129 SET_ADDRESS_RESOLUTION_ENABLE, 130 LE_SET_PRIVACY_MODE, 131 UPDATE_IRK, 132 }; 133 134 struct RotateRandomAddressCommand {}; 135 136 struct UpdateIRKCommand { 137 Octet16 rotation_irk; 138 std::chrono::milliseconds minimum_rotation_time; 139 std::chrono::milliseconds maximum_rotation_time; 140 }; 141 142 struct HCICommand { 143 std::unique_ptr<CommandBuilder> command; 144 }; 145 146 struct Command { 147 CommandType 148 command_type; // Note that this field is only intended for logging, not control flow 149 std::variant<RotateRandomAddressCommand, UpdateIRKCommand, HCICommand> contents; 150 }; 151 152 void pause_registered_clients(); 153 void push_command(Command command); 154 void ack_pause(LeAddressManagerCallback* callback); 155 void resume_registered_clients(); 156 void ack_resume(LeAddressManagerCallback* callback); 157 void register_client(LeAddressManagerCallback* callback); 158 void unregister_client(LeAddressManagerCallback* callback); 159 void prepare_to_rotate(); 160 void rotate_random_address(); 161 void schedule_rotate_random_address(); 162 void set_random_address(); 163 void prepare_to_update_irk(UpdateIRKCommand command); 164 void update_irk(UpdateIRKCommand command); 165 hci::Address generate_rpa(); 166 hci::Address generate_nrpa(); 167 void handle_next_command(); 168 void check_cached_commands(); 169 template <class View> 170 void on_command_complete(CommandCompleteView view); 171 172 common::Callback<void(std::unique_ptr<CommandBuilder>)> enqueue_command_; 173 os::Handler* handler_; 174 std::map<LeAddressManagerCallback*, ClientState> registered_clients_; 175 176 AddressWithType le_address_; 177 AddressWithType cached_address_; 178 Address public_address_; 179 std::unique_ptr<os::Alarm> address_rotation_wake_alarm_; 180 std::unique_ptr<os::Alarm> address_rotation_non_wake_alarm_; 181 Octet16 rotation_irk_; 182 uint8_t accept_list_size_; 183 uint8_t resolving_list_size_; 184 std::queue<Command> cached_commands_; 185 bool supports_ble_privacy_{false}; 186 187 // Only used for logging error in address rotation time. 188 std::optional<std::chrono::time_point<std::chrono::system_clock>> address_rotation_interval_min; 189 std::optional<std::chrono::time_point<std::chrono::system_clock>> address_rotation_interval_max; 190 191 Controller* controller_; 192 }; 193 194 } // namespace hci 195 } // namespace bluetooth 196