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 <bluetooth/log.h>
19 #include <com_android_bluetooth_flags.h>
20 #include <unistd.h>
21 
22 #include <future>
23 #include <sstream>
24 #include <string>
25 
26 #include "hal/snoop_logger.h"
27 #include "hci/acl_manager.h"
28 #include "hci/controller_interface.h"
29 #include "main/shim/entry.h"
30 #include "main/shim/stack.h"
31 #include "module.h"
32 #include "os/system_properties.h"
33 #include "os/wakelock_manager.h"
34 #include "shim/dumpsys.h"
35 
36 namespace bluetooth {
37 namespace shim {
38 
39 namespace {
40 constexpr char kModuleName[] = "shim::Dumpsys";
41 }  // namespace
42 
43 struct Dumpsys::impl {
44 public:
45   void DumpSync(int fd, std::promise<void> promise);
46   int GetNumberOfBundledSchemas() const;
47 
48   impl(const Dumpsys& dumpsys_module);
49   ~impl() = default;
50 
51 private:
52   void DumpAsync(int fd) const;
53 
54   const Dumpsys& dumpsys_module_;
55 };
56 
57 const ModuleFactory Dumpsys::Factory =
__anon938bc6e70202() 58         ModuleFactory([]() { return new Dumpsys(); });
59 
impl(const Dumpsys & dumpsys_module)60 Dumpsys::impl::impl(const Dumpsys& dumpsys_module)
61     : dumpsys_module_(dumpsys_module) {}
62 
DumpAsync(int fd) const63 void Dumpsys::impl::DumpAsync(int fd) const {
64   const auto registry = dumpsys_module_.GetModuleRegistry();
65   bluetooth::shim::GetController()->Dump(fd);
66   bluetooth::shim::GetAclManager()->Dump(fd);
67   bluetooth::os::WakelockManager::Get().Dump(fd);
68   bluetooth::shim::GetSnoopLogger()->DumpSnoozLogToFile();
69 }
70 
DumpSync(int fd,std::promise<void> promise)71 void Dumpsys::impl::DumpSync(int fd, std::promise<void> promise) {
72   if (bluetooth::shim::Stack::GetInstance()->LockForDumpsys([=, *this]() {
73         log::info("Started dumpsys procedure");
74         this->DumpAsync(fd);
75       })) {
76     log::info("Successful dumpsys procedure");
77   } else {
78     log::info("Failed dumpsys procedure as stack was not longer active");
79   }
80   promise.set_value();
81 }
82 
Dumpsys()83 Dumpsys::Dumpsys() {}
84 
Dump(int fd,const char **,std::promise<void> promise)85 void Dumpsys::Dump(int fd, const char** /*args*/, std::promise<void> promise) {
86   if (fd <= 0) {
87     promise.set_value();
88     return;
89   }
90   CallOn(pimpl_.get(), &Dumpsys::impl::DumpSync, fd, std::move(promise));
91 }
92 
93 /**
94  * Module methods
95  */
ListDependencies(ModuleList *) const96 void Dumpsys::ListDependencies(ModuleList* /* list */) const {}
97 
Start()98 void Dumpsys::Start() { pimpl_ = std::make_unique<impl>(*this); }
99 
Stop()100 void Dumpsys::Stop() { pimpl_.reset(); }
101 
ToString() const102 std::string Dumpsys::ToString() const { return kModuleName; }
103 
104 }  // namespace shim
105 }  // namespace bluetooth
106