From 98b7fb90d633024222405e474e7c460d24ae3f17 Mon Sep 17 00:00:00 2001 From: xuxiaoqing Date: Thu, 29 Jun 2023 03:04:59 +0000 Subject: [PATCH] fix: match driver and connect ability Signed-off-by: xuxiaoqing --- sa_profile/5110.json | 3 +- sa_profile/hdf_ext_devmgr.cfg | 2 +- services/BUILD.gn | 1 + .../bus_extension/core/bus_extension_core.h | 1 + .../include/device_manager/etx_device_mgr.h | 2 +- .../driver_extension_controller.h | 6 +- .../bus_extension/core/bus_extension_core.cpp | 3 +- .../src/device_manager/device.cpp | 35 ++++++----- .../src/device_manager/etx_device_mgr.cpp | 58 +++++++++++++++---- .../src/driver_ext_mgr.cpp | 3 + 10 files changed, 78 insertions(+), 36 deletions(-) diff --git a/sa_profile/5110.json b/sa_profile/5110.json index ad4d827..862d8c2 100644 --- a/sa_profile/5110.json +++ b/sa_profile/5110.json @@ -4,7 +4,8 @@ { "name": 5110, "libpath": "libdriver_extension_manager.z.so", - "run-on-create": true, + "run-on-create": false, + "auto-restart": true, "distributed": false, "dump_level": 1 } diff --git a/sa_profile/hdf_ext_devmgr.cfg b/sa_profile/hdf_ext_devmgr.cfg index 138b29c..2260d7b 100644 --- a/sa_profile/hdf_ext_devmgr.cfg +++ b/sa_profile/hdf_ext_devmgr.cfg @@ -2,7 +2,7 @@ "services" : [{ "name" : "hdf_ext_devmgr", "path" : ["/system/bin/sa_main", "/system/profile/hdf_ext_devmgr.json"], - "ondemand" : false, + "ondemand" : true, "uid" : "hdf_ext_devmgr", "gid" : ["hdf_ext_devmgr", "shell"], "permission" : [ diff --git a/services/BUILD.gn b/services/BUILD.gn index 86d9474..96e6688 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -29,6 +29,7 @@ ohos_shared_library("driver_extension_manager") { "${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", + "${ext_mgr_path}/services/native/driver_extension_manager/include/driver_controller", ] configs = [ "${utils_path}:utils_config" ] deps = [ 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 index 0eaac2d..112aeea 100644 --- 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 @@ -32,6 +32,7 @@ public: int32_t Init(std::shared_ptr callback); int32_t Register(BusType busType, std::shared_ptr busExtension); std::shared_ptr GetBusExtensionByName(std::string busName); + void LoadBusExtensionLibs(); private: BusExtensionCore() = default; 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 4256c3c..1fce8c5 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 @@ -33,7 +33,7 @@ class ExtDeviceManager final { DECLARE_SINGLE_INSTANCE_BASE(ExtDeviceManager); public: - ~ExtDeviceManager() = default; + ~ExtDeviceManager(); int32_t Init(); int32_t RegisterDevice(shared_ptr devInfo); int32_t UnRegisterDevice(const shared_ptr devInfo); diff --git a/services/native/driver_extension_manager/include/driver_controller/driver_extension_controller.h b/services/native/driver_extension_manager/include/driver_controller/driver_extension_controller.h index e76b825..bf42d63 100644 --- a/services/native/driver_extension_manager/include/driver_controller/driver_extension_controller.h +++ b/services/native/driver_extension_manager/include/driver_controller/driver_extension_controller.h @@ -25,8 +25,9 @@ namespace OHOS { namespace ExternalDeviceManager { class IDriverExtensionConnectCallback; class DriverExtensionController { - DECLARE_SINGLE_INSTANCE(DriverExtensionController); + DECLARE_SINGLE_INSTANCE_BASE(DriverExtensionController); public: + ~DriverExtensionController() = default; int32_t StartDriverExtension(std::string baudleName, std::string abilityName); int32_t StopDriverExtension(std::string bundleName, std::string abilityName); int32_t ConnectDriverExtension( @@ -42,6 +43,9 @@ public: uint32_t deviceId = 0 ); class DriverExtensionAbilityConnection; + +private: + DriverExtensionController() = default; }; struct DrvExtConnectionInfo { 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 index f288cf8..20cf3ec 100644 --- 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 @@ -34,7 +34,7 @@ namespace OHOS { namespace ExternalDeviceManager { IMPLEMENT_SINGLE_INSTANCE(BusExtensionCore); -static void LoadLib() +void BusExtensionCore::LoadBusExtensionLibs() { for (BusType i = BUS_TYPE_USB; i < BUS_TYPE_MAX; i = (BusType)(i + 1)) { std::ostringstream libPath; @@ -58,7 +58,6 @@ static void LoadLib() int32_t BusExtensionCore::Init(std::shared_ptr callback) { - LoadLib(); int ret = EDM_OK; for (auto &iter : busExtensions_) { if (iter.second->SetDevChangeCallback(callback) != EDM_OK) { diff --git a/services/native/driver_extension_manager/src/device_manager/device.cpp b/services/native/driver_extension_manager/src/device_manager/device.cpp index c699514..fea8b86 100644 --- a/services/native/driver_extension_manager/src/device_manager/device.cpp +++ b/services/native/driver_extension_manager/src/device_manager/device.cpp @@ -44,12 +44,13 @@ int32_t Device::Connect() { EDM_LOGI(MODULE_DEV_MGR, "%{public}s enter", __func__); std::lock_guard lock(deviceMutex_); - uint64_t deviceId = GetDeviceInfo()->GetDeviceId(); - // temporary test data - std::string bundleName = "com.ohos.nativeapplication"; - std::string abilityName = "DriverAbility"; + uint32_t busDevId = GetDeviceInfo()->GetBusDevId(); + std::string bundleInfo = GetBundleInfo(); + std::string bundleName = Device::GetBundleName(bundleInfo); + std::string abilityName = Device::GetAbilityName(bundleInfo); + AddDrvExtConnNotify(); int32_t ret = DriverExtensionController::GetInstance().ConnectDriverExtension( - bundleName, abilityName, connectNofitier_, deviceId); + bundleName, abilityName, connectNofitier_, busDevId); if (ret != UsbErrCode::EDM_OK) { EDM_LOGE(MODULE_DEV_MGR, "failed to connect driver extension"); return ret; @@ -78,11 +79,13 @@ int32_t Device::Connect(const sptr &connectCallback) } UpdateDrvExtConnNotify(); - // temporary test data - std::string bundleName = "com.ohos.nativeapplication"; - std::string abilityName = "DriverAbility"; + std::string bundleInfo = GetBundleInfo(); + std::string bundleName = Device::GetBundleName(bundleInfo); + std::string abilityName = Device::GetAbilityName(bundleInfo); + AddDrvExtConnNotify(); + uint32_t busDevId = GetDeviceInfo()->GetBusDevId(); ret = DriverExtensionController::GetInstance().ConnectDriverExtension( - bundleName, abilityName, connectNofitier_, deviceId); + bundleName, abilityName, connectNofitier_, busDevId); if (ret != UsbErrCode::EDM_OK) { EDM_LOGE(MODULE_DEV_MGR, "failed to connect driver extension"); UnregisterDrvExtMgrCallback(connectCallback); @@ -95,16 +98,12 @@ int32_t Device::Disconnect() { EDM_LOGI(MODULE_DEV_MGR, "%{public}s enter", __func__); std::lock_guard lock(deviceMutex_); - uint64_t deviceId = GetDeviceInfo()->GetDeviceId(); - // std::string bundleInfo = GetBundleInfo(); - // std::string bundleName = Device::GetBundleName(bundleInfo); - // std::string abilityName = Device::GetAbilityName(bundleInfo); - - // temporary test data - std::string bundleName = "com.ohos.nativeapplication"; - std::string abilityName = "DriverAbility"; + uint32_t busDevId = GetDeviceInfo()->GetBusDevId(); + std::string bundleInfo = GetBundleInfo(); + std::string bundleName = Device::GetBundleName(bundleInfo); + std::string abilityName = Device::GetAbilityName(bundleInfo); int32_t ret = DriverExtensionController::GetInstance().DisconnectDriverExtension( - bundleName, abilityName, connectNofitier_, deviceId); + bundleName, abilityName, connectNofitier_, busDevId); if (ret != UsbErrCode::EDM_OK) { EDM_LOGE(MODULE_DEV_MGR, "failed to connect driver extension"); return ret; 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 747b95a..d404ae0 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 @@ -29,6 +29,12 @@ constexpr uint32_t UNLOAD_SA_TIMER_INTERVAL = 30 * 1000; std::string Device::stiching_ = "_stiching_"; IMPLEMENT_SINGLE_INSTANCE(ExtDeviceManager); +ExtDeviceManager::~ExtDeviceManager() +{ + unloadSelftimer_.Unregister(unloadSelftimerId_); + unloadSelftimer_.Shutdown(); +} + void ExtDeviceManager::PrintMatchDriverMap() { if (!bundleMatchMap_.empty()) { @@ -59,7 +65,7 @@ int32_t ExtDeviceManager::AddDevIdOfBundleInfoMap(shared_ptr device, str { if (bundleInfo.empty() || device == nullptr) { EDM_LOGE(MODULE_DEV_MGR, "bundleInfo or device is null"); - return EDM_NOK; + return EDM_ERR_INVALID_PARAM; } // update bundle info @@ -70,17 +76,24 @@ int32_t ExtDeviceManager::AddDevIdOfBundleInfoMap(shared_ptr device, str unordered_set tmpSet; tmpSet.emplace(deviceId); bundleMatchMap_.emplace(bundleInfo, tmpSet); - EDM_LOGD(MODULE_DEV_MGR, "bundleMap emplace New driver, add deviceId %{public}016" PRIX64 "", deviceId); + EDM_LOGI(MODULE_DEV_MGR, "bundleMap emplace New driver, add deviceId %{public}016" PRIX64 "", deviceId); } else { auto pairRet = pos->second.emplace(deviceId); // Check whether the deviceId matches the driver if (!pairRet.second || pos->second.size() > 1) { - EDM_LOGD(MODULE_DEV_MGR, "bundleMap had existed driver, add deviceId %{public}016" PRIX64 "", deviceId); + EDM_LOGI(MODULE_DEV_MGR, "bundleMap had existed driver, add deviceId %{public}016" PRIX64 "", deviceId); PrintMatchDriverMap(); } } // start ability + int32_t ret = device->Connect(); + if (ret != EDM_OK) { + EDM_LOGE(MODULE_DEV_MGR, + "deviceId[%{public}016" PRIX64 "] connect driver extension ability[%{public}s] failed[%{public}d]", + deviceId, Device::GetAbilityName(bundleInfo).c_str(), ret); + return EDM_NOK; + } PrintMatchDriverMap(); return EDM_OK; } @@ -89,7 +102,7 @@ int32_t ExtDeviceManager::RemoveDevIdOfBundleInfoMap(shared_ptr device, { if (bundleInfo.empty() || device == nullptr) { EDM_LOGE(MODULE_DEV_MGR, "bundleInfo or device is null"); - return EDM_NOK; + return EDM_ERR_INVALID_PARAM; } // update bundle info @@ -112,6 +125,14 @@ int32_t ExtDeviceManager::RemoveDevIdOfBundleInfoMap(shared_ptr device, EDM_LOGD(MODULE_DEV_MGR, "bundleMap remove bundleInfo[%{public}s]", bundleInfo.c_str()); bundleMatchMap_.erase(pos); + // stop ability and destory sa + int32_t ret = device->Disconnect(); + if (ret != EDM_OK) { + EDM_LOGE(MODULE_DEV_MGR, + "deviceId[%{public}016" PRIX64 "] disconnect driver extension ability[%{public}s] failed[%{public}d]", + deviceId, Device::GetAbilityName(bundleInfo).c_str(), ret); + return ret; + } PrintMatchDriverMap(); return EDM_OK; } @@ -130,6 +151,13 @@ int32_t ExtDeviceManager::RemoveAllDevIdOfBundleInfoMap(shared_ptr devic } bundleMatchMap_.erase(pos); + // stop ability and destory sa + int32_t ret = device->Disconnect(); + if (ret != EDM_OK) { + EDM_LOGE(MODULE_DEV_MGR, "disconnect driver extension ability[%{public}s] failed[%{public}d]", + Device::GetAbilityName(bundleInfo).c_str(), ret); + return ret; + } return EDM_OK; } @@ -281,20 +309,29 @@ int32_t ExtDeviceManager::RegisterDevice(shared_ptr devInfo) { BusType type = devInfo->GetBusType(); uint64_t deviceId = devInfo->GetDeviceId(); - + shared_ptr device; lock_guard lock(deviceMapMutex_); if (deviceMap_.find(type) != deviceMap_.end()) { unordered_map> &map = deviceMap_[type]; if (map.find(deviceId) != map.end()) { + device = map.find(deviceId)->second; + // device has been registered and do not need to connect again + if (device->GetDrvExtRemote() != nullptr) { + EDM_LOGI(MODULE_DEV_MGR, "device has been registered, deviceId is %{public}016" PRIx64 "", deviceId); + return EDM_OK; + } // device has been registered and need to connect EDM_LOGI(MODULE_DEV_MGR, "device has been registered, deviceId is %{public}016" PRIx64 "", deviceId); - return EDM_OK; } } EDM_LOGD(MODULE_DEV_MGR, "begin to register device, deviceId is %{public}016" PRIx64 "", deviceId); - + // device need to register + if (device == nullptr) { + device = make_shared(devInfo); + deviceMap_[type].emplace(deviceId, device); + EDM_LOGI(MODULE_DEV_MGR, "successfully registered device, deviceId = %{public}016" PRIx64 "", deviceId); + } // driver match - std::shared_ptr device = make_shared(devInfo); std::string bundleInfo = device->GetBundleInfo(); // if device does not have a matching driver, match driver here if (bundleInfo.empty()) { @@ -304,9 +341,6 @@ int32_t ExtDeviceManager::RegisterDevice(shared_ptr devInfo) device->AddBundleInfo(bundleInfo); } } - // register device - deviceMap_[type].emplace(deviceId, device); - EDM_LOGD(MODULE_DEV_MGR, "successfully registered device, deviceId = %{public}016" PRIx64 "", deviceId); unloadSelftimer_.Unregister(unloadSelftimerId_); // match driver failed, waitting to install driver package @@ -322,7 +356,7 @@ int32_t ExtDeviceManager::RegisterDevice(shared_ptr devInfo) ret); return EDM_NOK; } - EDM_LOGD(MODULE_DEV_MGR, "successfully match driver[%{public}s], deviceId is %{public}016" PRIx64 "", + EDM_LOGI(MODULE_DEV_MGR, "successfully match driver[%{public}s], deviceId is %{public}016" PRIx64 "", bundleInfo.c_str(), deviceId); return ret; diff --git a/services/native/driver_extension_manager/src/driver_ext_mgr.cpp b/services/native/driver_extension_manager/src/driver_ext_mgr.cpp index 94513a7..d45b083 100644 --- a/services/native/driver_extension_manager/src/driver_ext_mgr.cpp +++ b/services/native/driver_extension_manager/src/driver_ext_mgr.cpp @@ -23,6 +23,7 @@ #include "iservice_registry.h" #include "system_ability_definition.h" #include "usb_device_info.h" +#include "driver_extension_controller.h" namespace OHOS { namespace ExternalDeviceManager { @@ -36,6 +37,7 @@ void DriverExtMgr::OnStart() { int32_t ret; EDM_LOGI(MODULE_SERVICE, "hdf_ext_devmgr OnStart"); + BusExtensionCore::GetInstance().LoadBusExtensionLibs(); ret = DriverPkgManager::GetInstance().Init(); if (ret != EDM_OK) { EDM_LOGE(MODULE_SERVICE, "DriverPkgManager Init failed %{public}d", ret); @@ -61,6 +63,7 @@ void DriverExtMgr::OnStop() delete &(DriverPkgManager::GetInstance()); delete &(ExtDeviceManager::GetInstance()); delete &(BusExtensionCore::GetInstance()); + delete &(DriverExtensionController::GetInstance()); } int DriverExtMgr::Dump(int fd, const std::vector &args) -- Gitee