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