diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index 9420b7922349e6a6ff8b47d637ffbc54fd0ad232..57c8528d130b5021d78396339f6b7cec6d4e9b4f 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 3f7ad2a9381b05fc49d1b6dc5df466e437bd220d..ecd2636dd0a57907d3cb5cf9e741f1b9b5ca5b58 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 713cbfc49090635c1383ba80fab95cc9cb00f456..9b601bb4c75da507bca81e344ccd29783e322ac5 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};