• 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     return config_ < other.config_ && memory_only_config_ < other.memory_only_config_ && section_ < other.section_;
53   }
54   bool operator>(const ClassicDevice& rhs) const {
55     return (rhs < *this);
56   }
57   bool operator<=(const ClassicDevice& rhs) const {
58     return !(*this > rhs);
59   }
60   bool operator>=(const ClassicDevice& rhs) const {
61     return !(*this < rhs);
62   }
63 
64   // Get the parent device
65   Device Parent();
66 
67   // For logging purpose only, you can't get a ClassicDevice object from parsing a std::string
68   std::string ToLogString() const;
69 
70   // Get address of this classic device, it must exist
71   hci::Address GetAddress() const;
72 
73   // Return true if device has a link key in one of |kLinkKeyProperties|
74   bool IsPaired() const;
75 
76   // Property names that correspond to a link key used in Bluetooth classic device
77   static const std::unordered_set<std::string_view> kLinkKeyProperties;
78 
79  private:
80   ConfigCache* config_;
81   ConfigCache* memory_only_config_;
82   std::string section_;
83   friend std::hash<ClassicDevice>;
84 
85  public:
86   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(LinkKey, hci::LinkKey, "LinkKey");
87   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(LinkKeyType, hci::KeyType, "LinkKeyType");
88   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(ServiceUuids, std::vector<hci::Uuid>, "Service");
89   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(SdpDiManufacturer, uint16_t, "SdpDiManufacturer");
90   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(SdpDiModel, uint16_t, "SdpDiModel");
91   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(SdpDiHardwareVersion, uint16_t, "SdpDiHardwareVersion");
92   GENERATE_PROPERTY_GETTER_SETTER_REMOVER(SdpDiVendorIdSource, uint16_t, "SdpDiVendorIdSource");
93 };
94 
95 }  // namespace storage
96 }  // namespace bluetooth
97 
98 namespace std {
99 template <>
100 struct hash<bluetooth::storage::ClassicDevice> {
101   std::size_t operator()(const bluetooth::storage::ClassicDevice& val) const noexcept {
102     std::size_t pointer_hash_1 = std::hash<bluetooth::storage::ConfigCache*>{}(val.config_);
103     std::size_t pointer_hash_2 = std::hash<bluetooth::storage::ConfigCache*>{}(val.config_);
104     std::size_t addr_hash = std::hash<std::string>{}(val.section_);
105     return addr_hash ^ (pointer_hash_1 << 1) ^ (pointer_hash_2 << 2);
106   }
107 };
108 }  // namespace std