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 "napi/native_api.h" 175ccb8f90Sopenharmony_ci#include "napi/native_common.h" 185ccb8f90Sopenharmony_ci#include "napi/native_node_api.h" 195ccb8f90Sopenharmony_ci 205ccb8f90Sopenharmony_ci#include "power.h" 215ccb8f90Sopenharmony_ci#include "power_log.h" 225ccb8f90Sopenharmony_ci#include "power_mode_info.h" 235ccb8f90Sopenharmony_ci#include "power_napi.h" 245ccb8f90Sopenharmony_ci 255ccb8f90Sopenharmony_ciusing namespace OHOS::PowerMgr; 265ccb8f90Sopenharmony_ci 275ccb8f90Sopenharmony_cistatic napi_value EnumPowerModeClassConstructor(napi_env env, napi_callback_info info) 285ccb8f90Sopenharmony_ci{ 295ccb8f90Sopenharmony_ci napi_value thisArg = nullptr; 305ccb8f90Sopenharmony_ci void* data = nullptr; 315ccb8f90Sopenharmony_ci 325ccb8f90Sopenharmony_ci napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data); 335ccb8f90Sopenharmony_ci 345ccb8f90Sopenharmony_ci napi_value global = nullptr; 355ccb8f90Sopenharmony_ci napi_get_global(env, &global); 365ccb8f90Sopenharmony_ci 375ccb8f90Sopenharmony_ci return thisArg; 385ccb8f90Sopenharmony_ci} 395ccb8f90Sopenharmony_ci 405ccb8f90Sopenharmony_cistatic napi_value CreateDevicePowerMode(napi_env env, napi_value exports) 415ccb8f90Sopenharmony_ci{ 425ccb8f90Sopenharmony_ci napi_value normal = nullptr; 435ccb8f90Sopenharmony_ci napi_value powerSave = nullptr; 445ccb8f90Sopenharmony_ci napi_value performance = nullptr; 455ccb8f90Sopenharmony_ci napi_value extremePowerSave = nullptr; 465ccb8f90Sopenharmony_ci 475ccb8f90Sopenharmony_ci napi_create_int32(env, (int32_t)PowerMode::NORMAL_MODE, &normal); 485ccb8f90Sopenharmony_ci napi_create_int32(env, (int32_t)PowerMode::POWER_SAVE_MODE, &powerSave); 495ccb8f90Sopenharmony_ci napi_create_int32(env, (int32_t)PowerMode::PERFORMANCE_MODE, &performance); 505ccb8f90Sopenharmony_ci napi_create_int32(env, (int32_t)PowerMode::EXTREME_POWER_SAVE_MODE, &extremePowerSave); 515ccb8f90Sopenharmony_ci 525ccb8f90Sopenharmony_ci napi_property_descriptor desc[] = { 535ccb8f90Sopenharmony_ci DECLARE_NAPI_STATIC_PROPERTY("MODE_NORMAL", normal), 545ccb8f90Sopenharmony_ci DECLARE_NAPI_STATIC_PROPERTY("MODE_POWER_SAVE", powerSave), 555ccb8f90Sopenharmony_ci DECLARE_NAPI_STATIC_PROPERTY("MODE_PERFORMANCE", performance), 565ccb8f90Sopenharmony_ci DECLARE_NAPI_STATIC_PROPERTY("MODE_EXTREME_POWER_SAVE", extremePowerSave), 575ccb8f90Sopenharmony_ci }; 585ccb8f90Sopenharmony_ci napi_value result = nullptr; 595ccb8f90Sopenharmony_ci napi_define_class(env, "DevicePowerMode", NAPI_AUTO_LENGTH, EnumPowerModeClassConstructor, nullptr, 605ccb8f90Sopenharmony_ci sizeof(desc) / sizeof(*desc), desc, &result); 615ccb8f90Sopenharmony_ci 625ccb8f90Sopenharmony_ci napi_set_named_property(env, exports, "DevicePowerMode", result); 635ccb8f90Sopenharmony_ci 645ccb8f90Sopenharmony_ci return exports; 655ccb8f90Sopenharmony_ci} 665ccb8f90Sopenharmony_ci 675ccb8f90Sopenharmony_ciEXTERN_C_START 685ccb8f90Sopenharmony_ci/* 695ccb8f90Sopenharmony_ci * function for module exports 705ccb8f90Sopenharmony_ci */ 715ccb8f90Sopenharmony_cistatic napi_value PowerInit(napi_env env, napi_value exports) 725ccb8f90Sopenharmony_ci{ 735ccb8f90Sopenharmony_ci POWER_HILOGD(COMP_FWK, "Initialize the Power module"); 745ccb8f90Sopenharmony_ci napi_property_descriptor desc[] = { 755ccb8f90Sopenharmony_ci // Old interfaces deprecated since 9 765ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("shutdownDevice", Power::ShutdownDevice), 775ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("rebootDevice", Power::RebootDevice), 785ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("isScreenOn", Power::IsScreenOn), 795ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("wakeupDevice", Power::WakeupDevice), 805ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("suspendDevice", Power::SuspendDevice), 815ccb8f90Sopenharmony_ci 825ccb8f90Sopenharmony_ci // New interfaces 835ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("shutdown", PowerNapi::Shutdown), 845ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("reboot", PowerNapi::Reboot), 855ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("isActive", PowerNapi::IsActive), 865ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("wakeup", PowerNapi::Wakeup), 875ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("suspend", PowerNapi::Suspend), 885ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("hibernate", PowerNapi::Hibernate), 895ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("setPowerMode", PowerNapi::SetPowerMode), 905ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("getPowerMode", PowerNapi::GetPowerMode), 915ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("isStandby", PowerNapi::IsStandby), 925ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("setScreenOffTime", PowerNapi::SetScreenOffTime)}; 935ccb8f90Sopenharmony_ci NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); 945ccb8f90Sopenharmony_ci CreateDevicePowerMode(env, exports); 955ccb8f90Sopenharmony_ci POWER_HILOGD(COMP_FWK, "The initialization of the Power module is complete"); 965ccb8f90Sopenharmony_ci 975ccb8f90Sopenharmony_ci return exports; 985ccb8f90Sopenharmony_ci} 995ccb8f90Sopenharmony_ciEXTERN_C_END 1005ccb8f90Sopenharmony_ci 1015ccb8f90Sopenharmony_ci/* 1025ccb8f90Sopenharmony_ci * Module definition 1035ccb8f90Sopenharmony_ci */ 1045ccb8f90Sopenharmony_cistatic napi_module g_module = {.nm_version = 1, 1055ccb8f90Sopenharmony_ci .nm_flags = 0, 1065ccb8f90Sopenharmony_ci .nm_filename = "power", 1075ccb8f90Sopenharmony_ci .nm_register_func = PowerInit, 1085ccb8f90Sopenharmony_ci .nm_modname = "power", 1095ccb8f90Sopenharmony_ci .nm_priv = ((void*)0), 1105ccb8f90Sopenharmony_ci .reserved = {0}}; 1115ccb8f90Sopenharmony_ci 1125ccb8f90Sopenharmony_ci/* 1135ccb8f90Sopenharmony_ci * Module registration 1145ccb8f90Sopenharmony_ci */ 1155ccb8f90Sopenharmony_ciextern "C" __attribute__((constructor)) void RegisterPowerModule(void) 1165ccb8f90Sopenharmony_ci{ 1175ccb8f90Sopenharmony_ci napi_module_register(&g_module); 1185ccb8f90Sopenharmony_ci} 119