1 /*
2 * Copyright (c) 2021-2023 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 <cstdint>
17 #include <memory>
18 #include <string>
19
20 #include "napi/native_api.h"
21 #include "napi/native_common.h"
22 #include "napi/native_node_api.h"
23
24 #include "power.h"
25 #include "power_log.h"
26 #include "power_mgr_client.h"
27 #include "power_state_machine_info.h"
28
29 namespace OHOS {
30 namespace PowerMgr {
31 namespace {
32 constexpr int REASON_MAX = 512;
33 constexpr int RESULT_SIZE = 2;
34 static PowerMgrClient& g_powerMgrClient = PowerMgrClient::GetInstance();
35 }
36
RebootOrShutdown(napi_env env,napi_callback_info info,bool isReboot)37 napi_value Power::RebootOrShutdown(napi_env env, napi_callback_info info, bool isReboot)
38 {
39 size_t argc = 1;
40 napi_value args[1] = {0};
41 napi_value jsthis;
42 void* data = nullptr;
43
44 napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data);
45 NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info");
46 napi_valuetype type = napi_undefined;
47 NAPI_CALL(env, napi_typeof(env, args[0], &type));
48 NAPI_ASSERT(env, type == napi_string, "Wrong argument type. string expected.");
49
50 char reason[REASON_MAX] = {0};
51 size_t reasonLen = 0;
52 status = napi_get_value_string_utf8(env, args[0], reason, REASON_MAX - 1, &reasonLen);
53 if (status != napi_ok) {
54 POWER_HILOGE(FEATURE_SHUTDOWN, "Get shutdown reason failed");
55 return nullptr;
56 }
57 POWER_HILOGD(FEATURE_SHUTDOWN, "reboot: %{public}d, reason: %{public}s", isReboot, reason);
58 if (isReboot) {
59 g_powerMgrClient.RebootDeviceForDeprecated(std::string(reason));
60 } else {
61 g_powerMgrClient.ShutDownDevice(std::string(reason));
62 }
63 return nullptr;
64 }
65
ShutdownDevice(napi_env env,napi_callback_info info)66 napi_value Power::ShutdownDevice(napi_env env, napi_callback_info info)
67 {
68 return RebootOrShutdown(env, info, false);
69 }
70
RebootDevice(napi_env env,napi_callback_info info)71 napi_value Power::RebootDevice(napi_env env, napi_callback_info info)
72 {
73 return RebootOrShutdown(env, info, true);
74 }
75
IsScreenOnCallBack(napi_env env,std::unique_ptr<PowerAsyncCallbackInfo> & asCallbackInfoPtr)76 void Power::IsScreenOnCallBack(napi_env env, std::unique_ptr<PowerAsyncCallbackInfo>& asCallbackInfoPtr)
77 {
78 napi_value resource = nullptr;
79 napi_create_string_utf8(env, "IsScreenOn", NAPI_AUTO_LENGTH, &resource);
80 napi_create_async_work(
81 env, nullptr, resource,
82 [](napi_env env, void* data) {
83 PowerAsyncCallbackInfo* asyncCallbackInfo = reinterpret_cast<PowerAsyncCallbackInfo*>(data);
84 asyncCallbackInfo->screenOn = g_powerMgrClient.IsScreenOn();
85 POWER_HILOGD(COMP_FWK, "Screen is %{public}s ", asyncCallbackInfo->screenOn ? "ON" : "OFF");
86 },
87 [](napi_env env, napi_status status, void* data) {
88 PowerAsyncCallbackInfo* asyncCallbackInfo = reinterpret_cast<PowerAsyncCallbackInfo*>(data);
89 napi_value result[RESULT_SIZE] = {0};
90 napi_get_boolean(env, asyncCallbackInfo->screenOn, &result[1]);
91 if (asyncCallbackInfo->deferred) {
92 napi_resolve_deferred(env, asyncCallbackInfo->deferred, result[1]);
93 } else {
94 napi_value tmp = nullptr;
95 napi_value callback = nullptr;
96 napi_get_reference_value(env, asyncCallbackInfo->callbackRef, &callback);
97 napi_get_undefined(env, &result[0]);
98 napi_call_function(env, nullptr, callback, RESULT_SIZE, result, &tmp);
99 napi_delete_reference(env, asyncCallbackInfo->callbackRef);
100 }
101 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
102 delete asyncCallbackInfo;
103 },
104 reinterpret_cast<void*>(asCallbackInfoPtr.get()), &asCallbackInfoPtr->asyncWork);
105 if (napi_ok == napi_queue_async_work_with_qos(env, asCallbackInfoPtr->asyncWork, napi_qos_utility)) {
106 asCallbackInfoPtr.release();
107 }
108 }
109
IsScreenOn(napi_env env,napi_callback_info info)110 napi_value Power::IsScreenOn(napi_env env, napi_callback_info info)
111 {
112 size_t argc = 1;
113 napi_value args[1] = {0};
114 napi_value jsthis;
115 void* data = nullptr;
116
117 napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data);
118 NAPI_ASSERT(env, (status == napi_ok), "Failed to get cb info");
119
120 std::unique_ptr<PowerAsyncCallbackInfo> asyncCallbackInfo = std::make_unique<PowerAsyncCallbackInfo>();
121 if (asyncCallbackInfo == nullptr) {
122 POWER_HILOGE(COMP_FWK, "Failed to create asyncCallbackInfo");
123 return nullptr;
124 }
125 asyncCallbackInfo->env = env;
126 napi_valuetype type;
127 if (argc == 1) {
128 NAPI_CALL(env, napi_typeof(env, args[0], &type));
129 if (type != napi_function) {
130 POWER_HILOGE(COMP_FWK, "Wrong argument type. napi_function expected");
131 return nullptr;
132 }
133 napi_create_reference(env, args[0], 1, &asyncCallbackInfo->callbackRef);
134 }
135 napi_value result = nullptr;
136 if (asyncCallbackInfo->callbackRef == nullptr) {
137 POWER_HILOGD(COMP_FWK, "callbackRef is null");
138 napi_create_promise(env, &asyncCallbackInfo->deferred, &result);
139 } else {
140 POWER_HILOGD(COMP_FWK, "callbackRef is not null");
141 napi_get_undefined(env, &result);
142 }
143 IsScreenOnCallBack(env, asyncCallbackInfo);
144 return result;
145 }
146
WakeupDevice(napi_env env,napi_callback_info info)147 napi_value Power::WakeupDevice(napi_env env, napi_callback_info info)
148 {
149 size_t argc = 1;
150 napi_value args[1] = {0};
151 napi_value jsthis;
152 void* data = nullptr;
153
154 napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data);
155 NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info");
156 napi_valuetype type = napi_undefined;
157 NAPI_CALL(env, napi_typeof(env, args[0], &type));
158 NAPI_ASSERT(env, type == napi_string, "Wrong argument type. string expected.");
159
160 char reason[REASON_MAX] = {0};
161 size_t reasonLen = 0;
162 status = napi_get_value_string_utf8(env, args[0], reason, REASON_MAX - 1, &reasonLen);
163 if (status != napi_ok) {
164 POWER_HILOGE(FEATURE_WAKEUP, "Get wakeup reason failed");
165 return nullptr;
166 }
167 POWER_HILOGD(FEATURE_WAKEUP, "Wakeup type: APPLICATION, reason: %{public}s", reason);
168 g_powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string(reason));
169 return nullptr;
170 }
171
SuspendDevice(napi_env env,napi_callback_info info)172 napi_value Power::SuspendDevice(napi_env env, napi_callback_info info)
173 {
174 napi_value thisArg = nullptr;
175 void* data = nullptr;
176
177 napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
178 NAPI_ASSERT(env, (status == napi_ok), "Failed to get cb info");
179
180 POWER_HILOGD(FEATURE_SUSPEND, "Suspend type: APPLICATION");
181 g_powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false);
182 return nullptr;
183 }
184 } // namespace PowerMgr
185 } // namespace OHOS
186