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