• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "json_object.h"
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 GetLocalDisplayDeviceNameAsyncCallbackInfo {
76     napi_env env = nullptr;
77     napi_async_work asyncWork = nullptr;
78     std::string bundleName;
79     int32_t maxNameLength = 0;
80     napi_deferred deferred = nullptr;
81     int32_t code = -1;
82     std::string displayName;
83 };
84 
85 struct GetDeviceNetworkIdListAsyncCallbackInfo {
86     napi_env env = nullptr;
87     napi_async_work asyncWork = nullptr;
88     std::string bundleName;
89     napi_deferred deferred = nullptr;
90     int32_t code = -1;
91     OHOS::DistributedHardware::NetworkIdQueryFilter filterOptions;
92     std::vector<std::string> deviceNetworkIds;
93 };
94 
95 struct DeviceIconInfoAsyncCallbackInfo {
96     napi_env env = nullptr;
97     napi_async_work asyncWork = nullptr;
98     std::string bundleName;
99     napi_deferred deferred = nullptr;
100     int32_t code = -1;
101     OHOS::DistributedHardware::DmDeviceIconInfoFilterOptions filterOptions;
102     OHOS::DistributedHardware::DmDeviceIconInfo deviceIconInfo;
103 };
104 
105 struct PutDeviceProfileInfoListAsyncCallbackInfo {
106     napi_env env = nullptr;
107     napi_async_work asyncWork = nullptr;
108     std::string bundleName;
109     napi_deferred deferred = nullptr;
110     int32_t code = -1;
111     std::vector<OHOS::DistributedHardware::DmDeviceProfileInfo> deviceProfileInfos;
112 };
113 
114 struct SetLocalDeviceNameAsyncCallbackInfo {
115     napi_env env = nullptr;
116     napi_async_work asyncWork = nullptr;
117     std::string bundleName = "";
118     std::string deviceName = "";
119     napi_deferred deferred = nullptr;
120     int32_t code = -1;
121 };
122 
123 struct SetRemoteDeviceNameAsyncCallbackInfo {
124     napi_env env = nullptr;
125     napi_async_work asyncWork = nullptr;
126     std::string bundleName = "";
127     std::string deviceName = "";
128     std::string deviceId = "";
129     napi_deferred deferred = nullptr;
130     int32_t code = -1;
131 };
132 
133 struct AuthAsyncCallbackInfo {
134     napi_env env = nullptr;
135 
136     char bundleName[DM_NAPI_BUF_LENGTH] = {0};
137     size_t bundleNameLen = 0;
138 
139     napi_ref callback = nullptr;
140     int32_t authType = -1;
141 };
142 
143 struct DmNapiStatusJsCallback {
144     std::string bundleName_;
145     uint16_t subscribeId_;
146     int32_t reason_;
147     OHOS::DistributedHardware::DmDeviceBasicInfo deviceBasicInfo_;
148 
DmNapiStatusJsCallbackDmNapiStatusJsCallback149     DmNapiStatusJsCallback(std::string bundleName, uint16_t subscribeId, int32_t reason,
150         OHOS::DistributedHardware::DmDeviceBasicInfo deviceBasicInfo_)
151         : bundleName_(bundleName), subscribeId_(subscribeId), reason_(reason), deviceBasicInfo_(deviceBasicInfo_) {}
152 };
153 
154 struct DmNapiPublishJsCallback {
155     std::string bundleName_;
156     int32_t publishId_;
157     int32_t reason_;
158 
DmNapiPublishJsCallbackDmNapiPublishJsCallback159     DmNapiPublishJsCallback(std::string bundleName, int32_t publishId, int32_t reason)
160         : bundleName_(bundleName), publishId_(publishId), reason_(reason) {}
161 };
162 
163 struct DmNapiAuthJsCallback {
164     std::string bundleName_;
165     std::string deviceId_;
166     std::string token_;
167     int32_t status_;
168     int32_t reason_;
169 
DmNapiAuthJsCallbackDmNapiAuthJsCallback170     DmNapiAuthJsCallback(std::string bundleName, std::string deviceId, std::string token, int32_t status,
171         int32_t reason)
172         : bundleName_(bundleName), deviceId_(deviceId), token_(token), status_(status), reason_(reason) {}
173 };
174 
175 enum DmNapiDevStatusChange { UNKNOWN = 0, AVAILABLE = 1, UNAVAILABLE = 2, CHANGE = 3};
176 
177 class DmNapiInitCallback : public OHOS::DistributedHardware::DmInitCallback {
178 public:
DmNapiInitCallback(napi_env env,std::string & bundleName)179     explicit DmNapiInitCallback(napi_env env, std::string &bundleName) : env_(env), bundleName_(bundleName)
180     {
181     }
~DmNapiInitCallback()182     ~DmNapiInitCallback() override {}
183     void OnRemoteDied() override;
184 
185 private:
186     napi_env env_;
187     std::string bundleName_;
188 };
189 
190 class DmNapiDeviceStatusCallback : public OHOS::DistributedHardware::DeviceStatusCallback {
191 public:
DmNapiDeviceStatusCallback(napi_env env,std::string & bundleName)192     explicit DmNapiDeviceStatusCallback(napi_env env, std::string &bundleName) : env_(env), bundleName_(bundleName)
193     {
194     }
~DmNapiDeviceStatusCallback()195     ~DmNapiDeviceStatusCallback() override {};
196     void OnDeviceOnline(const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo) override;
197     void OnDeviceReady(const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo) override;
198     void OnDeviceOffline(const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo) override;
199     void OnDeviceChanged(const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo) override;
200 private:
201     napi_env env_;
202     std::string bundleName_;
203 };
204 
205 class DmNapiDiscoveryCallback : public OHOS::DistributedHardware::DiscoveryCallback {
206 public:
DmNapiDiscoveryCallback(napi_env env,std::string & bundleName)207     explicit DmNapiDiscoveryCallback(napi_env env, std::string &bundleName)
208         : env_(env), refCount_(0), bundleName_(bundleName)
209     {
210     }
~DmNapiDiscoveryCallback()211     ~DmNapiDiscoveryCallback() override {};
212     void OnDeviceFound(uint16_t subscribeId,
213         const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo) override;
214     void OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason) override;
215     void OnDiscoverySuccess(uint16_t subscribeId) override;
216     void IncreaseRefCount();
217     void DecreaseRefCount();
218     int32_t GetRefCount();
219 
220 private:
221     napi_env env_;
222     std::atomic<int32_t> refCount_;
223     std::string bundleName_;
224 };
225 
226 class DmNapiPublishCallback : public OHOS::DistributedHardware::PublishCallback {
227 public:
DmNapiPublishCallback(napi_env env,std::string & bundleName)228     explicit DmNapiPublishCallback(napi_env env, std::string &bundleName)
229         : env_(env), refCount_(0), bundleName_(bundleName)
230     {
231     }
~DmNapiPublishCallback()232     ~DmNapiPublishCallback() override {};
233     void OnPublishResult(int32_t publishId, int32_t publishResult) override;
234     void IncreaseRefCount();
235     void DecreaseRefCount();
236     int32_t GetRefCount();
237 
238 private:
239     napi_env env_;
240     std::atomic<int32_t> refCount_;
241     std::string bundleName_;
242 };
243 
244 class DmNapiDeviceManagerUiCallback : public OHOS::DistributedHardware::DeviceManagerUiCallback {
245 public:
DmNapiDeviceManagerUiCallback(napi_env env,std::string & bundleName)246     explicit DmNapiDeviceManagerUiCallback(napi_env env, std::string &bundleName) : env_(env), bundleName_(bundleName)
247     {
248     }
~DmNapiDeviceManagerUiCallback()249     ~DmNapiDeviceManagerUiCallback() override {};
250     void OnCall(const std::string &paramJson) override;
251 
252 private:
253     napi_env env_;
254     std::string bundleName_;
255 };
256 
257 class DmNapiAuthenticateCallback : public OHOS::DistributedHardware::AuthenticateCallback {
258 public:
DmNapiAuthenticateCallback(napi_env env,std::string & bundleName)259     explicit DmNapiAuthenticateCallback(napi_env env, std::string &bundleName) : env_(env), bundleName_(bundleName)
260     {
261     }
~DmNapiAuthenticateCallback()262     ~DmNapiAuthenticateCallback() override {};
263     void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, int32_t reason) override;
264 
265 private:
266     napi_env env_;
267     std::string bundleName_;
268 };
269 
270 class DmNapiBindTargetCallback : public OHOS::DistributedHardware::BindTargetCallback {
271 public:
DmNapiBindTargetCallback(napi_env env,std::string & bundleName)272     explicit DmNapiBindTargetCallback(napi_env env, std::string &bundleName) : env_(env), bundleName_(bundleName)
273     {
274     }
~DmNapiBindTargetCallback()275     ~DmNapiBindTargetCallback() override {};
276     void OnBindResult(const OHOS::DistributedHardware::PeerTargetId &targetId, int32_t result,
277         int32_t status, std::string content) override;
278 
279 private:
280     napi_env env_;
281     std::string bundleName_;
282 };
283 
284 class DmNapiGetDeviceProfileInfoListCallback : public OHOS::DistributedHardware::GetDeviceProfileInfoListCallback {
285 public:
DmNapiGetDeviceProfileInfoListCallback(napi_env env,const std::string & bundleName,const napi_deferred & deferred)286     explicit DmNapiGetDeviceProfileInfoListCallback(napi_env env, const std::string &bundleName,
287         const napi_deferred &deferred) : env_(env), bundleName_(bundleName), deferred_(deferred)
288     {}
~DmNapiGetDeviceProfileInfoListCallback()289     ~DmNapiGetDeviceProfileInfoListCallback() override {};
290 
291     void OnResult(const std::vector<OHOS::DistributedHardware::DmDeviceProfileInfo> &deviceProfileInfos,
292         int32_t code) override;
293 
294 private:
295     napi_env env_;
296     std::string bundleName_;
297     napi_deferred deferred_ = nullptr;
298 };
299 
300 class DmNapiGetDeviceIconInfoCallback : public OHOS::DistributedHardware::GetDeviceIconInfoCallback {
301 public:
DmNapiGetDeviceIconInfoCallback(napi_env env,const std::string & bundleName,const napi_deferred & deferred)302     explicit DmNapiGetDeviceIconInfoCallback(napi_env env, const std::string &bundleName,
303         const napi_deferred &deferred) : env_(env), bundleName_(bundleName), deferred_(deferred)
304     {
305     }
~DmNapiGetDeviceIconInfoCallback()306     ~DmNapiGetDeviceIconInfoCallback() override {};
307 
308     void OnResult(const OHOS::DistributedHardware::DmDeviceIconInfo &deviceIconInfo, int32_t code) override;
309 
310 private:
311     napi_env env_;
312     std::string bundleName_ = "";
313     napi_deferred deferred_ = nullptr;
314 };
315 
316 class DmNapiSetLocalDeviceNameCallback : public OHOS::DistributedHardware::SetLocalDeviceNameCallback {
317 public:
DmNapiSetLocalDeviceNameCallback(napi_env env,const std::string & bundleName,const napi_deferred & deferred)318     explicit DmNapiSetLocalDeviceNameCallback(napi_env env, const std::string &bundleName,
319         const napi_deferred &deferred) : env_(env), bundleName_(bundleName), deferred_(deferred)
320     {}
~DmNapiSetLocalDeviceNameCallback()321     ~DmNapiSetLocalDeviceNameCallback() override {};
322 
323     void OnResult(int32_t code) override;
324 
325 private:
326     napi_env env_;
327     std::string bundleName_;
328     napi_deferred deferred_ = nullptr;
329 };
330 
331 class DmNapiSetRemoteDeviceNameCallback : public OHOS::DistributedHardware::SetRemoteDeviceNameCallback {
332 public:
DmNapiSetRemoteDeviceNameCallback(napi_env env,const std::string & bundleName,const napi_deferred & deferred)333     explicit DmNapiSetRemoteDeviceNameCallback(napi_env env, const std::string &bundleName,
334         const napi_deferred &deferred) : env_(env), bundleName_(bundleName), deferred_(deferred)
335     {}
~DmNapiSetRemoteDeviceNameCallback()336     ~DmNapiSetRemoteDeviceNameCallback() override {};
337 
338     void OnResult(int32_t code) override;
339 
340 private:
341     napi_env env_;
342     std::string bundleName_;
343     napi_deferred deferred_ = nullptr;
344 };
345 
346 class DeviceManagerNapi : public DmNativeEvent {
347 public:
348     explicit DeviceManagerNapi(napi_env env, napi_value thisVar);
349     ~DeviceManagerNapi() override;
350     static napi_value Init(napi_env env, napi_value exports);
351     static napi_value Constructor(napi_env env, napi_callback_info info);
352     static napi_value EnumTypeConstructor(napi_env env, napi_callback_info info);
353     static napi_value InitDeviceStatusChangeActionEnum(napi_env env, napi_value exports);
354     static napi_value CreateDeviceManager(napi_env env, napi_callback_info info);
355     static napi_value ReleaseDeviceManager(napi_env env, napi_callback_info info);
356     static napi_value SetUserOperationSync(napi_env env, napi_callback_info info);
357     static napi_value GetAvailableDeviceListSync(napi_env env, napi_callback_info info);
358     static napi_value GetAvailableDeviceList(napi_env env, napi_callback_info info);
359     static napi_value GetLocalDeviceNetworkId(napi_env env, napi_callback_info info);
360     static napi_value GetLocalDeviceId(napi_env env, napi_callback_info info);
361     static napi_value GetLocalDeviceName(napi_env env, napi_callback_info info);
362     static napi_value GetLocalDeviceType(napi_env env, napi_callback_info info);
363     static napi_value GetDeviceName(napi_env env, napi_callback_info info);
364     static napi_value GetDeviceType(napi_env env, napi_callback_info info);
365     static napi_value StartDeviceDiscover(napi_env env, napi_callback_info info);
366     static napi_value StopDeviceDiscover(napi_env env, napi_callback_info info);
367     static napi_value PublishDeviceDiscoverySync(napi_env env, napi_callback_info info);
368     static napi_value UnPublishDeviceDiscoverySync(napi_env env, napi_callback_info info);
369     static napi_value BindTarget(napi_env env, napi_callback_info info);
370     static napi_value UnBindTarget(napi_env env, napi_callback_info info);
371     static napi_value JsOn(napi_env env, napi_callback_info info);
372     static napi_value JsOff(napi_env env, napi_callback_info info);
373     static napi_value JsGetDeviceProfileInfoList(napi_env env, napi_callback_info info);
374     static napi_value JsGetDeviceIconInfo(napi_env env, napi_callback_info info);
375     static napi_value JsPutDeviceProfileInfoList(napi_env env, napi_callback_info info);
376     static napi_value JsGetLocalDisplayDeviceName(napi_env env, napi_callback_info info);
377     static napi_value JsSetLocalDeviceName(napi_env env, napi_callback_info info);
378     static napi_value JsSetRemoteDeviceName(napi_env env, napi_callback_info info);
379     static napi_value JsRestoreLocalDeviceName(napi_env env, napi_callback_info info);
380     static napi_value JsGetDeviceNetworkIdList(napi_env env, napi_callback_info info);
381     static DeviceManagerNapi *GetDeviceManagerNapi(std::string &bundleName);
382     static void CreateDmCallback(napi_env env, std::string &bundleName, std::string &eventType);
383     static void CreateDmCallback(napi_env env, std::string &bundleName, std::string &eventType, std::string &extra);
384     static void ReleaseDmCallback(std::string &bundleName, std::string &eventType);
385     void OnDeviceStatusChange(DmNapiDevStatusChange action,
386                              const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo);
387     void OnDeviceFound(uint16_t subscribeId, const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo);
388     void OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason);
389     void OnPublishResult(int32_t publishId, int32_t publishResult);
390     void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, int32_t reason);
391     void OnDmUiCall(const std::string &paramJson);
392     void OnGetDeviceProfileInfoListCallbackResult(DeviceProfileInfosAsyncCallbackInfo *jsCallback);
393     void OnGetDeviceIconInfoCallbackResult(DeviceIconInfoAsyncCallbackInfo *jsCallback);
394     static napi_value InitStrategyForHeartbeatEnum(napi_env env, napi_value exports);
395     static napi_value SetHeartbeatPolicy(napi_env env, napi_callback_info info);
396     void OnSetLocalDeviceNameCallbackResult(SetLocalDeviceNameAsyncCallbackInfo *jsCallback);
397     void OnSetRemoteDeviceNameCallbackResult(SetRemoteDeviceNameAsyncCallbackInfo *jsCallback);
398 
399 private:
400     static void ReleasePublishCallback(std::string &bundleName);
401     static void ReleaseDiscoveryCallback(std::string &bundleName);
402     static void LockDiscoveryCallbackMutex(napi_env env, std::string &bundleName,
403         std::map<std::string, std::string> discParam, std::string &extra, uint32_t subscribeId);
404     static void ClearBundleCallbacks(std::string &bundleName);
405     static napi_value JsOffFrench(napi_env env, int32_t num, napi_value thisVar, napi_value argv[]);
406     static napi_value JsOnFrench(napi_env env, int32_t num, napi_value thisVar, napi_value argv[]);
407     static void CallAsyncWork(napi_env env, DeviceBasicInfoListAsyncCallbackInfo *deviceBasicInfoListAsyncCallbackInfo);
408     static void CallGetAvailableDeviceListStatus(napi_env env, napi_status &status,
409         DeviceBasicInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo);
410     static napi_value CallDeviceList(napi_env env, napi_callback_info info,
411         DeviceBasicInfoListAsyncCallbackInfo *deviceBasicInfoListAsyncCallbackInfo);
412     static napi_value GetAvailableDeviceListPromise(napi_env env,
413         DeviceBasicInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo);
414     static void BindDevOrTarget(DeviceManagerNapi *deviceManagerWrapper, const std::string &deviceId, napi_env env,
415         napi_value &object);
416     static int32_t BindTargetWarpper(const std::string &pkgName, const std::string &deviceId,
417         const std::string &bindParam, std::shared_ptr<DmNapiBindTargetCallback> callback);
418     static void RegisterDevStatusCallback(napi_env env, std::string &bundleName);
419     static int32_t DumpDeviceInfo(DeviceBasicInfoListAsyncCallbackInfo *deviceBasicInfoListAsyncCallbackInfo);
420     static napi_value GetDeviceProfileInfoListPromise(napi_env env, DeviceProfileInfosAsyncCallbackInfo *jsCallback);
421     static napi_value GetDeviceIconInfoPromise(napi_env env, DeviceIconInfoAsyncCallbackInfo *jsCallback);
422     static napi_value PutDeviceProfileInfoListPromise(napi_env env,
423         PutDeviceProfileInfoListAsyncCallbackInfo *jsCallback);
424     static napi_value GetLocalDisplayDeviceNamePromise(napi_env env,
425         GetLocalDisplayDeviceNameAsyncCallbackInfo *jsCallback);
426     static napi_value SetLocalDeviceNamePromise(napi_env env, SetLocalDeviceNameAsyncCallbackInfo *jsCallback);
427     static napi_value SetRemoteDeviceNamePromise(napi_env env, SetRemoteDeviceNameAsyncCallbackInfo *jsCallback);
428     static napi_value GetDeviceNetworkIdListPromise(napi_env env,
429         GetDeviceNetworkIdListAsyncCallbackInfo *jsCallback);
430 private:
431     napi_env env_;
432     static thread_local napi_ref sConstructor_;
433     std::string bundleName_;
434     static AuthAsyncCallbackInfo authAsyncCallbackInfo_;
435 };
436 #endif // OHOS_DM_NATIVE_DEVICEMANAGER_JS_H
437