1 /*
2  * Copyright 2023 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 "BtGdModule"
17 
18 #include "module_dumper.h"
19 
20 #include <sstream>
21 
22 #include "common/init_flags.h"
23 #include "dumpsys_data_generated.h"
24 #include "module.h"
25 #include "os/wakelock_manager.h"
26 
27 using ::bluetooth::os::WakelockManager;
28 
29 namespace bluetooth {
30 
DumpState(std::string * output,std::ostringstream &) const31 void ModuleDumper::DumpState(std::string* output, std::ostringstream& /*oss*/) const {
32   log::assert_that(output != nullptr, "assert failed: output != nullptr");
33 
34   flatbuffers::FlatBufferBuilder builder(1024);
35   auto title = builder.CreateString(title_);
36 
37   common::InitFlagsDataBuilder init_flags_builder(builder);
38   init_flags_builder.add_title(builder.CreateString("----- Init Flags -----"));
39   std::vector<flatbuffers::Offset<common::InitFlagValue>> flags;
40   for (const auto& flag : common::init_flags::dump()) {
41     flags.push_back(common::CreateInitFlagValue(
42         builder,
43         builder.CreateString(std::string(flag.flag)),
44         builder.CreateString(std::string(flag.value))));
45   }
46   init_flags_builder.add_values(builder.CreateVector(flags));
47   auto init_flags_offset = init_flags_builder.Finish();
48 
49   auto wakelock_offset = WakelockManager::Get().GetDumpsysData(&builder);
50 
51   std::queue<DumpsysDataFinisher> queue;
52   for (auto it = module_registry_.start_order_.rbegin(); it != module_registry_.start_order_.rend();
53        it++) {
54     auto instance = module_registry_.started_modules_.find(*it);
55     log::assert_that(
56         instance != module_registry_.started_modules_.end(),
57         "assert failed: instance != module_registry_.started_modules_.end()");
58     log::verbose("Starting dumpsys module:{}", instance->second->ToString());
59     queue.push(instance->second->GetDumpsysData(&builder));
60     log::verbose("Finished dumpsys module:{}", instance->second->ToString());
61   }
62 
63   DumpsysDataBuilder data_builder(builder);
64   data_builder.add_title(title);
65   data_builder.add_init_flags(init_flags_offset);
66   data_builder.add_wakelock_manager_data(wakelock_offset);
67 
68   while (!queue.empty()) {
69     queue.front()(&data_builder);
70     queue.pop();
71   }
72 
73   builder.Finish(data_builder.Finish());
74   *output = std::string(builder.GetBufferPointer(), builder.GetBufferPointer() + builder.GetSize());
75 }
76 
77 }  // namespace bluetooth
78