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