15ccb8f90Sopenharmony_ci/*
25ccb8f90Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
35ccb8f90Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45ccb8f90Sopenharmony_ci * you may not use this file except in compliance with the License.
55ccb8f90Sopenharmony_ci * You may obtain a copy of the License at
65ccb8f90Sopenharmony_ci *
75ccb8f90Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85ccb8f90Sopenharmony_ci *
95ccb8f90Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105ccb8f90Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115ccb8f90Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125ccb8f90Sopenharmony_ci * See the License for the specific language governing permissions and
135ccb8f90Sopenharmony_ci * limitations under the License.
145ccb8f90Sopenharmony_ci */
155ccb8f90Sopenharmony_ci
165ccb8f90Sopenharmony_ci#include "power_napi.h"
175ccb8f90Sopenharmony_ci
185ccb8f90Sopenharmony_ci#include "napi_errors.h"
195ccb8f90Sopenharmony_ci#include "napi_utils.h"
205ccb8f90Sopenharmony_ci#include "power_common.h"
215ccb8f90Sopenharmony_ci#include "power_log.h"
225ccb8f90Sopenharmony_ci#include "power_mgr_client.h"
235ccb8f90Sopenharmony_ci#include <fcntl.h>
245ccb8f90Sopenharmony_ci#include <sys/ioctl.h>
255ccb8f90Sopenharmony_ci#include <unistd.h>
265ccb8f90Sopenharmony_ci
275ccb8f90Sopenharmony_ci#define SHUT_STAGE_FRAMEWORK_START 1
285ccb8f90Sopenharmony_ci#define BOOT_DETECTOR_IOCTL_BASE 'B'
295ccb8f90Sopenharmony_ci#define SET_SHUT_STAGE _IOW(BOOT_DETECTOR_IOCTL_BASE, 106, int)
305ccb8f90Sopenharmony_ci#define SET_REBOOT _IOW(BOOT_DETECTOR_IOCTL_BASE, 109, int)
315ccb8f90Sopenharmony_ci
325ccb8f90Sopenharmony_cinamespace OHOS {
335ccb8f90Sopenharmony_cinamespace PowerMgr {
345ccb8f90Sopenharmony_cinamespace {
355ccb8f90Sopenharmony_ciconstexpr uint32_t REBOOT_SHUTDOWN_MAX_ARGC = 1;
365ccb8f90Sopenharmony_ciconstexpr uint32_t WAKEUP_MAX_ARGC = 1;
375ccb8f90Sopenharmony_ciconstexpr uint32_t SET_MODE_CALLBACK_MAX_ARGC = 2;
385ccb8f90Sopenharmony_ciconstexpr uint32_t SET_MODE_PROMISE_MAX_ARGC = 1;
395ccb8f90Sopenharmony_ciconstexpr uint32_t SUSPEND_MAX_ARGC = 1;
405ccb8f90Sopenharmony_ciconstexpr uint32_t SET_SCREEN_OFFTIME_ARGC = 1;
415ccb8f90Sopenharmony_ciconstexpr uint32_t HIBERNATE_ARGC = 1;
425ccb8f90Sopenharmony_ciconstexpr int32_t INDEX_0 = 0;
435ccb8f90Sopenharmony_ciconstexpr int32_t INDEX_1 = 1;
445ccb8f90Sopenharmony_ciconstexpr int32_t RESTORE_DEFAULT_SCREENOFF_TIME = -1;
455ccb8f90Sopenharmony_cistatic PowerMgrClient& g_powerMgrClient = PowerMgrClient::GetInstance();
465ccb8f90Sopenharmony_ci} // namespace
475ccb8f90Sopenharmony_cinapi_value PowerNapi::Shutdown(napi_env env, napi_callback_info info)
485ccb8f90Sopenharmony_ci{
495ccb8f90Sopenharmony_ci    return RebootOrShutdown(env, info, false);
505ccb8f90Sopenharmony_ci}
515ccb8f90Sopenharmony_ci
525ccb8f90Sopenharmony_cinapi_value PowerNapi::Reboot(napi_env env, napi_callback_info info)
535ccb8f90Sopenharmony_ci{
545ccb8f90Sopenharmony_ci    return RebootOrShutdown(env, info, true);
555ccb8f90Sopenharmony_ci}
565ccb8f90Sopenharmony_ci
575ccb8f90Sopenharmony_cinapi_value PowerNapi::IsActive(napi_env env, napi_callback_info info)
585ccb8f90Sopenharmony_ci{
595ccb8f90Sopenharmony_ci    bool isScreen = g_powerMgrClient.IsScreenOn();
605ccb8f90Sopenharmony_ci    napi_value napiValue;
615ccb8f90Sopenharmony_ci    NAPI_CALL(env, napi_get_boolean(env, isScreen, &napiValue));
625ccb8f90Sopenharmony_ci    return napiValue;
635ccb8f90Sopenharmony_ci}
645ccb8f90Sopenharmony_ci
655ccb8f90Sopenharmony_cinapi_value PowerNapi::Wakeup(napi_env env, napi_callback_info info)
665ccb8f90Sopenharmony_ci{
675ccb8f90Sopenharmony_ci    size_t argc = WAKEUP_MAX_ARGC;
685ccb8f90Sopenharmony_ci    napi_value argv[argc];
695ccb8f90Sopenharmony_ci    NapiUtils::GetCallbackInfo(env, info, argc, argv);
705ccb8f90Sopenharmony_ci
715ccb8f90Sopenharmony_ci    NapiErrors error;
725ccb8f90Sopenharmony_ci    if (argc != WAKEUP_MAX_ARGC || !NapiUtils::CheckValueType(env, argv[INDEX_0], napi_string)) {
735ccb8f90Sopenharmony_ci        return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
745ccb8f90Sopenharmony_ci    }
755ccb8f90Sopenharmony_ci
765ccb8f90Sopenharmony_ci    std::string detail = NapiUtils::GetStringFromNapi(env, argv[INDEX_0]);
775ccb8f90Sopenharmony_ci    POWER_HILOGD(FEATURE_WAKEUP, "Wakeup type: APPLICATION, reason: %{public}s", detail.c_str());
785ccb8f90Sopenharmony_ci    PowerErrors code = g_powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, detail);
795ccb8f90Sopenharmony_ci    if (code != PowerErrors::ERR_OK) {
805ccb8f90Sopenharmony_ci        error.ThrowError(env, code);
815ccb8f90Sopenharmony_ci    }
825ccb8f90Sopenharmony_ci    return nullptr;
835ccb8f90Sopenharmony_ci}
845ccb8f90Sopenharmony_ci
855ccb8f90Sopenharmony_cinapi_value PowerNapi::Suspend(napi_env env, napi_callback_info info)
865ccb8f90Sopenharmony_ci{
875ccb8f90Sopenharmony_ci    size_t argc = SUSPEND_MAX_ARGC;
885ccb8f90Sopenharmony_ci    napi_value argv[argc];
895ccb8f90Sopenharmony_ci    NapiUtils::GetCallbackInfo(env, info, argc, argv);
905ccb8f90Sopenharmony_ci
915ccb8f90Sopenharmony_ci    NapiErrors error;
925ccb8f90Sopenharmony_ci    if (argc != SUSPEND_MAX_ARGC || !NapiUtils::CheckValueType(env, argv[INDEX_0], napi_boolean)) {
935ccb8f90Sopenharmony_ci        if (!NapiUtils::CheckValueType(env, argv[INDEX_0], napi_undefined)) {
945ccb8f90Sopenharmony_ci            std::string detail = NapiUtils::GetStringFromNapi(env, argv[INDEX_0]);
955ccb8f90Sopenharmony_ci            if (!detail.empty()) {
965ccb8f90Sopenharmony_ci                return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
975ccb8f90Sopenharmony_ci            }
985ccb8f90Sopenharmony_ci        }
995ccb8f90Sopenharmony_ci    }
1005ccb8f90Sopenharmony_ci
1015ccb8f90Sopenharmony_ci    bool isForce = false;
1025ccb8f90Sopenharmony_ci    napi_get_value_bool(env, argv[0], &isForce);
1035ccb8f90Sopenharmony_ci
1045ccb8f90Sopenharmony_ci    PowerErrors code;
1055ccb8f90Sopenharmony_ci    if (isForce) {
1065ccb8f90Sopenharmony_ci        code = g_powerMgrClient.ForceSuspendDevice();
1075ccb8f90Sopenharmony_ci    } else {
1085ccb8f90Sopenharmony_ci        code = g_powerMgrClient.SuspendDevice();
1095ccb8f90Sopenharmony_ci    }
1105ccb8f90Sopenharmony_ci    if (code != PowerErrors::ERR_OK) {
1115ccb8f90Sopenharmony_ci        POWER_HILOGE(FEATURE_WAKEUP, "Suspend Device fail, isForce:%{public}d", isForce);
1125ccb8f90Sopenharmony_ci        return error.ThrowError(env, code);
1135ccb8f90Sopenharmony_ci    }
1145ccb8f90Sopenharmony_ci    return nullptr;
1155ccb8f90Sopenharmony_ci}
1165ccb8f90Sopenharmony_ci
1175ccb8f90Sopenharmony_cinapi_value PowerNapi::Hibernate(napi_env env, napi_callback_info info)
1185ccb8f90Sopenharmony_ci{
1195ccb8f90Sopenharmony_ci    size_t argc = HIBERNATE_ARGC;
1205ccb8f90Sopenharmony_ci    napi_value argv[argc];
1215ccb8f90Sopenharmony_ci    NapiUtils::GetCallbackInfo(env, info, argc, argv);
1225ccb8f90Sopenharmony_ci
1235ccb8f90Sopenharmony_ci    NapiErrors error;
1245ccb8f90Sopenharmony_ci    if (argc != HIBERNATE_ARGC || !NapiUtils::CheckValueType(env, argv[INDEX_0], napi_boolean)) {
1255ccb8f90Sopenharmony_ci        if (!NapiUtils::CheckValueType(env, argv[INDEX_0], napi_undefined)) {
1265ccb8f90Sopenharmony_ci            std::string detail = NapiUtils::GetStringFromNapi(env, argv[INDEX_0]);
1275ccb8f90Sopenharmony_ci            if (!detail.empty()) {
1285ccb8f90Sopenharmony_ci                return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
1295ccb8f90Sopenharmony_ci            }
1305ccb8f90Sopenharmony_ci        }
1315ccb8f90Sopenharmony_ci    }
1325ccb8f90Sopenharmony_ci
1335ccb8f90Sopenharmony_ci    bool clearMemory = false;
1345ccb8f90Sopenharmony_ci    napi_get_value_bool(env, argv[0], &clearMemory);
1355ccb8f90Sopenharmony_ci
1365ccb8f90Sopenharmony_ci    PowerErrors code = g_powerMgrClient.Hibernate(clearMemory);
1375ccb8f90Sopenharmony_ci    if (code != PowerErrors::ERR_OK) {
1385ccb8f90Sopenharmony_ci        POWER_HILOGE(FEATURE_WAKEUP, "Hibernate failed.");
1395ccb8f90Sopenharmony_ci        error.ThrowError(env, code);
1405ccb8f90Sopenharmony_ci    }
1415ccb8f90Sopenharmony_ci    return nullptr;
1425ccb8f90Sopenharmony_ci}
1435ccb8f90Sopenharmony_ci
1445ccb8f90Sopenharmony_cinapi_value PowerNapi::SetPowerMode(napi_env env, napi_callback_info info)
1455ccb8f90Sopenharmony_ci{
1465ccb8f90Sopenharmony_ci    size_t argc = SET_MODE_CALLBACK_MAX_ARGC;
1475ccb8f90Sopenharmony_ci    napi_value argv[argc];
1485ccb8f90Sopenharmony_ci    NapiUtils::GetCallbackInfo(env, info, argc, argv);
1495ccb8f90Sopenharmony_ci
1505ccb8f90Sopenharmony_ci    NapiErrors error;
1515ccb8f90Sopenharmony_ci    if (argc != SET_MODE_CALLBACK_MAX_ARGC && argc != SET_MODE_PROMISE_MAX_ARGC) {
1525ccb8f90Sopenharmony_ci        return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
1535ccb8f90Sopenharmony_ci    }
1545ccb8f90Sopenharmony_ci
1555ccb8f90Sopenharmony_ci    std::unique_ptr<AsyncCallbackInfo> asyncInfo = std::make_unique<AsyncCallbackInfo>();
1565ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(asyncInfo == nullptr, nullptr);
1575ccb8f90Sopenharmony_ci    // callback
1585ccb8f90Sopenharmony_ci    if (argc == SET_MODE_CALLBACK_MAX_ARGC) {
1595ccb8f90Sopenharmony_ci        POWER_HILOGD(FEATURE_POWER_MODE, "Call setPowerMode callback");
1605ccb8f90Sopenharmony_ci        return SetPowerModeCallback(env, argv, asyncInfo);
1615ccb8f90Sopenharmony_ci    }
1625ccb8f90Sopenharmony_ci
1635ccb8f90Sopenharmony_ci    // promise
1645ccb8f90Sopenharmony_ci    POWER_HILOGD(FEATURE_POWER_MODE, "Call setPowerMode promise");
1655ccb8f90Sopenharmony_ci    return SetPowerModePromise(env, argv, asyncInfo);
1665ccb8f90Sopenharmony_ci}
1675ccb8f90Sopenharmony_ci
1685ccb8f90Sopenharmony_cinapi_value PowerNapi::GetPowerMode(napi_env env, napi_callback_info info)
1695ccb8f90Sopenharmony_ci{
1705ccb8f90Sopenharmony_ci    PowerMode mode = g_powerMgrClient.GetDeviceMode();
1715ccb8f90Sopenharmony_ci    napi_value napiValue;
1725ccb8f90Sopenharmony_ci    NAPI_CALL(env, napi_create_uint32(env, static_cast<uint32_t>(mode), &napiValue));
1735ccb8f90Sopenharmony_ci    return napiValue;
1745ccb8f90Sopenharmony_ci}
1755ccb8f90Sopenharmony_ci
1765ccb8f90Sopenharmony_cistatic void SetFrameworkBootStage(bool isReboot)
1775ccb8f90Sopenharmony_ci{
1785ccb8f90Sopenharmony_ci    int fd = open("/dev/bbox", O_WRONLY);
1795ccb8f90Sopenharmony_ci    if (fd < 0) {
1805ccb8f90Sopenharmony_ci        POWER_HILOGE(FEATURE_SHUTDOWN, "open /dev/bbox failed!");
1815ccb8f90Sopenharmony_ci        return;
1825ccb8f90Sopenharmony_ci    }
1835ccb8f90Sopenharmony_ci    int rebootFlag = isReboot ? 1 : 0;
1845ccb8f90Sopenharmony_ci    int ret = ioctl(fd, SET_REBOOT, &rebootFlag);
1855ccb8f90Sopenharmony_ci    if (ret < 0) {
1865ccb8f90Sopenharmony_ci        POWER_HILOGE(FEATURE_SHUTDOWN, "set reboot flag failed!");
1875ccb8f90Sopenharmony_ci        close(fd);
1885ccb8f90Sopenharmony_ci        return;
1895ccb8f90Sopenharmony_ci    }
1905ccb8f90Sopenharmony_ci    int stage = SHUT_STAGE_FRAMEWORK_START;
1915ccb8f90Sopenharmony_ci    ret = ioctl(fd, SET_SHUT_STAGE, &stage);
1925ccb8f90Sopenharmony_ci    if (ret < 0) {
1935ccb8f90Sopenharmony_ci        POWER_HILOGE(FEATURE_SHUTDOWN, "set shut stage failed!");
1945ccb8f90Sopenharmony_ci    }
1955ccb8f90Sopenharmony_ci    close(fd);
1965ccb8f90Sopenharmony_ci    return;
1975ccb8f90Sopenharmony_ci}
1985ccb8f90Sopenharmony_ci
1995ccb8f90Sopenharmony_cinapi_value PowerNapi::RebootOrShutdown(napi_env env, napi_callback_info info, bool isReboot)
2005ccb8f90Sopenharmony_ci{
2015ccb8f90Sopenharmony_ci    size_t argc = REBOOT_SHUTDOWN_MAX_ARGC;
2025ccb8f90Sopenharmony_ci    napi_value argv[argc];
2035ccb8f90Sopenharmony_ci    SetFrameworkBootStage(isReboot);
2045ccb8f90Sopenharmony_ci    NapiUtils::GetCallbackInfo(env, info, argc, argv);
2055ccb8f90Sopenharmony_ci
2065ccb8f90Sopenharmony_ci    NapiErrors error;
2075ccb8f90Sopenharmony_ci    if (argc != REBOOT_SHUTDOWN_MAX_ARGC || !NapiUtils::CheckValueType(env, argv[INDEX_0], napi_string)) {
2085ccb8f90Sopenharmony_ci        return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
2095ccb8f90Sopenharmony_ci    }
2105ccb8f90Sopenharmony_ci
2115ccb8f90Sopenharmony_ci    std::string reason = NapiUtils::GetStringFromNapi(env, argv[INDEX_0]);
2125ccb8f90Sopenharmony_ci    POWER_HILOGD(FEATURE_SHUTDOWN, "reboot: %{public}d, reason: %{public}s", isReboot, reason.c_str());
2135ccb8f90Sopenharmony_ci
2145ccb8f90Sopenharmony_ci    PowerErrors code;
2155ccb8f90Sopenharmony_ci    if (isReboot) {
2165ccb8f90Sopenharmony_ci        code = g_powerMgrClient.RebootDevice(reason);
2175ccb8f90Sopenharmony_ci    } else {
2185ccb8f90Sopenharmony_ci        code = g_powerMgrClient.ShutDownDevice(reason);
2195ccb8f90Sopenharmony_ci    }
2205ccb8f90Sopenharmony_ci    if (code != PowerErrors::ERR_OK) {
2215ccb8f90Sopenharmony_ci        error.ThrowError(env, code);
2225ccb8f90Sopenharmony_ci    }
2235ccb8f90Sopenharmony_ci
2245ccb8f90Sopenharmony_ci    return nullptr;
2255ccb8f90Sopenharmony_ci}
2265ccb8f90Sopenharmony_ci
2275ccb8f90Sopenharmony_cinapi_value PowerNapi::SetPowerModeCallback(
2285ccb8f90Sopenharmony_ci    napi_env& env, napi_value argv[], std::unique_ptr<AsyncCallbackInfo>& asyncInfo)
2295ccb8f90Sopenharmony_ci{
2305ccb8f90Sopenharmony_ci    bool isNum = NapiUtils::CheckValueType(env, argv[INDEX_0], napi_number);
2315ccb8f90Sopenharmony_ci    bool isFunc = NapiUtils::CheckValueType(env, argv[INDEX_1], napi_function);
2325ccb8f90Sopenharmony_ci    if (!isNum || !isFunc) {
2335ccb8f90Sopenharmony_ci        POWER_HILOGW(FEATURE_POWER_MODE, "isNum: %{public}d, isFunc: %{public}d", isNum, isFunc);
2345ccb8f90Sopenharmony_ci        return asyncInfo->GetError().ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
2355ccb8f90Sopenharmony_ci    }
2365ccb8f90Sopenharmony_ci
2375ccb8f90Sopenharmony_ci    asyncInfo->GetData().SetMode(env, argv[INDEX_0]);
2385ccb8f90Sopenharmony_ci    asyncInfo->CreateCallback(env, argv[INDEX_1]);
2395ccb8f90Sopenharmony_ci
2405ccb8f90Sopenharmony_ci    AsyncWork(
2415ccb8f90Sopenharmony_ci        env, asyncInfo, "SetPowerModeCallback",
2425ccb8f90Sopenharmony_ci        [](napi_env env, void* data) {
2435ccb8f90Sopenharmony_ci            AsyncCallbackInfo* asyncInfo = reinterpret_cast<AsyncCallbackInfo*>(data);
2445ccb8f90Sopenharmony_ci            RETURN_IF(asyncInfo == nullptr);
2455ccb8f90Sopenharmony_ci            PowerErrors error = g_powerMgrClient.SetDeviceMode(asyncInfo->GetData().GetMode());
2465ccb8f90Sopenharmony_ci            asyncInfo->GetError().Error(error);
2475ccb8f90Sopenharmony_ci        },
2485ccb8f90Sopenharmony_ci        [](napi_env env, napi_status status, void* data) {
2495ccb8f90Sopenharmony_ci            AsyncCallbackInfo* asyncInfo = reinterpret_cast<AsyncCallbackInfo*>(data);
2505ccb8f90Sopenharmony_ci            RETURN_IF(asyncInfo == nullptr);
2515ccb8f90Sopenharmony_ci            asyncInfo->CallFunction(env, nullptr);
2525ccb8f90Sopenharmony_ci            asyncInfo->Release(env);
2535ccb8f90Sopenharmony_ci            delete asyncInfo;
2545ccb8f90Sopenharmony_ci        });
2555ccb8f90Sopenharmony_ci    return nullptr;
2565ccb8f90Sopenharmony_ci}
2575ccb8f90Sopenharmony_ci
2585ccb8f90Sopenharmony_cinapi_value PowerNapi::SetPowerModePromise(
2595ccb8f90Sopenharmony_ci    napi_env& env, napi_value argv[], std::unique_ptr<AsyncCallbackInfo>& asyncInfo)
2605ccb8f90Sopenharmony_ci{
2615ccb8f90Sopenharmony_ci    bool isNum = NapiUtils::CheckValueType(env, argv[INDEX_0], napi_number);
2625ccb8f90Sopenharmony_ci    if (!isNum) {
2635ccb8f90Sopenharmony_ci        POWER_HILOGW(FEATURE_POWER_MODE, "isNum: %{public}d", isNum);
2645ccb8f90Sopenharmony_ci        return asyncInfo->GetError().ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
2655ccb8f90Sopenharmony_ci    }
2665ccb8f90Sopenharmony_ci    napi_value promise;
2675ccb8f90Sopenharmony_ci    asyncInfo->CreatePromise(env, promise);
2685ccb8f90Sopenharmony_ci    RETURN_IF_WITH_RET(promise == nullptr, nullptr);
2695ccb8f90Sopenharmony_ci    asyncInfo->GetData().SetMode(env, argv[INDEX_0]);
2705ccb8f90Sopenharmony_ci
2715ccb8f90Sopenharmony_ci    AsyncWork(
2725ccb8f90Sopenharmony_ci        env, asyncInfo, "SetPowerModePromise",
2735ccb8f90Sopenharmony_ci        [](napi_env env, void* data) {
2745ccb8f90Sopenharmony_ci            AsyncCallbackInfo* asyncInfo = reinterpret_cast<AsyncCallbackInfo*>(data);
2755ccb8f90Sopenharmony_ci            RETURN_IF(asyncInfo == nullptr);
2765ccb8f90Sopenharmony_ci            PowerErrors error = g_powerMgrClient.SetDeviceMode(asyncInfo->GetData().GetMode());
2775ccb8f90Sopenharmony_ci            asyncInfo->GetError().Error(error);
2785ccb8f90Sopenharmony_ci        },
2795ccb8f90Sopenharmony_ci        [](napi_env env, napi_status status, void* data) {
2805ccb8f90Sopenharmony_ci            AsyncCallbackInfo* asyncInfo = reinterpret_cast<AsyncCallbackInfo*>(data);
2815ccb8f90Sopenharmony_ci            RETURN_IF(asyncInfo == nullptr);
2825ccb8f90Sopenharmony_ci            if (asyncInfo->GetError().IsError()) {
2835ccb8f90Sopenharmony_ci                napi_reject_deferred(env, asyncInfo->GetDeferred(), asyncInfo->GetError().GetNapiError(env));
2845ccb8f90Sopenharmony_ci            } else {
2855ccb8f90Sopenharmony_ci                napi_value undefined;
2865ccb8f90Sopenharmony_ci                napi_get_undefined(env, &undefined);
2875ccb8f90Sopenharmony_ci                napi_resolve_deferred(env, asyncInfo->GetDeferred(), undefined);
2885ccb8f90Sopenharmony_ci            }
2895ccb8f90Sopenharmony_ci            asyncInfo->Release(env);
2905ccb8f90Sopenharmony_ci            delete asyncInfo;
2915ccb8f90Sopenharmony_ci        });
2925ccb8f90Sopenharmony_ci    return promise;
2935ccb8f90Sopenharmony_ci}
2945ccb8f90Sopenharmony_ci
2955ccb8f90Sopenharmony_civoid PowerNapi::AsyncWork(napi_env& env, std::unique_ptr<AsyncCallbackInfo>& asyncInfo, const std::string& resourceName,
2965ccb8f90Sopenharmony_ci    napi_async_execute_callback execute, napi_async_complete_callback complete)
2975ccb8f90Sopenharmony_ci{
2985ccb8f90Sopenharmony_ci    napi_value resource = nullptr;
2995ccb8f90Sopenharmony_ci    napi_create_string_utf8(env, resourceName.c_str(), NAPI_AUTO_LENGTH, &resource);
3005ccb8f90Sopenharmony_ci    napi_create_async_work(env, nullptr, resource, execute, complete,
3015ccb8f90Sopenharmony_ci        reinterpret_cast<void*>(asyncInfo.get()), &(asyncInfo->GetAsyncWork()));
3025ccb8f90Sopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_queue_async_work_with_qos(env, asyncInfo->GetAsyncWork(), napi_qos_utility));
3035ccb8f90Sopenharmony_ci    asyncInfo.release();
3045ccb8f90Sopenharmony_ci}
3055ccb8f90Sopenharmony_ci
3065ccb8f90Sopenharmony_cinapi_value PowerNapi::SetScreenOffTime(napi_env env, napi_callback_info info)
3075ccb8f90Sopenharmony_ci{
3085ccb8f90Sopenharmony_ci    size_t argc = SET_SCREEN_OFFTIME_ARGC;
3095ccb8f90Sopenharmony_ci    napi_value argv[argc];
3105ccb8f90Sopenharmony_ci    NapiUtils::GetCallbackInfo(env, info, argc, argv);
3115ccb8f90Sopenharmony_ci
3125ccb8f90Sopenharmony_ci    NapiErrors error;
3135ccb8f90Sopenharmony_ci    if (argc != SET_SCREEN_OFFTIME_ARGC || !NapiUtils::CheckValueType(env, argv[INDEX_0], napi_number)) {
3145ccb8f90Sopenharmony_ci        POWER_HILOGE(FEATURE_WAKEUP, "check value type failed.");
3155ccb8f90Sopenharmony_ci        return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
3165ccb8f90Sopenharmony_ci    }
3175ccb8f90Sopenharmony_ci
3185ccb8f90Sopenharmony_ci    int64_t timeout;
3195ccb8f90Sopenharmony_ci    if (napi_ok != napi_get_value_int64(env, argv[INDEX_0], &timeout)) {
3205ccb8f90Sopenharmony_ci        POWER_HILOGE(FEATURE_WAKEUP, "napi get int64 value failed.");
3215ccb8f90Sopenharmony_ci        return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
3225ccb8f90Sopenharmony_ci    }
3235ccb8f90Sopenharmony_ci
3245ccb8f90Sopenharmony_ci    if (timeout == 0 || (timeout < 0 && timeout != RESTORE_DEFAULT_SCREENOFF_TIME)) {
3255ccb8f90Sopenharmony_ci        POWER_HILOGE(FEATURE_WAKEUP, "timeout is not right.");
3265ccb8f90Sopenharmony_ci        return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
3275ccb8f90Sopenharmony_ci    }
3285ccb8f90Sopenharmony_ci
3295ccb8f90Sopenharmony_ci    PowerErrors code;
3305ccb8f90Sopenharmony_ci    if (timeout == RESTORE_DEFAULT_SCREENOFF_TIME) {
3315ccb8f90Sopenharmony_ci        code = g_powerMgrClient.RestoreScreenOffTime();
3325ccb8f90Sopenharmony_ci    } else {
3335ccb8f90Sopenharmony_ci        code = g_powerMgrClient.OverrideScreenOffTime(timeout);
3345ccb8f90Sopenharmony_ci    }
3355ccb8f90Sopenharmony_ci    if (code != PowerErrors::ERR_OK) {
3365ccb8f90Sopenharmony_ci        POWER_HILOGE(FEATURE_WAKEUP, "SetScreenOffTime failed.");
3375ccb8f90Sopenharmony_ci        return error.ThrowError(env, code);
3385ccb8f90Sopenharmony_ci    }
3395ccb8f90Sopenharmony_ci    return nullptr;
3405ccb8f90Sopenharmony_ci}
3415ccb8f90Sopenharmony_ci
3425ccb8f90Sopenharmony_cinapi_value PowerNapi::IsStandby(napi_env env, napi_callback_info info)
3435ccb8f90Sopenharmony_ci{
3445ccb8f90Sopenharmony_ci    bool isStandby = false;
3455ccb8f90Sopenharmony_ci    PowerErrors code = g_powerMgrClient.IsStandby(isStandby);
3465ccb8f90Sopenharmony_ci    if (code == PowerErrors::ERR_OK) {
3475ccb8f90Sopenharmony_ci        napi_value napiValue;
3485ccb8f90Sopenharmony_ci        NAPI_CALL(env, napi_get_boolean(env, isStandby, &napiValue));
3495ccb8f90Sopenharmony_ci        return napiValue;
3505ccb8f90Sopenharmony_ci    }
3515ccb8f90Sopenharmony_ci    NapiErrors error;
3525ccb8f90Sopenharmony_ci    return error.ThrowError(env, code);
3535ccb8f90Sopenharmony_ci}
3545ccb8f90Sopenharmony_ci} // namespace PowerMgr
3555ccb8f90Sopenharmony_ci} // namespace OHOS
356