1 /*
2 * Copyright (c) 2022-2023 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 "devicestatus_napi.h"
17
18 #include <js_native_api.h>
19
20 #include "napi/native_api.h"
21 #include "napi/native_node_api.h"
22
23 #include "devicestatus_client.h"
24 #include "devicestatus_define.h"
25 #include "devicestatus_napi_error.h"
26 #include "stationary_manager.h"
27
28 #undef LOG_TAG
29 #define LOG_TAG "DeviceStatusNapi"
30
31 namespace OHOS {
32 namespace Msdp {
33 namespace DeviceStatus {
34 namespace {
35 constexpr size_t ARG_0 { 0 };
36 constexpr size_t ARG_1 { 1 };
37 constexpr size_t ARG_2 { 2 };
38 constexpr size_t ARG_3 { 3 };
39 constexpr size_t ARG_4 { 4 };
40 constexpr int32_t NAPI_BUF_LENGTH { 256 };
41 constexpr int32_t NANO { 1000000000 };
42 const std::vector<std::string> vecDeviceStatusValue {
43 "VALUE_ENTER", "VALUE_EXIT"
44 };
45 thread_local DeviceStatusNapi *g_obj = nullptr;
46 } // namespace
47 std::map<int32_t, sptr<IRemoteDevStaCallback>> DeviceStatusNapi::callbacks_;
48 napi_ref DeviceStatusNapi::devicestatusValueRef_ = nullptr;
49
OnDeviceStatusChanged(const Data & devicestatusData)50 void DeviceStatusCallback::OnDeviceStatusChanged(const Data& devicestatusData)
51 {
52 CALL_DEBUG_ENTER;
53 std::lock_guard<std::mutex> guard(mutex_);
54 FI_HILOGD("devicestatusData.type:%{public}d, devicestatusData.value:%{public}d",
55 devicestatusData.type, devicestatusData.value);
56 data_ = devicestatusData;
57
58 auto task = [this]() {
59 FI_HILOGI("Execute lamdba");
60 EmitOnEvent(&this->data_);
61 };
62 if (napi_status::napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) {
63 FI_HILOGE("Failed to SendEvent");
64 }
65 }
66
EmitOnEvent(Data * data)67 void DeviceStatusCallback::EmitOnEvent(Data* data)
68 {
69 CHKPV(data);
70 DeviceStatusNapi* deviceStatusNapi = DeviceStatusNapi::GetDeviceStatusNapi();
71 CHKPV(deviceStatusNapi);
72 int32_t type = static_cast<int32_t>(data->type);
73 int32_t value = static_cast<int32_t>(data->value);
74 FI_HILOGD("type:%{public}d, value:%{public}d", type, value);
75 deviceStatusNapi->OnDeviceStatusChangedDone(type, value, false);
76 }
77
GetDeviceStatusNapi()78 DeviceStatusNapi* DeviceStatusNapi::GetDeviceStatusNapi()
79 {
80 return g_obj;
81 }
82
DeviceStatusNapi(napi_env env)83 DeviceStatusNapi::DeviceStatusNapi(napi_env env) : DeviceStatusEvent(env)
84 {
85 env_ = env;
86 devicestatusValueRef_ = nullptr;
87 DeviceStatusClient::GetInstance().RegisterDeathListener([this] {
88 FI_HILOGI("Receive death notification");
89 callbacks_.clear();
90 ClearEventMap();
91 });
92 }
93
~DeviceStatusNapi()94 DeviceStatusNapi::~DeviceStatusNapi()
95 {
96 if (devicestatusValueRef_ != nullptr) {
97 napi_delete_reference(env_, devicestatusValueRef_);
98 }
99 }
100
OnDeviceStatusChangedDone(int32_t type,int32_t value,bool isOnce)101 void DeviceStatusNapi::OnDeviceStatusChangedDone(int32_t type, int32_t value, bool isOnce)
102 {
103 CALL_DEBUG_ENTER;
104 FI_HILOGD("value:%{public}d", value);
105 OnEvent(type, ARG_1, value, isOnce);
106 }
107
ConvertTypeToInt(const std::string & type)108 int32_t DeviceStatusNapi::ConvertTypeToInt(const std::string &type)
109 {
110 if (type == "absoluteStill") {
111 return Type::TYPE_ABSOLUTE_STILL;
112 } else if (type == "horizontalPosition") {
113 return Type::TYPE_HORIZONTAL_POSITION;
114 } else if (type == "verticalPosition") {
115 return Type::TYPE_VERTICAL_POSITION;
116 } else if (type == "still") {
117 return Type::TYPE_STILL;
118 } else if (type == "relativeStill") {
119 return Type::TYPE_RELATIVE_STILL;
120 } else if (type == "carBluetooth") {
121 return Type::TYPE_CAR_BLUETOOTH;
122 } else {
123 return Type::TYPE_INVALID;
124 }
125 }
126
CheckArguments(napi_env env,napi_callback_info info)127 bool DeviceStatusNapi::CheckArguments(napi_env env, napi_callback_info info)
128 {
129 CALL_DEBUG_ENTER;
130 int32_t arr[ARG_4] = { 0 };
131 size_t argc = ARG_4;
132 napi_value args[ARG_4] = { nullptr };
133 napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
134 if (status != napi_ok) {
135 FI_HILOGE("Failed to get_cb_info");
136 return false;
137 }
138 for (size_t i = 0; i < ARG_4; i++) {
139 napi_valuetype valueType = napi_undefined;
140 status = napi_typeof(env, args[i], &valueType);
141 if (status != napi_ok) {
142 FI_HILOGE("Failed to get valueType");
143 return false;
144 }
145 FI_HILOGD("valueType:%{public}d", valueType);
146 arr[i] = valueType;
147 }
148 if (arr[ARG_0] != napi_string || arr[ARG_1] != napi_number || arr[ARG_2] != napi_number ||
149 arr[ARG_3] != napi_function) {
150 FI_HILOGE("Failed to get arguements");
151 return false;
152 }
153 return true;
154 }
155
IsMatchType(napi_env env,napi_value value,napi_valuetype type)156 bool DeviceStatusNapi::IsMatchType(napi_env env, napi_value value, napi_valuetype type)
157 {
158 CALL_DEBUG_ENTER;
159 napi_valuetype valueType = napi_undefined;
160 napi_status status = napi_typeof(env, value, &valueType);
161 if (status != napi_ok) {
162 FI_HILOGE("Failed to get valueType");
163 return false;
164 }
165 return valueType == type;
166 }
167
CheckGetArguments(napi_env env,napi_callback_info info)168 bool DeviceStatusNapi::CheckGetArguments(napi_env env, napi_callback_info info)
169 {
170 CALL_DEBUG_ENTER;
171 int32_t arr[ARG_2] = { 0 };
172 size_t argc = ARG_2;
173 napi_value args[ARG_2] = { nullptr };
174 napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
175 if (status != napi_ok) {
176 FI_HILOGE("Failed to get_cb_info");
177 return false;
178 }
179 for (size_t i = 0; i < ARG_2; i++) {
180 napi_valuetype valueType = napi_undefined;
181 status = napi_typeof(env, args[i], &valueType);
182 if (status != napi_ok) {
183 FI_HILOGE("Failed to get valueType");
184 return false;
185 }
186 FI_HILOGD("valueType:%{public}d", valueType);
187 arr[i] = valueType;
188 }
189 if (arr[ARG_0] != napi_string || arr[ARG_1] != napi_function) {
190 FI_HILOGE("Failed to get arguements");
191 return false;
192 }
193 return true;
194 }
195
CheckSubscribeParam(napi_env env,napi_callback_info info)196 std::tuple<bool, napi_value, std::string, int32_t, int32_t> DeviceStatusNapi::CheckSubscribeParam(napi_env env,
197 napi_callback_info info)
198 {
199 std::tuple<bool, napi_value, std::string, int32_t, int64_t> result { false, nullptr, "", -1, -1 };
200 size_t argc = ARG_4;
201 napi_value args[ARG_4] = { nullptr };
202 napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
203 if ((status != napi_ok) || (argc < ARG_4)) {
204 ThrowErr(env, PARAM_ERROR, "Bad parameters");
205 return result;
206 }
207 if (!CheckArguments(env, info)) {
208 ThrowErr(env, PARAM_ERROR, "Failed to get on arguments");
209 return result;
210 }
211 size_t modLen = 0;
212 status = napi_get_value_string_utf8(env, args[ARG_0], nullptr, 0, &modLen);
213 if (status != napi_ok) {
214 ThrowErr(env, PARAM_ERROR, "Failed to get string item");
215 return result;
216 }
217 char mode[NAPI_BUF_LENGTH] = { 0 };
218 status = napi_get_value_string_utf8(env, args[ARG_0], mode, modLen + 1, &modLen);
219 if (status != napi_ok) {
220 ThrowErr(env, PARAM_ERROR, "Failed to get mode");
221 return result;
222 }
223 int32_t eventMode = 0;
224 status = napi_get_value_int32(env, args[ARG_1], &eventMode);
225 if (status != napi_ok) {
226 ThrowErr(env, PARAM_ERROR, "Failed to get event value item");
227 return result;
228 }
229 int64_t latencyMode = 0;
230 status = napi_get_value_int64(env, args[ARG_2], &latencyMode);
231 if (status != napi_ok) {
232 ThrowErr(env, PARAM_ERROR, "Failed to get latency value item");
233 return result;
234 }
235 latencyMode = latencyMode / NANO;
236 return std::make_tuple(true, args[ARG_3], std::string(mode), eventMode, latencyMode);
237 }
238
CheckGetParam(napi_env env,napi_callback_info info)239 std::tuple<bool, napi_value, int32_t> DeviceStatusNapi::CheckGetParam(napi_env env, napi_callback_info info)
240 {
241 std::tuple<bool, napi_value, int32_t> result { false, nullptr, -1 };
242 size_t argc = ARG_2;
243 napi_value args[ARG_2] = { nullptr };
244 napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
245 if ((status != napi_ok) || (argc < ARG_2)) {
246 ThrowErr(env, PARAM_ERROR, "Bad parameters");
247 return result;
248 }
249 if (!CheckGetArguments(env, info)) {
250 ThrowErr(env, PARAM_ERROR, "Failed to get once arguments");
251 return result;
252 }
253 size_t modLen = 0;
254 napi_status napiStatus = napi_get_value_string_utf8(env, args[ARG_0], nullptr, 0, &modLen);
255 if (napiStatus != napi_ok) {
256 ThrowErr(env, PARAM_ERROR, "Failed to get string item");
257 return result;
258 }
259 char mode[NAPI_BUF_LENGTH] = { 0 };
260 napiStatus = napi_get_value_string_utf8(env, args[ARG_0], mode, modLen + 1, &modLen);
261 if (napiStatus != napi_ok) {
262 ThrowErr(env, PARAM_ERROR, "Failed to get mode");
263 return result;
264 }
265 int32_t type = ConvertTypeToInt(mode);
266 if ((type != Type::TYPE_STILL) && (type != Type::TYPE_RELATIVE_STILL)) {
267 ThrowErr(env, PARAM_ERROR, "Type is illegal");
268 return result;
269 }
270 return std::make_tuple(true, args[ARG_1], type);
271 }
272
GetParameters(napi_env env,size_t argc,const napi_value * args)273 napi_value DeviceStatusNapi::GetParameters(napi_env env, size_t argc, const napi_value* args)
274 {
275 CALL_DEBUG_ENTER;
276 size_t modLen = 0;
277 napi_status status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &modLen);
278 if (status != napi_ok) {
279 ThrowErr(env, PARAM_ERROR, "Failed to get string item");
280 return nullptr;
281 }
282 char mode[NAPI_BUF_LENGTH] = { 0 };
283 status = napi_get_value_string_utf8(env, args[0], mode, modLen + 1, &modLen);
284 if (status != napi_ok) {
285 ThrowErr(env, PARAM_ERROR, "Failed to get mode");
286 return nullptr;
287 }
288 int32_t type = DeviceStatusNapi::ConvertTypeToInt(mode);
289 if ((type != Type::TYPE_STILL) && (type != Type::TYPE_RELATIVE_STILL)) {
290 ThrowErr(env, PARAM_ERROR, "Type is illegal");
291 return nullptr;
292 }
293 int32_t event = 0;
294 status = napi_get_value_int32(env, args[1], &event);
295 if (status != napi_ok) {
296 ThrowErr(env, PARAM_ERROR, "Failed to get int32 item");
297 return nullptr;
298 }
299 if ((event < ActivityEvent::ENTER) || (event > ActivityEvent::ENTER_EXIT)) {
300 ThrowErr(env, PARAM_ERROR, "Event is illegal");
301 return nullptr;
302 }
303 CHKPP(g_obj);
304 if ((argc < 3) || IsMatchType(env, args[2], napi_undefined) || IsMatchType(env, args[2], napi_null)) {
305 if (!g_obj->RemoveAllCallback(type)) {
306 FI_HILOGE("Callback type is not exist");
307 return nullptr;
308 }
309 UnsubscribeCallback(env, type, event);
310 return nullptr;
311 }
312 FI_HILOGD("type:%{public}d, event:%{public}d", type, event);
313 if (!IsMatchType(env, args[2], napi_function)) {
314 ThrowErr(env, PARAM_ERROR, "get error callback type");
315 return nullptr;
316 }
317 if (!g_obj->Off(type, args[2])) {
318 FI_HILOGE("Not ready to unsubscribe for type:%{public}d", type);
319 return nullptr;
320 }
321 UnsubscribeCallback(env, type, event);
322 return nullptr;
323 }
324
SubscribeDeviceStatusCallback(napi_env env,napi_callback_info info,napi_value handler,int32_t type,int32_t event,int32_t latency)325 napi_value DeviceStatusNapi::SubscribeDeviceStatusCallback(napi_env env, napi_callback_info info, napi_value handler,
326 int32_t type, int32_t event, int32_t latency)
327 {
328 CALL_DEBUG_ENTER;
329 if (g_obj == nullptr) {
330 g_obj = new (std::nothrow) DeviceStatusNapi(env);
331 CHKPP(g_obj);
332 FI_HILOGD("Didn't find object, so created it");
333 }
334 napi_value thisArg = nullptr;
335 void *data = nullptr;
336 napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
337 if (status != napi_ok) {
338 FI_HILOGE("Failed to get_cb_info item");
339 delete g_obj;
340 g_obj = nullptr;
341 return nullptr;
342 }
343 status = napi_wrap(env, thisArg, reinterpret_cast<void *>(g_obj),
344 [](napi_env env, void *data, void *hint) {
345 (void)env;
346 (void)hint;
347 CHKPV(data);
348 DeviceStatusNapi *devicestatus = static_cast<DeviceStatusNapi *>(data);
349 delete devicestatus;
350 g_obj = nullptr;
351 },
352 nullptr, nullptr);
353 if (status != napi_ok) {
354 FI_HILOGE("napi_wrap failed");
355 delete g_obj;
356 g_obj = nullptr;
357 return nullptr;
358 }
359 if (!g_obj->On(type, handler, false)) {
360 FI_HILOGE("type:%{public}d already exists", type);
361 return nullptr;
362 }
363 std::lock_guard<std::mutex> guard(g_obj->mutex_);
364 auto callbackIter = callbacks_.find(type);
365 if (callbackIter != callbacks_.end()) {
366 FI_HILOGD("Callback exists");
367 return nullptr;
368 }
369 sptr<IRemoteDevStaCallback> callback = new (std::nothrow) DeviceStatusCallback(env);
370 CHKPP(callback);
371 int32_t subscribeRet = StationaryManager::GetInstance()->SubscribeCallback(static_cast<Type>(type),
372 static_cast<ActivityEvent>(event), static_cast<ReportLatencyNs>(latency), callback);
373 if (subscribeRet != RET_OK) {
374 ThrowErr(env, SERVICE_EXCEPTION, "On:Failed to SubscribeCallback");
375 return nullptr;
376 }
377 auto ret = callbacks_.insert(std::pair<int32_t, sptr<IRemoteDevStaCallback>>(type, callback));
378 if (!ret.second) {
379 FI_HILOGE("Failed to insert");
380 }
381 return nullptr;
382 }
383
SubscribeDeviceStatus(napi_env env,napi_callback_info info)384 napi_value DeviceStatusNapi::SubscribeDeviceStatus(napi_env env, napi_callback_info info)
385 {
386 CALL_DEBUG_ENTER;
387 const auto [ret, handler, typeMode, event, latency] = CheckSubscribeParam(env, info);
388 if (!ret) {
389 FI_HILOGE("On:Failed to SubscribeDeviceStatus");
390 return nullptr;
391 }
392 int32_t type = ConvertTypeToInt(typeMode);
393 FI_HILOGD("type:%{public}d, event:%{public}d, latency:%{public}d", type, event, latency);
394 if ((type != Type::TYPE_STILL) && (type != Type::TYPE_RELATIVE_STILL)) {
395 ThrowErr(env, PARAM_ERROR, "Type is illegal");
396 return nullptr;
397 }
398 if ((event < ActivityEvent::ENTER) || (event > ActivityEvent::ENTER_EXIT)) {
399 ThrowErr(env, PARAM_ERROR, "Event is illegal");
400 return nullptr;
401 }
402 if ((latency < ReportLatencyNs::SHORT) || (latency > ReportLatencyNs::LONG)) {
403 ThrowErr(env, PARAM_ERROR, "Latency is illegal");
404 return nullptr;
405 }
406 return SubscribeDeviceStatusCallback(env, info, handler, type, event, latency);
407 }
408
UnsubscribeDeviceStatus(napi_env env,napi_callback_info info)409 napi_value DeviceStatusNapi::UnsubscribeDeviceStatus(napi_env env, napi_callback_info info)
410 {
411 CALL_DEBUG_ENTER;
412 size_t argc = 3;
413 napi_value args[3] = { nullptr };
414 napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
415 if (status != napi_ok) {
416 ThrowErr(env, PARAM_ERROR, "Bad parameters");
417 return nullptr;
418 }
419 if (argc < 2) {
420 ThrowErr(env, PARAM_ERROR, "Param number is invalid");
421 return nullptr;
422 }
423 return GetParameters(env, argc, args);
424 }
425
UnsubscribeCallback(napi_env env,int32_t type,int32_t event)426 napi_value DeviceStatusNapi::UnsubscribeCallback(napi_env env, int32_t type, int32_t event)
427 {
428 CALL_DEBUG_ENTER;
429 std::lock_guard<std::mutex> guard(g_obj->mutex_);
430 auto callbackIter = callbacks_.find(type);
431 if (callbackIter == callbacks_.end()) {
432 NAPI_ASSERT(env, false, "No existed callback");
433 return nullptr;
434 }
435 int32_t unsubscribeRet = StationaryManager::GetInstance()->UnsubscribeCallback(static_cast<Type>(type),
436 static_cast<ActivityEvent>(event), callbackIter->second);
437 if (unsubscribeRet != RET_OK) {
438 ThrowErr(env, SERVICE_EXCEPTION, "Off:Failed to UnsubscribeCallback");
439 }
440 callbacks_.erase(type);
441 return nullptr;
442 }
443
GetDeviceStatus(napi_env env,napi_callback_info info)444 napi_value DeviceStatusNapi::GetDeviceStatus(napi_env env, napi_callback_info info)
445 {
446 CALL_DEBUG_ENTER;
447 const auto [ret, handler, type] = CheckGetParam(env, info);
448 if (!ret) {
449 FI_HILOGE("Once:Failed to GetDeviceStatus");
450 return nullptr;
451 }
452 napi_value thisArg = nullptr;
453 void *data = nullptr;
454 napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
455 if (status != napi_ok) {
456 FI_HILOGE("Failed to get_cb_info item");
457 delete g_obj;
458 g_obj = nullptr;
459 return nullptr;
460 }
461 if (g_obj == nullptr) {
462 g_obj = new (std::nothrow) DeviceStatusNapi(env);
463 CHKPP(g_obj);
464 status = napi_wrap(env, thisArg, reinterpret_cast<void *>(g_obj),
465 [](napi_env env, void *data, void *hint) {
466 (void)env;
467 (void)hint;
468 CHKPV(data);
469 DeviceStatusNapi *devicestatus = static_cast<DeviceStatusNapi *>(data);
470 delete devicestatus;
471 g_obj = nullptr;
472 },
473 nullptr, nullptr);
474 if (status != napi_ok) {
475 FI_HILOGE("napi_wrap failed");
476 delete g_obj;
477 g_obj = nullptr;
478 return nullptr;
479 }
480 }
481 if (!g_obj->On(type, handler, true)) {
482 FI_HILOGE("type:%{public}d already exists", type);
483 return nullptr;
484 }
485 Data devicestatusData = StationaryManager::GetInstance()->GetDeviceStatusData(static_cast<Type>(type));
486 if (devicestatusData.type == Type::TYPE_INVALID) {
487 ThrowErr(env, SERVICE_EXCEPTION, "Once:Failed to get device status data");
488 }
489 g_obj->OnDeviceStatusChangedDone(devicestatusData.type, devicestatusData.value, true);
490 g_obj->OffOnce(devicestatusData.type, handler);
491 return nullptr;
492 }
493
EnumActivityEventConstructor(napi_env env,napi_callback_info info)494 napi_value DeviceStatusNapi::EnumActivityEventConstructor(napi_env env, napi_callback_info info)
495 {
496 CALL_DEBUG_ENTER;
497 napi_value thisArg = nullptr;
498 void *data = nullptr;
499 napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
500 if (status != napi_ok) {
501 FI_HILOGE("Failed to get_cb_info item");
502 return nullptr;
503 }
504 napi_value global = nullptr;
505 status = napi_get_global(env, &global);
506 if (status != napi_ok) {
507 FI_HILOGE("Failed to get_global item");
508 return nullptr;
509 }
510 return thisArg;
511 }
512
DeclareEventTypeInterface(napi_env env,napi_value exports)513 napi_value DeviceStatusNapi::DeclareEventTypeInterface(napi_env env, napi_value exports)
514 {
515 CALL_DEBUG_ENTER;
516 napi_value enter = nullptr;
517 napi_status status = napi_create_int32(env, static_cast<int32_t>(ActivityEvent::ENTER), &enter);
518 if (status != napi_ok) {
519 FI_HILOGE("Failed to create ENTER item");
520 return nullptr;
521 }
522 napi_value exit = nullptr;
523 status = napi_create_int32(env, static_cast<int32_t>(ActivityEvent::EXIT), &exit);
524 if (status != napi_ok) {
525 FI_HILOGE("Failed to create EXIT item");
526 return nullptr;
527 }
528 napi_value enter_exit = nullptr;
529 status = napi_create_int32(env, static_cast<int32_t>(ActivityEvent::ENTER_EXIT), &enter_exit);
530 if (status != napi_ok) {
531 FI_HILOGE("Failed to create ENTER_EXIT item");
532 return nullptr;
533 }
534 napi_property_descriptor desc[] = {
535 DECLARE_NAPI_STATIC_PROPERTY("ENTER", enter),
536 DECLARE_NAPI_STATIC_PROPERTY("EXIT", exit),
537 DECLARE_NAPI_STATIC_PROPERTY("ENTER_EXIT", enter_exit)
538 };
539 napi_value result = nullptr;
540 status = napi_define_class(env, "ActivityEvent", NAPI_AUTO_LENGTH,
541 EnumActivityEventConstructor, nullptr, sizeof(desc) / sizeof(*desc), desc, &result);
542 if (status != napi_ok) {
543 FI_HILOGE("Failed to define_class item");
544 return nullptr;
545 }
546 status = napi_set_named_property(env, exports, "ActivityEvent", result);
547 if (status != napi_ok) {
548 FI_HILOGE("Failed to set_named_property item");
549 return nullptr;
550 }
551 return exports;
552 }
553
Init(napi_env env,napi_value exports)554 napi_value DeviceStatusNapi::Init(napi_env env, napi_value exports)
555 {
556 CALL_DEBUG_ENTER;
557 napi_property_descriptor desc[] = {
558 DECLARE_NAPI_FUNCTION("on", SubscribeDeviceStatus),
559 DECLARE_NAPI_FUNCTION("off", UnsubscribeDeviceStatus),
560 DECLARE_NAPI_FUNCTION("once", GetDeviceStatus)
561 };
562 DeclareEventTypeInterface(env, exports);
563 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
564 return exports;
565 }
566
567 EXTERN_C_START
568 /*
569 * function for module exports
570 */
DeviceStatusInit(napi_env env,napi_value exports)571 static napi_value DeviceStatusInit(napi_env env, napi_value exports)
572 {
573 CALL_DEBUG_ENTER;
574 napi_value ret = DeviceStatusNapi::Init(env, exports);
575 return ret;
576 }
577 EXTERN_C_END
578
579 /*
580 * Module definition
581 */
582 static napi_module g_module = {
583 .nm_version = 1,
584 .nm_flags = 0,
585 .nm_filename = "stationary",
586 .nm_register_func = DeviceStatusInit,
587 .nm_modname = "stationary",
588 .nm_priv = (static_cast<void *>(0)),
589 .reserved = {0}
590 };
591
592 /*
593 * Module registration
594 */
RegisterModule(void)595 extern "C" __attribute__((constructor)) void RegisterModule(void)
596 {
597 napi_module_register(&g_module);
598 }
599 } // namespace DeviceStatus
600 } // namespace Msdp
601 } // namespace OHOS
602