From a285b514e86dbcf5d22e5b7bb30a652730e5f85f Mon Sep 17 00:00:00 2001 From: li-tiangang4 Date: Mon, 1 Apr 2024 16:36:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=9B=E5=BB=BA=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E5=A4=B1=E8=B4=A5=E9=87=8D=E8=AF=95=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: li-tiangang4 --- .../src/distributed_input_node_manager.cpp | 12 ++++++++++++ services/source/inputinject/src/virtual_device.cpp | 2 ++ .../src/distributed_input_source_manager.cpp | 4 ++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index 9420b79..57c8528 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -31,6 +31,10 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { +namespace { + constexpr int32_t RETRY_MAX_TIMES = 3; + constexpr uint32_t SLEEP_TIME_US = 10 * 1000; +} DistributedInputNodeManager::DistributedInputNodeManager() : isInjectThreadCreated_(false), isInjectThreadRunning_(false), virtualTouchScreenFd_(UN_INIT_FD_VALUE) { @@ -354,6 +358,14 @@ int32_t DistributedInputNodeManager::CreateHandle(const InputDevice &inputDevice if (!virtualDevice->SetUp(inputDevice, devId, dhId)) { DHLOGE("could not create new virtual device\n"); + for (int32_t i = 0; i < RETRY_MAX_TIMES; ++i) { + if (virtualDevice->SetUp(inputDevice, devId, dhId)) { + DHLOGI("Create new virtual success"); + AddDeviceLocked(devId, inputDevice.descriptor, std::move(virtualDevice)); + return DH_SUCCESS; + } + usleep(SLEEP_TIME_US); + } return ERR_DH_INPUT_SERVER_SOURCE_CREATE_HANDLE_FAIL; } AddDeviceLocked(devId, inputDevice.descriptor, std::move(virtualDevice)); diff --git a/services/source/inputinject/src/virtual_device.cpp b/services/source/inputinject/src/virtual_device.cpp index 3f7ad2a..ecd2636 100644 --- a/services/source/inputinject/src/virtual_device.cpp +++ b/services/source/inputinject/src/virtual_device.cpp @@ -169,6 +169,8 @@ bool VirtualDevice::SetUp(const InputDevice &inputDevice, const std::string &dev char sysfsDeviceName[16] = {0}; if (ioctl(fd_, UI_GET_SYSNAME(sizeof(sysfsDeviceName)), sysfsDeviceName) < 0) { DHLOGE("Unable to get input device name"); + CloseFd(fd_); + return false; } DHLOGI("get input device name: %{public}s, fd: %{public}d", GetAnonyString(sysfsDeviceName).c_str(), fd_); return true; diff --git a/services/source/sourcemanager/src/distributed_input_source_manager.cpp b/services/source/sourcemanager/src/distributed_input_source_manager.cpp index 713cbfc..9b601bb 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -48,7 +48,7 @@ namespace DistributedInput { namespace { constexpr int32_t RAND_NUM_MIN = 0; constexpr int32_t RAND_NUM_MAX = 20; - constexpr int32_t SLEEP_TIME_US = 1000; + constexpr int32_t US_PER_MS = 1000; } REGISTER_SYSTEM_ABILITY_BY_ID(DistributedInputSourceManager, DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, true); @@ -272,7 +272,7 @@ int32_t DistributedInputSourceManager::RegisterDistributedHardware(const std::st DHLOGI("RegisterDistributedHardware called, deviceId: %{public}s, dhId: %{public}s, parameters: %{public}s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), SetAnonyId(parameters).c_str()); int32_t randNumber = GenRandInt(RAND_NUM_MIN, RAND_NUM_MAX); - usleep(randNumber * SLEEP_TIME_US); + usleep(randNumber * US_PER_MS); std::lock_guard lock(regDisHardwareMutex_); DInputClientRegistInfo info {devId, dhId, callback}; -- Gitee