diff --git a/common/include/constants_dinput.h b/common/include/constants_dinput.h index 4543d3268fcf5d4a690a7fee6359391edf951d98..f4b2a4456b147feb24f1980dc94f26dc1d275cce 100644 --- a/common/include/constants_dinput.h +++ b/common/include/constants_dinput.h @@ -136,6 +136,13 @@ namespace DistributedInput { const std::string DINPUT_SPLIT_COMMA = ", "; + const char VIR_NODE_SPLIT_CHAR = '|'; + const std::string VIR_NODE_SPLIT = "|"; + const std::string 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/common/include/input_hub.cpp b/common/include/input_hub.cpp index 4f08970cb37b1251969447dd446cd62b033027e2..d6d720104c0dc57655a22a13951bfd5fb5a5ddc4 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -44,7 +44,7 @@ const uint32_t SLEEP_TIME_US = 100 * 1000; const std::string MOUSE_NODE_KEY = "mouse"; } -InputHub::InputHub() : epollFd_(0), iNotifyFd_(0), inputWd_(0), needToScanDevices_(true), nextDeviceId_(1), +InputHub::InputHub() : epollFd_(0), iNotifyFd_(0), inputWd_(0), needToScanDevices_(true), mPendingEventItems{}, pendingEventCount_(0), pendingEventIndex_(0), pendingINotify_(false), deviceChanged_(false), inputTypes_(0), isStartCollectEvent_(false), isStartCollectHandler_(false) { diff --git a/common/include/input_hub.h b/common/include/input_hub.h index d702e2d42abede573576fea3447b59c0207c2d51..83ff3370e6310b1bc93bb73f78921a7b4f8669a7 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -213,9 +213,7 @@ private: std::set skipDevicePaths_; std::atomic needToScanDevices_; - std::string deviceId_; std::string touchDescriptor; - std::atomic nextDeviceId_; // The array of pending epoll events and the index of the next event to be handled. struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS]; diff --git a/inputdevicehandler/include/distributed_input_handler.h b/inputdevicehandler/include/distributed_input_handler.h index 16087a89cbe20ee68c79ce5ff683260edfe4fdd5..c41d0958d6f3230eff48f0992d3ec4cb786264f5 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 69724635a820448799650fe729ecc052ce46a5f1..8cb619cbe626b2f61e037771ad8cae89e7b6d064 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/inputdevicehandler/test/inputhandlertest/distributed_input_handler_test.cpp b/inputdevicehandler/test/inputhandlertest/distributed_input_handler_test.cpp index 979a5cd667c94b8849cf6f1a95ef81e066a7ebf3..bcf713bcab6e6372656a69be4f1edcbf4e8f1dc4 100644 --- a/inputdevicehandler/test/inputhandlertest/distributed_input_handler_test.cpp +++ b/inputdevicehandler/test/inputhandlertest/distributed_input_handler_test.cpp @@ -58,11 +58,10 @@ HWTEST_F(DInputHandlerTest, FindDevicesInfoByType_001, testing::ext::TestSize.Le uint32_t inputTypes = static_cast(DInputDeviceType::ALL); std::map datas; std::vector dhidsVec; - std::string deviceId = "deviceId_test"; dInputHandler.FindDevicesInfoByType(inputTypes, datas); dInputHandler.FindDevicesInfoByDhId(dhidsVec, datas); dInputHandler.Query(); - dInputHandler.StartInputMonitorDeviceThread(deviceId); + dInputHandler.StartInputMonitorDeviceThread(); InputDevice inputDevice; dInputHandler.mEventBuffer[0].type = DeviceType::DEVICE_ADDED; @@ -77,7 +76,7 @@ HWTEST_F(DInputHandlerTest, FindDevicesInfoByType_001, testing::ext::TestSize.Le std::map ret = dInputHandler.QueryExtraInfo(); EXPECT_EQ(0, ret.size()); dInputHandler.inputHub_ = std::make_unique(); - dInputHandler.StartInputMonitorDeviceThread(deviceId); + dInputHandler.StartInputMonitorDeviceThread(); } } // namespace DistributedInput } // namespace DistributedHardware diff --git a/services/source/inputinject/include/distributed_input_inject.h b/services/source/inputinject/include/distributed_input_inject.h index 7bb502817c559bbd4835d85e0244248d4f0cbf63..cb4951a7ce057a48d7377f188ba037522deb0130 100644 --- a/services/source/inputinject/include/distributed_input_inject.h +++ b/services/source/inputinject/include/distributed_input_inject.h @@ -34,8 +34,7 @@ public: int32_t RegisterDistributedHardware(const std::string &devId, const std::string &dhId, const std::string ¶meters); int32_t UnregisterDistributedHardware(const std::string &devId, const std::string &dhId); - int32_t RegisterDistributedEvent(RawEvent *buffer, size_t bufferSize); - int32_t RegisterDistributedEvent(const std::vector &events); + int32_t RegisterDistributedEvent(const std::string &devId, const std::vector &events); int32_t StructTransJson(const InputDevice &pBuf, std::string &strDescriptor); void StartInjectThread(); void StopInjectThread(); @@ -43,16 +42,14 @@ public: const uint32_t sourceWinHeight); int32_t CreateVirtualTouchScreenNode(const std::string &devId, const std::string &dhId, const uint64_t srcWinId, const uint32_t sourcePhyWidth, const uint32_t sourcePhyHeight); - int32_t RemoveVirtualTouchScreenNode(const std::string &dhId); + int32_t RemoveVirtualTouchScreenNode(const std::string &devId, const std::string &dhId); int32_t GetVirtualTouchScreenFd(); - int32_t GetDhIdsByInputType(const std::string &devId, const uint32_t &inputTypes, std::vector &dhIds); int32_t RegisterInjectEventCb(sptr callback); int32_t UnregisterInjectEventCb(); - void NotifyNodeMgrScanVirNode(const std::string &dhId); - void InputDeviceEventInject(const std::shared_ptr &rawEvent); - void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, - std::vector &virKeyboardPaths, std::vector &virKeyboardDhIds); + void NotifyNodeMgrScanVirNode(const std::string &devId, const std::string &dhId); + void GetVirtualKeyboardPaths(const std::string &devId, const std::vector &dhIds, + std::vector &virKeyboardPaths); private: DistributedInputInject(); ~DistributedInputInject(); diff --git a/services/source/inputinject/include/distributed_input_node_manager.h b/services/source/inputinject/include/distributed_input_node_manager.h index 9f5ead5f9247b94138cd7d18d22a7be649c87c2d..ace7b0ee437f9d7228a5f00b816458dbb652c64e 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -37,7 +37,19 @@ namespace DistributedHardware { namespace DistributedInput { constexpr uint32_t DINPUT_NODE_MANAGER_SCAN_ALL_NODE = 1; constexpr uint32_t DINPUT_INJECT_EVENT_FAIL = 2; +const std::string INPUT_NODE_DEVID = "devId"; const std::string INPUT_NODE_DHID = "dhId"; +/** + * @brief the unique id for the input peripheral. + * left is device networkid, right is dhId in that device. + */ +using DhUniqueID = std::pair; +/** + * @brief a batch events form one device + * left: device networid where these events from + * right: the event batch + */ +using EventBatch = std::pair>; class DistributedInputNodeManager { public: DistributedInputNodeManager(); @@ -45,25 +57,28 @@ public: int32_t OpenDevicesNode(const std::string &devId, const std::string &dhId, const std::string ¶meters); - int32_t GetDevice(const std::string &dhId, VirtualDevice *&device); - void ReportEvent(const RawEvent rawEvent); - void ReportEvent(const std::vector &events); - int32_t CloseDeviceLocked(const std::string &dhId); + int32_t GetDevice(const std::string &devId, const std::string &dhId, VirtualDevice *&device); + void ReportEvent(const std::string &devId, const std::vector &events); + int32_t CloseDeviceLocked(const std::string &devId, const std::string &dhId); void StartInjectThread(); void StopInjectThread(); int32_t CreateVirtualTouchScreenNode(const std::string &devId, const std::string &dhId, const uint64_t srcWinId, const uint32_t sourcePhyWidth, const uint32_t sourcePhyHeight); - int32_t RemoveVirtualTouchScreenNode(const std::string &dhId); + int32_t RemoveVirtualTouchScreenNode(const std::string &devId, const std::string &dhId); int32_t GetVirtualTouchScreenFd(); - int32_t GetDeviceInfo(std::string &deviceId); - void GetDevicesInfoByType(const std::string &networkId, uint32_t inputTypes, std::map &datas); - void GetDevicesInfoByDhId(std::vector dhidsVec, std::map &datas); - void ProcessInjectEvent(const std::shared_ptr &rawEvent); - - void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, - std::vector &virKeyboardPaths, std::vector &virKeyboardDhIds); - void NotifyNodeMgrScanVirNode(const std::string &dhId); + void ProcessInjectEvent(const EventBatch &events); + + /** + * @brief Get the Virtual Keyboard Paths By Dh Ids object + * + * @param dhKeys list for device identify({networkId, dhId}) + * @param virKeyboardPaths the matched keyboard device + * @param virKeyboardDhIds matched keyboard device identify + */ + void GetVirtualKeyboardPaths(const std::vector &dhUniqueIds, + std::vector &virKeyboardPaths); + void NotifyNodeMgrScanVirNode(const std::string &devId, const std::string &dhId); void RegisterInjectEventCb(sptr callback); void UnregisterInjectEventCb(); @@ -84,7 +99,7 @@ public: }; private: - void AddDeviceLocked(const std::string &dhId, std::unique_ptr device); + void AddDeviceLocked(const std::string &networkId, const std::string &dhId, std::unique_ptr device); int32_t CreateHandle(const InputDevice &inputDevice, const std::string &devId, const std::string &dhId); void ParseInputDeviceJson(const std::string &str, InputDevice &pBuf); void ParseInputDevice(const nlohmann::json &inputDeviceJson, InputDevice &pBuf); @@ -92,15 +107,15 @@ private: void ParseInputDeviceEvents(const nlohmann::json &inputDeviceJson, InputDevice &pBuf); void InjectEvent(); - void ScanSinkInputDevices(const std::string &dhId); - void OpenInputDevice(const std::string &devicePath, const std::string &dhId); + void ScanSinkInputDevices(const std::string &devId, const std::string &dhId); + bool MatchAndSavePhysicalPath(const std::string &devicePath, const std::string &devId, const std::string &dhId); bool IsVirtualDev(int fd); - bool GetDevDhIdByFd(int fd, std::string &dhId, std::string &physicalPath); - void SetPathForDevMap(const std::string &dhId, const std::string &devicePath); + bool GetDevDhUniqueIdByFd(int fd, DhUniqueID &dhUnqueId, std::string &physicalPath); + void SetPathForVirDev(const DhUniqueID &dhUniqueId, const std::string &devicePath); void RunInjectEventCallback(const std::string &dhId, const uint32_t injectEvent); - /* the key is dhId, and the value is virtualDevice */ - std::map> virtualDeviceMap_; + /* the key is {networkId, dhId}, and the value is virtualDevice */ + std::map> virtualDeviceMap_; std::mutex virtualDeviceMapMutex_; std::atomic isInjectThreadCreated_; std::atomic isInjectThreadRunning_; @@ -108,7 +123,7 @@ private: std::thread eventInjectThread_; std::mutex injectThreadMutex_; std::condition_variable conditionVariable_; - std::queue> injectQueue_; + std::queue injectQueue_; std::unique_ptr inputHub_; int32_t virtualTouchScreenFd_; std::once_flag callOnceFlag_; diff --git a/services/source/inputinject/src/distributed_input_inject.cpp b/services/source/inputinject/src/distributed_input_inject.cpp index d5065847a6a2b4c121309bf89ef0d4934d393994..8f53ecf9677552c9d9546be03cad018a635769a5 100644 --- a/services/source/inputinject/src/distributed_input_inject.cpp +++ b/services/source/inputinject/src/distributed_input_inject.cpp @@ -74,7 +74,7 @@ int32_t DistributedInputInject::UnregisterDistributedHardware(const std::string DHLOGE("the DistributedInputNodeManager is null\n"); return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; } - if (inputNodeManager_->CloseDeviceLocked(dhId) < 0) { + if (inputNodeManager_->CloseDeviceLocked(devId, dhId) < 0) { DHLOGE("delete virtual device error\n"); return ERR_DH_INPUT_SERVER_SOURCE_INJECT_UNREGISTER_FAIL; } @@ -83,22 +83,6 @@ int32_t DistributedInputInject::UnregisterDistributedHardware(const std::string return DH_SUCCESS; } -int32_t DistributedInputInject::GetDhIdsByInputType(const std::string &devId, const uint32_t &inputTypes, - std::vector &dhIds) -{ - std::lock_guard lock(inputNodeManagerMutex_); - if (inputNodeManager_ == nullptr) { - DHLOGE("the inputNodeListener is nullptr"); - return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; - } - std::map datas; - inputNodeManager_->GetDevicesInfoByType(devId, inputTypes, datas); - for (const auto &data_ : datas) { - dhIds.push_back(data_.second); - } - return DH_SUCCESS; -} - int32_t DistributedInputInject::StructTransJson(const InputDevice &pBuf, std::string &strDescriptor) { DHLOGI("[%s] %d, %d, %d, %d, %s.\n", (pBuf.name).c_str(), pBuf.bus, pBuf.vendor, pBuf.product, pBuf.version, @@ -126,35 +110,8 @@ int32_t DistributedInputInject::StructTransJson(const InputDevice &pBuf, std::st return DH_SUCCESS; } -void DistributedInputInject::InputDeviceEventInject(const std::shared_ptr &rawEvent) -{ - std::lock_guard lock(inputNodeManagerMutex_); - if (inputNodeManager_ == nullptr) { - DHLOGE("the inputNodeListener is nullptr"); - return; - } - if (rawEvent == nullptr) { - DHLOGE("the rawEvent is nullptr"); - return; - } - inputNodeManager_->ProcessInjectEvent(rawEvent); -} - -int32_t DistributedInputInject::RegisterDistributedEvent(RawEvent *buffer, size_t bufferSize) -{ - std::lock_guard lock(inputNodeManagerMutex_); - if (inputNodeManager_ == nullptr) { - DHLOGE("the DistributedInputNodeManager is null"); - return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; - } - - for (size_t i = 0; i < bufferSize; i++) { - inputNodeManager_->ReportEvent(buffer[i]); - } - return DH_SUCCESS; -} - -int32_t DistributedInputInject::RegisterDistributedEvent(const std::vector &events) +int32_t DistributedInputInject::RegisterDistributedEvent(const std::string &devId, + const std::vector &events) { std::lock_guard lock(inputNodeManagerMutex_); if (inputNodeManager_ == nullptr) { @@ -162,7 +119,7 @@ int32_t DistributedInputInject::RegisterDistributedEvent(const std::vectorReportEvent(events); + inputNodeManager_->ReportEvent(devId, events); return DH_SUCCESS; } @@ -201,14 +158,14 @@ int32_t DistributedInputInject::CreateVirtualTouchScreenNode(const std::string & return inputNodeManager_->CreateVirtualTouchScreenNode(devId, dhId, srcWinId, sourcePhyWidth, sourcePhyHeight); } -int32_t DistributedInputInject::RemoveVirtualTouchScreenNode(const std::string &dhId) +int32_t DistributedInputInject::RemoveVirtualTouchScreenNode(const std::string &devId, const std::string &dhId) { std::lock_guard lock(inputNodeManagerMutex_); if (inputNodeManager_ == nullptr) { DHLOGE("inputNodeManager is nullptr"); return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; } - return inputNodeManager_->RemoveVirtualTouchScreenNode(dhId); + return inputNodeManager_->RemoveVirtualTouchScreenNode(devId, dhId); } int32_t DistributedInputInject::GetVirtualTouchScreenFd() @@ -237,25 +194,31 @@ int32_t DistributedInputInject::UnregisterInjectEventCb() return DH_SUCCESS; } -void DistributedInputInject::GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, - std::vector &virKeyboardPaths, std::vector &virKeyboardDhIds) +void DistributedInputInject::GetVirtualKeyboardPaths(const std::string &devId, + const std::vector &dhIds, std::vector &virKeyboardPaths) { std::lock_guard lock(inputNodeManagerMutex_); if (inputNodeManager_ == nullptr) { DHLOGE("inputNodeManager is nullptr"); return; } - inputNodeManager_->GetVirtualKeyboardPathsByDhIds(dhIds, virKeyboardPaths, virKeyboardDhIds); + std::vector dhUniqIds; + std::for_each(dhIds.begin(), dhIds.end(), + [&devId, &dhUniqIds](std::string dhId) { + DhUniqueID id = {devId, dhId}; + dhUniqIds.push_back(id); + }); + inputNodeManager_->GetVirtualKeyboardPaths(dhUniqIds, virKeyboardPaths); } -void DistributedInputInject::NotifyNodeMgrScanVirNode(const std::string &dhId) +void DistributedInputInject::NotifyNodeMgrScanVirNode(const std::string &devId, const std::string &dhId) { std::lock_guard lock(inputNodeManagerMutex_); if (inputNodeManager_ == nullptr) { DHLOGE("inputNodeManager is nullptr"); return; } - inputNodeManager_->NotifyNodeMgrScanVirNode(dhId); + inputNodeManager_->NotifyNodeMgrScanVirNode(devId, dhId); } } // 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 4e195d6e549e16299fdb5f5db73f22b3510f11db..2c59710e815a96c13ffb6dd7ed9b17e20eb4a1e4 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -152,13 +152,17 @@ void DistributedInputNodeManager::ParseInputDeviceEvents(const nlohmann::json &i } } -void DistributedInputNodeManager::ScanSinkInputDevices(const std::string &dhId) +void DistributedInputNodeManager::ScanSinkInputDevices(const std::string &devId, const std::string &dhId) { - DHLOGI("ScanSinkInputDevices enter, dhId %s.", dhId.c_str()); + DHLOGI("ScanSinkInputDevices enter, deviceId: %s, dhId: %s.", + GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); std::vector vecInputDevPath; ScanInputDevicesPath(DEVICE_PATH, vecInputDevPath); for (auto &tempPath: vecInputDevPath) { - OpenInputDevice(tempPath, dhId); + if (MatchAndSavePhysicalPath(tempPath, devId, dhId)) { + DHLOGI("Set physical path success"); + break; + } } } @@ -197,15 +201,17 @@ void DistributedInputNodeManager::DInputNodeManagerEventHandler::ScanAllNode( std::shared_ptr dataMsg = event->GetSharedObject(); auto it = dataMsg->begin(); nlohmann::json innerMsg = *(it); + std::string devId = innerMsg[INPUT_NODE_DEVID]; std::string devicedhId = innerMsg[INPUT_NODE_DHID]; - nodeManagerObj_->ScanSinkInputDevices(devicedhId); + nodeManagerObj_->ScanSinkInputDevices(devId, devicedhId); } -void DistributedInputNodeManager::NotifyNodeMgrScanVirNode(const std::string &dhId) +void DistributedInputNodeManager::NotifyNodeMgrScanVirNode(const std::string &devId, const std::string &dhId) { DHLOGI("NotifyNodeMgrScanVirNode enter."); std::shared_ptr jsonArrayMsg = std::make_shared(); nlohmann::json tmpJson; + tmpJson[INPUT_NODE_DEVID] = devId; tmpJson[INPUT_NODE_DHID] = dhId; jsonArrayMsg->push_back(tmpJson); AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get( @@ -232,7 +238,7 @@ bool DistributedInputNodeManager::IsVirtualDev(int fd) return true; } -bool DistributedInputNodeManager::GetDevDhIdByFd(int fd, std::string &dhId, std::string &physicalPath) +bool DistributedInputNodeManager::GetDevDhUniqueIdByFd(int fd, DhUniqueID &dhUnqueId, std::string &physicalPath) { char buffer[INPUT_EVENT_BUFFER_SIZE] = {0}; if (ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) { @@ -242,23 +248,32 @@ bool DistributedInputNodeManager::GetDevDhIdByFd(int fd, std::string &dhId, std: buffer[sizeof(buffer) - 1] = '\0'; physicalPath = buffer; - DHLOGD("GetDevDhIdByFd physicalPath %s.", physicalPath.c_str()); - dhId = physicalPath.substr(physicalPath.find(DH_ID_PREFIX)); - if (dhId.size() == 0) { - DHLOGE("Get dev dhid failed."); + DHLOGD("GetDevDhUniqueIdByFd physicalPath %s.", physicalPath.c_str()); + std::vector phyPathVec; + SplitStringToVector(physicalPath, VIR_NODE_SPLIT_CHAR, phyPathVec); + if (phyPathVec.size() != VIR_NODE_PHY_LEN) { + DHLOGE("The physical path is invalid"); + return false; + } + 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; } - DHLOGD("Device dhId %s.", GetAnonyString(dhId).c_str()); + dhUnqueId.first = devId; + dhUnqueId.second = dhId; + DHLOGD("Device deviceid: %s, dhId %s.", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); return true; } -void DistributedInputNodeManager::SetPathForDevMap(const std::string &dhId, const std::string &devicePath) +void DistributedInputNodeManager::SetPathForVirDev(const DhUniqueID &dhUniqueId, const std::string &devicePath) { std::lock_guard lock(virtualDeviceMapMutex_); auto iter = virtualDeviceMap_.begin(); while (iter != virtualDeviceMap_.end()) { - DHLOGD("Virtual device map dhid %s.", iter->first.c_str()); - if (dhId.compare(iter->first) == 0) { + 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); break; @@ -267,45 +282,59 @@ void DistributedInputNodeManager::SetPathForDevMap(const std::string &dhId, cons } } -void DistributedInputNodeManager::OpenInputDevice(const std::string &devicePath, const std::string &dhId) +bool DistributedInputNodeManager::MatchAndSavePhysicalPath(const std::string &devicePath, + const std::string &devId, const std::string &dhId) { DHLOGI("Opening input device path: %s", devicePath.c_str()); - std::string curDhId; + DhUniqueID curDhUniqueId; std::string physicalPath; int fd = OpenInputDeviceFdByPath(devicePath); if (fd == UN_INIT_FD_VALUE) { DHLOGE("The fd open failed, devicePath %s.", devicePath.c_str()); - return; + return false; } if (!IsVirtualDev(fd)) { DHLOGE("The dev not virtual, devicePath %s.", devicePath.c_str()); - return; + CloseFd(fd); + return false; } - if (!GetDevDhIdByFd(fd, curDhId, physicalPath) || dhId != curDhId) { - DHLOGE("This is not same dev, curDhId %s.", devicePath.c_str()); - return; + if (!GetDevDhUniqueIdByFd(fd, curDhUniqueId, physicalPath)) { + DHLOGE("Get unique id failed, device path %s.", devicePath.c_str()); + CloseFd(fd); + return false; + } + + 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()); + + if (curDhUniqueId.first != devId || curDhUniqueId.second != dhId) { + DHLOGW("It is not the target vir node, skip it."); + CloseFd(fd); + return false; } - DHLOGD("curDhId %s.", GetAnonyString(curDhId).c_str()); - SetPathForDevMap(curDhId, devicePath); + + SetPathForVirDev(curDhUniqueId, devicePath); + CloseFd(fd); + return true; } -void DistributedInputNodeManager::GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, - std::vector &virKeyboardPaths, std::vector &virKeyboardDhIds) +void DistributedInputNodeManager::GetVirtualKeyboardPaths(const std::vector &dhUniqueIds, + std::vector &virKeyboardPaths) { std::lock_guard lock(virtualDeviceMapMutex_); - for (const auto &dhId : dhIds) { + for (const auto &dhUniqueId : dhUniqueIds) { auto iter = virtualDeviceMap_.begin(); while (iter != virtualDeviceMap_.end()) { if (iter->second == nullptr) { DHLOGE("device is nullptr"); continue; } - if ((iter->first.compare(dhId) == 0) && + if ((iter->first == dhUniqueId) && ((iter->second->GetClasses() & INPUT_DEVICE_CLASS_KEYBOARD) != 0)) { - DHLOGI("Found vir keyboard path %s, dhid %s", iter->second->GetPath().c_str(), - GetAnonyString(dhId).c_str()); + DHLOGI("Found vir keyboard path %s, deviceId %s, dhid %s", iter->second->GetPath().c_str(), + GetAnonyString(dhUniqueId.first).c_str(), GetAnonyString(dhUniqueId.second).c_str()); virKeyboardPaths.push_back(iter->second->GetPath()); - virKeyboardDhIds.push_back(dhId); } iter++; } @@ -325,7 +354,7 @@ int32_t DistributedInputNodeManager::CreateHandle(const InputDevice &inputDevice DHLOGE("could not create new virtual device\n"); return ERR_DH_INPUT_SERVER_SOURCE_CREATE_HANDLE_FAIL; } - AddDeviceLocked(inputDevice.descriptor, std::move(virtualDevice)); + AddDeviceLocked(devId, inputDevice.descriptor, std::move(virtualDevice)); return DH_SUCCESS; } @@ -344,14 +373,14 @@ int32_t DistributedInputNodeManager::CreateVirtualTouchScreenNode(const std::str return ERR_DH_INPUT_SERVER_SOURCE_CREATE_HANDLE_FAIL; } virtualTouchScreenFd_ = device->GetDeviceFd(); - AddDeviceLocked(dhId, std::move(device)); + AddDeviceLocked(devId, dhId, std::move(device)); DHLOGI("CreateVirtualTouchScreenNode end, dhId: %s", GetAnonyString(dhId).c_str()); return DH_SUCCESS; } -int32_t DistributedInputNodeManager::RemoveVirtualTouchScreenNode(const std::string &dhId) +int32_t DistributedInputNodeManager::RemoveVirtualTouchScreenNode(const std::string &devId, const std::string &dhId) { - return CloseDeviceLocked(dhId); + return CloseDeviceLocked(devId, dhId); } int32_t DistributedInputNodeManager::GetVirtualTouchScreenFd() @@ -359,34 +388,43 @@ int32_t DistributedInputNodeManager::GetVirtualTouchScreenFd() return virtualTouchScreenFd_; } -void DistributedInputNodeManager::AddDeviceLocked(const std::string &dhId, std::unique_ptr device) +void DistributedInputNodeManager::AddDeviceLocked(const std::string &networkId, const std::string &dhId, + std::unique_ptr device) { - DHLOGI("dhId=%s", GetAnonyString(dhId).c_str()); + DHLOGI("AddDeviceLocked deviceId=%s, dhId=%s", + GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str()); std::lock_guard lock(virtualDeviceMapMutex_); - auto [dev_it, inserted] = virtualDeviceMap_.insert_or_assign(dhId, std::move(device)); + auto [dev_it, inserted] = virtualDeviceMap_.insert_or_assign( + {networkId, dhId}, std::move(device)); if (!inserted) { - DHLOGI("Device id %s exists, replaced. \n", GetAnonyString(dhId).c_str()); + DHLOGI("Device exists, deviceId=%s, dhId=%s, replaced. \n", + GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str()); } } -int32_t DistributedInputNodeManager::CloseDeviceLocked(const std::string &dhId) +int32_t DistributedInputNodeManager::CloseDeviceLocked(const std::string &devId, const std::string &dhId) { - DHLOGI("CloseDeviceLocked called, dhId=%s", GetAnonyString(dhId).c_str()); + DHLOGI("CloseDeviceLocked called, deviceId=%s, dhId=%s", + GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); std::lock_guard lock(virtualDeviceMapMutex_); - std::map>::iterator iter = virtualDeviceMap_.find(dhId); + DhUniqueID dhUniqueId = {devId, dhId}; + std::map>::iterator iter = virtualDeviceMap_.find(dhUniqueId); if (iter != virtualDeviceMap_.end()) { - DHLOGI("CloseDeviceLocked called success, dhId=%s", GetAnonyString(dhId).c_str()); + DHLOGI("CloseDeviceLocked called success, deviceId=%s, dhId=%s", + GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); virtualDeviceMap_.erase(iter); return DH_SUCCESS; } - DHLOGE("CloseDeviceLocked called failure, dhId=%s", GetAnonyString(dhId).c_str()); + DHLOGE("CloseDeviceLocked called failure, deviceId=%s, dhId=%s", + GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); 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 &devId, const std::string &dhId, + VirtualDevice *&device) { std::lock_guard lock(virtualDeviceMapMutex_); - auto iter = virtualDeviceMap_.find(dhId); + auto iter = virtualDeviceMap_.find({devId, dhId}); if (iter != virtualDeviceMap_.end()) { device = iter->second.get(); return DH_SUCCESS; @@ -420,19 +458,10 @@ void DistributedInputNodeManager::StopInjectThread() } } -void DistributedInputNodeManager::ReportEvent(const RawEvent rawEvent) +void DistributedInputNodeManager::ReportEvent(const std::string &devId, const std::vector &events) { std::lock_guard lockGuard(injectThreadMutex_); - injectQueue_.push(std::make_shared(rawEvent)); - conditionVariable_.notify_all(); -} - -void DistributedInputNodeManager::ReportEvent(const std::vector &events) -{ - std::lock_guard lockGuard(injectThreadMutex_); - for (auto ev : events) { - injectQueue_.push(std::make_shared(ev)); - } + injectQueue_.push({devId, events}); conditionVariable_.notify_all(); } @@ -444,7 +473,7 @@ void DistributedInputNodeManager::InjectEvent() } DHLOGI("start"); while (isInjectThreadRunning_.load()) { - std::shared_ptr event = nullptr; + EventBatch events; { std::unique_lock waitEventLock(injectThreadMutex_); conditionVariable_.wait(waitEventLock, @@ -452,16 +481,12 @@ void DistributedInputNodeManager::InjectEvent() if (injectQueue_.empty()) { continue; } - event = injectQueue_.front(); + events = injectQueue_.front(); injectQueue_.pop(); } - if (event == nullptr) { - DHLOGD("event is null!"); - continue; - } DHLOGD("process event, inject queue size: %zu", injectQueue_.size()); - ProcessInjectEvent(event); + ProcessInjectEvent(events); } DHLOGI("end"); } @@ -488,76 +513,26 @@ void DistributedInputNodeManager::RunInjectEventCallback(const std::string &dhId SessionStateCallback_->OnResult(dhId, DINPUT_INJECT_EVENT_FAIL); } -void DistributedInputNodeManager::ProcessInjectEvent(const std::shared_ptr &rawEvent) -{ - std::string dhId = rawEvent->descriptor; - struct input_event event = { - .type = rawEvent->type, - .code = rawEvent->code, - .value = rawEvent->value - }; - DHLOGI("InjectEvent dhId: %s, eventType: %d, eventCode: %d, eventValue: %d, when: " PRId64"", - GetAnonyString(dhId).c_str(), event.type, event.code, event.value, rawEvent->when); - VirtualDevice* device = nullptr; - if (GetDevice(dhId, device) < 0) { - DHLOGE("could not find the device"); - return; - } - if (device != nullptr) { - device->InjectInputEvent(event); - } -} - -int32_t DistributedInputNodeManager::GetDeviceInfo(std::string &deviceId) -{ - 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; - } - - deviceId = localNode->networkId; - DHLOGI("device id is %s", GetAnonyString(deviceId).c_str()); - return DH_SUCCESS; -} - -void DistributedInputNodeManager::GetDevicesInfoByType(const std::string &networkId, uint32_t inputTypes, - std::map &datas) -{ - uint32_t inputType = 0; - - if ((inputTypes & static_cast(DInputDeviceType::MOUSE)) != 0) { - inputType |= INPUT_DEVICE_CLASS_CURSOR; - } - - if ((inputTypes & static_cast(DInputDeviceType::KEYBOARD)) != 0) { - inputType |= INPUT_DEVICE_CLASS_KEYBOARD; - } - - if ((inputTypes & static_cast(DInputDeviceType::MOUSE)) != 0) { - inputType |= INPUT_DEVICE_CLASS_TOUCH; - } - - std::lock_guard lock(virtualDeviceMapMutex_); - for (const auto &[id, virdevice] : virtualDeviceMap_) { - if ((virdevice->GetDeviceType() & inputType) && (virdevice->GetNetWorkId() == networkId)) { - datas.insert(std::pair(virdevice->GetDeviceFd(), id)); +void DistributedInputNodeManager::ProcessInjectEvent(const EventBatch &events) +{ + std::string deviceId = events.first; + for (const auto &rawEvent : events.second) { + std::string dhId = rawEvent.descriptor; + struct input_event event = { + .type = rawEvent.type, + .code = rawEvent.code, + .value = rawEvent.value + }; + DHLOGI("InjectEvent deviceId: %s, dhId: %s, eventType: %d, eventCode: %d, eventValue: %d, when: " PRId64"", + GetAnonyString(deviceId).c_str(), GetAnonyString(dhId).c_str(), + event.type, event.code, event.value, rawEvent.when); + VirtualDevice* device = nullptr; + if (GetDevice(deviceId, dhId, device) < 0) { + DHLOGE("could not find the device"); + return; } - } -} - -void DistributedInputNodeManager::GetDevicesInfoByDhId( - std::vector dhidsVec, std::map &datas) -{ - for (auto dhId : dhidsVec) { - std::lock_guard lock(virtualDeviceMapMutex_); - for (const auto &[id, virdevice] : virtualDeviceMap_) { - if (id == dhId) { - datas.insert(std::pair(virdevice->GetDeviceFd(), id)); - break; - } + if (device != nullptr) { + device->InjectInputEvent(event); } } } diff --git a/services/source/inputinject/src/virtual_device.cpp b/services/source/inputinject/src/virtual_device.cpp index 799890efc2fb1f76396dda1fd6890e8342cbced5..f75dcbfa311d5af6f82756e0dc74be595de594ea 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 598f7a7f2aea5b7ff3878a98c2cba2298df24036..43f2c162413c90fc6dd16341325ce064668848ef 100644 --- a/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp +++ b/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp @@ -160,157 +160,129 @@ HWTEST_F(DistributedInputSourceInjectTest, UnregisterDistributedHardware_002, te HWTEST_F(DistributedInputSourceInjectTest, RegisterDistributedEvent01, testing::ext::TestSize.Level1) { - struct RawEvent writeBuffer[4]; - RawEvent* event = writeBuffer; - - event->when = 0; - event->type = EV_KEY; - event->code = KEY_D; - event->value = 1; - event->descriptor = "afv4s8b1dr1b8er1bd65fb16redb1dfb18d1b56df1b68d"; - event += 1; - - event->when = 1; - event->type = EV_KEY; - event->code = KEY_D; - event->value = 0; - event->descriptor = "afv4s8b1dr1b8er1bd65fb16redb1dfb18d1b56df1b68d"; - event += 1; - - event->when = 2; - event->type = EV_KEY; - event->code = KEY_D; - event->value = 1; - event->descriptor = "afv4s8b1dr1b8er1bd65fb16redb1dfb18d1b56df1b68d"; - event += 1; - - event->when = 3; - event->type = EV_KEY; - event->code = KEY_D; - event->value = 0; - event->descriptor = "afv4s8b1dr1b8er1bd65fb16redb1dfb18d1b56df1b68d"; - - size_t count = (size_t)(sizeof(writeBuffer) / sizeof(RawEvent)); + RawEvent event1 = { + .when = 0, + .type = EV_KEY, + .code = KEY_D, + .value = 1, + .descriptor = "afv4s8b1dr1b8er1bd65fb16redb1dfb18d1b56df1b68d" + }; + RawEvent event2 = { + .when = 1, + .type = EV_KEY, + .code = KEY_D, + .value = 0, + .descriptor = "afv4s8b1dr1b8er1bd65fb16redb1dfb18d1b56df1b68d" + }; + RawEvent event3 = { + .when = 2, + .type = EV_KEY, + .code = KEY_D, + .value = 1, + .descriptor = "afv4s8b1dr1b8er1bd65fb16redb1dfb18d1b56df1b68d" + }; + RawEvent event4 = { + .when = 3, + .type = EV_KEY, + .code = KEY_D, + .value = 0, + .descriptor = "afv4s8b1dr1b8er1bd65fb16redb1dfb18d1b56df1b68d" + }; + std::vector writeBuffer = { event1, event2, event3, event4 }; + DistributedInputInject::GetInstance().inputNodeManager_ = std::make_unique(); DistributedInputInject::GetInstance().StartInjectThread(); DistributedInputInject::GetInstance().StopInjectThread(); - int32_t ret = DistributedInputInject::GetInstance().RegisterDistributedEvent(writeBuffer, count); + std::string deviceId = "aefbg1nr81n521be8rb1erbe1w8bg1erb18"; + int32_t ret = DistributedInputInject::GetInstance().RegisterDistributedEvent(deviceId, writeBuffer); EXPECT_EQ(DH_SUCCESS, ret); } HWTEST_F(DistributedInputSourceInjectTest, RegisterDistributedEvent02, testing::ext::TestSize.Level1) { - struct RawEvent writeBuffer[4]; - RawEvent* event = writeBuffer; - - event->when = 0; - event->type = EV_REL; - event->code = REL_X; - event->value = 2; - event->descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18"; - event += 1; - - event->when = 1; - event->type = EV_REL; - event->code = REL_Y; - event->value = 2; - event->descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18"; - event += 1; - - event->when = 2; - event->type = EV_REL; - event->code = REL_X; - event->value = 2; - event->descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18"; - event += 1; - - event->when = 3; - event->type = EV_REL; - event->code = REL_Y; - event->value = 2; - event->descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18"; - - event->when = 4; - event->type = EV_SYN; - event->code = SYN_REPORT; - event->value = 0; - event->descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18"; - - size_t count = (size_t)(sizeof(writeBuffer) / sizeof(RawEvent)); - int32_t ret = DistributedInputInject::GetInstance().RegisterDistributedEvent(writeBuffer, count); + RawEvent event1 = { + .when = 0, + .type = EV_REL, + .code = REL_X, + .value = 2, + .descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18" + }; + RawEvent event2 = { + .when = 1, + .type = EV_REL, + .code = REL_Y, + .value = 2, + .descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18" + }; + RawEvent event3 = { + .when = 2, + .type = EV_REL, + .code = REL_X, + .value = 3, + .descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18" + }; + RawEvent event4 = { + .when = 3, + .type = EV_REL, + .code = REL_Y, + .value = 3, + .descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18" + }; + RawEvent event5 = { + .when = 4, + .type = EV_SYN, + .code = SYN_REPORT, + .value = 0, + .descriptor = "rt12r1nr81n521be8rb1erbe1w8bg1erb18" + }; + 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); } HWTEST_F(DistributedInputSourceInjectTest, RegisterDistributedEvent03, testing::ext::TestSize.Level1) { - struct RawEvent writeBuffer[4]; - RawEvent* event = writeBuffer; - - event->when = 0; - event->type = EV_ABS; - event->code = ABS_X; - event->value = 1; - event->descriptor = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; - event += 1; - - event->when = 1; - event->type = EV_ABS; - event->code = ABS_X; - event->value = 2; - event->descriptor = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; - event += 1; - - event->when = 2; - event->type = EV_ABS; - event->code = ABS_X; - event->value = 3; - event->descriptor = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; - event += 1; - - event->when = 3; - event->type = EV_ABS; - event->code = ABS_X; - event->value = 4; - event->descriptor = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; - - size_t count = (size_t)(sizeof(writeBuffer) / sizeof(RawEvent)); - int32_t ret = DistributedInputInject::GetInstance().RegisterDistributedEvent(writeBuffer, count); + RawEvent event1 = { + .when = 0, + .type = EV_ABS, + .code = ABS_X, + .value = 1, + .descriptor = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8" + }; + RawEvent event2 = { + .when = 1, + .type = EV_ABS, + .code = ABS_X, + .value = 2, + .descriptor = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8" + }; + RawEvent event3 = { + .when = 2, + .type = EV_ABS, + .code = ABS_X, + .value = 3, + .descriptor = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8" + }; + RawEvent event4 = { + .when = 3, + .type = EV_ABS, + .code = ABS_X, + .value = 4, + .descriptor = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8" + }; + std::vector writeBuffer = { event1, event2, event3, event4 }; + 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) { - struct RawEvent writeBuffer[4]; - size_t count = (size_t)(sizeof(writeBuffer) / sizeof(RawEvent)); - DistributedInputInject::GetInstance().inputNodeManager_ = nullptr; - int32_t ret = DistributedInputInject::GetInstance().RegisterDistributedEvent(writeBuffer, count); - EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL, ret); -} - -HWTEST_F(DistributedInputSourceInjectTest, GetDhIdsByInputType_001, testing::ext::TestSize.Level1) -{ - std::string devId = "umkyu1b165e1be98151891erbe8r91ev"; - std::vector dhIds; - std::string dhId = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; - dhIds.push_back(dhId); - std::shared_ptr rawEvent = nullptr; - DistributedInputInject::GetInstance().InputDeviceEventInject(rawEvent); - DistributedInputInject::GetInstance().inputNodeManager_ = std::make_unique(); - int32_t ret = DistributedInputInject::GetInstance().GetDhIdsByInputType(devId, - static_cast(DInputDeviceType::ALL), dhIds); - EXPECT_EQ(DH_SUCCESS, ret); -} - -HWTEST_F(DistributedInputSourceInjectTest, GetDhIdsByInputType_002, testing::ext::TestSize.Level1) -{ + std::string deviceId = "aefbg1nr81n521be8rb1erbe1w8bg1erb18"; + std::vector writeBuffer(4); DistributedInputInject::GetInstance().inputNodeManager_ = nullptr; - std::shared_ptr rawEvent = std::make_shared(); - DistributedInputInject::GetInstance().InputDeviceEventInject(rawEvent); - - std::string devId = "umkyu1b165e1be98151891erbe8r91ev"; - std::vector dhIds; - int32_t ret = DistributedInputInject::GetInstance().GetDhIdsByInputType(devId, - static_cast(DInputDeviceType::ALL), dhIds); + int32_t ret = DistributedInputInject::GetInstance().RegisterDistributedEvent(deviceId, writeBuffer); EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL, ret); } @@ -337,8 +309,9 @@ HWTEST_F(DistributedInputSourceInjectTest, GetVirtualTouchScreenFd_001, testing: HWTEST_F(DistributedInputSourceInjectTest, RemoveVirtualTouchScreenNode_001, testing::ext::TestSize.Level1) { + std::string devId = "umkyu1b165e1be98151891erbe8r91ev"; std::string dhId = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; - int32_t ret = DistributedInputInject::GetInstance().RemoveVirtualTouchScreenNode(dhId); + int32_t ret = DistributedInputInject::GetInstance().RemoveVirtualTouchScreenNode(devId, dhId); EXPECT_EQ(DH_SUCCESS, ret); } @@ -353,9 +326,10 @@ HWTEST_F(DistributedInputSourceInjectTest, CreateVirtualTouchScreenNode_002, tes HWTEST_F(DistributedInputSourceInjectTest, RemoveVirtualTouchScreenNode_002, testing::ext::TestSize.Level1) { + std::string devId = "umkyu1b165e1be98151891erbe8r91ev"; std::string dhId = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; DistributedInputInject::GetInstance().inputNodeManager_ = nullptr; - int32_t ret = DistributedInputInject::GetInstance().RemoveVirtualTouchScreenNode(dhId); + int32_t ret = DistributedInputInject::GetInstance().RemoveVirtualTouchScreenNode(devId, dhId); EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL, ret); } @@ -379,32 +353,18 @@ HWTEST_F(DistributedInputSourceInjectTest, CreateVirtualTouchScreenNode_003, tes HWTEST_F(DistributedInputSourceInjectTest, RemoveVirtualTouchScreenNode_003, testing::ext::TestSize.Level1) { + std::string devId = "umkyu1b165e1be98151891erbe8r91ev"; std::string dhId = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; - int32_t ret = DistributedInputInject::GetInstance().inputNodeManager_->RemoveVirtualTouchScreenNode(dhId); + int32_t ret = DistributedInputInject::GetInstance().inputNodeManager_->RemoveVirtualTouchScreenNode(devId, dhId); EXPECT_EQ(DH_SUCCESS, ret); } -HWTEST_F(DistributedInputSourceInjectTest, GetVirtualTouchScreenFd_003, testing::ext::TestSize.Level1) -{ - std::string networkId = "umkyu1b165e1be98151891erbe8r91ev"; - uint32_t inputTypes = 1; - std::map datas; - DistributedInputInject::GetInstance().inputNodeManager_->GetDevicesInfoByType(networkId, inputTypes, datas); - - inputTypes = 2; - DistributedInputInject::GetInstance().inputNodeManager_->GetDevicesInfoByType(networkId, inputTypes, datas); - - std::vector dhidsVec; - DistributedInputInject::GetInstance().inputNodeManager_->GetDevicesInfoByDhId(dhidsVec, datas); - int32_t ret = DistributedInputInject::GetInstance().inputNodeManager_->GetVirtualTouchScreenFd(); - EXPECT_NE(-1, ret); -} - HWTEST_F(DistributedInputSourceInjectTest, GetDevice_001, testing::ext::TestSize.Level1) { + std::string deviceId = "umkyu1b165e1be98151891erbe8r91ev"; std::string dhId = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; VirtualDevice* device = nullptr; - int32_t ret = DistributedInputInject::GetInstance().inputNodeManager_->GetDevice(dhId, device); + int32_t ret = DistributedInputInject::GetInstance().inputNodeManager_->GetDevice(deviceId, dhId, device); EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_GET_DEVICE_FAIL, ret); } @@ -445,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 diff --git a/services/source/sourcemanager/src/dinput_source_listener.cpp b/services/source/sourcemanager/src/dinput_source_listener.cpp index 1553424485c078832e5cdae718adddd44ac47831..5aadc16d19a7d88b6b283152dd099cc2d371374d 100644 --- a/services/source/sourcemanager/src/dinput_source_listener.cpp +++ b/services/source/sourcemanager/src/dinput_source_listener.cpp @@ -370,7 +370,7 @@ void DInputSourceListener::OnReceivedEventRemoteInput(const std::string deviceId ++idx; } - DistributedInputInject::GetInstance().RegisterDistributedEvent(mEventBuffer); + DistributedInputInject::GetInstance().RegisterDistributedEvent(deviceId, mEventBuffer); } void DInputSourceListener::OnReceiveRelayPrepareResult(int32_t status, diff --git a/services/source/sourcemanager/src/distributed_input_source_manager.cpp b/services/source/sourcemanager/src/distributed_input_source_manager.cpp index a0fbb8a698e23e76a1f1071e038dc469b9d446fb..48f4c2d5187d13542643fab54092f40a263a42e5 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -302,7 +302,7 @@ int32_t DistributedInputSourceManager::RegisterDistributedHardware(const std::st callback->OnResult(devId, dhId, DH_SUCCESS); // 5. Notify node mgr to scan vir dev node info - DistributedInputInject::GetInstance().NotifyNodeMgrScanVirNode(dhId); + DistributedInputInject::GetInstance().NotifyNodeMgrScanVirNode(devId, dhId); return DH_SUCCESS; } @@ -1580,7 +1580,8 @@ void DistributedInputSourceManager::RunKeyStateCallback(const std::string &sinkI mEventBuffer.code = code; mEventBuffer.value = value; mEventBuffer.descriptor = dhId; - DistributedInputInject::GetInstance().RegisterDistributedEvent(&mEventBuffer, DINPUT_SOURCE_WRITE_EVENT_SIZE); + std::vector eventBuffers = {mEventBuffer}; + DistributedInputInject::GetInstance().RegisterDistributedEvent(sinkId, eventBuffers); return; } @@ -1801,7 +1802,7 @@ void DistributedInputSourceManager::StopDScreenListener::OnMessage(const DHTopic SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(screenInfoKey); int32_t removeNodeRes = DistributedInputInject::GetInstance().RemoveVirtualTouchScreenNode( - srcScreenInfo.sourcePhyId); + sourceDevId, srcScreenInfo.sourcePhyId); if (removeNodeRes != DH_SUCCESS) { DHLOGE("Remove virtual touch screen node failed!"); return; @@ -1880,9 +1881,9 @@ void DistributedInputSourceManager::DScreenSourceSvrRecipient::OnRemoteDied(cons } std::string screenInfoKey = DInputContext::GetInstance().GetScreenInfoKey(srcDevId_, srcWinId_); SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(screenInfoKey); - + std::string sourceDevId = GetLocalNetworkId(); int32_t removeNodeRes = DistributedInputInject::GetInstance().RemoveVirtualTouchScreenNode( - srcScreenInfo.sourcePhyId); + sourceDevId, srcScreenInfo.sourcePhyId); if (removeNodeRes != DH_SUCCESS) { DHLOGE("Remove virtual touch screen node failed!"); return; diff --git a/services/source/transport/include/distributed_input_source_transport.h b/services/source/transport/include/distributed_input_source_transport.h index 6441f19b16c33edac2b6e1e593c7fe2de63ccc24..88bcba6b0f39a650389559698693422111689235 100644 --- a/services/source/transport/include/distributed_input_source_transport.h +++ b/services/source/transport/include/distributed_input_source_transport.h @@ -139,7 +139,7 @@ private: std::vector SplitDhIdString(const std::string &dhIdsString); void RegRespFunMap(); - void ResetKeyboardKeyState(const std::vector &dhids); + void ResetKeyboardKeyState(const std::string &deviceId, const std::vector &dhids); 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 5bd4c311725e4afae706f073a6acd5b30cf1b716..031f10c890acef3982464aa08f073b0c6f322972 100644 --- a/services/source/transport/src/distributed_input_source_transport.cpp +++ b/services/source/transport/src/distributed_input_source_transport.cpp @@ -326,7 +326,7 @@ int32_t DistributedInputSourceTransport::StopRemoteInputDhids(int32_t srcTsrcSeI } DHLOGI("StopRemoteInputDhids srcTsrcSeId:%d, sinkSessionId:%d.", srcTsrcSeId, sinkSessionId); std::vector dhIdsVec = SplitDhIdString(dhids); - ResetKeyboardKeyState(dhIdsVec); + ResetKeyboardKeyState(deviceId, dhIdsVec); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_STOP_DHID_FOR_REL; @@ -754,13 +754,13 @@ int32_t DistributedInputSourceTransport::StartRemoteInput(const std::string &dev return DH_SUCCESS; } -void DistributedInputSourceTransport::ResetKeyboardKeyState(const std::vector &dhids) +void DistributedInputSourceTransport::ResetKeyboardKeyState(const std::string &deviceId, + const std::vector &dhids) { std::vector keyboardNodePaths; - std::vector keyboardNodeDhIds; - DistributedInputInject::GetInstance().GetVirtualKeyboardPathsByDhIds(dhids, keyboardNodePaths, keyboardNodeDhIds); + DistributedInputInject::GetInstance().GetVirtualKeyboardPaths(deviceId, dhids, keyboardNodePaths); DHLOGI("Try reset keyboard states, dhIds: %s, nodePaths: %s", - GetString(keyboardNodeDhIds).c_str(), GetString(keyboardNodePaths).c_str()); + GetString(dhids).c_str(), GetString(keyboardNodePaths).c_str()); ResetVirtualDevicePressedKeys(keyboardNodePaths); } @@ -773,7 +773,7 @@ int32_t DistributedInputSourceTransport::StopRemoteInput(const std::string &devi return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_STOP_FAIL; } DHLOGI("StopRemoteInput sessionId:%d.", sessionId); - ResetKeyboardKeyState(dhids); + ResetKeyboardKeyState(deviceId, dhids); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_STOP_DHID;