13fc297bdSopenharmony_ci/*
23fc297bdSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
33fc297bdSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43fc297bdSopenharmony_ci * you may not use this file except in compliance with the License.
53fc297bdSopenharmony_ci * You may obtain a copy of the License at
63fc297bdSopenharmony_ci *
73fc297bdSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83fc297bdSopenharmony_ci *
93fc297bdSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103fc297bdSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113fc297bdSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123fc297bdSopenharmony_ci * See the License for the specific language governing permissions and
133fc297bdSopenharmony_ci * limitations under the License.
143fc297bdSopenharmony_ci */
153fc297bdSopenharmony_ci
163fc297bdSopenharmony_ci
173fc297bdSopenharmony_ci#include <cstdint>           // for int32_t
183fc297bdSopenharmony_ci#include <cstdlib>           // for atoi
193fc297bdSopenharmony_ci#include <vector>            // for vector
203fc297bdSopenharmony_ci#include <cstring>           // for strcmp
213fc297bdSopenharmony_ci#include "accesstoken_kit.h"
223fc297bdSopenharmony_ci#include "nativetoken_kit.h"
233fc297bdSopenharmony_ci#include "socperf_client.h"  // for SocPerfClient
243fc297bdSopenharmony_ci#include "socperf_log.h"
253fc297bdSopenharmony_ci#include "token_setproc.h"
263fc297bdSopenharmony_ci
273fc297bdSopenharmony_ciconst static int32_t PARAMETERS_NUM_MIN        = 2;
283fc297bdSopenharmony_ciconst static int32_t PARAMETERS_NUM_WITHOUT_EX = 3;
293fc297bdSopenharmony_ciconst static int32_t PARAMETERS_NUM_WITH_EX    = 4;
303fc297bdSopenharmony_ciconst static int32_t PARAMETERS_NUM_LIMIT      = 5;
313fc297bdSopenharmony_ci
323fc297bdSopenharmony_cistatic void PerfRequest(int32_t argc, char *argv[])
333fc297bdSopenharmony_ci{
343fc297bdSopenharmony_ci    if (argc == PARAMETERS_NUM_WITHOUT_EX) {
353fc297bdSopenharmony_ci        char* cmdId = argv[2];
363fc297bdSopenharmony_ci        if (cmdId) {
373fc297bdSopenharmony_ci            OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(atoi(cmdId), "");
383fc297bdSopenharmony_ci        }
393fc297bdSopenharmony_ci    }
403fc297bdSopenharmony_ci}
413fc297bdSopenharmony_ci
423fc297bdSopenharmony_cistatic void PerfRequestEx(int32_t argc, char *argv[])
433fc297bdSopenharmony_ci{
443fc297bdSopenharmony_ci    if (argc == PARAMETERS_NUM_WITH_EX) {
453fc297bdSopenharmony_ci        char* cmdId = argv[2];
463fc297bdSopenharmony_ci        char* onOffTag = argv[3];
473fc297bdSopenharmony_ci        if (cmdId && onOffTag) {
483fc297bdSopenharmony_ci            if (strcmp(onOffTag, "true") == 0) {
493fc297bdSopenharmony_ci                OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequestEx(atoi(cmdId), true, "");
503fc297bdSopenharmony_ci            } else if (strcmp(onOffTag, "false") == 0) {
513fc297bdSopenharmony_ci                OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequestEx(atoi(cmdId), false, "");
523fc297bdSopenharmony_ci            }
533fc297bdSopenharmony_ci        }
543fc297bdSopenharmony_ci    }
553fc297bdSopenharmony_ci}
563fc297bdSopenharmony_ci
573fc297bdSopenharmony_cistatic void PowerLimitBoost(int32_t argc, char *argv[])
583fc297bdSopenharmony_ci{
593fc297bdSopenharmony_ci    if (argc == PARAMETERS_NUM_WITHOUT_EX) {
603fc297bdSopenharmony_ci        char* onOffTag = argv[2];
613fc297bdSopenharmony_ci        if (onOffTag) {
623fc297bdSopenharmony_ci            if (strcmp(onOffTag, "true") == 0) {
633fc297bdSopenharmony_ci                OHOS::SOCPERF::SocPerfClient::GetInstance().PowerLimitBoost(true, "");
643fc297bdSopenharmony_ci            } else if (strcmp(onOffTag, "false") == 0) {
653fc297bdSopenharmony_ci                OHOS::SOCPERF::SocPerfClient::GetInstance().PowerLimitBoost(false, "");
663fc297bdSopenharmony_ci            }
673fc297bdSopenharmony_ci        }
683fc297bdSopenharmony_ci    }
693fc297bdSopenharmony_ci}
703fc297bdSopenharmony_ci
713fc297bdSopenharmony_cistatic void ThermalLimitBoost(int32_t argc, char *argv[])
723fc297bdSopenharmony_ci{
733fc297bdSopenharmony_ci    if (argc == PARAMETERS_NUM_WITHOUT_EX) {
743fc297bdSopenharmony_ci        char* onOffTag = argv[2];
753fc297bdSopenharmony_ci        if (onOffTag) {
763fc297bdSopenharmony_ci            if (strcmp(onOffTag, "true") == 0) {
773fc297bdSopenharmony_ci                OHOS::SOCPERF::SocPerfClient::GetInstance().ThermalLimitBoost(true, "");
783fc297bdSopenharmony_ci            } else if (strcmp(onOffTag, "false") == 0) {
793fc297bdSopenharmony_ci                OHOS::SOCPERF::SocPerfClient::GetInstance().ThermalLimitBoost(false, "");
803fc297bdSopenharmony_ci            }
813fc297bdSopenharmony_ci        }
823fc297bdSopenharmony_ci    }
833fc297bdSopenharmony_ci}
843fc297bdSopenharmony_ci
853fc297bdSopenharmony_cistatic void LimitRequest(int32_t argc, char *argv[])
863fc297bdSopenharmony_ci{
873fc297bdSopenharmony_ci    if (argc == PARAMETERS_NUM_LIMIT) {
883fc297bdSopenharmony_ci        char* clientId = argv[2];
893fc297bdSopenharmony_ci        char* tags = argv[3];
903fc297bdSopenharmony_ci        char* configs = argv[4];
913fc297bdSopenharmony_ci        std::vector<int32_t> tagsVector = { atoi(tags) };
923fc297bdSopenharmony_ci        std::vector<int64_t> configsVector = { atoll(configs) };
933fc297bdSopenharmony_ci        OHOS::SOCPERF::SocPerfClient::GetInstance().LimitRequest(atoi(clientId), tagsVector, configsVector, "");
943fc297bdSopenharmony_ci    }
953fc297bdSopenharmony_ci}
963fc297bdSopenharmony_ci
973fc297bdSopenharmony_cistatic void SetRequestStatus(int32_t argc, char *argv[])
983fc297bdSopenharmony_ci{
993fc297bdSopenharmony_ci    if (argc == PARAMETERS_NUM_WITHOUT_EX) {
1003fc297bdSopenharmony_ci        char* status = argv[2];
1013fc297bdSopenharmony_ci        if (strcmp(status, "true") == 0) {
1023fc297bdSopenharmony_ci            OHOS::SOCPERF::SocPerfClient::GetInstance().SetRequestStatus(true, "");
1033fc297bdSopenharmony_ci        } else if (strcmp(status, "false") == 0) {
1043fc297bdSopenharmony_ci            OHOS::SOCPERF::SocPerfClient::GetInstance().SetRequestStatus(false, "");
1053fc297bdSopenharmony_ci        }
1063fc297bdSopenharmony_ci    }
1073fc297bdSopenharmony_ci}
1083fc297bdSopenharmony_ci
1093fc297bdSopenharmony_cistatic void SetThermalLevel(int32_t argc, char *argv[])
1103fc297bdSopenharmony_ci{
1113fc297bdSopenharmony_ci    if (argc == PARAMETERS_NUM_WITHOUT_EX) {
1123fc297bdSopenharmony_ci        char* level = argv[2];
1133fc297bdSopenharmony_ci        OHOS::SOCPERF::SocPerfClient::GetInstance().SetThermalLevel(atoi(level));
1143fc297bdSopenharmony_ci    }
1153fc297bdSopenharmony_ci}
1163fc297bdSopenharmony_ci
1173fc297bdSopenharmony_cistatic void RequestDeviceMode(int32_t argc, char *argv[])
1183fc297bdSopenharmony_ci{
1193fc297bdSopenharmony_ci    if (argc == PARAMETERS_NUM_WITHOUT_EX) {
1203fc297bdSopenharmony_ci        std::string mode = argv[2];
1213fc297bdSopenharmony_ci        char* status = argv[3];
1223fc297bdSopenharmony_ci        if (strcmp(status, "true") == 0) {
1233fc297bdSopenharmony_ci            OHOS::SOCPERF::SocPerfClient::GetInstance().RequestDeviceMode(mode, true);
1243fc297bdSopenharmony_ci        } else if (strcmp(status, "false") == 0) {
1253fc297bdSopenharmony_ci            OHOS::SOCPERF::SocPerfClient::GetInstance().RequestDeviceMode(mode, false);
1263fc297bdSopenharmony_ci        }
1273fc297bdSopenharmony_ci    }
1283fc297bdSopenharmony_ci}
1293fc297bdSopenharmony_ci
1303fc297bdSopenharmony_cistatic void RequestCmdIdCount(int32_t argc, char *argv[])
1313fc297bdSopenharmony_ci{
1323fc297bdSopenharmony_ci    if (argc == PARAMETERS_NUM_MIN) {
1333fc297bdSopenharmony_ci        std::string ret = OHOS::SOCPERF::SocPerfClient::GetInstance().RequestCmdIdCount("");
1343fc297bdSopenharmony_ci        SOC_PERF_LOGI("%{public}s", ret.c_str());
1353fc297bdSopenharmony_ci    }
1363fc297bdSopenharmony_ci}
1373fc297bdSopenharmony_ci
1383fc297bdSopenharmony_cistatic void MockProcess()
1393fc297bdSopenharmony_ci{
1403fc297bdSopenharmony_ci    static const char *perms[] = {
1413fc297bdSopenharmony_ci        "ohos.permission.REPORT_RESOURCE_SCHEDULE_EVENT"
1423fc297bdSopenharmony_ci    };
1433fc297bdSopenharmony_ci    uint64_t tokenId;
1443fc297bdSopenharmony_ci    NativeTokenInfoParams infoInstance = {
1453fc297bdSopenharmony_ci        .dcapsNum = 0,
1463fc297bdSopenharmony_ci        .permsNum = sizeof(perms) / sizeof(perms[0]),
1473fc297bdSopenharmony_ci        .aclsNum = 0,
1483fc297bdSopenharmony_ci        .dcaps = nullptr,
1493fc297bdSopenharmony_ci        .perms = perms,
1503fc297bdSopenharmony_ci        .acls = nullptr,
1513fc297bdSopenharmony_ci        .processName = "socperf_test",
1523fc297bdSopenharmony_ci        .aplStr = "system_core",
1533fc297bdSopenharmony_ci    };
1543fc297bdSopenharmony_ci    tokenId = GetAccessTokenId(&infoInstance);
1553fc297bdSopenharmony_ci    SetSelfTokenID(tokenId);
1563fc297bdSopenharmony_ci    OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
1573fc297bdSopenharmony_ci}
1583fc297bdSopenharmony_ci
1593fc297bdSopenharmony_ciint32_t main(int32_t argc, char *argv[])
1603fc297bdSopenharmony_ci{
1613fc297bdSopenharmony_ci    MockProcess();
1623fc297bdSopenharmony_ci    if (argc >= PARAMETERS_NUM_MIN && argv) {
1633fc297bdSopenharmony_ci        char* function = argv[1];
1643fc297bdSopenharmony_ci        if (strcmp(function, "PerfRequest") == 0) {
1653fc297bdSopenharmony_ci            PerfRequest(argc, argv);
1663fc297bdSopenharmony_ci        } else if (strcmp(function, "PerfRequestEx") == 0) {
1673fc297bdSopenharmony_ci            PerfRequestEx(argc, argv);
1683fc297bdSopenharmony_ci        } else if (strcmp(function, "PowerLimitBoost") == 0) {
1693fc297bdSopenharmony_ci            PowerLimitBoost(argc, argv);
1703fc297bdSopenharmony_ci        } else if (strcmp(function, "ThermalLimitBoost") == 0) {
1713fc297bdSopenharmony_ci            ThermalLimitBoost(argc, argv);
1723fc297bdSopenharmony_ci        } else if (strcmp(function, "LimitRequest") == 0) {
1733fc297bdSopenharmony_ci            LimitRequest(argc, argv);
1743fc297bdSopenharmony_ci        } else if (strcmp(function, "SetRequestStatus") == 0) {
1753fc297bdSopenharmony_ci            SetRequestStatus(argc, argv);
1763fc297bdSopenharmony_ci        } else if (strcmp(function, "SetThermalLevel") == 0) {
1773fc297bdSopenharmony_ci            SetThermalLevel(argc, argv);
1783fc297bdSopenharmony_ci        } else if (strcmp(function, "RequestDeviceMode") == 0) {
1793fc297bdSopenharmony_ci            RequestDeviceMode(argc, argv);
1803fc297bdSopenharmony_ci        } else if (strcmp(function, "RequestCmdIdCount") == 0) {
1813fc297bdSopenharmony_ci            RequestCmdIdCount(argc, argv);
1823fc297bdSopenharmony_ci        }
1833fc297bdSopenharmony_ci    }
1843fc297bdSopenharmony_ci    return 0;
1853fc297bdSopenharmony_ci}
186