• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_DEVICE_MANAGER_NATIVE_DEVICEMANAGER_JS_H
17 #define OHOS_DEVICE_MANAGER_NATIVE_DEVICEMANAGER_JS_H
18 
19 #include <memory>
20 #include <string>
21 #include <pthread.h>
22 #include "device_manager_callback.h"
23 #include "dm_native_event.h"
24 #include "dm_device_info.h"
25 #include "dm_subscribe_info.h"
26 #include "nlohmann/json.hpp"
27 #include "dm_device_info.h"
28 #include "jsi.h"
29 #include "js_ability.h"
30 #include "dm_app_image_info.h"
31 
32 namespace OHOS {
33 namespace ACELite {
34 #define DM_JSI_BUF_LENGTH (256)
35 #define DM_JSI_AUTH_REQUEST_FINISH (7)
36 
37 struct AuthFuncParams {
38     JSIValue handlerRef = JSI::CreateUndefined();
39     JSIValue thisVarRef_ = JSI::CreateUndefined();
40     const JSIValue *args = nullptr;
41     uint8_t argsSize = 0;
42 };
43 
44 struct AuthAsyncCallbackInfo {
45     JSIValue thisVal_ = JSI::CreateUndefined();
46 
47     char bundleName[DM_JSI_BUF_LENGTH] = {0};
48 
49     JSIValue callback = JSI::CreateUndefined();
50     int32_t authType = -1;
51 };
52 
53 enum DmJSIDevStateChangeAction {
54     ONLINE = 0,
55     READY = 1,
56     OFFLINE = 2,
57     CHANGE = 3
58 };
59 
60 class DmJSIInitCallback : public OHOS::DistributedHardware::DmInitCallback {
61 public:
DmJSIInitCallback(std::string & bundleName)62     explicit DmJSIInitCallback(std::string &bundleName) : bundleName_(bundleName) {}
~DmJSIInitCallback()63     virtual ~DmJSIInitCallback() {}
64     void OnRemoteDied() override;
65 
66 private:
67     std::string bundleName_;
68 };
69 
70 class DmJSIDeviceStateCallback : public OHOS::DistributedHardware::DeviceStateCallback {
71 public:
DmJSIDeviceStateCallback(std::string & bundleName)72     explicit DmJSIDeviceStateCallback(std::string &bundleName) : bundleName_(bundleName) {}
~DmJSIDeviceStateCallback()73     virtual ~DmJSIDeviceStateCallback() {};
74     void OnDeviceOnline(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override;
75     void OnDeviceReady(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override;
76     void OnDeviceOffline(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override;
77     void OnDeviceChanged(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override;
78 
79 private:
80     std::string bundleName_;
81 };
82 
83 class DmJSIDiscoverCallback : public OHOS::DistributedHardware::DiscoveryCallback {
84 public:
DmJSIDiscoverCallback(std::string & bundleName)85     explicit DmJSIDiscoverCallback(std::string &bundleName) : refCount_(0), bundleName_(bundleName) {}
~DmJSIDiscoverCallback()86     virtual ~DmJSIDiscoverCallback() {};
87     void OnDeviceFound(uint16_t subscribeId, const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override;
88     void OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason) override;
89     void OnDiscoverySuccess(uint16_t subscribeId) override;
90     void IncreaseRefCount();
91     void DecreaseRefCount();
92     int32_t GetRefCount();
93 
94 private:
95     int32_t refCount_ = 0;
96     pthread_mutex_t lock_;
97     std::string bundleName_;
98 };
99 
100 class DmJSIAuthenticateCallback : public OHOS::DistributedHardware::AuthenticateCallback {
101 public:
DmJSIAuthenticateCallback(std::string & bundleName)102     explicit DmJSIAuthenticateCallback(std::string &bundleName) : bundleName_(bundleName) {}
~DmJSIAuthenticateCallback()103     virtual ~DmJSIAuthenticateCallback() {};
104     void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, int32_t reason) override;
105 
106 private:
107     std::string bundleName_;
108 };
109 
110 class DmJSICheckAuthCallback : public OHOS::DistributedHardware::VerifyAuthCallback {
111 public:
DmJSICheckAuthCallback(std::string & bundleName)112     explicit DmJSICheckAuthCallback(std::string &bundleName) : bundleName_(bundleName) {}
~DmJSICheckAuthCallback()113     virtual ~DmJSICheckAuthCallback() {};
114     void OnVerifyAuthResult(const std::string &deviceId, int32_t resultCode, int32_t flag) override;
115 
116 private:
117     std::string bundleName_;
118 };
119 
120 class DmJSIDeviceManagerFaCallback : public OHOS::DistributedHardware::DeviceManagerUiCallback {
121 public:
DmJSIDeviceManagerFaCallback(std::string & bundleName)122     explicit DmJSIDeviceManagerFaCallback(std::string &bundleName) : bundleName_(bundleName) {}
~DmJSIDeviceManagerFaCallback()123     virtual ~DmJSIDeviceManagerFaCallback() {};
124     void OnCall(const std::string &paramJson) override;
125 
126 private:
127     std::string bundleName_;
128 };
129 
130 class DeviceManagerModule final : public MemoryHeap, DmNativeEvent {
131 public:
132     explicit DeviceManagerModule();
133     virtual ~DeviceManagerModule();
134     static JSIValue CreateDeviceManager(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
135     static JSIValue ReleaseDeviceManager(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
136     static JSIValue GetTrustedDeviceListSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
137     static JSIValue StartDeviceDiscoverSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
138     static JSIValue StopDeviceDiscoverSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
139     static JSIValue AuthenticateDevice(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
140     static JSIValue VerifyAuthInfo(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
141     static JSIValue JsOn(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
142     static JSIValue JsOff(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
143     static JSIValue SetUserOperationSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
144     static JSIValue GetAuthenticationParamSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
145     static JSIValue GetLocalDeviceInfoSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
146     static JSIValue UnAuthenticateDevice(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
147     static DeviceManagerModule *GetDeviceManagerJSI(std::string &bundleName);
148     static void AuthRsultVerifyInfoAsyncWorkFunc(void *data);
149     static char *GetJSIAppBundleName();
150     static void DmAuthParamToJsAuthParamy(const OHOS::DistributedHardware::DmAuthParam &authParam,
151                                           JSIValue &paramResult);
152     void OnDmfaCall(const std::string &paramJson);
153     void OnVerifyResult(const std::string &deviceId, int32_t resultCode, int32_t flag);
154     void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, int32_t reason);
155     void OnDiscoverFailed(uint16_t subscribeId, int32_t failedReason);
156     void OnDeviceFound(uint16_t subscribeId, const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo);
157     void OnDeviceStateChange(DmJSIDevStateChangeAction action,
158                              const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo);
159     void OnRemoteDied();
160     static void JsToDmAuthExtra(const JSIValue &param, nlohmann::json &jsonObj);
161     static void JsToDmTokenInfo(const JSIValue &object, const std::string &fieldStr, nlohmann::json &jsonObj);
162     static void JsToJsonObject(const JSIValue &object, const std::string &fieldStr, nlohmann::json &jsonObj);
163     static void JsToDmBuffer(const JSIValue &object,
164                              const std::string &fieldStr, uint8_t **bufferPtr, int32_t &bufferLen);
165     static void JsToDmAuthInfo(const JSIValue &object, std::string &extra);
166     static void JsToDmAppImageInfoAndDmExtra(const JSIValue &object,
167                                              OHOS::DistributedHardware::DmAppImageInfo& appImageInfo,
168                                              std::string &extra, int32_t &authType);
169     static void JsToDmDeviceInfo(const JSIValue &object, OHOS::DistributedHardware::DmDeviceInfo& info);
170     static int32_t JsToDmSubscribeInfo(const JSIValue &object, OHOS::DistributedHardware::DmSubscribeInfo& info);
171     static char *JsObjectToString(const JSIValue &object, const std::string &fieldStr);
172     static bool JsObjectToBool(const JSIValue &object, const std::string &fieldStr);
173     static int32_t JsObjectToInt(const JSIValue &object, const std::string &fieldStr);
174     static void DmAuthParamToJsAuthParam(const OHOS::DistributedHardware::DmAuthParam &authParam,
175                                          JSIValue &paramResult);
176     static void CreateDmCallback(std::string &bundleName, std::string &eventType);
177     static void ReleaseDmCallback(std::string &bundleName, std::string &eventType);
178     static void DeviceInfoToJsArray(const std::vector<OHOS::DistributedHardware::DmDeviceInfo> &vecDevInfo,
179                                     const int32_t idx, JSIValue &arrayResult);
180 
181 private:
182     std::string bundleName_;
183     static AuthAsyncCallbackInfo authAsyncCallbackInfo_;
184     static AuthAsyncCallbackInfo verifyAsyncCallbackInfo_;
185 };
186 void InitDeviceManagerModule(JSIValue exports);
187 }
188 }
189 
190 #endif // OHOS_DEVICE_MANAGER_NATIVE_DEVICEMANAGER_JS_H
191