• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
76 
GetDiskSN_(char * diskSN,int size)77 INIT_LOCAL_API int GetDiskSN_(char *diskSN, int size)
78 {
79     if (size < DISK_SN_LEN || diskSN == NULL) {
80         return EC_FAILURE;
81     }
82     char diskSNPath[DISK_SN_PATH_LEN] = {0};
83     uint32_t diskSNPathLen = DISK_SN_PATH_LEN;
84     if (SystemGetParameter("const.disk.sn.filepath", diskSNPath, &diskSNPathLen) != 0) {
85         BEGET_LOGE("const.disk.sn.filepath read failed");
86         return EC_FAILURE;
87     }
88     FILE *file = fopen(diskSNPath, "r");
89     if (file == NULL) {
90         BEGET_LOGE("GetDiskSN_ open file failed");
91         return EC_FAILURE;
92     }
93     if (fscanf_s(file, "%s", diskSN, size) <= 0) {
94         BEGET_LOGE("GetDiskSN_ read file failed");
95         if (fclose(file) != 0) {
96             BEGET_LOGE("GetDiskSN_ close file failed");
97         }
98         return EC_FAILURE;
99     }
100     if (fclose(file) != 0) {
101         BEGET_LOGE("GetDiskSN_ close file failed");
102     }
103     return 0;
104 }