From 2d9666e742a48c88d7466a35d035a547a10b1138 Mon Sep 17 00:00:00 2001 From: wuqi0105 Date: Tue, 8 Mar 2022 15:26:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9deviceState=E6=9E=9A=E4=B8=BE?= =?UTF-8?q?for=20hap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuqi0105 --- .../kits/js/include/native_devicemanager_js.h | 46 +-- .../kits/js/src/native_devicemanager_js.cpp | 359 ++++++++++++++---- 2 files changed, 286 insertions(+), 119 deletions(-) diff --git a/interfaces/kits/js/include/native_devicemanager_js.h b/interfaces/kits/js/include/native_devicemanager_js.h index 9d895b1c6..e0469adf9 100644 --- a/interfaces/kits/js/include/native_devicemanager_js.h +++ b/interfaces/kits/js/include/native_devicemanager_js.h @@ -80,39 +80,6 @@ struct AuthAsyncCallbackInfo { int32_t authType = -1; }; -struct DmNapiStateJsCallback { - std::string bundleName_; - uint16_t subscribeId_; - int32_t reason_; - OHOS::DistributedHardware::DmDeviceInfo deviceInfo_; - - DmNapiStateJsCallback(std::string bundleName, uint16_t subscribeId, int32_t reason, - OHOS::DistributedHardware::DmDeviceInfo deviceInfo) - : bundleName_(bundleName), subscribeId_(subscribeId), reason_(reason), deviceInfo_(deviceInfo) {} -}; - -struct DmNapiAuthJsCallback { - std::string bundleName_; - std::string deviceId_; - std::string token_; - int32_t status_; - int32_t reason_; - - DmNapiAuthJsCallback(std::string bundleName, std::string deviceId, std::string token, int32_t status, - int32_t reason) - : bundleName_(bundleName), deviceId_(deviceId), token_(token), status_(status), reason_(reason) {} -}; - -struct DmNapiVerifyJsCallback { - std::string bundleName_; - std::string deviceId_; - int32_t resultCode_; - int32_t flag_; - - DmNapiVerifyJsCallback(std::string bundleName, std::string deviceId, int32_t resultCode, int32_t flag) - : bundleName_(bundleName), deviceId_(deviceId), resultCode_(resultCode), flag_(flag) {} -}; - enum DmNapiDevStateChangeAction { ONLINE = 0, READY = 1, OFFLINE = 2, CHANGE = 3 }; class DmNapiInitCallback : public OHOS::DistributedHardware::DmInitCallback { @@ -128,7 +95,6 @@ public: private: napi_env env_; std::string bundleName_; - std::unique_ptr jsCallback_; }; class DmNapiDeviceStateCallback : public OHOS::DistributedHardware::DeviceStateCallback { @@ -145,7 +111,6 @@ public: private: napi_env env_; std::string bundleName_; - std::unique_ptr jsCallback_; }; class DmNapiDiscoveryCallback : public OHOS::DistributedHardware::DiscoveryCallback { @@ -166,7 +131,6 @@ private: napi_env env_; std::atomic refCount_; std::string bundleName_; - std::unique_ptr jsCallback_; }; class DmNapiDeviceManagerFaCallback : public OHOS::DistributedHardware::DeviceManagerFaCallback { @@ -180,7 +144,6 @@ public: private: napi_env env_; std::string bundleName_; - std::unique_ptr jsCallback_; }; class DmNapiAuthenticateCallback : public OHOS::DistributedHardware::AuthenticateCallback { @@ -194,7 +157,6 @@ public: private: napi_env env_; std::string bundleName_; - std::unique_ptr jsCallback_; }; class DmNapiVerifyAuthCallback : public OHOS::DistributedHardware::VerifyAuthCallback { @@ -208,7 +170,6 @@ public: private: napi_env env_; std::string bundleName_; - std::unique_ptr jsCallback_; }; class DeviceManagerNapi : public DmNativeEvent { @@ -217,6 +178,13 @@ public: virtual ~DeviceManagerNapi(); static napi_value Init(napi_env env, napi_value exports); static napi_value Constructor(napi_env env, napi_callback_info info); + static napi_value EnumTypeConstructor(napi_env env, napi_callback_info info); + static napi_value InitDeviceTypeEnum(napi_env env, napi_value exports); + static napi_value InitDeviceStateChangeActionEnum(napi_env env, napi_value exports); + static napi_value InitDiscoverModeEnum(napi_env env, napi_value exports); + static napi_value InitExchangeMediumEnum(napi_env env, napi_value exports); + static napi_value InitExchangeFreqEnum(napi_env env, napi_value exports); + static napi_value InitSubscribeCapEnum(napi_env env, napi_value exports); static napi_value CreateDeviceManager(napi_env env, napi_callback_info info); static napi_value ReleaseDeviceManager(napi_env env, napi_callback_info info); static napi_value SetUserOperationSync(napi_env env, napi_callback_info info); diff --git a/interfaces/kits/js/src/native_devicemanager_js.cpp b/interfaces/kits/js/src/native_devicemanager_js.cpp index b6cec4792..876225df5 100644 --- a/interfaces/kits/js/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js/src/native_devicemanager_js.cpp @@ -46,6 +46,12 @@ 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 deviceTypeEnumConstructor_ = nullptr; +napi_ref deviceStateChangeActionEnumConstructor_ = nullptr; +napi_ref discoverModeEnumConstructor_ = nullptr; +napi_ref exchangeMediumEnumConstructor_ = nullptr; +napi_ref exchangeFreqEnumConstructor_ = nullptr; +napi_ref subscribeCapEnumConstructor_ = nullptr; std::map g_deviceManagerMap; std::map> g_initCallbackMap; @@ -68,20 +74,16 @@ void DmNapiInitCallback::OnRemoteDied() return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { + if (loop == nullptr) { LOGE("DmNapiInitCallback: OnRemoteDied, No memory"); return; } - DmDeviceInfo info; - jsCallback_ = std::make_unique(bundleName_, 0, 0, info); - work->data = reinterpret_cast(jsCallback_.get()); - + static std::string staticBundleName = bundleName_; int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DmNapiStateJsCallback *callback = reinterpret_cast(work->data); - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); if (deviceManagerNapi == nullptr) { - LOGE("OnRemoteDied, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); + LOGE("OnRemoteDied, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); return; } deviceManagerNapi->OnEvent("serviceDie", 0, nullptr); @@ -101,22 +103,20 @@ void DmNapiDeviceStateCallback::OnDeviceOnline(const DmDeviceInfo &deviceInfo) return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { + if (loop == nullptr) { LOGE("DmNapiDeviceStateCallback: OnDeviceOnline, No memory"); return; } - jsCallback_ = std::make_unique(bundleName_, 0, 0, deviceInfo); - work->data = reinterpret_cast(jsCallback_.get()); - + static std::string staticBundleName = bundleName_; + static DmDeviceInfo staticDeviceInfo = deviceInfo; int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DmNapiStateJsCallback *callback = reinterpret_cast(work->data); - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); if (deviceManagerNapi == nullptr) { - LOGE("OnDeviceOnline, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); + LOGE("OnDeviceOnline, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); return; } - deviceManagerNapi->OnDeviceStateChange(DmNapiDevStateChangeAction::ONLINE, callback->deviceInfo_); + deviceManagerNapi->OnDeviceStateChange(DmNapiDevStateChangeAction::ONLINE, staticDeviceInfo); delete work; }); if (ret != 0) { @@ -133,22 +133,20 @@ void DmNapiDeviceStateCallback::OnDeviceReady(const DmDeviceInfo &deviceInfo) return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { + if (loop == nullptr) { LOGE("DmNapiDeviceStateCallback: OnDeviceReady, No memory"); return; } - jsCallback_ = std::make_unique(bundleName_, 0, 0, deviceInfo); - work->data = reinterpret_cast(jsCallback_.get()); - + static std::string staticBundleName = bundleName_; + static DmDeviceInfo staticDeviceInfo = deviceInfo; int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DmNapiStateJsCallback *callback = reinterpret_cast(work->data); - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); if (deviceManagerNapi == nullptr) { - LOGE("OnDeviceReady, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); + LOGE("OnDeviceReady, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); return; } - deviceManagerNapi->OnDeviceStateChange(DmNapiDevStateChangeAction::READY, callback->deviceInfo_); + deviceManagerNapi->OnDeviceStateChange(DmNapiDevStateChangeAction::READY, staticDeviceInfo); delete work; }); if (ret != 0) { @@ -165,22 +163,20 @@ void DmNapiDeviceStateCallback::OnDeviceOffline(const DmDeviceInfo &deviceInfo) return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { + if (loop == nullptr) { LOGE("DmNapiDeviceStateCallback: OnDeviceOffline, No memory"); return; } - jsCallback_ = std::make_unique(bundleName_, 0, 0, deviceInfo); - work->data = reinterpret_cast(jsCallback_.get()); - + static std::string staticBundleName = bundleName_; + static DmDeviceInfo staticDeviceInfo = deviceInfo; int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DmNapiStateJsCallback *callback = reinterpret_cast(work->data); - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); if (deviceManagerNapi == nullptr) { - LOGE("OnDeviceOffline, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); + LOGE("OnDeviceOffline, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); return; } - deviceManagerNapi->OnDeviceStateChange(DmNapiDevStateChangeAction::OFFLINE, callback->deviceInfo_); + deviceManagerNapi->OnDeviceStateChange(DmNapiDevStateChangeAction::OFFLINE, staticDeviceInfo); delete work; }); if (ret != 0) { @@ -202,17 +198,15 @@ void DmNapiDeviceStateCallback::OnDeviceChanged(const DmDeviceInfo &deviceInfo) return; } - jsCallback_ = std::make_unique(bundleName_, 0, 0, deviceInfo); - work->data = reinterpret_cast(jsCallback_.get()); - + static std::string staticBundleName = bundleName_; + static DmDeviceInfo staticDeviceInfo = deviceInfo; int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DmNapiStateJsCallback *callback = reinterpret_cast(work->data); - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); if (deviceManagerNapi == nullptr) { - LOGE("OnDeviceChanged, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); + LOGE("OnDeviceChanged, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); return; } - deviceManagerNapi->OnDeviceStateChange(DmNapiDevStateChangeAction::CHANGE, callback->deviceInfo_); + deviceManagerNapi->OnDeviceStateChange(DmNapiDevStateChangeAction::CHANGE, staticDeviceInfo); delete work; }); if (ret != 0) { @@ -231,22 +225,21 @@ void DmNapiDiscoveryCallback::OnDeviceFound(uint16_t subscribeId, const DmDevice return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { + if (loop == nullptr) { LOGE("DmNapiDiscoveryCallback: OnDeviceFound, No memory"); return; } - jsCallback_ = std::make_unique(bundleName_, subscribeId, 0, deviceInfo); - work->data = reinterpret_cast(jsCallback_.get()); - + static std::string staticBundleName = bundleName_; + static uint16_t staticSubscribeId = subscribeId; + static DmDeviceInfo staticDeviceInfo = deviceInfo; int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DmNapiStateJsCallback *callback = reinterpret_cast(work->data); - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); if (deviceManagerNapi == nullptr) { - LOGE("OnDeviceFound, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); + LOGE("OnDeviceFound, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); return; } - deviceManagerNapi->OnDeviceFound(callback->subscribeId_, callback->deviceInfo_); + deviceManagerNapi->OnDeviceFound(staticSubscribeId, staticDeviceInfo); delete work; }); if (ret != 0) { @@ -265,23 +258,21 @@ void DmNapiDiscoveryCallback::OnDiscoveryFailed(uint16_t subscribeId, int32_t fa return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { + if (loop == nullptr) { LOGE("DmNapiDiscoveryCallback: OnDiscoveryFailed, No memory"); return; } - DmDeviceInfo info; - jsCallback_ = std::make_unique(bundleName_, subscribeId, failedReason, info); - work->data = reinterpret_cast(jsCallback_.get()); - + static std::string staticBundleName = bundleName_; + static uint16_t staticSubscribeId = subscribeId; + static int32_t staticReason = failedReason; int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DmNapiStateJsCallback *callback = reinterpret_cast(work->data); - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); if (deviceManagerNapi == nullptr) { - LOGE("OnDiscoveryFailed, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); + LOGE("OnDiscoveryFailed, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); return; } - deviceManagerNapi->OnDiscoveryFailed(callback->subscribeId_, callback->reason_); + deviceManagerNapi->OnDiscoveryFailed(staticSubscribeId, staticReason); delete work; }); if (ret != 0) { @@ -324,22 +315,23 @@ void DmNapiAuthenticateCallback::OnAuthResult(const std::string &deviceId, const return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { + if (loop == nullptr) { LOGE("DmNapiAuthenticateCallback: OnAuthResult, No memory"); return; } - jsCallback_ = std::make_unique(bundleName_, deviceId, token, status, reason); - work->data = reinterpret_cast(jsCallback_.get()); - + static std::string staticBundleName = bundleName_; + static std::string staticDeviceId = deviceId; + static std::string staticToken = token; + static int32_t staticStatus = status; + static int32_t staticReason = reason; int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DmNapiAuthJsCallback *callback = reinterpret_cast(work->data); - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); if (deviceManagerNapi == nullptr) { - LOGE("OnAuthResult, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); + LOGE("OnAuthResult, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); return; } - deviceManagerNapi->OnAuthResult(callback->deviceId_, callback->token_, callback->status_, callback->reason_); + deviceManagerNapi->OnAuthResult(staticDeviceId, staticToken, staticStatus, staticReason); delete work; }); if (ret != 0) { @@ -356,22 +348,22 @@ void DmNapiVerifyAuthCallback::OnVerifyAuthResult(const std::string &deviceId, i return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { + if (loop == nullptr) { LOGE("DmNapiVerifyAuthCallback: OnVerifyAuthResult, No memory"); return; } - jsCallback_ = std::make_unique(bundleName_, deviceId, resultCode, flag); - work->data = reinterpret_cast(jsCallback_.get()); - + static std::string staticBundleName = bundleName_; + static std::string staticDeviceId = deviceId; + static int32_t staticCode = resultCode; + static int32_t staticFlag = flag; int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DmNapiVerifyJsCallback *callback = reinterpret_cast(work->data); - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); if (deviceManagerNapi == nullptr) { - LOGE("OnVerifyAuthResult, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); + LOGE("OnVerifyAuthResult, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); return; } - deviceManagerNapi->OnVerifyResult(callback->deviceId_, callback->resultCode_, callback->flag_); + deviceManagerNapi->OnVerifyResult(staticDeviceId, staticCode, staticFlag); delete work; }); if (ret != 0) { @@ -1027,22 +1019,20 @@ void DmNapiDeviceManagerFaCallback::OnCall(const std::string ¶mJson) return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { + if (loop == nullptr) { LOGE("DmNapiDeviceManagerFaCallback: OnCall, No memory"); return; } - jsCallback_ = std::make_unique(bundleName_, "", paramJson, 0, 0); - work->data = reinterpret_cast(jsCallback_.get()); - + static std::string staticBundleName = bundleName_; + static std::string staticParamJson = paramJson; int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DmNapiAuthJsCallback *callback = reinterpret_cast(work->data); - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); if (deviceManagerNapi == nullptr) { - LOGE("OnCall, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); + LOGE("OnCall, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); return; } - deviceManagerNapi->OnDmfaCall(callback->token_); + deviceManagerNapi->OnDmfaCall(staticParamJson); delete work; }); if (ret != 0) { @@ -1944,6 +1934,209 @@ napi_value DeviceManagerNapi::Init(napi_env env, napi_value exports) return exports; } +napi_value DeviceManagerNapi::EnumTypeConstructor(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 DeviceManagerNapi::InitDeviceTypeEnum(napi_env env, napi_value exports) +{ + napi_value unknown_type; + napi_value speaker; + napi_value phone; + napi_value tablet; + napi_value wearable; + napi_value car; + napi_value tv; + int32_t refCount = 1; + + napi_create_uint32(env, static_cast(DmDeviceType::DEVICE_TYPE_UNKNOWN), + &unknown_type); + napi_create_uint32(env, static_cast(DmDeviceType::DEVICE_TYPE_AUDIO), + &speaker); + napi_create_uint32(env, static_cast(DmDeviceType::DEVICE_TYPE_PHONE), + &phone); + napi_create_uint32(env, static_cast(DmDeviceType::DEVICE_TYPE_PAD), + &tablet); + napi_create_uint32(env, static_cast(DmDeviceType::DEVICE_TYPE_WATCH), + &wearable); + napi_create_uint32(env, static_cast(DmDeviceType::DEVICE_TYPE_CAR), + &car); + napi_create_uint32(env, static_cast(DmDeviceType::DEVICE_TYPE_TV), + &tv); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("UNKNOWN_TYPE", unknown_type), + DECLARE_NAPI_STATIC_PROPERTY("SPEAKER", speaker), + DECLARE_NAPI_STATIC_PROPERTY("PHONE", phone), + DECLARE_NAPI_STATIC_PROPERTY("TABLET", tablet), + DECLARE_NAPI_STATIC_PROPERTY("WEARABLE", wearable), + DECLARE_NAPI_STATIC_PROPERTY("CAR", car), + DECLARE_NAPI_STATIC_PROPERTY("TV", tv), + }; + + napi_value result = nullptr; + napi_define_class(env, "DeviceType", NAPI_AUTO_LENGTH, EnumTypeConstructor, + nullptr, sizeof(desc) / sizeof(*desc), desc, &result); + napi_create_reference(env, result, refCount, &deviceTypeEnumConstructor_); + napi_set_named_property(env, exports, "DeviceType", result); + return exports; +} + +napi_value DeviceManagerNapi::InitDeviceStateChangeActionEnum(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, EnumTypeConstructor, + nullptr, sizeof(desc) / sizeof(*desc), desc, &result); + napi_create_reference(env, result, refCount, &deviceStateChangeActionEnumConstructor_); + napi_set_named_property(env, exports, "DeviceStateChangeAction", result); + return exports; +} + +napi_value DeviceManagerNapi::InitDiscoverModeEnum(napi_env env, napi_value exports) +{ + napi_value discover_mode_passive; + napi_value discover_mode_active; + int32_t refCount = 1; + + napi_create_uint32(env, static_cast(DmDiscoverMode::DM_DISCOVER_MODE_PASSIVE), + &discover_mode_passive); + napi_create_uint32(env, static_cast(DmDiscoverMode::DM_DISCOVER_MODE_ACTIVE), + &discover_mode_active); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("DISCOVER_MODE_PASSIVE", discover_mode_passive), + DECLARE_NAPI_STATIC_PROPERTY("DISCOVER_MODE_ACTIVE", discover_mode_active), + }; + + napi_value result = nullptr; + napi_define_class(env, "DiscoverMode", NAPI_AUTO_LENGTH, EnumTypeConstructor, + nullptr, sizeof(desc) / sizeof(*desc), desc, &result); + napi_create_reference(env, result, refCount, &discoverModeEnumConstructor_); + napi_set_named_property(env, exports, "DiscoverMode", result); + return exports; +} + +napi_value DeviceManagerNapi::InitExchangeMediumEnum(napi_env env, napi_value exports) +{ + napi_value medium_auto; + napi_value medium_ble; + napi_value medium_coap; + napi_value medium_usb; + int32_t refCount = 1; + + napi_create_uint32(env, static_cast(DmExchangeMedium::DM_AUTO), + &medium_auto); + napi_create_uint32(env, static_cast(DmExchangeMedium::DM_BLE), + &medium_ble); + napi_create_uint32(env, static_cast(DmExchangeMedium::DM_COAP), + &medium_coap); + napi_create_uint32(env, static_cast(DmExchangeMedium::DM_USB), + &medium_usb); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("AUTO", medium_auto), + DECLARE_NAPI_STATIC_PROPERTY("BLE", medium_ble), + DECLARE_NAPI_STATIC_PROPERTY("COAP", medium_coap), + DECLARE_NAPI_STATIC_PROPERTY("USB", medium_usb), + }; + + napi_value result = nullptr; + napi_define_class(env, "ExchangeMedium", NAPI_AUTO_LENGTH, EnumTypeConstructor, + nullptr, sizeof(desc) / sizeof(*desc), desc, &result); + napi_create_reference(env, result, refCount, &exchangeMediumEnumConstructor_); + napi_set_named_property(env, exports, "ExchangeMedium", result); + return exports; +} + +napi_value DeviceManagerNapi::InitExchangeFreqEnum(napi_env env, napi_value exports) +{ + napi_value low; + napi_value mid; + napi_value high; + napi_value super_high; + int32_t refCount = 1; + + napi_create_uint32(env, static_cast(DmExchangeFreq::DM_LOW), + &low); + napi_create_uint32(env, static_cast(DmExchangeFreq::DM_MID), + &mid); + napi_create_uint32(env, static_cast(DmExchangeFreq::DM_HIGH), + &high); + napi_create_uint32(env, static_cast(DmExchangeFreq::DM_SUPER_HIGH), + &super_high); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("LOW", low), + DECLARE_NAPI_STATIC_PROPERTY("MID", mid), + DECLARE_NAPI_STATIC_PROPERTY("HIGH", high), + DECLARE_NAPI_STATIC_PROPERTY("SUPER_HIGH", super_high), + }; + + napi_value result = nullptr; + napi_define_class(env, "ExchangeFreq", NAPI_AUTO_LENGTH, EnumTypeConstructor, + nullptr, sizeof(desc) / sizeof(*desc), desc, &result); + napi_create_reference(env, result, refCount, &exchangeFreqEnumConstructor_); + napi_set_named_property(env, exports, "ExchangeFreq", result); + return exports; +} + +napi_value DeviceManagerNapi::InitSubscribeCapEnum(napi_env env, napi_value exports) +{ + napi_value subscribe_capability_ddmp; + napi_value subscribe_capability_osd; + int32_t refCount = 1; + + napi_create_uint32(env, static_cast(DM_NAPI_SUBSCRIBE_CAPABILITY_DDMP), + &subscribe_capability_ddmp); + napi_create_uint32(env, static_cast(DM_NAPI_SUBSCRIBE_CAPABILITY_OSD), + &subscribe_capability_osd); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("SUBSCRIBE_CAPABILITY_DDMP", subscribe_capability_ddmp), + DECLARE_NAPI_STATIC_PROPERTY("SUBSCRIBE_CAPABILITY_OSD", subscribe_capability_osd), + }; + + napi_value result = nullptr; + napi_define_class(env, "SubscribeCap", NAPI_AUTO_LENGTH, EnumTypeConstructor, + nullptr, sizeof(desc) / sizeof(*desc), desc, &result); + napi_create_reference(env, result, refCount, &subscribeCapEnumConstructor_); + napi_set_named_property(env, exports, "SubscribeCap", result); + return exports; +} + /* * Function registering all props and functions of ohos.distributedhardware */ @@ -1951,6 +2144,12 @@ static napi_value Export(napi_env env, napi_value exports) { LOGI("Export() is called!"); DeviceManagerNapi::Init(env, exports); + DeviceManagerNapi::InitDeviceTypeEnum(env, exports); + DeviceManagerNapi::InitDeviceStateChangeActionEnum(env, exports); + DeviceManagerNapi::InitDiscoverModeEnum(env, exports); + DeviceManagerNapi::InitExchangeMediumEnum(env, exports); + DeviceManagerNapi::InitExchangeFreqEnum(env, exports); + DeviceManagerNapi::InitSubscribeCapEnum(env, exports); return exports; } -- Gitee