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 <optional>
17 #include <unordered_map>
18 
19 #include "pw_bluetooth_sapphire/internal/host/common/device_address.h"
20 #include "pw_bluetooth_sapphire/internal/host/common/macros.h"
21 #include "pw_bluetooth_sapphire/internal/host/common/uint128.h"
22 
23 namespace bt::gap {
24 
25 // This class provides functions to obtain an identity address by resolving a
26 // given RPA. Resolution is performed using identity information stored in the
27 // registry.
28 //
29 // TODO(fxbug.dev/42164183): Manage the controller-based list here.
30 class IdentityResolvingList final {
31  public:
32   IdentityResolvingList() = default;
33   ~IdentityResolvingList() = default;
34 
35   // Associate the given |irk| with |identity|. If |identity| is already in the
36   // list, the existing entry is updated with the new IRK.
37   void Add(DeviceAddress identity, const UInt128& irk);
38 
39   // Delete |identity| and associated IRK, if present.
40   void Remove(DeviceAddress identity);
41 
42   // Tries to resolve the given RPA against the identities in the registry.
43   // Returns std::nullopt if the address is not a RPA or cannot be resolved.
44   // Otherwise, returns a value containing the identity address.
45   std::optional<DeviceAddress> Resolve(DeviceAddress rpa) const;
46 
47  private:
48   // Maps identity addresses to IRKs.
49   std::unordered_map<DeviceAddress, UInt128> registry_;
50 
51   BT_DISALLOW_COPY_ASSIGN_AND_MOVE(IdentityResolvingList);
52 };
53 
54 }  // namespace bt::gap
55