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
25INIT_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        BEGET_CHECK((ohosSerial = (char *)calloc(1, PARAM_VALUE_LEN_MAX)) != NULL, return NULL);
33    }
34    uint32_t len = PARAM_VALUE_LEN_MAX;
35    int ret = SystemGetParameter("ohos.boot.sn", ohosSerial, &len);
36    BEGET_CHECK(ret == 0, return NULL);
37    return ohosSerial;
38#endif
39}
40
41INIT_LOCAL_API int GetUdidFromParam(char *udid, uint32_t size)
42{
43    uint32_t len = size;
44    int ret = SystemGetParameter("const.product.udid", udid, &len);
45    BEGET_CHECK(ret != 0, return ret);
46
47    len = size;
48    ret = SystemGetParameter("const.product.devUdid", udid, &len);
49    BEGET_CHECK(ret != 0, return ret);
50    return ret;
51}
52
53INIT_LOCAL_API int GetDevUdid_(char *udid, int size)
54{
55    if (size < UDID_LEN || udid == NULL) {
56        return EC_FAILURE;
57    }
58
59    int ret = GetUdidFromParam(udid, (uint32_t)size);
60    BEGET_CHECK(ret != 0, return ret);
61
62#ifdef OHOS_LITE
63    ret = CalcDevUdid(udid, size);
64#endif
65    return ret;
66}