1 /* 2 * Copyright (c) 2021 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 (8) 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 private: 94 int32_t refCount_ = 0; 95 pthread_mutex_t lock_; 96 std::string bundleName_; 97 }; 98 99 class DmJSIAuthenticateCallback : public OHOS::DistributedHardware::AuthenticateCallback { 100 public: DmJSIAuthenticateCallback(std::string & bundleName)101 explicit DmJSIAuthenticateCallback(std::string &bundleName) : bundleName_(bundleName) {} ~DmJSIAuthenticateCallback()102 virtual ~DmJSIAuthenticateCallback() {}; 103 void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, int32_t reason) override; 104 105 private: 106 std::string bundleName_; 107 }; 108 109 class DmJSICheckAuthCallback : public OHOS::DistributedHardware::VerifyAuthCallback { 110 public: DmJSICheckAuthCallback(std::string & bundleName)111 explicit DmJSICheckAuthCallback(std::string &bundleName) : bundleName_(bundleName) {} ~DmJSICheckAuthCallback()112 virtual ~DmJSICheckAuthCallback() {}; 113 void OnVerifyAuthResult(const std::string &deviceId, int32_t resultCode, int32_t flag) override; 114 115 private: 116 std::string bundleName_; 117 }; 118 119 class DmJSIDeviceManagerFaCallback : public OHOS::DistributedHardware::DeviceManagerUiCallback { 120 public: DmJSIDeviceManagerFaCallback(std::string & bundleName)121 explicit DmJSIDeviceManagerFaCallback(std::string &bundleName) : bundleName_(bundleName) {} ~DmJSIDeviceManagerFaCallback()122 virtual ~DmJSIDeviceManagerFaCallback() {}; 123 void OnCall(const std::string ¶mJson) override; 124 125 private: 126 std::string bundleName_; 127 }; 128 129 class DeviceManagerModule final : public MemoryHeap, DmNativeEvent { 130 public: 131 explicit DeviceManagerModule(); 132 virtual ~DeviceManagerModule(); 133 static JSIValue CreateDeviceManager(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 134 static JSIValue ReleaseDeviceManager(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 135 static JSIValue GetTrustedDeviceListSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 136 static JSIValue StartDeviceDiscoverSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 137 static JSIValue StopDeviceDiscoverSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 138 static JSIValue AuthenticateDevice(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 139 static JSIValue VerifyAuthInfo(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 140 static JSIValue JsOn(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 141 static JSIValue JsOff(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 142 static JSIValue SetUserOperationSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 143 static JSIValue GetAuthenticationParamSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 144 static JSIValue GetLocalDeviceInfoSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 145 static JSIValue UnAuthenticateDevice(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 146 static DeviceManagerModule *GetDeviceManagerJSI(std::string &bundleName); 147 static void AuthRsultVerifyInfoAsyncWorkFunc(void *data); 148 static char *GetJSIAppBundleName(); 149 static void DmAuthParamToJsAuthParamy(const OHOS::DistributedHardware::DmAuthParam &authParam, 150 JSIValue ¶mResult); 151 void OnDmfaCall(const std::string ¶mJson); 152 void OnVerifyResult(const std::string &deviceId, int32_t resultCode, int32_t flag); 153 void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, int32_t reason); 154 void OnDiscoverFailed(uint16_t subscribeId, int32_t failedReason); 155 void OnDeviceFound(uint16_t subscribeId, const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo); 156 void OnDeviceStateChange(DmJSIDevStateChangeAction action, 157 const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo); 158 void OnRemoteDied(); 159 static void JsToDmAuthExtra(const JSIValue ¶m, nlohmann::json &jsonObj); 160 static void JsToDmTokenInfo(const JSIValue &object, const std::string &fieldStr, nlohmann::json &jsonObj); 161 static void JsToJsonObject(const JSIValue &object, const std::string &fieldStr, nlohmann::json &jsonObj); 162 static void JsToDmBuffer(const JSIValue &object, 163 const std::string &fieldStr, uint8_t **bufferPtr, int32_t &bufferLen); 164 static void JsToDmAuthInfo(const JSIValue &object, std::string &extra); 165 static void JsToDmAppImageInfoAndDmExtra(const JSIValue &object, 166 OHOS::DistributedHardware::DmAppImageInfo& appImageInfo, 167 std::string &extra, int32_t &authType); 168 static void JsToDmDeviceInfo(const JSIValue &object, OHOS::DistributedHardware::DmDeviceInfo& info); 169 static int32_t JsToDmSubscribeInfo(const JSIValue &object, OHOS::DistributedHardware::DmSubscribeInfo& info); 170 static char *JsObjectToString(const JSIValue &object, const std::string &fieldStr); 171 static bool JsObjectToBool(const JSIValue &object, const std::string &fieldStr); 172 static int32_t JsObjectToInt(const JSIValue &object, const std::string &fieldStr); 173 static void DmAuthParamToJsAuthParam(const OHOS::DistributedHardware::DmAuthParam &authParam, 174 JSIValue ¶mResult); 175 static void CreateDmCallback(std::string &bundleName, std::string &eventType); 176 static void ReleaseDmCallback(std::string &bundleName, std::string &eventType); 177 static void DeviceInfoToJsArray(const std::vector<OHOS::DistributedHardware::DmDeviceInfo> &vecDevInfo, 178 const int32_t idx, JSIValue &arrayResult); 179 private: 180 std::string bundleName_; 181 static AuthAsyncCallbackInfo authAsyncCallbackInfo_; 182 static AuthAsyncCallbackInfo verifyAsyncCallbackInfo_; 183 }; 184 void InitDeviceManagerModule(JSIValue exports); 185 } 186 } 187 188 #endif // OHOS_DEVICE_MANAGER_NATIVE_DEVICEMANAGER_JS_H 189