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