• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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_native_event.h"
27 #include "dm_subscribe_info.h"
28 #include "dm_publish_info.h"
29 #include "dm_anonymous.h"
30 #include "dm_error_message.h"
31 #include "napi/native_api.h"
32 #include "napi/native_node_api.h"
33 #include "nlohmann/json.hpp"
34 #define DM_NAPI_BUF_LENGTH (256)
35 #define DM_NAPI_CREDENTIAL_BUF_LENGTH (6000)
36 #define DM_NAPI_DESCRIPTION_BUF_LENGTH (16384)
37 
38 struct AsyncCallbackInfo {
39     napi_env env = nullptr;
40     napi_async_work asyncWork = nullptr;
41 
42     char bundleName[DM_NAPI_BUF_LENGTH] = {0};
43     size_t bundleNameLen = 0;
44 
45     napi_ref callback = nullptr;
46     int32_t status = -1;
47     int32_t ret = 0;
48 };
49 
50 struct CredentialAsyncCallbackInfo {
51     napi_env env = nullptr;
52     napi_async_work asyncWork = nullptr;
53 
54     std::string bundleName;
55     std::string reqInfo;
56     std::string returnJsonStr;
57     int32_t status = -1;
58     int32_t ret = 0;
59     napi_ref callback = nullptr;
60 };
61 
62 struct DeviceBasicInfoListAsyncCallbackInfo {
63     napi_env env = nullptr;
64     napi_async_work asyncWork = nullptr;
65 
66     std::string bundleName;
67     size_t bundleNameLen = 0;
68     std::vector<OHOS::DistributedHardware::DmDeviceBasicInfo> devList;
69     std::string extra;
70     // OHOS::DistributedHardware::DmFilterOptions filter;
71     napi_ref callback = nullptr;
72     napi_value thisVar = nullptr;
73     napi_deferred deferred = nullptr;
74     int32_t status = -1;
75     int32_t ret = 0;
76 };
77 
78 struct AuthAsyncCallbackInfo {
79     napi_env env = nullptr;
80 
81     char bundleName[DM_NAPI_BUF_LENGTH] = {0};
82     size_t bundleNameLen = 0;
83 
84     napi_ref callback = nullptr;
85     int32_t authType = -1;
86 };
87 
88 struct DmNapiStatusJsCallback {
89     std::string bundleName_;
90     uint16_t subscribeId_;
91     int32_t reason_;
92     OHOS::DistributedHardware::DmDeviceBasicInfo deviceBasicInfo_;
93 
DmNapiStatusJsCallbackDmNapiStatusJsCallback94     DmNapiStatusJsCallback(std::string bundleName, uint16_t subscribeId, int32_t reason,
95         OHOS::DistributedHardware::DmDeviceBasicInfo deviceBasicInfo_)
96         : bundleName_(bundleName), subscribeId_(subscribeId), reason_(reason), deviceBasicInfo_(deviceBasicInfo_) {}
97 };
98 
99 struct DmNapiPublishJsCallback {
100     std::string bundleName_;
101     int32_t publishId_;
102     int32_t reason_;
103 
DmNapiPublishJsCallbackDmNapiPublishJsCallback104     DmNapiPublishJsCallback(std::string bundleName, int32_t publishId, int32_t reason)
105         : bundleName_(bundleName), publishId_(publishId), reason_(reason) {}
106 };
107 
108 struct DmNapiCredentialJsCallback {
109     std::string bundleName_;
110     int32_t action_;
111     std::string credentialResult_;
112 
DmNapiCredentialJsCallbackDmNapiCredentialJsCallback113     DmNapiCredentialJsCallback(std::string bundleName, int32_t action, std::string credentialResult)
114         : bundleName_(bundleName), action_(action), credentialResult_(credentialResult) {}
115 };
116 
117 struct DmNapiAuthJsCallback {
118     std::string bundleName_;
119     std::string deviceId_;
120     std::string token_;
121     int32_t status_;
122     int32_t reason_;
123 
DmNapiAuthJsCallbackDmNapiAuthJsCallback124     DmNapiAuthJsCallback(std::string bundleName, std::string deviceId, std::string token, int32_t status,
125         int32_t reason)
126         : bundleName_(bundleName), deviceId_(deviceId), token_(token), status_(status), reason_(reason) {}
127 };
128 
129 struct DmNapiVerifyJsCallback {
130     std::string bundleName_;
131     std::string deviceId_;
132     int32_t resultCode_;
133     int32_t flag_;
134 
DmNapiVerifyJsCallbackDmNapiVerifyJsCallback135     DmNapiVerifyJsCallback(std::string bundleName, std::string deviceId, int32_t resultCode, int32_t flag)
136         : bundleName_(bundleName), deviceId_(deviceId), resultCode_(resultCode), flag_(flag) {}
137 };
138 
139 enum DmNapiDevStatusChange { UNKNOWN = 0, AVAILABLE = 1, UNAVAILABLE = 2, CHANGE = 3};
140 
141 class DmNapiInitCallback : public OHOS::DistributedHardware::DmInitCallback {
142 public:
DmNapiInitCallback(napi_env env,std::string & bundleName)143     explicit DmNapiInitCallback(napi_env env, std::string &bundleName) : env_(env), bundleName_(bundleName)
144     {
145     }
~DmNapiInitCallback()146     ~DmNapiInitCallback() override {}
147     void OnRemoteDied() override;
148 
149 private:
150     napi_env env_;
151     std::string bundleName_;
152 };
153 
154 class DmNapiDeviceStatusCallback : public OHOS::DistributedHardware::DeviceStatusCallback {
155 public:
DmNapiDeviceStatusCallback(napi_env env,std::string & bundleName)156     explicit DmNapiDeviceStatusCallback(napi_env env, std::string &bundleName) : env_(env), bundleName_(bundleName)
157     {
158     }
~DmNapiDeviceStatusCallback()159     ~DmNapiDeviceStatusCallback() override {};
160     void OnDeviceOnline(const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo) override;
161     void OnDeviceReady(const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo) override;
162     void OnDeviceOffline(const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo) override;
163     void OnDeviceChanged(const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo) override;
164 private:
165     napi_env env_;
166     std::string bundleName_;
167 };
168 
169 class DmNapiDiscoveryCallback : public OHOS::DistributedHardware::DiscoveryCallback {
170 public:
DmNapiDiscoveryCallback(napi_env env,std::string & bundleName)171     explicit DmNapiDiscoveryCallback(napi_env env, std::string &bundleName)
172         : env_(env), refCount_(0), bundleName_(bundleName)
173     {
174     }
~DmNapiDiscoveryCallback()175     ~DmNapiDiscoveryCallback() override {};
176     void OnDeviceFound(uint16_t subscribeId,
177         const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo) override;
178     void OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason) override;
179     void OnDiscoverySuccess(uint16_t subscribeId) override;
180     void IncreaseRefCount();
181     void DecreaseRefCount();
182     int32_t GetRefCount();
183 
184 private:
185     napi_env env_;
186     std::atomic<int32_t> refCount_;
187     std::string bundleName_;
188 };
189 
190 class DmNapiPublishCallback : public OHOS::DistributedHardware::PublishCallback {
191 public:
DmNapiPublishCallback(napi_env env,std::string & bundleName)192     explicit DmNapiPublishCallback(napi_env env, std::string &bundleName)
193         : env_(env), refCount_(0), bundleName_(bundleName)
194     {
195     }
~DmNapiPublishCallback()196     ~DmNapiPublishCallback() override {};
197     void OnPublishResult(int32_t publishId, int32_t publishResult) override;
198     void IncreaseRefCount();
199     void DecreaseRefCount();
200     int32_t GetRefCount();
201 
202 private:
203     napi_env env_;
204     std::atomic<int32_t> refCount_;
205     std::string bundleName_;
206 };
207 
208 class DmNapiDeviceManagerUiCallback : public OHOS::DistributedHardware::DeviceManagerUiCallback {
209 public:
DmNapiDeviceManagerUiCallback(napi_env env,std::string & bundleName)210     explicit DmNapiDeviceManagerUiCallback(napi_env env, std::string &bundleName) : env_(env), bundleName_(bundleName)
211     {
212     }
~DmNapiDeviceManagerUiCallback()213     ~DmNapiDeviceManagerUiCallback() override {};
214     void OnCall(const std::string &paramJson) override;
215 
216 private:
217     napi_env env_;
218     std::string bundleName_;
219 };
220 
221 class DmNapiCredentialCallback : public OHOS::DistributedHardware::CredentialCallback {
222 public:
DmNapiCredentialCallback(napi_env env,const std::string & bundleName)223     explicit DmNapiCredentialCallback(napi_env env, const std::string &bundleName) : env_(env), bundleName_(bundleName)
224     {
225     }
~DmNapiCredentialCallback()226     ~DmNapiCredentialCallback() override {};
227     void OnCredentialResult(int32_t &action, const std::string &credentialResult) override;
228 
229 private:
230     napi_env env_;
231     std::string bundleName_;
232 };
233 
234 class DmNapiAuthenticateCallback : public OHOS::DistributedHardware::AuthenticateCallback {
235 public:
DmNapiAuthenticateCallback(napi_env env,std::string & bundleName)236     explicit DmNapiAuthenticateCallback(napi_env env, std::string &bundleName) : env_(env), bundleName_(bundleName)
237     {
238     }
~DmNapiAuthenticateCallback()239     ~DmNapiAuthenticateCallback() override {};
240     void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, int32_t reason) override;
241 
242 private:
243     napi_env env_;
244     std::string bundleName_;
245 };
246 
247 class DmNapiVerifyAuthCallback : public OHOS::DistributedHardware::VerifyAuthCallback {
248 public:
DmNapiVerifyAuthCallback(napi_env env,std::string & bundleName)249     explicit DmNapiVerifyAuthCallback(napi_env env, std::string &bundleName) : env_(env), bundleName_(bundleName)
250     {
251     }
~DmNapiVerifyAuthCallback()252     ~DmNapiVerifyAuthCallback() override {};
253     void OnVerifyAuthResult(const std::string &deviceId, int32_t resultCode, int32_t flag) override;
254 
255 private:
256     napi_env env_;
257     std::string bundleName_;
258 };
259 
260 class DeviceManagerNapi : public DmNativeEvent {
261 public:
262     explicit DeviceManagerNapi(napi_env env, napi_value thisVar);
263     ~DeviceManagerNapi() override;
264     static napi_value Init(napi_env env, napi_value exports);
265     static napi_value Constructor(napi_env env, napi_callback_info info);
266     static napi_value EnumTypeConstructor(napi_env env, napi_callback_info info);
267     static napi_value InitDeviceStatusChangeActionEnum(napi_env env, napi_value exports);
268     static napi_value CreateDeviceManager(napi_env env, napi_callback_info info);
269     static napi_value ReleaseDeviceManager(napi_env env, napi_callback_info info);
270     static napi_value SetUserOperationSync(napi_env env, napi_callback_info info);
271     static napi_value GetAvailableDeviceListSync(napi_env env, napi_callback_info info);
272     static napi_value GetAvailableDeviceList(napi_env env, napi_callback_info info);
273     static napi_value GetLocalDeviceNetworkId(napi_env env, napi_callback_info info);
274     static napi_value GetLocalDeviceId(napi_env env, napi_callback_info info);
275     static napi_value GetLocalDeviceName(napi_env env, napi_callback_info info);
276     static napi_value GetLocalDeviceType(napi_env env, napi_callback_info info);
277     static napi_value GetDeviceName(napi_env env, napi_callback_info info);
278     static napi_value GetDeviceType(napi_env env, napi_callback_info info);
279     static napi_value StartDeviceDiscover(napi_env env, napi_callback_info info);
280     static napi_value StopDeviceDiscover(napi_env env, napi_callback_info info);
281     static napi_value PublishDeviceDiscoverySync(napi_env env, napi_callback_info info);
282     static napi_value UnPublishDeviceDiscoverySync(napi_env env, napi_callback_info info);
283     static napi_value GetAuthenticationParamSync(napi_env env, napi_callback_info info);
284     static napi_value BindTarget(napi_env env, napi_callback_info info);
285     static napi_value UnBindTarget(napi_env env, napi_callback_info info);
286     static napi_value VerifyAuthInfo(napi_env env, napi_callback_info info);
287     static napi_value RequestCredential(napi_env env, napi_callback_info info);
288     static napi_value ImportCredential(napi_env env, napi_callback_info info);
289     static napi_value DeleteCredential(napi_env env, napi_callback_info info);
290     static napi_value JsOn(napi_env env, napi_callback_info info);
291     static napi_value JsOff(napi_env env, napi_callback_info info);
292     static DeviceManagerNapi *GetDeviceManagerNapi(std::string &bundleName);
293     static void CreateDmCallback(napi_env env, std::string &bundleName, std::string &eventType);
294     static void CreateDmCallback(napi_env env, std::string &bundleName, std::string &eventType, std::string &extra);
295     static void ReleaseDmCallback(std::string &bundleName, std::string &eventType);
296     static void DeviceBasicInfoToJsArray(const napi_env &env,
297                                     const std::vector<OHOS::DistributedHardware::DmDeviceBasicInfo> &vecDevInfo,
298                                     const int32_t idx, napi_value &arrayResult);
299     static bool DmAuthParamDetection(const OHOS::DistributedHardware::DmAuthParam &authParam);
300     static void DmAuthParamToJsAuthParam(const napi_env &env, const OHOS::DistributedHardware::DmAuthParam &authParam,
301                                          napi_value &paramResult);
302     static void SetValueInt32(const napi_env &env, const std::string &fieldStr, const int32_t intValue,
303                               napi_value &result);
304     static void SetValueUtf8String(const napi_env &env, const std::string &fieldStr, const std::string &str,
305                                    napi_value &result);
306     static void JsObjectToString(const napi_env &env, const napi_value &object, const std::string &fieldStr, char *dest,
307                                  const int32_t destLen);
308     static void JsObjectToBool(const napi_env &env, const napi_value &object, const std::string &fieldStr,
309                                bool &fieldRef);
310     static void JsObjectToInt(const napi_env &env, const napi_value &object, const std::string &fieldStr,
311                               int32_t &fieldRef);
312     static std::string JsObjectToString(const napi_env &env, const napi_value &param);
313     static void JsToDmPublishInfo(const napi_env &env, const napi_value &object,
314                                   OHOS::DistributedHardware::DmPublishInfo &info);
315     static void JsToDmExtra(const napi_env &env, const napi_value &object, std::string &extra, int32_t &authType);
316     static void JsToBindParam(const napi_env &env, const napi_value &object, std::string &bindParam, int32_t &bindType);
317     static void JsToDmAuthInfo(const napi_env &env, const napi_value &object, std::string &extra);
318     static void JsToDmBuffer(const napi_env &env, const napi_value &object, const std::string &fieldStr,
319                              uint8_t **bufferPtr, int32_t &bufferLen);
320     static void JsToJsonObject(const napi_env &env, const napi_value &object, const std::string &fieldStr,
321                                nlohmann::json &jsonObj);
322     static void JsToDmDiscoveryExtra(const napi_env &env, const napi_value &object, std::string &extra);
323     static bool JsToDiscoverTargetType(napi_env env, const napi_value &object, int32_t &discoverTargetType);
324     static int32_t RegisterCredentialCallback(napi_env env, const std::string &pkgName);
325     static void AsyncAfterTaskCallback(napi_env env, napi_status status, void *data);
326     static void AsyncTaskCallback(napi_env env, void *data);
327     void OnDeviceStatusChange(DmNapiDevStatusChange action,
328                              const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo);
329     void OnDeviceFound(uint16_t subscribeId, const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo);
330     void OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason);
331     void OnPublishResult(int32_t publishId, int32_t publishResult);
332     void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, int32_t reason);
333     void OnCredentialResult(int32_t &action, const std::string &credentialResult);
334     void OnVerifyResult(const std::string &deviceId, int32_t resultCode, int32_t flag);
335     void OnDmUiCall(const std::string &paramJson);
336 
337 private:
338     static void ReleasePublishCallback(std::string &bundleName);
339     static napi_value JsOffFrench(napi_env env, int32_t num, napi_value thisVar, napi_value argv[]);
340     static napi_value JsOnFrench(napi_env env, int32_t num, napi_value thisVar, napi_value argv[]);
341     static void CallAsyncWorkSync(napi_env env,
342         DeviceBasicInfoListAsyncCallbackInfo *deviceBasicInfoListAsyncCallbackInfo);
343     static void CallAsyncWork(napi_env env, DeviceBasicInfoListAsyncCallbackInfo *deviceBasicInfoListAsyncCallbackInfo);
344     static void CallCredentialAsyncWork(napi_env env, CredentialAsyncCallbackInfo *creAsyncCallbackInfo);
345     static void CallRequestCreInfoStatus(napi_env env, napi_status &status,
346         CredentialAsyncCallbackInfo *creAsyncCallbackInfo);
347     static void CallGetAvailableDeviceListStatusSync(napi_env env, napi_status &status,
348         DeviceBasicInfoListAsyncCallbackInfo *deviceBasicInfoListAsyncCallbackInfo);
349     static void CallGetAvailableDeviceListStatus(napi_env env, napi_status &status,
350         DeviceBasicInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo);
351     static napi_value CallDeviceList(napi_env env, napi_callback_info info,
352         DeviceBasicInfoListAsyncCallbackInfo *deviceBasicInfoListAsyncCallbackInfo);
353     static napi_value GetAvailableDeviceListPromise(napi_env env,
354         DeviceBasicInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo);
355     static napi_value GetAvailableDeviceListByFilter(napi_env env, napi_callback_info info,
356         DeviceBasicInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo);
357     static bool IsSystemApp();
358     static std::string GetDeviceTypeById(OHOS::DistributedHardware::DmDeviceType type);
359 private:
360     napi_env env_;
361     static thread_local napi_ref sConstructor_;
362     std::string bundleName_;
363     static std::mutex creMapLocks_;
364     static AuthAsyncCallbackInfo authAsyncCallbackInfo_;
365     static AuthAsyncCallbackInfo verifyAsyncCallbackInfo_;
366     static CredentialAsyncCallbackInfo creAsyncCallbackInfo_;
367 };
368 #endif // OHOS_DM_NATIVE_DEVICEMANAGER_JS_H
369