diff --git a/interfaces/kits/js/src/native_devicemanager_js.cpp b/interfaces/kits/js/src/native_devicemanager_js.cpp index c56e4138132422427ce314d04343b49614f6bd9a..eb631c32843d3dbfe99ed2c5ee143c9a2e790ea8 100644 --- a/interfaces/kits/js/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js/src/native_devicemanager_js.cpp @@ -46,6 +46,7 @@ const int32_t DM_NAPI_ARGS_TWO = 2; const int32_t DM_NAPI_ARGS_THREE = 3; const int32_t DM_NAPI_SUB_ID_MAX = 65535; +napi_ref deviceStateTypeConstructor_ = nullptr; std::map g_deviceManagerMap; std::map> g_initCallbackMap; @@ -1928,6 +1929,53 @@ napi_value DeviceManagerNapi::Init(napi_env env, napi_value exports) return exports; } +napi_value DeviceStateTypeConstructor(napi_env env, napi_callback_info info) +{ + size_t argc = 0; + napi_value args[DM_NAPI_ARGS_ONE] = {0}; + napi_value res = nullptr; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, args, &res, &data); + if (status != napi_ok) { + return nullptr; + } + + return res; +} +napi_value InitDeviceStateType(napi_env env, napi_value exports) +{ + napi_value device_state_online; + napi_value device_state_ready; + napi_value device_state_offline; + napi_value device_state_change; + int32_t refCount = 1; + + napi_create_uint32(env, static_cast(DmDeviceState::DEVICE_STATE_ONLINE), + &device_state_online); + napi_create_uint32(env, static_cast(DmDeviceState::DEVICE_INFO_READY), + &device_state_ready); + napi_create_uint32(env, static_cast(DmDeviceState::DEVICE_STATE_OFFLINE), + &device_state_offline); + napi_create_uint32(env, static_cast(DmDeviceState::DEVICE_INFO_CHANGED), + &device_state_change); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("ONLINE", device_state_online), + DECLARE_NAPI_STATIC_PROPERTY("READY", device_state_ready), + DECLARE_NAPI_STATIC_PROPERTY("OFFLINE", device_state_offline), + DECLARE_NAPI_STATIC_PROPERTY("CHANGE", device_state_change), + }; + + napi_value result = nullptr; + napi_define_class(env, "DeviceStateChangeAction", NAPI_AUTO_LENGTH, DeviceStateTypeConstructor, + nullptr, sizeof(desc) / sizeof(*desc), desc, &result); + napi_create_reference(env, result, refCount, &deviceStateTypeConstructor_); + napi_set_named_property(env, exports, "DeviceStateChangeAction", result); + return exports; +} + +///////////////////////////////////////////////////////////// /* * Function registering all props and functions of ohos.distributedhardware */ @@ -1935,6 +1983,7 @@ static napi_value Export(napi_env env, napi_value exports) { LOGI("Export() is called!"); DeviceManagerNapi::Init(env, exports); + InitDeviceStateType(env, exports); return exports; }