From a9af0b5d9d54f35550fb16922723f6a182479a41 Mon Sep 17 00:00:00 2001 From: xuxiaoqing Date: Sat, 10 Jun 2023 10:21:16 +0000 Subject: [PATCH] fix: modify the singleton of ExtDeviceManager and BusExtensionCore Signed-off-by: xuxiaoqing --- bundle.json | 3 +- .../include/device_manager/etx_device_mgr.h | 2 +- .../src/device_manager/etx_device_mgr.cpp | 40 ++++------- test/moduletest/BUILD.gn | 19 +++++ .../bus_extension_core_mt.cpp | 70 +++++++++++++++++++ .../device_manager_test.cpp | 34 ++++----- utils/include/ext_object.h | 16 ++--- utils/include/single_instance.h | 46 ++++++++++++ 8 files changed, 175 insertions(+), 55 deletions(-) create mode 100644 test/moduletest/bus_extension_core_mt/bus_extension_core_mt.cpp create mode 100644 utils/include/single_instance.h diff --git a/bundle.json b/bundle.json index 817e032..8cd07ca 100644 --- a/bundle.json +++ b/bundle.json @@ -51,7 +51,8 @@ "test": [ "//drivers/external_device_manager/test/unittest:external_device_manager_test", "//drivers/external_device_manager/test/unittest/driver_extension_context_test:driver_extension_context_test", - "//drivers/external_device_manager/test/fuzztest:fuzztest" + "//drivers/external_device_manager/test/fuzztest:fuzztest", + "//drivers/external_device_manager/test//moduletest/:external_device_manager_mt" ] } } 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 eb42ca8..f7cc5c8 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 @@ -38,7 +38,7 @@ private: }; class ExtDeviceManager final { - DECLARE_DELAYED_SINGLETON(ExtDeviceManager) + DECLARE_SINGLE_INSTANCE(ExtDeviceManager); public: int32_t Init(); int32_t RegisterDevice(std::shared_ptr devInfo); 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 3872a44..72da2bc 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 @@ -21,36 +21,20 @@ namespace OHOS { namespace ExternalDeviceManager { -BusExtensionCore::BusExtensionCore() -{ - return; -} - -BusExtensionCore::~BusExtensionCore() -{ - return; -} - -ExtDeviceManager::~ExtDeviceManager() -{ - return; -} - -ExtDeviceManager::ExtDeviceManager() -{ - return; -} +IMPLEMENT_SINGLE_INSTANCE(BusExtensionCore); +IMPLEMENT_SINGLE_INSTANCE(ExtDeviceManager); int32_t BusExtensionCore::Init() { int ret = EDM_OK; for (auto &iter : busExtensions_) { std::shared_ptr callback = - std::make_shared(iter.first, DelayedSingleton::GetInstance()); + 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; } @@ -66,13 +50,13 @@ int32_t BusExtensionCore::Register(BusType busType, std::shared_ptr devInfo) } std::shared_ptr device = std::make_shared(devInfo); deviceMap_[type].push_back(device); - EDM_LOGI(MODULE_DEV_MGR, "successfully registered device, deviceId is %{public}016" PRIx64 "", deviceId); + EDM_LOGD(MODULE_DEV_MGR, "successfully registered device, deviceId is %{public}016" PRIx64 "", deviceId); // driver match return EDM_OK; } @@ -117,21 +101,21 @@ void ExtDeviceManager::UnRegisterDevice(const std::shared_ptr devInf } } } - EDM_LOGI(MODULE_DEV_MGR, "device has been unregistered, deviceId is %{public}016" PRIx64 "", deviceId); + EDM_LOGD(MODULE_DEV_MGR, "device has been unregistered, deviceId is %{public}016" PRIx64 "", deviceId); } int32_t DevChangeCallback::OnDeviceAdd(std::shared_ptr device) { - EDM_LOGI(MODULE_DEV_MGR, "OnDeviceAdd start"); + EDM_LOGD(MODULE_DEV_MGR, "OnDeviceAdd start"); device->devInfo_.devBusInfo.busType = this->busType_; - return this->extDevMgr_->RegisterDevice(device); + return this->extDevMgr_.RegisterDevice(device); } int32_t DevChangeCallback::OnDeviceRemove(std::shared_ptr device) { - EDM_LOGI(MODULE_DEV_MGR, "OnDeviceRemove start"); + EDM_LOGD(MODULE_DEV_MGR, "OnDeviceRemove start"); device->devInfo_.devBusInfo.busType = this->busType_; - this->extDevMgr_->UnRegisterDevice(device); + this->extDevMgr_.UnRegisterDevice(device); return EDM_OK; } } // namespace ExternalDeviceManager diff --git a/test/moduletest/BUILD.gn b/test/moduletest/BUILD.gn index 5be0eb4..f0ccf19 100644 --- a/test/moduletest/BUILD.gn +++ b/test/moduletest/BUILD.gn @@ -41,8 +41,27 @@ ohos_executable("driver_extension_controller_mt") { part_name = "external_device_manager" install_enable = false } + +ohos_executable("bus_extension_core_mt") { + sources = [ "bus_extension_core_mt/bus_extension_core_mt.cpp" ] + include_dirs = [ + "${ext_mgr_path}/services/native/driver_extension_manager/include/device_manager", + "${ext_mgr_path}/services/native/driver_extension_manager/include/bus_extension/usb", + ] + configs = [ "${utils_path}:utils_config" ] + deps = [ "${ext_mgr_path}/services/native/driver_extension_manager/src/device_manager:bus_extension_core" ] + external_deps = [ + "c_utils:utils", + "drivers_interface_usb:libusb_proxy_1.0", + "hiviewdfx_hilog_native:libhilog", + ] + part_name = "external_device_manager" + install_enable = false +} + group("external_device_manager_mt") { deps = [ + ":bus_extension_core_mt", ":driver_extension_controller_mt", ":usb_bus_extension_mt", ] diff --git a/test/moduletest/bus_extension_core_mt/bus_extension_core_mt.cpp b/test/moduletest/bus_extension_core_mt/bus_extension_core_mt.cpp new file mode 100644 index 0000000..49c527e --- /dev/null +++ b/test/moduletest/bus_extension_core_mt/bus_extension_core_mt.cpp @@ -0,0 +1,70 @@ +/* + * 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 "edm_errors.h" +#include "hilog_wrapper.h" +#include "iostream" +#define private public +#include "etx_device_mgr.h" +#undef private + +using namespace OHOS::ExternalDeviceManager; +using namespace std; +static void PrintAllDevice() +{ + ExtDeviceManager &devmgr = ExtDeviceManager::GetInstance(); + cout << "------------------" << endl; + std::list> 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; + cout << device->GetDeviceInfo()->GetDeviceDescription().c_str() << endl; + } + cout << "------------------" << endl; +} + +int main(int argc, char **argv) +{ + std::string libPath = "/system/lib/libbus_extension.z.so"; + void *handler = dlopen(libPath.c_str(), RTLD_LAZY); + if (handler == nullptr) { + cout << "dlopen libbus_extension.z.so failed" << endl; + return -1; + } + BusExtensionCore &core = BusExtensionCore::GetInstance(); + size_t size = core.busExtensions_.size(); + cout << "busExtensions_.size: " << size << endl; + int32_t ret = core.Init(); + if (ret != EDM_OK) { + cout << "busExtensionCore init failed" << endl; + return ret; + } + while (true) { + std::string in; + cin >> in; + if (in == "q") { + break; + } else if (in == "p") { + PrintAllDevice(); + } else { + cout << in; + } + } + cout << "exit!" << endl; + return ret; +} \ No newline at end of file diff --git a/test/unittest/device_manager_test/device_manager_test.cpp b/test/unittest/device_manager_test/device_manager_test.cpp index f6fd468..0937bd3 100644 --- a/test/unittest/device_manager_test/device_manager_test.cpp +++ b/test/unittest/device_manager_test/device_manager_test.cpp @@ -41,15 +41,15 @@ public: HWTEST_F(DeviceManagerTest, BusExtensionRegisterTest, TestSize.Level1) { - std::shared_ptr core = DelayedSingleton::GetInstance(); - int32_t ret = core->Register(BusType::BUS_TYPE_USB, std::make_shared()); + BusExtensionCore &core = BusExtensionCore::GetInstance(); + int32_t ret = core.Register(BusType::BUS_TYPE_USB, std::make_shared()); ASSERT_EQ(ret, EDM_OK); - ASSERT_NE(core->busExtensions_[BusType::BUS_TYPE_USB], nullptr); + ASSERT_NE(core.busExtensions_[BusType::BUS_TYPE_USB], nullptr); } HWTEST_F(DeviceManagerTest, InitTest, TestSize.Level1) { - int32_t ret = DelayedSingleton::GetInstance()->Init(); + int32_t ret = ExtDeviceManager::GetInstance().Init(); ASSERT_EQ(ret, EDM_OK); } @@ -57,24 +57,24 @@ HWTEST_F(DeviceManagerTest, InitTest, TestSize.Level1) HWTEST_F(DeviceManagerTest, OnDeviceAddRemoveTest001, TestSize.Level1) { std::shared_ptr callback = - std::make_shared(BusType::BUS_TYPE_TEST, DelayedSingleton::GetInstance()); + std::make_shared(BusType::BUS_TYPE_TEST, ExtDeviceManager::GetInstance()); std::shared_ptr device = std::make_shared(0); device->devInfo_.devBusInfo.busType = BusType::BUS_TYPE_TEST; device->devInfo_.devBusInfo.busDeviceId = 1; int32_t ret = callback->OnDeviceAdd(device); ASSERT_EQ(ret, EDM_OK); - std::shared_ptr extMgr = DelayedSingleton::GetInstance(); - ASSERT_EQ(extMgr->deviceMap_[BusType::BUS_TYPE_TEST].size(), 1); + ExtDeviceManager &extMgr = ExtDeviceManager::GetInstance(); + ASSERT_EQ(extMgr.deviceMap_[BusType::BUS_TYPE_TEST].size(), 1); ret = callback->OnDeviceRemove(device); ASSERT_EQ(ret, EDM_OK); - ASSERT_EQ(extMgr->deviceMap_[BusType::BUS_TYPE_TEST].size(), 0); + ASSERT_EQ(extMgr.deviceMap_[BusType::BUS_TYPE_TEST].size(), 0); } // test adding device repeatedly HWTEST_F(DeviceManagerTest, OnDeviceAddRemoveTest002, TestSize.Level1) { std::shared_ptr callback = - std::make_shared(BusType::BUS_TYPE_TEST, DelayedSingleton::GetInstance()); + std::make_shared(BusType::BUS_TYPE_TEST, ExtDeviceManager::GetInstance()); std::shared_ptr device = std::make_shared(0); device->devInfo_.devBusInfo.busType = BusType::BUS_TYPE_TEST; device->devInfo_.devBusInfo.busDeviceId = 1; @@ -82,11 +82,11 @@ HWTEST_F(DeviceManagerTest, OnDeviceAddRemoveTest002, TestSize.Level1) ASSERT_EQ(ret, EDM_OK); ret = callback->OnDeviceAdd(device); ASSERT_EQ(ret, EDM_OK); - std::shared_ptr extMgr = DelayedSingleton::GetInstance(); - ASSERT_EQ(extMgr->deviceMap_[BusType::BUS_TYPE_TEST].size(), 1); + ExtDeviceManager &extMgr = ExtDeviceManager::GetInstance(); + ASSERT_EQ(extMgr.deviceMap_[BusType::BUS_TYPE_TEST].size(), 1); ret = callback->OnDeviceRemove(device); ASSERT_EQ(ret, EDM_OK); - ASSERT_EQ(extMgr->deviceMap_[BusType::BUS_TYPE_TEST].size(), 0); + ASSERT_EQ(extMgr.deviceMap_[BusType::BUS_TYPE_TEST].size(), 0); ret = callback->OnDeviceRemove(device); ASSERT_EQ(ret, EDM_OK); } @@ -94,7 +94,7 @@ HWTEST_F(DeviceManagerTest, OnDeviceAddRemoveTest002, TestSize.Level1) HWTEST_F(DeviceManagerTest, OnDeviceAddRemoveTest003, TestSize.Level1) { std::shared_ptr callback = - std::make_shared(BusType::BUS_TYPE_TEST, DelayedSingleton::GetInstance()); + std::make_shared(BusType::BUS_TYPE_TEST, ExtDeviceManager::GetInstance()); std::shared_ptr device0 = std::make_shared(0); device0->devInfo_.devBusInfo.busType = BusType::BUS_TYPE_TEST; device0->devInfo_.devBusInfo.busDeviceId = 1; @@ -105,14 +105,14 @@ HWTEST_F(DeviceManagerTest, OnDeviceAddRemoveTest003, TestSize.Level1) device1->devInfo_.devBusInfo.busDeviceId = 2; ret = callback->OnDeviceAdd(device1); ASSERT_EQ(ret, EDM_OK); - std::shared_ptr extMgr = DelayedSingleton::GetInstance(); - ASSERT_EQ(extMgr->deviceMap_[BusType::BUS_TYPE_TEST].size(), 2); + ExtDeviceManager &extMgr = ExtDeviceManager::GetInstance(); + ASSERT_EQ(extMgr.deviceMap_[BusType::BUS_TYPE_TEST].size(), 2); ret = callback->OnDeviceRemove(device1); ASSERT_EQ(ret, EDM_OK); - ASSERT_EQ(extMgr->deviceMap_[BusType::BUS_TYPE_TEST].size(), 1); + ASSERT_EQ(extMgr.deviceMap_[BusType::BUS_TYPE_TEST].size(), 1); ret = callback->OnDeviceRemove(device0); ASSERT_EQ(ret, EDM_OK); - ASSERT_EQ(extMgr->deviceMap_[BusType::BUS_TYPE_TEST].size(), 0); + ASSERT_EQ(extMgr.deviceMap_[BusType::BUS_TYPE_TEST].size(), 0); } } // namespace ExternalDeviceManager } // namespace OHOS diff --git a/utils/include/ext_object.h b/utils/include/ext_object.h index 94d8525..cb10f00 100644 --- a/utils/include/ext_object.h +++ b/utils/include/ext_object.h @@ -16,8 +16,10 @@ #define EXT_OBJECT_H #include #include -#include "singleton.h" + #include "edm_errors.h" +#include "single_instance.h" +#include "singleton.h" namespace OHOS { namespace ExternalDeviceManager { enum BusType : uint32_t { @@ -96,19 +98,18 @@ public: class DevChangeCallback final : public IDevChangeCallback { public: - DevChangeCallback(BusType busType, std::shared_ptr extDevMgr) - : busType_(busType), extDevMgr_(extDevMgr) {}; + 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_; - std::shared_ptr extDevMgr_; + ExtDeviceManager &extDevMgr_; }; - class BusExtensionCore { - DECLARE_DELAYED_SINGLETON(BusExtensionCore) + DECLARE_SINGLE_INSTANCE(BusExtensionCore); + public: int32_t Init(); int32_t Register(BusType busType, std::shared_ptr busExtension); @@ -122,8 +123,7 @@ private: template void RegisterBusExtension(BusType busType) { - DelayedSingleton::GetInstance()->Register(\ - busType, std::make_shared()); + BusExtensionCore::GetInstance().Register(busType, std::make_shared()); } } // namespace ExternalDeviceManager } // namespace OHOS diff --git a/utils/include/single_instance.h b/utils/include/single_instance.h new file mode 100644 index 0000000..dc6036f --- /dev/null +++ b/utils/include/single_instance.h @@ -0,0 +1,46 @@ +/* + * 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 OHOS_EDM_SINGLE_INSTANCE_H +#define OHOS_EDM_SINGLE_INSTANCE_H + +namespace OHOS { +namespace ExternalDeviceManager { +#define DECLARE_SINGLE_INSTANCE_BASE(className) \ +public: \ + static className &GetInstance(); \ + \ +private: \ + className(const className &) = delete; \ + className &operator=(const className &) = delete; \ + className(className &&) = delete; \ + className &operator=(className &&) = delete; + +#define DECLARE_SINGLE_INSTANCE(className) \ + DECLARE_SINGLE_INSTANCE_BASE(className) \ + \ +private: \ + className() = default; \ + ~className() = default; + +#define IMPLEMENT_SINGLE_INSTANCE(className) \ + className &className::GetInstance() \ + { \ + static auto instance = new className(); \ + return *instance; \ + } +}; // namespace ExternalDeviceManager +}; // namespace OHOS +#endif -- Gitee