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