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/octets.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 accept_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       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       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(
81       LeAddressManagerCallback* callback,
82       std::chrono::milliseconds timeout = kUnregisterSyncTimeoutInMs);
83   virtual AddressWithType GetInitiatorAddress();      // What was set in SetRandomAddress()
84   virtual AddressWithType NewResolvableAddress();     // A new random address without rotating.
85   virtual AddressWithType NewNonResolvableAddress();  // A new non-resolvable address
86 
87   uint8_t GetFilterAcceptListSize();
88   uint8_t GetResolvingListSize();
89   void AddDeviceToFilterAcceptList(FilterAcceptListAddressType accept_list_address_type, Address address);
90   void AddDeviceToResolvingList(
91       PeerAddressType peer_identity_address_type,
92       Address peer_identity_address,
93       const std::array<uint8_t, 16>& peer_irk,
94       const std::array<uint8_t, 16>& local_irk);
95   void RemoveDeviceFromFilterAcceptList(FilterAcceptListAddressType accept_list_address_type, Address address);
96   void RemoveDeviceFromResolvingList(PeerAddressType peer_identity_address_type, Address peer_identity_address);
97   void ClearFilterAcceptList();
98   void ClearResolvingList();
99   void OnCommandComplete(CommandCompleteView view);
100   std::chrono::milliseconds GetNextPrivateAddressIntervalMs();
101 
102   // Unsynchronized check for testing purposes
NumberCachedCommands()103   size_t NumberCachedCommands() const {
104     return cached_commands_.size();
105   }
106 
107  protected:
108   AddressPolicy address_policy_ = AddressPolicy::POLICY_NOT_SET;
109   std::chrono::milliseconds minimum_rotation_time_;
110   std::chrono::milliseconds maximum_rotation_time_;
111 
112  private:
113   enum class ClientState;
114   std::string ClientStateText(const ClientState cs);
115 
116   enum CommandType {
117     ROTATE_RANDOM_ADDRESS,
118     ADD_DEVICE_TO_ACCEPT_LIST,
119     REMOVE_DEVICE_FROM_ACCEPT_LIST,
120     CLEAR_ACCEPT_LIST,
121     ADD_DEVICE_TO_RESOLVING_LIST,
122     REMOVE_DEVICE_FROM_RESOLVING_LIST,
123     CLEAR_RESOLVING_LIST,
124     SET_ADDRESS_RESOLUTION_ENABLE,
125     LE_SET_PRIVACY_MODE,
126     UPDATE_IRK,
127   };
128 
129   struct RotateRandomAddressCommand {};
130 
131   struct UpdateIRKCommand {
132     Octet16 rotation_irk;
133     std::chrono::milliseconds minimum_rotation_time;
134     std::chrono::milliseconds maximum_rotation_time;
135   };
136 
137   struct HCICommand {
138     std::unique_ptr<CommandBuilder> command;
139   };
140 
141   struct Command {
142     CommandType command_type;  // Note that this field is only intended for logging, not control flow
143     std::variant<RotateRandomAddressCommand, UpdateIRKCommand, HCICommand> contents;
144   };
145 
146   void pause_registered_clients();
147   void push_command(Command command);
148   void ack_pause(LeAddressManagerCallback* callback);
149   void resume_registered_clients();
150   void ack_resume(LeAddressManagerCallback* callback);
151   void register_client(LeAddressManagerCallback* callback);
152   void unregister_client(LeAddressManagerCallback* callback);
153   void prepare_to_rotate();
154   void rotate_random_address();
155   void schedule_rotate_random_address();
156   void set_random_address();
157   void prepare_to_update_irk(UpdateIRKCommand command);
158   void update_irk(UpdateIRKCommand command);
159   hci::Address generate_rpa();
160   hci::Address generate_nrpa();
161   void handle_next_command();
162   void check_cached_commands();
163   template <class View>
164   void on_command_complete(CommandCompleteView view);
165 
166   common::Callback<void(std::unique_ptr<CommandBuilder>)> enqueue_command_;
167   os::Handler* handler_;
168   std::map<LeAddressManagerCallback*, ClientState> registered_clients_;
169 
170   AddressWithType le_address_;
171   AddressWithType cached_address_;
172   Address public_address_;
173   std::unique_ptr<os::Alarm> address_rotation_alarm_;
174   Octet16 rotation_irk_;
175   uint8_t accept_list_size_;
176   uint8_t resolving_list_size_;
177   std::queue<Command> cached_commands_;
178   bool supports_ble_privacy_{false};
179 };
180 
181 }  // namespace hci
182 }  // namespace bluetooth
183