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