1f16e0440Sopenharmony_ci/*
2f16e0440Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3f16e0440Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f16e0440Sopenharmony_ci * you may not use this file except in compliance with the License.
5f16e0440Sopenharmony_ci * You may obtain a copy of the License at
6f16e0440Sopenharmony_ci *
7f16e0440Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f16e0440Sopenharmony_ci *
9f16e0440Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f16e0440Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f16e0440Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f16e0440Sopenharmony_ci * See the License for the specific language governing permissions and
13f16e0440Sopenharmony_ci * limitations under the License.
14f16e0440Sopenharmony_ci */
15f16e0440Sopenharmony_ci
16f16e0440Sopenharmony_ci#include "napi_utils.h"
17f16e0440Sopenharmony_ci#include "battery_log.h"
18f16e0440Sopenharmony_ci
19f16e0440Sopenharmony_cinamespace OHOS {
20f16e0440Sopenharmony_cinamespace PowerMgr {
21f16e0440Sopenharmony_cinamespace {
22f16e0440Sopenharmony_ciconstexpr uint32_t MAX_SIZE = 255;
23f16e0440Sopenharmony_ci}
24f16e0440Sopenharmony_cinapi_value NapiUtils::GetCallbackInfo(napi_env& env, napi_callback_info& info, size_t& argc, napi_value argv[])
25f16e0440Sopenharmony_ci{
26f16e0440Sopenharmony_ci    napi_value thisVar = nullptr;
27f16e0440Sopenharmony_ci    void* data = nullptr;
28f16e0440Sopenharmony_ci    if (napi_ok != napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)) {
29f16e0440Sopenharmony_ci        BATTERY_HILOGW(FEATURE_BATT_INFO, "Failed to get the input parameter");
30f16e0440Sopenharmony_ci    }
31f16e0440Sopenharmony_ci    return thisVar;
32f16e0440Sopenharmony_ci}
33f16e0440Sopenharmony_ci
34f16e0440Sopenharmony_cistd::string NapiUtils::GetStringFromNapi(napi_env& env, napi_value& napiStr)
35f16e0440Sopenharmony_ci{
36f16e0440Sopenharmony_ci    char str[MAX_SIZE] = {0};
37f16e0440Sopenharmony_ci    size_t strLen;
38f16e0440Sopenharmony_ci    if (napi_ok != napi_get_value_string_utf8(env, napiStr, str, MAX_SIZE - 1, &strLen)) {
39f16e0440Sopenharmony_ci        BATTERY_HILOGW(FEATURE_BATT_INFO, "napi_get_value_string_utf8 failed");
40f16e0440Sopenharmony_ci        return "";
41f16e0440Sopenharmony_ci    }
42f16e0440Sopenharmony_ci    return std::string(str);
43f16e0440Sopenharmony_ci}
44f16e0440Sopenharmony_ci
45f16e0440Sopenharmony_cinapi_ref NapiUtils::CreateReference(napi_env& env, napi_value& value)
46f16e0440Sopenharmony_ci{
47f16e0440Sopenharmony_ci    napi_ref refVal = nullptr;
48f16e0440Sopenharmony_ci    if (napi_ok != napi_create_reference(env, value, 1, &refVal)) {
49f16e0440Sopenharmony_ci        BATTERY_HILOGW(FEATURE_BATT_INFO, "Failed to create a value reference");
50f16e0440Sopenharmony_ci        return refVal;
51f16e0440Sopenharmony_ci    }
52f16e0440Sopenharmony_ci    return refVal;
53f16e0440Sopenharmony_ci}
54f16e0440Sopenharmony_ci
55f16e0440Sopenharmony_civoid NapiUtils::ReleaseReference(napi_env& env, napi_ref& ref)
56f16e0440Sopenharmony_ci{
57f16e0440Sopenharmony_ci    if (ref != nullptr) {
58f16e0440Sopenharmony_ci        napi_delete_reference(env, ref);
59f16e0440Sopenharmony_ci        ref = nullptr;
60f16e0440Sopenharmony_ci    }
61f16e0440Sopenharmony_ci}
62f16e0440Sopenharmony_ci
63f16e0440Sopenharmony_cibool NapiUtils::CheckValueType(napi_env& env, napi_value& value, napi_valuetype checkType)
64f16e0440Sopenharmony_ci{
65f16e0440Sopenharmony_ci    napi_valuetype valueType = napi_undefined;
66f16e0440Sopenharmony_ci    napi_typeof(env, value, &valueType);
67f16e0440Sopenharmony_ci    if (valueType != checkType) {
68f16e0440Sopenharmony_ci        BATTERY_HILOGW(FEATURE_BATT_INFO, "Parameter type error");
69f16e0440Sopenharmony_ci        return false;
70f16e0440Sopenharmony_ci    }
71f16e0440Sopenharmony_ci    return true;
72f16e0440Sopenharmony_ci}
73f16e0440Sopenharmony_ci} // namespace PowerMgr
74f16e0440Sopenharmony_ci} // namespace OHOS
75