1 /*
2  * Copyright (c) 2021 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 "thermal_manager_napi.h"
17 
18 #include <uv.h>
19 
20 #include "napi_utils.h"
21 #include "napi_errors.h"
22 #include "thermal_common.h"
23 #include "thermal_level_info.h"
24 
25 using namespace OHOS::PowerMgr;
26 using namespace OHOS;
27 
28 namespace {
29 const uint8_t ARG_0 = 0;
30 const uint8_t ARG_1 = 1;
31 constexpr uint32_t MAX_ARGC = 1;
32 thread_local auto& g_thermalMgrClient = ThermalMgrClient::GetInstance();
33 thread_local sptr<ThermalLevelCallback> g_thermalLevelCallback = new (std::nothrow) ThermalLevelCallback();
34 } // namespace
35 
~ThermalLevelCallback()36 ThermalLevelCallback::~ThermalLevelCallback()
37 {
38     ReleaseCallback();
39 }
40 
UpdateCallback(napi_env env,napi_value jsCallback)41 void ThermalLevelCallback::UpdateCallback(napi_env env, napi_value jsCallback)
42 {
43     std::lock_guard lock(mutex_);
44     if (napi_ok != napi_create_reference(env, jsCallback, 1, &callbackRef_)) {
45         THERMAL_HILOGW(COMP_FWK, "Failed to create a JS callback reference");
46         callbackRef_ = nullptr;
47     }
48     env_ = env;
49 }
50 
ReleaseCallback()51 void ThermalLevelCallback::ReleaseCallback()
52 {
53     std::lock_guard lock(mutex_);
54     if (callbackRef_ != nullptr) {
55         napi_delete_reference(env_, callbackRef_);
56     }
57     callbackRef_ = nullptr;
58     env_ = nullptr;
59 }
60 
OnThermalLevelChanged(ThermalLevel level)61 bool ThermalLevelCallback::OnThermalLevelChanged(ThermalLevel level)
62 {
63     std::lock_guard lock(mutex_);
64     level_ = level;
65     THERMAL_RETURN_IF_WITH_RET(env_ == nullptr, false);
66     uv_loop_s* loop = nullptr;
67     napi_get_uv_event_loop(env_, &loop);
68     THERMAL_RETURN_IF_WITH_RET(loop == nullptr, false);
69     uv_work_t* work = new (std::nothrow) uv_work_t;
70     THERMAL_RETURN_IF_WITH_RET(work == nullptr, false);
71     work->data = reinterpret_cast<void*>(this);
72 
73     int32_t ret = uv_queue_work_with_qos(
74         loop, work,
75         [](uv_work_t* work) {
76             THERMAL_HILOGD(COMP_FWK, "uv_queue_work callback function is called");
77         },
78         [](uv_work_t* work, int status) {
79             ThermalLevelCallback* callback = reinterpret_cast<ThermalLevelCallback*>(work->data);
80             if (callback != nullptr) {
81                 callback->OnThermalLevel();
82             }
83             delete work;
84             work = nullptr;
85         },
86         uv_qos_utility);
87     if (ret != ERR_OK) {
88         delete work;
89         work = nullptr;
90         THERMAL_HILOGW(COMP_FWK, "uv_queue_work is failed");
91         return false;
92     }
93     return true;
94 }
95 
OnThermalLevel()96 void ThermalLevelCallback::OnThermalLevel()
97 {
98     THERMAL_HILOGD(COMP_FWK, "level is: %{public}d", static_cast<int32_t>(level_));
99     THERMAL_RETURN_IF_WITH_LOG(callbackRef_ == nullptr || env_ == nullptr, "js callback ref or env is nullptr");
100 
101     napi_handle_scope scope = nullptr;
102     napi_open_handle_scope(env_, &scope);
103     if (scope == nullptr) {
104         THERMAL_HILOGW(COMP_FWK, "scope is nullptr");
105         return;
106     }
107 
108     napi_value levelValue = nullptr;
109     if (napi_ok != napi_create_int32(env_, static_cast<int32_t>(level_), &levelValue)) {
110         THERMAL_HILOGW(COMP_FWK, "napi_create_int32 callback failed");
111         napi_close_handle_scope(env_, scope);
112         return;
113     }
114 
115     napi_value callback = nullptr;
116     napi_status status = napi_get_reference_value(env_, callbackRef_, &callback);
117     if (status != napi_ok) {
118         THERMAL_HILOGE(COMP_FWK, "napi_get_reference_value callback failed, status = %{public}d", status);
119         napi_close_handle_scope(env_, scope);
120         return;
121     }
122 
123     napi_value callResult = nullptr;
124     status = napi_call_function(env_, nullptr, callback, ARG_1, &levelValue, &callResult);
125     if (status != napi_ok) {
126         THERMAL_HILOGE(COMP_FWK, "napi_call_function callback failed, status = %{public}d", status);
127     }
128     napi_close_handle_scope(env_, scope);
129 }
130 
Init(napi_env env,napi_value exports)131 napi_value ThermalManagerNapi::Init(napi_env env, napi_value exports)
132 {
133     napi_property_descriptor desc[] = {
134         // Old Interface
135         DECLARE_NAPI_STATIC_FUNCTION("subscribeThermalLevel", SubscribeThermalLevel),
136         DECLARE_NAPI_STATIC_FUNCTION("unsubscribeThermalLevel", UnSubscribeThermalLevel),
137         DECLARE_NAPI_STATIC_FUNCTION("getThermalLevel", GetThermalLevel),
138         // New Interface
139         DECLARE_NAPI_STATIC_FUNCTION("registerThermalLevelCallback", SubscribeThermalLevel),
140         DECLARE_NAPI_STATIC_FUNCTION("unregisterThermalLevelCallback", UnSubscribeThermalLevel),
141         DECLARE_NAPI_STATIC_FUNCTION("getLevel", GetThermalLevel),
142     };
143     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
144     InitThermalLevel(env, exports);
145 
146     return exports;
147 }
148 
InitThermalLevel(napi_env env,napi_value exports)149 napi_value ThermalManagerNapi::InitThermalLevel(napi_env env, napi_value exports)
150 {
151     napi_value cool;
152     napi_value normal;
153     napi_value warm;
154     napi_value hot;
155     napi_value overheated;
156     napi_value warning;
157     napi_value emergency;
158     napi_value escape;
159 
160     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::COOL), &cool);
161     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::NORMAL), &normal);
162     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::WARM), &warm);
163     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::HOT), &hot);
164     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::OVERHEATED), &overheated);
165     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::WARNING), &warning);
166     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::EMERGENCY), &emergency);
167     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::ESCAPE), &escape);
168 
169     napi_property_descriptor desc[] = {
170         DECLARE_NAPI_STATIC_PROPERTY("COOL", cool),
171         DECLARE_NAPI_STATIC_PROPERTY("NORMAL", normal),
172         DECLARE_NAPI_STATIC_PROPERTY("WARM", warm),
173         DECLARE_NAPI_STATIC_PROPERTY("HOT", hot),
174         DECLARE_NAPI_STATIC_PROPERTY("OVERHEATED", overheated),
175         DECLARE_NAPI_STATIC_PROPERTY("WARNING", warning),
176         DECLARE_NAPI_STATIC_PROPERTY("EMERGENCY", emergency),
177         DECLARE_NAPI_STATIC_PROPERTY("ESCAPE", escape),
178     };
179 
180     napi_value result = nullptr;
181     napi_define_class(env, "ThermalLevel", NAPI_AUTO_LENGTH, EnumThermalLevelConstructor, nullptr,
182         sizeof(desc) / sizeof(*desc), desc, &result);
183     napi_set_named_property(env, exports, "ThermalLevel", result);
184     return exports;
185 }
186 
EnumThermalLevelConstructor(napi_env env,napi_callback_info info)187 napi_value ThermalManagerNapi::EnumThermalLevelConstructor(napi_env env, napi_callback_info info)
188 {
189     size_t argc = 0;
190     napi_value argv[ARG_1] = {0};
191     napi_value jsthis = nullptr;
192     void* data = nullptr;
193 
194     napi_status status = napi_get_cb_info(env, info, &argc, argv, &jsthis, &data);
195 
196     THERMAL_HILOGI(COMP_FWK, "EnumThermalLevelConstructor %{public}d", status);
197     if (status != napi_ok) {
198         return nullptr;
199     }
200     return jsthis;
201 }
202 
GetThermalLevel(napi_env env,napi_callback_info info)203 napi_value ThermalManagerNapi::GetThermalLevel(napi_env env, napi_callback_info info)
204 {
205     ThermalLevel level = g_thermalMgrClient.GetThermalLevel();
206     int32_t levelValue = static_cast<int32_t>(level);
207     napi_value napiValue;
208     NAPI_CALL(env, napi_create_int32(env, levelValue, &napiValue));
209 
210     THERMAL_HILOGI(COMP_FWK, "level is %{public}d", levelValue);
211     return napiValue;
212 }
213 
SubscribeThermalLevel(napi_env env,napi_callback_info info)214 napi_value ThermalManagerNapi::SubscribeThermalLevel(napi_env env, napi_callback_info info)
215 {
216     size_t argc = MAX_ARGC;
217     napi_value argv[argc];
218     NapiUtils::GetCallbackInfo(env, info, argc, argv);
219 
220     NapiErrors error;
221     if (argc != MAX_ARGC || !NapiUtils::CheckValueType(env, argv[ARG_0], napi_function)) {
222         return error.ThrowError(env, ThermalErrors::ERR_PARAM_INVALID);
223     }
224 
225     napi_value result;
226     napi_get_undefined(env, &result);
227 
228     THERMAL_RETURN_IF_WITH_RET(g_thermalLevelCallback == nullptr, result);
229     g_thermalLevelCallback->ReleaseCallback();
230     g_thermalLevelCallback->UpdateCallback(env, argv[ARG_0]);
231     g_thermalMgrClient.SubscribeThermalLevelCallback(g_thermalLevelCallback);
232 
233     return result;
234 }
235 
UnSubscribeThermalLevel(napi_env env,napi_callback_info info)236 napi_value ThermalManagerNapi::UnSubscribeThermalLevel(napi_env env, napi_callback_info info)
237 {
238     size_t argc = MAX_ARGC;
239     napi_value argv[argc];
240     NapiUtils::GetCallbackInfo(env, info, argc, argv);
241 
242     THERMAL_RETURN_IF_WITH_RET(g_thermalLevelCallback == nullptr, nullptr);
243     g_thermalLevelCallback->ReleaseCallback();
244     g_thermalMgrClient.UnSubscribeThermalLevelCallback(g_thermalLevelCallback);
245 
246     THERMAL_RETURN_IF_WITH_RET(argc == ARG_0, nullptr);
247     NapiErrors error;
248     if (argc > MAX_ARGC || !NapiUtils::CheckValueType(env, argv[ARG_0], napi_function)) {
249         return error.ThrowError(env, ThermalErrors::ERR_PARAM_INVALID);
250     }
251 
252     napi_value handler = nullptr;
253     napi_ref handlerRef = nullptr;
254     napi_create_reference(env, argv[ARG_0], 1, &handlerRef);
255     napi_get_reference_value(env, handlerRef, &handler);
256     napi_delete_reference(env, handlerRef);
257 
258     napi_value result = nullptr;
259     if (handler == nullptr) {
260         THERMAL_HILOGW(COMP_FWK, "Handler should not be nullptr");
261         return result;
262     }
263 
264     napi_get_undefined(env, &result);
265     napi_status status = napi_call_function(env, nullptr, handler, ARG_0, nullptr, &result);
266     if (status != napi_ok) {
267         THERMAL_HILOGW(COMP_FWK, "status=%{public}d", status);
268         return result;
269     }
270     return result;
271 }
272 
273 EXTERN_C_START
274 /*
275  * function for module exports
276  */
ThermalInit(napi_env env,napi_value exports)277 static napi_value ThermalInit(napi_env env, napi_value exports)
278 {
279     THERMAL_HILOGD(COMP_FWK, "Enter");
280 
281     napi_value ret = ThermalManagerNapi::Init(env, exports);
282 
283     THERMAL_HILOGD(COMP_FWK, "Exit");
284 
285     return ret;
286 }
287 EXTERN_C_END
288 
289 /*
290  * Module definition
291  */
292 static napi_module g_module = {.nm_version = 1,
293     .nm_flags = 0,
294     .nm_filename = "thermal",
295     .nm_register_func = ThermalInit,
296     .nm_modname = "thermal",
297     .nm_priv = ((void*)0),
298     .reserved = {0}};
299 
300 /*
301  * Module registration
302  */
RegisterModule()303 extern "C" __attribute__((constructor)) void RegisterModule()
304 {
305     napi_module_register(&g_module);
306 }
307