diff --git a/common/include/constants_dinput.h b/common/include/constants_dinput.h index 93c98e5..db1f6a0 100644 --- a/common/include/constants_dinput.h +++ b/common/include/constants_dinput.h @@ -78,6 +78,8 @@ namespace DistributedInput { constexpr int32_t SESSION_WAIT_TIMEOUT_SECOND = 5; + constexpr int32_t EPOLL_WAITTIME = -1; + /* The input device is a keyboard or has buttons. */ constexpr uint32_t INPUT_DEVICE_CLASS_KEYBOARD = 0x00000001; diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index ce2afff..15d5514 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -381,7 +381,7 @@ void InputHub::GetDeviceHandler() int32_t InputHub::RefreshEpollItem(bool isSleep) { pendingEventIndex_ = 0; - int pollResult = epoll_wait(epollFd_, mPendingEventItems, EPOLL_MAX_EVENTS, 0); + int pollResult = epoll_wait(epollFd_, mPendingEventItems, EPOLL_MAX_EVENTS, EPOLL_WAITTIME); if (pollResult == 0) { // Timed out. pendingEventCount_ = 0; @@ -445,15 +445,31 @@ void InputHub::ScanInputDevices(const std::string& dirname) closedir(dir); } +void InputHub::CloseFd(int fd) +{ + if (fd < 0) { + DHLOGE("No fd need to be closed."); + return; + } + close(fd); + fd = -1; +} + +bool InputHub::IsDeviceRegistered(const std::string& devicePath) +{ + std::unique_lock deviceLock(devicesMutex_); + for (const auto& [deviceId, device] : devices_) { + if (device->path == devicePath) { + return true; // device was already registered + } + } + return false; +} + int32_t InputHub::OpenInputDeviceLocked(const std::string& devicePath) { - { - std::unique_lock deviceLock(devicesMutex_); - for (const auto& [deviceId, device] : devices_) { - if (device->path == devicePath) { - return DH_SUCCESS; // device was already registered - } - } + if (IsDeviceRegistered(devicePath)) { + return DH_SUCCESS; } std::unique_lock my_lock(operationMutex_); @@ -481,11 +497,13 @@ int32_t InputHub::OpenInputDeviceLocked(const std::string& devicePath) } if (count >= MAX_RETRY_COUNT) { DHLOGE("could not open %s, %s\n", devicePath.c_str(), ConvertErrNo().c_str()); + CloseFd(fd); return ERR_DH_INPUT_HUB_OPEN_DEVICEPATH_FAIL; } InputDevice identifier; if (QueryInputDeviceInfo(fd, identifier) < 0) { + CloseFd(fd); return ERR_DH_INPUT_HUB_QUERY_INPUT_DEVICE_INFO_FAIL; } GenerateDescriptor(identifier); @@ -496,6 +514,7 @@ int32_t InputHub::OpenInputDeviceLocked(const std::string& devicePath) RecordDeviceLog(deviceId, devicePath, identifier); if (MakeDevice(fd, std::move(device)) < 0) { + CloseFd(fd); return ERR_DH_INPUT_HUB_MAKE_DEVICE_FAIL; } @@ -522,14 +541,12 @@ int32_t InputHub::QueryInputDeviceInfo(int fd, InputDevice& identifier) int driverVersion; if (ioctl(fd, EVIOCGVERSION, &driverVersion)) { DHLOGE("could not get driver version for %s\n", ConvertErrNo().c_str()); - close(fd); return ERR_DH_INPUT_HUB_QUERY_INPUT_DEVICE_INFO_FAIL; } // Get device identifier. struct input_id inputId; if (ioctl(fd, EVIOCGID, &inputId)) { DHLOGE("could not get device input id for %s\n", ConvertErrNo().c_str()); - close(fd); return ERR_DH_INPUT_HUB_QUERY_INPUT_DEVICE_INFO_FAIL; } identifier.bus = inputId.bustype; diff --git a/common/include/input_hub.h b/common/include/input_hub.h index f2a7c21..d39b11d 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -121,6 +121,8 @@ private: Device* GetDeviceByPathLocked(const std::string& devicePath); Device* GetDeviceByFdLocked(int fd); Device* GetSupportDeviceByFd(int fd); + void CloseFd(int fd); + bool IsDeviceRegistered(const std::string& devicePath); bool ContainsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex); int64_t ProcessEventTimestamp(const input_event& event); diff --git a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp index 20311c7..f6fade2 100644 --- a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp +++ b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp @@ -550,8 +550,9 @@ void DistributedInputSinkManager::DInputSinkListener::CheckKeyState(const int32_ } break; } - if (fd > 0) { + if (fd >= 0) { close(fd); + fd = -1; } } diff --git a/services/transportbase/src/distributed_input_transport_base.cpp b/services/transportbase/src/distributed_input_transport_base.cpp index 0e9bb41..7544274 100644 --- a/services/transportbase/src/distributed_input_transport_base.cpp +++ b/services/transportbase/src/distributed_input_transport_base.cpp @@ -218,7 +218,7 @@ void DistributedInputTransportBase::StopSession(const std::string &remoteDevId) std::unique_lock sessionLock(operationMutex_); if (remoteDevSessionMap_.count(remoteDevId) == 0) { - DHLOGI("remoteDevSessionMap_ Not find remoteDevId: %s", GetAnonyString(remoteDevId).c_str()); + DHLOGE("remoteDevSessionMap not find remoteDevId: %s", GetAnonyString(remoteDevId).c_str()); return; } int32_t sessionId = remoteDevSessionMap_[remoteDevId]; @@ -288,15 +288,15 @@ int32_t DistributedInputTransportBase::OnSessionOpened(int32_t sessionId, int32_ char peerDevId[DEVICE_ID_SIZE_MAX] = {0}; int32_t ret = GetMySessionName(sessionId, mySessionName, sizeof(mySessionName)); if (ret != DH_SUCCESS) { - DHLOGI("get my session name failed, session id is %d", sessionId); + DHLOGE("get my session name failed, session id is %d", sessionId); } ret = GetPeerSessionName(sessionId, peerSessionName, sizeof(peerSessionName)); if (ret != DH_SUCCESS) { - DHLOGI("get peer session name failed, session id is %d", sessionId); + DHLOGE("get peer session name failed, session id is %d", sessionId); } ret = GetPeerDeviceId(sessionId, peerDevId, sizeof(peerDevId)); if (ret != DH_SUCCESS) { - DHLOGI("get peer device id failed, session id is %d", sessionId); + DHLOGE("get peer device id failed, session id is %d", sessionId); } { @@ -338,13 +338,13 @@ void DistributedInputTransportBase::OnSessionClosed(int32_t sessionId) channelStatusMap_.erase(deviceId); if (sinkCallback_ == nullptr) { - DHLOGI("sinkCallback is nullptr."); + DHLOGE("sinkCallback is nullptr."); return; } sinkCallback_->NotifySessionClosed(sessionId); if (srcCallback_ == nullptr) { - DHLOGI("srcCallback is nullptr."); + DHLOGE("srcCallback is nullptr."); return; } srcCallback_->NotifySessionClosed(); @@ -406,7 +406,7 @@ void DistributedInputTransportBase::HandleSession(int32_t sessionId, const std:: DHLOGI("HandleSession cmdType %u.", cmdType); if (cmdType < TRANS_MSG_SRC_SINK_SPLIT) { if (srcCallback_ == nullptr) { - DHLOGI("srcCallback is nullptr."); + DHLOGE("srcCallback is nullptr."); return; } DHLOGI("HandleSession to source."); @@ -415,7 +415,7 @@ void DistributedInputTransportBase::HandleSession(int32_t sessionId, const std:: } if (cmdType > TRANS_MSG_SRC_SINK_SPLIT) { if (sinkCallback_ == nullptr) { - DHLOGI("sinkCallback is nullptr."); + DHLOGE("sinkCallback is nullptr."); return; } DHLOGI("HandleSession to sink.");