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