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 "core_service_dump_helper.h"
17 
18 #include "core_service.h"
19 #include "enum_convert.h"
20 #include "signal_info.h"
21 #include "signal_information.h"
22 
23 namespace OHOS {
24 namespace Telephony {
Dump(const std::vector<std::string> & args,std::string & result) const25 bool CoreServiceDumpHelper::Dump(const std::vector<std::string> &args, std::string &result) const
26 {
27     result.clear();
28     ShowHelp(result);
29     ShowCoreServiceTimeInfo(result);
30     ShowCoreServiceInfo(result);
31     return true;
32 }
33 
ShowHelp(std::string & result) const34 void CoreServiceDumpHelper::ShowHelp(std::string &result) const
35 {
36     result.append("CoreService:\n")
37         .append("Usage:dump <command> [options]\n")
38         .append("Description:\n")
39         .append("-core_service_info          ")
40         .append("dump all core_service information in the system\n")
41         .append("-input_simulate <event>    ")
42         .append("simulate event from ohos core_service, supported events: login/logout/token_invalid\n")
43         .append("-output_simulate <event>    ")
44         .append("simulate event output\n")
45         .append("-show_log_level        ")
46         .append("show core_service SA's log level\n")
47         .append("-set_log_level <level>     ")
48         .append("set core_service SA's log level\n")
49         .append("-perf_dump         ")
50         .append("dump performance statistics\n");
51 }
52 
to_utf8(std::u16string str16)53 static std::string to_utf8(std::u16string str16)
54 {
55     return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(str16);
56 }
57 
ShowCoreServiceTimeInfo(std::string & result) const58 void CoreServiceDumpHelper::ShowCoreServiceTimeInfo(std::string &result) const
59 {
60     result.append("Ohos core_service service:\n");
61     result.append("BindTime = ");
62     result.append(std::to_string(DelayedSingleton<CoreService>::GetInstance()->GetBindTime()));
63     result.append("\nEndTime = ");
64     result.append(std::to_string(DelayedSingleton<CoreService>::GetInstance()->GetEndTime()));
65     result.append("\nSpendTime = ");
66     result.append(std::to_string(DelayedSingleton<CoreService>::GetInstance()->GetSpendTime()));
67     result.append("\n");
68 }
69 
ShowCoreServiceInfo(std::string & result) const70 void CoreServiceDumpHelper::ShowCoreServiceInfo(std::string &result) const
71 {
72     for (int32_t i = 0; i < SIM_SLOT_COUNT; i++) {
73         bool hasSimCard = false;
74         DelayedSingleton<CoreService>::GetInstance()->HasSimCard(i, hasSimCard);
75         if (hasSimCard) {
76             result.append("SlotId = ");
77             result.append(std::to_string(i));
78             result.append("\nIsSimActive = ");
79             result.append(GetBoolValue(DelayedSingleton<CoreService>::GetInstance()->IsSimActive(i)));
80             result.append("\nIsNrSupported = ");
81             result.append(GetBoolValue(DelayedSingleton<CoreService>::GetInstance()->IsNrSupported(i)));
82             result.append("\nSignalLevel = ");
83             std::vector<sptr<SignalInformation>> signals;
84             DelayedSingleton<CoreService>::GetInstance()->GetSignalInfoList(i, signals);
85             if (signals.size() != 0 && signals[0] != nullptr) {
86                 result.append(std::to_string(signals[0]->GetSignalLevel()));
87             }
88             result.append("\nCardType = ");
89             CardType cardType = CardType::UNKNOWN_CARD;
90             DelayedSingleton<CoreService>::GetInstance()->GetCardType(i, cardType);
91             result.append(GetCardType(static_cast<int32_t>(cardType)));
92             result.append("\nSimState = ");
93             SimState simState = SimState::SIM_STATE_UNKNOWN;
94             DelayedSingleton<CoreService>::GetInstance()->GetSimState(i, simState);
95             result.append(GetSimState(static_cast<int32_t>(simState)));
96             result.append("\nSpn = ");
97             std::u16string spn;
98             DelayedSingleton<CoreService>::GetInstance()->GetSimSpn(i, spn);
99             result.append(to_utf8(spn));
100             result.append("\nOperatorName = ");
101             std::u16string operatorName;
102             DelayedSingleton<CoreService>::GetInstance()->GetOperatorName(i, operatorName);
103             result.append(to_utf8(operatorName));
104             int32_t csRadioTech = 0;
105             int32_t psRadioTech = 0;
106             DelayedSingleton<CoreService>::GetInstance()->GetPsRadioTech(i, psRadioTech);
107             DelayedSingleton<CoreService>::GetInstance()->GetCsRadioTech(i, csRadioTech);
108             result.append("\nPsRadioTech = ");
109             result.append(GetCellularDataConnectionNetworkType(psRadioTech));
110             result.append("\nCsRadioTech = ");
111             result.append(GetCellularDataConnectionNetworkType(csRadioTech));
112             result.append("\n");
113         }
114     }
115     result.append("\n");
116 }
117 } // namespace Telephony
118 } // namespace OHOS