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 <lib/fit/function.h>
17 #include <lib/fit/result.h>
18 
19 #include <optional>
20 
21 #include "pw_bluetooth_sapphire/internal/host/common/device_address.h"
22 #include "pw_bluetooth_sapphire/internal/host/common/host_error.h"
23 #include "pw_bluetooth_sapphire/internal/host/common/uint128.h"
24 
25 namespace bt::hci {
26 
27 // Delegate interface for obtaining the host-maintained local address and
28 // identity information for the system.
29 class LocalAddressDelegate {
30  public:
31   virtual ~LocalAddressDelegate() = default;
32 
33   // Returns the currently assigned IRK, if any.
34   virtual std::optional<UInt128> irk() const = 0;
35 
36   // Returns the identity address.
37   virtual DeviceAddress identity_address() const = 0;
38 
39   // Asynchronously returns the local LE controller address used by all LE link
40   // layer procedures with the exception of 5.0 advertising sets. These include:
41   //   - Legacy and extended scan requests;
42   //   - Legacy and extended connection initiation;
43   //   - Legacy advertising.
44   //
45   // There are two kinds of address that can be returned by this function:
46   //   - A public device address (BD_ADDR) shared with the BR/EDR transport and
47   //     typically factory-assigned.
48   //   - A random device address that has been assigned to the controller by the
49   //     host using the HCI_LE_Set_Random_Address command.
50   //
51   // This method runs |callback| when the procedure ends. |callback| may run
52   // synchronously or asynchronously.
53   //
54   // The address returned will be a public address in either of these two cases:
55   //  * |address_type| is public (i.e. public type was explicitly requested by
56   //    a privileged client)
57   //  * privacy is not enabled and |address_type| is unspecified (i.e. nullopt)
58   // Note that requesting a random address while privacy is not enabled is not
59   // allowed. If none of the above is true, then it can only mean that privacy
60   // is enabled and:
61   //  * |address_type| is random (i.e. random type was explicitly requested by
62   //    any client)
63   //  * |address_type| was unspecified (in which case, we still want to
64   //    advertise a random type)
65   //
66   // This method runs |callback| when the procedure ends. |callback| may run
67   // synchronously or asynchronously.
68   using AddressCallback =
69       fit::function<void(fit::result<HostError, const DeviceAddress>)>;
70   virtual void EnsureLocalAddress(
71       std::optional<DeviceAddress::Type> address_type,
72       AddressCallback callback) = 0;
73 };
74 
75 // Interface to be implemented by all objects that are interested in and/or can
76 // prevent the configuration of a local private address.
77 class LocalAddressClient {
78  public:
79   virtual ~LocalAddressClient() = default;
80 
81   // Returns true if the procedures managed by this client do not currently
82   // prevent the reconfiguration of the controller LE random address.
83   virtual bool AllowsRandomAddressChange() const = 0;
84 };
85 
86 }  // namespace bt::hci
87