From e182d37f07b2cbf38e4592e9fd9936695f759f6a Mon Sep 17 00:00:00 2001 From: "weidong.liu@thundersoft.com" Date: Mon, 21 Feb 2022 11:53:15 +0800 Subject: [PATCH] test for ds --- .vscode/settings.json | 60 ++++ bundle.json | 3 +- common/include/ipc/ipc_def.h | 3 + .../model/ipc_notify_trust_list_result_req.h | 44 +++ .../ipc/model/ipc_register_decision_req.h | 44 +++ ext/decision/BUILD.gn | 75 +++++ .../include/device_decision_adapter.h | 51 ++++ ext/decision/src/device_decision_adapter.cpp | 187 ++++++++++++ ext/profile/src/device_profile_adapter.cpp | 2 +- .../native_cpp/include/device_manager.h | 5 +- .../include/device_manager_callback.h | 8 + .../native_cpp/include/device_manager_impl.h | 5 +- .../native_cpp/include/dm_decision_info.h | 42 +++ .../include/notify/device_manager_notify.h | 5 + .../native_cpp/src/device_manager_impl.cpp | 61 +++- .../src/ipc/standard/ipc_cmd_parser.cpp | 76 +++++ .../src/notify/device_manager_notify.cpp | 31 ++ .../kits/js/include/native_devicemanager_js.h | 39 ++- .../kits/js/src/native_devicemanager_js.cpp | 267 +++++++++++++----- .../include/adapter/decision_adapter.h | 16 ++ .../include/config/json_config.h | 10 +- .../include/device_manager_service.h | 2 + .../include/device_manager_service_listener.h | 1 + .../deviceinfo/dm_device_info_manager.h | 12 +- .../devicestate/dm_device_state_manager.h | 9 + .../src/device_manager_service.cpp | 14 +- .../src/device_manager_service_listener.cpp | 20 +- .../src/deviceinfo/dm_device_info_manager.cpp | 18 +- .../devicestate/dm_device_state_manager.cpp | 135 ++++++++- .../src/ipc/standard/ipc_cmd_parser.cpp | 62 ++++ test/unittest/BUILD.gn | 12 + .../UTTest_dm_device_info_manager.cpp | 5 +- test/unittest/UTTest_dm_ext_decision.cpp | 54 ++++ test/unittest/UTTest_dm_ext_decision.h | 35 +++ 34 files changed, 1316 insertions(+), 97 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 common/include/ipc/model/ipc_notify_trust_list_result_req.h create mode 100644 common/include/ipc/model/ipc_register_decision_req.h create mode 100644 ext/decision/BUILD.gn create mode 100644 ext/decision/include/device_decision_adapter.h create mode 100644 ext/decision/src/device_decision_adapter.cpp create mode 100644 interfaces/inner_kits/native_cpp/include/dm_decision_info.h create mode 100644 test/unittest/UTTest_dm_ext_decision.cpp create mode 100644 test/unittest/UTTest_dm_ext_decision.h diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..2e777c53b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,60 @@ +{ + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "condition_variable": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "list": "cpp", + "map": "cpp", + "set": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/bundle.json b/bundle.json index 06952ef47..c736f2842 100644 --- a/bundle.json +++ b/bundle.json @@ -47,7 +47,8 @@ "//foundation/distributedhardware/devicemanager/interfaces/kits:devicemanager_native_js", "//foundation/distributedhardware/devicemanager/services/devicemanagerservice:devicemanagerservice", "//foundation/distributedhardware/devicemanager/sa_profile:dm_sa_profile", - "//foundation/distributedhardware/devicemanager/ext:ext_modules" + "//foundation/distributedhardware/devicemanager/ext:ext_modules", + "//foundation/distributedhardware/devicemanager/ext/decision:devicemanagerext_decision" ], "inner_kits": [ { diff --git a/common/include/ipc/ipc_def.h b/common/include/ipc/ipc_def.h index ec3a93d3e..399b95024 100644 --- a/common/include/ipc/ipc_def.h +++ b/common/include/ipc/ipc_def.h @@ -51,9 +51,12 @@ enum IpcCmdID { SERVER_DISCOVER_FINISH, SERVER_AUTH_RESULT, SERVER_VERIFY_AUTH_RESULT, + SERVER_TRUST_LIST_RESULT, SERVER_GET_DMFA_INFO, SERVER_USER_AUTH_OPERATION, SERVER_DEVICE_FA_NOTIFY, + REGISTER_DECISION_FILTER, + UNREGISTER_DECISION_FILTER, }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/include/ipc/model/ipc_notify_trust_list_result_req.h b/common/include/ipc/model/ipc_notify_trust_list_result_req.h new file mode 100644 index 000000000..4338a04f7 --- /dev/null +++ b/common/include/ipc/model/ipc_notify_trust_list_result_req.h @@ -0,0 +1,44 @@ +/* + * 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_DM_IPC_NOTIFY_TRUST_LIST_RESULT_REQ_H +#define OHOS_DM_IPC_NOTIFY_TRUST_LIST_RESULT_REQ_H + +#include +#include "dm_device_info.h" +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcNotifyTrustListResultReq : public IpcReq { + DECLARE_IPC_MODEL(IpcNotifyTrustListResultReq); + +public: + const std::vector GetDeviceVec() const + { + return deviceVec_; + } + + void SetDeviceVec(const std::vector &deviceVec) + { + deviceVec_ = deviceVec; + } + +private: + std::vector deviceVec_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_NOTIFY_TRUST_LIST_RESULT_REQ_H diff --git a/common/include/ipc/model/ipc_register_decision_req.h b/common/include/ipc/model/ipc_register_decision_req.h new file mode 100644 index 000000000..8aed79ee9 --- /dev/null +++ b/common/include/ipc/model/ipc_register_decision_req.h @@ -0,0 +1,44 @@ +/* + * 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_DM_IPC_REGISTER_DECISION_REQ_H +#define OHOS_DM_IPC_REGISTER_DECISION_REQ_H + +#include + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcRegisterDecisionReq : public IpcReq { + DECLARE_IPC_MODEL(IpcRegisterDecisionReq); + +public: + const std::string &GetExtra() const + { + return extra_; + } + + void SetExtra(const std::string &extra) + { + extra_ = extra; + } + +private: + std::string extra_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_REGISTER_DECISION_REQ_H diff --git a/ext/decision/BUILD.gn b/ext/decision/BUILD.gn new file mode 100644 index 000000000..4de55539b --- /dev/null +++ b/ext/decision/BUILD.gn @@ -0,0 +1,75 @@ +# 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") + +if (defined(ohos_lite)) { +} else { + ohos_shared_library("devicemanagerext_decision") { + include_dirs = [ + "include", + "${common_path}/include", + "${common_path}/include/ipc", + "${common_path}/include/ipc/model", + "${ext_path}/decision/include", + "${services_path}/include", + "${services_path}/include/adapter", + "${services_path}/include/dependency/softbus", + "${services_path}/include/dependency/timer", + "${services_path}/include/ipc/standard", + "${services_path}/include/devicestate", + "${services_path}/include/deviceinfo", + "${utils_path}/include", + "${utils_path}/include/ipc/standard", + "${innerkits_path}/native_cpp/include", + "${innerkits_path}/native_cpp/include/ipc", + "${innerkits_path}/native_cpp/include/ipc/standard", + "//foundation/communication/dsoftbus/interfaces/kits/bus_center", + "//foundation/communication/dsoftbus/interfaces/kits/common", + "//foundation/communication/dsoftbus/interfaces/kits/discovery", + "//foundation/communication/dsoftbus/interfaces/kits/transport", + "//foundation/communication/dsoftbus/interfaces/inner_kits/transport", + ] + + sources = [ "src/device_decision_adapter.cpp" ] + + deps = [ + "${services_path}:devicemanagerservice", + "${utils_path}:devicemanagerutils", + "//foundation/aafwk/standard/interfaces/innerkits/want:want", + "//utils/native/base:utils", + ] + + external_deps = [ + #"device_profile_core:distributed_device_profile_client", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"devicemanagerext\"", + "LOG_DOMAIN=0xD004100", + ] + + subsystem_name = "distributedhardware" + + part_name = "device_manager_base" + } +} diff --git a/ext/decision/include/device_decision_adapter.h b/ext/decision/include/device_decision_adapter.h new file mode 100644 index 000000000..a3a258ef4 --- /dev/null +++ b/ext/decision/include/device_decision_adapter.h @@ -0,0 +1,51 @@ +/* + * 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_DM_ADAPTER_DEVICE_DECISION_H +#define OHOS_DM_ADAPTER_DEVICE_DECISION_H + +#include +#include +#include + +#include "decision_adapter.h" +#include "dm_decision_info.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +class DeviceDecisionAdapter : public IDecisionAdapter { +public: + DeviceDecisionAdapter(); + ~DeviceDecisionAdapter(); + std::string GetName(); + std::string GetVersion(); + int32_t FilterDeviceList(std::vector &infoList, const std::string &filterOptions); + int32_t SortDeviceList(std::vector &infoList, const std::string &sortOptions); + void SetDecisionId(const std::string &pkgName, int32_t decisionId); + int32_t RegisterDecisionListener(const std::string &pkgName, std::shared_ptr callback); + int32_t RegisterDecisionListener(const std::string &pkgName, std::shared_ptr callback); + int32_t UnRegisterDecisionListener(const std::string &pkgName); + void OnResult(const char *result); + +private: + std::map> stateListenerCallbackMap_; + std::map> infoListenerCallbackMap_; + std::map waitCallbackIdMap_; + std::vector infoListTemp_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_ADAPTER_DEVICE_DECISION_H diff --git a/ext/decision/src/device_decision_adapter.cpp b/ext/decision/src/device_decision_adapter.cpp new file mode 100644 index 000000000..f870b864c --- /dev/null +++ b/ext/decision/src/device_decision_adapter.cpp @@ -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. + */ + +#include "device_decision_adapter.h" +#include "dm_constants.h" +#include "dm_device_state_manager.h" +#include "dm_device_info_manager.h" +#include "dm_log.h" +#include "nlohmann/json.hpp" +#include "dm_timer.h" + +const static int32_t DECISION_TEST_CALLBACK_TIME = 5; +namespace OHOS { +namespace DistributedHardware { +std::shared_ptr StartTimer = std::make_shared("decisionTimeoutTask"); + +DeviceDecisionAdapter::DeviceDecisionAdapter() +{ +} + +DeviceDecisionAdapter::~DeviceDecisionAdapter() +{ +} + +std::string DeviceDecisionAdapter::GetName() +{ + std::string strName; + return strName; +} + +std::string DeviceDecisionAdapter::GetVersion() +{ + std::string strVersion; + return strVersion; +} + +int32_t DeviceDecisionAdapter::RegisterDecisionListener(const std::string &pkgName, + std::shared_ptr callback) +{ + LOGI("DeviceDecisionAdapter::RegisterDecisionListener, pkgName=%s", pkgName.c_str()); + stateListenerCallbackMap_[pkgName] = callback; + return DM_OK; +} + +int32_t DeviceDecisionAdapter::RegisterDecisionListener(const std::string &pkgName, + std::shared_ptr callback) +{ + LOGI("DeviceDecisionAdapter::RegisterDecisionListener, pkgName=%s", pkgName.c_str()); + infoListenerCallbackMap_[pkgName] = callback; + return DM_OK; +} + +int32_t DeviceDecisionAdapter::UnRegisterDecisionListener(const std::string &pkgName) +{ + LOGI("DeviceDecisionAdapter::UnRegisterDecisionListener, pkgName=%s", pkgName.c_str()); + if (stateListenerCallbackMap_.count(pkgName) == 1) { + stateListenerCallbackMap_.erase(pkgName); + } + if (infoListenerCallbackMap_.count(pkgName) == 1) { + infoListenerCallbackMap_.erase(pkgName); + } + if (waitCallbackIdMap_.count(pkgName) == 1) { + waitCallbackIdMap_.erase(pkgName); + } + return DM_OK; +} + +void DeviceDecisionAdapter::SetDecisionId(const std::string &pkgName, int32_t decisionId) +{ + LOGI("DeviceDecisionAdapter::SetDecisionId, pkgName=%s, decisionId=%d", pkgName.c_str(), decisionId); + waitCallbackIdMap_[pkgName] = decisionId; +} + +static void TimeOut(void *data) +{ + LOGI("DeviceDecisionAdapter time out "); + DeviceDecisionAdapter *decision = (DeviceDecisionAdapter *)data; + if (decision == nullptr) { + LOGE("time out error"); + return; + } + decision->OnResult("test"); +} + +int32_t DeviceDecisionAdapter::FilterDeviceList(std::vector &infoList, + const std::string &filterOptions) +{ + infoListTemp_ = infoList; + std::string extra = filterOptions; + nlohmann::json jsonObject = nlohmann::json::parse(extra, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("extra jsonStr error"); + return DM_FAILED; + } + + if (!jsonObject.contains("FilterOptions")) { + LOGE("err FilterOptions json string"); + return DM_FAILED; + } + + std::string filterString = jsonObject["FilterOptions"]["filter"]; + LOGI("DeviceDecisionAdapter::FilterOptions = %s", filterString.c_str()); + //////////////////////////////////////////////////////////////////////////////////////////// + StartTimer->Start(DECISION_TEST_CALLBACK_TIME, TimeOut, this); + //////////////////////////////////////////////////////////////////////////////////////////// + return DM_OK; +} + +int32_t DeviceDecisionAdapter::SortDeviceList(std::vector &infoList, const std::string &sortOptions) +{ + std::string extra = sortOptions; + nlohmann::json jsonObject = nlohmann::json::parse(extra, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("extra jsonStr error"); + return DM_FAILED; + } + + if (!jsonObject.contains("FilterOptions")) { + LOGE("err FilterOptions json string"); + return DM_FAILED; + } + + int32_t sortType = jsonObject["FilterOptions"]["sortType"]; + LOGI("DeviceDecisionAdapter::sortType = %d", sortType); + return DM_OK; +} + +void DeviceDecisionAdapter::OnResult(const char *result) +{ + LOGI("DeviceDecisionAdapter::OnResult"); + std::string resultString = std::string(result); + std::vector infoList; + infoList = infoListTemp_; // for test + for (auto iter : waitCallbackIdMap_) { + std::string pkgName = iter.first; + int32_t waitResultDecisionId = iter.second; + LOGI("DeviceDecisionAdapter::OnResult pkgName=%s, waitResultDecisionId=%d", + pkgName.c_str(), waitResultDecisionId); + if (waitResultDecisionId == DECISION_ID_TRUSTLIST) { + auto iter = infoListenerCallbackMap_.find(pkgName); + LOGI("infoListenerCallbackMap_ %d", infoListenerCallbackMap_.size()); + if (iter != infoListenerCallbackMap_.end()) { + std::shared_ptr infoManagerPtr = iter->second; + if (infoManagerPtr != nullptr) { + infoManagerPtr->OnDecisionResult(pkgName, infoList, DECISION_ID_TRUSTLIST); + } + } + } else if (waitResultDecisionId == DECISION_ID_ONLINE) { + auto iter = stateListenerCallbackMap_.find(pkgName); + LOGI("stateListenerCallbackMap_ %d", stateListenerCallbackMap_.size()); + if (iter != stateListenerCallbackMap_.end()) { + std::shared_ptr stateManagerPtr = iter->second; + if (stateManagerPtr != nullptr) { + stateManagerPtr->OnDecisionResult(pkgName, infoList, DECISION_ID_ONLINE); + } + } + } else if (waitResultDecisionId == DECISION_ID_OFFLINE) { + auto iter = stateListenerCallbackMap_.find(pkgName); + LOGI("stateListenerCallbackMap_ %d", stateListenerCallbackMap_.size()); + if (iter != stateListenerCallbackMap_.end()) { + std::shared_ptr stateManagerPtr = iter->second; + if (stateManagerPtr != nullptr) { + stateManagerPtr->OnDecisionResult(pkgName, infoList, DECISION_ID_OFFLINE); + } + } + } + } +} + +extern "C" IDecisionAdapter *CreateDeviceDecisionObject(void) +{ + return new DeviceDecisionAdapter; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/ext/profile/src/device_profile_adapter.cpp b/ext/profile/src/device_profile_adapter.cpp index 5d6165a01..5f8c73dc0 100644 --- a/ext/profile/src/device_profile_adapter.cpp +++ b/ext/profile/src/device_profile_adapter.cpp @@ -42,7 +42,7 @@ int32_t DeviceProfileAdapter::RegisterProfileListener(const std::string &pkgName int32_t DeviceProfileAdapter::UnRegisterProfileListener(const std::string &pkgName) { - LOGI("DeviceProfileAdapter::RegisterProfileListener"); + LOGI("DeviceProfileAdapter::UnRegisterProfileListener"); deviceProfileAdapterCallback_ = nullptr; profileConnector_->UnRegisterProfileCallback(pkgName); return DM_OK; diff --git a/interfaces/inner_kits/native_cpp/include/device_manager.h b/interfaces/inner_kits/native_cpp/include/device_manager.h index 9e76a1361..c367aa613 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager.h @@ -34,7 +34,8 @@ 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; + std::vector &deviceList, + std::shared_ptr callback = nullptr) = 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; @@ -54,6 +55,8 @@ public: 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; + virtual int32_t RegisterDecisionFilter(const std::string &pkgName, const std::string &extra) = 0; + virtual int32_t UnRegisterDecisionFilter(const std::string &pkgName, const std::string &extra) = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/include/device_manager_callback.h b/interfaces/inner_kits/native_cpp/include/device_manager_callback.h index f175f2344..223a84626 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_callback.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_callback.h @@ -61,6 +61,14 @@ public: int32_t reason) = 0; }; +class TrustListCallback { +public: + virtual ~TrustListCallback() + { + } + virtual void OnTrustListResult(const std::vector &deviceList) = 0; +}; + class VerifyAuthCallback { public: virtual ~VerifyAuthCallback() diff --git a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h index 46d8c3222..e2386e6a0 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -31,7 +31,8 @@ public: 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; + std::vector &deviceList, + std::shared_ptr callback = nullptr) 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; @@ -55,6 +56,8 @@ public: std::string &udid) override; virtual int32_t GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId, std::string &uuid) override; + virtual int32_t RegisterDecisionFilter(const std::string &pkgName, const std::string &extra) override; + virtual int32_t UnRegisterDecisionFilter(const std::string &pkgName, const std::string &extra) override; private: DeviceManagerImpl() = default; diff --git a/interfaces/inner_kits/native_cpp/include/dm_decision_info.h b/interfaces/inner_kits/native_cpp/include/dm_decision_info.h new file mode 100644 index 000000000..3dcbbd48a --- /dev/null +++ b/interfaces/inner_kits/native_cpp/include/dm_decision_info.h @@ -0,0 +1,42 @@ +/* + * 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_DM_DECISION_INFO_H +#define OHOS_DM_DECISION_INFO_H + +#include + +namespace OHOS { +namespace DistributedHardware { +typedef enum DeviceSortType { + /* Most Recent Use. */ + MOST_RECENT_USE = 0, + /* Most Frequently Use. */ + MOST_FREQUENT_USE = 1, + /* Intelligence Mode. */ + AI = 0x02, +} DeviceSortType; + +typedef struct FilterOptions { + /* Filter application-level trusted devices is it exits. */ + std::string targetPkgName; + /* device sortType */ + DeviceSortType sortType; + /* filters, json string */ + std::string filter; +} FilterOptions; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_DECISION_INFO_H diff --git a/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h b/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h index 2f882bb12..cd9b265ad 100644 --- a/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h +++ b/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h @@ -49,6 +49,9 @@ public: void UnRegisterVerifyAuthenticationCallback(const std::string &pkgName); void RegisterDeviceManagerFaCallback(const std::string &pkgName, std::shared_ptr callback); void UnRegisterDeviceManagerFaCallback(const std::string &pkgName); + void RegisterDeviceManagerTrustListCallback(const std::string &pkgName, + std::shared_ptr callback); + void UnRegisterDeviceManagerTrustListCallback(const std::string &pkgName); public: void OnRemoteDied(); @@ -62,6 +65,7 @@ public: 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, int32_t flag); + void OnTrustListResult(const std::string &pkgName, const std::vector &deviceList); void OnFaCall(std::string &pkgName, std::string ¶mJson); private: @@ -72,6 +76,7 @@ private: std::map> verifyAuthCallback_; std::map> dmInitCallback_; std::map> dmFaCallback_; + std::map> trustListCallback_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index 811a6d7f4..c24780171 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -43,6 +43,7 @@ #include "ipc_stop_discovery_req.h" #include "ipc_unauthenticate_device_req.h" #include "ipc_verify_authenticate_req.h" +#include "ipc_register_decision_req.h" #include "securec.h" #ifdef SUPPORT_CALLING_ABILITY @@ -142,7 +143,8 @@ int32_t DeviceManagerImpl::UnInitDeviceManager(const std::string &pkgName) } int32_t DeviceManagerImpl::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra, - std::vector &deviceList) + std::vector &deviceList, + std::shared_ptr callback) { LOGI("DeviceManager::GetTrustedDeviceList start, pkgName: %s", pkgName.c_str()); if (!isSystemAppCalling()) { @@ -155,6 +157,12 @@ int32_t DeviceManagerImpl::GetTrustedDeviceList(const std::string &pkgName, cons return DM_INVALID_VALUE; } + if (callback == nullptr) { + DeviceManagerNotify::GetInstance().UnRegisterDeviceManagerTrustListCallback(pkgName); + } else { + DeviceManagerNotify::GetInstance().RegisterDeviceManagerTrustListCallback(pkgName, callback); + } + std::shared_ptr req = std::make_shared(); std::shared_ptr rsp = std::make_shared(); req->SetPkgName(pkgName); @@ -219,6 +227,9 @@ int32_t DeviceManagerImpl::RegisterDevStateCallback(const std::string &pkgName, } DeviceManagerNotify::GetInstance().RegisterDeviceStateCallback(pkgName, callback); + if (extra != "") { + RegisterDecisionFilter(pkgName, extra); + } LOGI("RegisterDevStateCallback completed, pkgName: %s", pkgName.c_str()); return DM_OK; } @@ -237,6 +248,8 @@ int32_t DeviceManagerImpl::UnRegisterDevStateCallback(const std::string &pkgName } DeviceManagerNotify::GetInstance().UnRegisterDeviceStateCallback(pkgName); + std::string extra = ""; + UnRegisterDecisionFilter(pkgName, extra); LOGI("UnRegisterDevStateCallback completed, pkgName: %s", pkgName.c_str()); return DM_OK; } @@ -543,5 +556,51 @@ int32_t DeviceManagerImpl::GetUuidByNetworkId(const std::string &pkgName, const uuid = rsp->GetUuid(); return DM_OK; } + +int32_t DeviceManagerImpl::RegisterDecisionFilter(const std::string &pkgName, const std::string &extra) +{ + if (pkgName.empty()) { + LOGE("RegisterDecisionFilter 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->SetExtra(extra); + + if (ipcClientProxy_->SendRequest(REGISTER_DECISION_FILTER, req, rsp) != DM_OK) { + return DM_IPC_SEND_REQUEST_FAILED; + } + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("RegisterDecisionFilter Failed with ret %d", ret); + return ret; + } + return DM_OK; +} + +int32_t DeviceManagerImpl::UnRegisterDecisionFilter(const std::string &pkgName, const std::string &extra) +{ + if (pkgName.empty()) { + LOGE("UnRegisterDecisionFilter 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->SetExtra(extra); + + if (ipcClientProxy_->SendRequest(UNREGISTER_DECISION_FILTER, req, rsp) != DM_OK) { + return DM_IPC_SEND_REQUEST_FAILED; + } + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("UnRegisterDecisionFilter Failed with ret %d", ret); + return ret; + } + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp index be759e55e..fb6dd9f1d 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp @@ -34,6 +34,7 @@ #include "ipc_stop_discovery_req.h" #include "ipc_unauthenticate_device_req.h" #include "ipc_verify_authenticate_req.h" +#include "ipc_register_decision_req.h" #include "securec.h" namespace OHOS { @@ -349,6 +350,54 @@ ON_IPC_READ_RESPONSE(SERVER_USER_AUTH_OPERATION, MessageParcel &reply, std::shar return DM_OK; } +ON_IPC_SET_REQUEST(REGISTER_DECISION_FILTER, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + std::string extra = pReq->GetExtra(); + + if (!data.WriteString(pkgName)) { + LOGE("write pkgName failed"); + return DM_IPC_TRANSACTION_FAILED; + } + if (!data.WriteString(extra)) { + LOGE("write extra failed"); + return DM_WRITE_FAILED; + } + + return DM_OK; +} + +ON_IPC_READ_RESPONSE(REGISTER_DECISION_FILTER, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(UNREGISTER_DECISION_FILTER, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + std::string extra = pReq->GetExtra(); + + if (!data.WriteString(pkgName)) { + LOGE("write pkgName failed"); + return DM_IPC_TRANSACTION_FAILED; + } + if (!data.WriteString(extra)) { + LOGE("write extra failed"); + return DM_WRITE_FAILED; + } + + return DM_OK; +} + +ON_IPC_READ_RESPONSE(UNREGISTER_DECISION_FILTER, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + ON_IPC_CMD(SERVER_DEVICE_STATE_NOTIFY, MessageParcel &data, MessageParcel &reply) { std::string pkgName = data.ReadString(); @@ -435,6 +484,33 @@ ON_IPC_CMD(SERVER_VERIFY_AUTH_RESULT, MessageParcel &data, MessageParcel &reply) return DM_OK; } +ON_IPC_CMD(SERVER_TRUST_LIST_RESULT, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + + int32_t deviceNum = data.ReadInt32(); + uint32_t deviceTotalSize = deviceNum * (int32_t)sizeof(DmDeviceInfo); + std::vector deviceInfoVec; + + if (deviceTotalSize > 0) { + DmDeviceInfo dmDeviceInfo; + void *deviceInfo = nullptr; + size_t deviceSize = sizeof(DmDeviceInfo); + for (int32_t i = 0; i < deviceNum; ++i) { + deviceInfo = (void *)data.ReadRawData(deviceSize); + if (deviceInfo != nullptr && memcpy_s(&dmDeviceInfo, deviceSize, deviceInfo, deviceSize) != 0) { + reply.WriteInt32(DM_IPC_COPY_FAILED); + return DM_IPC_COPY_FAILED; + } + deviceInfoVec.emplace_back(dmDeviceInfo); + } + } + + DeviceManagerNotify::GetInstance().OnTrustListResult(pkgName, deviceInfoVec); + reply.WriteInt32(DM_OK); + return DM_OK; +} + ON_IPC_CMD(SERVER_DEVICE_FA_NOTIFY, MessageParcel &data, MessageParcel &reply) { std::string packagename = data.ReadString(); diff --git a/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp b/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp index 987ca17c5..da8ae69ac 100644 --- a/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp +++ b/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp @@ -130,6 +130,23 @@ void DeviceManagerNotify::UnRegisterDeviceManagerFaCallback(const std::string &p dmFaCallback_.erase(pkgName); } +void DeviceManagerNotify::RegisterDeviceManagerTrustListCallback(const std::string &pkgName, + std::shared_ptr callback) +{ + std::lock_guard autoLock(lock_); + trustListCallback_[pkgName] = callback; +} + +void DeviceManagerNotify::UnRegisterDeviceManagerTrustListCallback(const std::string &pkgName) +{ + std::lock_guard autoLock(lock_); + if (trustListCallback_.count(pkgName) == 0) { + LOGE("DeviceManager UnRegisterDeviceManagerTrustListCallback not register"); + return; + } + trustListCallback_.erase(pkgName); +} + void DeviceManagerNotify::OnRemoteDied() { LOGW("DeviceManager : OnRemoteDied"); @@ -271,6 +288,20 @@ void DeviceManagerNotify::OnVerifyAuthResult(const std::string &pkgName, const s verifyAuthCallback_.erase(pkgName); } +void DeviceManagerNotify::OnTrustListResult(const std::string &pkgName, const std::vector &deviceList) +{ + LOGI("DeviceManagerNotify::OnTrustListResult pkgName:%s", pkgName.c_str()); + std::lock_guard autoLock(lock_); + if (trustListCallback_.count(pkgName) == 0) { + LOGE("DeviceManager OnTrustListResult: no register trustListCallback_ for this package"); + return; + } + if (trustListCallback_[pkgName] != nullptr) { + trustListCallback_[pkgName]->OnTrustListResult(deviceList); + trustListCallback_.erase(pkgName); + } +} + void DeviceManagerNotify::OnFaCall(std::string &pkgName, std::string ¶mJson) { LOGI("DeviceManager OnFaCallback pkgName:%s", pkgName.c_str()); diff --git a/interfaces/kits/js/include/native_devicemanager_js.h b/interfaces/kits/js/include/native_devicemanager_js.h index 71cb995b4..a15683aad 100644 --- a/interfaces/kits/js/include/native_devicemanager_js.h +++ b/interfaces/kits/js/include/native_devicemanager_js.h @@ -47,6 +47,20 @@ struct DeviceInfoAsyncCallbackInfo { std::string bundleName; size_t bundleNameLen = 0; OHOS::DistributedHardware::DmDeviceInfo deviceInfo; + std::string extra; + // OHOS::DistributedHardware::DmFilterOptions filter; + napi_ref callback = nullptr; + napi_value thisVar = nullptr; + napi_deferred deferred = nullptr; + int32_t status = -1; +}; + +struct DeviceInfoListAsyncCallbackInfo { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + + std::string bundleName; + size_t bundleNameLen = 0; std::vector devList; std::string extra; // OHOS::DistributedHardware::DmFilterOptions filter; @@ -54,7 +68,6 @@ struct DeviceInfoAsyncCallbackInfo { napi_value thisVar = nullptr; napi_deferred deferred = nullptr; int32_t status = -1; - int32_t isList = 0; }; struct AuthAsyncCallbackInfo { @@ -159,6 +172,19 @@ private: std::string bundleName_; }; +class DmNapiTrustListCallback : public OHOS::DistributedHardware::TrustListCallback { +public: + explicit DmNapiTrustListCallback(napi_env env, std::string &bundleName) : env_(env), bundleName_(bundleName) + { + } + virtual ~DmNapiTrustListCallback() {}; + void OnTrustListResult(const std::vector &deviceList) override; + +private: + napi_env env_; + std::string bundleName_; +}; + class DeviceManagerNapi : public DmNativeEvent { public: explicit DeviceManagerNapi(napi_env env, napi_value thisVar); @@ -183,6 +209,7 @@ public: static void HandleCreateDmCallBack(const napi_env &env, AsyncCallbackInfo *asCallbackInfo); static DeviceManagerNapi *GetDeviceManagerNapi(std::string &buldleName); static void CreateDmCallback(napi_env env, std::string &bundleName, std::string &eventType); + static void CreateDmCallback(napi_env env, std::string &bundleName, std::string &eventType, std::string &extra); static void ReleaseDmCallback(std::string &bundleName, std::string &eventType); static void DeviceInfoToJsArray(const napi_env &env, const std::vector &vecDevInfo, @@ -222,6 +249,7 @@ public: void OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason); void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, int32_t reason); void OnVerifyResult(const std::string &deviceId, int32_t resultCode, int32_t flag); + void OnTrustListResult(napi_env env_, const std::vector &deviceList); void OnDmfaCall(const std::string ¶mJson); private: @@ -229,12 +257,14 @@ private: static napi_value JsOnFrench(napi_env env, int32_t num, napi_value thisVar, napi_value argv[]); static void CallAsyncWorkSync(napi_env env, DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo); static void CallAsyncWork(napi_env env, DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo); + static void CallAsyncWorkSync(napi_env env, DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo); + static void CallAsyncWork(napi_env env, DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo); static void CallGetTrustedDeviceListStatusSync(napi_env env, napi_status &status, - DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo); + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo); static void CallGetTrustedDeviceListStatus(napi_env env, napi_status &status, - DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo); + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo); static napi_value CallDeviceList(napi_env env, napi_callback_info info, - DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo); + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo); static void CallGetLocalDeviceInfoSync(napi_env env, napi_status &status, DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo); static void CallGetLocalDeviceInfo(napi_env env, napi_status &status, @@ -247,6 +277,7 @@ private: std::string bundleName_; static AuthAsyncCallbackInfo authAsyncCallbackInfo_; static AuthAsyncCallbackInfo verifyAsyncCallbackInfo_; + static DeviceInfoListAsyncCallbackInfo TrustListAsyncCallbackInfo_; }; #endif // OHOS_DM_NATIVE_DEVICEMANAGER_JS_H diff --git a/interfaces/kits/js/src/native_devicemanager_js.cpp b/interfaces/kits/js/src/native_devicemanager_js.cpp index bb96c37a7..74bdf8199 100644 --- a/interfaces/kits/js/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js/src/native_devicemanager_js.cpp @@ -54,11 +54,13 @@ std::map> g_DiscoveryCallb std::map> g_authCallbackMap; std::map> g_verifyAuthCallbackMap; std::map> g_dmfaCallbackMap; +std::map> g_TrustListCallbackMap; } // namespace thread_local napi_ref DeviceManagerNapi::sConstructor_ = nullptr; AuthAsyncCallbackInfo DeviceManagerNapi::authAsyncCallbackInfo_; AuthAsyncCallbackInfo DeviceManagerNapi::verifyAsyncCallbackInfo_; +DeviceInfoListAsyncCallbackInfo DeviceManagerNapi::TrustListAsyncCallbackInfo_; void DmNapiInitCallback::OnRemoteDied() { @@ -366,6 +368,17 @@ void DmNapiVerifyAuthCallback::OnVerifyAuthResult(const std::string &deviceId, i } } +void DmNapiTrustListCallback::OnTrustListResult(const std::vector &deviceList) +{ + LOGI("DmNapiTrustListCallback::OnTrustListResult"); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(bundleName_); + if (deviceManagerNapi == nullptr) { + LOGE("OnTrustListResult, deviceManagerNapi not find for bunderName %s", bundleName_.c_str()); + return; + } + deviceManagerNapi->OnTrustListResult(env_, deviceList); +} + DeviceManagerNapi::DeviceManagerNapi(napi_env env, napi_value thisVar) : DmNativeEvent(env, thisVar) { env_ = env; @@ -494,6 +507,18 @@ void DeviceManagerNapi::OnVerifyResult(const std::string &deviceId, int32_t resu g_verifyAuthCallbackMap.erase(bundleName_); } +void DeviceManagerNapi::OnTrustListResult(napi_env env_, const std::vector &deviceList) +{ + LOGI("DeviceManagerNapi::OnTrustListResult"); + LOGI("TrustListAsyncCallbackInfo_ %s, %d", + TrustListAsyncCallbackInfo_.bundleName.c_str(), TrustListAsyncCallbackInfo_.status); + napi_status status; + (void)status; + TrustListAsyncCallbackInfo_.devList = deviceList; + CallGetTrustedDeviceListStatus(env_, status, &TrustListAsyncCallbackInfo_); + g_TrustListCallbackMap.erase(bundleName_); +} + void DeviceManagerNapi::SetValueUtf8String(const napi_env &env, const std::string &fieldStr, const std::string &str, napi_value &result) { @@ -860,6 +885,25 @@ void DeviceManagerNapi::CreateDmCallback(napi_env env, std::string &bundleName, } } +void DeviceManagerNapi::CreateDmCallback(napi_env env, std::string &bundleName, + std::string &eventType, std::string &extra) +{ + LOGE("CreateDmCallback for bundleName %s eventType %s extra=%s", + bundleName.c_str(), eventType.c_str(), extra.c_str()); + if (eventType == DM_NAPI_EVENT_DEVICE_STATE_CHANGE) { + auto iter = g_deviceStateCallbackMap.find(bundleName); + if (iter == g_deviceStateCallbackMap.end()) { + auto callback = std::make_shared(env, bundleName); + int32_t ret = DeviceManager::GetInstance().RegisterDevStateCallback(bundleName, extra, callback); + if (ret != 0) { + LOGE("RegisterDevStateCallback failed for bunderName %s", bundleName.c_str()); + return; + } + g_deviceStateCallbackMap[bundleName] = callback; + } + } +} + void DeviceManagerNapi::ReleaseDmCallback(std::string &bundleName, std::string &eventType) { if (eventType == DM_NAPI_EVENT_DEVICE_STATE_CHANGE) { @@ -953,27 +997,28 @@ napi_value DeviceManagerNapi::SetUserOperationSync(napi_env env, napi_callback_i } void DeviceManagerNapi::CallGetTrustedDeviceListStatusSync(napi_env env, napi_status &status, - DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo) + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo) { - for (unsigned int i = 0; i < deviceInfoAsyncCallbackInfo->devList.size(); i++) { + for (unsigned int i = 0; i < deviceInfoListAsyncCallbackInfo->devList.size(); i++) { LOGI("DeviceManager::GetTrustedDeviceList deviceId:%s deviceName:%s deviceTypeId:%d ", - deviceInfoAsyncCallbackInfo->devList[i].deviceId, deviceInfoAsyncCallbackInfo->devList[i].deviceName, - deviceInfoAsyncCallbackInfo->devList[i].deviceTypeId); + deviceInfoListAsyncCallbackInfo->devList[i].deviceId, + deviceInfoListAsyncCallbackInfo->devList[i].deviceName, + deviceInfoListAsyncCallbackInfo->devList[i].deviceTypeId); } napi_value array[DM_NAPI_ARGS_TWO] = {0}; - if (deviceInfoAsyncCallbackInfo->status == 0) { - if (deviceInfoAsyncCallbackInfo->devList.size() > 0) { + if (deviceInfoListAsyncCallbackInfo->status == 0) { + if (deviceInfoListAsyncCallbackInfo->devList.size() > 0) { bool isArray = false; napi_create_array(env, &array[1]); napi_is_array(env, array[1], &isArray); if (isArray == false) { LOGE("napi_create_array fail"); } - for (unsigned int i = 0; i != deviceInfoAsyncCallbackInfo->devList.size(); ++i) { - DeviceInfoToJsArray(env, deviceInfoAsyncCallbackInfo->devList, i, array[1]); + for (unsigned int i = 0; i != deviceInfoListAsyncCallbackInfo->devList.size(); ++i) { + DeviceInfoToJsArray(env, deviceInfoListAsyncCallbackInfo->devList, i, array[1]); } - napi_resolve_deferred(env, deviceInfoAsyncCallbackInfo->deferred, array[1]); + napi_resolve_deferred(env, deviceInfoListAsyncCallbackInfo->deferred, array[1]); LOGE("devList is OK"); } else { LOGE("devList is null"); @@ -981,7 +1026,7 @@ void DeviceManagerNapi::CallGetTrustedDeviceListStatusSync(napi_env env, napi_st } else { napi_create_object(env, &array[0]); SetValueInt32(env, "code", status, array[0]); - napi_reject_deferred(env, deviceInfoAsyncCallbackInfo->deferred, array[0]); + napi_reject_deferred(env, deviceInfoListAsyncCallbackInfo->deferred, array[0]); } } @@ -1025,19 +1070,20 @@ void DeviceManagerNapi::OnDmfaCall(const std::string ¶mJson) } void DeviceManagerNapi::CallGetTrustedDeviceListStatus(napi_env env, napi_status &status, - DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo) + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo) { - for (unsigned int i = 0; i < deviceInfoAsyncCallbackInfo->devList.size(); i++) { + for (unsigned int i = 0; i < deviceInfoListAsyncCallbackInfo->devList.size(); i++) { LOGI("DeviceManager::GetTrustedDeviceList deviceId:%s deviceName:%s deviceTypeId:%d ", - deviceInfoAsyncCallbackInfo->devList[i].deviceId, deviceInfoAsyncCallbackInfo->devList[i].deviceName, - deviceInfoAsyncCallbackInfo->devList[i].deviceTypeId); + deviceInfoListAsyncCallbackInfo->devList[i].deviceId, + deviceInfoListAsyncCallbackInfo->devList[i].deviceName, + deviceInfoListAsyncCallbackInfo->devList[i].deviceTypeId); } napi_value callResult = nullptr; napi_value handler = nullptr; napi_value array[DM_NAPI_ARGS_TWO] = {0}; - if (deviceInfoAsyncCallbackInfo->status == 0) { - if (deviceInfoAsyncCallbackInfo->devList.size() > 0) { + if (deviceInfoListAsyncCallbackInfo->status == 0) { + if (deviceInfoListAsyncCallbackInfo->devList.size() > 0) { bool isArray = false; napi_create_array(env, &array[1]); napi_is_array(env, array[1], &isArray); @@ -1045,8 +1091,8 @@ void DeviceManagerNapi::CallGetTrustedDeviceListStatus(napi_env env, napi_status LOGE("napi_create_array fail"); } - for (size_t i = 0; i != deviceInfoAsyncCallbackInfo->devList.size(); ++i) { - DeviceInfoToJsArray(env, deviceInfoAsyncCallbackInfo->devList, i, array[1]); + for (size_t i = 0; i != deviceInfoListAsyncCallbackInfo->devList.size(); ++i) { + DeviceInfoToJsArray(env, deviceInfoListAsyncCallbackInfo->devList, i, array[1]); } LOGE("devList is OK"); } else { @@ -1057,10 +1103,10 @@ void DeviceManagerNapi::CallGetTrustedDeviceListStatus(napi_env env, napi_status SetValueInt32(env, "code", status, array[0]); } - napi_get_reference_value(env, deviceInfoAsyncCallbackInfo->callback, &handler); + napi_get_reference_value(env, deviceInfoListAsyncCallbackInfo->callback, &handler); if (handler != nullptr) { napi_call_function(env, nullptr, handler, DM_NAPI_ARGS_TWO, &array[0], &callResult); - napi_delete_reference(env, deviceInfoAsyncCallbackInfo->callback); + napi_delete_reference(env, deviceInfoListAsyncCallbackInfo->callback); } else { LOGE("handler is nullptr"); } @@ -1121,14 +1167,8 @@ void DeviceManagerNapi::CallAsyncWorkSync(napi_env env, DeviceInfoAsyncCallbackI (void)env; DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo = (DeviceInfoAsyncCallbackInfo *)data; int32_t ret = 0; - if (deviceInfoAsyncCallbackInfo->isList == 1) { - ret = DeviceManager::GetInstance().GetTrustedDeviceList(deviceInfoAsyncCallbackInfo->bundleName, - deviceInfoAsyncCallbackInfo->extra, - deviceInfoAsyncCallbackInfo->devList); - } else { - ret = DeviceManager::GetInstance().GetLocalDeviceInfo(deviceInfoAsyncCallbackInfo->bundleName, - deviceInfoAsyncCallbackInfo->deviceInfo); - } + ret = DeviceManager::GetInstance().GetLocalDeviceInfo(deviceInfoAsyncCallbackInfo->bundleName, + deviceInfoAsyncCallbackInfo->deviceInfo); if (ret != 0) { LOGE("CallAsyncWorkSync for bunderName %s failed, ret %d", deviceInfoAsyncCallbackInfo->bundleName.c_str(), ret); @@ -1141,11 +1181,7 @@ void DeviceManagerNapi::CallAsyncWorkSync(napi_env env, DeviceInfoAsyncCallbackI [](napi_env env, napi_status status, void *data) { (void)status; DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo = (DeviceInfoAsyncCallbackInfo *)data; - if (deviceInfoAsyncCallbackInfo->isList == 1) { - CallGetTrustedDeviceListStatusSync(env, status, deviceInfoAsyncCallbackInfo); - } else { - CallGetLocalDeviceInfoSync(env, status, deviceInfoAsyncCallbackInfo); - } + CallGetLocalDeviceInfoSync(env, status, deviceInfoAsyncCallbackInfo); napi_delete_async_work(env, deviceInfoAsyncCallbackInfo->asyncWork); delete deviceInfoAsyncCallbackInfo; }, @@ -1162,17 +1198,11 @@ void DeviceManagerNapi::CallAsyncWork(napi_env env, DeviceInfoAsyncCallbackInfo [](napi_env env, void *data) { DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo = (DeviceInfoAsyncCallbackInfo *)data; int32_t ret = 0; - if (deviceInfoAsyncCallbackInfo->isList == 1) { - ret = DeviceManager::GetInstance().GetTrustedDeviceList(deviceInfoAsyncCallbackInfo->bundleName, - deviceInfoAsyncCallbackInfo->extra, - deviceInfoAsyncCallbackInfo->devList); - } else { - ret = DeviceManager::GetInstance().GetLocalDeviceInfo(deviceInfoAsyncCallbackInfo->bundleName, - deviceInfoAsyncCallbackInfo->deviceInfo); - } + ret = DeviceManager::GetInstance().GetLocalDeviceInfo(deviceInfoAsyncCallbackInfo->bundleName, + deviceInfoAsyncCallbackInfo->deviceInfo); if (ret != 0) { - LOGE("CallAsyncWork for bunderName %s failed, ret %d", deviceInfoAsyncCallbackInfo->bundleName.c_str(), - ret); + LOGE("CallAsyncWork for bunderName %s failed, ret %d", + deviceInfoAsyncCallbackInfo->bundleName.c_str(), ret); deviceInfoAsyncCallbackInfo->status = -1; return; } @@ -1182,11 +1212,7 @@ void DeviceManagerNapi::CallAsyncWork(napi_env env, DeviceInfoAsyncCallbackInfo [](napi_env env, napi_status status, void *data) { (void)status; DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo = (DeviceInfoAsyncCallbackInfo *)data; - if (deviceInfoAsyncCallbackInfo->isList == 1) { - CallGetTrustedDeviceListStatus(env, status, deviceInfoAsyncCallbackInfo); - } else { - CallGetLocalDeviceInfo(env, status, deviceInfoAsyncCallbackInfo); - } + CallGetLocalDeviceInfo(env, status, deviceInfoAsyncCallbackInfo); napi_delete_async_work(env, deviceInfoAsyncCallbackInfo->asyncWork); delete deviceInfoAsyncCallbackInfo; }, @@ -1194,19 +1220,107 @@ void DeviceManagerNapi::CallAsyncWork(napi_env env, DeviceInfoAsyncCallbackInfo napi_queue_async_work(env, deviceInfoAsyncCallbackInfo->asyncWork); } +void DeviceManagerNapi::CallAsyncWorkSync(napi_env env, DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo) +{ + napi_value resourceName; + napi_create_string_latin1(env, "GetTrustListInfo", NAPI_AUTO_LENGTH, &resourceName); + napi_create_async_work( + env, nullptr, resourceName, + [](napi_env env, void *data) { + (void)env; + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo = (DeviceInfoListAsyncCallbackInfo *)data; + int32_t ret = 0; + ret = DeviceManager::GetInstance().GetTrustedDeviceList(deviceInfoListAsyncCallbackInfo->bundleName, + deviceInfoListAsyncCallbackInfo->extra, + deviceInfoListAsyncCallbackInfo->devList); + if (ret != 0) { + LOGE("CallAsyncWorkSync for bunderName %s failed, ret %d", + deviceInfoListAsyncCallbackInfo->bundleName.c_str(), ret); + deviceInfoListAsyncCallbackInfo->status = -1; + return; + } + deviceInfoListAsyncCallbackInfo->status = 0; + LOGE("CallAsyncWorkSync status %d", deviceInfoListAsyncCallbackInfo->status); + }, + [](napi_env env, napi_status status, void *data) { + (void)status; + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo = (DeviceInfoListAsyncCallbackInfo *)data; + CallGetTrustedDeviceListStatusSync(env, status, deviceInfoListAsyncCallbackInfo); + napi_delete_async_work(env, deviceInfoListAsyncCallbackInfo->asyncWork); + delete deviceInfoListAsyncCallbackInfo; + }, + (void *)deviceInfoListAsyncCallbackInfo, &deviceInfoListAsyncCallbackInfo->asyncWork); + napi_queue_async_work(env, deviceInfoListAsyncCallbackInfo->asyncWork); +} + +void DeviceManagerNapi::CallAsyncWork(napi_env env, DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo) +{ + napi_value resourceName; + napi_create_string_latin1(env, "GetTrustListInfo", NAPI_AUTO_LENGTH, &resourceName); + TrustListAsyncCallbackInfo_.status = deviceInfoListAsyncCallbackInfo->status; + TrustListAsyncCallbackInfo_.bundleName = deviceInfoListAsyncCallbackInfo->bundleName; + TrustListAsyncCallbackInfo_.devList = deviceInfoListAsyncCallbackInfo->devList; + TrustListAsyncCallbackInfo_.callback = deviceInfoListAsyncCallbackInfo->callback; + // for test + std::string extraString = + "{\"FilterOptions\": {\"targetPkgName\":\"targetPkgName\",\"sortType\":0,\"filter\":\"filter\"}}"; + deviceInfoListAsyncCallbackInfo->extra = extraString; + std::shared_ptr trustListCallback = nullptr; + auto iter = g_TrustListCallbackMap.find(deviceInfoListAsyncCallbackInfo->bundleName); + if (iter == g_TrustListCallbackMap.end()) { + trustListCallback = std::make_shared(env, deviceInfoListAsyncCallbackInfo->bundleName); + g_TrustListCallbackMap[deviceInfoListAsyncCallbackInfo->bundleName] = trustListCallback; + } else { + trustListCallback = iter->second; + } + DeviceManager::GetInstance().GetTrustedDeviceList(deviceInfoListAsyncCallbackInfo->bundleName, + deviceInfoListAsyncCallbackInfo->extra, + deviceInfoListAsyncCallbackInfo->devList, + trustListCallback); + TrustListAsyncCallbackInfo_.status = 0; + return; + + napi_create_async_work( + env, nullptr, resourceName, + [](napi_env env, void *data) { + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo = (DeviceInfoListAsyncCallbackInfo *)data; + int32_t ret = 0; + ret = DeviceManager::GetInstance().GetTrustedDeviceList(deviceInfoListAsyncCallbackInfo->bundleName, + deviceInfoListAsyncCallbackInfo->extra, + deviceInfoListAsyncCallbackInfo->devList); + if (ret != 0) { + LOGE("CallAsyncWork for bunderName %s failed, ret %d", + deviceInfoListAsyncCallbackInfo->bundleName.c_str(), ret); + deviceInfoListAsyncCallbackInfo->status = -1; + return; + } + deviceInfoListAsyncCallbackInfo->status = 0; + LOGE("CallAsyncWork status %d", deviceInfoListAsyncCallbackInfo->status); + }, + [](napi_env env, napi_status status, void *data) { + (void)status; + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo = (DeviceInfoListAsyncCallbackInfo *)data; + CallGetTrustedDeviceListStatus(env, status, deviceInfoListAsyncCallbackInfo); + napi_delete_async_work(env, deviceInfoListAsyncCallbackInfo->asyncWork); + delete deviceInfoListAsyncCallbackInfo; + }, + (void *)deviceInfoListAsyncCallbackInfo, &deviceInfoListAsyncCallbackInfo->asyncWork); + napi_queue_async_work(env, deviceInfoListAsyncCallbackInfo->asyncWork); +} + napi_value DeviceManagerNapi::CallDeviceList(napi_env env, napi_callback_info info, - DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo) + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo) { napi_value result = nullptr; std::string extra = ""; - deviceInfoAsyncCallbackInfo->extra = extra; + deviceInfoListAsyncCallbackInfo->extra = extra; GET_PARAMS(env, info, DM_NAPI_ARGS_ONE); napi_valuetype eventHandleType = napi_undefined; napi_typeof(env, argv[0], &eventHandleType); if (eventHandleType == napi_function) { LOGE("CallDeviceList for argc %d Type = %d", argc, (int)eventHandleType); - napi_create_reference(env, argv[0], 1, &deviceInfoAsyncCallbackInfo->callback); - CallAsyncWork(env, deviceInfoAsyncCallbackInfo); + napi_create_reference(env, argv[0], 1, &deviceInfoListAsyncCallbackInfo->callback); + CallAsyncWork(env, deviceInfoListAsyncCallbackInfo); napi_get_undefined(env, &result); return result; } else { @@ -1214,11 +1328,11 @@ napi_value DeviceManagerNapi::CallDeviceList(napi_env env, napi_callback_info in napi_deferred deferred; napi_value promise = 0; napi_create_promise(env, &deferred, &promise); - deviceInfoAsyncCallbackInfo->deferred = deferred; + deviceInfoListAsyncCallbackInfo->deferred = deferred; char extraString[20]; JsObjectToString(env, argv[0], "extra", extraString, sizeof(extraString)); - deviceInfoAsyncCallbackInfo->extra = extraString; - CallAsyncWorkSync(env, deviceInfoAsyncCallbackInfo); + deviceInfoListAsyncCallbackInfo->extra = extraString; + CallAsyncWorkSync(env, deviceInfoListAsyncCallbackInfo); return promise; } } @@ -1269,23 +1383,22 @@ napi_value DeviceManagerNapi::GetTrustedDeviceList(napi_env env, napi_callback_i DeviceManagerNapi *deviceManagerWrapper = nullptr; napi_unwrap(env, thisVar, reinterpret_cast(&deviceManagerWrapper)); - auto *deviceInfoAsyncCallbackInfo = new DeviceInfoAsyncCallbackInfo(); - deviceInfoAsyncCallbackInfo->env = env; - deviceInfoAsyncCallbackInfo->devList = devList; - deviceInfoAsyncCallbackInfo->isList = 1; - deviceInfoAsyncCallbackInfo->bundleName = deviceManagerWrapper->bundleName_; + auto *deviceInfoListAsyncCallbackInfo = new DeviceInfoListAsyncCallbackInfo(); + deviceInfoListAsyncCallbackInfo->env = env; + deviceInfoListAsyncCallbackInfo->devList = devList; + deviceInfoListAsyncCallbackInfo->bundleName = deviceManagerWrapper->bundleName_; LOGE("GetTrustedDeviceList for argc %d", argc); if (argc == 0) { std::string extra = ""; - deviceInfoAsyncCallbackInfo->extra = extra; + deviceInfoListAsyncCallbackInfo->extra = extra; napi_deferred deferred; napi_value promise = 0; napi_create_promise(env, &deferred, &promise); - deviceInfoAsyncCallbackInfo->deferred = deferred; - CallAsyncWorkSync(env, deviceInfoAsyncCallbackInfo); + deviceInfoListAsyncCallbackInfo->deferred = deferred; + CallAsyncWorkSync(env, deviceInfoListAsyncCallbackInfo); return promise; } else if (argc == 1) { - return CallDeviceList(env, info, deviceInfoAsyncCallbackInfo); + return CallDeviceList(env, info, deviceInfoListAsyncCallbackInfo); } else if (argc == DM_NAPI_ARGS_TWO) { GET_PARAMS(env, info, DM_NAPI_ARGS_TWO); napi_valuetype valueType; @@ -1299,9 +1412,9 @@ napi_value DeviceManagerNapi::GetTrustedDeviceList(napi_env env, napi_callback_i NAPI_ASSERT(env, eventHandleType == napi_function, "Wrong argument type. Object expected."); char extra[20]; JsObjectToString(env, argv[0], "extra", extra, sizeof(extra)); - deviceInfoAsyncCallbackInfo->extra = extra; - napi_create_reference(env, argv[1], 1, &deviceInfoAsyncCallbackInfo->callback); - CallAsyncWork(env, deviceInfoAsyncCallbackInfo); + deviceInfoListAsyncCallbackInfo->extra = extra; + napi_create_reference(env, argv[1], 1, &deviceInfoListAsyncCallbackInfo->callback); + CallAsyncWork(env, deviceInfoListAsyncCallbackInfo); napi_get_undefined(env, &result); return result; } @@ -1347,7 +1460,6 @@ napi_value DeviceManagerNapi::GetLocalDeviceInfo(napi_env env, napi_callback_inf auto *deviceInfoAsyncCallbackInfo = new DeviceInfoAsyncCallbackInfo(); deviceInfoAsyncCallbackInfo->env = env; deviceInfoAsyncCallbackInfo->deviceInfo = deviceInfo; - deviceInfoAsyncCallbackInfo->isList = 0; deviceInfoAsyncCallbackInfo->bundleName = deviceManagerWrapper->bundleName_; LOGE("GetLocalDeviceInfo for argc %d", argc); if (argc == 0) { @@ -1557,7 +1669,23 @@ napi_value DeviceManagerNapi::JsOnFrench(napi_env env, int32_t num, napi_value t LOGI("JsOn for bunderName %s, eventType %s ", deviceManagerWrapper->bundleName_.c_str(), eventType.c_str()); deviceManagerWrapper->On(eventType, argv[num + 1]); - CreateDmCallback(env, deviceManagerWrapper->bundleName_, eventType); + if (eventType == DM_NAPI_EVENT_DEVICE_STATE_CHANGE) { + // for test + std::string extraString = + "{\"FilterOptions\": {\"targetPkgName\":\"targetPkgName\",\"sortType\":0,\"filter\":\"filter\"}}"; + CreateDmCallback(env, deviceManagerWrapper->bundleName_, eventType, extraString); + } else if (num == 1) { + size_t extraLen = 0; + napi_get_value_string_utf8(env, argv[1], nullptr, 0, &extraLen); + NAPI_ASSERT(env, extraLen < DM_NAPI_BUF_LENGTH, "extraLen >= MAXLEN"); + char extra[DM_NAPI_BUF_LENGTH] = {0}; + napi_get_value_string_utf8(env, argv[1], extra, extraLen + 1, &extraLen); + std::string extraString = extra; + LOGI("extra = %s", extraString.c_str()); + CreateDmCallback(env, deviceManagerWrapper->bundleName_, eventType, extraString); + } else { + CreateDmCallback(env, deviceManagerWrapper->bundleName_, eventType); + } napi_value result = nullptr; napi_get_undefined(env, &result); @@ -1695,6 +1823,7 @@ napi_value DeviceManagerNapi::ReleaseDeviceManager(napi_env env, napi_callback_i g_DiscoveryCallbackMap.erase(deviceManagerWrapper->bundleName_); g_authCallbackMap.erase(deviceManagerWrapper->bundleName_); g_verifyAuthCallbackMap.erase(deviceManagerWrapper->bundleName_); + g_TrustListCallbackMap.erase(deviceManagerWrapper->bundleName_); napi_get_undefined(env, &result); return result; } diff --git a/services/devicemanagerservice/include/adapter/decision_adapter.h b/services/devicemanagerservice/include/adapter/decision_adapter.h index 4c6a35f28..5161e162d 100644 --- a/services/devicemanagerservice/include/adapter/decision_adapter.h +++ b/services/devicemanagerservice/include/adapter/decision_adapter.h @@ -23,6 +23,16 @@ namespace OHOS { namespace DistributedHardware { +class DmDeviceStateManager; +class DmDeviceInfoManager; + +typedef enum DecisionId { + DECISION_ID_DEFAULT = 0, + DECISION_ID_TRUSTLIST = 1, + DECISION_ID_ONLINE = 2, + DECISION_ID_OFFLINE = 3, +} DecisionId; + class IDecisionAdapter { public: virtual ~IDecisionAdapter() = default; @@ -30,6 +40,12 @@ public: virtual std::string GetVersion() = 0; virtual int32_t FilterDeviceList(std::vector &infoList, const std::string &filterOptions) = 0; virtual int32_t SortDeviceList(std::vector &infoList, const std::string &sortOptions) = 0; + virtual int32_t RegisterDecisionListener(const std::string &pkgName, + std::shared_ptr callback) = 0; + virtual int32_t RegisterDecisionListener(const std::string &pkgName, + std::shared_ptr callback) = 0; + virtual int32_t UnRegisterDecisionListener(const std::string &pkgName) = 0; + virtual void SetDecisionId(const std::string &pkgName, int32_t decisionId) = 0; }; using CreateIDecisionAdapterFuncPtr = IDecisionAdapter *(*)(void); diff --git a/services/devicemanagerservice/include/config/json_config.h b/services/devicemanagerservice/include/config/json_config.h index a1c495cc0..c23894cf9 100644 --- a/services/devicemanagerservice/include/config/json_config.h +++ b/services/devicemanagerservice/include/config/json_config.h @@ -38,6 +38,14 @@ const std::string adapterJsonConfigString = "funcName": "CreateDeviceProfileObject", "soName": "libdevicemanagerext_profile.z.so", "soPath": "/system/lib/" + }, + { + "name": "device_decision", + "type": "DECISION", + "version": "1.0", + "funcName": "CreateDeviceDecisionObject", + "soName": "libdevicemanagerext_decision.z.so", + "soPath": "/system/lib/" } ] })"; @@ -76,4 +84,4 @@ const std::string authJsonConfigString = })"; } // namespace DistributedHardware } // namespace OHOS -#endif // OHOS_OHOS_DM_JSON_CONFIG_H \ No newline at end of file +#endif // OHOS_OHOS_DM_JSON_CONFIG_H diff --git a/services/devicemanagerservice/include/device_manager_service.h b/services/devicemanagerservice/include/device_manager_service.h index aedfc875d..361ccf1ff 100644 --- a/services/devicemanagerservice/include/device_manager_service.h +++ b/services/devicemanagerservice/include/device_manager_service.h @@ -49,6 +49,8 @@ public: int32_t VerifyAuthentication(const std::string &authParam); int32_t GetFaParam(std::string &pkgName, DmAuthParam &authParam); int32_t SetUserOperation(std::string &pkgName, int32_t action); + int32_t RegisterDecisionFilter(const std::string &pkgName, const std::string &extra); + int32_t UnRegisterDecisionFilter(const std::string &pkgName, const std::string &extra); private: DeviceManagerService() = default; bool intFlag_ = false; diff --git a/services/devicemanagerservice/include/device_manager_service_listener.h b/services/devicemanagerservice/include/device_manager_service_listener.h index e52d9ed73..a1128ca99 100644 --- a/services/devicemanagerservice/include/device_manager_service_listener.h +++ b/services/devicemanagerservice/include/device_manager_service_listener.h @@ -35,6 +35,7 @@ public: int32_t reason); void OnVerifyAuthResult(const std::string &pkgName, const std::string &deviceId, int32_t resultCode, const std::string &flag); + void OnTrustListResult(const std::string &pkgName, const std::vector &deviceList); void OnFaCall(std::string &pkgName, std::string ¶mJson); private: diff --git a/services/devicemanagerservice/include/deviceinfo/dm_device_info_manager.h b/services/devicemanagerservice/include/deviceinfo/dm_device_info_manager.h index 9dfe5b159..7381d2809 100644 --- a/services/devicemanagerservice/include/deviceinfo/dm_device_info_manager.h +++ b/services/devicemanagerservice/include/deviceinfo/dm_device_info_manager.h @@ -22,18 +22,24 @@ #include "dm_adapter_manager.h" #include "dm_device_info.h" #include "softbus_connector.h" +#include "device_manager_service_listener.h" namespace OHOS { namespace DistributedHardware { -class DmDeviceInfoManager { +class DmDeviceInfoManager : public std::enable_shared_from_this { public: - DmDeviceInfoManager(std::shared_ptr &softbusConnectorPtr); + DmDeviceInfoManager(std::shared_ptr &softbusConnectorPtr, + std::shared_ptr &listener); + void OnDecisionResult(const std::string &pkgName, + std::vector &infoList, int32_t callbackId); int32_t GetTrustedDeviceList(const std::string &pkgName, const std::string &extra, - std::vector &deviceList); + std::vector &deviceList); int32_t GetLocalDeviceInfo(DmDeviceInfo &info); private: std::shared_ptr softbusConnector_; + std::shared_ptr listener_; + std::string decisionSoName_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h b/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h index a54ef47ef..367ef3a1c 100644 --- a/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h +++ b/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h @@ -28,12 +28,19 @@ public: DmDeviceStateManager(std::shared_ptr softbusConnector, std::shared_ptr listener); ~DmDeviceStateManager(); + int32_t RegisterProfileListener(const std::string &pkgName, const DmDeviceInfo &info); + int32_t UnRegisterProfileListener(const std::string &pkgName, const DmDeviceInfo &info); + void OnDeviceRealOnline(const std::string &pkgName, const DmDeviceInfo &info); + void OnDeviceRealOffline(const std::string &pkgName, const DmDeviceInfo &info); void OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &info); void OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &info); void OnDeviceChanged(const std::string &pkgName, const DmDeviceInfo &info); void OnDeviceReady(const std::string &pkgName, const DmDeviceInfo &info); void OnProfileReady(const std::string &pkgName, const std::string deviceId); + void OnDecisionResult(const std::string &pkgName, std::vector &infoList, int32_t callbackId); int32_t RegisterSoftbusStateCallback(); + void RegisterDecisionFilter(const std::string &pkgName, const std::string &extra); + void UnRegisterDecisionFilter(const std::string &pkgName, const std::string &extra); private: std::shared_ptr softbusConnector_; @@ -41,7 +48,9 @@ private: std::shared_ptr listener_; std::map deviceStateMap_; std::map remoteDeviceInfos_; + std::map decisionInfos_; std::string profileSoName_; + std::string decisionSoName_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/src/device_manager_service.cpp b/services/devicemanagerservice/src/device_manager_service.cpp index 8a02d7265..104548742 100644 --- a/services/devicemanagerservice/src/device_manager_service.cpp +++ b/services/devicemanagerservice/src/device_manager_service.cpp @@ -70,7 +70,7 @@ int32_t DeviceManagerService::Init() } } if (deviceInfoMgr_ == nullptr) { - deviceInfoMgr_ = std::make_shared(softbusConnector_); + deviceInfoMgr_ = std::make_shared(softbusConnector_, listener_); if (deviceInfoMgr_ == nullptr) { LOGE("Init failed, deviceInfoMgr_ apply for failure"); return DM_MAKE_SHARED_FAIL; @@ -264,5 +264,17 @@ int32_t DeviceManagerService::SetUserOperation(std::string &pkgName, int32_t act authMgr_->OnUserOperation(action); return DM_OK; } + +int32_t DeviceManagerService::RegisterDecisionFilter(const std::string &pkgName, const std::string &extra) +{ + deviceStateMgr_->RegisterDecisionFilter(pkgName, extra); + return DM_OK; +} + +int32_t DeviceManagerService::UnRegisterDecisionFilter(const std::string &pkgName, const std::string &extra) +{ + deviceStateMgr_->UnRegisterDecisionFilter(pkgName, extra); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/src/device_manager_service_listener.cpp b/services/devicemanagerservice/src/device_manager_service_listener.cpp index c67191a3e..a68add56f 100644 --- a/services/devicemanagerservice/src/device_manager_service_listener.cpp +++ b/services/devicemanagerservice/src/device_manager_service_listener.cpp @@ -23,6 +23,7 @@ #include "ipc_notify_device_state_req.h" #include "ipc_notify_discover_result_req.h" #include "ipc_notify_verify_auth_result_req.h" +#include "ipc_notify_trust_list_result_req.h" namespace OHOS { namespace DistributedHardware { @@ -33,9 +34,14 @@ void DeviceManagerServiceListener::OnDeviceStateChange(const std::string &pkgNam std::shared_ptr pReq = std::make_shared(); std::shared_ptr pRsp = std::make_shared(); + pReq->SetPkgName(pkgName); pReq->SetDeviceState(state); pReq->SetDeviceInfo(info); - ipcServerListener_.SendAll(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); + if (pkgName == DM_PKG_NAME) { + ipcServerListener_.SendAll(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); + } else { + ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); + } } void DeviceManagerServiceListener::OnDeviceFound(const std::string &pkgName, uint16_t subscribeId, @@ -103,6 +109,18 @@ void DeviceManagerServiceListener::OnVerifyAuthResult(const std::string &pkgName ipcServerListener_.SendAll(SERVER_VERIFY_AUTH_RESULT, pReq, pRsp); } +void DeviceManagerServiceListener::OnTrustListResult(const std::string &pkgName, + const std::vector &deviceList) +{ + LOGI("DeviceManagerServiceListener::OnTrustListResult List.size=%d", deviceList.size()); + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + + pReq->SetPkgName(pkgName); + pReq->SetDeviceVec(deviceList); + ipcServerListener_.SendRequest(SERVER_TRUST_LIST_RESULT, pReq, pRsp); +} + void DeviceManagerServiceListener::OnFaCall(std::string &pkgName, std::string ¶mJson) { LOGI("OnFaCall in"); diff --git a/services/devicemanagerservice/src/deviceinfo/dm_device_info_manager.cpp b/services/devicemanagerservice/src/deviceinfo/dm_device_info_manager.cpp index dcf240abf..f9bf87cc8 100644 --- a/services/devicemanagerservice/src/deviceinfo/dm_device_info_manager.cpp +++ b/services/devicemanagerservice/src/deviceinfo/dm_device_info_manager.cpp @@ -20,10 +20,18 @@ namespace OHOS { namespace DistributedHardware { -DmDeviceInfoManager::DmDeviceInfoManager(std::shared_ptr &softbusConnectorPtr) - : softbusConnector_(softbusConnectorPtr) +DmDeviceInfoManager::DmDeviceInfoManager(std::shared_ptr &softbusConnectorPtr, + std::shared_ptr &listener) + : softbusConnector_(softbusConnectorPtr), listener_(listener) { LOGI("DmDeviceInfoManager constructor"); + decisionSoName_ = "libdevicemanagerext_decision.z.so"; +} + +void DmDeviceInfoManager::OnDecisionResult(const std::string &pkgName, + std::vector &infoList, int32_t callbackId) +{ + listener_->OnTrustListResult(pkgName, infoList); } int32_t DmDeviceInfoManager::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra, @@ -36,11 +44,13 @@ int32_t DmDeviceInfoManager::GetTrustedDeviceList(const std::string &pkgName, co } if (!extra.empty() && !deviceList.empty()) { - std::string soName; DmAdapterManager &adapterMgrPtr = DmAdapterManager::GetInstance(); - std::shared_ptr decisionAdapter = adapterMgrPtr.GetDecisionAdapter(soName); + std::shared_ptr decisionAdapter = adapterMgrPtr.GetDecisionAdapter(decisionSoName_); if (decisionAdapter != nullptr) { + decisionAdapter->RegisterDecisionListener(pkgName, shared_from_this()); + decisionAdapter->SetDecisionId(pkgName, DECISION_ID_TRUSTLIST); decisionAdapter->FilterDeviceList(deviceList, extra); + deviceList.clear(); // real for ds back } else { LOGE("GetTrustedDeviceList decisionAdapter is nullptr"); } diff --git a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp index 8fc7214ac..fc3abb9dd 100644 --- a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp +++ b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp @@ -26,6 +26,7 @@ DmDeviceStateManager::DmDeviceStateManager(std::shared_ptr sof { LOGI("DmDeviceStateManager constructor"); profileSoName_ = "libdevicemanagerext_profile.z.so"; + decisionSoName_ = "libdevicemanagerext_decision.z.so"; } DmDeviceStateManager::~DmDeviceStateManager() @@ -34,9 +35,8 @@ DmDeviceStateManager::~DmDeviceStateManager() softbusConnector_->UnRegisterSoftbusStateCallback("DM_PKG_NAME"); } -void DmDeviceStateManager::OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &info) +int32_t DmDeviceStateManager::RegisterProfileListener(const std::string &pkgName, const DmDeviceInfo &info) { - LOGI("DmDeviceStateManager::OnDeviceOnline in"); DmAdapterManager &adapterMgrPtr = DmAdapterManager::GetInstance(); std::shared_ptr profileAdapter = adapterMgrPtr.GetProfileAdapter(profileSoName_); if (profileAdapter == nullptr) { @@ -58,13 +58,10 @@ void DmDeviceStateManager::OnDeviceOnline(const std::string &pkgName, const DmDe LOGI("RegisterProfileListener out"); } } - DmDeviceState state = DEVICE_STATE_ONLINE; - deviceStateMap_[info.deviceId] = DEVICE_STATE_ONLINE; - listener_->OnDeviceStateChange(pkgName, state, info); - LOGI("DmDeviceStateManager::OnDeviceOnline out"); + return DM_OK; } -void DmDeviceStateManager::OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &info) +int32_t DmDeviceStateManager::UnRegisterProfileListener(const std::string &pkgName, const DmDeviceInfo &info) { DmAdapterManager &adapterMgrPtr = DmAdapterManager::GetInstance(); std::shared_ptr profileAdapter = adapterMgrPtr.GetProfileAdapter(profileSoName_); @@ -79,9 +76,83 @@ void DmDeviceStateManager::OnDeviceOffline(const std::string &pkgName, const DmD remoteDeviceInfos_.erase(std::string(info.deviceId)); } } + return DM_OK; +} + +void DmDeviceStateManager::OnDeviceRealOnline(const std::string &pkgName, const DmDeviceInfo &info) +{ + LOGI("DmDeviceStateManager::OnDeviceRealOnline in"); + DmDeviceState state = DEVICE_STATE_ONLINE; + deviceStateMap_[info.deviceId] = DEVICE_STATE_ONLINE; + listener_->OnDeviceStateChange(pkgName, state, info); + LOGI("DmDeviceStateManager::OnDeviceRealOnline out"); +} + +void DmDeviceStateManager::OnDeviceRealOffline(const std::string &pkgName, const DmDeviceInfo &info) +{ + LOGI("DmDeviceStateManager::OnDeviceRealOffline in"); DmDeviceState state = DEVICE_STATE_OFFLINE; deviceStateMap_[info.deviceId] = DEVICE_STATE_OFFLINE; listener_->OnDeviceStateChange(pkgName, state, info); + UnRegisterProfileListener(pkgName, info); + LOGI("DmDeviceStateManager::OnDeviceRealOffline out"); +} + +void DmDeviceStateManager::OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &info) +{ + LOGI("DmDeviceStateManager::OnDeviceOnline in"); + RegisterProfileListener(pkgName, info); + + DmAdapterManager &adapterMgrPtr = DmAdapterManager::GetInstance(); + std::shared_ptr decisionAdapter = adapterMgrPtr.GetDecisionAdapter(decisionSoName_); + if (decisionAdapter == nullptr) { + LOGE("OnDeviceOnline decision adapter is null"); + OnDeviceRealOnline(pkgName, info); + } else if (decisionInfos_.size() == 0) { + OnDeviceRealOnline(pkgName, info); + } else { + std::string extra; + std::vector infoList; + LOGI("OnDeviceOnline decision decisionInfos_ size: %d", decisionInfos_.size()); + for (auto iter : decisionInfos_) { + std::string listenerPkgName = iter.first; + std::string extra = iter.second; + infoList.clear(); + infoList.push_back(info); + decisionAdapter->SetDecisionId(listenerPkgName, DECISION_ID_ONLINE); + decisionAdapter->FilterDeviceList(infoList, extra); + } + } + + LOGI("DmDeviceStateManager::OnDeviceOnline out"); +} + +void DmDeviceStateManager::OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &info) +{ + LOGI("DmDeviceStateManager::OnDeviceOffline in"); + UnRegisterProfileListener(pkgName, info); + + DmAdapterManager &adapterMgrPtr = DmAdapterManager::GetInstance(); + std::shared_ptr decisionAdapter = adapterMgrPtr.GetDecisionAdapter(decisionSoName_); + if (decisionAdapter == nullptr) { + LOGE("OnDeviceOnline decision adapter is null"); + OnDeviceRealOffline(pkgName, info); + } else if (decisionInfos_.size() == 0) { + OnDeviceRealOffline(pkgName, info); + } else { + std::string extra; + std::vector infoList; + LOGI("OnDeviceOnline decision decisionInfos_ size: %d", decisionInfos_.size()); + for (auto iter : decisionInfos_) { + std::string listenerPkgName = iter.first; + std::string extra = iter.second; + infoList.clear(); + infoList.push_back(info); + decisionAdapter->SetDecisionId(listenerPkgName, DECISION_ID_OFFLINE); + decisionAdapter->FilterDeviceList(infoList, extra); + } + } + LOGI("DmDeviceStateManager::OnDeviceOffline out"); } void DmDeviceStateManager::OnDeviceChanged(const std::string &pkgName, const DmDeviceInfo &info) @@ -107,11 +178,61 @@ void DmDeviceStateManager::OnProfileReady(const std::string &pkgName, const std: listener_->OnDeviceStateChange(pkgName, state, saveInfo); } +void DmDeviceStateManager::OnDecisionResult(const std::string &pkgName, + std::vector &infoList, int32_t callbackId) +{ + LOGI("DmDeviceStateManager::OnDecisionResult"); + if (pkgName == "") { + LOGE("pkgName is null"); + return; + } + if (infoList.size() == 1) { + if (callbackId == DECISION_ID_ONLINE) { + OnDeviceRealOnline(pkgName, infoList.at(0)); + } else if (callbackId == DECISION_ID_ONLINE) { + OnDeviceRealOffline(pkgName, infoList.at(0)); + } + } +} + int32_t DmDeviceStateManager::RegisterSoftbusStateCallback() { softbusConnector_->RegisterSoftbusStateCallback(DM_PKG_NAME, std::shared_ptr(shared_from_this())); return DM_OK; } + +void DmDeviceStateManager::RegisterDecisionFilter(const std::string &pkgName, const std::string &extra) +{ + LOGI("DmDeviceStateManager::RegisterDecisionFilter pkgName=%s, extra=%s", pkgName.c_str(), extra.c_str()); + if (pkgName != "") { + decisionInfos_[pkgName] = extra; + DmAdapterManager &adapterMgrPtr = DmAdapterManager::GetInstance(); + std::shared_ptr decisionAdapter = adapterMgrPtr.GetDecisionAdapter(decisionSoName_); + if (decisionAdapter == nullptr) { + LOGE("RegisterDecisionFilter decision adapter is null"); + } else { + decisionAdapter->RegisterDecisionListener(pkgName, + std::shared_ptr(shared_from_this())); + } + } +} + +void DmDeviceStateManager::UnRegisterDecisionFilter(const std::string &pkgName, const std::string &extra) +{ + LOGI("DmDeviceStateManager::UnRegisterDecisionFilter pkgName=%s, extra=%s", pkgName.c_str(), extra.c_str()); + auto iter = decisionInfos_.find(pkgName); + if (iter == decisionInfos_.end()) { + } else { + decisionInfos_.erase(pkgName); + DmAdapterManager &adapterMgrPtr = DmAdapterManager::GetInstance(); + std::shared_ptr decisionAdapter = adapterMgrPtr.GetDecisionAdapter(decisionSoName_); + if (decisionAdapter == nullptr) { + LOGE("UnRegisterDecisionFilter decision adapter is null"); + } else { + decisionAdapter->UnRegisterDecisionListener(pkgName); + } + } +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp index 2a61266b5..3235b79f1 100644 --- a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp @@ -27,6 +27,7 @@ #include "ipc_notify_device_state_req.h" #include "ipc_notify_discover_result_req.h" #include "ipc_notify_verify_auth_result_req.h" +#include "ipc_notify_trust_list_result_req.h" #include "ipc_server_stub.h" namespace OHOS { @@ -182,6 +183,43 @@ ON_IPC_READ_RESPONSE(SERVER_VERIFY_AUTH_RESULT, MessageParcel &reply, std::share return DM_OK; } +ON_IPC_SET_REQUEST(SERVER_TRUST_LIST_RESULT, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = + std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + std::vector deviceList; + deviceList = pReq->GetDeviceVec(); + int32_t infoNum = deviceList.size(); + DmDeviceInfo deviceInfo; + if (!data.WriteString(pkgName)) { + LOGE("write pkgName failed"); + return DM_FLATTEN_OBJECT; + } + if (!data.WriteInt32(infoNum)) { + LOGE("write infoNum failed"); + return DM_WRITE_FAILED; + } + if (!deviceList.empty()) { + for (; !deviceList.empty();) { + deviceInfo = deviceList.back(); + deviceList.pop_back(); + + if (!data.WriteRawData(&deviceInfo, sizeof(DmDeviceInfo))) { + LOGE("write subscribeInfo failed"); + return DM_WRITE_FAILED; + } + } + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(SERVER_TRUST_LIST_RESULT, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + ON_IPC_SET_REQUEST(SERVER_DEVICE_FA_NOTIFY, std::shared_ptr pBaseReq, MessageParcel &data) { std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); @@ -431,5 +469,29 @@ ON_IPC_CMD(SERVER_USER_AUTH_OPERATION, MessageParcel &data, MessageParcel &reply } return result; } + +ON_IPC_CMD(REGISTER_DECISION_FILTER, MessageParcel &data, MessageParcel &reply) +{ + std::string packageName = data.ReadString(); + std::string extra = data.ReadString(); + int result = DeviceManagerService::GetInstance().RegisterDecisionFilter(packageName, extra); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return DM_WRITE_FAILED; + } + return result; +} + +ON_IPC_CMD(UNREGISTER_DECISION_FILTER, MessageParcel &data, MessageParcel &reply) +{ + std::string packageName = data.ReadString(); + std::string extra = data.ReadString(); + int result = DeviceManagerService::GetInstance().UnRegisterDecisionFilter(packageName, extra); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return DM_WRITE_FAILED; + } + return result; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index fd0d387c7..ce600b21d 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -28,6 +28,7 @@ group("unittest") { ":UTTest_dm_device_info_manager", ":UTTest_dm_device_state_manager", ":UTTest_dm_discovery_manager", + ":UTTest_dm_ext_decision", ":UTTest_hichain_connector", ":UTTest_softbus_connector", ":UTTest_softbus_session", @@ -45,6 +46,17 @@ ohos_unittest("device_manager_impl_test") { ## UnitTest device_manager_impl_test }}} +## UnitTest UTTest_dm_ext_decision {{{ +ohos_unittest("UTTest_dm_ext_decision") { + module_out_path = module_out_path + include_dirs = [] + sources = [ "UTTest_dm_ext_decision.cpp" ] + + deps = [ ":device_manager_test_common" ] +} + +## UTTest_dm_ext_decision }}} + ## UnitTest UTTest_device_manager_service {{{ ohos_unittest("UTTest_device_manager_service") { module_out_path = module_out_path diff --git a/test/unittest/UTTest_dm_device_info_manager.cpp b/test/unittest/UTTest_dm_device_info_manager.cpp index 5dedde908..3e67716ad 100644 --- a/test/unittest/UTTest_dm_device_info_manager.cpp +++ b/test/unittest/UTTest_dm_device_info_manager.cpp @@ -39,7 +39,8 @@ void DeviceManagerImplTest::TearDownTestCase() namespace { std::shared_ptr softbusConnector = std::make_shared(); std::shared_ptr listener_ = std::make_shared(); -std::shared_ptr dmDeviceStateManager = std::make_shared(softbusConnector); +std::shared_ptr dmDeviceStateManager = std::make_shared(softbusConnector, + listener_); /** * @tc.name: DmDeviceInfoManager_001 @@ -51,7 +52,7 @@ std::shared_ptr dmDeviceStateManager = std::make_shared p = std::make_shared(softbusConnector); + std::shared_ptr p = std::make_shared(softbusConnector, listener_); ASSERT_NE(p, nullptr); } } // namespace diff --git a/test/unittest/UTTest_dm_ext_decision.cpp b/test/unittest/UTTest_dm_ext_decision.cpp new file mode 100644 index 000000000..d8a463d7e --- /dev/null +++ b/test/unittest/UTTest_dm_ext_decision.cpp @@ -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. + */ + +#include "UTTest_dm_ext_decision.h" + +#include "dm_constants.h" +#include "dm_log.h" + +namespace OHOS { +namespace DistributedHardware { +void ExtDecisionTest::SetUp() +{ +} + +void ExtDecisionTest::TearDown() +{ +} + +void ExtDecisionTest::SetUpTestCase() +{ +} + +void ExtDecisionTest::TearDownTestCase() +{ +} + +namespace { +/** + * @tc.name: DmDeviceInfoManager_001 + * @tc.desc: Returns a new pointer to the constructor DmDeviceInfoManager new + * to construct an environment where the device has been discovered, and stop discovering + * the device. Its return value is DM_INPUT_PARA_EMPTY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(ExtDecisionTest, ExtDecision_001, testing::ext::TestSize.Level0) +{ + std::cout<<"hello_test"< +#include + +#include +#include + +namespace OHOS { +namespace DistributedHardware { +class ExtDecisionTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file -- Gitee