blob: 6f6677023d51b3713d76824990ca3f6da643eb5c [file] [log] [blame]
Chris Manton0cb19c82020-01-13 21:11:48 -08001/*
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#define LOG_TAG "bt_gd_shim"
17
18#include <algorithm>
19#include <functional>
20#include <future>
21#include <memory>
Chris Mantone21efea2020-01-16 09:53:17 -080022#include <string>
Chris Manton0cb19c82020-01-13 21:11:48 -080023#include <unordered_map>
24#include <utility>
25
26#include "module.h"
27#include "os/handler.h"
28#include "os/log.h"
29#include "shim/dumpsys.h"
30
31namespace bluetooth {
32namespace shim {
33
Chris Mantone21efea2020-01-16 09:53:17 -080034namespace {
35constexpr char kModuleName[] = "shim::Dumpsys";
36} // namespace
37
Chris Manton0cb19c82020-01-13 21:11:48 -080038struct Dumpsys::impl {
39 public:
40 void Dump(int fd, std::promise<void> promise);
Chris Mantonff7ffd02020-01-17 19:25:36 -080041 void RegisterDumpsysFunction(const void* token, DumpsysFunction func);
42 void UnregisterDumpsysFunction(const void* token);
Chris Manton0cb19c82020-01-13 21:11:48 -080043
Chris Mantonff7ffd02020-01-17 19:25:36 -080044 ~impl() = default;
Chris Manton0cb19c82020-01-13 21:11:48 -080045
46 private:
Chris Mantonff7ffd02020-01-17 19:25:36 -080047 std::unordered_map<const void*, DumpsysFunction> dumpsys_functions_;
Chris Manton0cb19c82020-01-13 21:11:48 -080048};
49
50const ModuleFactory Dumpsys::Factory = ModuleFactory([]() { return new Dumpsys(); });
51
Chris Manton0cb19c82020-01-13 21:11:48 -080052void Dumpsys::impl::Dump(int fd, std::promise<void> promise) {
Chris Mantonff7ffd02020-01-17 19:25:36 -080053 dprintf(fd, "%s Registered submodules:%zd\n", kModuleName, dumpsys_functions_.size());
54 std::for_each(dumpsys_functions_.begin(), dumpsys_functions_.end(),
55 [fd](std::pair<const void*, DumpsysFunction> element) { element.second(fd); });
Chris Manton0cb19c82020-01-13 21:11:48 -080056 promise.set_value();
57}
58
Chris Mantonff7ffd02020-01-17 19:25:36 -080059void Dumpsys::impl::RegisterDumpsysFunction(const void* token, DumpsysFunction func) {
60 ASSERT(dumpsys_functions_.find(token) == dumpsys_functions_.end());
61 dumpsys_functions_[token] = func;
Chris Manton0cb19c82020-01-13 21:11:48 -080062}
63
Chris Mantonff7ffd02020-01-17 19:25:36 -080064void Dumpsys::impl::UnregisterDumpsysFunction(const void* token) {
65 ASSERT(dumpsys_functions_.find(token) != dumpsys_functions_.end());
66 dumpsys_functions_.erase(token);
Chris Manton0cb19c82020-01-13 21:11:48 -080067}
68
69void Dumpsys::Dump(int fd) {
70 std::promise<void> promise;
71 auto future = promise.get_future();
72 GetHandler()->Post(common::BindOnce(&Dumpsys::impl::Dump, common::Unretained(pimpl_.get()), fd, std::move(promise)));
73 future.get();
74}
75
Chris Mantonff7ffd02020-01-17 19:25:36 -080076void Dumpsys::RegisterDumpsysFunction(const void* token, DumpsysFunction func) {
77 GetHandler()->Post(
78 common::BindOnce(&Dumpsys::impl::RegisterDumpsysFunction, common::Unretained(pimpl_.get()), token, func));
Chris Manton0cb19c82020-01-13 21:11:48 -080079}
80
Chris Mantonff7ffd02020-01-17 19:25:36 -080081void Dumpsys::UnregisterDumpsysFunction(const void* token) {
82 GetHandler()->Post(
83 common::BindOnce(&Dumpsys::impl::UnregisterDumpsysFunction, common::Unretained(pimpl_.get()), token));
Chris Manton0cb19c82020-01-13 21:11:48 -080084}
85
Jakub Pawlowskif0033752020-02-19 02:29:20 +010086os::Handler* Dumpsys::GetGdShimHandler() {
87 return GetHandler();
88}
89
Chris Manton0cb19c82020-01-13 21:11:48 -080090/**
91 * Module methods
92 */
93void Dumpsys::ListDependencies(ModuleList* list) {}
94
95void Dumpsys::Start() {
96 pimpl_ = std::make_unique<impl>();
97}
98
99void Dumpsys::Stop() {
100 pimpl_.reset();
101}
102
Chris Mantone21efea2020-01-16 09:53:17 -0800103std::string Dumpsys::ToString() const {
104 return kModuleName;
105}
106
Chris Manton0cb19c82020-01-13 21:11:48 -0800107} // namespace shim
108} // namespace bluetooth