• 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 "thermal_common.h"
19 #include "thermal_level_info.h"
20 
21 using namespace OHOS::PowerMgr;
22 using namespace OHOS;
23 
24 namespace {
25 thread_local auto &g_thermalMgrClient = ThermalMgrClient::GetInstance();
26 thread_local ThermalManagerNapi *g_obj = nullptr;
27 const uint8_t ARG_0 = 0;
28 const uint8_t ARG_1 = 1;
29 const std::string THERMAL_NAPI_LEVEL_CHANGED = "LevelChanged";
30 }
31 
32 sptr<IThermalLevelCallback> ThermalManagerNapi::callback_ = nullptr;
33 napi_ref ThermalManagerNapi::thermalLevelConstructor_ = nullptr;
34 
GetThermalLevel(ThermalLevel level)35 void ThermalLevelCallback::GetThermalLevel(ThermalLevel level)
36 {
37     THERMAL_HILOGD(COMP_FWK, "Enter");
38     ThermalManagerNapi *thermalManagerNapi = ThermalManagerNapi::GetThermalManagerNapi();
39     if (thermalManagerNapi == nullptr) {
40         return;
41     }
42 
43     thermalManagerNapi->OnThermalLevelSucceed(level);
44     THERMAL_HILOGD(COMP_FWK, "Exit");
45 }
46 
47 
GetThermalManagerNapi()48 ThermalManagerNapi *ThermalManagerNapi::GetThermalManagerNapi()
49 {
50     return g_obj;
51 }
52 
ThermalManagerNapi(napi_env env,napi_value thisVar)53 ThermalManagerNapi::ThermalManagerNapi(napi_env env, napi_value thisVar) : ThermalManagerNativeEvent(env, thisVar)
54 {
55     env_ = env;
56     callbackRef_ = nullptr;
57 }
58 
~ThermalManagerNapi()59 ThermalManagerNapi::~ThermalManagerNapi()
60 {
61     if (callbackRef_ != nullptr) {
62         napi_delete_reference(env_, callbackRef_);
63     }
64 }
65 
OnThermalLevelSucceed(const ThermalLevel & level)66 void ThermalManagerNapi::OnThermalLevelSucceed(const ThermalLevel &level)
67 {
68     THERMAL_HILOGD(COMP_FWK, "level is: %{public}d", static_cast<int32_t>(level));
69     napi_value levelValue;
70     NAPI_CALL_RETURN_VOID(env_, napi_create_int32(env_, static_cast<int32_t>(level), &levelValue));
71     OnEvent(THERMAL_NAPI_LEVEL_CHANGED, ARG_1, &levelValue);
72     THERMAL_HILOGD(COMP_FWK, "Exit");
73 }
74 
Init(napi_env env,napi_value exports)75 napi_value ThermalManagerNapi::Init(napi_env env, napi_value exports)
76 {
77     THERMAL_HILOGD(COMP_FWK, "Enter");
78     napi_property_descriptor desc[] = {
79         DECLARE_NAPI_STATIC_FUNCTION("subscribeThermalLevel", SubscribeThermalLevel),
80         DECLARE_NAPI_STATIC_FUNCTION("unsubscribeThermalLevel", UnSubscribeThermalLevel),
81         DECLARE_NAPI_STATIC_FUNCTION("getThermalLevel", GetThermalLevel),
82     };
83     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
84     InitThermalLevel(env, exports);
85 
86     THERMAL_HILOGD(COMP_FWK, "Exit");
87     return exports;
88 }
89 
InitThermalLevel(napi_env env,napi_value exports)90 napi_value ThermalManagerNapi::InitThermalLevel(napi_env env, napi_value exports)
91 {
92     THERMAL_HILOGD(COMP_FWK, "Enter");
93     napi_value cool;
94     napi_value normal;
95     napi_value warm;
96     napi_value hot;
97     napi_value overheated;
98     napi_value warning;
99     napi_value emergency;
100     int32_t refCount = 1;
101 
102     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::COOL), &cool);
103     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::NORMAL), &normal);
104     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::WARM), &warm);
105     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::HOT), &hot);
106     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::OVERHEATED), &overheated);
107     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::WARNING), &warning);
108     napi_create_uint32(env, static_cast<uint32_t>(ThermalLevel::EMERGENCY), &emergency);
109 
110     napi_property_descriptor desc[] = {
111         DECLARE_NAPI_STATIC_PROPERTY("COOL", cool),
112         DECLARE_NAPI_STATIC_PROPERTY("NORMAL", normal),
113         DECLARE_NAPI_STATIC_PROPERTY("WARM", warm),
114         DECLARE_NAPI_STATIC_PROPERTY("HOT", hot),
115         DECLARE_NAPI_STATIC_PROPERTY("OVERHEATED", overheated),
116         DECLARE_NAPI_STATIC_PROPERTY("WARNING", warning),
117         DECLARE_NAPI_STATIC_PROPERTY("EMERGENCY", emergency),
118     };
119 
120     napi_value result = nullptr;
121     napi_define_class(env, "ThermalLevel", NAPI_AUTO_LENGTH, EnumThermalLevelConstructor,
122         nullptr, sizeof(desc) / sizeof(*desc), desc, &result);
123     napi_create_reference(env, result, refCount, &thermalLevelConstructor_);
124     napi_set_named_property(env, exports, "ThermalLevel", result);
125     THERMAL_HILOGD(COMP_FWK, "Exit");
126     return exports;
127 }
128 
EnumThermalLevelConstructor(napi_env env,napi_callback_info info)129 napi_value ThermalManagerNapi::EnumThermalLevelConstructor(napi_env env, napi_callback_info info)
130 {
131     THERMAL_HILOGD(COMP_FWK, "Enter");
132     size_t argc = 0;
133     napi_value args[ARG_1] = { 0 };
134     napi_value jsthis = nullptr;
135     void *data = nullptr;
136 
137     napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data);
138 
139     THERMAL_HILOGD(COMP_FWK, "EnumThermalLevelConstructor %{public}d", status);
140     if (status != napi_ok) {
141         return nullptr;
142     }
143     THERMAL_HILOGD(COMP_FWK, "Exit");
144     return jsthis;
145 }
146 
GetThermalLevel(napi_env env,napi_callback_info info)147 napi_value ThermalManagerNapi::GetThermalLevel(napi_env env, napi_callback_info info)
148 {
149     THERMAL_HILOGD(COMP_FWK, "Enter");
150     napi_value napiValue = nullptr;
151     ThermalLevel level = g_thermalMgrClient.GetThermalLevel();
152     int32_t levelValue = static_cast<int32_t>(level);
153     NAPI_CALL(env, napi_create_int32(env, levelValue, &napiValue));
154 
155     THERMAL_HILOGD(COMP_FWK, "level is %{public}d", levelValue);
156     THERMAL_HILOGD(COMP_FWK, "Exit");
157     return napiValue;
158 }
159 
SubscribeThermalLevel(napi_env env,napi_callback_info info)160 napi_value ThermalManagerNapi::SubscribeThermalLevel(napi_env env, napi_callback_info info)
161 {
162     THERMAL_HILOGD(COMP_FWK, "Enter");
163     size_t argc = ARG_1;
164     napi_value args[ARG_1] = {0};
165     napi_value jsthis;
166     void *data = nullptr;
167 
168     napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data);
169     NAPI_ASSERT(env, (status == napi_ok) && (argc >= 0), "Bad parameters");
170 
171     napi_valuetype valueType = napi_undefined;
172     napi_typeof(env, args[ARG_0], &valueType);
173     NAPI_ASSERT(env, valueType == napi_function, "type mismatch for parameter 1");
174 
175     g_obj = new ThermalManagerNapi(env, jsthis);
176     napi_wrap(env, jsthis, reinterpret_cast<void *>(g_obj),
177         [](napi_env env, void *data, void *hint) {
178             (void)env;
179             (void)hint;
180             ThermalManagerNapi *thermalManager = (ThermalManagerNapi *)data;
181             delete thermalManager;
182         },
183         nullptr, &(g_obj->callbackRef_));
184 
185     g_obj->On(THERMAL_NAPI_LEVEL_CHANGED, args[ARG_0]);
186     RegisterCallback(THERMAL_NAPI_LEVEL_CHANGED);
187 
188     napi_value result = nullptr;
189     napi_get_undefined(env, &result);
190     THERMAL_HILOGD(COMP_FWK, "Exit");
191     return result;
192 }
193 
UnSubscribeThermalLevel(napi_env env,napi_callback_info info)194 napi_value ThermalManagerNapi::UnSubscribeThermalLevel(napi_env env, napi_callback_info info)
195 {
196     THERMAL_HILOGD(COMP_FWK, "Enter");
197     size_t argc = ARG_1;
198     napi_value args[ARG_1] = { 0 };
199     napi_value jsthis;
200     void *data = nullptr;
201 
202     napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data);
203     NAPI_ASSERT(env, (status == napi_ok) && (argc >= 0), "Bad parameters");
204 
205     napi_valuetype valueType = napi_undefined;
206     napi_typeof(env, args[ARG_0], &valueType);
207     NAPI_ASSERT(env, valueType == napi_function, "type mismatch for parameter 1");
208 
209     if (callback_ != nullptr) {
210         g_thermalMgrClient.UnSubscribeThermalLevelCallback(callback_);
211     }
212     g_obj->Off(THERMAL_NAPI_LEVEL_CHANGED);
213 
214     napi_value handler = nullptr;
215     napi_ref handlerRef = nullptr;
216     napi_create_reference(env, args[ARG_0], 1, &handlerRef);
217     napi_get_reference_value(env, handlerRef, &handler);
218     napi_delete_reference(env, handlerRef);
219 
220     napi_value result = nullptr;
221     if (handler == nullptr) {
222         THERMAL_HILOGI(COMP_FWK, "Handler should not be NULL");
223         return result;
224     }
225 
226     napi_get_undefined(env, &result);
227     status = napi_call_function(env, nullptr, handler, ARG_0, nullptr, &result);
228     if (status != napi_ok) {
229         THERMAL_HILOGI(COMP_FWK, "status=%{public}d", status);
230         return result;
231     }
232     THERMAL_HILOGD(COMP_FWK, "Exit");
233     return result;
234 }
235 
RegisterCallback(const std::string & eventType)236 void ThermalManagerNapi::RegisterCallback(const std::string &eventType)
237 {
238     THERMAL_HILOGD(COMP_FWK, "Enter");
239     if (eventType == THERMAL_NAPI_LEVEL_CHANGED) {
240         if (callback_ == nullptr) {
241             callback_ = new ThermalLevelCallback();
242         }
243 
244         if (callback_ != nullptr) {
245             g_thermalMgrClient.SubscribeThermalLevelCallback(callback_);
246         }
247     }
248     THERMAL_HILOGD(COMP_FWK, "Exit");
249 }
250 
251 EXTERN_C_START
252 /*
253  * function for module exports
254  */
ThermalInit(napi_env env,napi_value exports)255 static napi_value ThermalInit(napi_env env, napi_value exports)
256 {
257     THERMAL_HILOGD(COMP_FWK, "Enter");
258 
259     napi_value ret = ThermalManagerNapi::Init(env, exports);
260 
261     THERMAL_HILOGD(COMP_FWK, "Exit");
262 
263     return ret;
264 }
265 EXTERN_C_END
266 
267 /*
268  * Module definition
269  */
270 static napi_module g_module = {
271     .nm_version = 1,
272     .nm_flags = 0,
273     .nm_filename = "thermal",
274     .nm_register_func = ThermalInit,
275     .nm_modname = "thermal",
276     .nm_priv = ((void *)0),
277     .reserved = {0}
278 };
279 
280 /*
281  * Module registration
282  */
RegisterModule(void)283 extern "C" __attribute__((constructor)) void RegisterModule(void)
284 {
285     napi_module_register(&g_module);
286 }
287