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 
IsLeakStateMode()81 bool IsLeakStateMode()
82 {
83     auto leakState = OHOS::system::GetParameter(KEY_LEAKDECTOR_MODE_STATE, "unknown");
84     return (leakState.find("enable") != std::string::npos);
85 }
86 
IsUCollectionSwitchOn()87 bool IsUCollectionSwitchOn()
88 {
89     std::string ucollectionState = GetString(HIVIEW_UCOLLECTION_STATE, "false");
90     return ucollectionState == "true";
91 }
92 
IsTestAppTraceOn()93 bool IsTestAppTraceOn()
94 {
95     std::string appTraceState = GetString(HIVIEW_UCOLLECTION_TEST_APP_TRACE_STATE, "false");
96     return appTraceState == "true";
97 }
98 
IsTraceCollectionSwitchOn()99 bool IsTraceCollectionSwitchOn()
100 {
101     std::string traceCollectionState = GetString(DEVELOP_HIVIEW_TRACE_RECORDER, "false");
102     return traceCollectionState == "true";
103 }
104 
IsLaboratoryMode()105 bool IsLaboratoryMode()
106 {
107     std::string laboratoryState = GetString(KEY_LABORATORY_MODE_STATE, "");
108     return laboratoryState == "reliability";
109 }
110 
GetDeviceTypeStr()111 std::string GetDeviceTypeStr()
112 {
113     static std::string deviceType = OHOS::system::GetDeviceType();
114     return deviceType;
115 }
116 
GetBrandStr()117 std::string GetBrandStr()
118 {
119     static std::string brandStr = std::string(GetBrand());
120     return brandStr;
121 }
122 
GetManufactureStr()123 std::string GetManufactureStr()
124 {
125     static std::string manufactureStr = std::string(GetManufacture());
126     return manufactureStr;
127 }
128 
GetMarketNameStr()129 std::string GetMarketNameStr()
130 {
131     static std::string marketNameStr = std::string(GetMarketName());
132     return marketNameStr;
133 }
134 
GetDeviceModelStr()135 std::string GetDeviceModelStr()
136 {
137     std::string displayedVersionStr = std::string(GetDisplayVersion());
138     auto pos = displayedVersionStr.find(' ');
139     if (pos == displayedVersionStr.npos) {
140         return DEFAULT_DES;
141     }
142     return displayedVersionStr.substr(0, pos);
143 }
144 
GetSysVersionStr()145 std::string GetSysVersionStr()
146 {
147     std::string displayedVersionStr = Parameter::GetDisplayVersionStr();
148     auto pos = displayedVersionStr.find(' ');
149     if (pos == displayedVersionStr.npos) {
150         return displayedVersionStr;
151     }
152     return displayedVersionStr.substr(pos + 1, displayedVersionStr.size());
153 }
154 
GetDistributionOsVersionStr()155 std::string GetDistributionOsVersionStr()
156 {
157     static std::string osVersion = std::string(GetDistributionOSVersion());
158     return osVersion;
159 }
160 
GetProductModelStr()161 std::string GetProductModelStr()
162 {
163     static std::string productModelStr = std::string(GetProductModel());
164     return productModelStr;
165 }
166 
GetSysVersionDetailsStr()167 std::string GetSysVersionDetailsStr()
168 {
169     std::string sysVersionDetails = std::to_string(GetMajorVersion());
170     sysVersionDetails += ".";
171     sysVersionDetails += std::to_string(GetSeniorVersion());
172     sysVersionDetails += ".";
173     sysVersionDetails += std::to_string(GetFeatureVersion());
174     return sysVersionDetails;
175 }
176 
GetVersionStr()177 std::string GetVersionStr()
178 {
179     return IsBetaVersion() ? "Beta" : "Commercial";
180 }
181 
GetRegionCode()182 int32_t GetRegionCode()
183 {
184     constexpr int32_t defaultCode = 156;
185     return defaultCode;
186 }
187 
GetPatchVersionStr()188 std::string GetPatchVersionStr()
189 {
190     return GetString(PATCH_VERSION, "");
191 }
192 } // namespace Parameter
193 } // namespace HiviewDFX
194 } // namespace OHOS
195