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 22 #include "common/callback.h" 23 #include "hci/address_with_type.h" 24 #include "hci/hci_layer.h" 25 #include "os/alarm.h" 26 27 namespace bluetooth { 28 namespace hci { 29 30 class LeAddressManagerCallback { 31 public: 32 virtual ~LeAddressManagerCallback() = default; 33 virtual void OnPause() = 0; 34 virtual void OnResume() = 0; 35 }; 36 37 class LeAddressManager { 38 public: 39 LeAddressManager( 40 common::Callback<void(std::unique_ptr<CommandBuilder>)> enqueue_command, 41 os::Handler* handler, 42 Address public_address, 43 uint8_t connect_list_size, 44 uint8_t resolving_list_size); 45 virtual ~LeAddressManager(); 46 47 enum AddressPolicy { 48 POLICY_NOT_SET, 49 USE_PUBLIC_ADDRESS, 50 USE_STATIC_ADDRESS, 51 USE_NON_RESOLVABLE_ADDRESS, 52 USE_RESOLVABLE_ADDRESS 53 }; 54 55 // Aborts if called more than once 56 void SetPrivacyPolicyForInitiatorAddress( 57 AddressPolicy address_policy, 58 AddressWithType fixed_address, 59 crypto_toolbox::Octet16 rotation_irk, 60 std::chrono::milliseconds minimum_rotation_time, 61 std::chrono::milliseconds maximum_rotation_time); 62 // TODO(jpawlowski): remove once we have config file abstraction in cert tests 63 void SetPrivacyPolicyForInitiatorAddressForTest( 64 AddressPolicy address_policy, 65 AddressWithType fixed_address, 66 crypto_toolbox::Octet16 rotation_irk, 67 std::chrono::milliseconds minimum_rotation_time, 68 std::chrono::milliseconds maximum_rotation_time); 69 AddressPolicy GetAddressPolicy(); 70 void AckPause(LeAddressManagerCallback* callback); 71 void AckResume(LeAddressManagerCallback* callback); 72 virtual AddressPolicy Register(LeAddressManagerCallback* callback); 73 virtual void Unregister(LeAddressManagerCallback* callback); 74 AddressWithType GetCurrentAddress(); // What was set in SetRandomAddress() 75 virtual AddressWithType GetAnotherAddress(); // A new random address without rotating. 76 77 uint8_t GetConnectListSize(); 78 uint8_t GetResolvingListSize(); 79 void AddDeviceToConnectList(ConnectListAddressType connect_list_address_type, Address address); 80 void AddDeviceToResolvingList( 81 PeerAddressType peer_identity_address_type, 82 Address peer_identity_address, 83 const std::array<uint8_t, 16>& peer_irk, 84 const std::array<uint8_t, 16>& local_irk); 85 void RemoveDeviceFromConnectList(ConnectListAddressType connect_list_address_type, Address address); 86 void RemoveDeviceFromResolvingList(PeerAddressType peer_identity_address_type, Address peer_identity_address); 87 void ClearConnectList(); 88 void ClearResolvingList(); 89 void OnCommandComplete(CommandCompleteView view); 90 std::chrono::milliseconds GetNextPrivateAddressIntervalMs(); 91 92 private: 93 enum ClientState { 94 WAITING_FOR_PAUSE, 95 PAUSED, 96 WAITING_FOR_RESUME, 97 RESUMED, 98 }; 99 100 enum CommandType { 101 ROTATE_RANDOM_ADDRESS, 102 ADD_DEVICE_TO_CONNECT_LIST, 103 REMOVE_DEVICE_FROM_CONNECT_LIST, 104 CLEAR_CONNECT_LIST, 105 ADD_DEVICE_TO_RESOLVING_LIST, 106 REMOVE_DEVICE_FROM_RESOLVING_LIST, 107 CLEAR_RESOLVING_LIST 108 }; 109 110 struct Command { 111 CommandType command_type; 112 std::unique_ptr<CommandBuilder> command_packet; 113 }; 114 115 void pause_registered_clients(); 116 void push_command(Command command); 117 void ack_pause(LeAddressManagerCallback* callback); 118 void resume_registered_clients(); 119 void ack_resume(LeAddressManagerCallback* callback); 120 void register_client(LeAddressManagerCallback* callback); 121 void unregister_client(LeAddressManagerCallback* callback); 122 void prepare_to_rotate(); 123 void rotate_random_address(); 124 void schedule_rotate_random_address(); 125 void set_random_address(); 126 hci::Address generate_rpa(); 127 hci::Address generate_nrpa(); 128 void handle_next_command(); 129 130 common::Callback<void(std::unique_ptr<CommandBuilder>)> enqueue_command_; 131 os::Handler* handler_; 132 std::map<LeAddressManagerCallback*, ClientState> registered_clients_; 133 134 AddressPolicy address_policy_ = AddressPolicy::POLICY_NOT_SET; 135 AddressWithType le_address_; 136 AddressWithType cached_address_; 137 Address public_address_; 138 std::unique_ptr<os::Alarm> address_rotation_alarm_; 139 crypto_toolbox::Octet16 rotation_irk_; 140 std::chrono::milliseconds minimum_rotation_time_; 141 std::chrono::milliseconds maximum_rotation_time_; 142 uint8_t connect_list_size_; 143 uint8_t resolving_list_size_; 144 std::queue<Command> cached_commands_; 145 }; 146 147 } // namespace hci 148 } // namespace bluetooth 149