• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 using namespace OHOS::PowerMgr;
26 
EnumPowerModeClassConstructor(napi_env env,napi_callback_info info)27 static 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 
CreateDevicePowerMode(napi_env env,napi_value exports)40 static 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     napi_value customPowerSave = nullptr;
47 
48     napi_create_int32(env, (int32_t)PowerMode::NORMAL_MODE, &normal);
49     napi_create_int32(env, (int32_t)PowerMode::POWER_SAVE_MODE, &powerSave);
50     napi_create_int32(env, (int32_t)PowerMode::PERFORMANCE_MODE, &performance);
51     napi_create_int32(env, (int32_t)PowerMode::EXTREME_POWER_SAVE_MODE, &extremePowerSave);
52     napi_create_int32(env, (int32_t)PowerMode::CUSTOM_POWER_SAVE_MODE, &customPowerSave);
53 
54     napi_property_descriptor desc[] = {
55         DECLARE_NAPI_STATIC_PROPERTY("MODE_NORMAL", normal),
56         DECLARE_NAPI_STATIC_PROPERTY("MODE_POWER_SAVE", powerSave),
57         DECLARE_NAPI_STATIC_PROPERTY("MODE_PERFORMANCE", performance),
58         DECLARE_NAPI_STATIC_PROPERTY("MODE_EXTREME_POWER_SAVE", extremePowerSave),
59         DECLARE_NAPI_STATIC_PROPERTY("MODE_CUSTOM_POWER_SAVE", customPowerSave),
60     };
61     napi_value result = nullptr;
62     napi_define_class(env, "DevicePowerMode", NAPI_AUTO_LENGTH, EnumPowerModeClassConstructor, nullptr,
63         sizeof(desc) / sizeof(*desc), desc, &result);
64 
65     napi_set_named_property(env, exports, "DevicePowerMode", result);
66 
67     return exports;
68 }
69 
70 EXTERN_C_START
71 /*
72  * function for module exports
73  */
PowerInit(napi_env env,napi_value exports)74 static napi_value PowerInit(napi_env env, napi_value exports)
75 {
76     POWER_HILOGD(COMP_FWK, "Initialize the Power module");
77     napi_property_descriptor desc[] = {
78         // Old interfaces deprecated since 9
79         DECLARE_NAPI_FUNCTION("shutdownDevice", Power::ShutdownDevice),
80         DECLARE_NAPI_FUNCTION("rebootDevice", Power::RebootDevice),
81         DECLARE_NAPI_FUNCTION("isScreenOn", Power::IsScreenOn),
82         DECLARE_NAPI_FUNCTION("wakeupDevice", Power::WakeupDevice),
83         DECLARE_NAPI_FUNCTION("suspendDevice", Power::SuspendDevice),
84 
85         // New interfaces
86         DECLARE_NAPI_FUNCTION("shutdown", PowerNapi::Shutdown),
87         DECLARE_NAPI_FUNCTION("reboot", PowerNapi::Reboot),
88         DECLARE_NAPI_FUNCTION("isActive", PowerNapi::IsActive),
89         DECLARE_NAPI_FUNCTION("wakeup", PowerNapi::Wakeup),
90         DECLARE_NAPI_FUNCTION("suspend", PowerNapi::Suspend),
91         DECLARE_NAPI_FUNCTION("hibernate", PowerNapi::Hibernate),
92         DECLARE_NAPI_FUNCTION("setPowerMode", PowerNapi::SetPowerMode),
93         DECLARE_NAPI_FUNCTION("getPowerMode", PowerNapi::GetPowerMode),
94         DECLARE_NAPI_FUNCTION("isStandby", PowerNapi::IsStandby),
95         DECLARE_NAPI_FUNCTION("setScreenOffTime", PowerNapi::SetScreenOffTime),
96         DECLARE_NAPI_FUNCTION("refreshActivity", PowerNapi::RefreshActivity)};
97     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
98     CreateDevicePowerMode(env, exports);
99     POWER_HILOGD(COMP_FWK, "The initialization of the Power module is complete");
100 
101     return exports;
102 }
103 EXTERN_C_END
104 
105 /*
106  * Module definition
107  */
108 static napi_module g_module = {.nm_version = 1,
109     .nm_flags = 0,
110     .nm_filename = "power",
111     .nm_register_func = PowerInit,
112     .nm_modname = "power",
113     .nm_priv = ((void*)0),
114     .reserved = {0}};
115 
116 /*
117  * Module registration
118  */
RegisterPowerModule(void)119 extern "C" __attribute__((constructor)) void RegisterPowerModule(void)
120 {
121     napi_module_register(&g_module);
122 }
123