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
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
67 EXTERN_C_START
68 /*
69 * function for module exports
70 */
PowerInit(napi_env env,napi_value exports)71 static 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("setPowerMode", PowerNapi::SetPowerMode),
89 DECLARE_NAPI_FUNCTION("getPowerMode", PowerNapi::GetPowerMode)};
90 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
91 CreateDevicePowerMode(env, exports);
92 POWER_HILOGD(COMP_FWK, "The initialization of the Power module is complete");
93
94 return exports;
95 }
96 EXTERN_C_END
97
98 /*
99 * Module definition
100 */
101 static napi_module g_module = {.nm_version = 1,
102 .nm_flags = 0,
103 .nm_filename = "power",
104 .nm_register_func = PowerInit,
105 .nm_modname = "power",
106 .nm_priv = ((void*)0),
107 .reserved = {0}};
108
109 /*
110 * Module registration
111 */
RegisterPowerModule(void)112 extern "C" __attribute__((constructor)) void RegisterPowerModule(void)
113 {
114 napi_module_register(&g_module);
115 }
116