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