From c3e42936d070070a5e1bbc951f5c996f8c96c6e6 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Tue, 25 Jul 2023 22:42:49 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liqiao49 --- common/include/constants_dinput.h | 2 - common/include/input_hub.cpp | 83 +------ common/include/input_hub.h | 5 +- .../include/distributed_input_collector.h | 2 +- .../src/distributed_input_collector.cpp | 16 +- .../include/distributed_input_sink_manager.h | 8 - .../src/distributed_input_sink_manager.cpp | 148 +++---------- .../distributed_input_sinkmanager_test.cpp | 12 - .../include/distributed_input_inject.h | 2 +- .../include/distributed_input_node_manager.h | 8 +- .../src/distributed_input_inject.cpp | 4 +- .../src/distributed_input_node_manager.cpp | 96 ++------ .../distributed_input_sourceinject_test.cpp | 4 +- .../distributed_input_source_manager.h | 1 - .../src/distributed_input_source_manager.cpp | 22 +- .../distributed_input_sourcemanager_test.cpp | 8 - .../distributed_input_source_transport.h | 2 - .../distributed_input_source_transport.cpp | 22 +- services/state/BUILD.gn | 3 + services/state/include/dinput_state.h | 26 ++- services/state/src/dinput_state.cpp | 206 +++++++++++------- utils/include/dinput_utils_tool.h | 7 +- utils/src/dinput_utils_tool.cpp | 101 ++++++++- 23 files changed, 328 insertions(+), 460 deletions(-) diff --git a/common/include/constants_dinput.h b/common/include/constants_dinput.h index 1fc7b40..04fbb9c 100644 --- a/common/include/constants_dinput.h +++ b/common/include/constants_dinput.h @@ -195,8 +195,6 @@ namespace DistributedInput { constexpr const char* CHECK_KEY_STATUS_THREAD_NAME = "checkKeyStatus"; - constexpr const char* KEYBOARD_UP_INJECT_THREAD_NAME = "KeyboardUpInject"; - constexpr int32_t LOG_MAX_LEN = 4096; constexpr uint32_t SCREEN_ID_DEFAULT = 0; diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index c70c431..41bbac7 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -40,8 +39,6 @@ namespace DistributedHardware { namespace DistributedInput { namespace { const uint32_t SLEEP_TIME_US = 100 * 1000; -const uint32_t ERROR_MSG_MAX_LEN = 256; -constexpr int32_t MAX_RETRY_COUNT = 10; } InputHub::InputHub() : epollFd_(0), iNotifyFd_(0), inputWd_(0), needToScanDevices_(true), nextDeviceId_(1), @@ -56,14 +53,6 @@ InputHub::~InputHub() Release(); } -static std::string ConvertErrNo() -{ - char errMsg[ERROR_MSG_MAX_LEN] = {0}; - strerror_r(errno, errMsg, ERROR_MSG_MAX_LEN); - std::string errNoMsg(errMsg); - return errNoMsg; -} - int32_t InputHub::Initialize() { epollFd_ = epoll_create1(EPOLL_CLOEXEC); @@ -430,41 +419,14 @@ std::vector InputHub::GetAllInputDevices() return vecDevice; } -void InputHub::ScanInputDevices(const std::string& dirname) +void InputHub::ScanInputDevices(const std::string& dirName) { - DIR *dir; - struct dirent *de; - dir = opendir(dirname.c_str()); - if (dir == nullptr) { - DHLOGE("error opendir /dev/input :%{public}s\n", ConvertErrNo().c_str()); - return; + DHLOGI("ScanInputDevices enter, dirName %s.", dirName.c_str()); + std::vector vecInputDevPath; + ScanInputDevicesPath(dirName, vecInputDevPath); + for (auto &tempPath: vecInputDevPath) { + OpenInputDeviceLocked(tempPath); } - size_t dirNameFirstPos = 0; - size_t dirNameSecondPos = 1; - size_t dirNameThirdPos = 2; - while ((de = readdir(dir))) { - /* - * The maximum value of d_name defined in the linux kernel is 260. Therefore, - * The d_name length does not need to be verified. - */ - if (de->d_name[dirNameFirstPos] == '.' && (de->d_name[dirNameSecondPos] == '\0' || - (de->d_name[dirNameSecondPos] == '.' && de->d_name[dirNameThirdPos] == '\0'))) { - continue; - } - std::string devName = dirname + "/" + std::string(de->d_name); - OpenInputDeviceLocked(devName); - } - 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) @@ -486,30 +448,9 @@ int32_t InputHub::OpenInputDeviceLocked(const std::string& devicePath) std::unique_lock my_lock(operationMutex_); DHLOGI("Opening device: %s", devicePath.c_str()); - chmod(devicePath.c_str(), S_IWRITE | S_IREAD); - char canonicalDevicePath[PATH_MAX + 1] = {0x00}; - if (devicePath.length() == 0 || devicePath.length() > PATH_MAX || - realpath(devicePath.c_str(), canonicalDevicePath) == nullptr) { - DHLOGE("path check fail, error path: %s", devicePath.c_str()); - return ERR_DH_INPUT_HUB_OPEN_DEVICEPATH_FAIL; - } - struct stat s; - if ((stat(canonicalDevicePath, &s) == 0) && (s.st_mode & S_IFDIR)) { - DHLOGI("path: %s is a dir.", devicePath.c_str()); - return DH_SUCCESS; - } - - int fd = open(canonicalDevicePath, O_RDWR | O_CLOEXEC | O_NONBLOCK); - int32_t count = 0; - while ((fd < 0) && (count < MAX_RETRY_COUNT)) { - ++count; - usleep(SLEEP_TIME_US); - fd = open(canonicalDevicePath, O_RDWR | O_CLOEXEC | O_NONBLOCK); - DHLOGE("could not open %s, %s; retry %d\n", devicePath.c_str(), ConvertErrNo().c_str(), count); - } - if (count >= MAX_RETRY_COUNT) { - DHLOGE("could not open %s, %s\n", devicePath.c_str(), ConvertErrNo().c_str()); - CloseFd(fd); + int fd = OpenInputDeviceFdByPath(devicePath); + if (fd == -1) { + DHLOGE("The fd open failed, devicePath %s.", devicePath.c_str()); return ERR_DH_INPUT_HUB_OPEN_DEVICEPATH_FAIL; } @@ -1140,10 +1081,10 @@ void InputHub::GetShareMousePathByDhId(std::vector dhIds, std::stri } } -void InputHub::GetShareKeyboardPathByDhId(std::vector dhIds, std::vector &shareDhidsPath, +void InputHub::GetShareKeyboardPathsByDhIds(std::vector dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds) { - DHLOGI("GetShareKeyboardPathByDhId: devices_.size:%d,", devices_.size()); + DHLOGI("GetShareKeyboardPathsByDhIds: devices_.size:%d,", devices_.size()); std::unique_lock deviceLock(devicesMutex_); for (auto dhId_ : dhIds) { for (const auto &[id, device] : devices_) { @@ -1156,7 +1097,7 @@ void InputHub::GetShareKeyboardPathByDhId(std::vector dhIds, std::v if ((device->identifier.descriptor == dhId_) && ((device->classes & INPUT_DEVICE_CLASS_KEYBOARD) != 0)) { shareDhIds.push_back(dhId_); - shareDhidsPath.push_back(device->path); + shareDhidsPaths.push_back(device->path); } } } diff --git a/common/include/input_hub.h b/common/include/input_hub.h index 408327a..a8f7aa3 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -53,10 +53,10 @@ public: void GetDevicesInfoByType(const uint32_t inputTypes, std::map &datas); void GetDevicesInfoByDhId(std::vector dhidsVec, std::map &datas); void GetShareMousePathByDhId(std::vector dhIds, std::string &path, std::string &dhId); - void GetShareKeyboardPathByDhId(std::vector dhIds, std::vector &shareDhidsPath, + void GetShareKeyboardPathsByDhIds(std::vector dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds); bool IsAllDevicesStoped(); - void ScanInputDevices(const std::string& dirname); + void ScanInputDevices(const std::string& dirName); private: struct Device { @@ -123,7 +123,6 @@ 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); diff --git a/services/sink/inputcollector/include/distributed_input_collector.h b/services/sink/inputcollector/include/distributed_input_collector.h index c4c9ef1..847704a 100644 --- a/services/sink/inputcollector/include/distributed_input_collector.h +++ b/services/sink/inputcollector/include/distributed_input_collector.h @@ -43,7 +43,7 @@ public: AffectDhIds SetSharingTypes(bool enabled, const uint32_t &inputType); AffectDhIds SetSharingDhIds(bool enabled, std::vector dhIds); void GetMouseNodePath(std::vector dhIds, std::string &mouseNodePath, std::string &dhid); - void GetKeyboardNodePath(std::vector dhIds, std::vector &shareDhidsPaths, + void GetShareKeyboardPathsByDhIds(std::vector dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds); // false for sharing device exist, true for all devices stop sharing bool IsAllDevicesStoped(); diff --git a/services/sink/inputcollector/src/distributed_input_collector.cpp b/services/sink/inputcollector/src/distributed_input_collector.cpp index 0725244..8b91e99 100644 --- a/services/sink/inputcollector/src/distributed_input_collector.cpp +++ b/services/sink/inputcollector/src/distributed_input_collector.cpp @@ -175,7 +175,7 @@ void DistributedInputCollector::ReportDhIdSharingState(const AffectDhIds &dhIds) { std::lock_guard lock(sharingDhIdListenerMtx_); if (sharingDhIdListeners_.size() == 0) { - DHLOGI("sharingDhIdListeners_ is null, can not report sharing dhid"); + DHLOGE("sharingDhIdListeners is null, can not report sharing dhid"); return; } @@ -208,26 +208,26 @@ void DistributedInputCollector::GetMouseNodePath( std::vector dhIds, std::string &mouseNodePath, std::string &dhid) { if (inputHub_ == nullptr) { - DHLOGI("inputHub is nullptr!"); + DHLOGE("inputHub is nullptr!"); return; } inputHub_->GetShareMousePathByDhId(dhIds, mouseNodePath, dhid); } -void DistributedInputCollector::GetKeyboardNodePath( - std::vector dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds) +void DistributedInputCollector::GetShareKeyboardPathsByDhIds(std::vector dhIds, + std::vector &shareDhidsPaths, std::vector &shareDhIds) { if (inputHub_ == nullptr) { - DHLOGI("inputHub is nullptr!"); + DHLOGE("inputHub is nullptr!"); return; } - inputHub_->GetShareKeyboardPathByDhId(dhIds, shareDhidsPaths, shareDhIds); + inputHub_->GetShareKeyboardPathsByDhIds(dhIds, shareDhidsPaths, shareDhIds); } bool DistributedInputCollector::IsAllDevicesStoped() { if (inputHub_ == nullptr) { - DHLOGI("inputHub is nullptr!"); + DHLOGE("inputHub is nullptr!"); return false; } return inputHub_->IsAllDevicesStoped(); @@ -245,7 +245,7 @@ void DistributedInputCollector::GetDeviceInfoByType(const uint32_t inputTypes, s std::string>& deviceInfo) { if (inputHub_ == nullptr) { - DHLOGI("inputHub is nullptr!"); + DHLOGE("inputHub is nullptr!"); return; } inputHub_->GetDevicesInfoByType(inputTypes, deviceInfo); diff --git a/services/sink/sinkmanager/include/distributed_input_sink_manager.h b/services/sink/sinkmanager/include/distributed_input_sink_manager.h index 0fa973c..aca1378 100644 --- a/services/sink/sinkmanager/include/distributed_input_sink_manager.h +++ b/services/sink/sinkmanager/include/distributed_input_sink_manager.h @@ -79,14 +79,6 @@ public: private: DistributedInputSinkManager *sinkManagerObj_; - static inline int BitIsSet(const unsigned long *array, int bit) - { - return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS))); - } - void SleepTimeMs(); - void StringSplit(const std::string &str, const char split, std::vector &vecStr); - void CreateCheckThread(const int32_t &sessionId, const std::string &strDhids); - void CheckKeyState(const int32_t &sessionId, const std::string &strDhids); }; class ProjectWindowListener : public PublisherListenerStub { diff --git a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp index dbafba9..cc6930b 100644 --- a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp +++ b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp @@ -210,7 +210,10 @@ void DistributedInputSinkManager::DInputSinkListener::OnStartRemoteInput( deviceInfos); for (auto deviceInfo : deviceInfos) { DHLOGI("deviceInfo dhId, %s", GetAnonyString(deviceInfo.second).c_str()); - CreateCheckThread(sessionId, deviceInfo.second); + std::vector vecStr; + StringSplitToVector(deviceInfo.second, INPUT_STRING_SPLIT_POINT, vecStr); + DInputState::GetInstance().AddDhids(vecStr); + DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_IN, sessionId); } } } @@ -268,17 +271,13 @@ void DistributedInputSinkManager::DInputSinkListener::OnStartRemoteInputDhid(con return; } - CreateCheckThread(sessionId, strDhids); - // add the dhids - if (startRes == DH_SUCCESS) { - std::vector vecStr; - StringSplit(strDhids, INPUT_STRING_SPLIT_POINT, vecStr); - AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, vecStr); - sinkManagerObj_->StoreStartDhids(sessionId, affDhIds.sharingDhIds); - DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); - DInputState::GetInstance().AddDhids(vecStr); - DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_OUT); - } + std::vector vecStr; + StringSplitToVector(strDhids, INPUT_STRING_SPLIT_POINT, vecStr); + DInputState::GetInstance().AddDhids(vecStr); + DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_OUT, sessionId); + AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, vecStr); + sinkManagerObj_->StoreStartDhids(sessionId, affDhIds.sharingDhIds); + DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); } void DistributedInputSinkManager::DInputSinkListener::OnStopRemoteInputDhid(const int32_t &sessionId, @@ -287,7 +286,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnStopRemoteInputDhid(cons DHLOGI("OnStopRemoteInputDhid called, sessionId: %d", sessionId); std::vector stopIndeedDhIds; std::vector stopOnCmdDhIds; - StringSplit(strDhids, INPUT_STRING_SPLIT_POINT, stopOnCmdDhIds); + StringSplitToVector(strDhids, INPUT_STRING_SPLIT_POINT, stopOnCmdDhIds); sinkManagerObj_->DeleteStopDhids(sessionId, stopOnCmdDhIds, stopIndeedDhIds); (void)DistributedInputCollector::GetInstance().SetSharingDhIds(false, stopIndeedDhIds); AffectDhIds stopIndeedOnes; @@ -295,7 +294,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnStopRemoteInputDhid(cons DistributedInputCollector::GetInstance().ReportDhIdSharingState(stopIndeedOnes); DInputState::GetInstance().AddDhids(stopOnCmdDhIds); - DInputState::GetInstance().SwitchState(stopOnCmdDhIds, DhidState::THROUGH_IN); + DInputState::GetInstance().SwitchState(stopOnCmdDhIds, DhidState::THROUGH_IN, -1); if (DistributedInputCollector::GetInstance().IsAllDevicesStoped()) { DHLOGE("All dhid stop sharing, sessionId: %d is closed.", sessionId); @@ -341,18 +340,13 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStartDhidRemoteInpu return; } - CreateCheckThread(toSinkSessionId, strDhids); - - // add the dhids - if (startRes == DH_SUCCESS) { - std::vector vecStr; - StringSplit(strDhids, INPUT_STRING_SPLIT_POINT, vecStr); - AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, vecStr); - sinkManagerObj_->StoreStartDhids(toSinkSessionId, affDhIds.sharingDhIds); - DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); - DInputState::GetInstance().AddDhids(vecStr); - DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_OUT); - } + std::vector vecStr; + StringSplitToVector(strDhids, INPUT_STRING_SPLIT_POINT, vecStr); + DInputState::GetInstance().AddDhids(vecStr); + DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_OUT, toSinkSessionId); + AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, vecStr); + sinkManagerObj_->StoreStartDhids(toSinkSessionId, affDhIds.sharingDhIds); + DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); } void DistributedInputSinkManager::DInputSinkListener::OnRelayStopDhidRemoteInput(const int32_t &toSrcSessionId, @@ -361,7 +355,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStopDhidRemoteInput DHLOGI("onRelayStopDhidRemoteInput called, toSinkSessionId: %d", toSinkSessionId); std::vector stopIndeedDhIds; std::vector stopOnCmdDhIds; - StringSplit(strDhids, INPUT_STRING_SPLIT_POINT, stopOnCmdDhIds); + StringSplitToVector(strDhids, INPUT_STRING_SPLIT_POINT, stopOnCmdDhIds); sinkManagerObj_->DeleteStopDhids(toSinkSessionId, stopOnCmdDhIds, stopIndeedDhIds); AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(false, stopIndeedDhIds); AffectDhIds stopIndeedOnes; @@ -369,7 +363,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStopDhidRemoteInput DistributedInputCollector::GetInstance().ReportDhIdSharingState(stopIndeedOnes); DInputState::GetInstance().AddDhids(stopOnCmdDhIds); - DInputState::GetInstance().SwitchState(stopOnCmdDhIds, DhidState::THROUGH_IN); + DInputState::GetInstance().SwitchState(stopOnCmdDhIds, DhidState::THROUGH_IN, -1); if (DistributedInputCollector::GetInstance().IsAllDevicesStoped()) { DHLOGE("All dhid stop sharing, sessionId: %d is closed.", toSinkSessionId); @@ -442,7 +436,10 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStartTypeRemoteInpu deviceInfos); for (auto deviceInfo : deviceInfos) { DHLOGI("deviceInfo dhId, %s", GetAnonyString(deviceInfo.second).c_str()); - CreateCheckThread(toSinkSessionId, deviceInfo.second); + std::vector vecStr; + StringSplitToVector(deviceInfo.second, INPUT_STRING_SPLIT_POINT, vecStr); + DInputState::GetInstance().AddDhids(vecStr); + DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_IN, toSinkSessionId); } } @@ -482,99 +479,6 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStopTypeRemoteInput } } -void DistributedInputSinkManager::DInputSinkListener::StringSplit(const std::string &str, const char split, - std::vector &vecStr) -{ - if (str.empty()) { - DHLOGE("param str is error."); - return; - } - std::string strTmp = str + split; - size_t pos = strTmp.find(split); - while (pos != strTmp.npos) { - std::string matchTmp = strTmp.substr(0, pos); - vecStr.push_back(matchTmp); - strTmp = strTmp.substr(pos + 1, strTmp.size()); - pos = strTmp.find(split); - } -} - -void DistributedInputSinkManager::DInputSinkListener::SleepTimeMs() -{ - std::this_thread::sleep_for(std::chrono::milliseconds(READ_SLEEP_TIME_MS)); -} - -void DistributedInputSinkManager::DInputSinkListener::CreateCheckThread(const int32_t &sessionId, - const std::string &strDhids) -{ - std::thread checkKeyStateThread = - std::thread(&DistributedInputSinkManager::DInputSinkListener::CheckKeyState, this, sessionId, strDhids); - int32_t ret = pthread_setname_np(checkKeyStateThread.native_handle(), CHECK_KEY_STATUS_THREAD_NAME); - if (ret != 0) { - DHLOGE("CreateCheckThread setname failed."); - } - checkKeyStateThread.detach(); -} - -void DistributedInputSinkManager::DInputSinkListener::CheckKeyState(const int32_t &sessionId, - const std::string &strDhids) -{ - std::vector vecStr; - StringSplit(strDhids, INPUT_STRING_SPLIT_POINT, vecStr); - std::string mouseNodePath; - std::string dhid; - DistributedInputCollector::GetInstance().GetMouseNodePath(vecStr, mouseNodePath, dhid); - - char canonicalPath[PATH_MAX + 1] = {0x00}; - if (mouseNodePath.length() == 0 || mouseNodePath.length() > PATH_MAX || - realpath(mouseNodePath.c_str(), canonicalPath) == nullptr) { - DHLOGE("mouse Nodepath check fail, error path: %s", mouseNodePath.c_str()); - return; - } - - int fd = open(canonicalPath, O_RDONLY | O_NONBLOCK); - if (fd < 0) { - DHLOGE("open mouse Node Path error:", errno); - return; - } - - uint32_t count = 0; - int leftKeyVal = 0; - int rightKeyVal = 0; - int midKeyVal = 0; - unsigned long keystate[NLONGS(KEY_CNT)] = { 0 }; - while (true) { - if (count > READ_RETRY_MAX) { - break; - } - // Query all key state - int rc = ioctl(fd, EVIOCGKEY(sizeof(keystate)), keystate); - if (rc < 0) { - DHLOGE("read all key state failed, rc=%d ", rc); - count += 1; - SleepTimeMs(); - continue; - } - leftKeyVal = BitIsSet(keystate, BTN_LEFT); - if (leftKeyVal != 0) { - DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhid, BTN_LEFT); - } - rightKeyVal = BitIsSet(keystate, BTN_RIGHT); - if (rightKeyVal != 0) { - DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhid, BTN_RIGHT); - } - midKeyVal = BitIsSet(keystate, BTN_MIDDLE); - if (midKeyVal != 0) { - DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhid, BTN_MIDDLE); - } - break; - } - if (fd >= 0) { - close(fd); - fd = -1; - } -} - bool DistributedInputSinkManager::IsStopDhidOnCmdStillNeed(int32_t sessionId, const std::string &stopDhId) { for (auto sessionDhid : sharingDhIdsMap_) { diff --git a/services/sink/sinkmanager/test/sinkmanagerunittest/distributed_input_sinkmanager_test.cpp b/services/sink/sinkmanager/test/sinkmanagerunittest/distributed_input_sinkmanager_test.cpp index 8820003..c92dcb7 100644 --- a/services/sink/sinkmanager/test/sinkmanagerunittest/distributed_input_sinkmanager_test.cpp +++ b/services/sink/sinkmanager/test/sinkmanagerunittest/distributed_input_sinkmanager_test.cpp @@ -186,18 +186,6 @@ HWTEST_F(DistributedInputSinkManagerTest, RegisterGetSinkScreenInfosCallback_02, EXPECT_EQ(DH_SUCCESS, ret); } -HWTEST_F(DistributedInputSinkManagerTest, StringSplit_01, testing::ext::TestSize.Level1) -{ - char splitPoint = '.'; - std::string str = ""; - std::vector vecStr; - sinkManager_->statuslistener_->StringSplit(str, splitPoint, vecStr); - - str = "123,123,123,123"; - sinkManager_->statuslistener_->StringSplit(str, splitPoint, vecStr); - EXPECT_NE(0, vecStr.size()); -} - HWTEST_F(DistributedInputSinkManagerTest, OnMessage_01, testing::ext::TestSize.Level1) { int32_t ret = sinkManager_->Init(); diff --git a/services/source/inputinject/include/distributed_input_inject.h b/services/source/inputinject/include/distributed_input_inject.h index 3022118..5846970 100644 --- a/services/source/inputinject/include/distributed_input_inject.h +++ b/services/source/inputinject/include/distributed_input_inject.h @@ -56,7 +56,7 @@ public: const std::string &sinkNodeDesc); void SyncNodeOfflineInfo(const std::string &srcDevId, const std::string &sinkDevId, const std::string &sinkNodeId); - void GetVirtualKeyboardPathByDhId(const std::vector &dhIds, std::vector &shareDhidsPaths, + void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds); private: diff --git a/services/source/inputinject/include/distributed_input_node_manager.h b/services/source/inputinject/include/distributed_input_node_manager.h index 63e6dd7..48b32cb 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -38,10 +38,9 @@ public: DistributedInputNodeManager(); ~DistributedInputNodeManager(); - int32_t OpenDevicesNode(const std::string& devId, const std::string& dhId, - const std::string& parameters); + int32_t OpenDevicesNode(const std::string& devId, const std::string& dhId, const std::string& parameters); - int32_t getDevice(const std::string& dhId, VirtualDevice*& device); + int32_t GetDevice(const std::string& dhId, VirtualDevice*& device); void ReportEvent(const RawEvent rawEvent); int32_t CloseDeviceLocked(const std::string& dhId); void StartInjectThread(); @@ -56,7 +55,7 @@ public: void GetDevicesInfoByDhId(std::vector dhidsVec, std::map &datas); void ProcessInjectEvent(const std::shared_ptr &rawEvent); - void GetVirtualKeyboardPathByDhId(const std::vector &dhIds, std::vector &shareDhidsPaths, + void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds); private: void AddDeviceLocked(const std::string& dhId, std::unique_ptr device); @@ -67,7 +66,6 @@ private: void ScanSinkInputDevices(const std::string& dirName); void OpenInputDevice(const std::string& devicePath); - int OpenInputDeviceFdByPath(std::string& canonicalDevicePath); bool IsVirtualDev(int fd); bool GetDevDhIdByFd(int fd, std::string& dhId, std::string& physicalPath); void SetPathForDevMap(std::string& dhId, const std::string& devicePath); diff --git a/services/source/inputinject/src/distributed_input_inject.cpp b/services/source/inputinject/src/distributed_input_inject.cpp index 249a299..1de63d9 100644 --- a/services/source/inputinject/src/distributed_input_inject.cpp +++ b/services/source/inputinject/src/distributed_input_inject.cpp @@ -256,14 +256,14 @@ int32_t DistributedInputInject::GetVirtualTouchScreenFd() return inputNodeManager_->GetVirtualTouchScreenFd(); } -void DistributedInputInject::GetVirtualKeyboardPathByDhId(const std::vector &dhIds, +void DistributedInputInject::GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds) { if (inputNodeManager_ == nullptr) { DHLOGE("inputNodeManager is nullptr"); return; } - inputNodeManager_->GetVirtualKeyboardPathByDhId(dhIds, shareDhidsPaths, shareDhIds); + inputNodeManager_->GetVirtualKeyboardPathsByDhIds(dhIds, shareDhidsPaths, shareDhIds); } } // namespace DistributedInput } // namespace DistributedHardware diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index 4a7ec00..1f053dd 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -18,11 +18,7 @@ #include #include -#include #include -#include -#include -#include #include "softbus_bus_center.h" @@ -35,9 +31,6 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { -const uint32_t SLEEP_TIME_US = 100 * 1000; -const uint32_t ERROR_MSG_MAX_LEN = 256; -constexpr int32_t MAX_RETRY_COUNT = 10; DistributedInputNodeManager::DistributedInputNodeManager() : isInjectThreadCreated_(false), isInjectThreadRunning_(false), inputHub_(std::make_unique()), virtualTouchScreenFd_(UN_INIT_FD_VALUE) { @@ -135,45 +128,14 @@ void DistributedInputNodeManager::VerifyInputDevice(const nlohmann::json& inputD } } -static std::string ConvertErrNo() -{ - char errMsg[ERROR_MSG_MAX_LEN] = {0}; - strerror_r(errno, errMsg, ERROR_MSG_MAX_LEN); - std::string errNoMsg(errMsg); - return errNoMsg; -} - -void CloseFd(int fd) -{ - if (fd < 0) { - DHLOGE("No fd need to beclosed."); - return; - } - close(fd); - fd = -1; -} - void DistributedInputNodeManager::ScanSinkInputDevices(const std::string& dirName) { - DIR *dir; - struct dirent *de; - dir = opendir(dirName.c_str()); - if (dir == nullptr) { - DHLOGE("error opendir /dev/input :%{public}s\n", ConvertErrNo().c_str()); - return; + DHLOGI("ScanSinkInputDevices enter, dirName %s.", dirName.c_str()); + std::vector vecInputDevPath; + ScanInputDevicesPath(dirName, vecInputDevPath); + for (auto &tempPath: vecInputDevPath) { + OpenInputDevice(tempPath); } - size_t dirNameFirstPos = 0; - size_t dirNameSecondPos = 1; - size_t dirNameThirdPos = 2; - while ((de = readdir(dir))) { - if (de->d_name[dirNameFirstPos] == '.' && (de->d_name[dirNameSecondPos] == '\0' || - (de->d_name[dirNameSecondPos] == '.' && de->d_name[dirNameThirdPos] == '\0'))) { - continue; - } - std::string tmpDevName = dirName + "/" + std::string(de->d_name); - OpenInputDevice(tmpDevName); - } - closedir(dir); } bool DistributedInputNodeManager::IsVirtualDev(int fd) @@ -199,19 +161,19 @@ bool DistributedInputNodeManager::GetDevDhIdByFd(int fd, std::string& dhId, std: { char buffer[256] = {0}; if (ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) { - DHLOGE("Could not get device physicalPath for %s", ConvertErrNo().c_str()); + DHLOGE("Could not get device physicalPath for %s.", ConvertErrNo().c_str()); return false; } buffer[sizeof(buffer) - 1] = '\0'; physicalPath = buffer; - DHLOGD("GetDevDhIdFd physicalPath: %s", physicalPath.c_str()); - dhId = physicalPath.substr(physicalPath.find("Input_")); + DHLOGD("GetDevDhIdByFd physicalPath %s.", physicalPath.c_str()); + dhId = physicalPath.substr(physicalPath.find(DH_ID_PREFIX)); if (dhId.size() == 0) { DHLOGE("Get dev dhid failed."); return false; } - DHLOGD("Device dhId %s", GetAnonyString(dhId).c_str()); + DHLOGD("Device dhId %s.", GetAnonyString(dhId).c_str()); return true; } @@ -234,49 +196,23 @@ void DistributedInputNodeManager::OpenInputDevice(const std::string& devicePath) DHLOGI("Opening input device path: %s", devicePath.c_str()); std::string dhId; std::string physicalPath; - struct stat s; - int fd = -1; - chmod(devicePath.c_str(), S_IREAD); - char canonicalDevicePath[PATH_MAX + 1] = {0x00}; - - if (devicePath.length() == 0 || devicePath.length() > PATH_MAX || - realpath(devicePath.c_str(), canonicalDevicePath) == nullptr) { - DHLOGE("path check fail, error path: %s", devicePath.c_str()); - return; - } - if ((stat(canonicalDevicePath, &s) == 0) && (s.st_mode & S_IFDIR)) { - DHLOGI("path: %s is a dir.", devicePath.c_str()); - return; - } - fd = open(canonicalDevicePath, O_RDWR | O_CLOEXEC | O_NONBLOCK); - int32_t count = 0; - while ((fd < 0) && (count < MAX_RETRY_COUNT)) { - ++count; - usleep(SLEEP_TIME_US); - fd = open(canonicalDevicePath, O_RDWR | O_CLOEXEC | O_NONBLOCK); - DHLOGE("could not open %s, %s; retry %d\n", devicePath.c_str(), ConvertErrNo().c_str(), count); - } - if (count >= MAX_RETRY_COUNT) { - DHLOGE("could not open %s, %s\n", devicePath.c_str(), ConvertErrNo().c_str()); - CloseFd(fd); - return; - } + int fd = OpenInputDeviceFdByPath(devicePath); if (fd == -1) { - DHLOGE("The fd open failed, devicePath %s\n", devicePath.c_str()); + DHLOGE("The fd open failed, devicePath %s.", devicePath.c_str()); return; } if (IsVirtualDev(fd) == false) { - DHLOGE("The dev not virtual, devicePath %s\n", devicePath.c_str()); + DHLOGE("The dev not virtual, devicePath %s.", devicePath.c_str()); return; } if (GetDevDhIdByFd(fd, dhId, physicalPath) == false) { - DHLOGE("Get dev dhid failed, devicePath %s\n", devicePath.c_str()); + DHLOGE("Get dev dhid failed, devicePath %s.", devicePath.c_str()); return; } SetPathForDevMap(dhId, devicePath); } -void DistributedInputNodeManager::GetVirtualKeyboardPathByDhId(const std::vector &dhIds, +void DistributedInputNodeManager::GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds) { for (auto dhId_ : dhIds) { @@ -370,7 +306,7 @@ int32_t DistributedInputNodeManager::CloseDeviceLocked(const std::string &dhId) return ERR_DH_INPUT_SERVER_SOURCE_CLOSE_DEVICE_FAIL; } -int32_t DistributedInputNodeManager::getDevice(const std::string& dhId, VirtualDevice*& device) +int32_t DistributedInputNodeManager::GetDevice(const std::string& dhId, VirtualDevice*& device) { std::lock_guard lock(virtualDeviceMapMutex_); auto iter = virtualDeviceMap_.find(dhId); @@ -455,7 +391,7 @@ void DistributedInputNodeManager::ProcessInjectEvent(const std::shared_ptrwhen); VirtualDevice* device = nullptr; - if (getDevice(dhId, device) < 0) { + if (GetDevice(dhId, device) < 0) { DHLOGE("could not find the device"); return; } diff --git a/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp b/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp index ce5bf02..0c929bb 100644 --- a/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp +++ b/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp @@ -413,11 +413,11 @@ HWTEST_F(DistributedInputSourceInjectTest, GetVirtualTouchScreenFd_003, testing: EXPECT_NE(-1, ret); } -HWTEST_F(DistributedInputSourceInjectTest, getDevice_001, testing::ext::TestSize.Level1) +HWTEST_F(DistributedInputSourceInjectTest, GetDevice_001, testing::ext::TestSize.Level1) { std::string dhId = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; VirtualDevice* device = nullptr; - int32_t ret = DistributedInputInject::GetInstance().inputNodeManager_->getDevice(dhId, device); + int32_t ret = DistributedInputInject::GetInstance().inputNodeManager_->GetDevice(dhId, device); EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_GET_DEVICE_FAIL, ret); } diff --git a/services/source/sourcemanager/include/distributed_input_source_manager.h b/services/source/sourcemanager/include/distributed_input_source_manager.h index f7cda1e..9611929 100644 --- a/services/source/sourcemanager/include/distributed_input_source_manager.h +++ b/services/source/sourcemanager/include/distributed_input_source_manager.h @@ -491,7 +491,6 @@ private: int32_t RelayStopRemoteInputByDhid(const std::string &srcId, const std::string &sinkId, const std::vector &dhIds, sptr callback); bool IsStringDataSame(const std::vector &oldDhIds, std::vector newDhIds); - void StringSplitToVector(const std::string &str, const char split, std::vector &vecStr); void DeleteNodeInfoAndNotify(const std::string& offlineDevId); void SendExistVirNodeInfos(sptr listener); std::set GetSyncNodeInfo(const std::string& devId); diff --git a/services/source/sourcemanager/src/distributed_input_source_manager.cpp b/services/source/sourcemanager/src/distributed_input_source_manager.cpp index 34eddf8..ad0566f 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -284,9 +284,9 @@ void DistributedInputSourceManager::DInputSourceListener::OnResponseStartRemoteI } std::vector vecStr; - sourceManagerObj_->StringSplitToVector(dhids, INPUT_STRING_SPLIT_POINT, vecStr); + StringSplitToVector(dhids, INPUT_STRING_SPLIT_POINT, vecStr); DInputState::GetInstance().AddDhids(vecStr); - DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_IN); + DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_IN, -1); std::shared_ptr jsonArrayMsg = std::make_shared(); nlohmann::json tmpJson; @@ -2459,24 +2459,6 @@ void DistributedInputSourceManager::RunKeyStateCallback(const std::string &sinkI return; } -void DistributedInputSourceManager::StringSplitToVector(const std::string &str, const char split, - std::vector &vecStr) -{ - if (str.empty()) { - DHLOGE("StringSplitToVector param str is error."); - return; - } - std::string strTmp = str + split; - size_t pos = strTmp.find(split); - while (pos != strTmp.npos) { - std::string matchTmp = strTmp.substr(0, pos); - vecStr.push_back(matchTmp); - - strTmp = strTmp.substr(pos + 1, strTmp.size()); - pos = strTmp.find(split); - } -} - DInputServerType DistributedInputSourceManager::GetStartTransFlag() { return isStartTrans_; diff --git a/services/source/sourcemanager/test/sourcemanagerunittest/distributed_input_sourcemanager_test.cpp b/services/source/sourcemanager/test/sourcemanagerunittest/distributed_input_sourcemanager_test.cpp index db31c2a..8211fa3 100644 --- a/services/source/sourcemanager/test/sourcemanagerunittest/distributed_input_sourcemanager_test.cpp +++ b/services/source/sourcemanager/test/sourcemanagerunittest/distributed_input_sourcemanager_test.cpp @@ -1588,14 +1588,6 @@ HWTEST_F(DistributedInputSourceManagerTest, RunRelayStopTypeCallback_01, testing EXPECT_EQ(1, sourceManager_->relayStpTypeCallbacks_.size()); } -HWTEST_F(DistributedInputSourceManagerTest, StringSplitToVector_01, testing::ext::TestSize.Level1) -{ - std::string str = ""; - std::vector vecStr; - sourceManager_->StringSplitToVector(str, ',', vecStr); - EXPECT_EQ(true, str.empty()); -} - HWTEST_F(DistributedInputSourceManagerTest, RemoveInputDeviceId_01, testing::ext::TestSize.Level1) { std::string deviceId = "umkyu1b165e1be98151891erbe8r91ev"; diff --git a/services/source/transport/include/distributed_input_source_transport.h b/services/source/transport/include/distributed_input_source_transport.h index 6daf67c..6979d94 100644 --- a/services/source/transport/include/distributed_input_source_transport.h +++ b/services/source/transport/include/distributed_input_source_transport.h @@ -138,8 +138,6 @@ private: std::string JointDhIds(const std::vector &dhids); void RegRespFunMap(); - void StringSplitToVector(const std::string &str, const char split, std::vector &vecStr); - private: std::mutex operationMutex_; std::set sessionIdSet_; diff --git a/services/source/transport/src/distributed_input_source_transport.cpp b/services/source/transport/src/distributed_input_source_transport.cpp index d6333c5..bee9d4a 100644 --- a/services/source/transport/src/distributed_input_source_transport.cpp +++ b/services/source/transport/src/distributed_input_source_transport.cpp @@ -286,24 +286,6 @@ int32_t DistributedInputSourceTransport::UnprepareRemoteInput(int32_t srcTsrcSeI return DH_SUCCESS; } -void DistributedInputSourceTransport::StringSplitToVector(const std::string &str, const char split, - std::vector &vecStr) -{ - if (str.empty()) { - DHLOGE("StringSplitToVector param str is error."); - return; - } - std::string strTmp = str + split; - size_t pos = strTmp.find(split); - while (pos != strTmp.npos) { - std::string matchTmp = strTmp.substr(0, pos); - vecStr.push_back(matchTmp); - - strTmp = strTmp.substr(pos + 1, strTmp.size()); - pos = strTmp.find(split); - } -} - int32_t DistributedInputSourceTransport::StartRemoteInputDhids(int32_t srcTsrcSeId, const std::string &deviceId, const std::string &dhids) { @@ -317,7 +299,7 @@ int32_t DistributedInputSourceTransport::StartRemoteInputDhids(int32_t srcTsrcSe std::vector vecStr; StringSplitToVector(dhids, INPUT_STRING_SPLIT_POINT, vecStr); DInputState::GetInstance().AddDhids(vecStr); - DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_IN); + DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_IN, -1); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_START_DHID_FOR_REL; @@ -783,7 +765,7 @@ int32_t DistributedInputSourceTransport::StopRemoteInput(const std::string &devi DHLOGI("StopRemoteInput sessionId:%d.", sessionId); DInputState::GetInstance().AddDhids(dhids); - DInputState::GetInstance().SwitchState(dhids, DhidState::THROUGH_OUT); + DInputState::GetInstance().SwitchState(dhids, DhidState::THROUGH_OUT, -1); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_STOP_DHID; diff --git a/services/state/BUILD.gn b/services/state/BUILD.gn index 19321a6..5ca8aaf 100644 --- a/services/state/BUILD.gn +++ b/services/state/BUILD.gn @@ -20,9 +20,11 @@ ohos_shared_library("libdinput_state") { "include", "${common_path}/include", "${utils_path}/include", + "${service_common}/include", "${services_sink_path}/inputcollector/include", "${distributedinput_path}/inputdevicehandler/include", "${services_source_path}/inputinject/include", + "${services_sink_path}/transport/include", "${frameworks_path}/include", "${fwk_common_path}/utils/include", ] @@ -38,6 +40,7 @@ ohos_shared_library("libdinput_state") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", "${services_sink_path}/inputcollector:libdinput_collector", + "${services_sink_path}/transport:libdinput_sink_trans", "${services_source_path}/inputinject:libdinput_inject", "${utils_path}:libdinput_utils", "//third_party/libevdev:libevdev", diff --git a/services/state/include/dinput_state.h b/services/state/include/dinput_state.h index d7e7f1e..b7a9b68 100644 --- a/services/state/include/dinput_state.h +++ b/services/state/include/dinput_state.h @@ -24,11 +24,16 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { +/* + * This enumeration class represents the two states of the peropheral: + * THROUGH_IN : The state indicates the peripheral takes effect on the local device. + * THROUGH_OUT : The state indicates that the peripheral takes effect at the remote device. +*/ enum class DhidState { - INIT = 0, - THROUGH_IN, + THROUGH_IN = 0, THROUGH_OUT, }; + class DInputState { public: static DInputState &GetInstance() @@ -41,18 +46,21 @@ public: int32_t Release(); int32_t AddDhids(const std::vector &dhids); int32_t DeleteDhids(const std::vector &dhids); - int32_t SwitchState(const std::vector &dhids, DhidState state); + int32_t SwitchState(const std::vector &dhids, DhidState state, const int32_t &sessionId); DhidState GetStateByDhid(std::string &dhid); private: ~DInputState(); - void CreateKeyUpInjectThread(const std::vector &dhids); - void CheckKeyState(std::string &dhid, std::string &keyboardNodePath); - void UpInject(int fd, std::vector &keyboardPressedKeys, std::string &dhid); - void KeyUpInject(std::vector shareDhidsPaths, std::vector shareDhIds); - bool IsExistDhid(const std::string &dhid); - void RecordEventLog(const input_event& event); + void CreateSpecialEventInjectThread(const int32_t &sessionId, const std::vector &dhids); + void CheckKeyboardState(std::string &dhid, std::string &keyboardNodePath, + std::vector &keyboardPressedKeys, int &fd); + void SpecEveInject(const int32_t &sessionId, std::vector dhids); + bool IsDhidExist(const std::string &dhid); + void RecordEventLog(const input_event &event); + void WriteEventToDev(int &fd, const input_event &event); + void CheckMouseKeyState(const int32_t &sessionId, const std::string &mouseNodePath, + const std::string &mouseNodeDhId); private: std::mutex operationMutex_; diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index 3ccdfe0..41dd375 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -28,6 +28,7 @@ #include "constants_dinput.h" #include "distributed_input_collector.h" #include "distributed_input_inject.h" +#include "distributed_input_sink_transport.h" namespace OHOS { namespace DistributedHardware { @@ -58,10 +59,10 @@ int32_t DInputState::AddDhids(const std::vector &dhids) std::unique_lock mapLock(operationMutex_); for (auto &dhid : dhids) { DHLOGD("add dhid : %s", GetAnonyString(dhid).c_str()); - if (IsExistDhid(dhid)) { + if (IsDhidExist(dhid)) { DHLOGI("dhid : %s already exist.", GetAnonyString(dhid).c_str()); } else { - dhidStateMap_[dhid] = DhidState::INIT; + dhidStateMap_[dhid] = DhidState::THROUGH_IN; } } return DH_SUCCESS; @@ -73,7 +74,7 @@ int32_t DInputState::DeleteDhids(const std::vector &dhids) std::unique_lock mapLock(operationMutex_); for (auto &dhid : dhids) { DHLOGD("delete dhid : %s", GetAnonyString(dhid).c_str()); - if (!IsExistDhid(dhid)) { + if (!IsDhidExist(dhid)) { DHLOGE("dhid : %s not exist.", GetAnonyString(dhid).c_str()); } else { dhidStateMap_.erase(dhid); @@ -82,12 +83,12 @@ int32_t DInputState::DeleteDhids(const std::vector &dhids) return DH_SUCCESS; } -int32_t DInputState::SwitchState(const std::vector &dhids, DhidState state) +int32_t DInputState::SwitchState(const std::vector &dhids, DhidState state, const int32_t &sessionId) { std::unique_lock mapLock(operationMutex_); for (auto &dhid : dhids) { DHLOGD("SwitchState dhid : %s, state : %d.", GetAnonyString(dhid).c_str(), state); - if (!IsExistDhid(dhid)) { + if (!IsDhidExist(dhid)) { DHLOGE("dhid : %s not exist.", GetAnonyString(dhid).c_str()); } else { dhidStateMap_[dhid] = state; @@ -95,7 +96,7 @@ int32_t DInputState::SwitchState(const std::vector &dhids, DhidStat } if (state == DhidState::THROUGH_OUT) { - CreateKeyUpInjectThread(dhids); + CreateSpecialEventInjectThread(sessionId, dhids); } return DH_SUCCESS; @@ -103,14 +104,14 @@ int32_t DInputState::SwitchState(const std::vector &dhids, DhidStat DhidState DInputState::GetStateByDhid(std::string &dhid) { - if (!IsExistDhid(dhid)) { + if (!IsDhidExist(dhid)) { DHLOGE("dhid : %s not exist.", GetAnonyString(dhid).c_str()); - return DhidState::INIT; + return DhidState::THROUGH_IN; } return dhidStateMap_[dhid]; } -bool DInputState::IsExistDhid(const std::string &dhid) +bool DInputState::IsDhidExist(const std::string &dhid) { if (dhidStateMap_.find(dhid) == dhidStateMap_.end()) { return false; @@ -118,41 +119,85 @@ bool DInputState::IsExistDhid(const std::string &dhid) return true; } -void DInputState::CreateKeyUpInjectThread(const std::vector &dhids) +void DInputState::CreateSpecialEventInjectThread(const int32_t &sessionId, const std::vector &dhids) { - DHLOGI("CreateKeyUpInjectThread enter, dhids.size = %d.", dhids.size()); - std::vector keyboardNodePaths; - std::vector shareDhIds; - DistributedInputCollector::GetInstance().GetKeyboardNodePath(dhids, keyboardNodePaths, shareDhIds); - DistributedInputInject::GetInstance().GetVirtualKeyboardPathByDhId(dhids, keyboardNodePaths, shareDhIds); - - std::thread keyUpInjectThread = - std::thread(&DInputState::KeyUpInject, this, keyboardNodePaths, shareDhIds); - int32_t ret = pthread_setname_np(keyUpInjectThread.native_handle(), KEYBOARD_UP_INJECT_THREAD_NAME); + DHLOGI("CreateSpecialEventInjectThread enter, dhids.size = %d, sessionId = %d.", dhids.size(), sessionId); + std::thread specEveInjectThread = + std::thread(&DInputState::SpecEveInject, this, sessionId, dhids); + int32_t ret = pthread_setname_np(specEveInjectThread.native_handle(), CHECK_KEY_STATUS_THREAD_NAME); if (ret != 0) { - DHLOGE("CreateKeyUpInjectThread setname failed."); + DHLOGE("specEveInjectThread setname failed."); } - keyUpInjectThread.detach(); + specEveInjectThread.detach(); } -void DInputState::KeyUpInject(std::vector shareDhidsPaths, std::vector shareDhIds) +void DInputState::RecordEventLog(const input_event &event) { - DHLOGI("KeyUpInject enter, shareDhidsPaths.size = %d, shareDhIds.size =%d.", - shareDhidsPaths.size(), shareDhIds.size()); - ssize_t len = shareDhidsPaths.size(); + std::string eventType = ""; + switch (event.type) { + case EV_KEY: + eventType = "EV_KEY"; + break; + case EV_SYN: + eventType = "EV_SYN"; + break; + default: + eventType = "other type"; + break; + } + DHLOGD("5.E2E-Test Source write event into input driver, EventType: %s, Code: %d, Value: %d", + eventType.c_str(), event.code, event.value); +} + +void DInputState::WriteEventToDev(int &fd, const input_event &event) +{ + if (write(fd, &event, sizeof(event)) < static_cast(sizeof(event))) { + DHLOGE("could not inject event, removed? (fd: %d)", fd); + return; + } + RecordEventLog(event); +} + +void DInputState::SpecEveInject(const int32_t &sessionId, std::vector dhids) +{ + DHLOGI("SpecEveInject enter"); + //mouse event send to remote device + if (sessionId != -1) { + std::string mouseNodePath; + std::string mouseNodeDhId; + DistributedInputCollector::GetInstance().GetMouseNodePath(dhids, mouseNodePath, mouseNodeDhId); + CheckMouseKeyState(sessionId, mouseNodePath, mouseNodeDhId); + } + + //keyboard up event inject local device + std::vector keyboardNodePaths; + std::vector keyboardNodeDhIds; + DistributedInputCollector::GetInstance().GetShareKeyboardPathsByDhIds(dhids, keyboardNodePaths, keyboardNodeDhIds); + DistributedInputInject::GetInstance().GetVirtualKeyboardPathsByDhIds(dhids, keyboardNodePaths, keyboardNodeDhIds); + ssize_t len = keyboardNodePaths.size(); for (int32_t i = 0; i < len; ++i) { - CheckKeyState(shareDhIds[i], shareDhidsPaths[i]); + std::vector keyboardPressedKeys; + int fd = -1; + CheckKeyboardState(keyboardNodeDhIds[i], keyboardNodePaths[i], keyboardPressedKeys, fd); + for (auto &code : keyboardPressedKeys) { + struct input_event event = { + .type = EV_KEY, + .code = code, + .value = KEY_UP_STATE + }; + WriteEventToDev(fd, event); + event.type = EV_SYN; + event.code = 0; + WriteEventToDev(fd, event); + } + CloseFd(fd); } } -int BitIsSet(const unsigned long *array, int bit) +void DInputState::CheckKeyboardState(std::string &dhid, std::string &keyboardNodePath, + std::vector &keyboardPressedKeys, int &fd) { - return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS))); -} - -void DInputState::CheckKeyState(std::string &dhid, std::string &keyboardNodePath) -{ - DHLOGI("CheckKeyState enter, dhid :%s, keyboardNodePath :%s.", GetAnonyString(dhid).c_str(), + DHLOGI("CheckKeyboardState enter, dhid %s, keyboardNodePath %s.", GetAnonyString(dhid).c_str(), keyboardNodePath.c_str()); char canonicalPath[PATH_MAX + 1] = {0x00}; if (keyboardNodePath.length() == 0 || keyboardNodePath.length() > PATH_MAX || @@ -160,15 +205,13 @@ void DInputState::CheckKeyState(std::string &dhid, std::string &keyboardNodePath DHLOGE("keyboard Nodepath check fail, error path: %s", keyboardNodePath.c_str()); return; } - - int fd = open(canonicalPath, O_WRONLY | O_NONBLOCK); + fd = open(canonicalPath, O_WRONLY | O_NONBLOCK); if (fd < 0) { DHLOGE("open keyboard Node Path error:", errno); return; } uint32_t count = 0; - std::vector keyboardPressedKeys; unsigned long keystate[NLONGS(KEY_CNT)] = { 0 }; while (true) { if (count > READ_RETRY_MAX) { @@ -189,56 +232,57 @@ void DInputState::CheckKeyState(std::string &dhid, std::string &keyboardNodePath } break; } - UpInject(fd, keyboardPressedKeys, dhid); - if (fd >= 0) { - close(fd); - fd = -1; - } } -void DInputState::UpInject(int fd, std::vector &keyboardPressedKeys, std::string &dhid) +void DInputState::CheckMouseKeyState(const int32_t &sessionId, const std::string &mouseNodePath, + const std::string &mouseNodeDhId) { - for (auto &code: keyboardPressedKeys) { - struct input_event event = { - .type = EV_KEY, - .code = code, - .value = KEY_UP_STATE - }; - if (write(fd, &event, sizeof(event)) < static_cast(sizeof(event))) { - DHLOGE("could not inject event, removed? (fd: %d)", fd); - return; - } - RecordEventLog(event); - - event = { - .type = EV_SYN, - .code = 0, - .value = KEY_UP_STATE - }; - if (write(fd, &event, sizeof(event)) < static_cast(sizeof(event))) { - DHLOGE("could not inject event, removed? (fd: %d)", fd); - return; - } - RecordEventLog(event); + DHLOGI("CheckMouseKeyState enter, mouseNodePath %s, mouseNodeDhId %s, sessionId %d.", mouseNodePath.c_str(), + GetAnonyString(mouseNodeDhId).c_str(), sessionId); + char canonicalPath[PATH_MAX + 1] = {0x00}; + if (mouseNodePath.length() == 0 || mouseNodePath.length() > PATH_MAX || + realpath(mouseNodePath.c_str(), canonicalPath) == nullptr) { + DHLOGE("mouse Nodepath check fail, error path: %s", mouseNodePath.c_str()); + return; } -} - -void DInputState::RecordEventLog(const input_event& event) -{ - std::string eventType = ""; - switch (event.type) { - case EV_KEY: - eventType = "EV_KEY"; - break; - case EV_SYN: - eventType = "EV_SYN"; - break; - default: - eventType = "other type"; - break; + int fd = open(canonicalPath, O_RDONLY | O_NONBLOCK); + if (fd < 0) { + DHLOGE("open mouse Node Path error:", errno); + return; } - DHLOGD("5.E2E-Test Source write event into input driver, EventType: %s, Code: %d, Value: %d", - eventType.c_str(), event.code, event.value); + + uint32_t count = 0; + int leftKeyVal = 0; + int rightKeyVal = 0; + int midKeyVal = 0; + unsigned long keystate[NLONGS(KEY_CNT)] = { 0 }; + while (true) { + if (count > READ_RETRY_MAX) { + break; + } + // Query all key state + int rc = ioctl(fd, EVIOCGKEY(sizeof(keystate)), keystate); + if (rc < 0) { + DHLOGE("read all key state failed, rc=%d ", rc); + count += 1; + std::this_thread::sleep_for(std::chrono::milliseconds(READ_SLEEP_TIME_MS)); + continue; + } + leftKeyVal = BitIsSet(keystate, BTN_LEFT); + if (leftKeyVal != 0) { + DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, mouseNodeDhId, BTN_LEFT); + } + rightKeyVal = BitIsSet(keystate, BTN_RIGHT); + if (rightKeyVal != 0) { + DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, mouseNodeDhId, BTN_RIGHT); + } + midKeyVal = BitIsSet(keystate, BTN_MIDDLE); + if (midKeyVal != 0) { + DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, mouseNodeDhId, BTN_MIDDLE); + } + break; + } + CloseFd(fd); } } // namespace DistributedInput } // namespace DistributedHardware diff --git a/utils/include/dinput_utils_tool.h b/utils/include/dinput_utils_tool.h index dba9054..8092ce2 100644 --- a/utils/include/dinput_utils_tool.h +++ b/utils/include/dinput_utils_tool.h @@ -50,8 +50,13 @@ std::string GetNodeDesc(std::string parameters); std::string GetAnonyString(const std::string &value); std::string GetAnonyInt32(const int32_t value); std::string Sha256(const std::string& string); +void CloseFd(int& fd); +int BitIsSet(const unsigned long *array, int bit); +void StringSplitToVector(const std::string& str, const char split, std::vector& vecStr); +int OpenInputDeviceFdByPath(std::string devicePath); +std::string ConvertErrNo(); +void ScanInputDevicesPath(std::string dirName, std::vector& vecInputDevPath); } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS - #endif // OHOS_DISTRIBUTED_INPUT_UTILS_TOOL_H \ No newline at end of file diff --git a/utils/src/dinput_utils_tool.cpp b/utils/src/dinput_utils_tool.cpp index 0cda783..1154c84 100644 --- a/utils/src/dinput_utils_tool.cpp +++ b/utils/src/dinput_utils_tool.cpp @@ -17,8 +17,12 @@ #include #include +#include +#include #include - +#include +#include +#include #include #include "nlohmann/json.hpp" @@ -46,6 +50,9 @@ namespace { constexpr unsigned char MASK = 0x0F; constexpr int32_t DOUBLE_TIMES = 2; constexpr int32_t INT32_STRING_LENGTH = 40; + constexpr int32_t MAX_RETRY_COUNT = 10; + constexpr uint32_t SLEEP_TIME_US = 100 * 1000; + constexpr uint32_t ERROR_MSG_MAX_LEN = 256; } DevInfo GetLocalDeviceInfo() { @@ -285,6 +292,98 @@ std::string Sha256(const std::string& in) out[SHA256_DIGEST_LENGTH * DOUBLE_TIMES] = 0; return reinterpret_cast(out); } + +void CloseFd(int& fd) +{ + if (fd < 0) { + DHLOGE("No fd need to beclosed."); + return; + } + close(fd); + fd = -1; +} + +int BitIsSet(const unsigned long *array, int bit) +{ + return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS))); +} + +void StringSplitToVector(const std::string& str, const char split, std::vector& vecStr) +{ + if (str.empty()) { + DHLOGE("StringSplitToVector param str is error."); + return; + } + std::string strTmp = str + split; + size_t pos = strTmp.find(split); + while (pos != strTmp.npos) { + std::string matchTmp = strTmp.substr(0, pos); + vecStr.push_back(matchTmp); + strTmp = strTmp.substr(pos + 1, strTmp.size()); + pos = strTmp.find(split); + } +} + +int OpenInputDeviceFdByPath(std::string devicePath) +{ + chmod(devicePath.c_str(), S_IWRITE | S_IREAD); + char canonicalDevicePath[PATH_MAX + 1] = {0x00}; + if (devicePath.length() == 0 || devicePath.length() > PATH_MAX || + realpath(devicePath.c_str(), canonicalDevicePath) == nullptr) { + DHLOGE("path check fail, error path: %s", devicePath.c_str()); + return -1; + } + struct stat s; + if ((stat(canonicalDevicePath, &s) == 0) && (s.st_mode & S_IFDIR)) { + DHLOGI("path: %s is a dir.", devicePath.c_str()); + return -1; + } + int fd = open(canonicalDevicePath, O_RDWR | O_CLOEXEC | O_NONBLOCK); + int32_t count = 0; + while ((fd < 0) && (count < MAX_RETRY_COUNT)) { + ++count; + usleep(SLEEP_TIME_US); + fd = open(canonicalDevicePath, O_RDWR | O_CLOEXEC | O_NONBLOCK); + DHLOGE("could not open %s, %s; retry %d\n", devicePath.c_str(), ConvertErrNo().c_str(), count); + } + if (count >= MAX_RETRY_COUNT) { + DHLOGE("could not open %s, %s\n", devicePath.c_str(), ConvertErrNo().c_str()); + CloseFd(fd); + return -1; + } + return fd; +} + +std::string ConvertErrNo() +{ + char errMsg[ERROR_MSG_MAX_LEN] = {0}; + strerror_r(errno, errMsg, ERROR_MSG_MAX_LEN); + std::string errNoMsg(errMsg); + return errNoMsg; +} + +void ScanInputDevicesPath(std::string dirName, std::vector& vecInputDevPath) +{ + DIR *dir; + struct dirent *de; + dir = opendir(dirName.c_str()); + if (dir == nullptr) { + DHLOGE("error opendir /dev/input :%{public}s\n", ConvertErrNo().c_str()); + return; + } + size_t dirNameFirstPos = 0; + size_t dirNameSecondPos = 1; + size_t dirNameThirdPos = 2; + while ((de = readdir(dir))) { + if (de->d_name[dirNameFirstPos] == '.' && (de->d_name[dirNameSecondPos] == '\0' || + (de->d_name[dirNameSecondPos] == '.' && de->d_name[dirNameThirdPos] == '\0'))) { + continue; + } + std::string tmpDevName = dirName + "/" + std::string(de->d_name); + vecInputDevPath.push_back(tmpDevName); + } + closedir(dir); +} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file