diff --git a/BUILD.gn b/BUILD.gn index 7e5dfc76c562adc8cc23b22f6b9a1ff6c989cacb..374fba490c7677e6f4323621490706b4b22b9918 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -14,10 +14,21 @@ import("//build/lite/config/component/lite_component.gni") if (defined(ohos_lite)) { - lite_component("devicemanager_lite") { - if (ohos_kernel_type == "liteos_m") { - } else { - features = [] + if (ohos_kernel_type == "liteos_m") { + lite_component("devicemanager_mini") { + features = [ + "utils:devicemanagerutils_mini", + "services/devicemanagerservice:devicemanagerservice_mini", + "interfaces_mini/inner_kits/native_cpp:devicemanagersdk_mini", + "interfaces_mini/kits/js:devicemanager_native_js", + ] + } + } else { + lite_component("devicemanager_lite") { + if (ohos_kernel_type == "liteos_m") { + } else { + features = [] + } } } } diff --git a/interfaces_mini/inner_kits/native_cpp/BUILD.gn b/interfaces_mini/inner_kits/native_cpp/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..9f14b76df21321ab9d4779cb2e15e347b23f74b2 --- /dev/null +++ b/interfaces_mini/inner_kits/native_cpp/BUILD.gn @@ -0,0 +1,63 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +if (defined(ohos_lite)) { + import("//build/lite/config/component/lite_component.gni") +} else { + import("//build/ohos.gni") +} +import("//foundation/distributedhardware/devicemanager/devicemanager.gni") +innerkits_path_mini = "${devicemanager_path}/interfaces_mini/inner_kits" + +if (defined(ohos_lite)) { + if (ohos_kernel_type == "liteos_m") { + static_library("devicemanagersdk_mini") { + include_dirs = [ + "${innerkits_path_mini}/native_cpp/include", + "${innerkits_path_mini}/native_cpp/include/notify", + "${utils_path}/include", + "${common_path}/include", + "${services_path}/include/dispatch", + ] + include_dirs += [ + "//utils/native/lite/include", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog", + "//third_party/bounds_checking_function/include", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", + "//third_party/json/include", + ] + + sources = [ + "${innerkits_path_mini}/native_cpp/src/device_manager.cpp", + "${innerkits_path_mini}/native_cpp/src/device_manager_impl.cpp", + "${innerkits_path_mini}/native_cpp/src/notify/device_manager_notify.cpp", + ] + + defines = [ + "LITE_DEVICE", + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"devicemanagerkit\"", + "LOG_DOMAIN=0xD004100", + ] + + deps = [ + "${utils_path}:devicemanagerutils_mini", + "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_static", + "//foundation/distributedschedule/samgr_lite/samgr", + "//third_party/bounds_checking_function:libsec_static", + "//utils/native/lite:utils", + ] + } + } +} else { +} diff --git a/interfaces_mini/inner_kits/native_cpp/include/device_manager.h b/interfaces_mini/inner_kits/native_cpp/include/device_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..c2d27b16605c5aadf6ea1e1bd1231a4863afa1fb --- /dev/null +++ b/interfaces_mini/inner_kits/native_cpp/include/device_manager.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DEVICE_MANAGER_H +#define DEVICE_MANAGER_H + +#include +#include +#include +#include "dm_subscribe_info.h" +#include "device_manager_callback.h" +namespace OHOS { +namespace DistributedHardware { +class DeviceManager { +public: + static DeviceManager &GetInstance(); +public: + virtual int32_t InitDeviceManager(const std::string &pkgName, std::shared_ptr dmInitCallback) = 0; + virtual int32_t UnInitDeviceManager(const std::string &pkgName) = 0; + virtual int32_t GetTrustedDeviceList(const std::string &pkgName, const std::string &extra, + std::vector &deviceList) = 0; + virtual int32_t GetLocalDeviceInfo(const std::string &pkgName, DmDeviceInfo &deviceInfo) = 0; + virtual int32_t RegisterDevStateCallback(const std::string &pkgName, const std::string &extra, + std::shared_ptr callback) = 0; + virtual int32_t UnRegisterDevStateCallback(const std::string &pkgName) = 0; + virtual int32_t StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo, + const std::string &extra, std::shared_ptr callback) = 0; + virtual int32_t StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId) = 0; + virtual int32_t AuthenticateDevice(const std::string &pkgName, int32_t authType, const DmDeviceInfo &deviceInfo, + const std::string &extra, std::shared_ptr callback) = 0; + virtual int32_t UnAuthenticateDevice(const std::string &pkgName, const std::string &deviceId) = 0; + virtual int32_t VerifyAuthentication(const std::string &pkgName, const std::string &authPara, + std::shared_ptr callback) = 0; + virtual int32_t RegisterDeviceManagerFaCallback(const std::string &pkgName, + std::shared_ptr callback) = 0; + virtual int32_t UnRegisterDeviceManagerFaCallback(const std::string &pkgName) = 0; + virtual int32_t GetFaParam(const std::string &pkgName, DmAuthParam &faParam) = 0; + virtual int32_t SetUserOperation(const std::string &pkgName, int32_t action) = 0; + virtual int32_t GetUdidByNetworkId(const std::string &pkgName, const std::string &netWorkId, std::string &udid) = 0; + virtual int32_t GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId, std::string &uuid) = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_H diff --git a/interfaces_mini/inner_kits/native_cpp/include/device_manager_callback.h b/interfaces_mini/inner_kits/native_cpp/include/device_manager_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..74bcc5985a732eacdb134c06186d97bde271dbe0 --- /dev/null +++ b/interfaces_mini/inner_kits/native_cpp/include/device_manager_callback.h @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DEVICE_MANAGER_CALLBACK_H +#define OHOS_DEVICE_MANAGER_CALLBACK_H + +#include +#include + +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +class DmInitCallback { +public: + virtual ~DmInitCallback() + { + } + virtual void OnRemoteDied() = 0; +}; + +class DeviceStateCallback { +public: + virtual ~DeviceStateCallback() + { + } + virtual void OnDeviceOnline(const DmDeviceInfo &deviceInfo) = 0; + virtual void OnDeviceReady(const DmDeviceInfo &deviceInfo) = 0; + virtual void OnDeviceOffline(const DmDeviceInfo &deviceInfo) = 0; + virtual void OnDeviceChanged(const DmDeviceInfo &deviceInfo) = 0; +}; + +class DiscoveryCallback { +public: + virtual ~DiscoveryCallback() + { + } + virtual void OnDiscoverySuccess(uint16_t subscribeId) = 0; + virtual void OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason) = 0; + virtual void OnDeviceFound(uint16_t subscribeId, const DmDeviceInfo &deviceInfo) = 0; +}; + +class AuthenticateCallback { +public: + virtual ~AuthenticateCallback() + { + } + virtual void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, + int32_t reason) = 0; +}; + +class VerifyAuthCallback { +public: + virtual ~VerifyAuthCallback() + { + } + virtual void OnVerifyAuthResult(const std::string &deviceId, int32_t resultCode, const std::string flag) = 0; +}; + +class DeviceManagerFaCallback { +public: + virtual ~DeviceManagerFaCallback() + { + } + virtual void OnCall(const std::string ¶mJson) = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_CALLBACK_H diff --git a/interfaces_mini/inner_kits/native_cpp/include/device_manager_impl.h b/interfaces_mini/inner_kits/native_cpp/include/device_manager_impl.h new file mode 100644 index 0000000000000000000000000000000000000000..64d1982e50b381715bac5b992394617caf5ada46 --- /dev/null +++ b/interfaces_mini/inner_kits/native_cpp/include/device_manager_impl.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DEVICE_MANAGER_IMPL_H +#define OHOS_DEVICE_MANAGER_IMPL_H + +#include +#include +#include "device_manager.h" +#include "single_instance.h" + + +namespace OHOS { +namespace DistributedHardware { +class DeviceManagerImpl : public DeviceManager { +#define DEVICEMANAGER_MESSAGE_FAILED (-1) +DECLARE_SINGLE_INSTANCE(DeviceManagerImpl); +public: + virtual int32_t InitDeviceManager(const std::string &pkgName, + std::shared_ptr dmInitCallback) override; + virtual int32_t UnInitDeviceManager(const std::string &pkgName) override; + virtual int32_t GetTrustedDeviceList(const std::string &pkgName, const std::string &extra, + std::vector &deviceList) override; + virtual int32_t GetLocalDeviceInfo(const std::string &pkgName, DmDeviceInfo &deviceInfo) override; + virtual int32_t RegisterDevStateCallback(const std::string &pkgName, const std::string &extra, + std::shared_ptr callback) override; + virtual int32_t UnRegisterDevStateCallback(const std::string &pkgName) override; + virtual int32_t StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo, + const std::string &extra, + std::shared_ptr callback) override; + virtual int32_t StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId) override; + virtual int32_t AuthenticateDevice(const std::string &pkgName, int32_t authType, const DmDeviceInfo &deviceInfo, + const std::string &extra, + std::shared_ptr callback) override; + virtual int32_t UnAuthenticateDevice(const std::string &pkgName, const std::string &deviceId) override; + virtual int32_t VerifyAuthentication(const std::string &pkgName, const std::string &authPara, + std::shared_ptr callback) override; + virtual int32_t RegisterDeviceManagerFaCallback(const std::string &pkgName, + std::shared_ptr callback) override; + virtual int32_t UnRegisterDeviceManagerFaCallback(const std::string &pkgName) override; + virtual int32_t GetFaParam(const std::string &pkgName, DmAuthParam &faParam) override; + virtual int32_t SetUserOperation(const std::string &pkgName, int32_t action) override; + virtual int32_t GetUdidByNetworkId(const std::string &pkgName, const std::string &netWorkId, + std::string &udid) override; + virtual int32_t GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId, + std::string &uuid) override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IMPL_H diff --git a/interfaces_mini/inner_kits/native_cpp/include/dm_app_image_info.h b/interfaces_mini/inner_kits/native_cpp/include/dm_app_image_info.h new file mode 100644 index 0000000000000000000000000000000000000000..927de61dfd5863dee384101e0bc8e343820e747b --- /dev/null +++ b/interfaces_mini/inner_kits/native_cpp/include/dm_app_image_info.h @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DEVICE_MANAGER_APP_IMAGE_INFO_H +#define OHOS_DEVICE_MANAGER_APP_IMAGE_INFO_H + +#include + +#include "securec.h" + +namespace OHOS { +namespace DistributedHardware { +class DmAppImageInfo { +public: + DmAppImageInfo() = default; + explicit DmAppImageInfo(uint8_t *appIcon_, int32_t appIconLen_, uint8_t *appThumbnail_, int32_t appThumbnailLen_) + { + SaveData(appIcon_, appIconLen_, appThumbnail_, appThumbnailLen_); + } + + void Reset(uint8_t *appIcon_, int32_t appIconLen_, uint8_t *appThumbnail_, int32_t appThumbnailLen_) + { + SaveData(appIcon_, appIconLen_, appThumbnail_, appThumbnailLen_); + } + + void ResetIcon(uint8_t *appIcon_, int32_t appIconLen_) + { + SaveIconData(appIcon_, appIconLen_); + } + + void InitThumbnail(int32_t appThumbnailLen_) + { + if (appThumbnailLen_ <= 0 || appThumbnailLen_ > THUMB_MAX_LEN) { + appThumbnailLen = 0; + appThumbnail = nullptr; + return; + } + + appThumbnail = new (std::nothrow) uint8_t[appThumbnailLen_] {0}; + if (appThumbnail != nullptr) { + appThumbnailLen = appThumbnailLen_; + } + } + + int32_t SetThumbnailData(uint8_t *srcBuffer, int32_t srcBufferLen, int32_t copyIndex, int32_t copyLen) + { + if (srcBuffer == nullptr || srcBufferLen <= 0 || copyLen > srcBufferLen || copyIndex < 0) { + return -1; + } + + if ((copyIndex + copyLen) > appThumbnailLen) { + return -1; + } + + if (appThumbnail == nullptr) { + return -1; + } + + if (memcpy_s(appThumbnail + copyIndex, appThumbnailLen - copyIndex, srcBuffer, (uint32_t)copyLen) != 0) { + return -1; + } + + return 0; + } + + ~DmAppImageInfo() + { + if (appIcon != nullptr) { + delete[] appIcon; + appIcon = nullptr; + } + if (appThumbnail != nullptr) { + delete[] appThumbnail; + appThumbnail = nullptr; + } + } + + DmAppImageInfo(const DmAppImageInfo &other) + { + if (this != &other) { + *this = other; + } + } + + DmAppImageInfo& operator=(const DmAppImageInfo &other) + { + if (this != &other) { + SaveData(other.GetAppIcon(), other.GetAppIconLen(), other.GetAppThumbnail(), other.GetAppThumbnailLen()); + } + return *this; + } + + DmAppImageInfo(DmAppImageInfo&&) = delete; + DmAppImageInfo& operator=(DmAppImageInfo&&) = delete; + + int32_t GetAppIconLen() const + { + return appIconLen; + } + + const uint8_t *GetAppIcon() const + { + return appIcon; + } + + int32_t GetAppThumbnailLen() const + { + return appThumbnailLen; + } + + const uint8_t *GetAppThumbnail() const + { + return appThumbnail; + } +private: + void SaveData(const uint8_t *appIcon_, int32_t appIconLen_, const uint8_t *appThumbnail_, int32_t appThumbnailLen_) + { + SaveIconData(appIcon_, appIconLen_); + SaveThumbnailData(appThumbnail_, appThumbnailLen_); + } + + void SaveIconData(const uint8_t *appIcon_, int32_t appIconLen_) + { + if (appIconLen_ > 0 && appIconLen_ < ICON_MAX_LEN && appIcon_ != nullptr) { + if (appIconLen < appIconLen_) { + if (appIcon != nullptr && appIconLen > 0) { + delete[] appIcon; + appIcon = nullptr; + appIconLen = 0; + } + appIcon = new (std::nothrow) uint8_t[appIconLen_] {0}; + } + if (appIcon != nullptr) { + appIconLen = appIconLen_; + if (memcpy_s(appIcon, (uint32_t)appIconLen, appIcon_, appIconLen_) != 0) { + return; + } + } + } + } + + void SaveThumbnailData(const uint8_t *appThumbnail_, int32_t appThumbnailLen_) + { + if (appThumbnailLen_ > 0 && appThumbnailLen_ < THUMB_MAX_LEN && appThumbnail_ != nullptr) { + if (appThumbnailLen < appThumbnailLen_) { + if (appThumbnail != nullptr && appThumbnailLen > 0) { + delete[] appThumbnail; + appThumbnail = nullptr; + appThumbnailLen = 0; + } + appThumbnail = new (std::nothrow) uint8_t[appThumbnailLen_] {0}; + } + if (appThumbnail != nullptr) { + appThumbnailLen = appThumbnailLen_; + if (memcpy_s(appThumbnail, (uint32_t)appThumbnailLen, appThumbnail_, appThumbnailLen_) != 0) { + return; + } + } + } + } +private: + int32_t appIconLen {0}; + uint8_t *appIcon {nullptr}; + int32_t appThumbnailLen {0}; + uint8_t *appThumbnail {nullptr}; + const int32_t ICON_MAX_LEN = 32 * 1024; + const int32_t THUMB_MAX_LEN = 153 * 1024; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_APP_IMAGE_INFO_H diff --git a/interfaces_mini/inner_kits/native_cpp/include/dm_device_info.h b/interfaces_mini/inner_kits/native_cpp/include/dm_device_info.h new file mode 100644 index 0000000000000000000000000000000000000000..26088b8bbdb2771f2d1c9e1bc4cee6748b58907d --- /dev/null +++ b/interfaces_mini/inner_kits/native_cpp/include/dm_device_info.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DEVICE_MANAGER_DEVICE_INFO_H +#define OHOS_DEVICE_MANAGER_DEVICE_INFO_H + +#include + +#include "dm_app_image_info.h" + +#define DM_MAX_DEVICE_ID_LEN (96) +#define DM_MAX_DEVICE_NAME_LEN (128) + +namespace OHOS { +namespace DistributedHardware { +typedef enum DmDeviceType { + DEVICE_TYPE_UNKNOWN = 0x00, + DEVICE_TYPE_WIFI_CAMERA = 0x08, + DEVICE_TYPE_AUDIO = 0x0A, + DEVICE_TYPE_PC = 0x0C, + DEVICE_TYPE_PHONE = 0x0E, + DEVICE_TYPE_PAD = 0x11, + DEVICE_TYPE_WATCH = 0x6D, + DEVICE_TYPE_CAR = 0x83, + DEVICE_TYPE_TV = 0x9C, +} DmDeviceType; + +typedef enum DmDeviceState { + DEVICE_STATE_UNKNOWN = -1, + DEVICE_STATE_ONLINE = 0, + DEVICE_INFO_READY = 1, + DEVICE_STATE_OFFLINE = 2, + DEVICE_INFO_CHANGED = 3, +} DmDeviceState; + +typedef struct DmDeviceInfo { + char deviceId[DM_MAX_DEVICE_ID_LEN]; + char deviceName[DM_MAX_DEVICE_NAME_LEN]; + uint16_t deviceTypeId; + char networkId[DM_MAX_DEVICE_ID_LEN]; +} DmDeviceInfo; + +typedef struct DmAuthParam { + std::string authToken; + std::string packageName; + std::string appName; + std::string appDescription; + int32_t authType; + int32_t business; + int32_t pincode; + int32_t direction; + int32_t pinToken; + DmAppImageInfo imageinfo; +} DmAuthParam; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_DEVICE_INFO_H diff --git a/interfaces_mini/inner_kits/native_cpp/include/dm_subscribe_info.h b/interfaces_mini/inner_kits/native_cpp/include/dm_subscribe_info.h new file mode 100644 index 0000000000000000000000000000000000000000..91e12f88fd6ffdcfefc0f277bb9a81321d842995 --- /dev/null +++ b/interfaces_mini/inner_kits/native_cpp/include/dm_subscribe_info.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DEVICE_MANAGER_SUBSCRIBE_INFO_H +#define OHOS_DEVICE_MANAGER_SUBSCRIBE_INFO_H + +#include + +#define DM_MAX_DEVICE_CAPABILITY_LEN 65 + +namespace OHOS { +namespace DistributedHardware { +typedef enum DmDiscoverMode { + /* Passive */ + DM_DISCOVER_MODE_PASSIVE = 0x55, + /* Proactive */ + DM_DISCOVER_MODE_ACTIVE = 0xAA +} DmDiscoverMode; + +typedef enum DmExchangeMedium { + /** Automatic medium selection */ + DM_AUTO = 0, + /** Bluetooth */ + DM_BLE = 1, + /** Wi-Fi */ + DM_COAP = 2, + /** USB */ + DM_USB = 3, + DM_MEDIUM_BUTT +} DmExchangeMedium; + +/** + * @brief Enumerates frequencies for publishing services. + * + * This enumeration applies only to Bluetooth and is not supported currently. + */ +typedef enum DmExchangeFreq { + /** Low */ + DM_LOW = 0, + /** Medium */ + DM_MID = 1, + /** High */ + DM_HIGH = 2, + /** Super-high */ + DM_SUPER_HIGH = 3, + DM_FREQ_BUTT +} DmExchangeFreq; + +typedef struct DmSubscribeInfo { + /** Service ID */ + uint16_t subscribeId; + /** Discovery mode for service subscription. For details, see {@link DmDiscoverMode}. */ + DmDiscoverMode mode; + /** Service subscription medium. For details, see {@link DmExchangeMedium}. */ + DmExchangeMedium medium; + /** Service subscription frequency. For details, see {@link DmExchangeFreq}. */ + DmExchangeFreq freq; + /** only find the device with the same account */ + bool isSameAccount; + /** find the sleeping devices */ + bool isWakeRemote; + /** Service subscription capability. */ + char capability[DM_MAX_DEVICE_CAPABILITY_LEN]; +} DmSubscribeInfo; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_SUBSCRIBE_INFO_H diff --git a/interfaces_mini/inner_kits/native_cpp/include/notify/device_manager_notify.h b/interfaces_mini/inner_kits/native_cpp/include/notify/device_manager_notify.h new file mode 100644 index 0000000000000000000000000000000000000000..5aea5f48a4a64f32fc21f472a785dfdde7e3d712 --- /dev/null +++ b/interfaces_mini/inner_kits/native_cpp/include/notify/device_manager_notify.h @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DEVICE_MANAGER_NOTIFY_H +#define OHOS_DEVICE_MANAGER_NOTIFY_H + +#include +#include +#include +#include +#include +#include + +#include "dm_device_info.h" +#include "dm_subscribe_info.h" +#include "device_manager_callback.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedHardware { +class DeviceManagerNotify { +DECLARE_SINGLE_INSTANCE(DeviceManagerNotify); +public: + void RegisterDeathRecipientCallback(const std::string &pkgName, std::shared_ptr dmInitCallback); + void UnRegisterDeathRecipientCallback(const std::string &pkgName); + void RegisterDeviceStateCallback(const std::string &pkgName, std::shared_ptr callback); + void UnRegisterDeviceStateCallback(const std::string &pkgName); + void RegisterDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId, + std::shared_ptr callback); + void UnRegisterDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId); + void RegisterAuthenticateCallback(const std::string &pkgName, const std::string &deviceId, + std::shared_ptr callback); + void UnRegisterAuthenticateCallback(const std::string &pkgName, const std::string &deviceId); + void UnRegisterPackageCallback(const std::string &pkgName); + void RegisterVerifyAuthenticationCallback(const std::string &pkgName, const std::string &authPara, + std::shared_ptr callback); + void UnRegisterVerifyAuthenticationCallback(const std::string &pkgName); + void RegisterDeviceManagerFaCallback(const std::string &pkgName, std::shared_ptr callback); + void UnRegisterDeviceManagerFaCallback(const std::string &pkgName); + +public: + void OnRemoteDied(); + void OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &deviceInfo); + void OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &deviceInfo); + void OnDeviceChanged(const std::string &pkgName, const DmDeviceInfo &deviceInfo); + void OnDeviceReady(const std::string &pkgName, const DmDeviceInfo &deviceInfo); + void OnDeviceFound(const std::string &pkgName, uint16_t subscribeId, const DmDeviceInfo &deviceInfo); + void OnDiscoveryFailed(const std::string &pkgName, uint16_t subscribeId, int32_t failedReason); + void OnDiscoverySuccess(const std::string &pkgName, uint16_t subscribeId); + void OnAuthResult(const std::string &pkgName, const std::string &deviceId, const std::string &token, + uint32_t status, uint32_t reason); + void OnVerifyAuthResult(const std::string &pkgName, const std::string &deviceId, + int32_t resultCode, const std::string flag); + void OnFaCall(std::string &pkgName, std::string ¶mJson); + void AddPkgName(const std::string &pkgName); + void DeletePkgName(const std::string &pkgName); + const std::list& GetPkgNameList() const; +private: + std::map> deviceStateCallback_; + std::map>> deviceDiscoveryCallbacks_; + std::map>> authenticateCallback_; + std::map> verifyAuthCallback_; + std::map> dmInitCallback_; + std::map> dmFaCallback_; + std::list dmPkgName_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_NOTIFY_H diff --git a/interfaces_mini/inner_kits/native_cpp/src/device_manager.cpp b/interfaces_mini/inner_kits/native_cpp/src/device_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9a3b456db1cff0de8b3b5ebb053ac931c8c2c3f --- /dev/null +++ b/interfaces_mini/inner_kits/native_cpp/src/device_manager.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "device_manager.h" + +#include "device_manager_impl.h" + +namespace OHOS { +namespace DistributedHardware { +DeviceManager &DeviceManager::GetInstance() +{ + return DeviceManagerImpl::GetInstance(); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces_mini/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces_mini/inner_kits/native_cpp/src/device_manager_impl.cpp new file mode 100644 index 0000000000000000000000000000000000000000..218c1575cb4989b9ecb2d5626811b076394c6a1f --- /dev/null +++ b/interfaces_mini/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -0,0 +1,425 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "command_dispatch.h" +#include "dm_log.h" +#include "device_manager_notify.h" +#include "dm_constants.h" +#include "message_def.h" +#include "get_trustdevice_req.h" +#include "get_trustdevice_rsp.h" +#include "start_discovery_req.h" +#include "stop_discovery_req.h" +#include "set_useroperation_req.h" +#include "get_authenticationparam_rsp.h" +#include "authenticate_device_req.h" +#include "verify_authenticate_req.h" +#include "get_local_device_info_rsp.h" +#include "get_info_by_network_rsp.h" +#include "get_info_by_network_req.h" +#include "unauthenticate_device_req.h" +#include "get_dmfaparam_rsp.h" +#include "device_manager_impl.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(DeviceManagerImpl); + +int32_t DeviceManagerImpl::InitDeviceManager(const std::string &pkgName, std::shared_ptr dmInitCallback) +{ + LOGI("DeviceManager::InitDeviceManager start, pkgName: %s", pkgName.c_str()); + if (pkgName.empty() || dmInitCallback == nullptr) { + LOGE("InitDeviceManager error: Invalid parameter"); + return DM_INVALID_VALUE; + } + + DeviceManagerNotify::GetInstance().AddPkgName(pkgName); + DeviceManagerNotify::GetInstance().RegisterDeathRecipientCallback(pkgName, dmInitCallback); + LOGI("InitDeviceManager success"); + return DM_OK; +} + +int32_t DeviceManagerImpl::UnInitDeviceManager(const std::string &pkgName) +{ + LOGI("DeviceManager::UnInitDeviceManager start, pkgName: %s", pkgName.c_str()); + if (pkgName.empty()) { + LOGE("InitDeviceManager error: Invalid parameter"); + return DM_INVALID_VALUE; + } + + DeviceManagerNotify::GetInstance().DeletePkgName(pkgName); + DeviceManagerNotify::GetInstance().UnRegisterPackageCallback(pkgName); + LOGI("UnInitDeviceManager success"); + return DM_OK; +} + +int32_t DeviceManagerImpl::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra, + std::vector &deviceList) +{ + LOGI("DeviceManager::GetTrustedDeviceList start, pkgName: %s", pkgName.c_str()); + if (pkgName.empty()) { + LOGE("GetTrustedDeviceList error: Invalid para"); + return DM_INVALID_VALUE; + } + + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetExtra(extra); + + if (CommandDispatch::GetInstance().MessageSendCmd(GET_TRUST_DEVICE_LIST, req, rsp) != DM_OK) { + return DEVICEMANAGER_MESSAGE_FAILED; + } + + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("GetTrustedDeviceList error: failed ret: %d", ret); + return ret; + } + + deviceList = rsp->GetDeviceVec(); + LOGI("GetTrustedDeviceList completed, pkgName: %s", pkgName.c_str()); + return DM_OK; +} + +int32_t DeviceManagerImpl::GetLocalDeviceInfo(const std::string &pkgName, DmDeviceInfo &info) +{ + LOGI("DeviceManager::GetLocalDeviceInfo start, pkgName: %s", pkgName.c_str()); + if (pkgName.empty()) { + LOGE("GetLocalDeviceInfo error: Invalid para"); + return DM_INVALID_VALUE; + } + + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + + if (CommandDispatch::GetInstance().MessageSendCmd(GET_LOCAL_DEVICE_INFO, req, rsp) != DM_OK) { + return DEVICEMANAGER_MESSAGE_FAILED; + } + + if (rsp->GetErrCode() != DM_OK) { + LOGE("GetLocalDeviceInfo error: failed ret: %d", rsp->GetErrCode()); + return DM_IPC_RESPOND_ERROR; + } + + info = rsp->GetLocalDeviceInfo(); + LOGI("GetLocalDeviceInfo completed,pkgname%s", req->GetPkgName().c_str()); + return DM_OK; +} + +int32_t DeviceManagerImpl::RegisterDevStateCallback(const std::string &pkgName, const std::string &extra, + std::shared_ptr callback) +{ + LOGI("DeviceManager::RegisterDevStateCallback start, pkgName: %s", pkgName.c_str()); + if (pkgName.empty() || callback == nullptr) { + LOGE("RegisterDevStateCallback error: Invalid para"); + return DM_INVALID_VALUE; + } + + DeviceManagerNotify::GetInstance().RegisterDeviceStateCallback(pkgName, callback); + LOGI("RegisterDevStateCallback completed, pkgName: %s", pkgName.c_str()); + return DM_OK; +} + +int32_t DeviceManagerImpl::UnRegisterDevStateCallback(const std::string &pkgName) +{ + LOGI("DeviceManager::UnRegisterDevStateCallback start, pkgName: %s", pkgName.c_str()); + if (pkgName.empty()) { + LOGE("UnRegisterDevStateCallback error: Invalid para"); + return DM_INVALID_VALUE; + } + + DeviceManagerNotify::GetInstance().UnRegisterDeviceStateCallback(pkgName); + LOGI("UnRegisterDevStateCallback completed, pkgName: %s", pkgName.c_str()); + return DM_OK; +} +int32_t DeviceManagerImpl::StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo, + const std::string &extra, std::shared_ptr callback) +{ + LOGI("DeviceManager::StartDeviceDiscovery start, pkgName: %s", pkgName.c_str()); + if (pkgName.empty() || callback == nullptr) { + LOGE("StartDeviceDiscovery error: Invalid para"); + return DM_INVALID_VALUE; + } + + LOGI("DeviceManager StartDeviceDiscovery in, pkgName %s", pkgName.c_str()); + DeviceManagerNotify::GetInstance().RegisterDiscoveryCallback(pkgName, subscribeInfo.subscribeId, callback); + + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetExtra(extra); + req->SetSubscribeInfo(subscribeInfo); + + if (CommandDispatch::GetInstance().MessageSendCmd(START_DEVICE_DISCOVER, req, rsp) != DM_OK) { + return DEVICEMANAGER_MESSAGE_FAILED; + } + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("StartDeviceDiscovery error: Failed with ret %d", ret); + return ret; + } + return DM_OK; +} + +int32_t DeviceManagerImpl::StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId) +{ + LOGI("DeviceManager::StopDeviceDiscovery start , pkgName: %s", pkgName.c_str()); + if (pkgName.empty()) { + LOGE("StopDeviceDiscovery error: Invalid para"); + return DM_INVALID_VALUE; + } + + LOGI("StopDeviceDiscovery in, pkgName %s", pkgName.c_str()); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetSubscribeId(subscribeId); + + if (CommandDispatch::GetInstance().MessageSendCmd(STOP_DEVICE_DISCOVER, req, rsp) != DM_OK) { + return DEVICEMANAGER_MESSAGE_FAILED; + } + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("StopDeviceDiscovery error: Failed with ret %d", ret); + return ret; + } + + DeviceManagerNotify::GetInstance().UnRegisterDiscoveryCallback(pkgName, subscribeId); + LOGI("StopDeviceDiscovery completed, pkgName: %s", pkgName.c_str()); + return DM_OK; +} + +int32_t DeviceManagerImpl::AuthenticateDevice(const std::string &pkgName, int32_t authType, + const DmDeviceInfo &deviceInfo, const std::string &extra, + std::shared_ptr callback) +{ + LOGI("DeviceManager::AuthenticateDevice start , pkgName: %s", pkgName.c_str()); + if (pkgName.empty()) { + LOGE("AuthenticateDevice error: Invalid para"); + return DM_INVALID_VALUE; + } + + std::string strDeviceId = deviceInfo.deviceId; + DeviceManagerNotify::GetInstance().RegisterAuthenticateCallback(pkgName, strDeviceId, callback); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetExtra(extra); + req->SetAuthType(authType); + req->SetDeviceInfo(deviceInfo); + + if (CommandDispatch::GetInstance().MessageSendCmd(AUTHENTICATE_DEVICE, req, rsp) != DM_OK) { + return DEVICEMANAGER_MESSAGE_FAILED; + } + + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("AuthenticateDevice error: Failed with ret %d", ret); + return ret; + } + + LOGI("DeviceManager::AuthenticateDevice completed, pkgName: %s", pkgName.c_str()); + return DM_OK; +} + +int32_t DeviceManagerImpl::UnAuthenticateDevice(const std::string &pkgName, const std::string &deviceId) +{ + LOGI("DeviceManager::UnAuthenticateDevice start , pkgName: %s, deviceId: %s", pkgName.c_str(), deviceId.c_str()); + if (deviceId.empty()) { + LOGE("UnAuthenticateDevice error: Invalid para"); + return DM_INVALID_VALUE; + } + + DmDeviceInfo deviceInfo; + strcpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, deviceId.c_str()); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetDeviceInfo(deviceInfo); + + if (CommandDispatch::GetInstance().MessageSendCmd(UNAUTHENTICATE_DEVICE, req, rsp) != DM_OK) { + return DEVICEMANAGER_MESSAGE_FAILED; + } + + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("UnAuthenticateDevice error: Failed with ret %d", ret); + return ret; + } + + LOGI("UnAuthenticateDevice completed, pkgName: %s", pkgName.c_str()); + return DM_OK; +} + +int32_t DeviceManagerImpl::RegisterDeviceManagerFaCallback(const std::string &pkgName, + std::shared_ptr callback) +{ + LOGI("DeviceManager::RegisterDeviceManagerFaCallback start, pkgName: %s", pkgName.c_str()); + if (pkgName.empty() || callback == nullptr) { + LOGE("RegisterDeviceManagerFaCallback error: Invalid para"); + return DM_INVALID_VALUE; + } + + DeviceManagerNotify::GetInstance().RegisterDeviceManagerFaCallback(pkgName, callback); + LOGI("DeviceManager::RegisterDevStateCallback completed, pkgName: %s", pkgName.c_str()); + return DM_OK; +} + +int32_t DeviceManagerImpl::UnRegisterDeviceManagerFaCallback(const std::string &pkgName) +{ + LOGI("DeviceManager::UnRegisterDeviceManagerFaCallback start, pkgName: %s", pkgName.c_str()); + if (pkgName.empty()) { + LOGE("UnRegisterDeviceManagerFaCallback error: Invalid para"); + return DM_INVALID_VALUE; + } + + DeviceManagerNotify::GetInstance().UnRegisterDeviceManagerFaCallback(pkgName); + LOGI("DeviceManager::UnRegisterDevStateCallback completed, pkgName: %s", pkgName.c_str()); + return DM_OK; +} +int32_t DeviceManagerImpl::VerifyAuthentication(const std::string &pkgName, const std::string &authPara, + std::shared_ptr callback) +{ + LOGI("DeviceManager::VerifyAuthentication start, pkgName: %s", pkgName.c_str()); + if (pkgName.empty()) { + LOGE("VerifyAuthentication error: Invalid para"); + return DM_INVALID_VALUE; + } + + DeviceManagerNotify::GetInstance().RegisterVerifyAuthenticationCallback(pkgName, authPara, callback); + + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetAuthPara(authPara); + + if (CommandDispatch::GetInstance().MessageSendCmd(VERIFY_AUTHENTICATION, req, rsp) != DM_OK) { + return DEVICEMANAGER_MESSAGE_FAILED; + } + + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("VerifyAuthentication error: Failed with ret %d", ret); + return ret; + } + + LOGI("VerifyAuthentication completed, pkgName: %s", pkgName.c_str()); + return DM_OK; +} + +int32_t DeviceManagerImpl::GetFaParam(const std::string &pkgName, DmAuthParam &dmFaParam) +{ + LOGI("DeviceManager::GetFaParam start, pkgName: %s", pkgName.c_str()); + if (pkgName.empty()) { + LOGE("GetFaParam failed, pkgName is empty"); + return DM_INVALID_VALUE; + } + + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + + if (CommandDispatch::GetInstance().MessageSendCmd(SERVER_GET_DMFA_INFO, req, rsp) != DM_OK) { + return DEVICEMANAGER_MESSAGE_FAILED; + } + + dmFaParam = rsp->GetDmAuthParam(); + LOGI("GetFaParam completed, pkgName: %s", pkgName.c_str()); + return DM_OK; +} + +int32_t DeviceManagerImpl::SetUserOperation(const std::string &pkgName, int32_t action) +{ + LOGI("DeviceManager::SetUserOperation start, pkgName: %s", pkgName.c_str()); + + if (pkgName.empty()) { + LOGE("VerifyAuthentication failed, pkgName is empty"); + return DM_INVALID_VALUE; + } + + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + + req->SetPkgName(pkgName); + req->SetOperation(action); + + if (CommandDispatch::GetInstance().MessageSendCmd(SERVER_USER_AUTH_OPERATION, req, rsp) != DM_OK) { + return DEVICEMANAGER_MESSAGE_FAILED; + } + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("SetUserOperation Failed with ret %d", ret); + return ret; + } + + LOGI("SetUserOperation completed, pkgName: %s", pkgName.c_str()); + return DM_OK; +} + +int32_t DeviceManagerImpl::GetUdidByNetworkId(const std::string &pkgName, const std::string &netWorkId, + std::string &udid) +{ + if (pkgName.empty()) { + LOGE("GetUdidByNetworkId failed, pkgName is empty"); + return DM_INVALID_VALUE; + } + + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetNetWorkId(netWorkId); + + if (CommandDispatch::GetInstance().MessageSendCmd(GET_UDID_BY_NETWORK, req, rsp) != DM_OK) { + return DEVICEMANAGER_MESSAGE_FAILED; + } + + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("DeviceManager::GetUdidByNetworkId Failed with ret %d", ret); + return ret; + } + + udid = rsp->GetUdid(); + return DM_OK; +} + +int32_t DeviceManagerImpl::GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId, + std::string &uuid) +{ + if (pkgName.empty()) { + LOGE("GetUuidByNetworkId failed, pkgName is empty"); + return DM_INVALID_VALUE; + } + + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetNetWorkId(netWorkId); + + if (CommandDispatch::GetInstance().MessageSendCmd(GET_UUID_BY_NETWORK, req, rsp) != DM_OK) { + return DEVICEMANAGER_MESSAGE_FAILED; + } + + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("GetUuidByNetworkId Failed with ret %d", ret); + return ret; + } + uuid = rsp->GetUuid(); + return DM_OK; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces_mini/inner_kits/native_cpp/src/notify/device_manager_notify.cpp b/interfaces_mini/inner_kits/native_cpp/src/notify/device_manager_notify.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a665978c04cbb884bf50b2213b881d8bc352c7da --- /dev/null +++ b/interfaces_mini/inner_kits/native_cpp/src/notify/device_manager_notify.cpp @@ -0,0 +1,275 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "device_manager_notify.h" +#include "dm_log.h" +#include "nlohmann/json.hpp" +#include "dm_constants.h" +#include "device_manager.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(DeviceManagerNotify); + +void DeviceManagerNotify::RegisterDeathRecipientCallback(const std::string &pkgName, + std::shared_ptr dmInitCallback) +{ + dmInitCallback_[pkgName] = dmInitCallback; +} + +void DeviceManagerNotify::UnRegisterDeathRecipientCallback(const std::string &pkgName) +{ + dmInitCallback_.erase(pkgName); +} + +void DeviceManagerNotify::RegisterDeviceStateCallback(const std::string &pkgName, + std::shared_ptr callback) +{ + deviceStateCallback_[pkgName] = callback; +} + +void DeviceManagerNotify::UnRegisterDeviceStateCallback(const std::string &pkgName) +{ + deviceStateCallback_.erase(pkgName); +} + +void DeviceManagerNotify::RegisterDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId, + std::shared_ptr callback) +{ + if (deviceDiscoveryCallbacks_.count(pkgName) == 0) { + deviceDiscoveryCallbacks_[pkgName] = std::map>(); + } + deviceDiscoveryCallbacks_[pkgName][subscribeId] = callback; +} + +void DeviceManagerNotify::UnRegisterDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId) +{ + if (deviceDiscoveryCallbacks_.count(pkgName) > 0) { + deviceDiscoveryCallbacks_[pkgName].erase(subscribeId); + if (deviceDiscoveryCallbacks_[pkgName].empty()) { + deviceDiscoveryCallbacks_.erase(pkgName); + } + } +} + +void DeviceManagerNotify::RegisterAuthenticateCallback(const std::string &pkgName, const std::string &deviceId, + std::shared_ptr callback) +{ + if (authenticateCallback_.count(pkgName) == 0) { + authenticateCallback_[pkgName] = std::map>(); + } + authenticateCallback_[pkgName][deviceId] = callback; +} + +void DeviceManagerNotify::UnRegisterAuthenticateCallback(const std::string &pkgName, const std::string &deviceId) +{ + if (authenticateCallback_.count(pkgName) > 0) { + authenticateCallback_[pkgName].erase(deviceId); + if (authenticateCallback_[pkgName].empty()) { + authenticateCallback_.erase(pkgName); + } + } +} + +void DeviceManagerNotify::UnRegisterPackageCallback(const std::string &pkgName) +{ + deviceStateCallback_.erase(pkgName); + deviceDiscoveryCallbacks_.erase(pkgName); + authenticateCallback_.erase(pkgName); + dmInitCallback_.erase(pkgName); +} + +void DeviceManagerNotify::RegisterVerifyAuthenticationCallback(const std::string &pkgName, const std::string &authPara, + std::shared_ptr callback) +{ + verifyAuthCallback_[pkgName] = callback; +} + +void DeviceManagerNotify::UnRegisterVerifyAuthenticationCallback(const std::string &pkgName) +{ + verifyAuthCallback_.erase(pkgName); +} + +void DeviceManagerNotify::RegisterDeviceManagerFaCallback(const std::string &pkgName, + std::shared_ptr callback) +{ + dmFaCallback_[pkgName] = callback; +} + +void DeviceManagerNotify::UnRegisterDeviceManagerFaCallback(const std::string &pkgName) +{ + if (dmFaCallback_.count(pkgName) == 0) { + LOGE("DeviceManager UnRegisterDeviceManagerFaCallback not register"); + return; + } + dmFaCallback_.erase(pkgName); +} + + +void DeviceManagerNotify::OnRemoteDied() +{ + LOGW("DeviceManager : OnRemoteDied"); + for (auto iter : dmInitCallback_) { + iter.second->OnRemoteDied(); + } +} + +void DeviceManagerNotify::OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &deviceInfo) +{ + LOGI("DeviceManager OnDeviceOnline pkgName:%s", pkgName.c_str()); + if (deviceStateCallback_.count(pkgName) == 0) { + LOGE("DeviceManager OnDeviceOnlinecallback not register"); + return; + } + deviceStateCallback_[pkgName]->OnDeviceOnline(deviceInfo); +} + +void DeviceManagerNotify::OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &deviceInfo) +{ + LOGI("DeviceManager OnDeviceOffline pkgName:%s", pkgName.c_str()); + + if (deviceStateCallback_.count(pkgName) == 0) { + LOGE("DeviceManager OnDeviceOfflinecallback not register"); + return; + } + deviceStateCallback_[pkgName]->OnDeviceOffline(deviceInfo); +} + +void DeviceManagerNotify::OnDeviceChanged(const std::string &pkgName, const DmDeviceInfo &deviceInfo) +{ + LOGI("DeviceManager OnDeviceChanged pkgName:%s", pkgName.c_str()); + + if (deviceStateCallback_.count(pkgName) == 0) { + LOGE("DeviceManager OnDeviceChangedcallback not register"); + return; + } + deviceStateCallback_[pkgName]->OnDeviceChanged(deviceInfo); +} + +void DeviceManagerNotify::OnDeviceFound(const std::string &pkgName, uint16_t subscribeId, + const DmDeviceInfo &deviceInfo) +{ + LOGI("DeviceManager OnDeviceFound pkgName:%s, subscribeId:%d.", pkgName.c_str(), (int32_t)subscribeId); + if (deviceDiscoveryCallbacks_.count(pkgName) == 0) { + LOGE("DeviceManager OnDeviceFound: no register DiscoveryCallback for this package"); + return; + } + std::map> &discoverCallMap = deviceDiscoveryCallbacks_[pkgName]; + auto iter = discoverCallMap.find(subscribeId); + if (iter == discoverCallMap.end()) { + LOGE("DeviceManager OnDeviceFound: no register DiscoveryCallback for subscribeId %d", subscribeId); + return; + } + iter->second->OnDeviceFound(subscribeId, deviceInfo); +} + +void DeviceManagerNotify::OnDiscoveryFailed(const std::string &pkgName, uint16_t subscribeId, int32_t failedReason) +{ + LOGI("DeviceManager OnDiscoveryFailed pkgName:%s, subscribeId %d, reason %d", pkgName.c_str(), subscribeId, + failedReason); + + if (deviceDiscoveryCallbacks_.count(pkgName) == 0) { + LOGE("DeviceManager OnDiscoveryFailed: no register DiscoveryCallback for this package"); + return; + } + std::map> &discoverCallMap = deviceDiscoveryCallbacks_[pkgName]; + auto iter = discoverCallMap.find(subscribeId); + if (iter == discoverCallMap.end()) { + LOGE("DeviceManager OnDiscoveryFailed: no register DiscoveryCallback for subscribeId %d", subscribeId); + return; + } + iter->second->OnDiscoveryFailed(subscribeId, failedReason); +} + +void DeviceManagerNotify::OnDiscoverySuccess(const std::string &pkgName, uint16_t subscribeId) +{ + LOGI("DeviceManager OnDiscoverySuccess pkgName:%s, subscribeId:%d.", pkgName.c_str(), subscribeId); + + if (deviceDiscoveryCallbacks_.count(pkgName) == 0) { + LOGE("DeviceManager OnDiscoverySuccess: no register DiscoveryCallback for this package"); + return; + } + std::map> &discoverCallMap = deviceDiscoveryCallbacks_[pkgName]; + auto iter = discoverCallMap.find(subscribeId); + if (iter == discoverCallMap.end()) { + LOGE("DeviceManager OnDiscoverySuccess: no register DiscoveryCallback for subscribeId %d", subscribeId); + return; + } + iter->second->OnDiscoverySuccess(subscribeId); +} + +void DeviceManagerNotify::OnAuthResult(const std::string &pkgName, const std::string &deviceId, + const std::string &token, uint32_t status, uint32_t reason) +{ + LOGI("DeviceManagerNotify::OnAuthResult pkgName:%s, status:%d, reason:%d", pkgName.c_str(), status, reason); + + if (authenticateCallback_.count(pkgName) == 0) { + LOGE("DeviceManager OnAuthResult: no register authCallback for this package"); + return; + } + std::map> &authCallMap = authenticateCallback_[pkgName]; + auto iter = authCallMap.find(deviceId); + if (iter == authCallMap.end()) { + LOGE("DeviceManager OnAuthResult: no register authCallback for deviceID "); + return; + } + iter->second->OnAuthResult(deviceId, token, status, reason); + authenticateCallback_[pkgName].erase(deviceId); + if (authenticateCallback_[pkgName].empty()) { + authenticateCallback_.erase(pkgName); + } +} + +void DeviceManagerNotify::OnVerifyAuthResult(const std::string &pkgName, const std::string &deviceId, + int32_t resultCode, const std::string flag) +{ + LOGI("DeviceManagerNotify::OnCheckAuthResult pkgName:%s, resultCode:%d, flag:%d", pkgName.c_str(), resultCode, + flag); + + if (verifyAuthCallback_.count(pkgName) == 0) { + LOGE("DeviceManager OnCheckAuthResult: no register authCallback for this package"); + return; + } + verifyAuthCallback_[pkgName]->OnVerifyAuthResult(deviceId, resultCode, flag); + verifyAuthCallback_.erase(pkgName); +} + +void DeviceManagerNotify::OnFaCall(std::string &pkgName, std::string ¶mJson) +{ + LOGI("DeviceManager OnFaCallback pkgName:%s", pkgName.c_str()); + + if (dmFaCallback_.count(pkgName) == 0) { + LOGE("DeviceManager DmFaCallback not register"); + return; + } + dmFaCallback_[pkgName]->OnCall(paramJson); +} + +void DeviceManagerNotify::AddPkgName(const std::string &pkgName) +{ + dmPkgName_.push_back(pkgName); +} + +void DeviceManagerNotify::DeletePkgName(const std::string &pkgName) +{ + dmPkgName_.remove(pkgName); +} + +const std::list& DeviceManagerNotify::GetPkgNameList() const +{ + return dmPkgName_; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces_mini/kits/js/BUILD.gn b/interfaces_mini/kits/js/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..fa83c91b6875bfafdaaf9807ae4ceb94d3a585be --- /dev/null +++ b/interfaces_mini/kits/js/BUILD.gn @@ -0,0 +1,56 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +if (defined(ohos_lite)) { + import("//build/lite/config/component/lite_component.gni") +} else { + import("//build/ohos.gni") +} + +import("//foundation/distributedhardware/devicemanager/devicemanager.gni") +innerkits_path_mini = "${devicemanager_path}/interfaces_mini/inner_kits" +kits_path = "${devicemanager_path}/interfaces_mini/kits" + +if (ohos_kernel_type == "liteos_m") { + static_library("devicemanager_native_js") { + include_dirs = [ + "//third_party/node/src", + "//third_party/json/include", + "${common_path}/include", + "//utils/native/base/include", + "${kits_path}/js/include", + "${utils_path}/include", + "//foundation/ace/ace_engine_lite/interfaces/innerkits/builtin/base", + "//foundation/ace/ace_engine_lite/interfaces/innerkits/builtin/jsi", + "//foundation/ace/ace_engine_lite/interfaces/innerkits/builtin/async", + "//foundation/ace/ace_engine_lite/frameworks/include/context", + "${innerkits_path_mini}/native_cpp/include", + ] + + sources = [ + "${kits_path}/js/src/dm_native_event.cpp", + "${kits_path}/js/src/native_devicemanager_js.cpp", + ] + + deps = [ + "${innerkits_path_mini}/native_cpp:devicemanagersdk_mini", + "${utils_path}:devicemanagerutils_mini", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"devicemanagerkit_js\"", + "LOG_DOMAIN=0xD004100", + ] + } +} diff --git a/interfaces_mini/kits/js/include/dm_native_event.h b/interfaces_mini/kits/js/include/dm_native_event.h new file mode 100644 index 0000000000000000000000000000000000000000..c5987b3bfc7c1adbb52ea7083d8a9639ff8cdbc2 --- /dev/null +++ b/interfaces_mini/kits/js/include/dm_native_event.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_DEVICE_MANAGER_NATIVE_EVENT_H +#define OHOS_DEVICE_MANAGER_NATIVE_EVENT_H + +#include +#include +#include +#include "jsi.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace ACELite { +struct DmEventListener { + std::string eventType; + JSIValue handlerRef = JSI::CreateUndefined(); + JSIValue thisVarRef_ = JSI::CreateUndefined(); +}; + +struct FuncParams { + JSIValue handlerRef = JSI::CreateUndefined(); + JSIValue thisVarRef_ = JSI::CreateUndefined(); + const JSIValue *args = nullptr; + uint8_t argsSize = 0; +}; + +class DmNativeEvent { +public: + DmNativeEvent(); + DmNativeEvent(JSIValue thisVar); + virtual ~DmNativeEvent(); + virtual void On(std::string &eventType, JSIValue handler, JSIValue thisVal); + virtual void Off(std::string &eventType); + virtual void OnEvent(const std::string &eventType, uint8_t argsSize, const JSIValue *data); + static void OnEventAsyncWorkFunc(void *data); +protected: + static std::map> eventMap_; +}; +} +} + +#endif /* OHOS_DEVICE_MANAGER_NATIVE_EVENT_H */ \ No newline at end of file diff --git a/interfaces_mini/kits/js/include/native_devicemanager_js.h b/interfaces_mini/kits/js/include/native_devicemanager_js.h new file mode 100644 index 0000000000000000000000000000000000000000..de8469189e821a30cc09c91a1407edbe9ae6d65e --- /dev/null +++ b/interfaces_mini/kits/js/include/native_devicemanager_js.h @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DEVICE_MANAGER_NATIVE_DEVICEMANAGER_JS_H +#define OHOS_DEVICE_MANAGER_NATIVE_DEVICEMANAGER_JS_H + +#include +#include +#include +#include "device_manager_callback.h" +#include "dm_native_event.h" +#include "dm_device_info.h" +#include "dm_subscribe_info.h" +#include "nlohmann/json.hpp" +#include "dm_device_info.h" +#include "jsi.h" +#include "js_ability.h" +#include "dm_app_image_info.h" + +namespace OHOS { +namespace ACELite { +#define DM_JSI_BUF_LENGTH (256) + +struct AuthFuncParams { + JSIValue handlerRef = JSI::CreateUndefined(); + JSIValue thisVarRef_ = JSI::CreateUndefined(); + const JSIValue *args = nullptr; + uint8_t argsSize = 0; +}; + +struct AuthAsyncCallbackInfo { + JSIValue thisVal_ = JSI::CreateUndefined(); + + char bundleName[DM_JSI_BUF_LENGTH] = {0}; + + JSIValue callback = JSI::CreateUndefined(); + int32_t authType = -1; +}; + +enum DmJSIDevStateChangeAction { + ONLINE = 0, + READY = 1, + OFFLINE = 2, + CHANGE = 3 +}; + +class DmJSIInitCallback : public OHOS::DistributedHardware::DmInitCallback { +public: + explicit DmJSIInitCallback(std::string &bundleName) : bundleName_(bundleName) {} + virtual ~DmJSIInitCallback() {} + void OnRemoteDied() override; + +private: + std::string bundleName_; +}; + +class DmJSIDeviceStateCallback : public OHOS::DistributedHardware::DeviceStateCallback { +public: + explicit DmJSIDeviceStateCallback(std::string &bundleName) : bundleName_(bundleName) {} + virtual ~DmJSIDeviceStateCallback() {}; + void OnDeviceOnline(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override; + void OnDeviceReady(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override; + void OnDeviceOffline(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override; + void OnDeviceChanged(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override; + +private: + std::string bundleName_; +}; + +class DmJSIDiscoverCallback : public OHOS::DistributedHardware::DiscoveryCallback { +public: + explicit DmJSIDiscoverCallback(std::string &bundleName) : refCount_(0), bundleName_(bundleName) {} + virtual ~DmJSIDiscoverCallback() {}; + void OnDeviceFound(uint16_t subscribeId, const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override; + void OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason) override; + void OnDiscoverySuccess(uint16_t subscribeId) override; + void IncreaseRefCount(); + void DecreaseRefCount(); + int32_t GetRefCount(); +private: + int32_t refCount_ = 0; + pthread_mutex_t lock_; + std::string bundleName_; +}; + +class DmJSIAuthenticateCallback : public OHOS::DistributedHardware::AuthenticateCallback { +public: + explicit DmJSIAuthenticateCallback(std::string &bundleName) : bundleName_(bundleName) {} + virtual ~DmJSIAuthenticateCallback() {}; + void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, int32_t reason) override; + +private: + std::string bundleName_; +}; + +class DmJSICheckAuthCallback : public OHOS::DistributedHardware::VerifyAuthCallback { +public: + explicit DmJSICheckAuthCallback(std::string &bundleName) : bundleName_(bundleName) {} + virtual ~DmJSICheckAuthCallback() {}; + void OnVerifyAuthResult(const std::string &deviceId, int32_t resultCode, const std::string flag) override; + +private: + std::string bundleName_; +}; + +class DmJSIDeviceManagerFaCallback : public OHOS::DistributedHardware::DeviceManagerFaCallback { +public: + explicit DmJSIDeviceManagerFaCallback(std::string &bundleName) : bundleName_(bundleName) {} + virtual ~DmJSIDeviceManagerFaCallback() {}; + void OnCall(const std::string ¶mJson) override; + +private: + std::string bundleName_; +}; + +class DeviceManagerModule final : public MemoryHeap, DmNativeEvent { +public: + explicit DeviceManagerModule(); + virtual ~DeviceManagerModule(); + static JSIValue CreateDeviceManager(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); + static JSIValue ReleaseDeviceManager(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); + static JSIValue GetTrustedDeviceListSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); + static JSIValue StartDeviceDiscoverSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); + static JSIValue StopDeviceDiscoverSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); + static JSIValue AuthenticateDevice(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); + static JSIValue VerifyAuthInfo(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); + static JSIValue JsOn(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); + static JSIValue JsOff(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); + static JSIValue SetUserOperationSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); + static JSIValue GetAuthenticationParamSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); + static JSIValue GetLocalDeviceInfoSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); + static JSIValue UnAuthenticateDevice(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); + static DeviceManagerModule *GetDeviceManagerJSI(std::string &bundleName); + static void AuthRsultVerifyInfoAsyncWorkFunc(void *data); + static char *GetJSIAppBundleName(); + static void DmAuthParamToJsAuthParamy(const OHOS::DistributedHardware::DmAuthParam &authParam, + JSIValue ¶mResult); + void OnDmfaCall(const std::string ¶mJson); + void OnVerifyResult(const std::string &deviceId, int32_t resultCode, const std::string flag); + void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, int32_t reason); + void OnDiscoverFailed(uint16_t subscribeId, int32_t failedReason); + void OnDeviceFound(uint16_t subscribeId, const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo); + void OnDeviceStateChange(DmJSIDevStateChangeAction action, + const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo); + void OnRemoteDied(); + static void JsToDmAuthExtra(const JSIValue ¶m, nlohmann::json &jsonObj); + static void JsToDmTokenInfo(const JSIValue &object, const std::string &fieldStr, nlohmann::json &jsonObj); + static void JsToJsonObject(const JSIValue &object, const std::string &fieldStr, nlohmann::json &jsonObj); + static void JsToDmBuffer(const JSIValue &object, + const std::string &fieldStr, uint8_t **bufferPtr, int32_t &bufferLen); + static void JsToDmAuthInfo(const JSIValue &object, std::string &extra); + static void JsToDmAppImageInfoAndDmExtra(const JSIValue &object, + OHOS::DistributedHardware::DmAppImageInfo& appImageInfo, + std::string &extra, int32_t &authType); + static void JsToDmDeviceInfo(const JSIValue &object, OHOS::DistributedHardware::DmDeviceInfo& info); + static int32_t JsToDmSubscribeInfo(const JSIValue &object, OHOS::DistributedHardware::DmSubscribeInfo& info); + static char *JsObjectToString(const JSIValue &object, const std::string &fieldStr); + static bool JsObjectToBool(const JSIValue &object, const std::string &fieldStr); + static int32_t JsObjectToInt(const JSIValue &object, const std::string &fieldStr); + static void DmAuthParamToJsAuthParam(const OHOS::DistributedHardware::DmAuthParam &authParam, + JSIValue ¶mResult); + static void CreateDmCallback(std::string &bundleName, std::string &eventType); + static void ReleaseDmCallback(std::string &bundleName, std::string &eventType); + static void DeviceInfoToJsArray(const std::vector &vecDevInfo, + const int32_t idx, JSIValue &arrayResult); +private: + std::string bundleName_; + static AuthAsyncCallbackInfo authAsyncCallbackInfo_; + static AuthAsyncCallbackInfo verifyAsyncCallbackInfo_; +}; +void InitDeviceManagerModule(JSIValue exports); +} +} + +#endif // OHOS_DEVICE_MANAGER_NATIVE_DEVICEMANAGER_JS_H diff --git a/interfaces_mini/kits/js/src/dm_native_event.cpp b/interfaces_mini/kits/js/src/dm_native_event.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c63e0a9f70a0e9c64576d2eb82a3463b444f1d15 --- /dev/null +++ b/interfaces_mini/kits/js/src/dm_native_event.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dm_native_event.h" +#include "jsi.h" +#include "dm_log.h" +#include "js_async_work.h" + +using namespace OHOS::DistributedHardware; +using namespace std; + +namespace OHOS { +namespace ACELite { +std::map> DmNativeEvent::eventMap_; + +DmNativeEvent::DmNativeEvent() +{ + LOGI("DmNativeEvent::DmNativeEvent() in"); +} + +DmNativeEvent::DmNativeEvent(JSIValue thisVar) +{ + LOGI("DmNativeEvent::DmNativeEvent(JSIValue thisVar) in"); +} + +DmNativeEvent::~DmNativeEvent() +{ + LOGI("DmNativeEvent::~DmNativeEvent() in"); +} + +void DmNativeEvent::On(std::string &eventType, JSIValue handle, JSIValue thisVal) +{ + LOGI("DmNativeEvent On in for event: %s", eventType.c_str()); + std::shared_ptr listener= std::make_shared(); + + listener->eventType = eventType; + listener->handlerRef = JSI::AcquireValue(handle); + + listener->thisVarRef_ = JSI::AcquireValue(thisVal); + eventMap_[eventType] = listener; +} + +void DmNativeEvent::Off(std::string &eventType) +{ + LOGI("DmNativeEvent Off in for event: %s", eventType.c_str()); + auto iter = eventMap_.find(eventType); + if (iter == eventMap_.end()) { + LOGE("eventType %s not find", eventType.c_str()); + return; + } + std::shared_ptr listener = iter->second; + JSI::ReleaseValue(listener->handlerRef); + + JSI::ReleaseValue(listener->thisVarRef_); + eventMap_.erase(eventType); +} + +void DmNativeEvent::OnEvent(const std::string &eventType, uint8_t argsSize, const JSIValue *data) +{ + LOGI("OnEvent for %s", eventType.c_str()); + + auto iter = eventMap_.find(eventType); + if (iter == eventMap_.end()) { + LOGE("eventType %s not find", eventType.c_str()); + return; + } + auto listener = iter->second; + if (!JSI::ValueIsFunction(listener->handlerRef)) { + LOGI("OnEvent for %s handlerRef is null", eventType.c_str()); + return; + } + + FuncParams* params = new FuncParams(); + params->handlerRef = listener->handlerRef; + params->thisVarRef_ = listener->thisVarRef_; + params->args = data; + params->argsSize = argsSize; + + LOGI("OnEventAsyncWorkFunc for %s in", eventType.c_str()); + JsAsyncWork::DispatchAsyncWork(OnEventAsyncWorkFunc, reinterpret_cast(params)); +} + +void DmNativeEvent::OnEventAsyncWorkFunc(void *data) +{ + LOGI("OnEventAsyncWorkFunc in "); + FuncParams* params = reinterpret_cast(data); + JSI::CallFunction(params->handlerRef, params->thisVarRef_, params->args, params->argsSize); +} +} +} \ No newline at end of file diff --git a/interfaces_mini/kits/js/src/native_devicemanager_js.cpp b/interfaces_mini/kits/js/src/native_devicemanager_js.cpp new file mode 100644 index 0000000000000000000000000000000000000000..96e33d37cc2009aa49d56bfefcedfb4a7d4eef2c --- /dev/null +++ b/interfaces_mini/kits/js/src/native_devicemanager_js.cpp @@ -0,0 +1,1191 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "native_devicemanager_js.h" +#include +#include "nlohmann/json.hpp" +#include "device_manager.h" +#include "dm_constants.h" +#include "jsi_types.h" +#include "js_async_work.h" +#include "dm_log.h" +#include "dm_device_info.h" + +using namespace OHOS::DistributedHardware; +using namespace std; + +namespace OHOS { +namespace ACELite { +const std::string DM_JSI_EVENT_DEVICE_STATE_CHANGE = "deviceStateChange"; +const std::string DM_JSI_EVENT_DEVICE_FOUND = "deviceFound"; +const std::string DM_JSI_EVENT_DEVICE_DISCOVER_FAIL = "discoverFail"; +const std::string DM_JSI_EVENT_DMFA_CALLBACK = "dmFaCallback"; +const std::string DM_JSI_EVENT_DEVICE_SERVICE_DIE = "serviceDie"; + +const std::string DEVICE_MANAGER_JSI_CLASS_NAME = "DeviceManager"; +const uint8_t DM_JSI_ARGS_ONE = 1; +const uint8_t DM_JSI_ARGS_TWO = 2; +const uint8_t DM_JSI_ARGS_THREE = 3; +const int32_t DM_JSI_SUB_ID_MAX = 65535; + +const int32_t DM_AUTH_TYPE_PINCODE = 1; +const int32_t DM_AUTH_DIRECTION_CLIENT = 1; + +const int32_t DM_JSI_SUBSCRIBE_CAPABILITY_DDMP = 0; +const int32_t DM_JSI_SUBSCRIBE_CAPABILITY_OSD = 1; +const char *DM_CAPABILITY_OSD = "osdCapability"; + +std::map g_deviceManagerMap; +std::map> g_initCallbackMap; +std::map> g_deviceStateCallbackMap; +std::map> g_discoverCallbackMap; +std::map> g_authCallbackMap; +std::map> g_checkAuthCallbackMap; +std::map> g_dmfaCallbackMap; + +AuthAsyncCallbackInfo DeviceManagerModule::authAsyncCallbackInfo_; +AuthAsyncCallbackInfo DeviceManagerModule::verifyAsyncCallbackInfo_; + +DeviceManagerModule *DeviceManagerModule::GetDeviceManagerJSI(std::string &bundleName) +{ + auto iter = g_deviceManagerMap.find(bundleName); + if (iter == g_deviceManagerMap.end()) { + return nullptr; + } + return iter->second; +} + +DeviceManagerModule::DeviceManagerModule() : DmNativeEvent() +{ + LOGI("new DeviceManagerModule is success"); +} + +DeviceManagerModule::~DeviceManagerModule() +{ +} + +void DmJSIInitCallback::OnRemoteDied() +{ + DeviceManagerModule *deviceManagerJSI = DeviceManagerModule::GetDeviceManagerJSI(bundleName_); + if (deviceManagerJSI == nullptr) { + LOGE("OnRemoteDied, deviceManagerJSI not find for bunderName %s", bundleName_.c_str()); + return; + } + deviceManagerJSI->OnRemoteDied(); +} + +void DeviceManagerModule::OnRemoteDied() +{ + OnEvent("serviceDie", 0, nullptr); +} + +void DmJSIDeviceStateCallback::OnDeviceOnline(const DmDeviceInfo &deviceInfo) +{ + DeviceManagerModule *deviceManagerJSI = DeviceManagerModule::GetDeviceManagerJSI(bundleName_); + if (deviceManagerJSI == nullptr) { + LOGE("OnDeviceOnline, deviceManagerJSI not find for bunderName %s", bundleName_.c_str()); + return; + } + deviceManagerJSI->OnDeviceStateChange(DmJSIDevStateChangeAction::ONLINE, deviceInfo); +} + +void DmJSIDeviceStateCallback::OnDeviceReady(const DmDeviceInfo &deviceInfo) +{ + DeviceManagerModule *deviceManagerJSI = DeviceManagerModule::GetDeviceManagerJSI(bundleName_); + if (deviceManagerJSI == nullptr) { + LOGE("OnDeviceOnline, deviceManagerJSI not find for bunderName %s", bundleName_.c_str()); + return; + } + deviceManagerJSI->OnDeviceStateChange(DmJSIDevStateChangeAction::READY, deviceInfo); +} + +void DmJSIDeviceStateCallback::OnDeviceOffline(const DmDeviceInfo &deviceInfo) +{ + DeviceManagerModule *deviceManagerJSI = DeviceManagerModule::GetDeviceManagerJSI(bundleName_); + if (deviceManagerJSI == nullptr) { + LOGE("OnDeviceOffline, deviceManagerJSI not find for bunderName %s", bundleName_.c_str()); + return; + } + deviceManagerJSI->OnDeviceStateChange(DmJSIDevStateChangeAction::OFFLINE, deviceInfo); +} + +void DmJSIDeviceStateCallback::OnDeviceChanged(const DmDeviceInfo &deviceInfo) +{ + DeviceManagerModule *deviceManagerJSI = DeviceManagerModule::GetDeviceManagerJSI(bundleName_); + if (deviceManagerJSI == nullptr) { + LOGE("OnDeviceChanged, deviceManagerJSI not find for bunderName %s", bundleName_.c_str()); + return; + } + deviceManagerJSI->OnDeviceStateChange(DmJSIDevStateChangeAction::CHANGE, deviceInfo); +} + +void DmJSIDiscoverCallback::OnDeviceFound(uint16_t subscribeId, const DmDeviceInfo &deviceInfo) +{ + DeviceManagerModule *deviceManagerJSI = DeviceManagerModule::GetDeviceManagerJSI(bundleName_); + if (deviceManagerJSI == nullptr) { + LOGE("OnDeviceFound, deviceManagerJSI not find for bunderName %s", bundleName_.c_str()); + return; + } + + LOGI("OnDeviceFound for %s, subscribeId %d", bundleName_.c_str(), (int32_t)subscribeId); + deviceManagerJSI->OnDeviceFound(subscribeId, deviceInfo); +} + +void DmJSIDiscoverCallback::OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason) +{ + DeviceManagerModule *deviceManagerJSI = DeviceManagerModule::GetDeviceManagerJSI(bundleName_); + if (deviceManagerJSI == nullptr) { + LOGE("OnDiscoverFailed, deviceManagerJSI not find for bunderName %s", bundleName_.c_str()); + return; + } + + deviceManagerJSI->OnDiscoverFailed(subscribeId, failedReason); +} + +void DmJSIDiscoverCallback::OnDiscoverySuccess(uint16_t subscribeId) +{ + DeviceManagerModule *deviceManagerJSI = DeviceManagerModule::GetDeviceManagerJSI(bundleName_); + if (deviceManagerJSI == nullptr) { + LOGE("OnDiscoverySuccess, deviceManagerJSI not find for bunderName %s", bundleName_.c_str()); + return; + } + LOGI("DiscoverySuccess for %s, subscribeId %d", bundleName_.c_str(), (int32_t)subscribeId); +} + +void DmJSIAuthenticateCallback::OnAuthResult(const std::string &deviceId, const std::string &token, + int32_t status, int32_t reason) +{ + DeviceManagerModule *deviceManagerJSI = DeviceManagerModule::GetDeviceManagerJSI(bundleName_); + if (deviceManagerJSI == nullptr) { + LOGE("OnAuthResult, deviceManagerJSI not find for bunderName %s", bundleName_.c_str()); + return; + } + deviceManagerJSI->OnAuthResult(deviceId, token, status, reason); +} + +void DmJSICheckAuthCallback::OnVerifyAuthResult(const std::string &deviceId, int32_t resultCode, const std::string flag) +{ + DeviceManagerModule *deviceManagerJSI = DeviceManagerModule::GetDeviceManagerJSI(bundleName_); + if (deviceManagerJSI == nullptr) { + LOGE("OnCheckAuthResult, deviceManagerJSI not find for bunderName %s", bundleName_.c_str()); + return; + } + deviceManagerJSI->OnVerifyResult(deviceId, resultCode, flag); +} + +void DmJSIDeviceManagerFaCallback::OnCall(const std::string ¶mJson) +{ + DeviceManagerModule *deviceManagerJSI = DeviceManagerModule::GetDeviceManagerJSI(bundleName_); + if (deviceManagerJSI == nullptr) { + LOGE("OnCall, deviceManagerJSI not find for bunderName %s", bundleName_.c_str()); + return; + } + deviceManagerJSI->OnDmfaCall(paramJson); +} + +void DmJSIDiscoverCallback::IncreaseRefCount() +{ + uint32_t ret = pthread_mutex_init(&lock_, NULL); + if (ret != 0) { + LOGE("init mutex lock failed: %d.", ret); + } + pthread_mutex_lock(&lock_); + refCount_++; + pthread_mutex_unlock(&lock_); + LOGI("IncreaseRefCount: refCount_:%d.", refCount_); + ret = pthread_mutex_destroy(&lock_); + if (ret != 0) { + LOGE("destroy mutex lock failed: %d.", ret); + } +} + +void DmJSIDiscoverCallback::DecreaseRefCount() +{ + uint32_t ret = pthread_mutex_init(&lock_, NULL); + if (ret != 0) { + LOGE("init mutex lock failed: %d.", ret); + } + pthread_mutex_lock(&lock_); + refCount_--; + pthread_mutex_unlock(&lock_); + + LOGI("DecreaseRefCount: refCount_:%d.", refCount_); + ret = pthread_mutex_destroy(&lock_); + if (ret != 0) { + LOGE("destroy mutex lock failed: %d.", ret); + } +} + +int32_t DmJSIDiscoverCallback::GetRefCount() +{ + return refCount_; +} + +void DeviceManagerModule::OnDeviceStateChange(DmJSIDevStateChangeAction action, const DmDeviceInfo &deviceInfo) +{ + JSIValue result = JSI::CreateObject(); + JSI::SetNumberProperty(result, "action", (double)action); + + JSIValue device = JSI::CreateObject(); + JSI::SetStringProperty(device, "deviceId", deviceInfo.deviceId); + JSI::SetStringProperty(device, "deviceName", deviceInfo.deviceName); + JSI::SetNumberProperty(device, "deviceTypeId", (double)deviceInfo.deviceTypeId); + + JSIValue param[2] = {result, device}; + OnEvent("deviceStateChange", DM_JSI_ARGS_TWO, param); + JSI::ReleaseValueList(result, device, ARGS_END); +} + +void DeviceManagerModule::OnDeviceFound(uint16_t subscribeId, const DmDeviceInfo &deviceInfo) +{ + LOGI("OnDeviceFound for subscribeId %d", (int32_t)subscribeId); + JSIValue result = JSI::CreateObject(); + JSI::SetNumberProperty(result, "subscribeId", (double)subscribeId); + + JSIValue device = JSI::CreateObject(); + JSI::SetStringProperty(device, "deviceId", deviceInfo.deviceId); + JSI::SetStringProperty(device, "deviceName", deviceInfo.deviceName); + JSI::SetNumberProperty(device, "deviceTypeId", (double)deviceInfo.deviceTypeId); + LOGI("OnDeviceFound subscribeId %ld ", subscribeId); + LOGI("OnDeviceFound deviceId %s ", deviceInfo.deviceId); + LOGI("OnDeviceFound deviceName %s ", deviceInfo.deviceName); + LOGI("OnDeviceFound deviceTypeId %x ", deviceInfo.deviceTypeId); + + JSIValue param[2] = {result, device}; + OnEvent("deviceFound", DM_JSI_ARGS_TWO, param); + JSI::ReleaseValueList(result, device, ARGS_END); +} + +void DeviceManagerModule::OnDiscoverFailed(uint16_t subscribeId, int32_t failedReason) +{ + LOGI("OnDiscoverFailed for subscribeId %d", (int32_t)subscribeId); + JSIValue result = JSI::CreateObject(); + JSI::SetNumberProperty(result, "subscribeId", (double)subscribeId); + JSIValue reason = JSI::CreateObject(); + JSI::SetNumberProperty(reason, "reason", (double)failedReason); + + JSIValue param[2] = {result, reason}; + OnEvent("discoverFail", DM_JSI_ARGS_TWO, param); + JSI::ReleaseValueList(result, reason, ARGS_END); +} + +void DeviceManagerModule::OnDmfaCall(const std::string ¶mJson) +{ + LOGI("OnCall for paramJson"); + JSIValue result = JSI::CreateObject(); + JSI::SetStringProperty(result, "param", paramJson.c_str()); + + JSIValue param[1] = {result}; + OnEvent("dmFaCallback", DM_JSI_ARGS_ONE, param); + JSI::ReleaseValueList(result, ARGS_END); +} + +void DeviceManagerModule::OnAuthResult(const std::string &deviceId, const std::string &token, + int32_t status, int32_t reason) +{ + LOGI("OnAuthResult for status: %d, reason: %d", status, reason); + JSIValue thisVar = authAsyncCallbackInfo_.thisVal_; + JSIValue success = JSI::GetNamedProperty(authAsyncCallbackInfo_.callback, CB_SUCCESS); + JSIValue fail = JSI::GetNamedProperty(authAsyncCallbackInfo_.callback, CB_FAIL); + + JSIValue errOne = JSI::CreateObject(); + JSIValue errTwo = JSI::CreateObject(); + JSIValue successOne = JSI::CreateObject(); + JSIValue successTwo = JSI::CreateObject(); + + if (status == 0) { + LOGI("OnAuthResult success"); + JSI::SetStringProperty(successOne, "deviceId", deviceId.c_str()); + JSIValue param[1] = {successOne}; + AuthFuncParams* params = new AuthFuncParams(); + params->handlerRef = success; + params->thisVarRef_ = thisVar; + params->args = param; + params->argsSize = DM_JSI_ARGS_ONE; + LOGI("OnAuthResult SuccessCallBack in."); + JsAsyncWork::DispatchAsyncWork(AuthRsultVerifyInfoAsyncWorkFunc, reinterpret_cast(params)); + } else { + LOGI("OnAuthResult failed"); + JSI::SetNumberProperty(errOne, "code", (double)status); + JSI::SetNumberProperty(errTwo, "reason", (double)reason); + JSIValue param[2] = {errOne, errTwo}; + AuthFuncParams* params = new AuthFuncParams(); + params->handlerRef = fail; + params->thisVarRef_ = thisVar; + params->args = param; + params->argsSize = DM_JSI_ARGS_TWO; + LOGI("OnAuthResult FailCallBack in."); + JsAsyncWork::DispatchAsyncWork(AuthRsultVerifyInfoAsyncWorkFunc, reinterpret_cast(params)); + } + g_authCallbackMap.erase(bundleName_); + JSI::ReleaseValueList(thisVar, success, fail, errOne, errTwo, successOne, successTwo, + authAsyncCallbackInfo_.thisVal_, authAsyncCallbackInfo_.callback, ARGS_END); +} + +void DeviceManagerModule::OnVerifyResult(const std::string &deviceId, int32_t resultCode, const std::string flag) +{ + LOGI("OnVerifyResult for resultCode: %d, flag: %s", resultCode, flag.c_str()); + JSIValue thisVar = verifyAsyncCallbackInfo_.thisVal_; + JSIValue success = JSI::GetNamedProperty(verifyAsyncCallbackInfo_.callback, CB_SUCCESS); + JSIValue fail = JSI::GetNamedProperty(verifyAsyncCallbackInfo_.callback, CB_FAIL); + + JSIValue successOne = JSI::CreateObject(); + JSIValue successTwo = JSI::CreateObject(); + JSIValue errOne = JSI::CreateObject(); + + if (resultCode == 0) { + LOGI("OnVerifyResult success"); + JSI::SetStringProperty(successOne, "deviceId", deviceId.c_str()); + JSI::SetStringProperty(successTwo, "level", flag.c_str()); + JSIValue param[2] = {successOne, successTwo}; + AuthFuncParams* params = new AuthFuncParams(); + params->handlerRef = success; + params->thisVarRef_ = thisVar; + params->args = param; + params->argsSize = DM_JSI_ARGS_TWO; + LOGI("OnVerifyResult SuccessCallBack in."); + JsAsyncWork::DispatchAsyncWork(AuthRsultVerifyInfoAsyncWorkFunc, reinterpret_cast(params)); + } else { + LOGI("OnVerifyResult failed"); + JSI::SetNumberProperty(errOne, "code", (double)resultCode); + JSIValue param[1] = {errOne}; + AuthFuncParams* params = new AuthFuncParams(); + params->handlerRef = fail; + params->thisVarRef_ = thisVar; + params->args = param; + params->argsSize = DM_JSI_ARGS_ONE; + LOGI("OnVerifyResult FailCallBack in."); + JsAsyncWork::DispatchAsyncWork(AuthRsultVerifyInfoAsyncWorkFunc, reinterpret_cast(params)); + } + + g_checkAuthCallbackMap.erase(bundleName_); + JSI::ReleaseValueList(thisVar, success, fail, successOne, successTwo, errOne, + verifyAsyncCallbackInfo_.thisVal_, verifyAsyncCallbackInfo_.callback, ARGS_END); +} + +void DeviceManagerModule::DeviceInfoToJsArray(const std::vector &vecDevInfo, + const int32_t idx, JSIValue &arrayResult) +{ + bool status = false; + JSIValue result = JSI::CreateObject(); + char *deviceId = const_cast(vecDevInfo[idx].deviceId); + char *deviceName = const_cast(vecDevInfo[idx].deviceName); + + JSI::SetStringProperty(result, "deviceId", deviceId); + JSI::SetStringProperty(result, "deviceName", deviceName); + JSI::SetNumberProperty(result, "deviceTypeId", (double)vecDevInfo[idx].deviceTypeId); + + status = JSI::SetPropertyByIndex(arrayResult, idx, result); + if (status == false) { + LOGE("DmDeviceInfo To JsArray set element error"); + } + JSI::ReleaseValue(result); +} + +void DeviceManagerModule::DmAuthParamToJsAuthParamy(const DmAuthParam &authParam, JSIValue ¶mResult) +{ + LOGI("DmAuthParamToJsAuthParamy in"); + JSI::SetNumberProperty(paramResult, "authType", (double)authParam.authType); + + JSIValue extraInfo = JSI::CreateObject(); + JSI::SetNumberProperty(extraInfo, "direction", (double)authParam.direction); + JSI::SetNumberProperty(extraInfo, "pinToken", (double)authParam.pinToken); + if (authParam.direction == DM_AUTH_DIRECTION_CLIENT) { + JSI::SetNamedProperty(paramResult, "extraInfo", extraInfo); + return; + } + JSI::SetStringProperty(extraInfo, "packageName", authParam.packageName.c_str()); + JSI::SetStringProperty(extraInfo, "appName", authParam.appName.c_str()); + JSI::SetStringProperty(extraInfo, "appDescription", authParam.appDescription.c_str()); + JSI::SetNumberProperty(extraInfo, "business", (double)authParam.business); + JSI::SetNumberProperty(extraInfo, "pincode", (double)authParam.pincode); + JSI::SetNamedProperty(paramResult, "extraInfo", extraInfo); + LOGI("DeviceManagerModule::DmAuthParamToJsAuthParamy, packageName: %s", authParam.packageName.c_str()); + LOGI("DeviceManagerModule::DmAuthParamToJsAuthParamy, appName: %s", authParam.appName.c_str()); + LOGI("DeviceManagerModule::DmAuthParamToJsAuthParamy, appDescription: %s", authParam.appDescription.c_str()); + LOGI("DeviceManagerModule::DmAuthParamToJsAuthParamy, business: %d", authParam.business); + LOGI("DeviceManagerModule::DmAuthParamToJsAuthParamy, pincode: %d", authParam.pincode); + LOGI("DeviceManagerModule::DmAuthParamToJsAuthParamy, pinToken: %d", authParam.pinToken); + + size_t appIconLen = (size_t)authParam.imageinfo.GetAppIconLen(); + if (appIconLen > 0) { + uint8_t *appIcon = nullptr; + JSIValue appIconBuffer = JSI::CreateArrayBuffer(appIconLen, appIcon); + if (appIcon != nullptr && + memcpy_s(appIcon, appIconLen, reinterpret_cast(authParam.imageinfo.GetAppIcon()), + appIconLen) == 0) { + JSIValue appIconArray = JSI::CreateTypedArray(TypedArrayType::JSI_UINT8_ARRAY, + appIconLen, appIconBuffer, 0); + JSI::SetNamedProperty(paramResult, "appIcon", appIconArray); + } + } + + size_t appThumbnailLen = (size_t)authParam.imageinfo.GetAppThumbnailLen(); + if (appThumbnailLen > 0) { + uint8_t *appThumbnail = nullptr; + JSIValue appThumbnailBuffer = JSI::CreateArrayBuffer(appThumbnailLen, appThumbnail); + if (appThumbnail != nullptr && memcpy_s(appThumbnail, appThumbnailLen, + reinterpret_cast(authParam.imageinfo.GetAppThumbnail()), appThumbnailLen) == 0) { + JSIValue appThumbnailArray = JSI::CreateTypedArray(TypedArrayType::JSI_UINT8_ARRAY, + appThumbnailLen, + appThumbnailBuffer, 0); + JSI::SetNamedProperty(paramResult, "appThumbnail", appThumbnailArray); + } + } +} + +int32_t DeviceManagerModule::JsObjectToInt(const JSIValue &object, + const std::string &fieldStr) +{ + double result = JSI::GetNumberProperty(object, fieldStr.c_str()); + return (int32_t)result; +} + +bool DeviceManagerModule::JsObjectToBool(const JSIValue &object, + const std::string &fieldStr) +{ + bool result = JSI::GetBooleanProperty(object, fieldStr.c_str()); + return result; +} + +char *DeviceManagerModule::JsObjectToString(const JSIValue &object, + const std::string &fieldStr) +{ + char* str = JSI::GetStringProperty(object, fieldStr.c_str()); + return str; +} + +int32_t DeviceManagerModule::JsToDmSubscribeInfo(const JSIValue &object, DmSubscribeInfo &info) +{ + int32_t subscribeId = -1; + subscribeId = JsObjectToInt(object, "subscribeId"); + if (subscribeId < 0 || subscribeId > DM_JSI_SUB_ID_MAX) { + LOGE("DeviceManagerModule::JsToDmSubscribeInfo, subscribeId error, subscribeId: %d ", subscribeId); + return -1; + } + info.subscribeId = (uint16_t)subscribeId; + + int32_t mode = -1; + mode = JsObjectToInt(object, "mode"); + info.mode = (DmDiscoverMode)mode; + + int32_t medium = -1; + medium = JsObjectToInt(object, "medium"); + info.medium = (DmExchangeMedium)medium; + + int32_t freq = -1; + freq = JsObjectToInt(object, "freq"); + info.freq = (DmExchangeFreq)freq; + + info.isSameAccount = JsObjectToBool(object, "isSameAccount"); + info.isWakeRemote = JsObjectToBool(object, "isWakeRemote"); + + int32_t capability = -1; + capability = JsObjectToInt(object, "capability"); + if (capability == DM_JSI_SUBSCRIBE_CAPABILITY_DDMP || capability == DM_JSI_SUBSCRIBE_CAPABILITY_OSD) { + (void)strncpy_s(info.capability, sizeof(info.capability), DM_CAPABILITY_OSD, strlen(DM_CAPABILITY_OSD)); + } + return 0; +} + +void DeviceManagerModule::JsToDmDeviceInfo(const JSIValue &object, + DmDeviceInfo &info) +{ + int ret = strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, JsObjectToString(object, "deviceId")); + if (ret != DM_OK) { + LOGE("JsToDmDeviceInfo error: copy deviceId failed %d", ret); + return; + } + ret = strcpy_s(info.deviceName, DM_MAX_DEVICE_NAME_LEN, JsObjectToString(object, "deviceName")); + if (ret != DM_OK) { + LOGE("JsToDmDeviceInfo error: copy deviceName failed %d", ret); + return; + } + uint16_t deviceTypeId = -1; + deviceTypeId = (uint16_t)JsObjectToInt(object, "deviceTypeId"); + info.deviceTypeId = deviceTypeId; +} + +void DeviceManagerModule::JsToDmAppImageInfoAndDmExtra(const JSIValue &object, + DmAppImageInfo& appImageInfo, std::string &extra, int32_t &authType) +{ + LOGI("JsToDmAppImageInfoAndDmExtra in."); + int32_t authTypeTemp = -1; + authTypeTemp = (int32_t)JsObjectToInt(object, "authType"); + authType = authTypeTemp; + + uint8_t *appIconBufferPtr = nullptr; + int32_t appIconBufferLen = 0; + JsToDmBuffer(object, "appIcon", &appIconBufferPtr, appIconBufferLen); + + uint8_t *appThumbnailBufferPtr = nullptr; + int32_t appThumbnailBufferLen = 0; + JsToDmBuffer(object, "appThumbnail", &appThumbnailBufferPtr, appThumbnailBufferLen); + + appImageInfo.Reset(appIconBufferPtr, appIconBufferLen, appThumbnailBufferPtr, appThumbnailBufferLen); + if (appIconBufferPtr != nullptr) { + free(appIconBufferPtr); + appIconBufferPtr = nullptr; + } + if (appThumbnailBufferPtr != nullptr) { + free(appThumbnailBufferPtr); + appThumbnailBufferPtr = nullptr; + } + + nlohmann::json jsonObj; + jsonObj[AUTH_TYPE] = authType; + std::string extraInfo = "extraInfo"; + + JsToJsonObject(object, "extraInfo", jsonObj); + extra = jsonObj.dump(); + LOGI("appIconLen %d, appThumbnailLen %d", appIconBufferLen, appThumbnailBufferLen); +} + +void DeviceManagerModule::JsToDmBuffer(const JSIValue &object, + const std::string &fieldStr, uint8_t **bufferPtr, int32_t &bufferLen) +{ + LOGI("JsToDmBuffer in."); + JSIValue field = JSI::GetNamedProperty(object, fieldStr.c_str()); + if (field == JSI::CreateUndefined() || field == JSI::CreateNull()) { + LOGE("devicemanager JSI js to str no property: %s", fieldStr.c_str()); + return; + } + OHOS::ACELite::TypedArrayType type = TypedArrayType::JSI_UINT8_ARRAY; + size_t length = 0; + JSIValue buffer = nullptr; + size_t offset = 0; + uint8_t *data = nullptr; + data = JSI::GetTypedArrayInfo(field, type, length, buffer, offset); + if (type != TypedArrayType::JSI_UINT8_ARRAY || length == 0 || data == nullptr) { + LOGE("Invaild AppIconInfo"); + return; + } + *bufferPtr = (uint8_t*)calloc(sizeof(uint8_t), length); + if (*bufferPtr == nullptr) { + LOGE("low memory, calloc return nullptr, length is %d, filed %s", length, fieldStr.c_str()); + return; + } + if (memcpy_s(*bufferPtr, length, data, length) != 0) { + LOGE("memcpy_s failed, filed %s", fieldStr.c_str()); + free(*bufferPtr); + *bufferPtr = nullptr; + return; + } + bufferLen = length; +} + +void DeviceManagerModule::JsToJsonObject(const JSIValue &object, + const std::string &fieldStr, nlohmann::json &jsonObj) +{ + LOGI("JsToJsonObject in."); + JSIValue jsonField = JSI::GetNamedProperty(object, fieldStr.c_str()); + if (jsonField == JSI::CreateUndefined() || jsonField == JSI::CreateNull()) { + LOGE("devicemanager JSI js to str no property: %s", fieldStr.c_str()); + return; + } + + JSIValue jsProNameList = nullptr; + uint32_t jsProCount = 0; + jsProNameList = JSI::GetObjectKeys(jsonField); + jsProCount = JSI::GetArrayLength(jsProNameList); + LOGI("Property size=%d.", jsProCount); + + JSIValue jsProName = nullptr; + JSIValue jsProValue = nullptr; + for (uint32_t index = 0; index < jsProCount; index++) { + jsProName = JSI::GetPropertyByIndex(jsProNameList, index); + std::string strProName = JSI::ValueToString(jsProName); + jsProValue = JSI::GetNamedProperty(jsonField, strProName.c_str()); + if (JSI::ValueIsString(jsProValue)) { + std::string natValue = JSI::ValueToString(jsProValue); + LOGI("Property name=%s, string, value=%s", strProName.c_str(), natValue.c_str()); + jsonObj[strProName] = natValue; + } + if (JSI::ValueIsBoolean(jsProValue)) { + bool elementValue = JSI::ValueToBoolean(jsProValue); + LOGI("Property name=%s, boolean, value=%d.", strProName.c_str(), elementValue); + jsonObj[strProName] = elementValue; + } + if (JSI::ValueIsNumber(jsProValue)) { + int32_t elementValue = 0; + elementValue = (int32_t)JSI::ValueToNumber(jsProValue); + jsonObj[strProName] = elementValue; + LOGI("Property name=%s, number, value=%d.", strProName.c_str(), elementValue); + } + } +} + +void DeviceManagerModule::JsToDmAuthInfo(const JSIValue &object, std::string &extra) +{ + LOGI("%s called.", __func__); + int32_t authType = -1; + int32_t token = -1; + + authType = JsObjectToInt(object, "authType"); + token = JsObjectToInt(object, "token"); + + nlohmann::json jsonObj; + jsonObj[AUTH_TYPE] = authType; + if (authType == DM_AUTH_TYPE_PINCODE) { + jsonObj[PIN_TOKEN] = token; + } else { + jsonObj[TOKEN] = token; + } + JsToJsonObject(object, "extraInfo", jsonObj); + extra = jsonObj.dump(); +} + +void DeviceManagerModule::CreateDmCallback(std::string &bundleName, std::string &eventType) +{ + LOGE("CreateDmCallback for bunderName %s eventType %s", bundleName.c_str(), eventType.c_str()); + if (eventType == DM_JSI_EVENT_DEVICE_STATE_CHANGE) { + auto iter = g_deviceStateCallbackMap.find(bundleName); + if (iter == g_deviceStateCallbackMap.end()) { + auto callback = std::make_shared(bundleName); + std::string extra = ""; + int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().RegisterDevStateCallback( + bundleName, extra, callback); + if (ret != 0) { + LOGE("RegisterDevStateCallback failed for bunderName %s", bundleName.c_str()); + return; + } + g_deviceStateCallbackMap[bundleName] = callback; + } + return; + } + + if (eventType == DM_JSI_EVENT_DEVICE_FOUND || eventType == DM_JSI_EVENT_DEVICE_DISCOVER_FAIL) { + std::shared_ptr discoverCallback = nullptr; + auto iter = g_discoverCallbackMap.find(bundleName); + if (iter == g_discoverCallbackMap.end()) { + auto callback = std::make_shared(bundleName); + g_discoverCallbackMap[bundleName] = callback; + discoverCallback = callback; + } else { + discoverCallback = iter->second; + } + + discoverCallback->IncreaseRefCount(); + return; + } + + if (eventType == DM_JSI_EVENT_DMFA_CALLBACK) { + auto iter = g_dmfaCallbackMap.find(bundleName); + if (iter == g_dmfaCallbackMap.end()) { + auto callback = std::make_shared(bundleName); + int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().RegisterDeviceManagerFaCallback( + bundleName, callback); + if (ret != 0) { + LOGE("RegisterDeviceManagerFaCallback failed for bunderName %s", bundleName.c_str()); + return; + } + g_dmfaCallbackMap[bundleName] = callback; + } + return; + } +} + +void DeviceManagerModule::ReleaseDmCallback(std::string &bundleName, std::string &eventType) +{ + if (eventType == DM_JSI_EVENT_DEVICE_STATE_CHANGE) { + auto iter = g_deviceStateCallbackMap.find(bundleName); + if (iter == g_deviceStateCallbackMap.end()) { + LOGE("ReleaseDmCallback: cannot find stateCallback for bunderName %s", bundleName.c_str()); + return; + } + int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().UnRegisterDevStateCallback(bundleName); + if (ret != 0) { + LOGE("RegisterDevStateCallback failed for bunderName %s", bundleName.c_str()); + return; + } + g_deviceStateCallbackMap.erase(bundleName); + return; + } + + if (eventType == DM_JSI_EVENT_DEVICE_FOUND || eventType == DM_JSI_EVENT_DEVICE_DISCOVER_FAIL) { + std::shared_ptr discoverCallback = nullptr; + auto iter = g_discoverCallbackMap.find(bundleName); + if (iter == g_discoverCallbackMap.end()) { + return; + } + + discoverCallback = iter->second; + discoverCallback->DecreaseRefCount(); + if (discoverCallback->GetRefCount() == 0) { + g_discoverCallbackMap.erase(bundleName); + } + return; + } + + if (eventType == DM_JSI_EVENT_DMFA_CALLBACK) { + auto iter = g_dmfaCallbackMap.find(bundleName); + if (iter == g_dmfaCallbackMap.end()) { + LOGE("cannot find dmFaCallback for bunderName %s", bundleName.c_str()); + return; + } + int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().UnRegisterDeviceManagerFaCallback( + bundleName); + if (ret != 0) { + LOGE("RegisterDevStateCallback failed for bunderName %s", bundleName.c_str()); + return; + } + g_dmfaCallbackMap.erase(bundleName); + return; + } +} + +void DeviceManagerModule::AuthRsultVerifyInfoAsyncWorkFunc(void *data) +{ + LOGI("AuthRsultVerifyInfoAsyncWorkFunc in ............"); + AuthFuncParams* params = reinterpret_cast(data); + JSI::CallFunction(params->handlerRef, params->thisVarRef_, params->args, params->argsSize); +} + +JSIValue DeviceManagerModule::UnAuthenticateDevice(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize) +{ + LOGI("UnAuthenticateDevice in"); + if (argsSize < 1) { + LOGE("1 argument is required."); + return JSI::CreateNull(); + } + if (!JSI::ValueIsObject(args[0])) { + LOGE("a object is required."); + return JSI::CreateNull(); + } + std::string bundleName = GetJSIAppBundleName(); + std::string deviceId = JSI::GetStringProperty(args[0], "deviceId"); + LOGI("UnAuthenticateDevice deviceId=%s", deviceId.c_str()); + int32_t ret = 0; + ret = OHOS::DistributedHardware::DeviceManager::GetInstance().UnAuthenticateDevice(bundleName, deviceId); + if (ret != 0) { + LOGI("UnAuthenticateDevice for bunderName %s failed, ret %d", bundleName.c_str(), ret); + } + + JSIValue result = JSI::CreateObject(); + JSI::SetNumberProperty(result, "ret", (double)ret); + return result; +} + +JSIValue DeviceManagerModule::GetLocalDeviceInfoSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize) +{ + LOGI("GetLocalDeviceInfoSync in"); + + std::string bundleName = GetJSIAppBundleName(); + DmDeviceInfo deviceInfo; + int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().GetLocalDeviceInfo(bundleName, deviceInfo); + if (ret != 0) { + LOGE("GetLocalDeviceInfoSync for failed, ret %d", ret); + return JSI::CreateNull(); + } + LOGI("DeviceManager::GetLocalDeviceInfoSync deviceId:%s deviceName:%s deviceTypeId:%d ", deviceInfo.deviceId, + deviceInfo.deviceName, deviceInfo.deviceTypeId); + JSIValue result = JSI::CreateObject(); + char *deviceId = const_cast(deviceInfo.deviceId); + char *deviceName = const_cast(deviceInfo.deviceName); + + JSI::SetStringProperty(result, "deviceId", deviceId); + JSI::SetStringProperty(result, "deviceName", deviceName); + JSI::SetNumberProperty(result, "deviceTypeId", (double)deviceInfo.deviceTypeId); + return result; +} + +JSIValue DeviceManagerModule::SetUserOperationSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize) +{ + LOGI("SetUserOperationSync in"); + if (argsSize < 1) { + LOGE("1 argument is required."); + return JSI::CreateNull(); + } + + if (!JSI::ValueIsNumber(args[0])) { + LOGE("a Number is required."); + return JSI::CreateNull(); + } + + std::string bundleName = GetJSIAppBundleName(); + int32_t action = 0; + action = static_cast(JSI::ValueToNumber(args[0])); + + LOGI("SetUserOperation action %d", action); + + int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().SetUserOperation(bundleName, action); + if (ret != 0) { + LOGE("SetUserOperation for bunderName %s failed, ret %d", + bundleName.c_str(), ret); + return JSI::CreateNull(); + } + return JSI::CreateNull(); +} + +JSIValue DeviceManagerModule::GetAuthenticationParamSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize) +{ + LOGI("GetAuthenticationParamSync in"); + std::string bundleName = GetJSIAppBundleName(); + JSIValue resultParam = JSI::CreateObject(); + DmAuthParam authParam; + int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().GetFaParam(bundleName, authParam); + if (ret != 0) { + LOGE("GetAuthenticationParam for %s failed, ret %d", + bundleName.c_str(), ret); + return JSI::CreateNull(); + } + + DmAuthParamToJsAuthParamy(authParam, resultParam); + return resultParam; +} + +JSIValue DeviceManagerModule::GetTrustedDeviceListSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize) +{ + LOGI("GetTrustedDeviceList in"); + JSIValue array = JSI::CreateNull(); + std::string extra = ""; + std::vector devList; + std::string bundleName = GetJSIAppBundleName(); + + int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList( + bundleName, extra, devList); + if (ret != 0) { + LOGE("GetTrustedDeviceList for bunderName %s failed, ret %d", + bundleName.c_str(), ret); + return array; + } + if (devList.size() > 0) { + bool isArray = false; + array = JSI::CreateArray(devList.size()); + isArray = JSI::ValueIsArray(array); + if (isArray == false) { + LOGE("JSI_create_array fail"); + } + + for (size_t i = 0; i != devList.size(); ++i) { + DeviceInfoToJsArray(devList, i, array); + } + } else { + LOGE("devList is null"); + } + + return array; +} + +JSIValue DeviceManagerModule::StartDeviceDiscoverSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize) +{ + LOGI("StartDeviceDiscoverSync in"); + std::string bundleName = GetJSIAppBundleName(); + + if (argsSize < 1) { + LOGE("1 argument is required."); + return JSI::CreateNull(); + } + + if (!JSI::ValueIsObject(args[0])) { + LOGE("a object is required."); + return JSI::CreateNull(); + } + + std::shared_ptr discoverCallback = nullptr; + auto iter = g_discoverCallbackMap.find(bundleName); + if (iter == g_discoverCallbackMap.end()) { + discoverCallback = std::make_shared(bundleName); + g_discoverCallbackMap[bundleName] = discoverCallback; + } else { + discoverCallback = iter->second; + } + DmSubscribeInfo subInfo; + int32_t res = JsToDmSubscribeInfo(args[0], subInfo); + if (res != 0) { + LOGE("Wrong subscribeId."); + return JSI::CreateNull(); + } + + LOGI("subInfo %d , %d, %d, %d, %d , %d, %s", + subInfo.subscribeId, + subInfo.mode, + subInfo.medium, subInfo.freq, subInfo.isSameAccount, + subInfo.isWakeRemote, subInfo.capability); + std::string extra = ""; + int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().StartDeviceDiscovery(bundleName, + subInfo, extra, discoverCallback); + if (ret != 0) { + LOGE("StartDeviceDiscovery for bunderName %s failed, ret %d", + bundleName.c_str(), ret); + return JSI::CreateNull(); + } + return JSI::CreateNull(); +} + +JSIValue DeviceManagerModule::StopDeviceDiscoverSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize) +{ + LOGI("StopDeviceDiscoverSync in"); + std::string bundleName = GetJSIAppBundleName(); + + if (argsSize < 1) { + LOGE("1 argument is required."); + return JSI::CreateNull(); + } + + if (!JSI::ValueIsNumber(args[0])) { + LOGE("a Number is required."); + return JSI::CreateNull(); + } + + int16_t subscribeId = 0; + subscribeId = static_cast(JSI::ValueToNumber(args[0])); + LOGI("subscribeId %d", subscribeId); + int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().StopDeviceDiscovery(bundleName, subscribeId); + if (ret != 0) { + LOGE("StopDeviceDiscovery for bunderName %s failed, ret %d", + bundleName.c_str(), ret); + return JSI::CreateNull(); + } + return JSI::CreateNull(); +} + +JSIValue DeviceManagerModule::AuthenticateDevice(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize) +{ + LOGI("AuthenticateDevice in"); + std::string bundleName = GetJSIAppBundleName(); + if (argsSize < DM_JSI_ARGS_THREE) { + LOGE("3 argument is required."); + return JSI::CreateNull(); + } + + if (!JSI::ValueIsObject(args[0])) { + LOGE("a object is required."); + return JSI::CreateNull(); + } + + if (!JSI::ValueIsObject(args[1])) { + LOGE("a object is required."); + return JSI::CreateNull(); + } + + authAsyncCallbackInfo_.thisVal_ = JSI::AcquireValue(thisVal); + authAsyncCallbackInfo_.callback = JSI::AcquireValue(args[DM_JSI_ARGS_TWO]); + + std::shared_ptr authCallback = nullptr; + auto iter = g_authCallbackMap.find(bundleName); + if (iter == g_authCallbackMap.end()) { + authCallback = std::make_shared(bundleName); + g_authCallbackMap[bundleName] = authCallback; + } else { + authCallback = iter->second; + } + DmDeviceInfo deviceInfo; + JsToDmDeviceInfo(args[0], deviceInfo); + + LOGI("deviceInfo %s , %s, %d", deviceInfo.deviceId, + deviceInfo.deviceName, deviceInfo.deviceTypeId); + DmAppImageInfo appImageInfo(nullptr, 0, nullptr, 0); + std::string extra; + JsToDmAppImageInfoAndDmExtra(args[1], appImageInfo, extra, authAsyncCallbackInfo_.authType); + + int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().AuthenticateDevice( + bundleName, 1, deviceInfo, extra, authCallback); + if (ret != 0) { + LOGE("AuthenticateDevice for bunderName %s failed, ret %d", + bundleName.c_str(), ret); + } + + return JSI::CreateUndefined(); +} + +JSIValue DeviceManagerModule::VerifyAuthInfo(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize) +{ + LOGI("VerifyAuthInfo in"); + std::string bundleName = GetJSIAppBundleName(); + if (argsSize < DM_JSI_ARGS_TWO) { + LOGE("2 argument is required."); + return JSI::CreateNull(); + } + + if (!JSI::ValueIsObject(args[0])) { + LOGE("a object is required."); + return JSI::CreateNull(); + } + + verifyAsyncCallbackInfo_.thisVal_ = JSI::AcquireValue(thisVal); + verifyAsyncCallbackInfo_.callback = JSI::AcquireValue(args[1]); + + std::shared_ptr verifyCallback = nullptr; + auto iter = g_checkAuthCallbackMap.find(bundleName); + if (iter == g_checkAuthCallbackMap.end()) { + verifyCallback = std::make_shared(bundleName); + g_checkAuthCallbackMap[bundleName] = verifyCallback; + } else { + verifyCallback = iter->second; + } + std::string authParam; + JsToDmAuthInfo(args[0], authParam); + int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().VerifyAuthentication(bundleName, + authParam, verifyCallback); + if (ret != 0) { + LOGE("VerifyAuthInfo for bunderName %s failed, ret %d", + bundleName.c_str(), ret); + } + return JSI::CreateUndefined(); +} + +JSIValue DeviceManagerModule::JsOn(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize) +{ + LOGI("JsOn in"); + std::string bundleName = GetJSIAppBundleName(); + if (argsSize < DM_JSI_ARGS_TWO) { + LOGE("2 argument is required."); + return JSI::CreateNull(); + } + if (!JSI::ValueIsString(args[0])) { + LOGE("a string is required."); + return JSI::CreateNull(); + } + if (!JSI::ValueIsFunction(args[1])) { + LOGE("a FUNC is required."); + return JSI::CreateNull(); + } + std::string eventType = JSI::ValueToString(args[0]); + + LOGI("JsOn for bunderName %s, eventType %s ", bundleName.c_str(), + eventType.c_str()); + std::shared_ptr DmNativeEventobj = std::make_shared(thisVal); + DmNativeEventobj->On(eventType, args[1], thisVal); + CreateDmCallback(bundleName, eventType); + + return JSI::CreateUndefined(); +} + +JSIValue DeviceManagerModule::JsOff(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize) +{ + LOGI("JsOff in"); + std::string bundleName = GetJSIAppBundleName(); + + if (!JSI::ValueIsString(args[0])) { + LOGE("a string is required."); + return JSI::CreateNull(); + } + + std::string eventType = JSI::ValueToString(args[0]); + + LOGI("JsOff for bunderName %s, eventType %s ", bundleName.c_str(), + eventType.c_str()); + + DmNativeEvent* DmNativeEventobj = new DmNativeEvent(); + DmNativeEventobj->Off(eventType); + delete(DmNativeEventobj); + ReleaseDmCallback(bundleName, eventType); + + return JSI::CreateUndefined(); +} + +JSIValue DeviceManagerModule::ReleaseDeviceManager(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize) +{ + LOGI("ReleaseDeviceManager in"); + std::string bundleName = GetJSIAppBundleName(); + LOGI("ReleaseDeviceManager for bunderName %s", bundleName.c_str()); + + int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().UnInitDeviceManager(bundleName); + if (ret != 0) { + LOGE("ReleaseDeviceManager for bunderName %s failed, ret %d", + bundleName.c_str(), ret); + JSIValue result = JSI::CreateNumber((double)ret); + return result; + } + + g_deviceManagerMap.erase(bundleName); + g_initCallbackMap.erase(bundleName); + g_deviceStateCallbackMap.erase(bundleName); + g_discoverCallbackMap.erase(bundleName); + g_authCallbackMap.erase(bundleName); + g_checkAuthCallbackMap.erase(bundleName); + + return JSI::CreateUndefined(); +} + +JSIValue DeviceManagerModule::CreateDeviceManager(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize) +{ + if (argsSize < 1) { + LOGE("1 argument is required."); + return JSI::CreateNull(); + } + + if (!JSI::ValueIsString(args[0])) { + LOGE("a string is required."); + return JSI::CreateNull(); + } + std::string bundleName = GetJSIAppBundleName(); + LOGI("CreateDeviceManager for bunderName is %s", bundleName.c_str()); + + if (DeviceManagerModule::GetDeviceManagerJSI(bundleName) != nullptr) { + LOGI("CreateDeviceManager repeat for bunderName %s", bundleName.c_str()); + return JSI::CreateNull(); + } + + DeviceManagerModule *obj = new DeviceManagerModule(); + obj->bundleName_ = bundleName; + g_deviceManagerMap[bundleName] = obj; + + int32_t ret = 0; + std::shared_ptr initCallback = std::make_shared(bundleName); + ret = OHOS::DistributedHardware::DeviceManager::GetInstance().InitDeviceManager(bundleName, initCallback); + if (ret == 0) { + LOGI("InitDeviceManager for bunderName %s success", bundleName.c_str()); + JSIValue success = JSI::GetNamedProperty(args[1], CB_SUCCESS); + JSIValue data = JSI::CreateObject(); + std::string str = "InitDeviceManager success"; + JSI::SetStringProperty(data, "data", str.c_str()); + JSI::CallFunction(success, thisVal, &data, 1); + } + if (ret != 0) { + LOGI("InitDeviceManager for bunderName %s fail", bundleName.c_str()); + JSIValue fail = JSI::GetNamedProperty(args[1], CB_FAIL); + JSIValue err = JSI::CreateObject(); + std::string str = "InitDeviceManager fail"; + JSI::SetStringProperty(err, "err", str.c_str()); + JSI::CallFunction(fail, thisVal, &err, 1); + } + return JSI::CreateNull(); +} + +char *DeviceManagerModule::GetJSIAppBundleName() +{ + JSAbility *g_targetJSAbility = new JSAbility(); + const char *pname = g_targetJSAbility->GetPackageName(); + char *packageName = new char[strlen(pname)+1]; + int ret = strcpy_s(packageName, strlen(pname)+1, pname); + if (ret != DM_OK) { + LOGE("GetJSIAppBundleName error: copy BundleName failed %d", ret); + delete(g_targetJSAbility); + return nullptr; + } + delete(g_targetJSAbility); + return packageName; +} + +void InitDeviceManagerModule(JSIValue exports) +{ + JSI::SetModuleAPI(exports, "createDeviceManager", DeviceManagerModule::CreateDeviceManager); + JSI::SetModuleAPI(exports, "getTrustedDeviceListSync", DeviceManagerModule::GetTrustedDeviceListSync); + JSI::SetModuleAPI(exports, "release", DeviceManagerModule::ReleaseDeviceManager); + JSI::SetModuleAPI(exports, "startDeviceDiscovery", DeviceManagerModule::StartDeviceDiscoverSync); + JSI::SetModuleAPI(exports, "stopDeviceDiscovery", DeviceManagerModule::StopDeviceDiscoverSync); + JSI::SetModuleAPI(exports, "authenticateDevice", DeviceManagerModule::AuthenticateDevice); + JSI::SetModuleAPI(exports, "verifyAuthInfo", DeviceManagerModule::VerifyAuthInfo); + JSI::SetModuleAPI(exports, "setUserOperation", DeviceManagerModule::SetUserOperationSync); + JSI::SetModuleAPI(exports, "getAuthenticationParam", DeviceManagerModule::GetAuthenticationParamSync); + JSI::SetModuleAPI(exports, "on", DeviceManagerModule::JsOn); + JSI::SetModuleAPI(exports, "off", DeviceManagerModule::JsOff); + JSI::SetModuleAPI(exports, "getLocalDeviceInfoSync", DeviceManagerModule::GetLocalDeviceInfoSync); + JSI::SetModuleAPI(exports, "unAuthenticateDevice", DeviceManagerModule::UnAuthenticateDevice); +} +} +} diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 97f5c361e461e48393454d1ae35bfcdfed7537d9..65d19605dc15ac1b28702de4d23aaad6d87adbb1 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -20,7 +20,51 @@ if (defined(ohos_lite)) { import("//foundation/distributedhardware/devicemanager/devicemanager.gni") if (defined(ohos_lite)) { - shared_library("devicemanagerutils") { + if (ohos_kernel_type == "liteos_m") { + static_library("devicemanagerutils_mini") { + include_dirs = [ + "${utils_path}/include", + "${utils_path}/include/permission/lite", + "${common_path}/include", + ] + + include_dirs += [ + "//base/security/deviceauth/interfaces/innerkits", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara/include", + "//utils/native/lite/include", + "//utils/system/safwk/native/include", + "//third_party/json/include", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits", + "//third_party/bounds_checking_function/include", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", + ] + + sources = [ + "${utils_path}/src/dm_anonymous.cpp", + "${utils_path}/src/dm_log.cpp", + "${utils_path}/src/dm_random.cpp", + "${utils_path}/src/permission/lite/permission_manager.cpp", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"devicemanagerutils\"", + "LOG_DOMAIN=0xD004100", + ] + + deps = [ + "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_static", + "//base/startup/syspara_lite/frameworks/parameter/src:sysparam", + "//foundation/distributedschedule/samgr_lite/samgr", + "//third_party/bounds_checking_function:libsec_static", + "//third_party/mbedtls", + "//third_party/mbedtls:mbedtls_shared", + "//utils/native/lite:utils", + ] + } + } else { + shared_library("devicemanagerutils") { + } } } else { config("devicemanagerutils_config") {