Chris Manton | 0cb19c8 | 2020-01-13 21:11:48 -0800 | [diff] [blame] | 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 | #define LOG_TAG "bt_gd_shim" |
| 17 | |
| 18 | #include <algorithm> |
| 19 | #include <functional> |
| 20 | #include <future> |
| 21 | #include <memory> |
Chris Manton | e21efea | 2020-01-16 09:53:17 -0800 | [diff] [blame] | 22 | #include <string> |
Chris Manton | 0cb19c8 | 2020-01-13 21:11:48 -0800 | [diff] [blame] | 23 | #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 | |
| 31 | namespace bluetooth { |
| 32 | namespace shim { |
| 33 | |
Chris Manton | e21efea | 2020-01-16 09:53:17 -0800 | [diff] [blame] | 34 | namespace { |
| 35 | constexpr char kModuleName[] = "shim::Dumpsys"; |
| 36 | } // namespace |
| 37 | |
Chris Manton | 0cb19c8 | 2020-01-13 21:11:48 -0800 | [diff] [blame] | 38 | struct Dumpsys::impl { |
| 39 | public: |
| 40 | void Dump(int fd, std::promise<void> promise); |
Chris Manton | ff7ffd0 | 2020-01-17 19:25:36 -0800 | [diff] [blame] | 41 | void RegisterDumpsysFunction(const void* token, DumpsysFunction func); |
| 42 | void UnregisterDumpsysFunction(const void* token); |
Chris Manton | 0cb19c8 | 2020-01-13 21:11:48 -0800 | [diff] [blame] | 43 | |
Chris Manton | ff7ffd0 | 2020-01-17 19:25:36 -0800 | [diff] [blame] | 44 | ~impl() = default; |
Chris Manton | 0cb19c8 | 2020-01-13 21:11:48 -0800 | [diff] [blame] | 45 | |
| 46 | private: |
Chris Manton | ff7ffd0 | 2020-01-17 19:25:36 -0800 | [diff] [blame] | 47 | std::unordered_map<const void*, DumpsysFunction> dumpsys_functions_; |
Chris Manton | 0cb19c8 | 2020-01-13 21:11:48 -0800 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | const ModuleFactory Dumpsys::Factory = ModuleFactory([]() { return new Dumpsys(); }); |
| 51 | |
Chris Manton | 0cb19c8 | 2020-01-13 21:11:48 -0800 | [diff] [blame] | 52 | void Dumpsys::impl::Dump(int fd, std::promise<void> promise) { |
Chris Manton | ff7ffd0 | 2020-01-17 19:25:36 -0800 | [diff] [blame] | 53 | 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 Manton | 0cb19c8 | 2020-01-13 21:11:48 -0800 | [diff] [blame] | 56 | promise.set_value(); |
| 57 | } |
| 58 | |
Chris Manton | ff7ffd0 | 2020-01-17 19:25:36 -0800 | [diff] [blame] | 59 | void Dumpsys::impl::RegisterDumpsysFunction(const void* token, DumpsysFunction func) { |
| 60 | ASSERT(dumpsys_functions_.find(token) == dumpsys_functions_.end()); |
| 61 | dumpsys_functions_[token] = func; |
Chris Manton | 0cb19c8 | 2020-01-13 21:11:48 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Chris Manton | ff7ffd0 | 2020-01-17 19:25:36 -0800 | [diff] [blame] | 64 | void Dumpsys::impl::UnregisterDumpsysFunction(const void* token) { |
| 65 | ASSERT(dumpsys_functions_.find(token) != dumpsys_functions_.end()); |
| 66 | dumpsys_functions_.erase(token); |
Chris Manton | 0cb19c8 | 2020-01-13 21:11:48 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void 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 Manton | ff7ffd0 | 2020-01-17 19:25:36 -0800 | [diff] [blame] | 76 | void Dumpsys::RegisterDumpsysFunction(const void* token, DumpsysFunction func) { |
| 77 | GetHandler()->Post( |
| 78 | common::BindOnce(&Dumpsys::impl::RegisterDumpsysFunction, common::Unretained(pimpl_.get()), token, func)); |
Chris Manton | 0cb19c8 | 2020-01-13 21:11:48 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Chris Manton | ff7ffd0 | 2020-01-17 19:25:36 -0800 | [diff] [blame] | 81 | void Dumpsys::UnregisterDumpsysFunction(const void* token) { |
| 82 | GetHandler()->Post( |
| 83 | common::BindOnce(&Dumpsys::impl::UnregisterDumpsysFunction, common::Unretained(pimpl_.get()), token)); |
Chris Manton | 0cb19c8 | 2020-01-13 21:11:48 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Jakub Pawlowski | f003375 | 2020-02-19 02:29:20 +0100 | [diff] [blame] | 86 | os::Handler* Dumpsys::GetGdShimHandler() { |
| 87 | return GetHandler(); |
| 88 | } |
| 89 | |
Chris Manton | 0cb19c8 | 2020-01-13 21:11:48 -0800 | [diff] [blame] | 90 | /** |
| 91 | * Module methods |
| 92 | */ |
| 93 | void Dumpsys::ListDependencies(ModuleList* list) {} |
| 94 | |
| 95 | void Dumpsys::Start() { |
| 96 | pimpl_ = std::make_unique<impl>(); |
| 97 | } |
| 98 | |
| 99 | void Dumpsys::Stop() { |
| 100 | pimpl_.reset(); |
| 101 | } |
| 102 | |
Chris Manton | e21efea | 2020-01-16 09:53:17 -0800 | [diff] [blame] | 103 | std::string Dumpsys::ToString() const { |
| 104 | return kModuleName; |
| 105 | } |
| 106 | |
Chris Manton | 0cb19c8 | 2020-01-13 21:11:48 -0800 | [diff] [blame] | 107 | } // namespace shim |
| 108 | } // namespace bluetooth |