1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "btif_config_cache.h"
#include <limits>
#include <vector>
#include "stack/include/bt_octets.h"
#include "types/raw_address.h"
#include <base/logging.h>
namespace {
const std::unordered_set<std::string> kLinkKeyTypes = {
"LinkKey", "LE_KEY_PENC", "LE_KEY_PID",
"LE_KEY_PCSRK", "LE_KEY_LENC", "LE_KEY_LCSRK"};
const std::unordered_set<std::string> kLocalSectionNames = {"Info", "Metrics",
"Adapter"};
bool is_link_key(const std::string& key) {
return kLinkKeyTypes.find(key) != kLinkKeyTypes.end();
}
bool has_link_key_in_section(const section_t& section) {
for (const auto& entry : section.entries) {
if (is_link_key(entry.key)) {
return true;
}
}
return false;
}
bool is_local_section_info(const std::string& section) {
return kLocalSectionNames.find(section) != kLocalSectionNames.end();
}
// trim new line in place, return true if newline was found
bool trim_new_line(std::string& value) {
size_t newline_position = value.find_first_of('\n');
if (newline_position != std::string::npos) {
value.erase(newline_position);
return true;
}
return false;
}
} // namespace
BtifConfigCache::BtifConfigCache(size_t capacity)
: unpaired_devices_cache_(capacity, "bt_config_cache") {
LOG(INFO) << __func__ << ", capacity: " << capacity;
}
BtifConfigCache::~BtifConfigCache() { Clear(); }
void BtifConfigCache::Clear() {
unpaired_devices_cache_.Clear();
paired_devices_list_.sections.clear();
}
void BtifConfigCache::Init(std::unique_ptr<config_t> source) {
// get the config persistent data from btif_config file
paired_devices_list_ = std::move(*source);
source.reset();
}
bool BtifConfigCache::HasPersistentSection(const std::string& section_name) {
return paired_devices_list_.Find(section_name) !=
paired_devices_list_.sections.end();
}
bool BtifConfigCache::HasUnpairedSection(const std::string& section_name) {
return unpaired_devices_cache_.HasKey(section_name);
}
bool BtifConfigCache::HasSection(const std::string& section_name) {
return HasUnpairedSection(section_name) || HasPersistentSection(section_name);
}
bool BtifConfigCache::HasKey(const std::string& section_name,
const std::string& key) {
auto section_iter = paired_devices_list_.Find(section_name);
if (section_iter != paired_devices_list_.sections.end()) {
return section_iter->Has(key);
}
section_t* section = unpaired_devices_cache_.Find(section_name);
if (section == nullptr) {
return false;
}
return section->Has(key);
}
// remove sections with the restricted key
void BtifConfigCache::RemovePersistentSectionsWithKey(const std::string& key) {
for (auto it = paired_devices_list_.sections.begin();
it != paired_devices_list_.sections.end();) {
if (it->Has(key)) {
it = paired_devices_list_.sections.erase(it);
continue;
}
it++;
}
}
/* remove a key from section, section itself is removed when empty */
bool BtifConfigCache::RemoveKey(const std::string& section_name,
const std::string& key) {
section_t* section = unpaired_devices_cache_.Find(section_name);
if (section != nullptr) {
auto entry_iter = section->Find(key);
if (entry_iter == section->entries.end()) {
return false;
}
section->entries.erase(entry_iter);
if (section->entries.empty()) {
unpaired_devices_cache_.Remove(section_name);
}
return true;
} else {
auto section_iter = paired_devices_list_.Find(section_name);
if (section_iter == paired_devices_list_.sections.end()) {
return false;
}
auto entry_iter = section_iter->Find(key);
if (entry_iter == section_iter->entries.end()) {
return false;
}
section_iter->entries.erase(entry_iter);
if (section_iter->entries.empty()) {
paired_devices_list_.sections.erase(section_iter);
} else if (!has_link_key_in_section(*section_iter)) {
// if no link key in section after removal, move it to unpaired section
auto moved_section = std::move(*section_iter);
paired_devices_list_.sections.erase(section_iter);
unpaired_devices_cache_.Put(section_name, std::move(moved_section));
}
return true;
}
}
std::vector<std::string> BtifConfigCache::GetPersistentSectionNames() {
std::vector<std::string> result;
result.reserve(paired_devices_list_.sections.size());
for (const auto& section : paired_devices_list_.sections) {
result.emplace_back(section.name);
}
return result;
}
/* clone persistent sections (Local Adapter sections, remote paired devices
* section,..) */
config_t BtifConfigCache::PersistentSectionCopy() {
return paired_devices_list_;
}
void BtifConfigCache::SetString(std::string section_name, std::string key,
std::string value) {
if (trim_new_line(section_name) || trim_new_line(key) ||
trim_new_line(value)) {
android_errorWriteLog(0x534e4554, "70808273");
}
if (section_name.empty()) {
LOG(FATAL) << "Empty section not allowed";
return;
}
if (key.empty()) {
LOG(FATAL) << "Empty key not allowed";
return;
}
if (!paired_devices_list_.Has(section_name)) {
// section is not in paired_device_list, handle it in unpaired devices cache
section_t section = {};
bool in_unpaired_cache = true;
if (!unpaired_devices_cache_.Get(section_name, §ion)) {
// it's a new unpaired section, add it to unpaired devices cache
section.name = section_name;
in_unpaired_cache = false;
}
// set key to value and replace existing key if already exist
section.Set(key, value);
if (is_local_section_info(section_name) ||
(is_link_key(key) && RawAddress::IsValidAddress(section_name))) {
// remove this section that has the LinkKey from unpaired devices cache.
if (in_unpaired_cache) {
unpaired_devices_cache_.Remove(section_name);
}
// when a unpaired section got the LinkKey, move this section to the
// paired devices list
paired_devices_list_.sections.emplace_back(std::move(section));
} else {
// update to the unpaired devices cache
unpaired_devices_cache_.Put(section_name, section);
}
} else {
// already have section in paired device list, add key-value entry.
auto section_found = paired_devices_list_.Find(section_name);
if (section_found == paired_devices_list_.sections.end()) {
LOG(WARNING) << __func__ << " , section_found not found!";
return;
}
section_found->Set(key, value);
}
}
std::optional<std::string> BtifConfigCache::GetString(
const std::string& section_name, const std::string& key) {
// Check paired sections first
auto section_iter = paired_devices_list_.Find(section_name);
if (section_iter != paired_devices_list_.sections.end()) {
auto entry_iter = section_iter->Find(key);
if (entry_iter == section_iter->entries.end()) {
return std::nullopt;
}
return entry_iter->value;
}
// Check unpaired sections later
section_t section = {};
if (!unpaired_devices_cache_.Get(section_name, §ion)) {
return std::nullopt;
}
auto entry_iter = section.Find(key);
if (entry_iter == section.entries.end()) {
return std::nullopt;
}
return entry_iter->value;
}
void BtifConfigCache::SetInt(std::string section_name, std::string key,
int value) {
SetString(std::move(section_name), std::move(key), std::to_string(value));
}
std::optional<int> BtifConfigCache::GetInt(const std::string& section_name,
const std::string& key) {
auto value = GetString(section_name, key);
if (!value) {
return std::nullopt;
}
char* endptr;
long ret_long = strtol(value->c_str(), &endptr, 0);
if (*endptr != '\0') {
LOG(WARNING) << "Failed to parse value to long for section " << section_name
<< ", key " << key;
return std::nullopt;
}
if (ret_long >= std::numeric_limits<int>::max()) {
LOG(WARNING) << "Integer overflow when parsing value to int for section "
<< section_name << ", key " << key;
return std::nullopt;
}
return static_cast<int>(ret_long);
}
void BtifConfigCache::SetUint64(std::string section_name, std::string key,
uint64_t value) {
SetString(std::move(section_name), std::move(key), std::to_string(value));
}
std::optional<uint64_t> BtifConfigCache::GetUint64(
const std::string& section_name, const std::string& key) {
auto value = GetString(section_name, key);
if (!value) {
return std::nullopt;
}
char* endptr;
uint64_t ret = strtoull(value->c_str(), &endptr, 0);
if (*endptr != '\0') {
LOG(WARNING) << "Failed to parse value to uint64 for section "
<< section_name << ", key " << key;
return std::nullopt;
}
return ret;
}
void BtifConfigCache::SetBool(std::string section_name, std::string key,
bool value) {
SetString(std::move(section_name), std::move(key), value ? "true" : "false");
}
std::optional<bool> BtifConfigCache::GetBool(const std::string& section_name,
const std::string& key) {
auto value = GetString(section_name, key);
if (!value) {
return std::nullopt;
}
if (*value == "true") {
return true;
}
if (*value == "false") {
return false;
}
LOG(WARNING) << "Failed to parse value to boolean for section "
<< section_name << ", key " << key;
return std::nullopt;
}
|