• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   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 connect_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 connect_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 ClientState {
114     WAITING_FOR_PAUSE,
115     PAUSED,
116     WAITING_FOR_RESUME,
117     RESUMED,
118   };
119 
120   enum CommandType {
121     ROTATE_RANDOM_ADDRESS,
122     ADD_DEVICE_TO_CONNECT_LIST,
123     REMOVE_DEVICE_FROM_CONNECT_LIST,
124     CLEAR_CONNECT_LIST,
125     ADD_DEVICE_TO_RESOLVING_LIST,
126     REMOVE_DEVICE_FROM_RESOLVING_LIST,
127     CLEAR_RESOLVING_LIST,
128     SET_ADDRESS_RESOLUTION_ENABLE,
129     LE_SET_PRIVACY_MODE,
130     UPDATE_IRK,
131   };
132 
133   struct RotateRandomAddressCommand {};
134 
135   struct UpdateIRKCommand {
136     crypto_toolbox::Octet16 rotation_irk;
137     std::chrono::milliseconds minimum_rotation_time;
138     std::chrono::milliseconds maximum_rotation_time;
139   };
140 
141   struct HCICommand {
142     std::unique_ptr<CommandBuilder> command;
143   };
144 
145   struct Command {
146     CommandType command_type;  // Note that this field is only intended for logging, not control flow
147     std::variant<RotateRandomAddressCommand, UpdateIRKCommand, HCICommand> contents;
148   };
149 
150   void pause_registered_clients();
151   void push_command(Command command);
152   void ack_pause(LeAddressManagerCallback* callback);
153   void resume_registered_clients();
154   void ack_resume(LeAddressManagerCallback* callback);
155   void register_client(LeAddressManagerCallback* callback);
156   void unregister_client(LeAddressManagerCallback* callback);
157   void prepare_to_rotate();
158   void rotate_random_address();
159   void schedule_rotate_random_address();
160   void set_random_address();
161   void prepare_to_update_irk(UpdateIRKCommand command);
162   void update_irk(UpdateIRKCommand command);
163   hci::Address generate_rpa();
164   hci::Address generate_nrpa();
165   void handle_next_command();
166   void check_cached_commands();
167   template <class View>
168   void on_command_complete(CommandCompleteView view);
169 
170   common::Callback<void(std::unique_ptr<CommandBuilder>)> enqueue_command_;
171   os::Handler* handler_;
172   std::map<LeAddressManagerCallback*, ClientState> registered_clients_;
173 
174   AddressWithType le_address_;
175   AddressWithType cached_address_;
176   Address public_address_;
177   std::unique_ptr<os::Alarm> address_rotation_alarm_;
178   crypto_toolbox::Octet16 rotation_irk_;
179   uint8_t connect_list_size_;
180   uint8_t resolving_list_size_;
181   std::queue<Command> cached_commands_;
182   bool supports_ble_privacy_{false};
183 };
184 
185 }  // namespace hci
186 }  // namespace bluetooth
187