1/* 2 * Copyright (c) 2022 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 16#include "napi/native_api.h" 17#include "napi/native_common.h" 18#include "napi/native_node_api.h" 19 20#include "power.h" 21#include "power_log.h" 22#include "power_mode_info.h" 23#include "power_napi.h" 24 25using namespace OHOS::PowerMgr; 26 27static napi_value EnumPowerModeClassConstructor(napi_env env, napi_callback_info info) 28{ 29 napi_value thisArg = nullptr; 30 void* data = nullptr; 31 32 napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data); 33 34 napi_value global = nullptr; 35 napi_get_global(env, &global); 36 37 return thisArg; 38} 39 40static napi_value CreateDevicePowerMode(napi_env env, napi_value exports) 41{ 42 napi_value normal = nullptr; 43 napi_value powerSave = nullptr; 44 napi_value performance = nullptr; 45 napi_value extremePowerSave = nullptr; 46 47 napi_create_int32(env, (int32_t)PowerMode::NORMAL_MODE, &normal); 48 napi_create_int32(env, (int32_t)PowerMode::POWER_SAVE_MODE, &powerSave); 49 napi_create_int32(env, (int32_t)PowerMode::PERFORMANCE_MODE, &performance); 50 napi_create_int32(env, (int32_t)PowerMode::EXTREME_POWER_SAVE_MODE, &extremePowerSave); 51 52 napi_property_descriptor desc[] = { 53 DECLARE_NAPI_STATIC_PROPERTY("MODE_NORMAL", normal), 54 DECLARE_NAPI_STATIC_PROPERTY("MODE_POWER_SAVE", powerSave), 55 DECLARE_NAPI_STATIC_PROPERTY("MODE_PERFORMANCE", performance), 56 DECLARE_NAPI_STATIC_PROPERTY("MODE_EXTREME_POWER_SAVE", extremePowerSave), 57 }; 58 napi_value result = nullptr; 59 napi_define_class(env, "DevicePowerMode", NAPI_AUTO_LENGTH, EnumPowerModeClassConstructor, nullptr, 60 sizeof(desc) / sizeof(*desc), desc, &result); 61 62 napi_set_named_property(env, exports, "DevicePowerMode", result); 63 64 return exports; 65} 66 67EXTERN_C_START 68/* 69 * function for module exports 70 */ 71static napi_value PowerInit(napi_env env, napi_value exports) 72{ 73 POWER_HILOGD(COMP_FWK, "Initialize the Power module"); 74 napi_property_descriptor desc[] = { 75 // Old interfaces deprecated since 9 76 DECLARE_NAPI_FUNCTION("shutdownDevice", Power::ShutdownDevice), 77 DECLARE_NAPI_FUNCTION("rebootDevice", Power::RebootDevice), 78 DECLARE_NAPI_FUNCTION("isScreenOn", Power::IsScreenOn), 79 DECLARE_NAPI_FUNCTION("wakeupDevice", Power::WakeupDevice), 80 DECLARE_NAPI_FUNCTION("suspendDevice", Power::SuspendDevice), 81 82 // New interfaces 83 DECLARE_NAPI_FUNCTION("shutdown", PowerNapi::Shutdown), 84 DECLARE_NAPI_FUNCTION("reboot", PowerNapi::Reboot), 85 DECLARE_NAPI_FUNCTION("isActive", PowerNapi::IsActive), 86 DECLARE_NAPI_FUNCTION("wakeup", PowerNapi::Wakeup), 87 DECLARE_NAPI_FUNCTION("suspend", PowerNapi::Suspend), 88 DECLARE_NAPI_FUNCTION("hibernate", PowerNapi::Hibernate), 89 DECLARE_NAPI_FUNCTION("setPowerMode", PowerNapi::SetPowerMode), 90 DECLARE_NAPI_FUNCTION("getPowerMode", PowerNapi::GetPowerMode), 91 DECLARE_NAPI_FUNCTION("isStandby", PowerNapi::IsStandby), 92 DECLARE_NAPI_FUNCTION("setScreenOffTime", PowerNapi::SetScreenOffTime)}; 93 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); 94 CreateDevicePowerMode(env, exports); 95 POWER_HILOGD(COMP_FWK, "The initialization of the Power module is complete"); 96 97 return exports; 98} 99EXTERN_C_END 100 101/* 102 * Module definition 103 */ 104static napi_module g_module = {.nm_version = 1, 105 .nm_flags = 0, 106 .nm_filename = "power", 107 .nm_register_func = PowerInit, 108 .nm_modname = "power", 109 .nm_priv = ((void*)0), 110 .reserved = {0}}; 111 112/* 113 * Module registration 114 */ 115extern "C" __attribute__((constructor)) void RegisterPowerModule(void) 116{ 117 napi_module_register(&g_module); 118} 119