1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "thermal_mgr_dumper.h"
17 
18 #include <cstdlib>
19 
20 #include "constants.h"
21 #include "thermal_common.h"
22 #include "thermal_service.h"
23 #include "action_popup.h"
24 
25 namespace OHOS {
26 namespace PowerMgr {
27 namespace {
28 constexpr const char* ARGS_HELP = "-h";
29 constexpr const char* ARGS_DIALOG = "-d";
30 constexpr const char* ARGS_THERMALINFO = "-t";
31 constexpr const char* ARGS_SCENE = "-s";
32 constexpr const char* ARGS_LEVEL = "-l";
33 constexpr const char* ARGS_ACTION = "-a";
34 constexpr const char* ARGS_POLICY = "-p";
35 constexpr const char* ARGS_IDLE = "-i";
36 constexpr const char* ARGS_TEMP_REPORT = "-st";
37 constexpr const char* ARGS_STOP_TEMP_FLAG = "0";
38 constexpr const char* ARGS_TEMP_EMULATION = "-te";
39 constexpr int32_t TEMP_EMUL_PARAM_NUM = 2;
40 }
41 
Dump(const std::vector<std::string> & args,std::string & result)42 bool ThermalMgrDumper::Dump(const std::vector<std::string>& args, std::string& result)
43 {
44     result.clear();
45     if ((args.size() == 0) || (args[0] == ARGS_HELP)) {
46         ShowUsage(result);
47         return true;
48     }
49 
50     auto tms = ThermalService::GetInstance();
51     if (tms == nullptr) {
52         return false;
53     }
54 
55     if (args[0] == ARGS_TEMP_REPORT) {
56         SwitchTempReport(args, result, tms);
57     } else if (args[0] == ARGS_TEMP_EMULATION) {
58         EmulateTempReport(args, result, tms);
59     } else if (args[0] == ARGS_THERMALINFO) {
60         ShowThermalZoneInfo(result);
61     } else {
62         return DumpPolicy(args, result, tms);
63     }
64     return true;
65 }
66 
SwitchTempReport(const std::vector<std::string> & args,std::string & result,sptr<ThermalService> & tms)67 void ThermalMgrDumper::SwitchTempReport(const std::vector<std::string>& args,
68     std::string& result, sptr<ThermalService>& tms)
69 {
70 #ifndef THERMAL_USER_VERSION
71     if (args.size() >= TEMP_EMUL_PARAM_NUM && args[1] == ARGS_STOP_TEMP_FLAG) {
72         tms->SetTempReportSwitch(false);
73         result.append("Temperature reporting has been stopped.\n");
74     } else {
75         tms->SetTempReportSwitch(true);
76         result.append("Temperature reporting has been started.\n");
77     }
78 #else
79     result.append("[Failed] User version is not supported.\n");
80 #endif
81 }
82 
EmulateTempReport(const std::vector<std::string> & args,std::string & result,sptr<ThermalService> & tms)83 void ThermalMgrDumper::EmulateTempReport(const std::vector<std::string>& args,
84     std::string& result, sptr<ThermalService>& tms)
85 {
86 #ifndef THERMAL_USER_VERSION
87     if (args.size() <= TEMP_EMUL_PARAM_NUM) {
88         result.append("[Error] Insufficient input parameters!\n");
89         return;
90     }
91     TypeTempMap tempMap;
92     for (size_t i = 1; (TEMP_EMUL_PARAM_NUM * i) < args.size(); ++i) {
93         tempMap.emplace(args[TEMP_EMUL_PARAM_NUM * i - 1], atoi(args[TEMP_EMUL_PARAM_NUM * i].c_str()));
94         result.append("Report temperature [ ")
95             .append(args[TEMP_EMUL_PARAM_NUM * i - 1])
96             .append(" ]: ")
97             .append(args[TEMP_EMUL_PARAM_NUM * i])
98             .append("\n");
99     }
100     if (!tms->HandleTempEmulation(tempMap)) {
101         result.append("[Error] Original temperature reporting has not been closed! You need to close it first.\n");
102     }
103 #else
104     result.append("[Failed] User version is not supported.\n");
105 #endif
106 }
107 
DumpPolicy(const std::vector<std::string> & args,std::string & result,sptr<ThermalService> & tms)108 bool ThermalMgrDumper::DumpPolicy(const std::vector<std::string>& args,
109     std::string& result, sptr<ThermalService>& tms)
110 {
111     if (!tms->GetSimulationXml()) {
112         return false;
113     }
114 
115     for (auto it = args.begin(); it != args.end(); it++) {
116         if (*it == ARGS_DIALOG) {
117             tms->GetActionPopup()->ShowThermalDialog(ActionPopup::TempStatus::HIGHER_TEMP);
118         } else if (*it == ARGS_SCENE) {
119             tms->GetStateMachineObj()->DumpState(result);
120         } else if (*it == ARGS_LEVEL) {
121             tms->GetPolicy()->DumpLevel(result);
122         } else if (*it == ARGS_ACTION) {
123             tms->GetActionManagerObj()->DumpAction(result);
124         } else if (*it == ARGS_POLICY) {
125             tms->GetPolicy()->DumpPolicy(result);
126         } else if (*it == ARGS_IDLE) {
127             tms->GetStateMachineObj()->DumpIdle(result);
128         } else {
129             break;
130         }
131     }
132     return true;
133 }
134 
ShowThermalZoneInfo(std::string & result)135 void ThermalMgrDumper::ShowThermalZoneInfo(std::string& result)
136 {
137     auto tms = ThermalService::GetInstance();
138     if (tms == nullptr) {
139         THERMAL_HILOGE(COMP_SVC, "thermal service is nullptr");
140         return;
141     }
142     auto tzMap = tms->GetSubscriber()->GetSubscriberInfo();
143     for (auto& iter : tzMap) {
144         result.append("Type: ")
145             .append(iter.first)
146             .append("\n")
147             .append("Temperature: ")
148             .append(ToString(iter.second))
149             .append("\n");
150     }
151 }
152 
ShowUsage(std::string & result)153 void ThermalMgrDumper::ShowUsage(std::string& result)
154 {
155     result.append("Thermal manager dump options:\n")
156         .append("  [-h] \n")
157         .append("  description of the cmd option:\n")
158         .append("    -h: show this help.\n")
159         .append("    -t: show thermal zone data.\n")
160         .append("  only for engineer:\n")
161         .append("    -st: switch temperature report, 0: stop, 1: start.\n")
162         .append("    -te: temperature emulation.\n")
163         .append("    -d: show thermal level dialog.\n")
164         .append("    -s: show thermal scene data.\n")
165         .append("    -l: show thermal level data.\n")
166         .append("    -a: show thermal action data.\n")
167         .append("    -p: show thermal policy data.\n")
168         .append("    -i: show thermal charging idle state data.\n");
169 }
170 } // namespace PowerMgr
171 } // namespace OHOS
172