1 /*
2  * Copyright (c) 2023 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 "udid.h"
16 
17 #ifdef OHOS_LITE
18 #include "hal_sys_param.h"
19 #endif
20 #include "init_param.h"
21 #include "param_comm.h"
22 #include "securec.h"
23 #include "sysparam_errno.h"
24 
GetSerial_(void)25 INIT_LOCAL_API const char *GetSerial_(void)
26 {
27 #ifdef OHOS_LITE
28     return HalGetSerial();
29 #else
30     static char *ohosSerial = NULL;
31     if (ohosSerial != NULL) {
32         return ohosSerial;
33     }
34 
35     char *value;
36     BEGET_CHECK((value = (char *)calloc(1, PARAM_VALUE_LEN_MAX)) != NULL, return NULL);
37     uint32_t len = PARAM_VALUE_LEN_MAX;
38     int ret = SystemGetParameter("ohos.boot.sn", value, &len);
39     BEGET_CHECK(ret == 0, free(value);
40         return NULL);
41     if (ohosSerial != NULL) {
42         free(value);
43         return ohosSerial;
44     }
45     ohosSerial = value;
46     return ohosSerial;
47 #endif
48 }
49 
GetUdidFromParam(char * udid,uint32_t size)50 INIT_LOCAL_API int GetUdidFromParam(char *udid, uint32_t size)
51 {
52     uint32_t len = size;
53     int ret = SystemGetParameter("const.product.udid", udid, &len);
54     BEGET_CHECK(ret != 0, return ret);
55 
56     len = size;
57     ret = SystemGetParameter("const.product.devUdid", udid, &len);
58     BEGET_CHECK(ret != 0, return ret);
59     return ret;
60 }
61 
GetDevUdid_(char * udid,int size)62 INIT_LOCAL_API int GetDevUdid_(char *udid, int size)
63 {
64     if (size < UDID_LEN || udid == NULL) {
65         return EC_FAILURE;
66     }
67 
68     int ret = GetUdidFromParam(udid, (uint32_t)size);
69     BEGET_CHECK(ret != 0, return ret);
70 
71 #ifdef OHOS_LITE
72     ret = CalcDevUdid(udid, size);
73 #endif
74     return ret;
75 }