• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_work_t* work = new (std::nothrow) uv_work_t;
67     THERMAL_RETURN_IF_WITH_RET(work == nullptr, false);
68     work->data = reinterpret_cast<void*>(this);
69     auto uvcallback = [work]() mutable {
70         ThermalLevelCallback* callback = reinterpret_cast<ThermalLevelCallback*>(work->data);
71         if (callback != nullptr) {
72             callback->OnThermalLevel();
73         }
74         delete work;
75         work = nullptr;
76     };
77     if (napi_send_event(env_, uvcallback, napi_eprio_low) != napi_status::napi_ok) {
78         delete work;
79         work = nullptr;
80         THERMAL_HILOGW(COMP_FWK, "uv_queue_work is failed");
81         return false;
82     }
83     return true;
84 }
85 
OnThermalLevel()86 void ThermalLevelCallback::OnThermalLevel()
87 {
88     THERMAL_HILOGD(COMP_FWK, "level is: %{public}d", static_cast<int32_t>(level_));
89     THERMAL_RETURN_IF_WITH_LOG(callbackRef_ == nullptr || env_ == nullptr, "js callback ref or env is nullptr");
90 
91     napi_handle_scope scope = nullptr;
92     napi_open_handle_scope(env_, &scope);
93     if (scope == nullptr) {
94         THERMAL_HILOGW(COMP_FWK, "scope is nullptr");
95         return;
96     }
97 
98     napi_value levelValue = nullptr;
99     if (napi_ok != napi_create_int32(env_, static_cast<int32_t>(level_), &levelValue)) {
100         THERMAL_HILOGW(COMP_FWK, "napi_create_int32 callback failed");
101         napi_close_handle_scope(env_, scope);
102         return;
103     }
104 
105     napi_value callback = nullptr;
106     napi_status status = napi_get_reference_value(env_, callbackRef_, &callback);
107     if (status != napi_ok) {
108         THERMAL_HILOGE(COMP_FWK, "napi_get_reference_value callback failed, status = %{public}d", status);
109         napi_close_handle_scope(env_, scope);
110         return;
111     }
112 
113     napi_value callResult = nullptr;
114     status = napi_call_function(env_, nullptr, callback, ARG_1, &levelValue, &callResult);
115     if (status != napi_ok) {
116         THERMAL_HILOGE(COMP_FWK, "napi_call_function callback failed, status = %{public}d", status);
117     }
118     napi_close_handle_scope(env_, scope);
119 }
120 
Init(napi_env env,napi_value exports)121 napi_value ThermalManagerNapi::Init(napi_env env, napi_value exports)
122 {
123     napi_property_descriptor desc[] = {
124         // Old Interface
125         DECLARE_NAPI_STATIC_FUNCTION("subscribeThermalLevel", SubscribeThermalLevel),
126         DECLARE_NAPI_STATIC_FUNCTION("unsubscribeThermalLevel", UnSubscribeThermalLevel),
127         DECLARE_NAPI_STATIC_FUNCTION("getThermalLevel", GetThermalLevel),
128         // New Interface
129         DECLARE_NAPI_STATIC_FUNCTION("registerThermalLevelCallback", SubscribeThermalLevel),
130         DECLARE_NAPI_STATIC_FUNCTION("unregisterThermalLevelCallback", UnSubscribeThermalLevel),
131         DECLARE_NAPI_STATIC_FUNCTION("getLevel", GetThermalLevel),
132     };
133     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
134     InitThermalLevel(env, exports);
135 
136     return exports;
137 }
138 
InitThermalLevel(napi_env env,napi_value exports)139 napi_value ThermalManagerNapi::InitThermalLevel(napi_env env, napi_value exports)
140 {
141     napi_value cool;
142     napi_value normal;
143     napi_value warm;
144     napi_value hot;
145     napi_value overheated;
146     napi_value warning;
147     napi_value emergency;
148     napi_value escape;
149 
150     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::COOL), &cool);
151     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::NORMAL), &normal);
152     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::WARM), &warm);
153     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::HOT), &hot);
154     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::OVERHEATED), &overheated);
155     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::WARNING), &warning);
156     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::EMERGENCY), &emergency);
157     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::ESCAPE), &escape);
158 
159     napi_property_descriptor desc[] = {
160         DECLARE_NAPI_STATIC_PROPERTY("COOL", cool),
161         DECLARE_NAPI_STATIC_PROPERTY("NORMAL", normal),
162         DECLARE_NAPI_STATIC_PROPERTY("WARM", warm),
163         DECLARE_NAPI_STATIC_PROPERTY("HOT", hot),
164         DECLARE_NAPI_STATIC_PROPERTY("OVERHEATED", overheated),
165         DECLARE_NAPI_STATIC_PROPERTY("WARNING", warning),
166         DECLARE_NAPI_STATIC_PROPERTY("EMERGENCY", emergency),
167         DECLARE_NAPI_STATIC_PROPERTY("ESCAPE", escape),
168     };
169 
170     napi_value result = nullptr;
171     napi_define_class(env, "ThermalLevel", NAPI_AUTO_LENGTH, EnumThermalLevelConstructor, nullptr,
172         sizeof(desc) / sizeof(*desc), desc, &result);
173     napi_set_named_property(env, exports, "ThermalLevel", result);
174     return exports;
175 }
176 
EnumThermalLevelConstructor(napi_env env,napi_callback_info info)177 napi_value ThermalManagerNapi::EnumThermalLevelConstructor(napi_env env, napi_callback_info info)
178 {
179     size_t argc = 0;
180     napi_value argv[ARG_1] = {0};
181     napi_value jsthis = nullptr;
182     void* data = nullptr;
183 
184     napi_status status = napi_get_cb_info(env, info, &argc, argv, &jsthis, &data);
185 
186     THERMAL_HILOGI(COMP_FWK, "EnumThermalLevelConstructor %{public}d", status);
187     if (status != napi_ok) {
188         return nullptr;
189     }
190     return jsthis;
191 }
192 
GetThermalLevel(napi_env env,napi_callback_info info)193 napi_value ThermalManagerNapi::GetThermalLevel(napi_env env, napi_callback_info info)
194 {
195     ThermalLevel level = g_thermalMgrClient.GetThermalLevel();
196     int32_t levelValue = static_cast<int32_t>(level);
197     napi_value napiValue;
198     NAPI_CALL(env, napi_create_int32(env, levelValue, &napiValue));
199 
200     THERMAL_HILOGI(COMP_FWK, "level is %{public}d", levelValue);
201     return napiValue;
202 }
203 
SubscribeThermalLevel(napi_env env,napi_callback_info info)204 napi_value ThermalManagerNapi::SubscribeThermalLevel(napi_env env, napi_callback_info info)
205 {
206     size_t argc = MAX_ARGC;
207     napi_value argv[argc];
208     NapiUtils::GetCallbackInfo(env, info, argc, argv);
209 
210     NapiErrors error;
211     if (argc != MAX_ARGC || !NapiUtils::CheckValueType(env, argv[ARG_0], napi_function)) {
212         return error.ThrowError(env, ThermalErrors::ERR_PARAM_INVALID);
213     }
214 
215     napi_value result;
216     napi_get_undefined(env, &result);
217 
218     THERMAL_RETURN_IF_WITH_RET(g_thermalLevelCallback == nullptr, result);
219     g_thermalLevelCallback->ReleaseCallback();
220     g_thermalLevelCallback->UpdateCallback(env, argv[ARG_0]);
221     g_thermalMgrClient.SubscribeThermalLevelCallback(g_thermalLevelCallback);
222 
223     return result;
224 }
225 
UnSubscribeThermalLevel(napi_env env,napi_callback_info info)226 napi_value ThermalManagerNapi::UnSubscribeThermalLevel(napi_env env, napi_callback_info info)
227 {
228     size_t argc = MAX_ARGC;
229     napi_value argv[argc];
230     NapiUtils::GetCallbackInfo(env, info, argc, argv);
231 
232     THERMAL_RETURN_IF_WITH_RET(g_thermalLevelCallback == nullptr, nullptr);
233     g_thermalLevelCallback->ReleaseCallback();
234     g_thermalMgrClient.UnSubscribeThermalLevelCallback(g_thermalLevelCallback);
235 
236     THERMAL_RETURN_IF_WITH_RET(argc == ARG_0, nullptr);
237     NapiErrors error;
238     if (argc > MAX_ARGC || !NapiUtils::CheckValueType(env, argv[ARG_0], napi_function)) {
239         return error.ThrowError(env, ThermalErrors::ERR_PARAM_INVALID);
240     }
241 
242     napi_value handler = nullptr;
243     napi_ref handlerRef = nullptr;
244     napi_create_reference(env, argv[ARG_0], 1, &handlerRef);
245     napi_get_reference_value(env, handlerRef, &handler);
246     napi_delete_reference(env, handlerRef);
247 
248     napi_value result = nullptr;
249     if (handler == nullptr) {
250         THERMAL_HILOGW(COMP_FWK, "Handler should not be nullptr");
251         return result;
252     }
253 
254     napi_get_undefined(env, &result);
255     napi_status status = napi_call_function(env, nullptr, handler, ARG_0, nullptr, &result);
256     if (status != napi_ok) {
257         THERMAL_HILOGW(COMP_FWK, "status=%{public}d", status);
258         return result;
259     }
260     return result;
261 }
262 
263 EXTERN_C_START
264 /*
265  * function for module exports
266  */
ThermalInit(napi_env env,napi_value exports)267 static napi_value ThermalInit(napi_env env, napi_value exports)
268 {
269     THERMAL_HILOGD(COMP_FWK, "Enter");
270 
271     napi_value ret = ThermalManagerNapi::Init(env, exports);
272 
273     THERMAL_HILOGD(COMP_FWK, "Exit");
274 
275     return ret;
276 }
277 EXTERN_C_END
278 
279 /*
280  * Module definition
281  */
282 static napi_module g_module = {.nm_version = 1,
283     .nm_flags = 0,
284     .nm_filename = "thermal",
285     .nm_register_func = ThermalInit,
286     .nm_modname = "thermal",
287     .nm_priv = ((void*)0),
288     .reserved = {0}};
289 
290 /*
291  * Module registration
292  */
RegisterModule()293 extern "C" __attribute__((constructor)) void RegisterModule()
294 {
295     napi_module_register(&g_module);
296 }
297