1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #pragma once
16 #include "pw_async/heap_dispatcher.h"
17 #include "pw_bluetooth_sapphire/internal/host/common/device_address.h"
18 #include "pw_bluetooth_sapphire/internal/host/hci/local_address_delegate.h"
19 
20 namespace bt::hci {
21 
22 class FakeLocalAddressDelegate : public LocalAddressDelegate {
23  public:
FakeLocalAddressDelegate(pw::async::Dispatcher & pw_dispatcher)24   explicit FakeLocalAddressDelegate(pw::async::Dispatcher& pw_dispatcher)
25       : heap_dispatcher_(pw_dispatcher) {}
26   ~FakeLocalAddressDelegate() override = default;
27 
28   void EnablePrivacy(bool enabled);
29 
30   // Returns true if the privacy feature is currently enabled.
privacy_enabled()31   bool privacy_enabled() const { return privacy_enabled_; }
32 
irk()33   std::optional<UInt128> irk() const override { return std::nullopt; }
identity_address()34   DeviceAddress identity_address() const override { return identity_address_; }
35   void EnsureLocalAddress(std::optional<DeviceAddress::Type> address_type,
36                           AddressCallback callback) override;
37 
38   // Assign a callback to be notified any time the LE address changes.
register_address_changed_callback(fit::closure callback)39   void register_address_changed_callback(fit::closure callback) {
40     address_changed_callback_ = std::move(callback);
41   }
42 
43   void UpdateRandomAddress(DeviceAddress& address);
44 
current_address()45   const DeviceAddress current_address() const {
46     return (privacy_enabled_ && random_) ? random_.value() : identity_address_;
47   }
48 
49   // If set to true EnsureLocalAddress runs its callback asynchronously.
set_async(bool value)50   void set_async(bool value) { async_ = value; }
51 
set_identity_address(const DeviceAddress & value)52   void set_identity_address(const DeviceAddress& value) {
53     identity_address_ = value;
54   }
set_local_address(const DeviceAddress & value)55   void set_local_address(const DeviceAddress& value) { local_address_ = value; }
56 
57  private:
58   fit::closure address_changed_callback_;
59 
60   bool async_ = false;
61 
62   bool privacy_enabled_ = false;
63 
64   // The random device address assigned to the controller if privacy is enabled.
65   std::optional<DeviceAddress> random_;
66 
67   // LE public address
68   DeviceAddress identity_address_ =
69       DeviceAddress(DeviceAddress::Type::kLEPublic, {0});
70 
71   // LE resolvable private address
72   DeviceAddress local_address_ =
73       DeviceAddress(DeviceAddress::Type::kLERandom, {0});
74 
75   pw::async::HeapDispatcher heap_dispatcher_;
76 };
77 
78 }  // namespace bt::hci
79