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 if (config_ != other.config_) { 52 return config_ < other.config_; 53 } 54 if (memory_only_config_ != other.memory_only_config_) { 55 return memory_only_config_ < other.memory_only_config_; 56 } 57 return section_ < other.section_; 58 } 59 bool operator>(const LeDevice& rhs) const { 60 return (rhs < *this); 61 } 62 bool operator<=(const LeDevice& rhs) const { 63 return !(*this > rhs); 64 } 65 bool operator>=(const LeDevice& rhs) const { 66 return !(*this < rhs); 67 } 68 69 // Get the parent device 70 Device Parent(); 71 72 // For logging purpose only, you can't get a LeDevice object from parsing a std::string 73 std::string ToLogString() const; 74 75 // Return true if device has a link key in one of |kLinkKeyProperties| 76 bool IsPaired() const; 77 78 // Property names that correspond to a link key used in Bluetooth LE device 79 static const std::unordered_set<std::string_view> kLinkKeyProperties; 80 81 private: 82 ConfigCache* config_; 83 ConfigCache* memory_only_config_; 84 std::string section_; 85 friend std::hash<LeDevice>; 86 87 public: 88 // Get LE address type of the key address 89 GENERATE_PROPERTY_GETTER_SETTER_REMOVER(AddressType, hci::AddressType, "AddrType"); 90 // IRK + Identity Address Type + Identity Address 91 GENERATE_PROPERTY_GETTER_SETTER_REMOVER(PeerId, std::string, "LE_KEY_PID"); 92 // LTK + RAND + EDIV + Security Level + Key Length 93 GENERATE_PROPERTY_GETTER_SETTER_REMOVER(PeerEncryptionKeys, std::string, "LE_KEY_PENC"); 94 // counter + CSRK (connection signature resolving key) + security level 95 GENERATE_PROPERTY_GETTER_SETTER_REMOVER(PeerSignatureResolvingKeys, std::string, "LE_KEY_PCSRK"); 96 97 GENERATE_PROPERTY_GETTER_SETTER_REMOVER(LegacyPseudoAddress, hci::Address, "LeLegacyPseudoAddr"); 98 }; 99 100 } // namespace storage 101 } // namespace bluetooth 102 103 namespace std { 104 template <> 105 struct hash<bluetooth::storage::LeDevice> { 106 std::size_t operator()(const bluetooth::storage::LeDevice& val) const noexcept { 107 std::size_t pointer_hash_1 = std::hash<bluetooth::storage::ConfigCache*>{}(val.config_); 108 std::size_t pointer_hash_2 = std::hash<bluetooth::storage::ConfigCache*>{}(val.config_); 109 std::size_t addr_hash = std::hash<std::string>{}(val.section_); 110 return addr_hash ^ (pointer_hash_1 << 1) ^ (pointer_hash_2 << 2); 111 } 112 }; 113 } // namespace std