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