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 
17 #include "storage/device.h"
18 
19 #include <algorithm>
20 #include <limits>
21 
22 #include "os/log.h"
23 #include "storage/classic_device.h"
24 #include "storage/config_cache_helper.h"
25 #include "storage/le_device.h"
26 
27 namespace bluetooth {
28 namespace storage {
29 
30 using hci::DeviceType;
31 
32 namespace {
33 // TODO(siyuanh): also defined in storage/le_device.cc
34 const std::string kLeIdentityAddressKey = "LeIdentityAddr";
35 const std::string kLeLegacyPseudoAddr = "LeLegacyPseudoAddr";
36 
GetConfigSection(ConfigCache * config,const hci::Address & key_address,Device::ConfigKeyAddressType key_address_type)37 std::string GetConfigSection(
38     ConfigCache* config, const hci::Address& key_address, Device::ConfigKeyAddressType key_address_type) {
39   ASSERT_LOG(config != nullptr, "config cannot be null");
40   ASSERT_LOG(!key_address.IsEmpty(), "key_address cannot be empty");
41   // assume lower case
42   auto key_address_string = key_address.ToString();
43   switch (key_address_type) {
44     case Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS:
45     case Device::ConfigKeyAddressType::CLASSIC_ADDRESS:
46       return key_address_string;
47     case Device::ConfigKeyAddressType::LE_IDENTITY_ADDRESS:
48       for (const auto& section_and_property : config->GetSectionNamesWithProperty(kLeIdentityAddressKey)) {
49         if (section_and_property.property == key_address_string) {
50           return section_and_property.section;
51         }
52       }
53       return key_address_string;
54     case Device::ConfigKeyAddressType::LE_LEGACY_PSEUDO_ADDRESS:
55       for (const auto& section_and_property : config->GetSectionNamesWithProperty(kLeLegacyPseudoAddr)) {
56         if (section_and_property.property == key_address_string) {
57           return section_and_property.section;
58         }
59       }
60       // One cannot create a new device just using LE legacy pseudo address
61       [[fallthrough]];
62     default:
63       LOG_ALWAYS_FATAL("Unknown key_address_type %d", static_cast<int>(key_address_type));
64       return "";
65   }
66 }
67 
68 }  // namespace
69 
70 const std::unordered_set<std::string_view> Device::kLinkKeyProperties = {
71     "LinkKey", "LE_KEY_PENC", "LE_KEY_PID", "LE_KEY_PCSRK", "LE_KEY_LENC", "LE_KEY_LCSRK"};
72 
Device(ConfigCache * config,ConfigCache * memory_only_config,const hci::Address & key_address,ConfigKeyAddressType key_address_type)73 Device::Device(
74     ConfigCache* config,
75     ConfigCache* memory_only_config,
76     const hci::Address& key_address,
77     ConfigKeyAddressType key_address_type)
78     : Device(config, memory_only_config, GetConfigSection(config, key_address, key_address_type)) {}
79 
Device(ConfigCache * config,ConfigCache * memory_only_config,std::string section)80 Device::Device(ConfigCache* config, ConfigCache* memory_only_config, std::string section)
81     : config_(config), memory_only_config_(memory_only_config), section_(std::move(section)) {}
82 
Exists()83 bool Device::Exists() {
84   return config_->HasSection(section_);
85 }
86 
RemoveFromConfig()87 MutationEntry Device::RemoveFromConfig() {
88   return MutationEntry::Remove(MutationEntry::PropertyType::NORMAL, section_);
89 }
90 
RemoveFromTempConfig()91 MutationEntry Device::RemoveFromTempConfig() {
92   return MutationEntry::Remove(MutationEntry::PropertyType::MEMORY_ONLY, section_);
93 }
94 
Le()95 LeDevice Device::Le() {
96   auto device_type = GetDeviceType();
97   ASSERT(device_type);
98   ASSERT(device_type == DeviceType::LE || device_type == DeviceType::DUAL);
99   return LeDevice(config_, memory_only_config_, section_);
100 }
101 
Classic()102 ClassicDevice Device::Classic() {
103   auto device_type = GetDeviceType();
104   ASSERT(device_type);
105   ASSERT(device_type == DeviceType::BR_EDR || device_type == DeviceType::DUAL);
106   return ClassicDevice(config_, memory_only_config_, section_);
107 }
108 
GetAddress() const109 hci::Address Device::GetAddress() const {
110   // section name of a device is its address
111   auto addr = hci::Address::FromString(section_);
112   ASSERT(addr.has_value());
113   return addr.value();
114 }
115 
ToLogString() const116 std::string Device::ToLogString() const {
117   return section_;
118 }
119 
120 }  // namespace storage
121 }  // namespace bluetooth
122