• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hci/uuid.h"
25 #include "storage/config_cache.h"
26 #include "storage/config_cache_helper.h"
27 #include "storage/device.h"
28 
29 namespace bluetooth {
30 namespace storage {
31 
32 class ClassicDevice {
33  public:
34   ClassicDevice(ConfigCache* config, ConfigCache* memory_only_config, std::string section);
35 
36   // for move
37   ClassicDevice(ClassicDevice&& other) noexcept = default;
38   ClassicDevice& operator=(ClassicDevice&& other) noexcept = default;
39 
40   // for copy
41   ClassicDevice(const ClassicDevice& other) noexcept = default;
42   ClassicDevice& operator=(const ClassicDevice& other) noexcept = default;
43 
44   // operators
45   bool operator==(const ClassicDevice& other) const {
46     return config_ == other.config_ && memory_only_config_ == other.memory_only_config_ && section_ == other.section_;
47   }
48   bool operator!=(const ClassicDevice& other) const {
49     return !(*this == other);
50   }
51   bool operator<(const ClassicDevice& other) const {
52     if (config_ != other.config_) {
53       return config_ < other.config_;
54     }
55     if (memory_only_config_ != other.memory_only_config_) {
56       return memory_only_config_ < other.memory_only_config_;
57     }
58     return section_ < other.section_;
59   }
60   bool operator>(const ClassicDevice& rhs) const {
61     return (rhs < *this);
62   }
63   bool operator<=(const ClassicDevice& rhs) const {
64     return !(*this > rhs);
65   }
66   bool operator>=(const ClassicDevice& rhs) const {
67     return !(*this < rhs);
68   }
69 
70   // Get the parent device
71   Device Parent();
72 
73   // For logging purpose only, you can't get a ClassicDevice object from parsing a std::string
74   std::string ToLogString() const;
75 
76   // Get address of this classic device, it must exist
77   hci::Address GetAddress() const;
78 
79   // Return true if device has a link key in one of |kLinkKeyProperties|
80   bool IsPaired() const;
81 
82   // Property names that correspond to a link key used in Bluetooth classic device
83   static const std::unordered_set<std::string_view> kLinkKeyProperties;
84 
85  private:
86   ConfigCache* config_;
87   ConfigCache* memory_only_config_;
88   std::string section_;
89   friend std::hash<ClassicDevice>;
90 
91  public:
92   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(LinkKey, hci::LinkKey, "LinkKey");
93   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(LinkKeyType, hci::KeyType, "LinkKeyType");
94   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(ServiceUuids, std::vector<hci::Uuid>, "Service");
95   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(SdpDiManufacturer, uint16_t, "SdpDiManufacturer");
96   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(SdpDiModel, uint16_t, "SdpDiModel");
97   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(SdpDiHardwareVersion, uint16_t, "SdpDiHardwareVersion");
98   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(SdpDiVendorIdSource, uint16_t, "SdpDiVendorIdSource");
99 };
100 
101 }  // namespace storage
102 }  // namespace bluetooth
103 
104 namespace std {
105 template <>
106 struct hash<bluetooth::storage::ClassicDevice> {
107   std::size_t operator()(const bluetooth::storage::ClassicDevice& val) const noexcept {
108     std::size_t pointer_hash_1 = std::hash<bluetooth::storage::ConfigCache*>{}(val.config_);
109     std::size_t pointer_hash_2 = std::hash<bluetooth::storage::ConfigCache*>{}(val.config_);
110     std::size_t addr_hash = std::hash<std::string>{}(val.section_);
111     return addr_hash ^ (pointer_hash_1 << 1) ^ (pointer_hash_2 << 2);
112   }
113 };
114 }  // namespace std