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<CommandPacketBuilder>)> 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   void AckPause(LeAddressManagerCallback* callback);
70   void AckResume(LeAddressManagerCallback* callback);
71   virtual AddressPolicy Register(LeAddressManagerCallback* callback);
72   virtual void Unregister(LeAddressManagerCallback* callback);
73   AddressWithType GetCurrentAddress();          // What was set in SetRandomAddress()
74   virtual AddressWithType GetAnotherAddress();  // A new random address without rotating.
75 
76   uint8_t GetConnectListSize();
77   uint8_t GetResolvingListSize();
78   void AddDeviceToConnectList(ConnectListAddressType connect_list_address_type, Address address);
79   void AddDeviceToResolvingList(
80       PeerAddressType peer_identity_address_type,
81       Address peer_identity_address,
82       const std::array<uint8_t, 16>& peer_irk,
83       const std::array<uint8_t, 16>& local_irk);
84   void RemoveDeviceFromConnectList(ConnectListAddressType connect_list_address_type, Address address);
85   void RemoveDeviceFromResolvingList(PeerAddressType peer_identity_address_type, Address peer_identity_address);
86   void ClearConnectList();
87   void ClearResolvingList();
88   void OnCommandComplete(CommandCompleteView view);
89 
90  private:
91   void pause_registered_clients();
92   void ack_pause(LeAddressManagerCallback* callback);
93   void resume_registered_clients();
94   void ack_resume(LeAddressManagerCallback* callback);
95   void register_client(LeAddressManagerCallback* callback);
96   void unregister_client(LeAddressManagerCallback* callback);
97   void prepare_to_rotate();
98   void rotate_random_address();
99   void on_le_set_random_address_complete(CommandCompleteView view);
100   hci::Address generate_rpa();
101   hci::Address generate_nrpa();
102   std::chrono::milliseconds get_next_private_address_interval_ms();
103   void handle_next_command();
104 
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   };
121 
122   struct Command {
123     CommandType command_type;
124     std::unique_ptr<CommandPacketBuilder> command_packet;
125   };
126 
127   common::Callback<void(std::unique_ptr<CommandPacketBuilder>)> enqueue_command_;
128   os::Handler* handler_;
129   std::map<LeAddressManagerCallback*, ClientState> registered_clients_;
130 
131   AddressPolicy address_policy_ = AddressPolicy::POLICY_NOT_SET;
132   AddressWithType le_address_;
133   Address public_address_;
134   std::unique_ptr<os::Alarm> address_rotation_alarm_;
135   crypto_toolbox::Octet16 rotation_irk_;
136   std::chrono::milliseconds minimum_rotation_time_;
137   std::chrono::milliseconds maximum_rotation_time_;
138   uint8_t connect_list_size_;
139   uint8_t resolving_list_size_;
140   std::queue<Command> cached_commands_;
141 };
142 
143 }  // namespace hci
144 }  // namespace bluetooth
145