From 69bb79489306a110cff3de4e4e5f89486ce82d48 Mon Sep 17 00:00:00 2001 From: xuxiaoqing Date: Thu, 15 Jun 2023 03:23:00 +0000 Subject: [PATCH] fix: eliminate circular dependencies Signed-off-by: xuxiaoqing --- bundle.json | 8 +- services/BUILD.gn | 15 +- .../bus_extension/core/bus_extension_core.h | 50 +++++++ .../device_manager/dev_change_callback.h | 32 +++++ .../include/device_manager/etx_device_mgr.h | 8 +- .../drivers_pkg_manager/driver_pkg_manager.h | 1 + .../src/bus_extension/BUILD.gn | 3 +- .../src/bus_extension/bus_extension.cpp | 1 + .../src/bus_extension/core/BUILD.gn | 30 ++++ .../bus_extension/core/bus_extension_core.cpp | 107 ++++++++++++++ .../src/device_manager/BUILD.gn | 27 +++- .../device_manager/dev_change_callback.cpp | 35 +++++ .../src/device_manager/etx_device_mgr.cpp | 131 ++++-------------- .../src/driver_etx_mgr.cpp | 9 +- .../driver_pkg_manager.cpp | 3 +- .../drv_bundle_state_callback.cpp | 3 +- test/moduletest/BUILD.gn | 7 +- .../bus_extension_core_mt.cpp | 14 +- test/unittest/device_manager_test/BUILD.gn | 13 +- .../device_manager_test.cpp | 11 +- utils/include/ext_object.h | 49 +------ utils/include/ibus_extension.h | 1 + utils/include/idev_change_callback.h | 31 +++++ 23 files changed, 400 insertions(+), 189 deletions(-) create mode 100644 services/native/driver_extension_manager/include/bus_extension/core/bus_extension_core.h create mode 100644 services/native/driver_extension_manager/include/device_manager/dev_change_callback.h create mode 100644 services/native/driver_extension_manager/src/bus_extension/core/BUILD.gn create mode 100644 services/native/driver_extension_manager/src/bus_extension/core/bus_extension_core.cpp create mode 100644 services/native/driver_extension_manager/src/device_manager/dev_change_callback.cpp create mode 100644 utils/include/idev_change_callback.h diff --git a/bundle.json b/bundle.json index 8cd07ca..4f37752 100644 --- a/bundle.json +++ b/bundle.json @@ -30,7 +30,8 @@ "init", "ipc", "samgr", - "selinux" + "selinux", + "bundle_framework" ], "third_party": [ "bounds_checking_function", @@ -45,7 +46,10 @@ "//drivers/external_device_manager/services:driver_extension_manager", "//drivers/external_device_manager/services/native/driver_extension_manager/src/bus_extension:bus_extension", "//drivers/external_device_manager/services/native/driver_extension_manager/src/driver_controller:driver_extension_controller", - "//drivers/external_device_manager/services:drivers_pkg_manager" + "//drivers/external_device_manager/services:drivers_pkg_manager", + "//drivers/external_device_manager/services/native/driver_extension_manager/src/device_manager:driver_extension_dev_callback", + "//drivers/external_device_manager/services/native/driver_extension_manager/src/device_manager:driver_extension_device_manager", + "//drivers/external_device_manager/services/native/driver_extension_manager/src/bus_extension/core:driver_extension_bus_core" ], "inner_kits": [], "test": [ diff --git a/services/BUILD.gn b/services/BUILD.gn index 0acbcc1..58bc3bf 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -26,12 +26,14 @@ ohos_shared_library("driver_extension_manager") { "${ext_mgr_path}/services/zidl/include/", "${ext_mgr_path}/services/native/driver_extension_manager/include/drivers_pkg_manager", "${ext_mgr_path}/services/native/driver_extension_manager/include/device_manager", + "${ext_mgr_path}/services/native/driver_extension_manager/include/bus_extension/core", ] configs = [ "${utils_path}:utils_config" ] deps = [ ":drivers_pkg_manager", - "${ext_mgr_path}/services/native/driver_extension_manager/src/bus_extension:bus_extension", - "${ext_mgr_path}/services/native/driver_extension_manager/src/device_manager:bus_extension_core", + "${ext_mgr_path}/services/native/driver_extension_manager/src/bus_extension/core:driver_extension_bus_core", + "${ext_mgr_path}/services/native/driver_extension_manager/src/device_manager:driver_extension_dev_callback", + "${ext_mgr_path}/services/native/driver_extension_manager/src/device_manager:driver_extension_device_manager", ] external_deps = [ "ability_runtime:ability_manager", @@ -61,8 +63,10 @@ ohos_shared_library("drivers_pkg_manager") { "native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp", "native/driver_extension_manager/src/drivers_pkg_manager/drv_bundle_state_callback.cpp", ] - include_dirs = - [ "native/driver_extension_manager/include/drivers_pkg_manager" ] + include_dirs = [ + "native/driver_extension_manager/include/drivers_pkg_manager", + "native/driver_extension_manager/include/bus_extension/core", + ] configs = [ ":drvext_manager_public_config", ":drvext_manager_private_config", @@ -70,7 +74,8 @@ ohos_shared_library("drivers_pkg_manager") { ] public_configs = [ ":drvext_manager_public_config" ] deps = [ - "native/driver_extension_manager/src/bus_extension:bus_extension", + "${ext_mgr_path}/services/native/driver_extension_manager/src/bus_extension:bus_extension", + "${ext_mgr_path}/services/native/driver_extension_manager/src/bus_extension/core:driver_extension_bus_core", "//third_party/jsoncpp:jsoncpp", ] # Component internal dependencies diff --git a/services/native/driver_extension_manager/include/bus_extension/core/bus_extension_core.h b/services/native/driver_extension_manager/include/bus_extension/core/bus_extension_core.h new file mode 100644 index 0000000..0eaac2d --- /dev/null +++ b/services/native/driver_extension_manager/include/bus_extension/core/bus_extension_core.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 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 BUS_EXTENSION_CORE_H +#define BUS_EXTENSION_CORE_H + +#include +#include +#include "ext_object.h" +#include "idev_change_callback.h" +#include "single_instance.h" +#include "ibus_extension.h" + +namespace OHOS { +namespace ExternalDeviceManager { +class BusExtensionCore { + DECLARE_SINGLE_INSTANCE_BASE(BusExtensionCore); + +public: + ~BusExtensionCore() = default; + int32_t Init(std::shared_ptr callback); + int32_t Register(BusType busType, std::shared_ptr busExtension); + std::shared_ptr GetBusExtensionByName(std::string busName); + +private: + BusExtensionCore() = default; + std::unordered_map> busExtensions_; + const uint32_t MAX_BUS_EXTENSIONS = 100; +}; + +// bus extension should register by __attribute__ ((constructor)) when loading so +template +void RegisterBusExtension(BusType busType) +{ + BusExtensionCore::GetInstance().Register(busType, std::make_shared()); +} +} // namespace ExternalDeviceManager +} // namespace OHOS +#endif // BUS_EXTENSION_CORE_H \ No newline at end of file diff --git a/services/native/driver_extension_manager/include/device_manager/dev_change_callback.h b/services/native/driver_extension_manager/include/device_manager/dev_change_callback.h new file mode 100644 index 0000000..c57443a --- /dev/null +++ b/services/native/driver_extension_manager/include/device_manager/dev_change_callback.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 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 DEV_CHANGE_CALLBACK_H +#define DEV_CHANGE_CALLBACK_H + +#include +#include "ext_object.h" +#include "idev_change_callback.h" + +namespace OHOS { +namespace ExternalDeviceManager { +class DevChangeCallback final : public IDevChangeCallback { +public: + DevChangeCallback() = default; + int32_t OnDeviceAdd(std::shared_ptr device) override; + int32_t OnDeviceRemove(std::shared_ptr device) override; +}; +} // namespace ExternalDeviceManager +} // namespace OHOS +#endif // DEV_CHANGE_CALLBACK_H \ No newline at end of file diff --git a/services/native/driver_extension_manager/include/device_manager/etx_device_mgr.h b/services/native/driver_extension_manager/include/device_manager/etx_device_mgr.h index c40fad5..d13ad42 100644 --- a/services/native/driver_extension_manager/include/device_manager/etx_device_mgr.h +++ b/services/native/driver_extension_manager/include/device_manager/etx_device_mgr.h @@ -18,8 +18,9 @@ #include #include #include - #include "ext_object.h" +#include "single_instance.h" + namespace OHOS { namespace ExternalDeviceManager { class Device final { @@ -43,11 +44,12 @@ public: ~ExtDeviceManager() = default; int32_t Init(); int32_t RegisterDevice(std::shared_ptr devInfo); - void UnRegisterDevice(const std::shared_ptr devInfo); + int32_t UnRegisterDevice(const std::shared_ptr devInfo); + std::vector> QueryDeivce(const BusType busType); private: ExtDeviceManager() = default; - std::unordered_map>> deviceMap_; + std::unordered_map>> deviceMap_; std::mutex deviceMapMutex_; }; } // namespace ExternalDeviceManager diff --git a/services/native/driver_extension_manager/include/drivers_pkg_manager/driver_pkg_manager.h b/services/native/driver_extension_manager/include/drivers_pkg_manager/driver_pkg_manager.h index 1267d30..2af18fc 100644 --- a/services/native/driver_extension_manager/include/drivers_pkg_manager/driver_pkg_manager.h +++ b/services/native/driver_extension_manager/include/drivers_pkg_manager/driver_pkg_manager.h @@ -23,6 +23,7 @@ #include "drv_bundle_state_callback.h" #include "ibus_extension.h" #include "single_instance.h" +#include "ext_object.h" namespace OHOS { namespace ExternalDeviceManager { using namespace std; diff --git a/services/native/driver_extension_manager/src/bus_extension/BUILD.gn b/services/native/driver_extension_manager/src/bus_extension/BUILD.gn index 603e096..2604153 100644 --- a/services/native/driver_extension_manager/src/bus_extension/BUILD.gn +++ b/services/native/driver_extension_manager/src/bus_extension/BUILD.gn @@ -22,13 +22,14 @@ ohos_shared_library("bus_extension") { include_dirs = [ "//third_party/jsoncpp/include/json", "../../include/bus_extension/usb", + "../../include/bus_extension/core", ] configs = [ "${utils_path}:coverage_flags", "${utils_path}:utils_config", ] deps = [ - "${ext_mgr_path}/services/native/driver_extension_manager/src/device_manager:bus_extension_core", + "core:driver_extension_bus_core", "//third_party/jsoncpp:jsoncpp", ] external_deps = [ diff --git a/services/native/driver_extension_manager/src/bus_extension/bus_extension.cpp b/services/native/driver_extension_manager/src/bus_extension/bus_extension.cpp index 95abee2..325021d 100644 --- a/services/native/driver_extension_manager/src/bus_extension/bus_extension.cpp +++ b/services/native/driver_extension_manager/src/bus_extension/bus_extension.cpp @@ -17,6 +17,7 @@ #include "hilog_wrapper.h" #include "ibus_extension.h" #include "usb_bus_extension.h" +#include "bus_extension_core.h" namespace OHOS { namespace ExternalDeviceManager { shared_ptr IBusExtension::GetInstance(const string &busType) diff --git a/services/native/driver_extension_manager/src/bus_extension/core/BUILD.gn b/services/native/driver_extension_manager/src/bus_extension/core/BUILD.gn new file mode 100644 index 0000000..1f3d4f7 --- /dev/null +++ b/services/native/driver_extension_manager/src/bus_extension/core/BUILD.gn @@ -0,0 +1,30 @@ +# Copyright (c) 2023 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. + +import("../../../../../../extdevmgr.gni") + +ohos_shared_library("driver_extension_bus_core") { + install_enable = true + sources = [ "bus_extension_core.cpp" ] + + include_dirs = [ "${ext_mgr_path}/services/native/driver_extension_manager/include/bus_extension/core" ] + configs = [ "${utils_path}:utils_config" ] + external_deps = [ + "bundle_framework:appexecfwk_base", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] + + subsystem_name = "hdf" + part_name = "external_device_manager" +} diff --git a/services/native/driver_extension_manager/src/bus_extension/core/bus_extension_core.cpp b/services/native/driver_extension_manager/src/bus_extension/core/bus_extension_core.cpp new file mode 100644 index 0000000..07fb800 --- /dev/null +++ b/services/native/driver_extension_manager/src/bus_extension/core/bus_extension_core.cpp @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2023 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 +#include + +#include "edm_errors.h" +#include "hilog_wrapper.h" +#include "string_ex.h" +#include "bus_extension_core.h" + +#ifdef __aarch64__ +static constexpr const char *BUS_EXTENSION_SO_PATH = "/system/lib64"; +#else +static constexpr const char *BUS_EXTENSION_SO_PATH = "/system/lib"; +#endif +static constexpr const char *HDI_SO_SUFFIX = ".z.so"; +static constexpr const char *HDI_SO_PREFIX = "lib"; +static constexpr const char *USB_BUS_EXTENSION = "bus_extension"; + +namespace OHOS { +namespace ExternalDeviceManager { +IMPLEMENT_SINGLE_INSTANCE(BusExtensionCore); + +static void LoadLib() +{ + for (BusType i = BUS_TYPE_USB; i < BUS_TYPE_MAX; i = (BusType)(i + 1)) { + std::ostringstream libPath; + libPath << BUS_EXTENSION_SO_PATH << "/" << HDI_SO_PREFIX; + switch (i) { + case BUS_TYPE_USB: + libPath << USB_BUS_EXTENSION; + break; + default: + EDM_LOGE(MODULE_DEV_MGR, "invalid bus type"); + continue; + } + libPath << HDI_SO_SUFFIX; + void *handler = dlopen(libPath.str().c_str(), RTLD_LAZY); + if (handler == nullptr) { + EDM_LOGE(MODULE_DEV_MGR, "failed to dlopen %{public}s, %{public}s", libPath.str().c_str(), dlerror()); + continue; + } + } +} + +int32_t BusExtensionCore::Init(std::shared_ptr callback) +{ + LoadLib(); + int ret = EDM_OK; + for (auto &iter : busExtensions_) { + if (iter.second->SetDevChangeCallback(callback) != EDM_OK) { + ret = EDM_NOK; + EDM_LOGE(MODULE_DEV_MGR, "busExtension init failed, busType is %{public}d", iter.first); + } + EDM_LOGD(MODULE_DEV_MGR, "busExtension init successfully, busType is %{public}d", iter.first); + } + return ret; +} + +int32_t BusExtensionCore::Register(BusType busType, std::shared_ptr busExtension) +{ + if (busExtensions_.size() == MAX_BUS_EXTENSIONS) { + EDM_LOGE(MODULE_DEV_MGR, "the maximum number of busextension registrations has been reached"); + return EDM_NOK; + } + if (busExtensions_.find(busType) != busExtensions_.end()) { + EDM_LOGI(MODULE_DEV_MGR, "busType %{public}d has been registered", busType); + return EDM_OK; + } + busExtensions_.emplace(busType, busExtension); + EDM_LOGD(MODULE_DEV_MGR, "busType %{public}d register successfully", busType); + return EDM_OK; +} + +std::shared_ptr BusExtensionCore::GetBusExtensionByName(std::string busName) +{ + static std::unordered_map busTypeMap = { + {"bus", BusType::BUS_TYPE_USB} + }; + auto iterMap = busTypeMap.find(busName); + if (iterMap == busTypeMap.end()) { + EDM_LOGE(MODULE_DEV_MGR, "invalid bus name"); + return nullptr; + } + BusType busType = iterMap->second; + auto iterExtension = busExtensions_.find(busType); + if (iterExtension == busExtensions_.end()) { + EDM_LOGE(MODULE_DEV_MGR, "%{public}s bus extension not found", busName.c_str()); + return nullptr; + } + return busExtensions_[busType]; +} +} // namespace ExternalDeviceManager +} // namespace OHOS \ No newline at end of file diff --git a/services/native/driver_extension_manager/src/device_manager/BUILD.gn b/services/native/driver_extension_manager/src/device_manager/BUILD.gn index e9d87fa..786efab 100644 --- a/services/native/driver_extension_manager/src/device_manager/BUILD.gn +++ b/services/native/driver_extension_manager/src/device_manager/BUILD.gn @@ -13,16 +13,37 @@ import("../../../../../extdevmgr.gni") -ohos_shared_library("bus_extension_core") { +ohos_shared_library("driver_extension_dev_callback") { install_enable = true - sources = [ "etx_device_mgr.cpp" ] + sources = [ "dev_change_callback.cpp" ] include_dirs = [ "${ext_mgr_path}/services/native/driver_extension_manager/include/device_manager" ] configs = [ "${utils_path}:utils_config" ] + deps = [ ":driver_extension_device_manager" ] + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] + + subsystem_name = "hdf" + part_name = "external_device_manager" +} + +ohos_shared_library("driver_extension_device_manager") { + install_enable = true + sources = [ "etx_device_mgr.cpp" ] + + include_dirs = [ + "${ext_mgr_path}/services/native/driver_extension_manager/include/device_manager", + "${ext_mgr_path}/services/native/driver_extension_manager/include/drivers_pkg_manager", + ] + configs = [ "${utils_path}:utils_config" ] + deps = [ "${ext_mgr_path}/services:drivers_pkg_manager" ] external_deps = [ - "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", ] subsystem_name = "hdf" diff --git a/services/native/driver_extension_manager/src/device_manager/dev_change_callback.cpp b/services/native/driver_extension_manager/src/device_manager/dev_change_callback.cpp new file mode 100644 index 0000000..755ffe6 --- /dev/null +++ b/services/native/driver_extension_manager/src/device_manager/dev_change_callback.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 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 "dev_change_callback.h" +#include "edm_errors.h" +#include "etx_device_mgr.h" +#include "hilog_wrapper.h" + +namespace OHOS { +namespace ExternalDeviceManager { +int32_t DevChangeCallback::OnDeviceAdd(std::shared_ptr device) +{ + EDM_LOGD(MODULE_DEV_MGR, "OnDeviceAdd start"); + return ExtDeviceManager::GetInstance().RegisterDevice(device); +} + +int32_t DevChangeCallback::OnDeviceRemove(std::shared_ptr device) +{ + EDM_LOGD(MODULE_DEV_MGR, "OnDeviceRemove start"); + return ExtDeviceManager::GetInstance().UnRegisterDevice(device); +} +} // namespace ExternalDeviceManager +} // namespace OHOS \ No newline at end of file diff --git a/services/native/driver_extension_manager/src/device_manager/etx_device_mgr.cpp b/services/native/driver_extension_manager/src/device_manager/etx_device_mgr.cpp index 4cb137e..b11285a 100644 --- a/services/native/driver_extension_manager/src/device_manager/etx_device_mgr.cpp +++ b/services/native/driver_extension_manager/src/device_manager/etx_device_mgr.cpp @@ -13,82 +13,15 @@ * limitations under the License. */ -#include -#include - +#include "etx_device_mgr.h" #include "cinttypes" +#include "driver_pkg_manager.h" #include "edm_errors.h" #include "hilog_wrapper.h" -#include "ibus_extension.h" -#include "etx_device_mgr.h" - -#ifdef __aarch64__ -static constexpr const char *BUS_EXTENSION_SO_PATH = "/system/lib64"; -#else -static constexpr const char *BUS_EXTENSION_SO_PATH = "/system/lib"; -#endif -static constexpr const char *HDI_SO_SUFFIX = ".z.so"; -static constexpr const char *HDI_SO_PREFIX = "lib"; -static constexpr const char *USB_BUS_EXTENSION = "bus_extension"; - namespace OHOS { namespace ExternalDeviceManager { -IMPLEMENT_SINGLE_INSTANCE(BusExtensionCore); IMPLEMENT_SINGLE_INSTANCE(ExtDeviceManager); -static void LoadLib() -{ - for (BusType i = BUS_TYPE_USB; i < BUS_TYPE_MAX; i = (BusType)(i + 1)) { - std::ostringstream libPath; - libPath << BUS_EXTENSION_SO_PATH << "/" << HDI_SO_PREFIX; - switch (i) { - case BUS_TYPE_USB: - libPath << USB_BUS_EXTENSION; - break; - default: - EDM_LOGE(MODULE_DEV_MGR, "invalid bus type"); - continue; - } - libPath << HDI_SO_SUFFIX; - void *handler = dlopen(libPath.str().c_str(), RTLD_LAZY); - if (handler == nullptr) { - EDM_LOGE(MODULE_DEV_MGR, "failed to dlopen %{public}s, %{public}s", libPath.str().c_str(), dlerror()); - continue; - } - } -} - -int32_t BusExtensionCore::Init() -{ - LoadLib(); - int ret = EDM_OK; - for (auto &iter : busExtensions_) { - std::shared_ptr callback = - std::make_shared(iter.first, ExtDeviceManager::GetInstance()); - if (iter.second->SetDevChangeCallback(callback) != EDM_OK) { - ret = EDM_NOK; - EDM_LOGE(MODULE_DEV_MGR, "busExtension init failed, busType is %{public}d", iter.first); - } - EDM_LOGD(MODULE_DEV_MGR, "busExtension init successfully, busType is %{public}d", iter.first); - } - return ret; -} - -int32_t BusExtensionCore::Register(BusType busType, std::shared_ptr busExtension) -{ - if (busExtensions_.size() == MAX_BUS_EXTENSIONS) { - EDM_LOGE(MODULE_DEV_MGR, "the maximum number of busextension registrations has been reached"); - return EDM_NOK; - } - if (busExtensions_.count(busType) > 0) { - EDM_LOGI(MODULE_DEV_MGR, "busType %{public}d has been registered", busType); - return EDM_OK; - } - busExtensions_.insert(std::make_pair(busType, busExtension)); - EDM_LOGD(MODULE_DEV_MGR, "busType %{public}d register successfully", busType); - return EDM_OK; -} - int32_t ExtDeviceManager::Init() { EDM_LOGD(MODULE_DEV_MGR, "ExtDeviceManager Init start"); @@ -100,58 +33,48 @@ int32_t ExtDeviceManager::RegisterDevice(std::shared_ptr devInfo) BusType type = devInfo->GetBusType(); uint64_t deviceId = devInfo->GetDeviceId(); std::lock_guard lock(deviceMapMutex_); - if (deviceMap_.count(type) > 0) { - std::list> &list = deviceMap_[type]; - std::list>::iterator iter; - for (iter = list.begin(); iter != list.end(); iter++) { - std::shared_ptr device = *iter; - if (device->GetDeviceInfo()->GetDeviceId() == deviceId) { - EDM_LOGI(MODULE_DEV_MGR, "device has been registered, deviceId is %{public}016" PRIx64 "", deviceId); - return EDM_OK; - } + if (deviceMap_.find(type) != deviceMap_.end()) { + std::unordered_map> &map = deviceMap_[type]; + if (map.find(deviceId) != map.end()) { + EDM_LOGI(MODULE_DEV_MGR, "device has been registered, deviceId is %{public}016" PRIx64 "", deviceId); + return EDM_OK; } } std::shared_ptr device = std::make_shared(devInfo); - deviceMap_[type].push_back(device); + deviceMap_[type].emplace(deviceId, device); EDM_LOGD(MODULE_DEV_MGR, "successfully registered device, deviceId is %{public}016" PRIx64 "", deviceId); - // driver match + DriverPkgManager::GetInstance(); return EDM_OK; } -void ExtDeviceManager::UnRegisterDevice(const std::shared_ptr devInfo) +int32_t ExtDeviceManager::UnRegisterDevice(const std::shared_ptr devInfo) { BusType type = devInfo->GetBusType(); uint64_t deviceId = devInfo->GetDeviceId(); std::lock_guard lock(deviceMapMutex_); - if (deviceMap_.count(type) > 0) { - std::list> &list = deviceMap_[type]; - std::list>::iterator iter; - for (iter = list.begin(); iter != list.end(); iter++) { - std::shared_ptr device = *iter; - if (device->GetDeviceInfo()->GetDeviceId() == deviceId) { - iter = list.erase(iter); - EDM_LOGI( - MODULE_DEV_MGR, "successfully unregistered device, deviceId is %{public}016" PRIx64 "", deviceId); - return; - } + if (deviceMap_.find(type) != deviceMap_.end()) { + std::unordered_map> &map = deviceMap_[type]; + if (map.find(deviceId) != map.end()) { + map.erase(deviceId); + EDM_LOGI(MODULE_DEV_MGR, "successfully unregistered device, deviceId is %{public}016" PRIx64 "", deviceId); + return EDM_OK; } } EDM_LOGD(MODULE_DEV_MGR, "device has been unregistered, deviceId is %{public}016" PRIx64 "", deviceId); + return EDM_OK; } -int32_t DevChangeCallback::OnDeviceAdd(std::shared_ptr device) -{ - EDM_LOGD(MODULE_DEV_MGR, "OnDeviceAdd start"); - device->devInfo_.devBusInfo.busType = this->busType_; - return this->extDevMgr_.RegisterDevice(device); -} - -int32_t DevChangeCallback::OnDeviceRemove(std::shared_ptr device) +std::vector> ExtDeviceManager::QueryDeivce(const BusType busType) { - EDM_LOGD(MODULE_DEV_MGR, "OnDeviceRemove start"); - device->devInfo_.devBusInfo.busType = this->busType_; - this->extDevMgr_.UnRegisterDevice(device); - return EDM_OK; + std::vector> devInfoVec; + if (deviceMap_.find(busType) != deviceMap_.end()) { + return devInfoVec; + } + std::unordered_map> map = deviceMap_[busType]; + for (auto device : map) { + devInfoVec.push_back(device.second->GetDeviceInfo()); + } + return devInfoVec; } } // namespace ExternalDeviceManager } // namespace OHOS \ No newline at end of file diff --git a/services/native/driver_extension_manager/src/driver_etx_mgr.cpp b/services/native/driver_extension_manager/src/driver_etx_mgr.cpp index 4ee5244..7b0272b 100644 --- a/services/native/driver_extension_manager/src/driver_etx_mgr.cpp +++ b/services/native/driver_extension_manager/src/driver_etx_mgr.cpp @@ -20,6 +20,8 @@ #include "hilog_wrapper.h" #include "iservice_registry.h" #include "system_ability_definition.h" +#include "dev_change_callback.h" +#include "bus_extension_core.h" namespace OHOS { namespace ExternalDeviceManager { @@ -31,15 +33,16 @@ DriverExtMgr::~DriverExtMgr() {} void DriverExtMgr::OnStart() { - EDM_LOGI(MODULE_DEV_MGR, "hdf_ext_devmgr OnStart"); + EDM_LOGI(MODULE_SERVICE, "hdf_ext_devmgr OnStart"); DriverPkgManager::GetInstance().Init(); ExtDeviceManager::GetInstance().Init(); - BusExtensionCore::GetInstance().Init(); + std::shared_ptr callback = std::make_shared(); + BusExtensionCore::GetInstance().Init(callback); } void DriverExtMgr::OnStop() { - EDM_LOGI(MODULE_DEV_MGR, "hdf_ext_devmgr OnStop"); + EDM_LOGI(MODULE_SERVICE, "hdf_ext_devmgr OnStop"); delete &(DriverPkgManager::GetInstance()); delete &(ExtDeviceManager::GetInstance()); delete &(BusExtensionCore::GetInstance()); diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp b/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp index 648631b..cdce3e4 100644 --- a/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp @@ -22,6 +22,7 @@ #include "usb_bus_extension.h" #include "common_event_support.h" #include "common_event_subscribe_info.h" +#include "bus_extension_core.h" #include "driver_pkg_manager.h" namespace OHOS { @@ -107,7 +108,7 @@ shared_ptr DriverPkgManager::QueryMatchDriver(shared_ptr> list = devmgr.deviceMap_[BUS_TYPE_USB]; - cout << "usb device size: " << list.size() << endl; - std::list>::iterator iter; - for (iter = list.begin(); iter != list.end(); iter++) { - std::shared_ptr device = *iter; + std::unordered_map> &map = devmgr.deviceMap_[BUS_TYPE_USB]; + cout << "usb device size: " << map.size() << endl; + for (auto &iter : map) { + std::shared_ptr device = iter.second; cout << device->GetDeviceInfo()->GetDeviceDescription().c_str() << endl; } cout << "------------------" << endl; @@ -53,7 +54,8 @@ int main(int argc, char **argv) BusExtensionCore &core = BusExtensionCore::GetInstance(); size_t size = core.busExtensions_.size(); cout << "busExtensions_.size: " << size << endl; - int32_t ret = core.Init(); + std::shared_ptr callback = std::make_shared(); + int32_t ret = core.Init(callback); if (ret != EDM_OK) { cout << "busExtensionCore init failed" << endl; return ret; diff --git a/test/unittest/device_manager_test/BUILD.gn b/test/unittest/device_manager_test/BUILD.gn index 435eec7..6bd37ee 100644 --- a/test/unittest/device_manager_test/BUILD.gn +++ b/test/unittest/device_manager_test/BUILD.gn @@ -14,21 +14,20 @@ import("//build/test.gni") import("//drivers/external_device_manager/extdevmgr.gni") module_output_path = "external_device_manager/unittest" -device_manager_include_path = "${ext_mgr_path}/services/native/driver_extension_manager/include/device_manager" -bus_extension_include_path = "${ext_mgr_path}/services/native/driver_extension_manager/include/bus_extension/usb" -utils_include_path = "${ext_mgr_path}/utils/include" ohos_unittest("device_manager_test") { module_out_path = "${module_output_path}" sources = [ "device_manager_test.cpp" ] include_dirs = [ - "${device_manager_include_path}", - "${bus_extension_include_path}", - "${utils_include_path}", + "${ext_mgr_path}/services/native/driver_extension_manager/include/device_manager", + "${ext_mgr_path}/services/native/driver_extension_manager/include/bus_extension/usb", + "${ext_mgr_path}/services/native/driver_extension_manager/include/bus_extension/core", ] deps = [ "${ext_mgr_path}/services/native/driver_extension_manager/src/bus_extension:bus_extension", - "${ext_mgr_path}/services/native/driver_extension_manager/src/device_manager:bus_extension_core", + "${ext_mgr_path}/services/native/driver_extension_manager/src/bus_extension/core:driver_extension_bus_core", + "${ext_mgr_path}/services/native/driver_extension_manager/src/device_manager:driver_extension_dev_callback", + "${ext_mgr_path}/services/native/driver_extension_manager/src/device_manager:driver_extension_device_manager", ] external_deps = [ "c_utils:utils", diff --git a/test/unittest/device_manager_test/device_manager_test.cpp b/test/unittest/device_manager_test/device_manager_test.cpp index 0937bd3..fec1113 100644 --- a/test/unittest/device_manager_test/device_manager_test.cpp +++ b/test/unittest/device_manager_test/device_manager_test.cpp @@ -17,9 +17,11 @@ #include "edm_errors.h" #include "hilog_wrapper.h" #define private public +#include "dev_change_callback.h" #include "etx_device_mgr.h" #include "ibus_extension.h" #include "usb_bus_extension.h" +#include "bus_extension_core.h" #undef private namespace OHOS { @@ -56,8 +58,7 @@ HWTEST_F(DeviceManagerTest, InitTest, TestSize.Level1) // test OnDeviceAdd and OnDeviceRemove HWTEST_F(DeviceManagerTest, OnDeviceAddRemoveTest001, TestSize.Level1) { - std::shared_ptr callback = - std::make_shared(BusType::BUS_TYPE_TEST, ExtDeviceManager::GetInstance()); + std::shared_ptr callback = std::make_shared(); std::shared_ptr device = std::make_shared(0); device->devInfo_.devBusInfo.busType = BusType::BUS_TYPE_TEST; device->devInfo_.devBusInfo.busDeviceId = 1; @@ -73,8 +74,7 @@ HWTEST_F(DeviceManagerTest, OnDeviceAddRemoveTest001, TestSize.Level1) // test adding device repeatedly HWTEST_F(DeviceManagerTest, OnDeviceAddRemoveTest002, TestSize.Level1) { - std::shared_ptr callback = - std::make_shared(BusType::BUS_TYPE_TEST, ExtDeviceManager::GetInstance()); + std::shared_ptr callback = std::make_shared(); std::shared_ptr device = std::make_shared(0); device->devInfo_.devBusInfo.busType = BusType::BUS_TYPE_TEST; device->devInfo_.devBusInfo.busDeviceId = 1; @@ -93,8 +93,7 @@ HWTEST_F(DeviceManagerTest, OnDeviceAddRemoveTest002, TestSize.Level1) HWTEST_F(DeviceManagerTest, OnDeviceAddRemoveTest003, TestSize.Level1) { - std::shared_ptr callback = - std::make_shared(BusType::BUS_TYPE_TEST, ExtDeviceManager::GetInstance()); + std::shared_ptr callback = std::make_shared(); std::shared_ptr device0 = std::make_shared(0); device0->devInfo_.devBusInfo.busType = BusType::BUS_TYPE_TEST; device0->devInfo_.devBusInfo.busDeviceId = 1; diff --git a/utils/include/ext_object.h b/utils/include/ext_object.h index 0279458..d7d5fb0 100644 --- a/utils/include/ext_object.h +++ b/utils/include/ext_object.h @@ -14,12 +14,10 @@ */ #ifndef EXT_OBJECT_H #define EXT_OBJECT_H -#include -#include +#include +#include #include "edm_errors.h" -#include "single_instance.h" -#include "singleton.h" namespace OHOS { namespace ExternalDeviceManager { enum BusType : uint32_t { @@ -28,7 +26,7 @@ enum BusType : uint32_t { BUS_TYPE_MAX, BUS_TYPE_TEST, }; -class IBusExtension; + class DrvBundleStateCallback; class DriverInfoExt { public: @@ -78,7 +76,6 @@ public: } private: - friend class DevChangeCallback; union DevInfo { uint64_t deviceId; struct { @@ -88,46 +85,6 @@ private: } devInfo_; std::string description_ {""}; }; - -class ExtDeviceManager; -class IDevChangeCallback { -public: - virtual ~IDevChangeCallback() = default; - virtual int32_t OnDeviceAdd(std::shared_ptr device); - virtual int32_t OnDeviceRemove(std::shared_ptr device); -}; - -class DevChangeCallback final : public IDevChangeCallback { -public: - DevChangeCallback(BusType busType, ExtDeviceManager &extDevMgr) : busType_(busType), extDevMgr_(extDevMgr) {}; - int32_t OnDeviceAdd(std::shared_ptr device) override; - int32_t OnDeviceRemove(std::shared_ptr device) override; - -private: - BusType busType_; - ExtDeviceManager &extDevMgr_; -}; - -class BusExtensionCore { - DECLARE_SINGLE_INSTANCE_BASE(BusExtensionCore); - -public: - ~BusExtensionCore() = default; - int32_t Init(); - int32_t Register(BusType busType, std::shared_ptr busExtension); - -private: - BusExtensionCore() = default; - std::unordered_map> busExtensions_; - const uint32_t MAX_BUS_EXTENSIONS = 100; -}; - -// bus extension should register by __attribute__ ((constructor)) when loading so -template -void RegisterBusExtension(BusType busType) -{ - BusExtensionCore::GetInstance().Register(busType, std::make_shared()); -} } // namespace ExternalDeviceManager } // namespace OHOS #endif // EXT_OBJECT_H \ No newline at end of file diff --git a/utils/include/ibus_extension.h b/utils/include/ibus_extension.h index de3db9f..e6c8d50 100644 --- a/utils/include/ibus_extension.h +++ b/utils/include/ibus_extension.h @@ -21,6 +21,7 @@ #include #include "application_info.h" #include "ext_object.h" +#include "idev_change_callback.h" namespace OHOS { namespace ExternalDeviceManager { using namespace std; diff --git a/utils/include/idev_change_callback.h b/utils/include/idev_change_callback.h new file mode 100644 index 0000000..3fab4ab --- /dev/null +++ b/utils/include/idev_change_callback.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 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 IDEV_CHANGE_CALLBACK_H +#define IDEV_CHANGE_CALLBACK_H + +#include +#include "ext_object.h" + +namespace OHOS { +namespace ExternalDeviceManager { +class IDevChangeCallback { +public: + virtual ~IDevChangeCallback() = default; + virtual int32_t OnDeviceAdd(std::shared_ptr device); + virtual int32_t OnDeviceRemove(std::shared_ptr device); +}; +} // namespace ExternalDeviceManager +} // namespace OHOS +#endif // IDEV_CHANGE_CALLBACK_H \ No newline at end of file -- Gitee