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 <optional>
19 #include <string>
20 #include <unordered_set>
21 
22 #include "crypto_toolbox/crypto_toolbox.h"
23 #include "hci/hci_packets.h"
24 #include "storage/config_cache.h"
25 #include "storage/config_cache_helper.h"
26 #include "storage/device.h"
27 
28 namespace bluetooth {
29 namespace storage {
30 
31 class LeDevice {
32  public:
33   LeDevice(ConfigCache* config, ConfigCache* memory_only_config, std::string section);
34 
35   // for move
36   LeDevice(LeDevice&& other) noexcept = default;
37   LeDevice& operator=(LeDevice&& other) noexcept = default;
38 
39   // for copy
40   LeDevice(const LeDevice& other) noexcept = default;
41   LeDevice& operator=(const LeDevice& other) noexcept = default;
42 
43   // operators
44   bool operator==(const LeDevice& other) const {
45     return config_ == other.config_ && memory_only_config_ == other.memory_only_config_ && section_ == other.section_;
46   }
47   bool operator!=(const LeDevice& other) const {
48     return !(*this == other);
49   }
50   bool operator<(const LeDevice& other) const {
51     return config_ < other.config_ && memory_only_config_ < other.memory_only_config_ && section_ < other.section_;
52   }
53   bool operator>(const LeDevice& rhs) const {
54     return (rhs < *this);
55   }
56   bool operator<=(const LeDevice& rhs) const {
57     return !(*this > rhs);
58   }
59   bool operator>=(const LeDevice& rhs) const {
60     return !(*this < rhs);
61   }
62 
63   // Get the parent device
64   Device Parent();
65 
66   // For logging purpose only, you can't get a LeDevice object from parsing a std::string
67   std::string ToLogString() const;
68 
69   // Return true if device has a link key in one of |kLinkKeyProperties|
70   bool IsPaired() const;
71 
72   // Property names that correspond to a link key used in Bluetooth LE device
73   static const std::unordered_set<std::string_view> kLinkKeyProperties;
74 
75  private:
76   ConfigCache* config_;
77   ConfigCache* memory_only_config_;
78   std::string section_;
79   friend std::hash<LeDevice>;
80 
81  public:
82   // Get LE address type of the key address
83   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(AddressType, hci::AddressType, "AddrType");
84   // IRK + Identity Address Type + Identity Address
85   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(PeerId, std::string, "LE_KEY_PID");
86   // LTK + RAND + EDIV + Security Level + Key Length
87   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(PeerEncryptionKeys, std::string, "LE_KEY_PENC");
88   // counter + CSRK (connection signature resolving key) + security level
89   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(PeerSignatureResolvingKeys, std::string, "LE_KEY_PCSRK");
90 
91   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(LegacyPseudoAddress, hci::Address, "LeLegacyPseudoAddr");
92 };
93 
94 }  // namespace storage
95 }  // namespace bluetooth
96 
97 namespace std {
98 template <>
99 struct hash<bluetooth::storage::LeDevice> {
100   std::size_t operator()(const bluetooth::storage::LeDevice& val) const noexcept {
101     std::size_t pointer_hash_1 = std::hash<bluetooth::storage::ConfigCache*>{}(val.config_);
102     std::size_t pointer_hash_2 = std::hash<bluetooth::storage::ConfigCache*>{}(val.config_);
103     std::size_t addr_hash = std::hash<std::string>{}(val.section_);
104     return addr_hash ^ (pointer_hash_1 << 1) ^ (pointer_hash_2 << 2);
105   }
106 };
107 }  // namespace std