• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "parameter.h"
17 
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <stdatomic.h>
21 
22 #include "param_comm.h"
23 #include "init_param.h"
24 #include "param_init.h"
25 #include "init_utils.h"
26 #include "sysparam_errno.h"
27 #include "securec.h"
28 #include "beget_ext.h"
29 
WaitParameter(const char * key,const char * value,int timeout)30 int WaitParameter(const char *key, const char *value, int timeout)
31 {
32     BEGET_CHECK(!(key == NULL || value == NULL), return EC_INVALID);
33     int ret = SystemWaitParameter(key, value, timeout);
34     BEGET_CHECK_ONLY_ELOG(ret == 0, "WaitParameter failed! the errNum is: %d", ret);
35     return GetSystemError(ret);
36 }
37 
FindParameter(const char * key)38 uint32_t FindParameter(const char *key)
39 {
40     BEGET_CHECK(key != NULL, return (uint32_t)(-1));
41     uint32_t handle = 0;
42     int ret = SystemFindParameter(key, &handle);
43     if (ret != 0) {
44         return (uint32_t)(-1);
45     }
46     return handle;
47 }
48 
GetParameterCommitId(uint32_t handle)49 uint32_t GetParameterCommitId(uint32_t handle)
50 {
51     uint32_t commitId = 0;
52     int ret = SystemGetParameterCommitId(handle, &commitId);
53     BEGET_CHECK(ret == 0, return (uint32_t)(-1));
54     return commitId;
55 }
56 
GetParameterName(uint32_t handle,char * name,uint32_t len)57 int GetParameterName(uint32_t handle, char *name, uint32_t len)
58 {
59     if (name == NULL) {
60         return EC_INVALID;
61     }
62     int ret = SystemGetParameterName(handle, name, len);
63     if (ret == 0) {
64         return strlen(name);
65     }
66     BEGET_CHECK_ONLY_ELOG(ret == 0, "GetParameterName failed! the errNum is: %d", ret);
67     return GetSystemError(ret);
68 }
69 
GetParameterValue(uint32_t handle,char * value,uint32_t len)70 int GetParameterValue(uint32_t handle, char *value, uint32_t len)
71 {
72     if (value == NULL) {
73         return EC_INVALID;
74     }
75     uint32_t size = len;
76     int ret = SystemGetParameterValue(handle, value, &size);
77     if (ret == 0) {
78         return strlen(value);
79     }
80     BEGET_CHECK_ONLY_ELOG(ret == 0, "GetParameterValue failed! the errNum is: %d", ret);
81     return GetSystemError(ret);
82 }
83 
GetParameter(const char * key,const char * def,char * value,uint32_t len)84 int GetParameter(const char *key, const char *def, char *value, uint32_t len)
85 {
86     if ((key == NULL) || (value == NULL)) {
87         return EC_INVALID;
88     }
89     int ret = GetParameter_(key, def, value, len);
90     return (ret != 0) ? ret : strlen(value);
91 }
92 
SetParameter(const char * key,const char * value)93 int SetParameter(const char *key, const char *value)
94 {
95     if ((key == NULL) || (value == NULL)) {
96         return EC_INVALID;
97     }
98     int ret = SystemSetParameter(key, value);
99     BEGET_CHECK_ONLY_ELOG(ret == 0, "SetParameter failed! the errNum is:%d", ret);
100     return GetSystemError(ret);
101 }
102 
SaveParameters(void)103 int SaveParameters(void)
104 {
105     int ret = SystemSaveParameters();
106     return GetSystemError(ret);
107 }
108 
GetDeviceType(void)109 const char *GetDeviceType(void)
110 {
111     static const char *productType = NULL;
112     const char *deviceType = NULL;
113 #ifndef OHOS_LITE
114     deviceType = GetPropertyAtomic("const.product.devicetype", &productType);
115     if (deviceType != NULL) {
116         return deviceType;
117     }
118     return GetPropertyAtomic("const.build.characteristics", &productType);
119 #endif
120     deviceType = GetProperty("const.product.devicetype", &productType);
121     if (deviceType != NULL) {
122         return deviceType;
123     }
124     return GetProperty("const.build.characteristics", &productType);
125 }
126 
GetProductModel(void)127 const char *GetProductModel(void)
128 {
129     return GetProductModel_();
130 }
131 
GetProductModelAlias(void)132 const char *GetProductModelAlias(void)
133 {
134     return GetProductModelAlias_();
135 }
136 
GetManufacture(void)137 const char *GetManufacture(void)
138 {
139     return GetManufacture_();
140 }
141 
GetBrand(void)142 const char *GetBrand(void)
143 {
144     static const char *productBrand = NULL;
145     return GetProperty("const.product.brand", &productBrand);
146 }
147 
GetMarketName(void)148 const char *GetMarketName(void)
149 {
150     static const char *marketName = NULL;
151     return GetProperty("const.product.name", &marketName);
152 }
153 
GetProductSeries(void)154 const char *GetProductSeries(void)
155 {
156     static const char *productSeries = NULL;
157     return GetProperty("const.build.product", &productSeries);
158 }
159 
GetSoftwareModel(void)160 const char *GetSoftwareModel(void)
161 {
162     static const char *softwareModel = NULL;
163     return GetProperty("const.software.model", &softwareModel);
164 }
165 
GetHardwareModel(void)166 const char *GetHardwareModel(void)
167 {
168     static const char *hardwareModel = NULL;
169     return GetProperty("const.product.hardwareversion", &hardwareModel);
170 }
171 
GetHardwareProfile(void)172 const char *GetHardwareProfile(void)
173 {
174     static const char *hardwareProfile = NULL;
175     return GetProperty("const.product.hardwareprofile", &hardwareProfile);
176 }
177 
GetAbiList(void)178 const char *GetAbiList(void)
179 {
180     static const char *productAbiList = NULL;
181     return GetProperty("const.product.cpu.abilist", &productAbiList);
182 }
183 
GetBootloaderVersion(void)184 const char *GetBootloaderVersion(void)
185 {
186     static const char *productBootloader = NULL;
187     return GetProperty("const.product.bootloader.version", &productBootloader);
188 }
189 
GetFirstApiVersion(void)190 int GetFirstApiVersion(void)
191 {
192     static const char *firstApiVersion = NULL;
193     GetProperty("const.product.firstapiversion", &firstApiVersion);
194     if (firstApiVersion == NULL) {
195         return 0;
196     }
197     return atoi(firstApiVersion);
198 }
199 
GetDisplayVersion(void)200 const char *GetDisplayVersion(void)
201 {
202     static const char *displayVersion = NULL;
203     return GetProperty("const.product.software.version", &displayVersion);
204 }
205 
GetIncrementalVersion(void)206 const char *GetIncrementalVersion(void)
207 {
208     static const char *incrementalVersion = NULL;
209     return GetProperty("const.product.incremental.version", &incrementalVersion);
210 }
211 
GetOsReleaseType(void)212 const char *GetOsReleaseType(void)
213 {
214     static const char *osReleaseType = NULL;
215     return GetProperty("const.ohos.releasetype", &osReleaseType);
216 }
217 
GetSdkApiVersion_(void)218 static const char *GetSdkApiVersion_(void)
219 {
220     static const char *sdkApiVersion = NULL;
221     return GetProperty("const.ohos.apiversion", &sdkApiVersion);
222 }
223 
GetBuildType(void)224 const char *GetBuildType(void)
225 {
226     static const char *buildType = NULL;
227     return GetProperty("const.product.build.type", &buildType);
228 }
229 
GetBuildUser(void)230 const char *GetBuildUser(void)
231 {
232     static const char *buildUser = NULL;
233     return GetProperty("const.product.build.user", &buildUser);
234 }
235 
GetBuildHost(void)236 const char *GetBuildHost(void)
237 {
238     static const char *buildHost = NULL;
239     return GetProperty("const.product.build.host", &buildHost);
240 }
241 
GetBuildTime(void)242 const char *GetBuildTime(void)
243 {
244     static const char *buildTime = NULL;
245     return GetProperty("const.product.build.date", &buildTime);
246 }
247 
GetSerial(void)248 const char *GetSerial(void)
249 {
250     return GetSerial_();
251 }
252 
GetDevUdid(char * udid,int size)253 int GetDevUdid(char *udid, int size)
254 {
255     return GetDevUdid_(udid, size);
256 }
257 
BuildOSFullName(void)258 static const char *BuildOSFullName(void)
259 {
260     const char release[] = "Release";
261     const char *releaseType = GetOsReleaseType();
262     const char *fullName = GetFullName_();
263     if (fullName == NULL || releaseType == NULL) {
264         return NULL;
265     }
266     if (strncmp(releaseType, release, sizeof(release) - 1) != 0) {
267         char *value = calloc(1, OS_FULL_NAME_LEN);
268         if (value == NULL) {
269             return NULL;
270         }
271         int length = sprintf_s(value, OS_FULL_NAME_LEN, "%s(%s)", fullName, releaseType);
272         if (length < 0) {
273             free(value);
274             return NULL;
275         }
276         return value;
277     }
278     return strdup(fullName);
279 }
280 
GetOSFullName(void)281 const char *GetOSFullName(void)
282 {
283     static const char *osFullName = NULL;
284     if (osFullName != NULL) {
285         return osFullName;
286     }
287     osFullName = BuildOSFullName();
288     if (osFullName == NULL) {
289         return EMPTY_STR;
290     }
291     return osFullName;
292 }
293 
BuildVersionId(void)294 static const char *BuildVersionId(void)
295 {
296     char value[VERSION_ID_MAX_LEN] = {0};
297     if (GetDeviceType() == NULL) {
298         return NULL;
299     }
300 
301     int len = sprintf_s(value, VERSION_ID_MAX_LEN, "%s/%s/%s/%s/%s/%s/%s/%s/%s/%s",
302         GetDeviceType(), GetManufacture(), GetBrand(), GetProductSeries(),
303         GetOSFullName(), GetProductModel(), GetSoftwareModel(),
304         GetSdkApiVersion_(), GetIncrementalVersion(), GetBuildType());
305     if (len <= 0) {
306         return NULL;
307     }
308     const char *versionId = strdup(value);
309     return versionId;
310 }
311 
GetVersionId(void)312 const char *GetVersionId(void)
313 {
314     static const char *ohosVersionId = NULL;
315     if (ohosVersionId != NULL) {
316         return ohosVersionId;
317     }
318     ohosVersionId = BuildVersionId();
319     if (ohosVersionId == NULL) {
320         return EMPTY_STR;
321     }
322     return ohosVersionId;
323 }
324 
GetSdkApiVersion(void)325 int GetSdkApiVersion(void)
326 {
327     static const char *sdkApiVersion = NULL;
328     GetProperty("const.ohos.apiversion", &sdkApiVersion);
329     if (sdkApiVersion == NULL) {
330         return 0;
331     }
332     return atoi(sdkApiVersion);
333 }
334 
GetSdkMinorApiVersion(void)335 int GetSdkMinorApiVersion(void)
336 {
337     static const char *sdkApiMinorVersion = NULL;
338     GetProperty("const.ohos.apiminorversion", &sdkApiMinorVersion);
339     if (sdkApiMinorVersion == NULL) {
340         return -1;
341     }
342     return atoi(sdkApiMinorVersion);
343 }
344 
GetSdkPatchApiVersion(void)345 int GetSdkPatchApiVersion(void)
346 {
347     static const char *sdkApiPatchVersion = NULL;
348     GetProperty("const.ohos.apipatchversion", &sdkApiPatchVersion);
349     if (sdkApiPatchVersion == NULL) {
350         return -1;
351     }
352     return atoi(sdkApiPatchVersion);
353 }
354 
GetSecurityPatchTag(void)355 const char *GetSecurityPatchTag(void)
356 {
357     static const char *securityPatchTag = NULL;
358     return GetProperty("const.ohos.version.security_patch", &securityPatchTag);
359 }
360 
GetBuildRootHash(void)361 const char *GetBuildRootHash(void)
362 {
363     static const char *buildRootHash = NULL;
364     return GetProperty("const.ohos.buildroothash", &buildRootHash);
365 }
366 
GetIntParameter(const char * key,int32_t def)367 int32_t GetIntParameter(const char *key, int32_t def)
368 {
369     char value[MAX_INT_LEN] = {0};
370     uint32_t size = sizeof(value);
371     int ret = SystemGetParameter(key, value, &size);
372     if (ret != 0) {
373         return def;
374     }
375 
376     long long int result = 0;
377     if (StringToLL(value, &result) != 0) {
378         return def;
379     }
380     if (result <= INT32_MIN || result >= INT32_MAX) {
381         return def;
382     }
383     return (int32_t)result;
384 }
385 
GetUintParameter(const char * key,uint32_t def)386 uint32_t GetUintParameter(const char *key, uint32_t def)
387 {
388     char value[MAX_INT_LEN] = {0};
389     uint32_t size = sizeof(value);
390     int ret = SystemGetParameter(key, value, &size);
391     if (ret != 0) {
392         return def;
393     }
394 
395     unsigned long long int result = 0;
396     if (StringToULL(value, &result) != 0) {
397         return def;
398     }
399     if (result >= UINT32_MAX) {
400         return def;
401     }
402     return (uint32_t)result;
403 }
404 
GetDistributionOSName(void)405 const char *GetDistributionOSName(void)
406 {
407     static const char *distributionOsName = NULL;
408     GetProperty("const.product.os.dist.name", &distributionOsName);
409     if (distributionOsName == NULL) {
410         distributionOsName = EMPTY_STR;
411     }
412     return distributionOsName;
413 }
414 
GetDistributionOSVersion(void)415 const char *GetDistributionOSVersion(void)
416 {
417     static const char *distributionOsVersion = NULL;
418     GetProperty("const.product.os.dist.version", &distributionOsVersion);
419     if (distributionOsVersion == NULL) {
420         distributionOsVersion = GetOSFullName();
421     }
422     return distributionOsVersion;
423 }
424 
GetDistributionOSApiVersion(void)425 int GetDistributionOSApiVersion(void)
426 {
427     static const char *distributionOsApiVersion = NULL;
428     GetProperty("const.product.os.dist.apiversion", &distributionOsApiVersion);
429     if (distributionOsApiVersion == NULL) {
430         distributionOsApiVersion = GetSdkApiVersion_();
431     }
432     return atoi(distributionOsApiVersion);
433 }
GetDistributionOSApiName(void)434 const char *GetDistributionOSApiName(void)
435 {
436     static const char *distributionOsApiName = NULL;
437     GetProperty("const.product.os.dist.apiname", &distributionOsApiName);
438     if (distributionOsApiName == NULL) {
439         distributionOsApiName = EMPTY_STR;
440     }
441     return distributionOsApiName;
442 }
443 
GetDistributionOSReleaseType(void)444 const char *GetDistributionOSReleaseType(void)
445 {
446     static const char *distributionOsReleaseType = NULL;
447     GetProperty("const.product.os.dist.releasetype", &distributionOsReleaseType);
448     if (distributionOsReleaseType == NULL) {
449         distributionOsReleaseType = GetOsReleaseType();
450     }
451     return distributionOsReleaseType;
452 }
453 
GetPerformanceClass(void)454 int GetPerformanceClass(void)
455 {
456     static const char *performanceClass = NULL;
457     GetProperty("const.sys.performance_class", &performanceClass);
458     if (performanceClass == NULL) {
459         return PERFORMANCE_CLASS_HIGH_LEVEL;
460     }
461     int performanceClassValue = atoi(performanceClass);
462     if (performanceClassValue < PERFORMANCE_CLASS_HIGH_LEVEL || performanceClassValue > PERFORMANCE_CLASS_LOW_LEVEL) {
463         return PERFORMANCE_CLASS_HIGH_LEVEL;
464     }
465     return performanceClassValue;
466 }
467