1 /*
2 * Copyright (c) 2024 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 "socperf_server.h"
17 #include <file_ex.h>
18 #include <string_ex.h>
19 #include "accesstoken_kit.h"
20 #include "ipc_skeleton.h"
21 #include "parameters.h"
22 #include "system_ability_definition.h"
23
24 namespace OHOS {
25 namespace SOCPERF {
26 const bool REGISTER_RESULT =
27 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<SocPerfServer>::GetInstance().get());
28 const int32_t ENG_MODE = OHOS::system::GetIntParameter("const.debuggable", 0);
29
SocPerfServer()30 SocPerfServer::SocPerfServer() : SystemAbility(SOC_PERF_SERVICE_SA_ID, true)
31 {
32 }
33
~SocPerfServer()34 SocPerfServer::~SocPerfServer()
35 {
36 }
37
OnStart()38 void SocPerfServer::OnStart()
39 {
40 if (!Publish(DelayedSingleton<SocPerfServer>::GetInstance().get())) {
41 SOC_PERF_LOGE("Register SystemAbility for SocPerf FAILED.");
42 return;
43 }
44 if (!socPerf.Init()) {
45 SOC_PERF_LOGE("SocPerf Init FAILED");
46 return;
47 }
48 }
49
OnStop()50 void SocPerfServer::OnStop()
51 {
52 }
53
AllowDump()54 bool SocPerfServer::AllowDump()
55 {
56 if (ENG_MODE == 0) {
57 SOC_PERF_LOGE("Not allow to dump SocPerfServer, mode:%{public}d", ENG_MODE);
58 return false;
59 }
60 Security::AccessToken::AccessTokenID tokenId = IPCSkeleton::GetFirstTokenID();
61 int32_t res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, "ohos.permission.DUMP");
62 if (res != Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
63 SOC_PERF_LOGE("Not allow to dump SocPerfServer, permission state:%{public}d", res);
64 return false;
65 }
66 return true;
67 }
68
Dump(int32_t fd,const std::vector<std::u16string> & args)69 int32_t SocPerfServer::Dump(int32_t fd, const std::vector<std::u16string>& args)
70 {
71 if (!AllowDump()) {
72 return ERR_PERMISSION_DENIED;
73 }
74 std::vector<std::string> argsInStr;
75 std::transform(args.begin(), args.end(), std::back_inserter(argsInStr),
76 [](const std::u16string &arg) {
77 return Str16ToStr8(arg);
78 });
79 std::string result;
80 result.append("usage: soc_perf service dump [<options>]\n")
81 .append(" 1. PerfRequest(cmdId, msg)\n")
82 .append(" 2. PerfRequestEx(cmdId, onOffTag, msg)\n")
83 .append(" 3. LimitRequest(clientId, tags, configs, msg)\n")
84 .append(" -h: show the help.\n")
85 .append(" -a: show all info.\n");
86 if (!SaveStringToFd(fd, result)) {
87 SOC_PERF_LOGE("Dump FAILED");
88 }
89 return ERR_OK;
90 }
91
PerfRequest(int32_t cmdId,const std::string & msg)92 void SocPerfServer::PerfRequest(int32_t cmdId, const std::string& msg)
93 {
94 socPerf.PerfRequest(cmdId, msg);
95 }
96
PerfRequestEx(int32_t cmdId,bool onOffTag,const std::string & msg)97 void SocPerfServer::PerfRequestEx(int32_t cmdId, bool onOffTag, const std::string& msg)
98 {
99 socPerf.PerfRequestEx(cmdId, onOffTag, msg);
100 }
101
PowerLimitBoost(bool onOffTag,const std::string & msg)102 void SocPerfServer::PowerLimitBoost(bool onOffTag, const std::string& msg)
103 {
104 socPerf.PowerLimitBoost(onOffTag, msg);
105 }
106
ThermalLimitBoost(bool onOffTag,const std::string & msg)107 void SocPerfServer::ThermalLimitBoost(bool onOffTag, const std::string& msg)
108 {
109 socPerf.ThermalLimitBoost(onOffTag, msg);
110 }
111
LimitRequest(int32_t clientId,const std::vector<int32_t> & tags,const std::vector<int64_t> & configs,const std::string & msg)112 void SocPerfServer::LimitRequest(int32_t clientId,
113 const std::vector<int32_t>& tags, const std::vector<int64_t>& configs, const std::string& msg)
114 {
115 socPerf.LimitRequest(clientId, tags, configs, msg);
116 }
117
SetRequestStatus(bool status,const std::string & msg)118 void SocPerfServer::SetRequestStatus(bool status, const std::string &msg)
119 {
120 socPerf.SetRequestStatus(status, msg);
121 }
122
SetThermalLevel(int32_t level)123 void SocPerfServer::SetThermalLevel(int32_t level)
124 {
125 socPerf.SetThermalLevel(level);
126 }
RequestDeviceMode(const std::string & mode,bool status)127 void SocPerfServer::RequestDeviceMode(const std::string& mode, bool status)
128 {
129 socPerf.RequestDeviceMode(mode, status);
130 }
131
RequestCmdIdCount(const std::string & msg)132 std::string SocPerfServer::RequestCmdIdCount(const std::string& msg)
133 {
134 return socPerf.RequestCmdIdCount(msg);
135 }
136 } // namespace SOCPERF
137 } // namespace OHOS
138