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 <array>
19 #include <optional>
20 #include <string>
21 #include <unordered_set>
22 
23 #include "hci/link_key.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 ClassicDevice {
32  public:
33   ClassicDevice(ConfigCache* config, ConfigCache* memory_only_config, std::string section);
34 
35   // for move
36   ClassicDevice(ClassicDevice&& other) noexcept = default;
37   ClassicDevice& operator=(ClassicDevice&& other) noexcept = default;
38 
39   // for copy
40   ClassicDevice(const ClassicDevice& other) noexcept = default;
41   ClassicDevice& operator=(const ClassicDevice& other) noexcept = default;
42 
43   // operators
44   bool operator==(const ClassicDevice& other) const {
45     return config_ == other.config_ && memory_only_config_ == other.memory_only_config_ && section_ == other.section_;
46   }
47   bool operator!=(const ClassicDevice& other) const {
48     return !(*this == other);
49   }
50   bool operator<(const ClassicDevice& 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 ClassicDevice& rhs) const {
60     return (rhs < *this);
61   }
62   bool operator<=(const ClassicDevice& rhs) const {
63     return !(*this > rhs);
64   }
65   bool operator>=(const ClassicDevice& 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 ClassicDevice object from parsing a std::string
73   std::string ToLogString() const;
74 
75   // Get address of this classic device, it must exist
76   hci::Address GetAddress() const;
77 
78   // Return true if device has a link key in one of |kLinkKeyProperties|
79   bool IsPaired() const;
80 
81   // Property names that correspond to a link key used in Bluetooth classic device
82   static const std::unordered_set<std::string_view> kLinkKeyProperties;
83 
84  private:
85   ConfigCache* config_;
86   ConfigCache* memory_only_config_;
87   std::string section_;
88   friend std::hash<ClassicDevice>;
89 
90  public:
91   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(LinkKey, hci::LinkKey, "LinkKey");
92   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(LinkKeyType, hci::KeyType, "LinkKeyType");
93   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(SdpDiManufacturer, uint16_t, "SdpDiManufacturer");
94   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(SdpDiModel, uint16_t, "SdpDiModel");
95   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(SdpDiHardwareVersion, uint16_t, "SdpDiHardwareVersion");
96   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(SdpDiVendorIdSource, uint16_t, "SdpDiVendorIdSource");
97 };
98 
99 }  // namespace storage
100 }  // namespace bluetooth
101 
102 namespace std {
103 template <>
104 struct hash<bluetooth::storage::ClassicDevice> {
105   std::size_t operator()(const bluetooth::storage::ClassicDevice& val) const noexcept {
106     std::size_t pointer_hash_1 = std::hash<bluetooth::storage::ConfigCache*>{}(val.config_);
107     std::size_t pointer_hash_2 = std::hash<bluetooth::storage::ConfigCache*>{}(val.config_);
108     std::size_t addr_hash = std::hash<std::string>{}(val.section_);
109     return addr_hash ^ (pointer_hash_1 << 1) ^ (pointer_hash_2 << 2);
110   }
111 };
112 }  // namespace std
113