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