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 auto asyncCallbackInfo = new PowerAsyncCallbackInfo();
121 if (asyncCallbackInfo == nullptr) {
122 POWER_HILOGE(COMP_FWK, "Failed to create asyncCallbackInfo");
123 return nullptr;
124 }
125 asyncCallbackInfo->env = env;
126
127 std::unique_ptr<PowerAsyncCallbackInfo> asCallbackInfoPtr(asyncCallbackInfo);
128 napi_valuetype type;
129 if (argc == 1) {
130 NAPI_CALL(env, napi_typeof(env, args[0], &type));
131 if (type != napi_function) {
132 POWER_HILOGE(COMP_FWK, "Wrong argument type. napi_function expected");
133 return nullptr;
134 }
135 napi_create_reference(env, args[0], 1, &asyncCallbackInfo->callbackRef);
136 }
137 napi_value result = nullptr;
138 if (asyncCallbackInfo->callbackRef == nullptr) {
139 POWER_HILOGD(COMP_FWK, "callbackRef is null");
140 napi_create_promise(env, &asyncCallbackInfo->deferred, &result);
141 } else {
142 POWER_HILOGD(COMP_FWK, "callbackRef is not null");
143 napi_get_undefined(env, &result);
144 }
145 IsScreenOnCallBack(env, asCallbackInfoPtr);
146 return result;
147 }
148
WakeupDevice(napi_env env,napi_callback_info info)149 napi_value Power::WakeupDevice(napi_env env, napi_callback_info info)
150 {
151 size_t argc = 1;
152 napi_value args[1] = {0};
153 napi_value jsthis;
154 void* data = nullptr;
155
156 napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data);
157 NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info");
158 napi_valuetype type = napi_undefined;
159 NAPI_CALL(env, napi_typeof(env, args[0], &type));
160 NAPI_ASSERT(env, type == napi_string, "Wrong argument type. string expected.");
161
162 char reason[REASON_MAX] = {0};
163 size_t reasonLen = 0;
164 status = napi_get_value_string_utf8(env, args[0], reason, REASON_MAX - 1, &reasonLen);
165 if (status != napi_ok) {
166 POWER_HILOGE(FEATURE_WAKEUP, "Get wakeup reason failed");
167 return nullptr;
168 }
169 POWER_HILOGD(FEATURE_WAKEUP, "Wakeup type: APPLICATION, reason: %{public}s", reason);
170 g_powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string(reason));
171 return nullptr;
172 }
173
SuspendDevice(napi_env env,napi_callback_info info)174 napi_value Power::SuspendDevice(napi_env env, napi_callback_info info)
175 {
176 napi_value thisArg = nullptr;
177 void* data = nullptr;
178
179 napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
180 NAPI_ASSERT(env, (status == napi_ok), "Failed to get cb info");
181
182 POWER_HILOGD(FEATURE_SUSPEND, "Suspend type: APPLICATION");
183 g_powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false);
184 return nullptr;
185 }
186 } // namespace PowerMgr
187 } // namespace OHOS
188