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 "power_napi.h"
17
18 #include "napi_errors.h"
19 #include "napi_utils.h"
20 #include "power_common.h"
21 #include "power_log.h"
22 #include "power_mgr_client.h"
23
24 namespace OHOS {
25 namespace PowerMgr {
26 namespace {
27 constexpr uint32_t REBOOT_SHUTDOWN_MAX_ARGC = 1;
28 constexpr uint32_t WAKEUP_MAX_ARGC = 1;
29 constexpr uint32_t SET_MODE_CALLBACK_MAX_ARGC = 2;
30 constexpr uint32_t SET_MODE_PROMISE_MAX_ARGC = 1;
31 constexpr int32_t INDEX_0 = 0;
32 constexpr int32_t INDEX_1 = 1;
33 static PowerMgrClient& g_powerMgrClient = PowerMgrClient::GetInstance();
34 } // namespace
Shutdown(napi_env env,napi_callback_info info)35 napi_value PowerNapi::Shutdown(napi_env env, napi_callback_info info)
36 {
37 return RebootOrShutdown(env, info, false);
38 }
39
Reboot(napi_env env,napi_callback_info info)40 napi_value PowerNapi::Reboot(napi_env env, napi_callback_info info)
41 {
42 return RebootOrShutdown(env, info, true);
43 }
44
IsActive(napi_env env,napi_callback_info info)45 napi_value PowerNapi::IsActive(napi_env env, napi_callback_info info)
46 {
47 bool isScreen = g_powerMgrClient.IsScreenOn();
48 napi_value napiValue;
49 NAPI_CALL(env, napi_get_boolean(env, isScreen, &napiValue));
50 return napiValue;
51 }
52
Wakeup(napi_env env,napi_callback_info info)53 napi_value PowerNapi::Wakeup(napi_env env, napi_callback_info info)
54 {
55 size_t argc = WAKEUP_MAX_ARGC;
56 napi_value argv[argc];
57 NapiUtils::GetCallbackInfo(env, info, argc, argv);
58
59 NapiErrors error;
60 if (argc != WAKEUP_MAX_ARGC || !NapiUtils::CheckValueType(env, argv[INDEX_0], napi_string)) {
61 return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
62 }
63
64 std::string detail = NapiUtils::GetStringFromNapi(env, argv[INDEX_0]);
65 POWER_HILOGD(FEATURE_WAKEUP, "Wakeup type: APPLICATION, reason: %{public}s", detail.c_str());
66 PowerErrors code = g_powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, detail);
67 if (code != PowerErrors::ERR_OK) {
68 error.ThrowError(env, code);
69 }
70 return nullptr;
71 }
72
Suspend(napi_env env,napi_callback_info info)73 napi_value PowerNapi::Suspend(napi_env env, napi_callback_info info)
74 {
75 NapiErrors error;
76 PowerErrors code = g_powerMgrClient.SuspendDevice();
77 if (code != PowerErrors::ERR_OK) {
78 error.ThrowError(env, code);
79 }
80 return nullptr;
81 }
82
SetPowerMode(napi_env env,napi_callback_info info)83 napi_value PowerNapi::SetPowerMode(napi_env env, napi_callback_info info)
84 {
85 size_t argc = SET_MODE_CALLBACK_MAX_ARGC;
86 napi_value argv[argc];
87 NapiUtils::GetCallbackInfo(env, info, argc, argv);
88
89 NapiErrors error;
90 if (argc != SET_MODE_CALLBACK_MAX_ARGC && argc != SET_MODE_PROMISE_MAX_ARGC) {
91 return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
92 }
93
94 std::unique_ptr<AsyncCallbackInfo> asyncInfo = std::make_unique<AsyncCallbackInfo>();
95 RETURN_IF_WITH_RET(asyncInfo == nullptr, nullptr);
96 // callback
97 if (argc == SET_MODE_CALLBACK_MAX_ARGC) {
98 POWER_HILOGD(FEATURE_POWER_MODE, "Call setPowerMode callback");
99 return SetPowerModeCallback(env, argv, asyncInfo);
100 }
101
102 // promise
103 POWER_HILOGD(FEATURE_POWER_MODE, "Call setPowerMode promise");
104 return SetPowerModePromise(env, argv, asyncInfo);
105 }
106
GetPowerMode(napi_env env,napi_callback_info info)107 napi_value PowerNapi::GetPowerMode(napi_env env, napi_callback_info info)
108 {
109 PowerMode mode = g_powerMgrClient.GetDeviceMode();
110 napi_value napiValue;
111 NAPI_CALL(env, napi_create_uint32(env, static_cast<uint32_t>(mode), &napiValue));
112 return napiValue;
113 }
114
RebootOrShutdown(napi_env env,napi_callback_info info,bool isReboot)115 napi_value PowerNapi::RebootOrShutdown(napi_env env, napi_callback_info info, bool isReboot)
116 {
117 size_t argc = REBOOT_SHUTDOWN_MAX_ARGC;
118 napi_value argv[argc];
119 NapiUtils::GetCallbackInfo(env, info, argc, argv);
120
121 NapiErrors error;
122 if (argc != REBOOT_SHUTDOWN_MAX_ARGC || !NapiUtils::CheckValueType(env, argv[INDEX_0], napi_string)) {
123 return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
124 }
125
126 std::string reason = NapiUtils::GetStringFromNapi(env, argv[INDEX_0]);
127 POWER_HILOGD(FEATURE_SHUTDOWN, "reboot: %{public}d, reason: %{public}s", isReboot, reason.c_str());
128
129 PowerErrors code;
130 if (isReboot) {
131 code = g_powerMgrClient.RebootDevice(reason);
132 } else {
133 code = g_powerMgrClient.ShutDownDevice(reason);
134 }
135 if (code != PowerErrors::ERR_OK) {
136 error.ThrowError(env, code);
137 }
138
139 return nullptr;
140 }
141
SetPowerModeCallback(napi_env & env,napi_value argv[],std::unique_ptr<AsyncCallbackInfo> & asyncInfo)142 napi_value PowerNapi::SetPowerModeCallback(
143 napi_env& env, napi_value argv[], std::unique_ptr<AsyncCallbackInfo>& asyncInfo)
144 {
145 bool isNum = NapiUtils::CheckValueType(env, argv[INDEX_0], napi_number);
146 bool isFunc = NapiUtils::CheckValueType(env, argv[INDEX_1], napi_function);
147 if (!isNum || !isFunc) {
148 POWER_HILOGW(FEATURE_POWER_MODE, "isNum: %{public}d, isFunc: %{public}d", isNum, isFunc);
149 return asyncInfo->GetError().ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
150 }
151
152 asyncInfo->GetData().SetMode(env, argv[INDEX_0]);
153 asyncInfo->CreateCallback(env, argv[INDEX_1]);
154
155 AsyncWork(
156 env, asyncInfo, "SetPowerModeCallback",
157 [](napi_env env, void* data) {
158 AsyncCallbackInfo* asyncInfo = (AsyncCallbackInfo*)data;
159 RETURN_IF(asyncInfo == nullptr);
160 PowerErrors error = g_powerMgrClient.SetDeviceMode(asyncInfo->GetData().GetMode());
161 asyncInfo->GetError().Error(error);
162 },
163 [](napi_env env, napi_status status, void* data) {
164 AsyncCallbackInfo* asyncInfo = (AsyncCallbackInfo*)data;
165 RETURN_IF(asyncInfo == nullptr);
166 asyncInfo->CallFunction(env, nullptr);
167 asyncInfo->Release(env);
168 delete asyncInfo;
169 });
170 return nullptr;
171 }
172
SetPowerModePromise(napi_env & env,napi_value argv[],std::unique_ptr<AsyncCallbackInfo> & asyncInfo)173 napi_value PowerNapi::SetPowerModePromise(
174 napi_env& env, napi_value argv[], std::unique_ptr<AsyncCallbackInfo>& asyncInfo)
175 {
176 bool isNum = NapiUtils::CheckValueType(env, argv[INDEX_0], napi_number);
177 if (!isNum) {
178 POWER_HILOGW(FEATURE_POWER_MODE, "isNum: %{public}d", isNum);
179 return asyncInfo->GetError().ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
180 }
181 napi_value promise;
182 asyncInfo->CreatePromise(env, promise);
183 RETURN_IF_WITH_RET(promise == nullptr, nullptr);
184 asyncInfo->GetData().SetMode(env, argv[INDEX_0]);
185
186 AsyncWork(
187 env, asyncInfo, "SetPowerModePromise",
188 [](napi_env env, void* data) {
189 AsyncCallbackInfo* asyncInfo = (AsyncCallbackInfo*)data;
190 RETURN_IF(asyncInfo == nullptr);
191 PowerErrors error = g_powerMgrClient.SetDeviceMode(asyncInfo->GetData().GetMode());
192 asyncInfo->GetError().Error(error);
193 },
194 [](napi_env env, napi_status status, void* data) {
195 AsyncCallbackInfo* asyncInfo = (AsyncCallbackInfo*)data;
196 RETURN_IF(asyncInfo == nullptr);
197 if (asyncInfo->GetError().IsError()) {
198 napi_reject_deferred(env, asyncInfo->GetDeferred(), asyncInfo->GetError().GetNapiError(env));
199 } else {
200 napi_value undefined;
201 napi_get_undefined(env, &undefined);
202 napi_resolve_deferred(env, asyncInfo->GetDeferred(), undefined);
203 }
204 asyncInfo->Release(env);
205 delete asyncInfo;
206 });
207 return promise;
208 }
209
AsyncWork(napi_env & env,std::unique_ptr<AsyncCallbackInfo> & asyncInfo,const std::string resourceName,napi_async_execute_callback execute,napi_async_complete_callback complete)210 void PowerNapi::AsyncWork(napi_env& env, std::unique_ptr<AsyncCallbackInfo>& asyncInfo, const std::string resourceName,
211 napi_async_execute_callback execute, napi_async_complete_callback complete)
212 {
213 napi_value resource = nullptr;
214 napi_create_string_utf8(env, resourceName.c_str(), NAPI_AUTO_LENGTH, &resource);
215 napi_create_async_work(
216 env, nullptr, resource, execute, complete, (void*)asyncInfo.get(), &(asyncInfo->GetAsyncWork()));
217 NAPI_CALL_RETURN_VOID(env, napi_queue_async_work(env, asyncInfo->GetAsyncWork()));
218 asyncInfo.release();
219 }
220 } // namespace PowerMgr
221 } // namespace OHOS
222