1 /*
2 * Copyright (c) 2022 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 "res_sched_service.h"
17 #include <file_ex.h>
18 #include <string_ex.h>
19 #include "plugin_mgr.h"
20 #include "res_sched_log.h"
21 #include "res_sched_mgr.h"
22
23 namespace OHOS {
24 namespace ResourceSchedule {
25 namespace {
26 constexpr int32_t DUMP_OPTION = 0;
27 constexpr int32_t DUMP_PARAM_INDEX = 1;
28 }
29
ReportData(uint32_t resType,int64_t value,const nlohmann::json & payload)30 void ResSchedService::ReportData(uint32_t resType, int64_t value, const nlohmann::json& payload)
31 {
32 RESSCHED_LOGI("ResSchedService::ReportData from ipc receive data resType = %{public}u, value = %{public}lld.",
33 resType, (long long)value);
34 ResSchedMgr::GetInstance().ReportData(resType, value, payload);
35 }
36
Dump(int32_t fd,const std::vector<std::u16string> & args)37 int32_t ResSchedService::Dump(int32_t fd, const std::vector<std::u16string>& args)
38 {
39 RESSCHED_LOGI("%{public}s Dump service", __func__);
40 std::vector<std::string> argsInStr;
41 std::transform(args.begin(), args.end(), std::back_inserter(argsInStr),
42 [](const std::u16string &arg) {
43 std::string ret = Str16ToStr8(arg);
44 RESSCHED_LOGI("%{public}s arg: %{public}s", __func__, ret.c_str());
45 return ret;
46 });
47 std::string result;
48 if (argsInStr.size() == 0) {
49 // hidumper -s said '-h'
50 DumpUsage(result);
51 } else if (argsInStr.size() == DUMP_OPTION + 1) {
52 // hidumper -s said '-h' or hidumper -s said '-a'
53 if (argsInStr[DUMP_OPTION] == "-h") {
54 DumpUsage(result);
55 } else if (argsInStr[DUMP_OPTION] == "-a") {
56 DumpAllInfo(result);
57 } else if (argsInStr[DUMP_OPTION] == "-p") {
58 PluginMgr::GetInstance().DumpAllPlugin(result);
59 } else {
60 result.append("Error params.");
61 }
62 } else if (argsInStr.size() >= DUMP_PARAM_INDEX + 1) {
63 if (argsInStr[DUMP_OPTION] == "-p") {
64 std::vector<std::string> argsInStrToPlugin;
65 argsInStrToPlugin.assign(argsInStr.begin() + DUMP_PARAM_INDEX + 1, argsInStr.end());
66 PluginMgr::GetInstance().DumpOnePlugin(result, argsInStr[DUMP_PARAM_INDEX], argsInStrToPlugin);
67 }
68 }
69
70 if (!SaveStringToFd(fd, result)) {
71 RESSCHED_LOGE("%{public}s save to fd failed.", __func__);
72 }
73 return ERR_OK;
74 }
DumpUsage(std::string & result)75 void ResSchedService::DumpUsage(std::string &result)
76 {
77 result.append("usage: resource schedule service dump [<options>]\n")
78 .append(" -h: show the help.\n")
79 .append(" -a: show all info.\n")
80 .append(" -p: show the all plugin info.\n")
81 .append(" -p (plugin name): show one plugin info.\n");
82 PluginMgr::GetInstance().DumpHelpFromPlugin(result);
83 }
84
DumpAllInfo(std::string & result)85 void ResSchedService::DumpAllInfo(std::string &result)
86 {
87 result.append("================Resource Schedule Service Infos================\n");
88 PluginMgr::GetInstance().DumpAllPlugin(result);
89 }
90 } // namespace ResourceSchedule
91 } // namespace OHOS
92
93