1 /*
2  * Copyright (c) 2022-2023 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 
17 #include <cstdint>           // for int32_t
18 #include <cstdlib>           // for atoi
19 #include <vector>            // for vector
20 #include <cstring>           // for strcmp
21 #include "accesstoken_kit.h"
22 #include "nativetoken_kit.h"
23 #include "socperf_client.h"  // for SocPerfClient
24 #include "socperf_log.h"
25 #include "token_setproc.h"
26 
27 const static int32_t PARAMETERS_NUM_MIN        = 2;
28 const static int32_t PARAMETERS_NUM_WITHOUT_EX = 3;
29 const static int32_t PARAMETERS_NUM_WITH_EX    = 4;
30 const static int32_t PARAMETERS_NUM_LIMIT      = 5;
31 
PerfRequest(int32_t argc,char * argv[])32 static void PerfRequest(int32_t argc, char *argv[])
33 {
34     if (argc == PARAMETERS_NUM_WITHOUT_EX) {
35         char* cmdId = argv[2];
36         if (cmdId) {
37             OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(atoi(cmdId), "");
38         }
39     }
40 }
41 
PerfRequestEx(int32_t argc,char * argv[])42 static void PerfRequestEx(int32_t argc, char *argv[])
43 {
44     if (argc == PARAMETERS_NUM_WITH_EX) {
45         char* cmdId = argv[2];
46         char* onOffTag = argv[3];
47         if (cmdId && onOffTag) {
48             if (strcmp(onOffTag, "true") == 0) {
49                 OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequestEx(atoi(cmdId), true, "");
50             } else if (strcmp(onOffTag, "false") == 0) {
51                 OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequestEx(atoi(cmdId), false, "");
52             }
53         }
54     }
55 }
56 
PowerLimitBoost(int32_t argc,char * argv[])57 static void PowerLimitBoost(int32_t argc, char *argv[])
58 {
59     if (argc == PARAMETERS_NUM_WITHOUT_EX) {
60         char* onOffTag = argv[2];
61         if (onOffTag) {
62             if (strcmp(onOffTag, "true") == 0) {
63                 OHOS::SOCPERF::SocPerfClient::GetInstance().PowerLimitBoost(true, "");
64             } else if (strcmp(onOffTag, "false") == 0) {
65                 OHOS::SOCPERF::SocPerfClient::GetInstance().PowerLimitBoost(false, "");
66             }
67         }
68     }
69 }
70 
ThermalLimitBoost(int32_t argc,char * argv[])71 static void ThermalLimitBoost(int32_t argc, char *argv[])
72 {
73     if (argc == PARAMETERS_NUM_WITHOUT_EX) {
74         char* onOffTag = argv[2];
75         if (onOffTag) {
76             if (strcmp(onOffTag, "true") == 0) {
77                 OHOS::SOCPERF::SocPerfClient::GetInstance().ThermalLimitBoost(true, "");
78             } else if (strcmp(onOffTag, "false") == 0) {
79                 OHOS::SOCPERF::SocPerfClient::GetInstance().ThermalLimitBoost(false, "");
80             }
81         }
82     }
83 }
84 
LimitRequest(int32_t argc,char * argv[])85 static void LimitRequest(int32_t argc, char *argv[])
86 {
87     if (argc == PARAMETERS_NUM_LIMIT) {
88         char* clientId = argv[2];
89         char* tags = argv[3];
90         char* configs = argv[4];
91         std::vector<int32_t> tagsVector = { atoi(tags) };
92         std::vector<int64_t> configsVector = { atoll(configs) };
93         OHOS::SOCPERF::SocPerfClient::GetInstance().LimitRequest(atoi(clientId), tagsVector, configsVector, "");
94     }
95 }
96 
SetRequestStatus(int32_t argc,char * argv[])97 static void SetRequestStatus(int32_t argc, char *argv[])
98 {
99     if (argc == PARAMETERS_NUM_WITHOUT_EX) {
100         char* status = argv[2];
101         if (strcmp(status, "true") == 0) {
102             OHOS::SOCPERF::SocPerfClient::GetInstance().SetRequestStatus(true, "");
103         } else if (strcmp(status, "false") == 0) {
104             OHOS::SOCPERF::SocPerfClient::GetInstance().SetRequestStatus(false, "");
105         }
106     }
107 }
108 
SetThermalLevel(int32_t argc,char * argv[])109 static void SetThermalLevel(int32_t argc, char *argv[])
110 {
111     if (argc == PARAMETERS_NUM_WITHOUT_EX) {
112         char* level = argv[2];
113         OHOS::SOCPERF::SocPerfClient::GetInstance().SetThermalLevel(atoi(level));
114     }
115 }
116 
RequestDeviceMode(int32_t argc,char * argv[])117 static void RequestDeviceMode(int32_t argc, char *argv[])
118 {
119     if (argc == PARAMETERS_NUM_WITHOUT_EX) {
120         std::string mode = argv[2];
121         char* status = argv[3];
122         if (strcmp(status, "true") == 0) {
123             OHOS::SOCPERF::SocPerfClient::GetInstance().RequestDeviceMode(mode, true);
124         } else if (strcmp(status, "false") == 0) {
125             OHOS::SOCPERF::SocPerfClient::GetInstance().RequestDeviceMode(mode, false);
126         }
127     }
128 }
129 
RequestCmdIdCount(int32_t argc,char * argv[])130 static void RequestCmdIdCount(int32_t argc, char *argv[])
131 {
132     if (argc == PARAMETERS_NUM_MIN) {
133         std::string ret = OHOS::SOCPERF::SocPerfClient::GetInstance().RequestCmdIdCount("");
134         SOC_PERF_LOGI("%{public}s", ret.c_str());
135     }
136 }
137 
MockProcess()138 static void MockProcess()
139 {
140     static const char *perms[] = {
141         "ohos.permission.REPORT_RESOURCE_SCHEDULE_EVENT"
142     };
143     uint64_t tokenId;
144     NativeTokenInfoParams infoInstance = {
145         .dcapsNum = 0,
146         .permsNum = sizeof(perms) / sizeof(perms[0]),
147         .aclsNum = 0,
148         .dcaps = nullptr,
149         .perms = perms,
150         .acls = nullptr,
151         .processName = "socperf_test",
152         .aplStr = "system_core",
153     };
154     tokenId = GetAccessTokenId(&infoInstance);
155     SetSelfTokenID(tokenId);
156     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
157 }
158 
main(int32_t argc,char * argv[])159 int32_t main(int32_t argc, char *argv[])
160 {
161     MockProcess();
162     if (argc >= PARAMETERS_NUM_MIN && argv) {
163         char* function = argv[1];
164         if (strcmp(function, "PerfRequest") == 0) {
165             PerfRequest(argc, argv);
166         } else if (strcmp(function, "PerfRequestEx") == 0) {
167             PerfRequestEx(argc, argv);
168         } else if (strcmp(function, "PowerLimitBoost") == 0) {
169             PowerLimitBoost(argc, argv);
170         } else if (strcmp(function, "ThermalLimitBoost") == 0) {
171             ThermalLimitBoost(argc, argv);
172         } else if (strcmp(function, "LimitRequest") == 0) {
173             LimitRequest(argc, argv);
174         } else if (strcmp(function, "SetRequestStatus") == 0) {
175             SetRequestStatus(argc, argv);
176         } else if (strcmp(function, "SetThermalLevel") == 0) {
177             SetThermalLevel(argc, argv);
178         } else if (strcmp(function, "RequestDeviceMode") == 0) {
179             RequestDeviceMode(argc, argv);
180         } else if (strcmp(function, "RequestCmdIdCount") == 0) {
181             RequestCmdIdCount(argc, argv);
182         }
183     }
184     return 0;
185 }
186