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
29int 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
37uint32_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
48uint32_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
56int 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
69int 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
83int 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
92int 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
102int SaveParameters(void)
103{
104    int ret = SystemSaveParameters();
105    return GetSystemError(ret);
106}
107
108const 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
118const char *GetProductModel(void)
119{
120    return GetProductModel_();
121}
122
123const char *GetManufacture(void)
124{
125    return GetManufacture_();
126}
127
128const char *GetBrand(void)
129{
130    static const char *productBrand = NULL;
131    return GetProperty("const.product.brand", &productBrand);
132}
133
134const char *GetMarketName(void)
135{
136    static const char *marketName = NULL;
137    return GetProperty("const.product.name", &marketName);
138}
139
140const char *GetProductSeries(void)
141{
142    static const char *productSeries = NULL;
143    return GetProperty("const.build.product", &productSeries);
144}
145
146const char *GetSoftwareModel(void)
147{
148    static const char *softwareModel = NULL;
149    return GetProperty("const.software.model", &softwareModel);
150}
151
152const char *GetHardwareModel(void)
153{
154    static const char *hardwareModel = NULL;
155    return GetProperty("const.product.hardwareversion", &hardwareModel);
156}
157
158const char *GetHardwareProfile(void)
159{
160    static const char *hardwareProfile = NULL;
161    return GetProperty("const.product.hardwareprofile", &hardwareProfile);
162}
163
164const char *GetAbiList(void)
165{
166    static const char *productAbiList = NULL;
167    return GetProperty("const.product.cpu.abilist", &productAbiList);
168}
169
170const char *GetBootloaderVersion(void)
171{
172    static const char *productBootloader = NULL;
173    return GetProperty("const.product.bootloader.version", &productBootloader);
174}
175
176int GetFirstApiVersion(void)
177{
178    static const char *firstApiVersion = NULL;
179    GetProperty("const.product.firstapiversion", &firstApiVersion);
180    if (firstApiVersion == NULL) {
181        return 0;
182    }
183    return atoi(firstApiVersion);
184}
185
186const char *GetDisplayVersion(void)
187{
188    static const char *displayVersion = NULL;
189    return GetProperty("const.product.software.version", &displayVersion);
190}
191
192const char *GetIncrementalVersion(void)
193{
194    static const char *incrementalVersion = NULL;
195    return GetProperty("const.product.incremental.version", &incrementalVersion);
196}
197
198const char *GetOsReleaseType(void)
199{
200    static const char *osReleaseType = NULL;
201    return GetProperty("const.ohos.releasetype", &osReleaseType);
202}
203
204static const char *GetSdkApiVersion_(void)
205{
206    static const char *sdkApiVersion = NULL;
207    return GetProperty("const.ohos.apiversion", &sdkApiVersion);
208}
209
210const char *GetBuildType(void)
211{
212    static const char *buildType = NULL;
213    return GetProperty("const.product.build.type", &buildType);
214}
215
216const char *GetBuildUser(void)
217{
218    static const char *buildUser = NULL;
219    return GetProperty("const.product.build.user", &buildUser);
220}
221
222const char *GetBuildHost(void)
223{
224    static const char *buildHost = NULL;
225    return GetProperty("const.product.build.host", &buildHost);
226}
227
228const char *GetBuildTime(void)
229{
230    static const char *buildTime = NULL;
231    return GetProperty("const.product.build.date", &buildTime);
232}
233
234const char *GetSerial(void)
235{
236    return GetSerial_();
237}
238
239int GetDevUdid(char *udid, int size)
240{
241    return GetDevUdid_(udid, size);
242}
243
244static const char *BuildOSFullName(void)
245{
246    const char release[] = "Release";
247    const char *releaseType = GetOsReleaseType();
248    const char *fullName = GetFullName_();
249    if (fullName == NULL || releaseType == NULL) {
250        return NULL;
251    }
252    if (strncmp(releaseType, release, sizeof(release) - 1) != 0) {
253        char *value = calloc(1, OS_FULL_NAME_LEN);
254        if (value == NULL) {
255            return NULL;
256        }
257        int length = sprintf_s(value, OS_FULL_NAME_LEN, "%s(%s)", fullName, releaseType);
258        if (length < 0) {
259            free(value);
260            return NULL;
261        }
262        return value;
263    }
264    return strdup(fullName);
265}
266
267const char *GetOSFullName(void)
268{
269    static const char *osFullName = NULL;
270    if (osFullName != NULL) {
271        return osFullName;
272    }
273    osFullName = BuildOSFullName();
274    if (osFullName == NULL) {
275        return EMPTY_STR;
276    }
277    return osFullName;
278}
279
280static const char *BuildVersionId(void)
281{
282    char value[VERSION_ID_MAX_LEN] = {0};
283    if (GetDeviceType() == NULL) {
284        return NULL;
285    }
286
287    int len = sprintf_s(value, VERSION_ID_MAX_LEN, "%s/%s/%s/%s/%s/%s/%s/%s/%s/%s",
288        GetDeviceType(), GetManufacture(), GetBrand(), GetProductSeries(),
289        GetOSFullName(), GetProductModel(), GetSoftwareModel(),
290        GetSdkApiVersion_(), GetIncrementalVersion(), GetBuildType());
291    if (len <= 0) {
292        return NULL;
293    }
294    const char *versionId = strdup(value);
295    return versionId;
296}
297
298const char *GetVersionId(void)
299{
300    static const char *ohosVersionId = NULL;
301    if (ohosVersionId != NULL) {
302        return ohosVersionId;
303    }
304    ohosVersionId = BuildVersionId();
305    if (ohosVersionId == NULL) {
306        return EMPTY_STR;
307    }
308    return ohosVersionId;
309}
310
311int GetSdkApiVersion(void)
312{
313    static const char *sdkApiVersion = NULL;
314    GetProperty("const.ohos.apiversion", &sdkApiVersion);
315    if (sdkApiVersion == NULL) {
316        return 0;
317    }
318    return atoi(sdkApiVersion);
319}
320
321const char *GetSecurityPatchTag(void)
322{
323    static const char *securityPatchTag = NULL;
324    return GetProperty("const.ohos.version.security_patch", &securityPatchTag);
325}
326
327const char *GetBuildRootHash(void)
328{
329    static const char *buildRootHash = NULL;
330    return GetProperty("const.ohos.buildroothash", &buildRootHash);
331}
332
333int32_t GetIntParameter(const char *key, int32_t def)
334{
335    char value[MAX_INT_LEN] = {0};
336    uint32_t size = sizeof(value);
337    int ret = SystemGetParameter(key, value, &size);
338    if (ret != 0) {
339        return def;
340    }
341
342    long long int result = 0;
343    if (StringToLL(value, &result) != 0) {
344        return def;
345    }
346    if (result <= INT32_MIN || result >= INT32_MAX) {
347        return def;
348    }
349    return (int32_t)result;
350}
351
352uint32_t GetUintParameter(const char *key, uint32_t def)
353{
354    char value[MAX_INT_LEN] = {0};
355    uint32_t size = sizeof(value);
356    int ret = SystemGetParameter(key, value, &size);
357    if (ret != 0) {
358        return def;
359    }
360
361    unsigned long long int result = 0;
362    if (StringToULL(value, &result) != 0) {
363        return def;
364    }
365    if (result >= UINT32_MAX) {
366        return def;
367    }
368    return (uint32_t)result;
369}
370
371const char *GetDistributionOSName(void)
372{
373    static const char *distributionOsName = NULL;
374    GetProperty("const.product.os.dist.name", &distributionOsName);
375    if (distributionOsName == NULL) {
376        distributionOsName = EMPTY_STR;
377    }
378    return distributionOsName;
379}
380
381const char *GetDistributionOSVersion(void)
382{
383    static const char *distributionOsVersion = NULL;
384    GetProperty("const.product.os.dist.version", &distributionOsVersion);
385    if (distributionOsVersion == NULL) {
386        distributionOsVersion = GetOSFullName();
387    }
388    return distributionOsVersion;
389}
390
391int GetDistributionOSApiVersion(void)
392{
393    static const char *distributionOsApiVersion = NULL;
394    GetProperty("const.product.os.dist.apiversion", &distributionOsApiVersion);
395    if (distributionOsApiVersion == NULL) {
396        distributionOsApiVersion = GetSdkApiVersion_();
397    }
398    return atoi(distributionOsApiVersion);
399}
400const char *GetDistributionOSApiName(void)
401{
402    static const char *distributionOsApiName = NULL;
403    GetProperty("const.product.os.dist.apiname", &distributionOsApiName);
404    if (distributionOsApiName == NULL) {
405        distributionOsApiName = EMPTY_STR;
406    }
407    return distributionOsApiName;
408}
409
410const char *GetDistributionOSReleaseType(void)
411{
412    static const char *distributionOsReleaseType = NULL;
413    GetProperty("const.product.os.dist.releasetype", &distributionOsReleaseType);
414    if (distributionOsReleaseType == NULL) {
415        distributionOsReleaseType = GetOsReleaseType();
416    }
417    return distributionOsReleaseType;
418}
419