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