• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "parameter_ex.h"
16 #include <cstdint>
17 
18 #include "parameter.h"
19 #include "parameters.h"
20 #include "sysversion.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace Parameter {
25 namespace {
26 constexpr char DEFAULT_DES[] = "unknown";
27 constexpr char PATCH_VERSION[] = "ro.patchversion";
28 }
GetString(const std::string & key,const std::string & defaultValue)29 std::string GetString(const std::string& key, const std::string& defaultValue)
30 {
31     return OHOS::system::GetParameter(key, defaultValue);
32 }
33 
GetInteger(const std::string & key,const int64_t defaultValue)34 int64_t GetInteger(const std::string& key, const int64_t defaultValue)
35 {
36     return OHOS::system::GetIntParameter(key, defaultValue);
37 }
38 
GetUnsignedInteger(const std::string & key,const uint64_t defaultValue)39 uint64_t GetUnsignedInteger(const std::string& key, const uint64_t defaultValue)
40 {
41     return OHOS::system::GetUintParameter(key, defaultValue);
42 }
43 
GetBoolean(const std::string & key,const bool defaultValue)44 bool GetBoolean(const std::string& key, const bool defaultValue)
45 {
46     return OHOS::system::GetBoolParameter(key, defaultValue);
47 }
48 
SetProperty(const std::string & key,const std::string & defaultValue)49 bool SetProperty(const std::string& key, const std::string& defaultValue)
50 {
51     return OHOS::system::SetParameter(key, defaultValue);
52 }
53 
WaitParamSync(const char * key,const char * value,int timeout)54 int WaitParamSync(const char *key, const char *value, int timeout)
55 {
56     return WaitParameter(key, value, timeout);
57 }
58 
WatchParamChange(const char * keyPrefix,ParameterChgPtr callback,void * context)59 int WatchParamChange(const char *keyPrefix, ParameterChgPtr callback, void *context)
60 {
61     return WatchParameter(keyPrefix, callback, context);
62 }
63 
GetDisplayVersionStr()64 std::string GetDisplayVersionStr()
65 {
66     static std::string displayedVersionStr = std::string(GetDisplayVersion());
67     return displayedVersionStr;
68 }
69 
IsBetaVersion()70 bool IsBetaVersion()
71 {
72     static bool isBetaVersion = GetString(KEY_HIVIEW_VERSION_TYPE, "unknown").find("beta") != std::string::npos;
73     return isBetaVersion;
74 }
75 
IsDeveloperMode()76 bool IsDeveloperMode()
77 {
78     return GetBoolean(KEY_DEVELOPER_MODE_STATE, false);
79 }
80 
IsOversea()81 bool IsOversea()
82 {
83     // if param not been set, take it as china version
84     static bool isOversea = (GetString("const.global.region", "CN") != "CN");
85     return isOversea;
86 }
87 
GetUserType()88 UserType GetUserType()
89 {
90     if (IsOversea()) {
91         return IsBetaVersion() ? USER_TYPE_OVERSEA_BETA : USER_TYPE_OVERSEA_COMMERCIAL;
92     } else {
93         return IsBetaVersion() ? USER_TYPE_CHINA_BETA : USER_TYPE_CHINA_COMMERCIAL;
94     }
95 }
96 
IsLeakStateMode()97 bool IsLeakStateMode()
98 {
99     auto leakState = OHOS::system::GetParameter(KEY_LEAKDECTOR_MODE_STATE, "unknown");
100     return (leakState.find("enable") != std::string::npos);
101 }
102 
IsUCollectionSwitchOn()103 bool IsUCollectionSwitchOn()
104 {
105     std::string ucollectionState = GetString(HIVIEW_UCOLLECTION_STATE, "false");
106     return ucollectionState == "true";
107 }
108 
IsTestAppTraceOn()109 bool IsTestAppTraceOn()
110 {
111     std::string appTraceState = GetString(HIVIEW_UCOLLECTION_TEST_APP_TRACE_STATE, "false");
112     return appTraceState == "true";
113 }
114 
IsTraceCollectionSwitchOn()115 bool IsTraceCollectionSwitchOn()
116 {
117     std::string traceCollectionState = GetString(DEVELOP_HIVIEW_TRACE_RECORDER, "false");
118     return traceCollectionState == "true";
119 }
120 
IsLaboratoryMode()121 bool IsLaboratoryMode()
122 {
123     std::string laboratoryState = GetString(KEY_LABORATORY_MODE_STATE, "");
124     return laboratoryState == "reliability";
125 }
126 
GetDeviceTypeStr()127 std::string GetDeviceTypeStr()
128 {
129     static std::string deviceType = OHOS::system::GetDeviceType();
130     return deviceType;
131 }
132 
GetBrandStr()133 std::string GetBrandStr()
134 {
135     static std::string brandStr = std::string(GetBrand());
136     return brandStr;
137 }
138 
GetManufactureStr()139 std::string GetManufactureStr()
140 {
141     static std::string manufactureStr = std::string(GetManufacture());
142     return manufactureStr;
143 }
144 
GetMarketNameStr()145 std::string GetMarketNameStr()
146 {
147     static std::string marketNameStr = std::string(GetMarketName());
148     return marketNameStr;
149 }
150 
GetDeviceModelStr()151 std::string GetDeviceModelStr()
152 {
153     std::string displayedVersionStr = std::string(GetDisplayVersion());
154     auto pos = displayedVersionStr.find(' ');
155     if (pos == displayedVersionStr.npos) {
156         return DEFAULT_DES;
157     }
158     return displayedVersionStr.substr(0, pos);
159 }
160 
GetSysVersionStr()161 std::string GetSysVersionStr()
162 {
163     std::string displayedVersionStr = Parameter::GetDisplayVersionStr();
164     auto pos = displayedVersionStr.find(' ');
165     if (pos == displayedVersionStr.npos) {
166         return displayedVersionStr;
167     }
168     return displayedVersionStr.substr(pos + 1, displayedVersionStr.size());
169 }
170 
GetDistributionOsVersionStr()171 std::string GetDistributionOsVersionStr()
172 {
173     static std::string osVersion = std::string(GetDistributionOSVersion());
174     return osVersion;
175 }
176 
GetProductModelStr()177 std::string GetProductModelStr()
178 {
179     static std::string productModelStr = std::string(GetProductModel());
180     return productModelStr;
181 }
182 
GetSysVersionDetailsStr()183 std::string GetSysVersionDetailsStr()
184 {
185     std::string sysVersionDetails = std::to_string(GetMajorVersion());
186     sysVersionDetails += ".";
187     sysVersionDetails += std::to_string(GetSeniorVersion());
188     sysVersionDetails += ".";
189     sysVersionDetails += std::to_string(GetFeatureVersion());
190     return sysVersionDetails;
191 }
192 
GetVersionTypeStr()193 std::string GetVersionTypeStr()
194 {
195     return IsBetaVersion() ? "Beta" : "Commercial";
196 }
197 
GetPatchVersionStr()198 std::string GetPatchVersionStr()
199 {
200     return GetString(PATCH_VERSION, "");
201 }
202 } // namespace Parameter
203 } // namespace HiviewDFX
204 } // namespace OHOS
205