1686862fbSopenharmony_ci/*
2686862fbSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3686862fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4686862fbSopenharmony_ci * you may not use this file except in compliance with the License.
5686862fbSopenharmony_ci * You may obtain a copy of the License at
6686862fbSopenharmony_ci *
7686862fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8686862fbSopenharmony_ci *
9686862fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10686862fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11686862fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12686862fbSopenharmony_ci * See the License for the specific language governing permissions and
13686862fbSopenharmony_ci * limitations under the License.
14686862fbSopenharmony_ci */
15686862fbSopenharmony_ci
16686862fbSopenharmony_ci#include "dfx/distributed_sched_dumper.h"
17686862fbSopenharmony_ci
18686862fbSopenharmony_ci#include "accesstoken_kit.h"
19686862fbSopenharmony_ci#include "dfx/dms_continue_time_dumper.h"
20686862fbSopenharmony_ci#include "distributed_sched_service.h"
21686862fbSopenharmony_ci#include "dtbschedmgr_log.h"
22686862fbSopenharmony_ci#include "ipc_skeleton.h"
23686862fbSopenharmony_ci
24686862fbSopenharmony_cinamespace OHOS {
25686862fbSopenharmony_cinamespace DistributedSchedule {
26686862fbSopenharmony_cinamespace {
27686862fbSopenharmony_ciconst std::string TAG = "DistributedSchedDumper";
28686862fbSopenharmony_ciconst std::string HIDUMPER_PROCESS_NAME = "hidumper_service";
29686862fbSopenharmony_ciconst std::string ARGS_HELP = "-h";
30686862fbSopenharmony_ciconst std::string ARGS_CONNECT_REMOTE_ABILITY = "-connect";
31686862fbSopenharmony_ciconst std::string ARGS_CONNECT_CONTINUETIME_ABILITY = "-continueTime";
32686862fbSopenharmony_ciconstexpr size_t MIN_ARGS_SIZE = 1;
33686862fbSopenharmony_ci}
34686862fbSopenharmony_ci
35686862fbSopenharmony_cibool DistributedSchedDumper::Dump(const std::vector<std::string>& args, std::string& result)
36686862fbSopenharmony_ci{
37686862fbSopenharmony_ci    result.clear();
38686862fbSopenharmony_ci    if (!CanDump()) {
39686862fbSopenharmony_ci        result.append("Dump failed, not allowed");
40686862fbSopenharmony_ci        return false;
41686862fbSopenharmony_ci    }
42686862fbSopenharmony_ci    if (args.size() < MIN_ARGS_SIZE) {
43686862fbSopenharmony_ci        return DumpDefault(result);
44686862fbSopenharmony_ci    }
45686862fbSopenharmony_ci    if (args.size() == MIN_ARGS_SIZE) {
46686862fbSopenharmony_ci        // -h
47686862fbSopenharmony_ci        if (args[0] == ARGS_HELP) {
48686862fbSopenharmony_ci            ShowHelp(result);
49686862fbSopenharmony_ci            return true;
50686862fbSopenharmony_ci        }
51686862fbSopenharmony_ci        // -connect
52686862fbSopenharmony_ci        if (args[0] == ARGS_CONNECT_REMOTE_ABILITY) {
53686862fbSopenharmony_ci            ShowConnectRemoteAbility(result);
54686862fbSopenharmony_ci            return true;
55686862fbSopenharmony_ci        }
56686862fbSopenharmony_ci        // -continueTime
57686862fbSopenharmony_ci        if (args[0] == ARGS_CONNECT_CONTINUETIME_ABILITY) {
58686862fbSopenharmony_ci            ShowDuration(result);
59686862fbSopenharmony_ci            return true;
60686862fbSopenharmony_ci        }
61686862fbSopenharmony_ci    }
62686862fbSopenharmony_ci    IllegalInput(result);
63686862fbSopenharmony_ci    return false;
64686862fbSopenharmony_ci}
65686862fbSopenharmony_ci
66686862fbSopenharmony_cibool DistributedSchedDumper::CanDump()
67686862fbSopenharmony_ci{
68686862fbSopenharmony_ci    uint32_t accessToken = IPCSkeleton::GetCallingTokenID();
69686862fbSopenharmony_ci    Security::AccessToken::NativeTokenInfo nativeTokenInfo;
70686862fbSopenharmony_ci    int32_t result = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(accessToken, nativeTokenInfo);
71686862fbSopenharmony_ci    if (result == ERR_OK && nativeTokenInfo.processName == HIDUMPER_PROCESS_NAME) {
72686862fbSopenharmony_ci        return true;
73686862fbSopenharmony_ci    }
74686862fbSopenharmony_ci    return false;
75686862fbSopenharmony_ci}
76686862fbSopenharmony_ci
77686862fbSopenharmony_cibool DistributedSchedDumper::DumpDefault(std::string& result)
78686862fbSopenharmony_ci{
79686862fbSopenharmony_ci    result.append("DistributedSched Dump\n");
80686862fbSopenharmony_ci    result.append("\n");
81686862fbSopenharmony_ci    ShowConnectRemoteAbility(result);
82686862fbSopenharmony_ci    return true;
83686862fbSopenharmony_ci}
84686862fbSopenharmony_ci
85686862fbSopenharmony_civoid DistributedSchedDumper::ShowConnectRemoteAbility(std::string& result)
86686862fbSopenharmony_ci{
87686862fbSopenharmony_ci    DistributedSchedService::GetInstance().DumpConnectInfo(result);
88686862fbSopenharmony_ci}
89686862fbSopenharmony_ci
90686862fbSopenharmony_civoid DistributedSchedDumper::ShowDuration(std::string& result)
91686862fbSopenharmony_ci{
92686862fbSopenharmony_ci    DmsContinueTime::GetInstance().ShowInfo(result);
93686862fbSopenharmony_ci}
94686862fbSopenharmony_ci
95686862fbSopenharmony_civoid DistributedSchedDumper::ShowHelp(std::string& result)
96686862fbSopenharmony_ci{
97686862fbSopenharmony_ci    result.append("DistributedSched Dump options:\n")
98686862fbSopenharmony_ci        .append("  [-h] [cmd]...\n")
99686862fbSopenharmony_ci        .append("cmd maybe one of:\n")
100686862fbSopenharmony_ci        .append("  -connect: show all connected remote abilities.\n");
101686862fbSopenharmony_ci}
102686862fbSopenharmony_ci
103686862fbSopenharmony_civoid DistributedSchedDumper::IllegalInput(std::string& result)
104686862fbSopenharmony_ci{
105686862fbSopenharmony_ci    result.append("The arguments are illegal and you can enter '-h' for help.\n");
106686862fbSopenharmony_ci}
107686862fbSopenharmony_ci} // namespace DistributedSchedule
108686862fbSopenharmony_ci} // namespace OHOS