1 /* 2 * Copyright (c) 2022-2025 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 #ifndef OHOS_DM_NATIVE_DEVICEMANAGER_JS_H 17 #define OHOS_DM_NATIVE_DEVICEMANAGER_JS_H 18 19 #include <memory> 20 #include <string> 21 #include <mutex> 22 23 #include "device_manager_callback.h" 24 #include "dm_app_image_info.h" 25 #include "dm_device_info.h" 26 #include "dm_device_profile_info.h" 27 #include "dm_native_event.h" 28 #include "dm_subscribe_info.h" 29 #include "dm_publish_info.h" 30 #include "dm_anonymous.h" 31 #include "dm_error_message.h" 32 #include "napi/native_api.h" 33 #include "napi/native_node_api.h" 34 #include "nlohmann/json.hpp" 35 #define DM_NAPI_BUF_LENGTH (256) 36 37 struct AsyncCallbackInfo { 38 napi_env env = nullptr; 39 napi_async_work asyncWork = nullptr; 40 41 char bundleName[DM_NAPI_BUF_LENGTH] = {0}; 42 size_t bundleNameLen = 0; 43 44 napi_ref callback = nullptr; 45 int32_t status = -1; 46 int32_t ret = 0; 47 }; 48 49 struct DeviceBasicInfoListAsyncCallbackInfo { 50 napi_env env = nullptr; 51 napi_async_work asyncWork = nullptr; 52 53 std::string bundleName; 54 size_t bundleNameLen = 0; 55 std::vector<OHOS::DistributedHardware::DmDeviceBasicInfo> devList; 56 std::string extra; 57 // OHOS::DistributedHardware::DmFilterOptions filter; 58 napi_ref callback = nullptr; 59 napi_value thisVar = nullptr; 60 napi_deferred deferred = nullptr; 61 int32_t status = -1; 62 int32_t ret = 0; 63 }; 64 65 struct DeviceProfileInfosAsyncCallbackInfo { 66 napi_env env = nullptr; 67 napi_async_work asyncWork = nullptr; 68 std::string bundleName; 69 napi_deferred deferred = nullptr; 70 int32_t code = -1; 71 OHOS::DistributedHardware::DmDeviceProfileInfoFilterOptions filterOptions; 72 std::vector<OHOS::DistributedHardware::DmDeviceProfileInfo> deviceProfileInfos; 73 }; 74 75 struct AuthAsyncCallbackInfo { 76 napi_env env = nullptr; 77 78 char bundleName[DM_NAPI_BUF_LENGTH] = {0}; 79 size_t bundleNameLen = 0; 80 81 napi_ref callback = nullptr; 82 int32_t authType = -1; 83 }; 84 85 struct DmNapiStatusJsCallback { 86 std::string bundleName_; 87 uint16_t subscribeId_; 88 int32_t reason_; 89 OHOS::DistributedHardware::DmDeviceBasicInfo deviceBasicInfo_; 90 DmNapiStatusJsCallbackDmNapiStatusJsCallback91 DmNapiStatusJsCallback(std::string bundleName, uint16_t subscribeId, int32_t reason, 92 OHOS::DistributedHardware::DmDeviceBasicInfo deviceBasicInfo_) 93 : bundleName_(bundleName), subscribeId_(subscribeId), reason_(reason), deviceBasicInfo_(deviceBasicInfo_) {} 94 }; 95 96 struct DmNapiPublishJsCallback { 97 std::string bundleName_; 98 int32_t publishId_; 99 int32_t reason_; 100 DmNapiPublishJsCallbackDmNapiPublishJsCallback101 DmNapiPublishJsCallback(std::string bundleName, int32_t publishId, int32_t reason) 102 : bundleName_(bundleName), publishId_(publishId), reason_(reason) {} 103 }; 104 105 struct DmNapiAuthJsCallback { 106 std::string bundleName_; 107 std::string deviceId_; 108 std::string token_; 109 int32_t status_; 110 int32_t reason_; 111 DmNapiAuthJsCallbackDmNapiAuthJsCallback112 DmNapiAuthJsCallback(std::string bundleName, std::string deviceId, std::string token, int32_t status, 113 int32_t reason) 114 : bundleName_(bundleName), deviceId_(deviceId), token_(token), status_(status), reason_(reason) {} 115 }; 116 117 enum DmNapiDevStatusChange { UNKNOWN = 0, AVAILABLE = 1, UNAVAILABLE = 2, CHANGE = 3}; 118 119 class DmNapiInitCallback : public OHOS::DistributedHardware::DmInitCallback { 120 public: DmNapiInitCallback(napi_env env,std::string & bundleName)121 explicit DmNapiInitCallback(napi_env env, std::string &bundleName) : env_(env), bundleName_(bundleName) 122 { 123 } ~DmNapiInitCallback()124 ~DmNapiInitCallback() override {} 125 void OnRemoteDied() override; 126 127 private: 128 napi_env env_; 129 std::string bundleName_; 130 }; 131 132 class DmNapiDeviceStatusCallback : public OHOS::DistributedHardware::DeviceStatusCallback { 133 public: DmNapiDeviceStatusCallback(napi_env env,std::string & bundleName)134 explicit DmNapiDeviceStatusCallback(napi_env env, std::string &bundleName) : env_(env), bundleName_(bundleName) 135 { 136 } ~DmNapiDeviceStatusCallback()137 ~DmNapiDeviceStatusCallback() override {}; 138 void OnDeviceOnline(const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo) override; 139 void OnDeviceReady(const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo) override; 140 void OnDeviceOffline(const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo) override; 141 void OnDeviceChanged(const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo) override; 142 private: 143 napi_env env_; 144 std::string bundleName_; 145 }; 146 147 class DmNapiDiscoveryCallback : public OHOS::DistributedHardware::DiscoveryCallback { 148 public: DmNapiDiscoveryCallback(napi_env env,std::string & bundleName)149 explicit DmNapiDiscoveryCallback(napi_env env, std::string &bundleName) 150 : env_(env), refCount_(0), bundleName_(bundleName) 151 { 152 } ~DmNapiDiscoveryCallback()153 ~DmNapiDiscoveryCallback() override {}; 154 void OnDeviceFound(uint16_t subscribeId, 155 const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo) override; 156 void OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason) override; 157 void OnDiscoverySuccess(uint16_t subscribeId) override; 158 void IncreaseRefCount(); 159 void DecreaseRefCount(); 160 int32_t GetRefCount(); 161 162 private: 163 napi_env env_; 164 std::atomic<int32_t> refCount_; 165 std::string bundleName_; 166 }; 167 168 class DmNapiPublishCallback : public OHOS::DistributedHardware::PublishCallback { 169 public: DmNapiPublishCallback(napi_env env,std::string & bundleName)170 explicit DmNapiPublishCallback(napi_env env, std::string &bundleName) 171 : env_(env), refCount_(0), bundleName_(bundleName) 172 { 173 } ~DmNapiPublishCallback()174 ~DmNapiPublishCallback() override {}; 175 void OnPublishResult(int32_t publishId, int32_t publishResult) override; 176 void IncreaseRefCount(); 177 void DecreaseRefCount(); 178 int32_t GetRefCount(); 179 180 private: 181 napi_env env_; 182 std::atomic<int32_t> refCount_; 183 std::string bundleName_; 184 }; 185 186 class DmNapiDeviceManagerUiCallback : public OHOS::DistributedHardware::DeviceManagerUiCallback { 187 public: DmNapiDeviceManagerUiCallback(napi_env env,std::string & bundleName)188 explicit DmNapiDeviceManagerUiCallback(napi_env env, std::string &bundleName) : env_(env), bundleName_(bundleName) 189 { 190 } ~DmNapiDeviceManagerUiCallback()191 ~DmNapiDeviceManagerUiCallback() override {}; 192 void OnCall(const std::string ¶mJson) override; 193 194 private: 195 napi_env env_; 196 std::string bundleName_; 197 }; 198 199 class DmNapiAuthenticateCallback : public OHOS::DistributedHardware::AuthenticateCallback { 200 public: DmNapiAuthenticateCallback(napi_env env,std::string & bundleName)201 explicit DmNapiAuthenticateCallback(napi_env env, std::string &bundleName) : env_(env), bundleName_(bundleName) 202 { 203 } ~DmNapiAuthenticateCallback()204 ~DmNapiAuthenticateCallback() override {}; 205 void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, int32_t reason) override; 206 207 private: 208 napi_env env_; 209 std::string bundleName_; 210 }; 211 212 class DmNapiBindTargetCallback : public OHOS::DistributedHardware::BindTargetCallback { 213 public: DmNapiBindTargetCallback(napi_env env,std::string & bundleName)214 explicit DmNapiBindTargetCallback(napi_env env, std::string &bundleName) : env_(env), bundleName_(bundleName) 215 { 216 } ~DmNapiBindTargetCallback()217 ~DmNapiBindTargetCallback() override {}; 218 void OnBindResult(const OHOS::DistributedHardware::PeerTargetId &targetId, int32_t result, 219 int32_t status, std::string content) override; 220 221 private: 222 napi_env env_; 223 std::string bundleName_; 224 }; 225 226 class DmNapiGetDeviceProfileInfoListCallback : public OHOS::DistributedHardware::GetDeviceProfileInfoListCallback { 227 public: DmNapiGetDeviceProfileInfoListCallback(napi_env env,const std::string & bundleName,const napi_deferred & deferred)228 explicit DmNapiGetDeviceProfileInfoListCallback(napi_env env, const std::string &bundleName, 229 const napi_deferred &deferred) : env_(env), bundleName_(bundleName), deferred_(deferred) 230 {} ~DmNapiGetDeviceProfileInfoListCallback()231 ~DmNapiGetDeviceProfileInfoListCallback() override {}; 232 233 void OnResult(const std::vector<OHOS::DistributedHardware::DmDeviceProfileInfo> &deviceProfileInfos, 234 int32_t code) override; 235 236 private: 237 napi_env env_; 238 std::string bundleName_; 239 napi_deferred deferred_ = nullptr; 240 }; 241 242 class DeviceManagerNapi : public DmNativeEvent { 243 public: 244 explicit DeviceManagerNapi(napi_env env, napi_value thisVar); 245 ~DeviceManagerNapi() override; 246 static napi_value Init(napi_env env, napi_value exports); 247 static napi_value Constructor(napi_env env, napi_callback_info info); 248 static napi_value EnumTypeConstructor(napi_env env, napi_callback_info info); 249 static napi_value InitDeviceStatusChangeActionEnum(napi_env env, napi_value exports); 250 static napi_value CreateDeviceManager(napi_env env, napi_callback_info info); 251 static napi_value ReleaseDeviceManager(napi_env env, napi_callback_info info); 252 static napi_value SetUserOperationSync(napi_env env, napi_callback_info info); 253 static napi_value GetAvailableDeviceListSync(napi_env env, napi_callback_info info); 254 static napi_value GetAvailableDeviceList(napi_env env, napi_callback_info info); 255 static napi_value GetLocalDeviceNetworkId(napi_env env, napi_callback_info info); 256 static napi_value GetLocalDeviceId(napi_env env, napi_callback_info info); 257 static napi_value GetLocalDeviceName(napi_env env, napi_callback_info info); 258 static napi_value GetLocalDeviceType(napi_env env, napi_callback_info info); 259 static napi_value GetDeviceName(napi_env env, napi_callback_info info); 260 static napi_value GetDeviceType(napi_env env, napi_callback_info info); 261 static napi_value StartDeviceDiscover(napi_env env, napi_callback_info info); 262 static napi_value StopDeviceDiscover(napi_env env, napi_callback_info info); 263 static napi_value PublishDeviceDiscoverySync(napi_env env, napi_callback_info info); 264 static napi_value UnPublishDeviceDiscoverySync(napi_env env, napi_callback_info info); 265 static napi_value BindTarget(napi_env env, napi_callback_info info); 266 static napi_value UnBindTarget(napi_env env, napi_callback_info info); 267 static napi_value JsOn(napi_env env, napi_callback_info info); 268 static napi_value JsOff(napi_env env, napi_callback_info info); 269 static napi_value JsGetDeviceProfileInfoList(napi_env env, napi_callback_info info); 270 static DeviceManagerNapi *GetDeviceManagerNapi(std::string &bundleName); 271 static void CreateDmCallback(napi_env env, std::string &bundleName, std::string &eventType); 272 static void CreateDmCallback(napi_env env, std::string &bundleName, std::string &eventType, std::string &extra); 273 static void ReleaseDmCallback(std::string &bundleName, std::string &eventType); 274 void OnDeviceStatusChange(DmNapiDevStatusChange action, 275 const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo); 276 void OnDeviceFound(uint16_t subscribeId, const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo); 277 void OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason); 278 void OnPublishResult(int32_t publishId, int32_t publishResult); 279 void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, int32_t reason); 280 void OnDmUiCall(const std::string ¶mJson); 281 void OnGetDeviceProfileInfoListCallbackResult(DeviceProfileInfosAsyncCallbackInfo *jsCallback); 282 static napi_value InitStrategyForHeartbeatEnum(napi_env env, napi_value exports); 283 static napi_value SetHeartbeatPolicy(napi_env env, napi_callback_info info); 284 285 private: 286 static void ReleasePublishCallback(std::string &bundleName); 287 static void ReleaseDiscoveryCallback(std::string &bundleName); 288 static void LockDiscoveryCallbackMutex(napi_env env, std::string &bundleName, std::string &extra, 289 uint32_t subscribeId); 290 static void ClearBundleCallbacks(std::string &bundleName); 291 static napi_value JsOffFrench(napi_env env, int32_t num, napi_value thisVar, napi_value argv[]); 292 static napi_value JsOnFrench(napi_env env, int32_t num, napi_value thisVar, napi_value argv[]); 293 static void CallAsyncWork(napi_env env, DeviceBasicInfoListAsyncCallbackInfo *deviceBasicInfoListAsyncCallbackInfo); 294 static void CallGetAvailableDeviceListStatus(napi_env env, napi_status &status, 295 DeviceBasicInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo); 296 static napi_value CallDeviceList(napi_env env, napi_callback_info info, 297 DeviceBasicInfoListAsyncCallbackInfo *deviceBasicInfoListAsyncCallbackInfo); 298 static napi_value GetAvailableDeviceListPromise(napi_env env, 299 DeviceBasicInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo); 300 static void BindDevOrTarget(DeviceManagerNapi *deviceManagerWrapper, const std::string &deviceId, napi_env env, 301 napi_value &object); 302 static int32_t BindTargetWarpper(const std::string &pkgName, const std::string &deviceId, 303 const std::string &bindParam, std::shared_ptr<DmNapiBindTargetCallback> callback); 304 static void RegisterDevStatusCallback(napi_env env, std::string &bundleName); 305 static napi_value GetDeviceProfileInfoListPromise(napi_env env, DeviceProfileInfosAsyncCallbackInfo *jsCallback); 306 307 private: 308 napi_env env_; 309 static thread_local napi_ref sConstructor_; 310 std::string bundleName_; 311 static AuthAsyncCallbackInfo authAsyncCallbackInfo_; 312 }; 313 #endif // OHOS_DM_NATIVE_DEVICEMANAGER_JS_H 314