1 /*
2 * Copyright 2019 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 #define LOG_TAG "bt_shim_storage"
18
19 #include "main/shim/dumpsys.h"
20
21 #include <com_android_bluetooth_flags.h>
22
23 #include <unordered_map>
24
25 #include "main/shim/entry.h"
26 #include "main/shim/shim.h"
27 #include "main/shim/stack.h"
28
29 namespace {
30
31 constexpr char kModuleName[] = "shim::legacy::dumpsys";
32 static std::unordered_map<const void*, bluetooth::shim::DumpsysFunction> dumpsys_functions_;
33
34 } // namespace
35
RegisterDumpsysFunction(const void * token,DumpsysFunction func)36 void bluetooth::shim::RegisterDumpsysFunction(const void* token, DumpsysFunction func) {
37 log::assert_that(dumpsys_functions_.find(token) == dumpsys_functions_.end(),
38 "assert failed: dumpsys_functions_.find(token) == dumpsys_functions_.end()");
39 dumpsys_functions_.insert({token, func});
40 }
41
UnregisterDumpsysFunction(const void * token)42 void bluetooth::shim::UnregisterDumpsysFunction(const void* token) {
43 log::assert_that(dumpsys_functions_.find(token) != dumpsys_functions_.end(),
44 "assert failed: dumpsys_functions_.find(token) != dumpsys_functions_.end()");
45 dumpsys_functions_.erase(token);
46 }
47
Dump(int fd)48 void bluetooth::shim::Dump(int fd) {
49 if (dumpsys_functions_.empty()) {
50 dprintf(fd, "%s No registered dumpsys shim legacy targets\n", kModuleName);
51 } else {
52 dprintf(fd, "%s Dumping shim legacy targets:%zd\n", kModuleName, dumpsys_functions_.size());
53 for (auto& dumpsys : dumpsys_functions_) {
54 dumpsys.second(fd);
55 }
56 }
57 std::promise<void> promise;
58 std::future future = promise.get_future();
59 bluetooth::shim::Stack::GetInstance()->Dump(fd, std::move(promise));
60 log::assert_that(future.wait_for(std::chrono::seconds(1)) == std::future_status::ready,
61 "Timed out waiting for dumpsys to complete");
62 }
63