From becdd9273b07bc1819c51ddd3dac6f88f468cba4 Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Sat, 2 Dec 2023 11:11:51 +0800 Subject: [PATCH] add Signed-off-by: hwzhangchuang --- common/include/constants_dinput.h | 6 +++ .../include/distributed_input_handler.h | 3 +- .../src/distributed_input_handler.cpp | 22 +------- .../include/distributed_input_node_manager.h | 1 - .../src/distributed_input_node_manager.cpp | 29 +++------- .../source/inputinject/src/virtual_device.cpp | 4 +- .../distributed_input_sourceinject_test.cpp | 54 ++++++++----------- 7 files changed, 41 insertions(+), 78 deletions(-) diff --git a/common/include/constants_dinput.h b/common/include/constants_dinput.h index 4543d32..af2e06c 100644 --- a/common/include/constants_dinput.h +++ b/common/include/constants_dinput.h @@ -136,6 +136,12 @@ namespace DistributedInput { const std::string DINPUT_SPLIT_COMMA = ", "; + const char VIR_NODE_SPLIT = '|'; + const char VIR_NODE_PID_SPLIT = '/'; + const uint32_t VIR_NODE_PHY_LEN = 3; + const uint32_t VIR_NODE_PHY_DEVID_IDX = 1; + const uint32_t VIR_NODE_PHY_DHID_IDX = 2; + const std::string SOURCE_DEVICE_ID = "sourceDevId"; const std::string SINK_DEVICE_ID = "sinkDevId"; diff --git a/inputdevicehandler/include/distributed_input_handler.h b/inputdevicehandler/include/distributed_input_handler.h index 16087a8..c41d095 100644 --- a/inputdevicehandler/include/distributed_input_handler.h +++ b/inputdevicehandler/include/distributed_input_handler.h @@ -53,7 +53,6 @@ private: DistributedInputHandler(); ~DistributedInputHandler(); void StructTransJson(const InputDevice &pBuf, std::string &strDescriptor); - int32_t GetDeviceInfo(std::string &deviceId); std::shared_ptr m_listener; bool InitCollectEventsThread(); void NotifyHardWare(int iCnt); @@ -62,7 +61,7 @@ private: bool isCollectingEvents_; bool isStartCollectEventThread; static void *CollectEventsThread(void *param); - void StartInputMonitorDeviceThread(const std::string deviceId); + void StartInputMonitorDeviceThread(); void StopInputMonitorDeviceThread(); // The event queue. diff --git a/inputdevicehandler/src/distributed_input_handler.cpp b/inputdevicehandler/src/distributed_input_handler.cpp index 6972463..8cb619c 100644 --- a/inputdevicehandler/src/distributed_input_handler.cpp +++ b/inputdevicehandler/src/distributed_input_handler.cpp @@ -149,21 +149,6 @@ void DistributedInputHandler::UnRegisterPluginListener() this->m_listener = nullptr; } -int32_t DistributedInputHandler::GetDeviceInfo(std::string &deviceId) -{ - std::unique_lock my_lock(operationMutex_); - auto localNode = std::make_unique(); - int32_t retCode = GetLocalNodeDeviceInfo("ohos.dhardware", localNode.get()); - if (retCode != 0) { - DHLOGE("Could not get device id."); - return ERR_DH_INPUT_HANDLER_GET_DEVICE_ID_FAIL; - } - - deviceId = localNode->networkId; - DHLOGI("device id is %s", GetAnonyString(deviceId).c_str()); - return DH_SUCCESS; -} - bool DistributedInputHandler::InitCollectEventsThread() { pthread_attr_t attr; @@ -190,15 +175,12 @@ void *DistributedInputHandler::CollectEventsThread(void *param) DHLOGE("CollectEventsThread setname failed."); } DistributedInputHandler *pThis = reinterpret_cast(param); - - std::string deviceId; - pThis->GetDeviceInfo(deviceId); - pThis->StartInputMonitorDeviceThread(deviceId); + pThis->StartInputMonitorDeviceThread(); DHLOGI("DistributedInputHandler::CollectEventsThread exist!"); return nullptr; } -void DistributedInputHandler::StartInputMonitorDeviceThread(const std::string deviceId) +void DistributedInputHandler::StartInputMonitorDeviceThread() { if (inputHub_ == nullptr) { DHLOGE("inputHub_ not initialized"); diff --git a/services/source/inputinject/include/distributed_input_node_manager.h b/services/source/inputinject/include/distributed_input_node_manager.h index defc5bb..ace7b0e 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -67,7 +67,6 @@ public: int32_t RemoveVirtualTouchScreenNode(const std::string &devId, const std::string &dhId); int32_t GetVirtualTouchScreenFd(); - int32_t GetDeviceInfo(std::string &devId); void ProcessInjectEvent(const EventBatch &events); /** diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index 53e638d..c9e6c76 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -154,7 +154,7 @@ void DistributedInputNodeManager::ParseInputDeviceEvents(const nlohmann::json &i void DistributedInputNodeManager::ScanSinkInputDevices(const std::string &devId, const std::string &dhId) { - DHLOGI("ScanSinkInputDevices enter, deviceId:%s, dhId %s.", + DHLOGI("ScanSinkInputDevices enter, deviceId: %s, dhId: %s.", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); std::vector vecInputDevPath; ScanInputDevicesPath(DEVICE_PATH, vecInputDevPath); @@ -250,13 +250,13 @@ bool DistributedInputNodeManager::GetDevDhUniqueIdByFd(int fd, DhUniqueID &dhUnq DHLOGD("GetDevDhUniqueIdByFd physicalPath %s.", physicalPath.c_str()); std::vector phyPathVec; - SplitStringToVector(physicalPath, '|', phyPathVec); - if (phyPathVec.size() != 3) { + SplitStringToVector(physicalPath, VIR_NODE_SPLIT, phyPathVec); + if (phyPathVec.size() != VIR_NODE_PHY_LEN) { DHLOGE("The physical path is invalid"); return false; } - std::string devId = phyPathVec[1]; - std::string dhId = phyPathVec[2]; + std::string devId = phyPathVec[VIR_NODE_PHY_DEVID_IDX]; + std::string dhId = phyPathVec[VIR_NODE_PHY_DHID_IDX]; if (devId.empty() || dhId.empty()) { DHLOGE("Get dev deviceid and dhid failed."); return false; @@ -272,7 +272,7 @@ void DistributedInputNodeManager::SetPathForVirDev(const DhUniqueID &dhUniqueId, std::lock_guard lock(virtualDeviceMapMutex_); auto iter = virtualDeviceMap_.begin(); while (iter != virtualDeviceMap_.end()) { - DHLOGD("Check Virtual device deviceId %s, dhid %s.", iter->first.first.c_str(), iter->first.second.c_str()); + DHLOGD("Check Virtual device, deviceId %s, dhid %s.", iter->first.first.c_str(), iter->first.second.c_str()); if (iter->first == dhUniqueId) { DHLOGD("Found the virtual device, set path :%s", devicePath.c_str()); iter->second->SetPath(devicePath); @@ -304,7 +304,7 @@ bool DistributedInputNodeManager::MatchAndSavePhysicalPath(const std::string &de return false; } - DHLOGD("This opening node deviceId: %s, dhId %s, to match node deviceId: %s, dhId: %s", + DHLOGD("This opening node deviceId: %s, dhId: %s, to match node deviceId: %s, dhId: %s", GetAnonyString(curDhUniqueId.first).c_str(), GetAnonyString(curDhUniqueId.second).c_str(), GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); @@ -536,21 +536,6 @@ void DistributedInputNodeManager::ProcessInjectEvent(const EventBatch &events) } } } - -int32_t DistributedInputNodeManager::GetDeviceInfo(std::string &devId) -{ - std::unique_lock my_lock(operationMutex_); - auto localNode = std::make_unique(); - int32_t retCode = GetLocalNodeDeviceInfo(DINPUT_PKG_NAME.c_str(), localNode.get()); - if (retCode != 0) { - DHLOGE("Could not get device id."); - return ERR_DH_INPUT_HANDLER_GET_DEVICE_ID_FAIL; - } - - devId = localNode->networkId; - DHLOGI("device id is %s", GetAnonyString(devId).c_str()); - return DH_SUCCESS; -} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS diff --git a/services/source/inputinject/src/virtual_device.cpp b/services/source/inputinject/src/virtual_device.cpp index 799890e..f75dcbf 100644 --- a/services/source/inputinject/src/virtual_device.cpp +++ b/services/source/inputinject/src/virtual_device.cpp @@ -110,8 +110,8 @@ void VirtualDevice::SetABSInfo(struct uinput_user_dev &inputUserDev, const Input bool VirtualDevice::SetPhys(const std::string &deviceName, const std::string &dhId) { std::string phys; - phys.append(deviceName).append(pid_).append("/").append(pid_).append("|") - .append(netWorkId_).append("|").append(dhId); + phys.append(deviceName).append(pid_).append(VIR_NODE_PID_SPLIT).append(pid_).append(VIR_NODE_SPLIT) + .append(netWorkId_).append(VIR_NODE_SPLIT).append(dhId); if (ioctl(fd_, UI_SET_PHYS, phys.c_str()) < 0) { return false; 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 d670f44..43f2c16 100644 --- a/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp +++ b/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp @@ -162,38 +162,38 @@ HWTEST_F(DistributedInputSourceInjectTest, RegisterDistributedEvent01, testing:: { RawEvent event1 = { .when = 0, - .type = EV_KEy, + .type = EV_KEY, .code = KEY_D, .value = 1, - .descriptor = "afv4s8b1dr1b8er1bd65fb16redb1dfb18d1b56df1b68d"; + .descriptor = "afv4s8b1dr1b8er1bd65fb16redb1dfb18d1b56df1b68d" }; RawEvent event2 = { .when = 1, - .type = EV_KEy, + .type = EV_KEY, .code = KEY_D, .value = 0, - .descriptor = "afv4s8b1dr1b8er1bd65fb16redb1dfb18d1b56df1b68d"; + .descriptor = "afv4s8b1dr1b8er1bd65fb16redb1dfb18d1b56df1b68d" }; RawEvent event3 = { .when = 2, - .type = EV_KEy, + .type = EV_KEY, .code = KEY_D, .value = 1, - .descriptor = "afv4s8b1dr1b8er1bd65fb16redb1dfb18d1b56df1b68d"; + .descriptor = "afv4s8b1dr1b8er1bd65fb16redb1dfb18d1b56df1b68d" }; RawEvent event4 = { .when = 3, - .type = EV_KEy, + .type = EV_KEY, .code = KEY_D, .value = 0, - .descriptor = "afv4s8b1dr1b8er1bd65fb16redb1dfb18d1b56df1b68d"; + .descriptor = "afv4s8b1dr1b8er1bd65fb16redb1dfb18d1b56df1b68d" }; std::vector writeBuffer = { event1, event2, event3, event4 }; DistributedInputInject::GetInstance().inputNodeManager_ = std::make_unique(); DistributedInputInject::GetInstance().StartInjectThread(); DistributedInputInject::GetInstance().StopInjectThread(); - std::string deviceId = "deviceId123"; + std::string deviceId = "aefbg1nr81n521be8rb1erbe1w8bg1erb18"; int32_t ret = DistributedInputInject::GetInstance().RegisterDistributedEvent(deviceId, writeBuffer); EXPECT_EQ(DH_SUCCESS, ret); } @@ -205,38 +205,38 @@ HWTEST_F(DistributedInputSourceInjectTest, RegisterDistributedEvent02, testing:: .type = EV_REL, .code = REL_X, .value = 2, - .descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18"; + .descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18" }; RawEvent event2 = { .when = 1, .type = EV_REL, .code = REL_Y, .value = 2, - .descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18"; + .descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18" }; RawEvent event3 = { .when = 2, .type = EV_REL, .code = REL_X, .value = 3, - .descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18"; + .descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18" }; RawEvent event4 = { .when = 3, .type = EV_REL, .code = REL_Y, .value = 3, - .descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18"; + .descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18" }; RawEvent event5 = { .when = 4, .type = EV_SYN, .code = SYN_REPORT, .value = 0, - .descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18"; + .descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18" }; - std::vector writeBuffer = { event1, event2, event3, event4, event5}; - std::string deviceId = "deviceId123"; + std::vector writeBuffer = { event1, event2, event3, event4, event5 }; + std::string deviceId = "aefbg1nr81n521be8rb1erbe1w8bg1erb18"; int32_t ret = DistributedInputInject::GetInstance().RegisterDistributedEvent(deviceId, writeBuffer); EXPECT_EQ(DH_SUCCESS, ret); } @@ -248,39 +248,39 @@ HWTEST_F(DistributedInputSourceInjectTest, RegisterDistributedEvent03, testing:: .type = EV_ABS, .code = ABS_X, .value = 1, - .descriptor = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; + .descriptor = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8" }; RawEvent event2 = { .when = 1, .type = EV_ABS, .code = ABS_X, .value = 2, - .descriptor = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; + .descriptor = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8" }; RawEvent event3 = { .when = 2, .type = EV_ABS, .code = ABS_X, .value = 3, - .descriptor = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; + .descriptor = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8" }; RawEvent event4 = { .when = 3, .type = EV_ABS, .code = ABS_X, .value = 4, - .descriptor = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; + .descriptor = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8" }; std::vector writeBuffer = { event1, event2, event3, event4 }; - std::string deviceId = "deviceId123"; + std::string deviceId = "aefbg1nr81n521be8rb1erbe1w8bg1erb18"; int32_t ret = DistributedInputInject::GetInstance().RegisterDistributedEvent(deviceId, writeBuffer); EXPECT_EQ(DH_SUCCESS, ret); } HWTEST_F(DistributedInputSourceInjectTest, RegisterDistributedEvent04, testing::ext::TestSize.Level1) { - std::string deviceId = "deviceId123"; - struct RawEvent writeBuffer[4]; + std::string deviceId = "aefbg1nr81n521be8rb1erbe1w8bg1erb18"; + std::vector writeBuffer(4); DistributedInputInject::GetInstance().inputNodeManager_ = nullptr; int32_t ret = DistributedInputInject::GetInstance().RegisterDistributedEvent(deviceId, writeBuffer); EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL, ret); @@ -405,14 +405,6 @@ HWTEST_F(DistributedInputSourceInjectTest, OpenDevicesNode_002, testing::ext::Te ret = DistributedInputInject::GetInstance().inputNodeManager_->OpenDevicesNode(devId, dhIdtest, param); EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_OPEN_DEVICE_NODE_FAIL, ret); } - -HWTEST_F(DistributedInputSourceInjectTest, GetDeviceInfo_001, testing::ext::TestSize.Level1) -{ - std::string deviceId = ""; - DistributedInputInject::GetInstance().inputNodeManager_->InjectEvent(); - int32_t ret = DistributedInputInject::GetInstance().inputNodeManager_->GetDeviceInfo(deviceId); - EXPECT_EQ(DH_SUCCESS, ret); -} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS