From 9128ee5ed734c58674d8ba37946811d3bd74c3eb Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Sat, 22 Jul 2023 19:02:02 +0800 Subject: [PATCH 01/18] fix automatic input Signed-off-by: liqiao49 --- bundle.json | 1 + common/include/constants_dinput.h | 3 + common/include/input_hub.cpp | 22 ++ common/include/input_hub.h | 2 + .../include/distributed_input_collector.h | 2 + .../src/distributed_input_collector.cpp | 10 + services/sink/sinkmanager/BUILD.gn | 2 + .../include/distributed_input_sink_manager.h | 1 + .../src/distributed_input_sink_manager.cpp | 16 ++ services/source/inputinject/BUILD.gn | 1 + .../include/distributed_input_inject.h | 3 + .../include/distributed_input_node_manager.h | 10 + .../inputinject/include/virtual_device.h | 4 + .../src/distributed_input_inject.cpp | 10 + .../src/distributed_input_node_manager.cpp | 169 +++++++++++++ .../source/inputinject/src/virtual_device.cpp | 15 ++ services/source/sourcemanager/BUILD.gn | 2 + .../distributed_input_source_manager.h | 1 + .../src/distributed_input_source_manager.cpp | 12 + services/source/transport/BUILD.gn | 2 + .../distributed_input_source_transport.h | 3 + .../distributed_input_source_transport.cpp | 26 ++ services/state/BUILD.gn | 62 +++++ services/state/include/dinput_state.h | 72 ++++++ services/state/src/dinput_state.cpp | 233 ++++++++++++++++++ sourcehandler/BUILD.gn | 1 + 26 files changed, 685 insertions(+) create mode 100644 services/state/BUILD.gn create mode 100644 services/state/include/dinput_state.h create mode 100644 services/state/src/dinput_state.cpp diff --git a/bundle.json b/bundle.json index 993070c..5e3419c 100755 --- a/bundle.json +++ b/bundle.json @@ -62,6 +62,7 @@ "//foundation/distributedhardware/distributed_input/services/sink/transport:libdinput_sink_trans", "//foundation/distributedhardware/distributed_input/services/sink/inputcollector:libdinput_collector", "//foundation/distributedhardware/distributed_input/services/transportbase:libdinput_trans_base", + "//foundation/distributedhardware/distributed_input/services/state:libdinput_state", "//foundation/distributedhardware/distributed_input/sourcehandler:libdinput_source_handler", "//foundation/distributedhardware/distributed_input/sinkhandler:libdinput_sink_handler", "//foundation/distributedhardware/distributed_input/inputdevicehandler:libdinput_handler", diff --git a/common/include/constants_dinput.h b/common/include/constants_dinput.h index fd19bf7..1fc7b40 100644 --- a/common/include/constants_dinput.h +++ b/common/include/constants_dinput.h @@ -38,6 +38,7 @@ namespace DistributedInput { const char INPUT_STRING_SPLIT_POINT = '.'; const uint32_t KEY_DOWN_STATE = 1; + const uint32_t KEY_UP_STATE = 0; const uint32_t READ_SLEEP_TIME_MS = 50; const uint32_t READ_RETRY_MAX = 5; const uint32_t DH_ID_LENGTH_MAX = 256; @@ -194,6 +195,8 @@ 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 410e4f4..0d3387b 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -1140,6 +1140,28 @@ void InputHub::GetShareMousePathByDhId(std::vector dhIds, std::stri } } +void InputHub::GetShareKeyboardPathByDhId(std::vector dhIds, + std::vector &shareDhidsPath, std::vector &shareDhIds) +{ + DHLOGI("GetShareKeyboardPathByDhId: devices_.size:%d,", devices_.size()); + std::unique_lock deviceLock(devicesMutex_); + for (auto dhId_ : dhIds) { + for (const auto &[id, device] : devices_) { + if (device == nullptr) { + DHLOGE("device is nullptr"); + continue; + } + DHLOGI("descriptor:%s, isShare[%d], type[%d]", GetAnonyString(device->identifier.descriptor).c_str(), + device->isShare, device->classes); + if ((device->identifier.descriptor == dhId_) && + ((device->classes & INPUT_DEVICE_CLASS_KEYBOARD) != 0)) { + shareDhIds.push_back(dhId_); + shareDhidsPath.push_back(device->path); + } + } + } +} + void InputHub::GetDevicesInfoByType(const uint32_t inputTypes, std::map &datas) { uint32_t dhType = 0; diff --git a/common/include/input_hub.h b/common/include/input_hub.h index 71ab4a7..82d951f 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -53,6 +53,8 @@ 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, std::vector &shareDhIds); bool IsAllDevicesStoped(); void ScanInputDevices(const std::string& dirname); diff --git a/services/sink/inputcollector/include/distributed_input_collector.h b/services/sink/inputcollector/include/distributed_input_collector.h index 6cad8f6..1f95ea7 100644 --- a/services/sink/inputcollector/include/distributed_input_collector.h +++ b/services/sink/inputcollector/include/distributed_input_collector.h @@ -43,6 +43,8 @@ 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, std::vector &shareDhIds); // false for sharing device exist, true for all devices stop sharing bool IsAllDevicesStoped(); int32_t RegisterSharingDhIdListener(sptr sharingDhIdListener); diff --git a/services/sink/inputcollector/src/distributed_input_collector.cpp b/services/sink/inputcollector/src/distributed_input_collector.cpp index 0f664f4..0725244 100644 --- a/services/sink/inputcollector/src/distributed_input_collector.cpp +++ b/services/sink/inputcollector/src/distributed_input_collector.cpp @@ -214,6 +214,16 @@ void DistributedInputCollector::GetMouseNodePath( inputHub_->GetShareMousePathByDhId(dhIds, mouseNodePath, dhid); } +void DistributedInputCollector::GetKeyboardNodePath( + std::vector dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds) +{ + if (inputHub_ == nullptr) { + DHLOGI("inputHub is nullptr!"); + return; + } + inputHub_->GetShareKeyboardPathByDhId(dhIds, shareDhidsPaths, shareDhIds); +} + bool DistributedInputCollector::IsAllDevicesStoped() { if (inputHub_ == nullptr) { diff --git a/services/sink/sinkmanager/BUILD.gn b/services/sink/sinkmanager/BUILD.gn index 49a96ba..7b02237 100644 --- a/services/sink/sinkmanager/BUILD.gn +++ b/services/sink/sinkmanager/BUILD.gn @@ -35,6 +35,7 @@ ohos_shared_library("libdinput_sink") { "${utils_path}/include", "${fwk_interfaces_path}/include", "${fwk_interfaces_path}/include/ipc", + "${distributedinput_path}/services/state/include", ] sources = [ @@ -55,6 +56,7 @@ ohos_shared_library("libdinput_sink") { "${innerkits_path}:libdinput_sdk", "${services_sink_path}/inputcollector:libdinput_collector", "${services_sink_path}/transport:libdinput_sink_trans", + "${distributedinput_path}/services/state:libdinput_state", "${utils_path}:libdinput_utils", "//third_party/libevdev:libevdev", ] diff --git a/services/sink/sinkmanager/include/distributed_input_sink_manager.h b/services/sink/sinkmanager/include/distributed_input_sink_manager.h index 1fd3bda..0fa973c 100644 --- a/services/sink/sinkmanager/include/distributed_input_sink_manager.h +++ b/services/sink/sinkmanager/include/distributed_input_sink_manager.h @@ -37,6 +37,7 @@ #include "dinput_sink_trans_callback.h" #include "distributed_input_sink_stub.h" #include "distributed_input_sink_event_handler.h" +#include "dinput_state.h" namespace OHOS { namespace DistributedHardware { diff --git a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp index a986d3d..099f733 100644 --- a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp +++ b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp @@ -276,6 +276,8 @@ void DistributedInputSinkManager::DInputSinkListener::OnStartRemoteInputDhid(con AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, vecStr); sinkManagerObj_->StoreStartDhids(sessionId, affDhIds.sharingDhIds); DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); + StateMachine::GetInstance().AddDhids(vecStr); + StateMachine::GetInstance().SwitchState(vecStr, DhidState::THROUGH_OUT); } } @@ -292,6 +294,9 @@ void DistributedInputSinkManager::DInputSinkListener::OnStopRemoteInputDhid(cons stopIndeedOnes.noSharingDhIds = stopIndeedDhIds; DistributedInputCollector::GetInstance().ReportDhIdSharingState(stopIndeedOnes); + StateMachine::GetInstance().AddDhids(stopOnCmdDhIds); + StateMachine::GetInstance().SwitchState(stopOnCmdDhIds, DhidState::THROUGH_IN); + if (DistributedInputCollector::GetInstance().IsAllDevicesStoped()) { DHLOGE("All dhid stop sharing, sessionId: %d is closed.", sessionId); DistributedInputSinkSwitch::GetInstance().StopSwitch(sessionId); @@ -345,6 +350,8 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStartDhidRemoteInpu AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, vecStr); sinkManagerObj_->StoreStartDhids(toSinkSessionId, affDhIds.sharingDhIds); DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); + StateMachine::GetInstance().AddDhids(vecStr); + StateMachine::GetInstance().SwitchState(vecStr, DhidState::THROUGH_OUT); } } @@ -361,6 +368,9 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStopDhidRemoteInput stopIndeedOnes.noSharingDhIds = stopIndeedDhIds; DistributedInputCollector::GetInstance().ReportDhIdSharingState(stopIndeedOnes); + StateMachine::GetInstance().AddDhids(stopOnCmdDhIds); + StateMachine::GetInstance().SwitchState(stopOnCmdDhIds, DhidState::THROUGH_IN); + if (DistributedInputCollector::GetInstance().IsAllDevicesStoped()) { DHLOGE("All dhid stop sharing, sessionId: %d is closed.", toSinkSessionId); DistributedInputSinkSwitch::GetInstance().StopSwitch(toSinkSessionId); @@ -695,6 +705,12 @@ int32_t DistributedInputSinkManager::Init() return ERR_DH_INPUT_SERVER_SINK_MANAGER_INIT_FAIL; } + ret = StateMachine::GetInstance().Init(); + if (ret != DH_SUCCESS) { + DHLOGE("StateMachine init fail!"); + return ERR_DH_INPUT_SERVER_SINK_MANAGER_INIT_FAIL; + } + statuslistener_ = std::make_shared(this); DistributedInputSinkTransport::GetInstance().RegistSinkRespCallback(statuslistener_); diff --git a/services/source/inputinject/BUILD.gn b/services/source/inputinject/BUILD.gn index 34e49dd..07b4208 100644 --- a/services/source/inputinject/BUILD.gn +++ b/services/source/inputinject/BUILD.gn @@ -30,6 +30,7 @@ ohos_shared_library("libdinput_inject") { "${fwk_interfaces_path}/include", "${fwk_interfaces_path}/include/ipc", "${distributedinput_path}/inputdevicehandler/include", + "${distributedinput_path}/services/state/include", ] sources = [ diff --git a/services/source/inputinject/include/distributed_input_inject.h b/services/source/inputinject/include/distributed_input_inject.h index 0da45f7..fe351b8 100644 --- a/services/source/inputinject/include/distributed_input_inject.h +++ b/services/source/inputinject/include/distributed_input_inject.h @@ -56,6 +56,9 @@ public: const std::string &sinkNodeDesc); void SyncNodeOfflineInfo(const std::string &srcDevId, const std::string &sinkDevId, const std::string &sinkNodeId); + void GetVirtualKeyboardPathByDhId(std::vector &dhIds, std::vector &shareDhidsPaths, + std::vector &shareDhIds); + 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 9c3d037..a217138 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -56,6 +56,8 @@ public: void GetDevicesInfoByDhId(std::vector dhidsVec, std::map &datas); void ProcessInjectEvent(const std::shared_ptr &rawEvent); + void GetVirtualKeyboardPathByDhId(std::vector &dhIds, std::vector &shareDhidsPaths, + std::vector &shareDhIds); private: void AddDeviceLocked(const std::string& dhId, std::unique_ptr device); int32_t CreateHandle(const InputDevice& inputDevice, const std::string& devId, const std::string& dhId); @@ -63,6 +65,14 @@ private: void VerifyInputDevice(const nlohmann::json& inputDeviceJson, InputDevice& pBuf); void InjectEvent(); + void ScanSinkInputDevices(const std::string& dirName); + void OpenInputDevice(const std::string& devicePath); + int OpenInputDeviceFdByPath(std::string& canonicalDevicePath); + bool IsVirtualDev(int fd); + bool GetDevDhIdFd(int fd, std::string& dhId, std::string& physicalPath); + void SetPathForDevMap(std::string& dhId, const std::string& devicePath); + + /* the key is dhId, and the value is virtualDevice */ std::map> virtualDeviceMap_; std::mutex virtualDeviceMapMutex_; diff --git a/services/source/inputinject/include/virtual_device.h b/services/source/inputinject/include/virtual_device.h index 6bbe883..9dc0b36 100644 --- a/services/source/inputinject/include/virtual_device.h +++ b/services/source/inputinject/include/virtual_device.h @@ -43,7 +43,10 @@ public: bool SetUp(const InputDevice& inputDevice, const std::string &devId, const std::string &dhId); bool InjectInputEvent(const input_event &event); void SetNetWorkId(const std::string netWorkId); + void SetPath(const std::string path); std::string GetNetWorkId(); + std::string GetPath(); + uint16_t GetClasses(); int32_t GetDeviceFd(); uint16_t GetDeviceType(); @@ -52,6 +55,7 @@ private: int32_t fd_ = -1; std::string deviceName_; std::string netWorkId_; + std::string path_ {""}; const uint16_t busType_; const uint16_t vendorId_; const uint16_t productId_; diff --git a/services/source/inputinject/src/distributed_input_inject.cpp b/services/source/inputinject/src/distributed_input_inject.cpp index b3511b2..5a64585 100644 --- a/services/source/inputinject/src/distributed_input_inject.cpp +++ b/services/source/inputinject/src/distributed_input_inject.cpp @@ -255,6 +255,16 @@ int32_t DistributedInputInject::GetVirtualTouchScreenFd() } return inputNodeManager_->GetVirtualTouchScreenFd(); } + +void GetVirtualKeyboardPathByDhId(std::vector &dhIds, std::vector &shareDhidsPaths, + std::vector &shareDhIds) +{ + if (inputNodeManager_ == nullptr) { + DHLOGI("inputNodeManager_ is nullptr!"); + return; + } + inputNodeManager_->GetVirtualKeyboardPathByDhId(dhIds, shareDhidsPaths, shareDhIds); +} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index 6c4838b..fa541b8 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -21,6 +21,8 @@ #include #include #include +#include +#include #include "softbus_bus_center.h" @@ -33,6 +35,11 @@ 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) { @@ -130,6 +137,167 @@ 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 Closed(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; + } + 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); + OpenInputDeviceLocked(tmpDevName); + } + closedir(dir); +} + +bool DistributedInputNodeManager::IsVirtualDev(int fd) +{ + char buffer[256] = {0}; + std::string deviceName; + if (ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) { + DHLOGE("Could not get device name for %s", ConvertErrNo().c_str()); + return false; + } + buffer[sizeof(buffer) - 1] = '\0'; + deviceName = buffer; + + DHLOGD("IsVirtualDev deviceName: %s", buffer); + if (identifier.name.find(VIRTUAL_DEVICE_NAME) == std::string::npos) { + DHLOGD("This is not a virtual device, fd %d, deviceName: %s", fd, deviceName.c_str()); + return false; + } + return true; +} + +bool DistributedInputNodeManager::GetDevDhIdFd(int fd, std::string& dhId, std::string& physicalPath) +{ + char buffer[256] = {0}; + if (ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) { + DHLOGE("Could not get device name 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_")); + if (dhId.size() == 0) { + DHLOGE("Get dev dhid failed."); + return false; + } + DHLOGD("Device dhId %s", GetAnonyString(dhId).c_str()); + return true; +} + +void DistributedInputNodeManager::SetPathForDevMap(std::string& dhId, const std::string& devicePath) +{ + auto iter = virtualDeviceMap_.begin(); + while (iter != virtualDeviceMap_.end()) { + DHLOGD("Virtual device map dhid %s.", iter->first.c_str()); + if (dhId.compare(iter->first) == 0) { + DHLOGI("Found the virtual device, set path :%s", devicePath.c_str()); + iter->second->SetPath(devicePath) + break; + } + iter++; + } +} + +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; + } + if (fd =-1) { + DHLOGE("The fd open failed, devicePath %s\n", devicePath.c_str()); + return; + } + if (IsVirtualDev(fd) == false) { + DHLOGE("The dev not virtual, devicePath %s\n", devicePath.c_str()); + return; + } + if (GetDevDhIdFd(fd, dhId, physicalPath) == false) { + DHLOGE("Get dev dhid failed, devicePath %s\n", devicePath.c_str()); + return; + } + SetPathForDevMap(dhId, devicePath); +} + +void GetVirtualKeyboardPathByDhId(std::vector &dhIds, std::vector &shareDhidsPaths, + std::vector &shareDhIds); +{ + for (auto dhId_ : dhIds) { + auto iter = virtualDeviceMap_.begin(); + while (iter != virtualDeviceMap_.end()) { + if (iter->second == nullptr) { + DHLOGE("device is nullptr"); + continue; + } + if ((iter->first.compare(dhId_) == 0) && ((iter->second->GetClasses() & INPUT_DEVICE_CLASS_KEYBOARD) != 0)) { + DHLOGI("Found vir keyboard path %s, dhid %s", itersecond->GetPath().c_str()); + shareDhidsPath.push_back(diter->second->GetPath()); + shareDhIds.push_back(dhId_); + } + iter++; + } + } +} + int32_t DistributedInputNodeManager::CreateHandle(const InputDevice& inputDevice, const std::string& devId, const std::string& dhId) { @@ -144,6 +312,7 @@ int32_t DistributedInputNodeManager::CreateHandle(const InputDevice& inputDevice return ERR_DH_INPUT_SERVER_SOURCE_CREATE_HANDLE_FAIL; } AddDeviceLocked(inputDevice.descriptor, std::move(virtualDevice)); + ScanSinkInputDevices(DEVICE_PATH); return DH_SUCCESS; } diff --git a/services/source/inputinject/src/virtual_device.cpp b/services/source/inputinject/src/virtual_device.cpp index feef9e2..7e7e1cc 100644 --- a/services/source/inputinject/src/virtual_device.cpp +++ b/services/source/inputinject/src/virtual_device.cpp @@ -185,11 +185,26 @@ void VirtualDevice::SetNetWorkId(const std::string netWorkId) netWorkId_ = netWorkId; } +void SetPath(const std::string path) +{ + path_ = path; +} + std::string VirtualDevice::GetNetWorkId() { return netWorkId_; } +std::string GetPath() +{ + return path_; +} + +uint16_t GetClasses() +{ + return classes_; +} + void VirtualDevice::RecordEventLog(const input_event& event) { std::string eventType = ""; diff --git a/services/source/sourcemanager/BUILD.gn b/services/source/sourcemanager/BUILD.gn index 08e3a55..8a8b872 100644 --- a/services/source/sourcemanager/BUILD.gn +++ b/services/source/sourcemanager/BUILD.gn @@ -37,6 +37,7 @@ ohos_shared_library("libdinput_source") { "${fwk_interfaces_path}/include", "${fwk_interfaces_path}/include/ipc", "${distributedinput_path}/inputdevicehandler/include", + "${distributedinput_path}/services/state/include", ] sources = [ @@ -91,6 +92,7 @@ ohos_shared_library("libdinput_source") { "${services_source_path}/inputinject:libdinput_inject", "${services_source_path}/transport:libdinput_source_trans", "${utils_path}:libdinput_utils", + "${distributedinput_path}/services/state:libdinput_state", "//third_party/libevdev:libevdev", ] diff --git a/services/source/sourcemanager/include/distributed_input_source_manager.h b/services/source/sourcemanager/include/distributed_input_source_manager.h index 412c2a4..f7cda1e 100644 --- a/services/source/sourcemanager/include/distributed_input_source_manager.h +++ b/services/source/sourcemanager/include/distributed_input_source_manager.h @@ -37,6 +37,7 @@ #include "distributed_input_source_event_handler.h" #include "distributed_input_source_sa_cli_mgr.h" #include "distributed_input_source_stub.h" +#include "dinput_state.h" namespace OHOS { namespace DistributedHardware { diff --git a/services/source/sourcemanager/src/distributed_input_source_manager.cpp b/services/source/sourcemanager/src/distributed_input_source_manager.cpp index 39a78ef..e99bf1f 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -34,6 +34,7 @@ #include "dinput_errcode.h" #include "dinput_hitrace.h" #include "dinput_log.h" +#include "dinput_state.h" #include "dinput_utils_tool.h" #include "distributed_input_client.h" #include "distributed_input_inject.h" @@ -282,6 +283,11 @@ void DistributedInputSourceManager::DInputSourceListener::OnResponseStartRemoteI sourceManagerObj_->SetDeviceMapValue(deviceId, DINPUT_SOURCE_SWITCH_ON); } + std::vector vecStr; + sourceManagerObj_->StringSplitToVector(dhids, INPUT_STRING_SPLIT_POINT, vecStr); + StateMachine::GetInstance().AddDhid(vecStr); + StateMachine::GetInstance().SwitchState(vecStr, DhidState::THROUGH_IN); + std::shared_ptr jsonArrayMsg = std::make_shared(); nlohmann::json tmpJson; tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = deviceId; @@ -971,6 +977,12 @@ int32_t DistributedInputSourceManager::Init() dhFwkKit->RegisterPublisherListener(DHTopic::TOPIC_STOP_DSCREEN, stopDScreenListener_); dhFwkKit->RegisterPublisherListener(DHTopic::TOPIC_DEV_OFFLINE, deviceOfflineListener_); + ret = StateMachine::GetInstance().Init(); + if (ret != DH_SUCCESS) { + DHLOGE("StateMachine init fail!"); + return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_INIT_FAIL; + } + return DH_SUCCESS; } diff --git a/services/source/transport/BUILD.gn b/services/source/transport/BUILD.gn index 5796bcb..ad1bb96 100755 --- a/services/source/transport/BUILD.gn +++ b/services/source/transport/BUILD.gn @@ -31,6 +31,7 @@ ohos_shared_library("libdinput_source_trans") { "${frameworks_path}/include", "${distributedinput_path}/inputdevicehandler/include", "${distributedinput_path}/services/transportbase/include", + "${distributedinput_path}/services/state/include", ] sources = [ "src/distributed_input_source_transport.cpp" ] @@ -45,6 +46,7 @@ ohos_shared_library("libdinput_source_trans") { "${dfx_utils_path}:libdinput_dfx_utils", "${distributedinput_path}/services/transportbase:libdinput_trans_base", "${services_source_path}/inputinject:libdinput_inject", + "${services_source_path}/services/state:libdinput_state", "${utils_path}:libdinput_utils", "//third_party/libevdev:libevdev", ] diff --git a/services/source/transport/include/distributed_input_source_transport.h b/services/source/transport/include/distributed_input_source_transport.h index 08d40df..6daf67c 100644 --- a/services/source/transport/include/distributed_input_source_transport.h +++ b/services/source/transport/include/distributed_input_source_transport.h @@ -29,6 +29,7 @@ #include "nlohmann/json.hpp" #include "securec.h" +#include "dinput_state.h" #include "dinput_source_trans_callback.h" #include "dinput_transbase_source_callback.h" @@ -137,6 +138,8 @@ 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 e8676da..a49e6e0 100644 --- a/services/source/transport/src/distributed_input_source_transport.cpp +++ b/services/source/transport/src/distributed_input_source_transport.cpp @@ -286,6 +286,24 @@ 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) { @@ -296,6 +314,11 @@ int32_t DistributedInputSourceTransport::StartRemoteInputDhids(int32_t srcTsrcSe } DHLOGI("StartRemoteInputDhids srcTsrcSeId:%d, sinkSessionId:%d.", srcTsrcSeId, sinkSessionId); + std::vector vecStr; + StringSplitToVector(dhids, INPUT_STRING_SPLIT_POINT, vecStr); + StateMachine::GetInstance().AddDhids(vecStr); + StateMachine::GetInstance().SwitchState(vecStr,DhidState::THROUGH_IN); + nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_START_DHID_FOR_REL; jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = deviceId; @@ -759,6 +782,9 @@ int32_t DistributedInputSourceTransport::StopRemoteInput(const std::string &devi } DHLOGI("StopRemoteInput sessionId:%d.", sessionId); + StateMachine::GetInstance().AddDhids(dhids); + StateMachine::GetInstance().SwitchState(dhids,DhidState::THROUGH_OUT); + nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_STOP_DHID; jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = deviceId; diff --git a/services/state/BUILD.gn b/services/state/BUILD.gn new file mode 100644 index 0000000..97ab868 --- /dev/null +++ b/services/state/BUILD.gn @@ -0,0 +1,62 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import( + "//foundation/distributedhardware/distributed_input/distributedinput.gni") + +ohos_shared_library("libdinput_state") { + include_dirs = [ + "include", + "${common_path}/include", + "${utils_path}/include", + "${services_sink_path}/inputcollector/include", + "${services_source_path}/inputdevicehandler/include", + "${services_sink_path}/inputcollector/include", + "${frameworks_path}/include", + "${fWK_common_path}/utils/include", + ] + + sources = [ "src/dinput_state.cpp" ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"distributedinputstate\"", + "LOG_DOMAIN=0xD004100", + ] + + deps = [ + "${dfx_utils_path}:libdinput_dfx_utils", + "${services_source_path}/inputinject:libdinput_inject", + "${services_sink_path}/inputcollector:libdinput_collector", + "${utils_path}:libdinput_utils", + "//third_party/libevdev:libevdev", + ] + + external_deps = [ + "c_utils:utils", + "distributed_hardware_fwk:libdhfwk_sdk", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "hitrace:hitrace_meter", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + + subsystem_name = "distributedhardware" + + part_name = "distributed_input" +} diff --git a/services/state/include/dinput_state.h b/services/state/include/dinput_state.h new file mode 100644 index 0000000..cbfb450 --- /dev/null +++ b/services/state/include/dinput_state.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISTRIBUTED_INPUT_STATE_BASE_H +#define DISTRIBUTED_INPUT_STATE_BASE_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { + +enum class DhidState +{ + INIT = 0, + THROUGH_IN, + THROUGH_OUT, +}; + +class StateMachine{ +public: + static StateMachine &GetInstance() { + static StateMachine instance; + return instance; + }; + + int32_t Init(); + 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); + DhidState GetStateByDhid(std::string &dhid); + +private: + ~StateMachine(); + + 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); + +private: + std::map dhidStateMap_; +}; + +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS +#endif // DISTRIBUTED_INPUT_STATE_BASE_H \ No newline at end of file diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp new file mode 100644 index 0000000..9b96352 --- /dev/null +++ b/services/state/src/dinput_state.cpp @@ -0,0 +1,233 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dinput_state.h" + +#include "dinput_errcode.h" +#include "dinput_log.h" +#include "dinput_utils_tool.h" +#include "constants_dinput.h" +#include "distributed_input_collector.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { + +StateMachine::~StateMachine() +{ + Release(); +} + +int32_t StateMachine::Init() +{ + DHLOGI("StateMachine Init."); + return DH_SUCCESS; +} + +int32_t StateMachine::Release() +{ + DHLOGI("StateMachine Release."); + dhidStateMap_.clear(); + return DH_SUCCESS; +} + +int32_t StateMachine::AddDhids(std::vector &dhids) +{ + DHLOGI("AddDhid dhids size = %zu", dhids.size()); + for (auto &dhid : dhids) { + DHLOGD("add dhid : %s", GetAnonyString(dhid).c_str()); + if (IsExistDhid(dhid)) { + DHLOGI("dhid : %s already exist.", GetAnonyString(dhid).c_str()); + } else { + dhidStateMap_[dhid] = DhidState::INIT; + } + } + return DH_SUCCESS; +} + +int32_t StateMachine::DeleteDhids(std::vector &dhids) +{ + DHLOGI("DeleteDhid dhids size = %zu", dhids.size()); + for (auto &dhid : dhids) { + DHLOGD("delete dhid : %s", GetAnonyString(dhid).c_str()); + if (!IsExistDhid(dhid)) { + DHLOGE("dhid : %s not exist.", GetAnonyString(dhid).c_str()); + } else { + dhidStateMap_.erase(dhid); + } + } + return DH_SUCCESS; +} + +int32_t StateMachine::SwitchState(std::vector &dhids, DhidState state) +{ + for (auto &dhid : dhids) { + DHLOGD("SwitchState dhid : %s, state : %d.", GetAnonyString(dhid).c_str(), state); + if (!IsExistDhid(dhid)) { + DHLOGE("dhid : %s not exist.", GetAnonyString(dhid).c_str()); + } else { + dhidStateMap_[dhid] = state; + } + } + + if (state == DhidState::THROUGH_OUT) { + CreateKeyUpInjectThread(dhids); + } + + return DH_SUCCESS; +} + +DhidState StateMachine::GetStateByDhid(std::string &dhid) +{ + if (!IsExistDhid(dhid)) { + DHLOGE("dhid : %s not exist.", GetAnonyString(dhid).c_str()); + return DhidState::INIT; + } + return dhidStateMap_[dhid]; +} + +bool StateMachine::IsExistDhid(std::string &dhid) +{ + if (dhidStateMap_.find(dhid) == dhidStateMap_.end()) { + return false; + } + return true; +} + +void StateMachine::CreateKeyUpInjectThread(const std::vector &dhids) +{ + std::vector keyboardNodePaths; + std::vector shareDhIds; + DistributedInputCollector::GetInstance().GetKeyboardNodePath(dhids, keyboardNodePaths, shareDhIds); + + DistributedInputInject::GetInstance().GetVirtualKeyboardPathByDhId(dhids, keyboardNodePaths, shareDhIds); + + std::thread keyUpInjectThread = + std::thread(&StateMachine::KeyUpInject, this, std::ref(keyboardNodePaths), std::ref(shareDhIds)); + int32_t ret = pthread_setname_np(keyUpInjectThread.native_handle(), KEYBOARD_UP_INJECT_THREAD_NAME); + if (ret != 0) { + DHLOGE("CreateKeyUpInjectThread setname failed."); + } + keyUpInjectThread.detach(); +} + +void StateMachine::KeyUpInject(std::vector &shareDhidsPaths, std::vector &shareDhIds) +{ + ssize len = shareDhidsPaths.size(); + for (int32_t i = 0; i < len; ++i) { + CheckKeyState(shareDhIds[i], shareDhidsPaths[i]); + } +} + +int BitIsSet(const unsigned long *array, int bit) +{ + return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS))); +} + +void StateMachine::CheckKeyState(std::string &dhid, std::string &keyboardNodePath) +{ + char canonicalPath[PATH_MAX + 1] = {0x00}; + if (keyboardNodePath.length() == 0 || keyboardNodePath.length() > PATH_MAX || + realpath(keyboardNodePath.c_str(), canonicalPath) == nullptr) { + DHLOGE("keyboard Nodepath check fail, error path: %s", keyboardNodePath.c_str()); + return; + } + + int 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) { + 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; + } + for (int32_t yalv = 0; yalv < KEY_MAX; yalv++) { + if (BitIsSet(keystate, yalv)) { + keyboardPressedKeys.push_back(yalv); + } + } + break; + } + UpInject(fd, keyboardPressedKeys, dhid); + if (fd >= 0) { + close(fd); + fd = -1; + } +} + +void StateMachine::UpInject(int fd, std::vector &keyboardPressedKeys, std::string &dhid) +{ + 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); + + struct input_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); + } +} + +void StateMachine::RecordEventLog(const input_event& event) +{ + std::string eventType = ""; + switch (event.type) { + case EV_KEY: + eventType = "EV_KEY"; + break; + case EV_REL: + eventType = "EV_REL"; + break; + case EV_ABS: + eventType = "EV_ABS"; + break; + default: + eventType = "other type"; + break; + } + DHLOGD("4.E2E-Test Source write event into input driver, EventType: %s, Code: %d, Value: %d, Sec: %ld, Sec1: %ld", + eventType.c_str(), event.code, event.value, event.input_event_sec, event.input_event_usec); +} + +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOSs \ No newline at end of file diff --git a/sourcehandler/BUILD.gn b/sourcehandler/BUILD.gn index 2c927d1..2cf11d7 100644 --- a/sourcehandler/BUILD.gn +++ b/sourcehandler/BUILD.gn @@ -33,6 +33,7 @@ ohos_shared_library("libdinput_source_handler") { "${fwk_interfaces_path}/include/ipc", "${utils_path}/include", "${services_source_path}/inputinject/include", + "${distributedinput_path}/services/state/include", ] sources = [ -- Gitee From 3b68a0313ce2e5ba8d94bc14b64fec3bdb6171b2 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Sun, 23 Jul 2023 12:19:18 +0800 Subject: [PATCH 02/18] fix build failed Signed-off-by: liqiao49 --- services/sink/sinkmanager/BUILD.gn | 2 +- .../include/distributed_input_inject.h | 2 +- .../include/distributed_input_node_manager.h | 2 +- .../src/distributed_input_inject.cpp | 4 ++-- .../src/distributed_input_node_manager.cpp | 18 +++++++++--------- .../source/inputinject/src/virtual_device.cpp | 6 +++--- services/source/sourcemanager/BUILD.gn | 2 +- .../src/distributed_input_source_manager.cpp | 2 +- services/source/transport/BUILD.gn | 2 +- services/state/BUILD.gn | 8 ++++---- services/state/src/dinput_state.cpp | 13 +++++++------ 11 files changed, 31 insertions(+), 30 deletions(-) diff --git a/services/sink/sinkmanager/BUILD.gn b/services/sink/sinkmanager/BUILD.gn index 7b02237..7ba1f32 100644 --- a/services/sink/sinkmanager/BUILD.gn +++ b/services/sink/sinkmanager/BUILD.gn @@ -53,10 +53,10 @@ ohos_shared_library("libdinput_sink") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", + "${distributedinput_path}/services/state:libdinput_state", "${innerkits_path}:libdinput_sdk", "${services_sink_path}/inputcollector:libdinput_collector", "${services_sink_path}/transport:libdinput_sink_trans", - "${distributedinput_path}/services/state:libdinput_state", "${utils_path}:libdinput_utils", "//third_party/libevdev:libevdev", ] diff --git a/services/source/inputinject/include/distributed_input_inject.h b/services/source/inputinject/include/distributed_input_inject.h index fe351b8..3022118 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(std::vector &dhIds, std::vector &shareDhidsPaths, + void GetVirtualKeyboardPathByDhId(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 a217138..0d986e4 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -56,7 +56,7 @@ public: void GetDevicesInfoByDhId(std::vector dhidsVec, std::map &datas); void ProcessInjectEvent(const std::shared_ptr &rawEvent); - void GetVirtualKeyboardPathByDhId(std::vector &dhIds, std::vector &shareDhidsPaths, + void GetVirtualKeyboardPathByDhId(const std::vector &dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds); private: void AddDeviceLocked(const std::string& dhId, std::unique_ptr device); diff --git a/services/source/inputinject/src/distributed_input_inject.cpp b/services/source/inputinject/src/distributed_input_inject.cpp index 5a64585..dfee80d 100644 --- a/services/source/inputinject/src/distributed_input_inject.cpp +++ b/services/source/inputinject/src/distributed_input_inject.cpp @@ -256,8 +256,8 @@ int32_t DistributedInputInject::GetVirtualTouchScreenFd() return inputNodeManager_->GetVirtualTouchScreenFd(); } -void GetVirtualKeyboardPathByDhId(std::vector &dhIds, std::vector &shareDhidsPaths, - std::vector &shareDhIds) +void DistributedInputInject::GetVirtualKeyboardPathByDhId(const std::vector &dhIds, + std::vector &shareDhidsPaths, std::vector &shareDhIds) { if (inputNodeManager_ == nullptr) { DHLOGI("inputNodeManager_ is nullptr!"); diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index fa541b8..d825941 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -145,7 +145,7 @@ static std::string ConvertErrNo() return errNoMsg; } -void Closed(int fd) +void CloseFd(int fd) { if (fd < 0) { DHLOGE("No fd need to beclosed."); @@ -173,7 +173,7 @@ void DistributedInputNodeManager::ScanSinkInputDevices(const std::string& dirNam continue; } std::string tmpDevName = dirName + "/" + std::string(de->d_name); - OpenInputDeviceLocked(tmpDevName); + OpenInputDevice(tmpDevName); } closedir(dir); } @@ -190,7 +190,7 @@ bool DistributedInputNodeManager::IsVirtualDev(int fd) deviceName = buffer; DHLOGD("IsVirtualDev deviceName: %s", buffer); - if (identifier.name.find(VIRTUAL_DEVICE_NAME) == std::string::npos) { + if (deviceName.find(VIRTUAL_DEVICE_NAME) == std::string::npos) { DHLOGD("This is not a virtual device, fd %d, deviceName: %s", fd, deviceName.c_str()); return false; } @@ -224,7 +224,7 @@ void DistributedInputNodeManager::SetPathForDevMap(std::string& dhId, const std: DHLOGD("Virtual device map dhid %s.", iter->first.c_str()); if (dhId.compare(iter->first) == 0) { DHLOGI("Found the virtual device, set path :%s", devicePath.c_str()); - iter->second->SetPath(devicePath) + iter->second->SetPath(devicePath); break; } iter++; @@ -263,7 +263,7 @@ void DistributedInputNodeManager::OpenInputDevice(const std::string& devicePath) CloseFd(fd); return; } - if (fd =-1) { + if (fd == -1) { DHLOGE("The fd open failed, devicePath %s\n", devicePath.c_str()); return; } @@ -278,8 +278,8 @@ void DistributedInputNodeManager::OpenInputDevice(const std::string& devicePath) SetPathForDevMap(dhId, devicePath); } -void GetVirtualKeyboardPathByDhId(std::vector &dhIds, std::vector &shareDhidsPaths, - std::vector &shareDhIds); +void DistributedInputNodeManager::GetVirtualKeyboardPathByDhId(const std::vector &dhIds, + std::vector &shareDhidsPaths, std::vector &shareDhIds) { for (auto dhId_ : dhIds) { auto iter = virtualDeviceMap_.begin(); @@ -289,8 +289,8 @@ void GetVirtualKeyboardPathByDhId(std::vector &dhIds, std::vectorfirst.compare(dhId_) == 0) && ((iter->second->GetClasses() & INPUT_DEVICE_CLASS_KEYBOARD) != 0)) { - DHLOGI("Found vir keyboard path %s, dhid %s", itersecond->GetPath().c_str()); - shareDhidsPath.push_back(diter->second->GetPath()); + DHLOGI("Found vir keyboard path %s, dhid %s", iter->second->GetPath().c_str()); + shareDhidsPaths.push_back(iter->second->GetPath()); shareDhIds.push_back(dhId_); } iter++; diff --git a/services/source/inputinject/src/virtual_device.cpp b/services/source/inputinject/src/virtual_device.cpp index 7e7e1cc..bd114d4 100644 --- a/services/source/inputinject/src/virtual_device.cpp +++ b/services/source/inputinject/src/virtual_device.cpp @@ -185,7 +185,7 @@ void VirtualDevice::SetNetWorkId(const std::string netWorkId) netWorkId_ = netWorkId; } -void SetPath(const std::string path) +void VirtualDevice::SetPath(const std::string path) { path_ = path; } @@ -195,12 +195,12 @@ std::string VirtualDevice::GetNetWorkId() return netWorkId_; } -std::string GetPath() +std::string VirtualDevice::GetPath() { return path_; } -uint16_t GetClasses() +uint16_t VirtualDevice::GetClasses() { return classes_; } diff --git a/services/source/sourcemanager/BUILD.gn b/services/source/sourcemanager/BUILD.gn index 8a8b872..1ef4428 100644 --- a/services/source/sourcemanager/BUILD.gn +++ b/services/source/sourcemanager/BUILD.gn @@ -88,11 +88,11 @@ ohos_shared_library("libdinput_source") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", + "${distributedinput_path}/services/state:libdinput_state", "${innerkits_path}:libdinput_sdk", "${services_source_path}/inputinject:libdinput_inject", "${services_source_path}/transport:libdinput_source_trans", "${utils_path}:libdinput_utils", - "${distributedinput_path}/services/state:libdinput_state", "//third_party/libevdev:libevdev", ] diff --git a/services/source/sourcemanager/src/distributed_input_source_manager.cpp b/services/source/sourcemanager/src/distributed_input_source_manager.cpp index e99bf1f..43ca967 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -285,7 +285,7 @@ void DistributedInputSourceManager::DInputSourceListener::OnResponseStartRemoteI std::vector vecStr; sourceManagerObj_->StringSplitToVector(dhids, INPUT_STRING_SPLIT_POINT, vecStr); - StateMachine::GetInstance().AddDhid(vecStr); + StateMachine::GetInstance().AddDhids(vecStr); StateMachine::GetInstance().SwitchState(vecStr, DhidState::THROUGH_IN); std::shared_ptr jsonArrayMsg = std::make_shared(); diff --git a/services/source/transport/BUILD.gn b/services/source/transport/BUILD.gn index ad1bb96..2c752cc 100755 --- a/services/source/transport/BUILD.gn +++ b/services/source/transport/BUILD.gn @@ -44,9 +44,9 @@ ohos_shared_library("libdinput_source_trans") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", + "${distributedinput_path}/services/state:libdinput_state", "${distributedinput_path}/services/transportbase:libdinput_trans_base", "${services_source_path}/inputinject:libdinput_inject", - "${services_source_path}/services/state:libdinput_state", "${utils_path}:libdinput_utils", "//third_party/libevdev:libevdev", ] diff --git a/services/state/BUILD.gn b/services/state/BUILD.gn index 97ab868..19321a6 100644 --- a/services/state/BUILD.gn +++ b/services/state/BUILD.gn @@ -21,10 +21,10 @@ ohos_shared_library("libdinput_state") { "${common_path}/include", "${utils_path}/include", "${services_sink_path}/inputcollector/include", - "${services_source_path}/inputdevicehandler/include", - "${services_sink_path}/inputcollector/include", + "${distributedinput_path}/inputdevicehandler/include", + "${services_source_path}/inputinject/include", "${frameworks_path}/include", - "${fWK_common_path}/utils/include", + "${fwk_common_path}/utils/include", ] sources = [ "src/dinput_state.cpp" ] @@ -37,8 +37,8 @@ ohos_shared_library("libdinput_state") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", - "${services_source_path}/inputinject:libdinput_inject", "${services_sink_path}/inputcollector:libdinput_collector", + "${services_source_path}/inputinject:libdinput_inject", "${utils_path}:libdinput_utils", "//third_party/libevdev:libevdev", ] diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index 9b96352..d2c79e8 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -20,6 +20,7 @@ #include "dinput_utils_tool.h" #include "constants_dinput.h" #include "distributed_input_collector.h" +#include "distributed_input_inject.h" namespace OHOS { namespace DistributedHardware { @@ -43,7 +44,7 @@ int32_t StateMachine::Release() return DH_SUCCESS; } -int32_t StateMachine::AddDhids(std::vector &dhids) +int32_t StateMachine::AddDhids(const std::vector &dhids) { DHLOGI("AddDhid dhids size = %zu", dhids.size()); for (auto &dhid : dhids) { @@ -57,7 +58,7 @@ int32_t StateMachine::AddDhids(std::vector &dhids) return DH_SUCCESS; } -int32_t StateMachine::DeleteDhids(std::vector &dhids) +int32_t StateMachine::DeleteDhids(const std::vector &dhids) { DHLOGI("DeleteDhid dhids size = %zu", dhids.size()); for (auto &dhid : dhids) { @@ -71,7 +72,7 @@ int32_t StateMachine::DeleteDhids(std::vector &dhids) return DH_SUCCESS; } -int32_t StateMachine::SwitchState(std::vector &dhids, DhidState state) +int32_t StateMachine::SwitchState(const std::vector &dhids, DhidState state) { for (auto &dhid : dhids) { DHLOGD("SwitchState dhid : %s, state : %d.", GetAnonyString(dhid).c_str(), state); @@ -98,7 +99,7 @@ DhidState StateMachine::GetStateByDhid(std::string &dhid) return dhidStateMap_[dhid]; } -bool StateMachine::IsExistDhid(std::string &dhid) +bool StateMachine::IsExistDhid(const std::string &dhid) { if (dhidStateMap_.find(dhid) == dhidStateMap_.end()) { return false; @@ -125,7 +126,7 @@ void StateMachine::CreateKeyUpInjectThread(const std::vector &dhids void StateMachine::KeyUpInject(std::vector &shareDhidsPaths, std::vector &shareDhIds) { - ssize len = shareDhidsPaths.size(); + ssize_t len = shareDhidsPaths.size(); for (int32_t i = 0; i < len; ++i) { CheckKeyState(shareDhIds[i], shareDhidsPaths[i]); } @@ -194,7 +195,7 @@ void StateMachine::UpInject(int fd, std::vector &keyboardPressedKeys, } RecordEventLog(event); - struct input_event event = { + event = { .type = EV_SYN, .code = 0, .value = KEY_UP_STATE -- Gitee From 644b6ca8378637ddebb3ac55dd1e443b15ac0e45 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Mon, 24 Jul 2023 08:46:58 +0800 Subject: [PATCH 03/18] fix fuzz ut build failed Signed-off-by: liqiao49 --- interfaces/ipc/test/clientunittest/BUILD.gn | 1 + services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn | 2 ++ services/sink/transport/BUILD.gn | 2 ++ services/sink/transport/test/sinktransunittest/BUILD.gn | 2 ++ .../source/sourcemanager/test/sourcemanagerunittest/BUILD.gn | 2 ++ services/source/transport/test/sourcetransunittest/BUILD.gn | 2 ++ services/transportbase/test/transbaseunittest/BUILD.gn | 1 + sourcehandler/test/unittest/BUILD.gn | 1 + test/fuzztest/dinputconfigdh_fuzzer/BUILD.gn | 1 + test/fuzztest/dinputinitsource_fuzzer/BUILD.gn | 1 + test/fuzztest/dinputreleasesource_fuzzer/BUILD.gn | 1 + test/fuzztest/distributedinputclient_fuzzer/BUILD.gn | 1 + test/fuzztest/distributedinputkit_fuzzer/BUILD.gn | 1 + test/fuzztest/distributedinputsourcetransport_fuzzer/BUILD.gn | 1 + 14 files changed, 19 insertions(+) diff --git a/interfaces/ipc/test/clientunittest/BUILD.gn b/interfaces/ipc/test/clientunittest/BUILD.gn index a9254db..a1a38e9 100644 --- a/interfaces/ipc/test/clientunittest/BUILD.gn +++ b/interfaces/ipc/test/clientunittest/BUILD.gn @@ -49,6 +49,7 @@ ohos_unittest("distributed_input_client_test") { "${services_sink_path}/inputcollector/include", "${services_sink_path}/sinkmanager/include", "${distributedinput_path}/inputdevicehandler/include", + "${distributedinput_path}/services/state/include", ] sources = [ diff --git a/services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn b/services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn index a5bc23d..2ab18a6 100755 --- a/services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn +++ b/services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn @@ -49,6 +49,7 @@ ohos_unittest("distributed_input_sinkmanager_test") { "${fwk_interfaces_path}/include/ipc", "${common_path}/test/mock", "${services_sink_path}/sinkmanager/test/sinkmanagerunittest/mock/", + "${distributedinput_path}/services/state/include", ] sources = [ @@ -84,6 +85,7 @@ ohos_unittest("distributed_input_sinkmanager_test") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", + "${distributedinput_path}/services/state:libdinput_state", "${distributedinput_path}/services/transportbase:libdinput_trans_base", "${services_sink_path}/transport:libdinput_sink_trans", "${utils_path}:libdinput_utils", diff --git a/services/sink/transport/BUILD.gn b/services/sink/transport/BUILD.gn index 240becf..8341c43 100644 --- a/services/sink/transport/BUILD.gn +++ b/services/sink/transport/BUILD.gn @@ -30,6 +30,7 @@ ohos_shared_library("libdinput_sink_trans") { "${fwk_interfaces_path}/include", "${fwk_interfaces_path}/include/ipc", "${distributedinput_path}/services/transportbase/include", + "${distributedinput_path}/services/state/include", ] sources = [ @@ -45,6 +46,7 @@ ohos_shared_library("libdinput_sink_trans") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", + "${distributedinput_path}/services/state:libdinput_state", "${distributedinput_path}/services/transportbase:libdinput_trans_base", "${utils_path}:libdinput_utils", "//third_party/libevdev:libevdev", diff --git a/services/sink/transport/test/sinktransunittest/BUILD.gn b/services/sink/transport/test/sinktransunittest/BUILD.gn index 48415d9..e817dc5 100755 --- a/services/sink/transport/test/sinktransunittest/BUILD.gn +++ b/services/sink/transport/test/sinktransunittest/BUILD.gn @@ -50,6 +50,7 @@ ohos_unittest("distributed_input_sinktrans_test") { "${services_sink_path}/inputcollector/include", "${services_source_path}/inputinject/include", "${distributedinput_path}/inputdevicehandler/include", + "${distributedinput_path}/services/state/include", ] sources = [ @@ -79,6 +80,7 @@ ohos_unittest("distributed_input_sinktrans_test") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", + "${distributedinput_path}/services/state:libdinput_state", "${services_sink_path}/inputcollector:libdinput_collector", "${services_sink_path}/sinkmanager:libdinput_sink", "${utils_path}:libdinput_utils", diff --git a/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn b/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn index 9b3bc45..9ec9226 100755 --- a/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn +++ b/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn @@ -51,6 +51,7 @@ ohos_unittest("distributed_input_sourcemanager_test") { "${common_path}/test/mock", "${distributedinput_path}/services/transportbase/include", "${distributedinput_path}/utils/include", + "${distributedinput_path}/services/state/include", ] sources = [ @@ -109,6 +110,7 @@ ohos_unittest("distributed_input_sourcemanager_test") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", + "${distributedinput_path}/services/state:libdinput_state", "${innerkits_path}:libdinput_sdk", "${services_source_path}/transport:libdinput_source_trans", "//third_party/libevdev:libevdev", diff --git a/services/source/transport/test/sourcetransunittest/BUILD.gn b/services/source/transport/test/sourcetransunittest/BUILD.gn index b6b84bc..6242cfe 100755 --- a/services/source/transport/test/sourcetransunittest/BUILD.gn +++ b/services/source/transport/test/sourcetransunittest/BUILD.gn @@ -46,6 +46,7 @@ ohos_unittest("distributed_input_sourcetrans_test") { "${utils_path}/include", "${ipc_path}/include", "${frameworks_path}/include", + "${distributedinput_path}/services/state/include", "${distributedinput_path}/inputdevicehandler/include", "${common_path}/test/mock", ] @@ -75,6 +76,7 @@ ohos_unittest("distributed_input_sourcetrans_test") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", + "${distributedinput_path}/services/state:libdinput_state", "${services_source_path}/inputinject:libdinput_inject", "${services_source_path}/sourcemanager:libdinput_source", "${utils_path}:libdinput_utils", diff --git a/services/transportbase/test/transbaseunittest/BUILD.gn b/services/transportbase/test/transbaseunittest/BUILD.gn index 7b09022..60d4bf9 100644 --- a/services/transportbase/test/transbaseunittest/BUILD.gn +++ b/services/transportbase/test/transbaseunittest/BUILD.gn @@ -43,6 +43,7 @@ ohos_unittest("distributed_input_transbase_test") { "${frameworks_path}/include", "//third_party/json/include", "${distributedinput_path}/inputdevicehandler/include", + "${distributedinput_path}/services/state/include", "${common_path}/test/mock", ] diff --git a/sourcehandler/test/unittest/BUILD.gn b/sourcehandler/test/unittest/BUILD.gn index 7c43860..a1fee41 100755 --- a/sourcehandler/test/unittest/BUILD.gn +++ b/sourcehandler/test/unittest/BUILD.gn @@ -44,6 +44,7 @@ ohos_unittest("distributed_input_source_handler_test") { "${fwk_interfaces_path}/include", "${fwk_interfaces_path}/include/ipc", "${services_source_path}/inputinject/include", + "${distributedinput_path}/services/state/include", "${utils_path}/include", ] diff --git a/test/fuzztest/dinputconfigdh_fuzzer/BUILD.gn b/test/fuzztest/dinputconfigdh_fuzzer/BUILD.gn index c6d933a..58863e4 100644 --- a/test/fuzztest/dinputconfigdh_fuzzer/BUILD.gn +++ b/test/fuzztest/dinputconfigdh_fuzzer/BUILD.gn @@ -33,6 +33,7 @@ ohos_fuzztest("DinputConfigDhFuzzTest") { "${distributedinput_path}/frameworks/include", "${distributedinput_path}/sourcehandler/include", "${distributedinput_path}/inputdevicehandler/include", + "${distributedinput_path}/services/state/include", "${service_common}/include", "${common_path}/include", "${fwk_common_path}/log/include", diff --git a/test/fuzztest/dinputinitsource_fuzzer/BUILD.gn b/test/fuzztest/dinputinitsource_fuzzer/BUILD.gn index e0218b8..b5b2bd5 100644 --- a/test/fuzztest/dinputinitsource_fuzzer/BUILD.gn +++ b/test/fuzztest/dinputinitsource_fuzzer/BUILD.gn @@ -30,6 +30,7 @@ ohos_fuzztest("DinputInitSourceFuzzTest") { "${distributedinput_path}/interfaces/ipc/include", "${distributedinput_path}/services/source/sourcemanager/include", "${distributedinput_path}/services/sink/sinkmanager/include", + "${distributedinput_path}/services/state/include", "${distributedinput_path}/frameworks/include", "${distributedinput_path}/sourcehandler/include", "${distributedinput_path}/inputdevicehandler/include", diff --git a/test/fuzztest/dinputreleasesource_fuzzer/BUILD.gn b/test/fuzztest/dinputreleasesource_fuzzer/BUILD.gn index 4885252..af05d70 100644 --- a/test/fuzztest/dinputreleasesource_fuzzer/BUILD.gn +++ b/test/fuzztest/dinputreleasesource_fuzzer/BUILD.gn @@ -33,6 +33,7 @@ ohos_fuzztest("DinputReleaseSourceFuzzTest") { "${distributedinput_path}/frameworks/include", "${distributedinput_path}/sourcehandler/include", "${distributedinput_path}/inputdevicehandler/include", + "${distributedinput_path}/services/state/include", "${service_common}/include", "${common_path}/include", "${fwk_common_path}/log/include", diff --git a/test/fuzztest/distributedinputclient_fuzzer/BUILD.gn b/test/fuzztest/distributedinputclient_fuzzer/BUILD.gn index a902db6..4c6ecaa 100755 --- a/test/fuzztest/distributedinputclient_fuzzer/BUILD.gn +++ b/test/fuzztest/distributedinputclient_fuzzer/BUILD.gn @@ -35,6 +35,7 @@ ohos_fuzztest("DistributedInputClientFuzzTest") { "${distributedinput_path}/sourcehandler/include", "${distributedinput_path}/sinkhandler/include", "${distributedinput_path}/inputdevicehandler/include", + "${distributedinput_path}/services/state/include", "${service_common}/include", "${common_path}/include", "${fwk_common_path}/log/include", diff --git a/test/fuzztest/distributedinputkit_fuzzer/BUILD.gn b/test/fuzztest/distributedinputkit_fuzzer/BUILD.gn index 9e1a052..280c9a9 100755 --- a/test/fuzztest/distributedinputkit_fuzzer/BUILD.gn +++ b/test/fuzztest/distributedinputkit_fuzzer/BUILD.gn @@ -43,6 +43,7 @@ ohos_fuzztest("DistributedInputKitFuzzTest") { "${fwk_interfaces_path}/include", "${fwk_interfaces_path}/include/ipc", "${services_source_path}/inputinject/include", + "${distributedinput_path}/services/state/include", ] cflags = [ diff --git a/test/fuzztest/distributedinputsourcetransport_fuzzer/BUILD.gn b/test/fuzztest/distributedinputsourcetransport_fuzzer/BUILD.gn index dd1c5ba..976f065 100755 --- a/test/fuzztest/distributedinputsourcetransport_fuzzer/BUILD.gn +++ b/test/fuzztest/distributedinputsourcetransport_fuzzer/BUILD.gn @@ -39,6 +39,7 @@ ohos_fuzztest("DistributedInputSourceTransportFuzzTest") { "${utils_path}/include", "${frameworks_path}/include", "${distributedinput_path}/inputdevicehandler/include", + "${distributedinput_path}/services/state/include", ] cflags = [ -- Gitee From 2a7d5c8b039bf4d6b84976e2151cab7444b6e375 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Mon, 24 Jul 2023 16:34:30 +0800 Subject: [PATCH 04/18] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B3=A8=E5=85=A5?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E6=8B=89=E8=B5=B7=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liqiao49 --- .../include/distributed_input_collector.h | 4 +- .../src/distributed_input_sink_manager.cpp | 20 +++---- .../include/distributed_input_node_manager.h | 1 - .../src/distributed_input_inject.cpp | 4 +- .../src/distributed_input_node_manager.cpp | 10 ++-- .../src/distributed_input_source_manager.cpp | 8 +-- .../distributed_input_source_transport.cpp | 8 +-- services/state/include/dinput_state.h | 28 +++++----- services/state/src/dinput_state.cpp | 55 ++++++++++--------- 9 files changed, 69 insertions(+), 69 deletions(-) diff --git a/services/sink/inputcollector/include/distributed_input_collector.h b/services/sink/inputcollector/include/distributed_input_collector.h index 1f95ea7..c4c9ef1 100644 --- a/services/sink/inputcollector/include/distributed_input_collector.h +++ b/services/sink/inputcollector/include/distributed_input_collector.h @@ -43,8 +43,8 @@ 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, std::vector &shareDhIds); + void GetKeyboardNodePath(std::vector dhIds, std::vector &shareDhidsPaths, + std::vector &shareDhIds); // false for sharing device exist, true for all devices stop sharing bool IsAllDevicesStoped(); int32_t RegisterSharingDhIdListener(sptr sharingDhIdListener); diff --git a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp index 099f733..dbafba9 100644 --- a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp +++ b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp @@ -276,8 +276,8 @@ void DistributedInputSinkManager::DInputSinkListener::OnStartRemoteInputDhid(con AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, vecStr); sinkManagerObj_->StoreStartDhids(sessionId, affDhIds.sharingDhIds); DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); - StateMachine::GetInstance().AddDhids(vecStr); - StateMachine::GetInstance().SwitchState(vecStr, DhidState::THROUGH_OUT); + DInputState::GetInstance().AddDhids(vecStr); + DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_OUT); } } @@ -294,8 +294,8 @@ void DistributedInputSinkManager::DInputSinkListener::OnStopRemoteInputDhid(cons stopIndeedOnes.noSharingDhIds = stopIndeedDhIds; DistributedInputCollector::GetInstance().ReportDhIdSharingState(stopIndeedOnes); - StateMachine::GetInstance().AddDhids(stopOnCmdDhIds); - StateMachine::GetInstance().SwitchState(stopOnCmdDhIds, DhidState::THROUGH_IN); + DInputState::GetInstance().AddDhids(stopOnCmdDhIds); + DInputState::GetInstance().SwitchState(stopOnCmdDhIds, DhidState::THROUGH_IN); if (DistributedInputCollector::GetInstance().IsAllDevicesStoped()) { DHLOGE("All dhid stop sharing, sessionId: %d is closed.", sessionId); @@ -350,8 +350,8 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStartDhidRemoteInpu AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, vecStr); sinkManagerObj_->StoreStartDhids(toSinkSessionId, affDhIds.sharingDhIds); DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); - StateMachine::GetInstance().AddDhids(vecStr); - StateMachine::GetInstance().SwitchState(vecStr, DhidState::THROUGH_OUT); + DInputState::GetInstance().AddDhids(vecStr); + DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_OUT); } } @@ -368,8 +368,8 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStopDhidRemoteInput stopIndeedOnes.noSharingDhIds = stopIndeedDhIds; DistributedInputCollector::GetInstance().ReportDhIdSharingState(stopIndeedOnes); - StateMachine::GetInstance().AddDhids(stopOnCmdDhIds); - StateMachine::GetInstance().SwitchState(stopOnCmdDhIds, DhidState::THROUGH_IN); + DInputState::GetInstance().AddDhids(stopOnCmdDhIds); + DInputState::GetInstance().SwitchState(stopOnCmdDhIds, DhidState::THROUGH_IN); if (DistributedInputCollector::GetInstance().IsAllDevicesStoped()) { DHLOGE("All dhid stop sharing, sessionId: %d is closed.", toSinkSessionId); @@ -705,9 +705,9 @@ int32_t DistributedInputSinkManager::Init() return ERR_DH_INPUT_SERVER_SINK_MANAGER_INIT_FAIL; } - ret = StateMachine::GetInstance().Init(); + ret = DInputState::GetInstance().Init(); if (ret != DH_SUCCESS) { - DHLOGE("StateMachine init fail!"); + DHLOGE("DInputState init fail!"); return ERR_DH_INPUT_SERVER_SINK_MANAGER_INIT_FAIL; } diff --git a/services/source/inputinject/include/distributed_input_node_manager.h b/services/source/inputinject/include/distributed_input_node_manager.h index 0d986e4..9608143 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -72,7 +72,6 @@ private: bool GetDevDhIdFd(int fd, std::string& dhId, std::string& physicalPath); void SetPathForDevMap(std::string& dhId, const std::string& devicePath); - /* the key is dhId, and the value is virtualDevice */ std::map> virtualDeviceMap_; std::mutex virtualDeviceMapMutex_; diff --git a/services/source/inputinject/src/distributed_input_inject.cpp b/services/source/inputinject/src/distributed_input_inject.cpp index dfee80d..1cd5e55 100644 --- a/services/source/inputinject/src/distributed_input_inject.cpp +++ b/services/source/inputinject/src/distributed_input_inject.cpp @@ -257,10 +257,10 @@ int32_t DistributedInputInject::GetVirtualTouchScreenFd() } void DistributedInputInject::GetVirtualKeyboardPathByDhId(const std::vector &dhIds, - std::vector &shareDhidsPaths, std::vector &shareDhIds) + std::vector &shareDhidsPaths, std::vector &shareDhIds) { if (inputNodeManager_ == nullptr) { - DHLOGI("inputNodeManager_ is nullptr!"); + DHLOGE("inputNodeManager is nullptr"); return; } inputNodeManager_->GetVirtualKeyboardPathByDhId(dhIds, shareDhidsPaths, shareDhIds); diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index d825941..6a3d53d 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -35,11 +35,9 @@ 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) { @@ -200,7 +198,7 @@ bool DistributedInputNodeManager::IsVirtualDev(int fd) bool DistributedInputNodeManager::GetDevDhIdFd(int fd, std::string& dhId, std::string& physicalPath) { char buffer[256] = {0}; - if (ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) { + if (ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) { DHLOGE("Could not get device name for %s", ConvertErrNo().c_str()); return false; } @@ -288,8 +286,10 @@ void DistributedInputNodeManager::GetVirtualKeyboardPathByDhId(const std::vector DHLOGE("device is nullptr"); continue; } - if ((iter->first.compare(dhId_) == 0) && ((iter->second->GetClasses() & INPUT_DEVICE_CLASS_KEYBOARD) != 0)) { - DHLOGI("Found vir keyboard path %s, dhid %s", iter->second->GetPath().c_str()); + if ((iter->first.compare(dhId_) == 0) && + ((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()); shareDhidsPaths.push_back(iter->second->GetPath()); shareDhIds.push_back(dhId_); } diff --git a/services/source/sourcemanager/src/distributed_input_source_manager.cpp b/services/source/sourcemanager/src/distributed_input_source_manager.cpp index 43ca967..34eddf8 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -285,8 +285,8 @@ void DistributedInputSourceManager::DInputSourceListener::OnResponseStartRemoteI std::vector vecStr; sourceManagerObj_->StringSplitToVector(dhids, INPUT_STRING_SPLIT_POINT, vecStr); - StateMachine::GetInstance().AddDhids(vecStr); - StateMachine::GetInstance().SwitchState(vecStr, DhidState::THROUGH_IN); + DInputState::GetInstance().AddDhids(vecStr); + DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_IN); std::shared_ptr jsonArrayMsg = std::make_shared(); nlohmann::json tmpJson; @@ -977,9 +977,9 @@ int32_t DistributedInputSourceManager::Init() dhFwkKit->RegisterPublisherListener(DHTopic::TOPIC_STOP_DSCREEN, stopDScreenListener_); dhFwkKit->RegisterPublisherListener(DHTopic::TOPIC_DEV_OFFLINE, deviceOfflineListener_); - ret = StateMachine::GetInstance().Init(); + ret = DInputState::GetInstance().Init(); if (ret != DH_SUCCESS) { - DHLOGE("StateMachine init fail!"); + DHLOGE("DInputState init fail!"); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_INIT_FAIL; } diff --git a/services/source/transport/src/distributed_input_source_transport.cpp b/services/source/transport/src/distributed_input_source_transport.cpp index a49e6e0..d6333c5 100644 --- a/services/source/transport/src/distributed_input_source_transport.cpp +++ b/services/source/transport/src/distributed_input_source_transport.cpp @@ -316,8 +316,8 @@ int32_t DistributedInputSourceTransport::StartRemoteInputDhids(int32_t srcTsrcSe std::vector vecStr; StringSplitToVector(dhids, INPUT_STRING_SPLIT_POINT, vecStr); - StateMachine::GetInstance().AddDhids(vecStr); - StateMachine::GetInstance().SwitchState(vecStr,DhidState::THROUGH_IN); + DInputState::GetInstance().AddDhids(vecStr); + DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_IN); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_START_DHID_FOR_REL; @@ -782,8 +782,8 @@ int32_t DistributedInputSourceTransport::StopRemoteInput(const std::string &devi } DHLOGI("StopRemoteInput sessionId:%d.", sessionId); - StateMachine::GetInstance().AddDhids(dhids); - StateMachine::GetInstance().SwitchState(dhids,DhidState::THROUGH_OUT); + DInputState::GetInstance().AddDhids(dhids); + DInputState::GetInstance().SwitchState(dhids, DhidState::THROUGH_OUT); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_STOP_DHID; diff --git a/services/state/include/dinput_state.h b/services/state/include/dinput_state.h index cbfb450..6dc50a5 100644 --- a/services/state/include/dinput_state.h +++ b/services/state/include/dinput_state.h @@ -16,32 +16,30 @@ #ifndef DISTRIBUTED_INPUT_STATE_BASE_H #define DISTRIBUTED_INPUT_STATE_BASE_H +#include +#include +#include #include -#include -#include +#include #include +#include #include -#include -#include -#include #include - +#include namespace OHOS { namespace DistributedHardware { namespace DistributedInput { - -enum class DhidState -{ +enum class DhidState { INIT = 0, THROUGH_IN, THROUGH_OUT, }; - -class StateMachine{ +class DInputState { public: - static StateMachine &GetInstance() { - static StateMachine instance; + static DInputState &GetInstance() + { + static DInputState instance; return instance; }; @@ -53,7 +51,7 @@ public: DhidState GetStateByDhid(std::string &dhid); private: - ~StateMachine(); + ~DInputState(); void CreateKeyUpInjectThread(const std::vector &dhids); void CheckKeyState(std::string &dhid, std::string &keyboardNodePath); @@ -63,9 +61,9 @@ private: void RecordEventLog(const input_event& event); private: + std::mutex operationMutex_; std::map dhidStateMap_; }; - } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index d2c79e8..7f989c0 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -26,27 +26,29 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { -StateMachine::~StateMachine() +DInputState::~DInputState() { Release(); } -int32_t StateMachine::Init() +int32_t DInputState::Init() { - DHLOGI("StateMachine Init."); + DHLOGI("DInputState Init."); return DH_SUCCESS; } -int32_t StateMachine::Release() +int32_t DInputState::Release() { - DHLOGI("StateMachine Release."); + DHLOGI("DInputState Release."); + std::unique_lock mapLock(operationMutex_); dhidStateMap_.clear(); return DH_SUCCESS; } -int32_t StateMachine::AddDhids(const std::vector &dhids) +int32_t DInputState::AddDhids(const std::vector &dhids) { DHLOGI("AddDhid dhids size = %zu", dhids.size()); + std::unique_lock mapLock(operationMutex_); for (auto &dhid : dhids) { DHLOGD("add dhid : %s", GetAnonyString(dhid).c_str()); if (IsExistDhid(dhid)) { @@ -58,9 +60,10 @@ int32_t StateMachine::AddDhids(const std::vector &dhids) return DH_SUCCESS; } -int32_t StateMachine::DeleteDhids(const std::vector &dhids) +int32_t DInputState::DeleteDhids(const std::vector &dhids) { DHLOGI("DeleteDhid dhids size = %zu", dhids.size()); + std::unique_lock mapLock(operationMutex_); for (auto &dhid : dhids) { DHLOGD("delete dhid : %s", GetAnonyString(dhid).c_str()); if (!IsExistDhid(dhid)) { @@ -72,8 +75,9 @@ int32_t StateMachine::DeleteDhids(const std::vector &dhids) return DH_SUCCESS; } -int32_t StateMachine::SwitchState(const std::vector &dhids, DhidState state) +int32_t DInputState::SwitchState(const std::vector &dhids, DhidState state) { + std::unique_lock mapLock(operationMutex_); for (auto &dhid : dhids) { DHLOGD("SwitchState dhid : %s, state : %d.", GetAnonyString(dhid).c_str(), state); if (!IsExistDhid(dhid)) { @@ -90,7 +94,7 @@ int32_t StateMachine::SwitchState(const std::vector &dhids, DhidSta return DH_SUCCESS; } -DhidState StateMachine::GetStateByDhid(std::string &dhid) +DhidState DInputState::GetStateByDhid(std::string &dhid) { if (!IsExistDhid(dhid)) { DHLOGE("dhid : %s not exist.", GetAnonyString(dhid).c_str()); @@ -99,7 +103,7 @@ DhidState StateMachine::GetStateByDhid(std::string &dhid) return dhidStateMap_[dhid]; } -bool StateMachine::IsExistDhid(const std::string &dhid) +bool DInputState::IsExistDhid(const std::string &dhid) { if (dhidStateMap_.find(dhid) == dhidStateMap_.end()) { return false; @@ -107,16 +111,16 @@ bool StateMachine::IsExistDhid(const std::string &dhid) return true; } -void StateMachine::CreateKeyUpInjectThread(const std::vector &dhids) +void DInputState::CreateKeyUpInjectThread(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(&StateMachine::KeyUpInject, this, std::ref(keyboardNodePaths), std::ref(shareDhIds)); + std::thread(&DInputState::KeyUpInject, this, keyboardNodePaths, shareDhIds); int32_t ret = pthread_setname_np(keyUpInjectThread.native_handle(), KEYBOARD_UP_INJECT_THREAD_NAME); if (ret != 0) { DHLOGE("CreateKeyUpInjectThread setname failed."); @@ -124,8 +128,10 @@ void StateMachine::CreateKeyUpInjectThread(const std::vector &dhids keyUpInjectThread.detach(); } -void StateMachine::KeyUpInject(std::vector &shareDhidsPaths, std::vector &shareDhIds) +void DInputState::KeyUpInject(std::vector shareDhidsPaths, std::vector shareDhIds) { + DHLOGI("KeyUpInject enter, shareDhidsPaths.size = %d, shareDhIds.size =%d.", + shareDhidsPaths.size(), shareDhIds.size()); ssize_t len = shareDhidsPaths.size(); for (int32_t i = 0; i < len; ++i) { CheckKeyState(shareDhIds[i], shareDhidsPaths[i]); @@ -137,8 +143,9 @@ int BitIsSet(const unsigned long *array, int bit) return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS))); } -void StateMachine::CheckKeyState(std::string &dhid, std::string &keyboardNodePath) +void DInputState::CheckKeyState(std::string &dhid, std::string &keyboardNodePath) { + DHLOGI("CheckKeyState 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 || realpath(keyboardNodePath.c_str(), canonicalPath) == nullptr) { @@ -159,7 +166,6 @@ void StateMachine::CheckKeyState(std::string &dhid, std::string &keyboardNodePat 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); @@ -169,6 +175,7 @@ void StateMachine::CheckKeyState(std::string &dhid, std::string &keyboardNodePat } for (int32_t yalv = 0; yalv < KEY_MAX; yalv++) { if (BitIsSet(keystate, yalv)) { + DHLOGD("yalv = %d, not up.", yalv); keyboardPressedKeys.push_back(yalv); } } @@ -181,7 +188,7 @@ void StateMachine::CheckKeyState(std::string &dhid, std::string &keyboardNodePat } } -void StateMachine::UpInject(int fd, std::vector &keyboardPressedKeys, std::string &dhid) +void DInputState::UpInject(int fd, std::vector &keyboardPressedKeys, std::string &dhid) { for (auto &code: keyboardPressedKeys) { struct input_event event = { @@ -208,27 +215,23 @@ void StateMachine::UpInject(int fd, std::vector &keyboardPressedKeys, } } -void StateMachine::RecordEventLog(const input_event& event) +void DInputState::RecordEventLog(const input_event& event) { std::string eventType = ""; switch (event.type) { case EV_KEY: eventType = "EV_KEY"; break; - case EV_REL: - eventType = "EV_REL"; - break; - case EV_ABS: - eventType = "EV_ABS"; + case EV_SYN: + eventType = "EV_SYN"; break; default: eventType = "other type"; break; } - DHLOGD("4.E2E-Test Source write event into input driver, EventType: %s, Code: %d, Value: %d, Sec: %ld, Sec1: %ld", - eventType.c_str(), event.code, event.value, event.input_event_sec, event.input_event_usec); + DHLOGD("5.E2E-Test Source write event into input driver, EventType: %s, Code: %d, Value: %d", + eventType.c_str(), event.code, event.value); } - } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOSs \ No newline at end of file -- Gitee From efcffdf4cce26f0df1ca0eab37638b80122b8259 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Mon, 24 Jul 2023 17:01:08 +0800 Subject: [PATCH 05/18] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liqiao49 --- common/include/input_hub.cpp | 4 ++-- common/include/input_hub.h | 4 ++-- .../include/distributed_input_node_manager.h | 4 ++-- .../src/distributed_input_node_manager.cpp | 12 ++++++------ services/state/include/dinput_state.h | 7 ------- services/state/src/dinput_state.cpp | 8 ++++++++ 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index 0d3387b..c70c431 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -1140,8 +1140,8 @@ void InputHub::GetShareMousePathByDhId(std::vector dhIds, std::stri } } -void InputHub::GetShareKeyboardPathByDhId(std::vector dhIds, - std::vector &shareDhidsPath, std::vector &shareDhIds) +void InputHub::GetShareKeyboardPathByDhId(std::vector dhIds, std::vector &shareDhidsPath, + std::vector &shareDhIds) { DHLOGI("GetShareKeyboardPathByDhId: devices_.size:%d,", devices_.size()); std::unique_lock deviceLock(devicesMutex_); diff --git a/common/include/input_hub.h b/common/include/input_hub.h index 82d951f..408327a 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -53,8 +53,8 @@ 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, std::vector &shareDhIds); + void GetShareKeyboardPathByDhId(std::vector dhIds, std::vector &shareDhidsPath, + std::vector &shareDhIds); bool IsAllDevicesStoped(); void ScanInputDevices(const std::string& dirname); diff --git a/services/source/inputinject/include/distributed_input_node_manager.h b/services/source/inputinject/include/distributed_input_node_manager.h index 9608143..63e6dd7 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -38,7 +38,7 @@ public: DistributedInputNodeManager(); ~DistributedInputNodeManager(); - int32_t openDevicesNode(const std::string& devId, const std::string& dhId, + int32_t OpenDevicesNode(const std::string& devId, const std::string& dhId, const std::string& parameters); int32_t getDevice(const std::string& dhId, VirtualDevice*& device); @@ -69,7 +69,7 @@ private: void OpenInputDevice(const std::string& devicePath); int OpenInputDeviceFdByPath(std::string& canonicalDevicePath); bool IsVirtualDev(int fd); - bool GetDevDhIdFd(int fd, std::string& dhId, std::string& physicalPath); + bool GetDevDhIdByFd(int fd, std::string& dhId, std::string& physicalPath); void SetPathForDevMap(std::string& dhId, const std::string& devicePath); /* the key is dhId, and the value is virtualDevice */ diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index 6a3d53d..145f460 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -58,7 +58,7 @@ DistributedInputNodeManager::~DistributedInputNodeManager() DHLOGI("destructor end"); } -int32_t DistributedInputNodeManager::openDevicesNode(const std::string& devId, const std::string& dhId, +int32_t DistributedInputNodeManager::OpenDevicesNode(const std::string& devId, const std::string& dhId, const std::string& parameters) { if (devId.size() > DEV_ID_LENGTH_MAX || devId.empty() || dhId.size() > DH_ID_LENGTH_MAX || dhId.empty() || @@ -181,7 +181,7 @@ bool DistributedInputNodeManager::IsVirtualDev(int fd) char buffer[256] = {0}; std::string deviceName; if (ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) { - DHLOGE("Could not get device name for %s", ConvertErrNo().c_str()); + DHLOGE("Could not get device name for %s.", ConvertErrNo().c_str()); return false; } buffer[sizeof(buffer) - 1] = '\0'; @@ -189,13 +189,13 @@ bool DistributedInputNodeManager::IsVirtualDev(int fd) DHLOGD("IsVirtualDev deviceName: %s", buffer); if (deviceName.find(VIRTUAL_DEVICE_NAME) == std::string::npos) { - DHLOGD("This is not a virtual device, fd %d, deviceName: %s", fd, deviceName.c_str()); + DHLOGD("This is not a virtual device, fd %d, deviceName: %s.", fd, deviceName.c_str()); return false; } return true; } -bool DistributedInputNodeManager::GetDevDhIdFd(int fd, std::string& dhId, std::string& physicalPath) +bool DistributedInputNodeManager::GetDevDhIdByFd(int fd, std::string& dhId, std::string& physicalPath) { char buffer[256] = {0}; if (ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) { @@ -269,7 +269,7 @@ void DistributedInputNodeManager::OpenInputDevice(const std::string& devicePath) DHLOGE("The dev not virtual, devicePath %s\n", devicePath.c_str()); return; } - if (GetDevDhIdFd(fd, dhId, physicalPath) == false) { + if (GetDevDhIdByFd(fd, dhId, physicalPath) == false) { DHLOGE("Get dev dhid failed, devicePath %s\n", devicePath.c_str()); return; } @@ -286,7 +286,7 @@ void DistributedInputNodeManager::GetVirtualKeyboardPathByDhId(const std::vector DHLOGE("device is nullptr"); continue; } - if ((iter->first.compare(dhId_) == 0) && + if ((iter->first.compare(dhId_) == 0) && ((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()); diff --git a/services/state/include/dinput_state.h b/services/state/include/dinput_state.h index 6dc50a5..60a46b7 100644 --- a/services/state/include/dinput_state.h +++ b/services/state/include/dinput_state.h @@ -16,16 +16,9 @@ #ifndef DISTRIBUTED_INPUT_STATE_BASE_H #define DISTRIBUTED_INPUT_STATE_BASE_H -#include -#include -#include #include #include -#include #include -#include -#include -#include namespace OHOS { namespace DistributedHardware { diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index 7f989c0..f1e7c01 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -15,6 +15,14 @@ #include "dinput_state.h" +#include +#include +#include +#include +#include +#include +#include + #include "dinput_errcode.h" #include "dinput_log.h" #include "dinput_utils_tool.h" -- Gitee From 492ca3938dd103b475a51a6942ca87b78fc9cc6a Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Mon, 24 Jul 2023 19:15:00 +0800 Subject: [PATCH 06/18] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liqiao49 --- .../inputinject/src/distributed_input_inject.cpp | 2 +- .../src/distributed_input_node_manager.cpp | 2 +- .../distributed_input_sourceinject_test.cpp | 16 ++++++++-------- services/state/include/dinput_state.h | 3 ++- services/state/src/dinput_state.cpp | 4 ++-- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/services/source/inputinject/src/distributed_input_inject.cpp b/services/source/inputinject/src/distributed_input_inject.cpp index 1cd5e55..249a299 100644 --- a/services/source/inputinject/src/distributed_input_inject.cpp +++ b/services/source/inputinject/src/distributed_input_inject.cpp @@ -56,7 +56,7 @@ int32_t DistributedInputInject::RegisterDistributedHardware(const std::string& d DHLOGE("the DistributedInputNodeManager is null\n"); return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; } - if (inputNodeManager_->openDevicesNode(devId, dhId, parameters) < 0) { + if (inputNodeManager_->OpenDevicesNode(devId, dhId, parameters) < 0) { DHLOGE("create virtual device error\n"); return ERR_DH_INPUT_SERVER_SOURCE_INJECT_REGISTER_FAIL; } diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index 145f460..4a7ec00 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -199,7 +199,7 @@ 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 name for %s", ConvertErrNo().c_str()); + DHLOGE("Could not get device physicalPath for %s", ConvertErrNo().c_str()); return false; } buffer[sizeof(buffer) - 1] = '\0'; 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 3d2827e..ce5bf02 100644 --- a/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp +++ b/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp @@ -421,41 +421,41 @@ HWTEST_F(DistributedInputSourceInjectTest, getDevice_001, testing::ext::TestSize EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_GET_DEVICE_FAIL, ret); } -HWTEST_F(DistributedInputSourceInjectTest, openDevicesNode_001, testing::ext::TestSize.Level1) +HWTEST_F(DistributedInputSourceInjectTest, OpenDevicesNode_001, testing::ext::TestSize.Level1) { std::string devId = "umkyu1b165e1be98151891erbe8r91ev"; std::string dhId = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; std::string parameters = ""; - int32_t ret = DistributedInputInject::GetInstance().inputNodeManager_->openDevicesNode(devId, dhId, parameters); + int32_t ret = DistributedInputInject::GetInstance().inputNodeManager_->OpenDevicesNode(devId, dhId, parameters); EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_OPEN_DEVICE_NODE_FAIL, ret); devId = ""; parameters = "parameters_test"; - ret = DistributedInputInject::GetInstance().inputNodeManager_->openDevicesNode(devId, dhId, parameters); + ret = DistributedInputInject::GetInstance().inputNodeManager_->OpenDevicesNode(devId, dhId, parameters); EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_OPEN_DEVICE_NODE_FAIL, ret); devId = "umkyu1b165e1be98151891erbe8r91ev"; dhId = ""; - ret = DistributedInputInject::GetInstance().inputNodeManager_->openDevicesNode(devId, dhId, parameters); + ret = DistributedInputInject::GetInstance().inputNodeManager_->OpenDevicesNode(devId, dhId, parameters); EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_OPEN_DEVICE_NODE_FAIL, ret); } -HWTEST_F(DistributedInputSourceInjectTest, openDevicesNode_002, testing::ext::TestSize.Level1) +HWTEST_F(DistributedInputSourceInjectTest, OpenDevicesNode_002, testing::ext::TestSize.Level1) { std::string devId(DEV_ID_LENGTH_MAX + 1, 'a'); std::string dhId = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; std::string parameters = "parameters_test"; - int32_t ret = DistributedInputInject::GetInstance().inputNodeManager_->openDevicesNode(devId, dhId, parameters); + int32_t ret = DistributedInputInject::GetInstance().inputNodeManager_->OpenDevicesNode(devId, dhId, parameters); EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_OPEN_DEVICE_NODE_FAIL, ret); devId = "umkyu1b165e1be98151891erbe8r91ev"; std::string dhIds(DH_ID_LENGTH_MAX + 1, 'a'); - ret = DistributedInputInject::GetInstance().inputNodeManager_->openDevicesNode(devId, dhIds, parameters); + ret = DistributedInputInject::GetInstance().inputNodeManager_->OpenDevicesNode(devId, dhIds, parameters); EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_OPEN_DEVICE_NODE_FAIL, ret); std::string dhIdtest = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; std::string param(STRING_MAX_SIZE + 1, 'a'); - ret = DistributedInputInject::GetInstance().inputNodeManager_->openDevicesNode(devId, dhIdtest, param); + ret = DistributedInputInject::GetInstance().inputNodeManager_->OpenDevicesNode(devId, dhIdtest, param); EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_OPEN_DEVICE_NODE_FAIL, ret); } diff --git a/services/state/include/dinput_state.h b/services/state/include/dinput_state.h index 60a46b7..d7e7f1e 100644 --- a/services/state/include/dinput_state.h +++ b/services/state/include/dinput_state.h @@ -19,6 +19,7 @@ #include #include #include +#include namespace OHOS { namespace DistributedHardware { @@ -49,7 +50,7 @@ private: 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); + void KeyUpInject(std::vector shareDhidsPaths, std::vector shareDhIds); bool IsExistDhid(const std::string &dhid); void RecordEventLog(const input_event& event); diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index f1e7c01..3ccdfe0 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -21,7 +21,6 @@ #include #include #include -#include #include "dinput_errcode.h" #include "dinput_log.h" @@ -153,7 +152,8 @@ int BitIsSet(const unsigned long *array, int bit) void DInputState::CheckKeyState(std::string &dhid, std::string &keyboardNodePath) { - DHLOGI("CheckKeyState enter, dhid :%s, keyboardNodePath :%s.", GetAnonyString(dhid).c_str(), keyboardNodePath.c_str()); + DHLOGI("CheckKeyState 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 || realpath(keyboardNodePath.c_str(), canonicalPath) == nullptr) { -- Gitee From f5aca6af4b2e572134003c2584f96cc65c7b9e9f Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Tue, 25 Jul 2023 22:42:49 +0800 Subject: [PATCH 07/18] =?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 | 198 +++++++++++------- utils/include/dinput_utils_tool.h | 7 +- utils/src/dinput_utils_tool.cpp | 101 ++++++++- 23 files changed, 324 insertions(+), 456 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(); - for (int32_t i = 0; i < len; ++i) { - CheckKeyState(shareDhIds[i], shareDhidsPaths[i]); + 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); } -int BitIsSet(const unsigned long *array, int bit) +void DInputState::WriteEventToDev(int &fd, const input_event &event) { - return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS))); + 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) { + 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); + } } -void DInputState::CheckKeyState(std::string &dhid, std::string &keyboardNodePath) +void DInputState::CheckKeyboardState(std::string &dhid, std::string &keyboardNodePath, + std::vector &keyboardPressedKeys, int &fd) { - 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; + } + int fd = open(canonicalPath, O_RDONLY | O_NONBLOCK); + if (fd < 0) { + DHLOGE("open mouse Node Path error:", errno); + 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"; + 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; } - DHLOGD("5.E2E-Test Source write event into input driver, EventType: %s, Code: %d, Value: %d", - eventType.c_str(), event.code, event.value); + 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 -- Gitee From 20ad39a511fc58ae7a1fd96a686310bffcf4cd60 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Tue, 25 Jul 2023 22:56:04 +0800 Subject: [PATCH 08/18] 1111 Signed-off-by: liqiao49 --- .../source/inputinject/include/distributed_input_inject.h | 4 ++-- .../inputinject/include/distributed_input_node_manager.h | 4 ++-- services/state/src/dinput_state.cpp | 5 ++--- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/services/source/inputinject/include/distributed_input_inject.h b/services/source/inputinject/include/distributed_input_inject.h index 5846970..d1d293f 100644 --- a/services/source/inputinject/include/distributed_input_inject.h +++ b/services/source/inputinject/include/distributed_input_inject.h @@ -56,8 +56,8 @@ public: const std::string &sinkNodeDesc); void SyncNodeOfflineInfo(const std::string &srcDevId, const std::string &sinkDevId, const std::string &sinkNodeId); - void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, std::vector &shareDhidsPaths, - std::vector &shareDhIds); + void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, + std::vector &shareDhidsPaths, std::vector &shareDhIds); private: DistributedInputInject(); diff --git a/services/source/inputinject/include/distributed_input_node_manager.h b/services/source/inputinject/include/distributed_input_node_manager.h index 48b32cb..e38def6 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -55,8 +55,8 @@ public: void GetDevicesInfoByDhId(std::vector dhidsVec, std::map &datas); void ProcessInjectEvent(const std::shared_ptr &rawEvent); - void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, std::vector &shareDhidsPaths, - std::vector &shareDhIds); + void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, + std::vector &shareDhidsPaths, std::vector &shareDhIds); private: void AddDeviceLocked(const std::string& dhId, std::unique_ptr device); int32_t CreateHandle(const InputDevice& inputDevice, const std::string& devId, const std::string& dhId); diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index 41dd375..3421298 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -33,7 +33,6 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { - DInputState::~DInputState() { Release(); @@ -161,7 +160,7 @@ void DInputState::WriteEventToDev(int &fd, const input_event &event) void DInputState::SpecEveInject(const int32_t &sessionId, std::vector dhids) { DHLOGI("SpecEveInject enter"); - //mouse event send to remote device + // mouse event send to remote device if (sessionId != -1) { std::string mouseNodePath; std::string mouseNodeDhId; @@ -169,7 +168,7 @@ void DInputState::SpecEveInject(const int32_t &sessionId, std::vector keyboardNodePaths; std::vector keyboardNodeDhIds; DistributedInputCollector::GetInstance().GetShareKeyboardPathsByDhIds(dhids, keyboardNodePaths, keyboardNodeDhIds); -- Gitee From b619b0962eb4374807071390fcd9f65fae6ceb8b Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Tue, 25 Jul 2023 22:59:52 +0800 Subject: [PATCH 09/18] 2222 Signed-off-by: liqiao49 --- services/sink/transport/BUILD.gn | 2 -- 1 file changed, 2 deletions(-) diff --git a/services/sink/transport/BUILD.gn b/services/sink/transport/BUILD.gn index 8341c43..240becf 100644 --- a/services/sink/transport/BUILD.gn +++ b/services/sink/transport/BUILD.gn @@ -30,7 +30,6 @@ ohos_shared_library("libdinput_sink_trans") { "${fwk_interfaces_path}/include", "${fwk_interfaces_path}/include/ipc", "${distributedinput_path}/services/transportbase/include", - "${distributedinput_path}/services/state/include", ] sources = [ @@ -46,7 +45,6 @@ ohos_shared_library("libdinput_sink_trans") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", - "${distributedinput_path}/services/state:libdinput_state", "${distributedinput_path}/services/transportbase:libdinput_trans_base", "${utils_path}:libdinput_utils", "//third_party/libevdev:libevdev", -- Gitee From 89688c9ec7fa70a590cccdd7123f083342063ce3 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Wed, 26 Jul 2023 10:47:47 +0800 Subject: [PATCH 10/18] 3333 Signed-off-by: liqiao49 --- .../sink/sinkmanager/src/distributed_input_sink_manager.cpp | 4 ++-- .../source/inputinject/src/distributed_input_node_manager.cpp | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp index cc6930b..8f95e57 100644 --- a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp +++ b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp @@ -213,7 +213,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnStartRemoteInput( std::vector vecStr; StringSplitToVector(deviceInfo.second, INPUT_STRING_SPLIT_POINT, vecStr); DInputState::GetInstance().AddDhids(vecStr); - DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_IN, sessionId); + DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_OUT, sessionId); } } } @@ -439,7 +439,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStartTypeRemoteInpu std::vector vecStr; StringSplitToVector(deviceInfo.second, INPUT_STRING_SPLIT_POINT, vecStr); DInputState::GetInstance().AddDhids(vecStr); - DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_IN, toSinkSessionId); + DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_OUT, toSinkSessionId); } } diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index 1f053dd..d90535e 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -65,7 +65,7 @@ int32_t DistributedInputNodeManager::OpenDevicesNode(const std::string& devId, c DHLOGE("Can not create virtual node!"); return ERR_DH_INPUT_SERVER_SOURCE_OPEN_DEVICE_NODE_FAIL; } - + ScanSinkInputDevices(DEVICE_PATH); return DH_SUCCESS; } @@ -248,7 +248,6 @@ int32_t DistributedInputNodeManager::CreateHandle(const InputDevice& inputDevice return ERR_DH_INPUT_SERVER_SOURCE_CREATE_HANDLE_FAIL; } AddDeviceLocked(inputDevice.descriptor, std::move(virtualDevice)); - ScanSinkInputDevices(DEVICE_PATH); return DH_SUCCESS; } -- Gitee From 6613f5987ae2c5c5659181f64b1f8c396515c456 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Wed, 26 Jul 2023 16:58:34 +0800 Subject: [PATCH 11/18] 4444 Signed-off-by: liqiao49 --- .../src/distributed_input_sink_manager.cpp | 18 +++---- .../src/distributed_input_source_manager.cpp | 3 +- .../distributed_input_source_transport.cpp | 6 +-- services/state/include/dinput_state.h | 7 ++- services/state/src/dinput_state.cpp | 49 +++++++------------ 5 files changed, 29 insertions(+), 54 deletions(-) diff --git a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp index 8f95e57..1dee49f 100644 --- a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp +++ b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp @@ -212,8 +212,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnStartRemoteInput( DHLOGI("deviceInfo dhId, %s", GetAnonyString(deviceInfo.second).c_str()); std::vector vecStr; StringSplitToVector(deviceInfo.second, INPUT_STRING_SPLIT_POINT, vecStr); - DInputState::GetInstance().AddDhids(vecStr); - DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_OUT, sessionId); + DInputState::GetInstance().RecordDhids(vecStr, DhidState::THROUGH_OUT, sessionId); } } } @@ -273,8 +272,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnStartRemoteInputDhid(con std::vector vecStr; StringSplitToVector(strDhids, INPUT_STRING_SPLIT_POINT, vecStr); - DInputState::GetInstance().AddDhids(vecStr); - DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_OUT, sessionId); + DInputState::GetInstance().RecordDhids(vecStr, DhidState::THROUGH_OUT, sessionId); AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, vecStr); sinkManagerObj_->StoreStartDhids(sessionId, affDhIds.sharingDhIds); DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); @@ -293,8 +291,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnStopRemoteInputDhid(cons stopIndeedOnes.noSharingDhIds = stopIndeedDhIds; DistributedInputCollector::GetInstance().ReportDhIdSharingState(stopIndeedOnes); - DInputState::GetInstance().AddDhids(stopOnCmdDhIds); - DInputState::GetInstance().SwitchState(stopOnCmdDhIds, DhidState::THROUGH_IN, -1); + DInputState::GetInstance().RecordDhids(stopOnCmdDhIds, DhidState::THROUGH_IN, -1); if (DistributedInputCollector::GetInstance().IsAllDevicesStoped()) { DHLOGE("All dhid stop sharing, sessionId: %d is closed.", sessionId); @@ -342,8 +339,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStartDhidRemoteInpu std::vector vecStr; StringSplitToVector(strDhids, INPUT_STRING_SPLIT_POINT, vecStr); - DInputState::GetInstance().AddDhids(vecStr); - DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_OUT, toSinkSessionId); + DInputState::GetInstance().RecordDhids(vecStr, DhidState::THROUGH_OUT, toSinkSessionId); AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, vecStr); sinkManagerObj_->StoreStartDhids(toSinkSessionId, affDhIds.sharingDhIds); DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); @@ -362,8 +358,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStopDhidRemoteInput stopIndeedOnes.noSharingDhIds = stopIndeedDhIds; DistributedInputCollector::GetInstance().ReportDhIdSharingState(stopIndeedOnes); - DInputState::GetInstance().AddDhids(stopOnCmdDhIds); - DInputState::GetInstance().SwitchState(stopOnCmdDhIds, DhidState::THROUGH_IN, -1); + DInputState::GetInstance().RecordDhids(stopOnCmdDhIds, DhidState::THROUGH_IN, -1); if (DistributedInputCollector::GetInstance().IsAllDevicesStoped()) { DHLOGE("All dhid stop sharing, sessionId: %d is closed.", toSinkSessionId); @@ -438,8 +433,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStartTypeRemoteInpu DHLOGI("deviceInfo dhId, %s", GetAnonyString(deviceInfo.second).c_str()); std::vector vecStr; StringSplitToVector(deviceInfo.second, INPUT_STRING_SPLIT_POINT, vecStr); - DInputState::GetInstance().AddDhids(vecStr); - DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_OUT, toSinkSessionId); + DInputState::GetInstance().RecordDhids(vecStr, DhidState::THROUGH_OUT, toSinkSessionId); } } diff --git a/services/source/sourcemanager/src/distributed_input_source_manager.cpp b/services/source/sourcemanager/src/distributed_input_source_manager.cpp index ad0566f..6c9c46b 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -285,8 +285,7 @@ void DistributedInputSourceManager::DInputSourceListener::OnResponseStartRemoteI std::vector vecStr; StringSplitToVector(dhids, INPUT_STRING_SPLIT_POINT, vecStr); - DInputState::GetInstance().AddDhids(vecStr); - DInputState::GetInstance().SwitchState(vecStr, DhidState::THROUGH_IN, -1); + DInputState::GetInstance().RecordDhids(vecStr, DhidState::THROUGH_IN, -1); std::shared_ptr jsonArrayMsg = std::make_shared(); nlohmann::json tmpJson; diff --git a/services/source/transport/src/distributed_input_source_transport.cpp b/services/source/transport/src/distributed_input_source_transport.cpp index bee9d4a..91d616d 100644 --- a/services/source/transport/src/distributed_input_source_transport.cpp +++ b/services/source/transport/src/distributed_input_source_transport.cpp @@ -298,8 +298,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, -1); + DInputState::GetInstance().RecordDhids(vecStr, DhidState::THROUGH_IN, -1); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_START_DHID_FOR_REL; @@ -764,8 +763,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, -1); + DInputState::GetInstance().RecordDhids(dhids, DhidState::THROUGH_OUT, -1); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_STOP_DHID; diff --git a/services/state/include/dinput_state.h b/services/state/include/dinput_state.h index b7a9b68..8931611 100644 --- a/services/state/include/dinput_state.h +++ b/services/state/include/dinput_state.h @@ -44,9 +44,8 @@ public: int32_t Init(); 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, const int32_t &sessionId); + int32_t RecordDhids(const std::vector &dhids, DhidState state, const int32_t &sessionId); + int32_t RemoveDhids(const std::vector &dhids); DhidState GetStateByDhid(std::string &dhid); private: @@ -55,7 +54,7 @@ private: 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); + void SpecEventInject(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); diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index 3421298..378b09e 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -52,52 +52,37 @@ int32_t DInputState::Release() return DH_SUCCESS; } -int32_t DInputState::AddDhids(const std::vector &dhids) +int32_t DInputState::RecordDhids(const std::vector &dhids, DhidState state, const int32_t &sessionId) { - DHLOGI("AddDhid dhids size = %zu", dhids.size()); + DHLOGI("RecordDhids dhids size = %zu", dhids.size()); std::unique_lock mapLock(operationMutex_); for (auto &dhid : dhids) { - DHLOGD("add dhid : %s", GetAnonyString(dhid).c_str()); + DHLOGD("add dhid : %s, state : %d.", GetAnonyString(dhid).c_str(), state); if (IsDhidExist(dhid)) { DHLOGI("dhid : %s already exist.", GetAnonyString(dhid).c_str()); } else { - dhidStateMap_[dhid] = DhidState::THROUGH_IN; + dhidStateMap_[dhid] = state; } } - return DH_SUCCESS; -} -int32_t DInputState::DeleteDhids(const std::vector &dhids) -{ - DHLOGI("DeleteDhid dhids size = %zu", dhids.size()); - std::unique_lock mapLock(operationMutex_); - for (auto &dhid : dhids) { - DHLOGD("delete dhid : %s", GetAnonyString(dhid).c_str()); - if (!IsDhidExist(dhid)) { - DHLOGE("dhid : %s not exist.", GetAnonyString(dhid).c_str()); - } else { - dhidStateMap_.erase(dhid); - } + if (state == DhidState::THROUGH_OUT) { + CreateSpecialEventInjectThread(sessionId, dhids); } return DH_SUCCESS; } -int32_t DInputState::SwitchState(const std::vector &dhids, DhidState state, const int32_t &sessionId) +int32_t DInputState::RemoveDhids(const std::vector &dhids) { + DHLOGI("RemoveDhids dhids size = %zu", dhids.size()); std::unique_lock mapLock(operationMutex_); for (auto &dhid : dhids) { - DHLOGD("SwitchState dhid : %s, state : %d.", GetAnonyString(dhid).c_str(), state); + DHLOGD("delete dhid : %s", GetAnonyString(dhid).c_str()); if (!IsDhidExist(dhid)) { DHLOGE("dhid : %s not exist.", GetAnonyString(dhid).c_str()); } else { - dhidStateMap_[dhid] = state; + dhidStateMap_.erase(dhid); } } - - if (state == DhidState::THROUGH_OUT) { - CreateSpecialEventInjectThread(sessionId, dhids); - } - return DH_SUCCESS; } @@ -121,13 +106,13 @@ bool DInputState::IsDhidExist(const std::string &dhid) void DInputState::CreateSpecialEventInjectThread(const int32_t &sessionId, const std::vector &dhids) { 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); + std::thread specEventInjectThread = + std::thread(&DInputState::SpecEventInject, this, sessionId, dhids); + int32_t ret = pthread_setname_np(specEventInjectThread.native_handle(), CHECK_KEY_STATUS_THREAD_NAME); if (ret != 0) { - DHLOGE("specEveInjectThread setname failed."); + DHLOGE("specEventInjectThread setname failed."); } - specEveInjectThread.detach(); + specEventInjectThread.detach(); } void DInputState::RecordEventLog(const input_event &event) @@ -157,7 +142,7 @@ void DInputState::WriteEventToDev(int &fd, const input_event &event) RecordEventLog(event); } -void DInputState::SpecEveInject(const int32_t &sessionId, std::vector dhids) +void DInputState::SpecEventInject(const int32_t &sessionId, std::vector dhids) { DHLOGI("SpecEveInject enter"); // mouse event send to remote device @@ -174,7 +159,7 @@ void DInputState::SpecEveInject(const int32_t &sessionId, std::vector keyboardPressedKeys; int fd = -1; CheckKeyboardState(keyboardNodeDhIds[i], keyboardNodePaths[i], keyboardPressedKeys, fd); -- Gitee From 68b424c0c02112e8ff89f725c968a722d8f28766 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Wed, 26 Jul 2023 18:08:36 +0800 Subject: [PATCH 12/18] 5555 Signed-off-by: liqiao49 --- services/state/include/dinput_state.h | 1 - services/state/src/dinput_state.cpp | 23 ++++------------------- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/services/state/include/dinput_state.h b/services/state/include/dinput_state.h index 8931611..8dd9b49 100644 --- a/services/state/include/dinput_state.h +++ b/services/state/include/dinput_state.h @@ -55,7 +55,6 @@ private: void CheckKeyboardState(std::string &dhid, std::string &keyboardNodePath, std::vector &keyboardPressedKeys, int &fd); void SpecEventInject(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, diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index 378b09e..6aa1282 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -58,11 +58,7 @@ int32_t DInputState::RecordDhids(const std::vector &dhids, DhidStat std::unique_lock mapLock(operationMutex_); for (auto &dhid : dhids) { DHLOGD("add dhid : %s, state : %d.", GetAnonyString(dhid).c_str(), state); - if (IsDhidExist(dhid)) { - DHLOGI("dhid : %s already exist.", GetAnonyString(dhid).c_str()); - } else { - dhidStateMap_[dhid] = state; - } + dhidStateMap_[dhid] = state; } if (state == DhidState::THROUGH_OUT) { @@ -77,32 +73,21 @@ int32_t DInputState::RemoveDhids(const std::vector &dhids) std::unique_lock mapLock(operationMutex_); for (auto &dhid : dhids) { DHLOGD("delete dhid : %s", GetAnonyString(dhid).c_str()); - if (!IsDhidExist(dhid)) { - DHLOGE("dhid : %s not exist.", GetAnonyString(dhid).c_str()); - } else { - dhidStateMap_.erase(dhid); - } + dhidStateMap_.erase(dhid); } return DH_SUCCESS; } DhidState DInputState::GetStateByDhid(std::string &dhid) { - if (!IsDhidExist(dhid)) { + std::unique_lock mapLock(operationMutex_); + if (dhidStateMap_.find(dhid) == dhidStateMap_.end()) { DHLOGE("dhid : %s not exist.", GetAnonyString(dhid).c_str()); return DhidState::THROUGH_IN; } return dhidStateMap_[dhid]; } -bool DInputState::IsDhidExist(const std::string &dhid) -{ - if (dhidStateMap_.find(dhid) == dhidStateMap_.end()) { - return false; - } - return true; -} - void DInputState::CreateSpecialEventInjectThread(const int32_t &sessionId, const std::vector &dhids) { DHLOGI("CreateSpecialEventInjectThread enter, dhids.size = %d, sessionId = %d.", dhids.size(), sessionId); -- Gitee From 738ad59b9531b7baa53f0724e694216c3be0aca9 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Wed, 26 Jul 2023 19:29:58 +0800 Subject: [PATCH 13/18] 6666 Signed-off-by: liqiao49 --- services/state/src/dinput_state.cpp | 6 ++++-- utils/src/dinput_utils_tool.cpp | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index 6aa1282..191adbe 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -169,7 +169,8 @@ void DInputState::CheckKeyboardState(std::string &dhid, std::string &keyboardNod 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 || + canonicalDevicePath[PATH_MAX] = '\0'; + if (keyboardNodePath.length() == 0 || keyboardNodePath.length() >= PATH_MAX || realpath(keyboardNodePath.c_str(), canonicalPath) == nullptr) { DHLOGE("keyboard Nodepath check fail, error path: %s", keyboardNodePath.c_str()); return; @@ -209,7 +210,8 @@ void DInputState::CheckMouseKeyState(const int32_t &sessionId, const std::string 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 || + canonicalDevicePath[PATH_MAX] = '\0'; + 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; diff --git a/utils/src/dinput_utils_tool.cpp b/utils/src/dinput_utils_tool.cpp index 1154c84..f4cb110 100644 --- a/utils/src/dinput_utils_tool.cpp +++ b/utils/src/dinput_utils_tool.cpp @@ -328,7 +328,8 @@ 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 || + canonicalDevicePath[PATH_MAX] = '\0'; + 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; -- Gitee From 4aa92af7ad985507fa0cd9d8bc3783089a104762 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Wed, 26 Jul 2023 19:32:03 +0800 Subject: [PATCH 14/18] 7777 Signed-off-by: liqiao49 --- services/state/src/dinput_state.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index 191adbe..bf773bf 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -169,7 +169,7 @@ void DInputState::CheckKeyboardState(std::string &dhid, std::string &keyboardNod DHLOGI("CheckKeyboardState enter, dhid %s, keyboardNodePath %s.", GetAnonyString(dhid).c_str(), keyboardNodePath.c_str()); char canonicalPath[PATH_MAX + 1] = {0x00}; - canonicalDevicePath[PATH_MAX] = '\0'; + canonicalPath[PATH_MAX] = '\0'; if (keyboardNodePath.length() == 0 || keyboardNodePath.length() >= PATH_MAX || realpath(keyboardNodePath.c_str(), canonicalPath) == nullptr) { DHLOGE("keyboard Nodepath check fail, error path: %s", keyboardNodePath.c_str()); @@ -210,7 +210,7 @@ void DInputState::CheckMouseKeyState(const int32_t &sessionId, const std::string DHLOGI("CheckMouseKeyState enter, mouseNodePath %s, mouseNodeDhId %s, sessionId %d.", mouseNodePath.c_str(), GetAnonyString(mouseNodeDhId).c_str(), sessionId); char canonicalPath[PATH_MAX + 1] = {0x00}; - canonicalDevicePath[PATH_MAX] = '\0'; + canonicalPath[PATH_MAX] = '\0'; 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()); -- Gitee From 22e363ebbd3985e87c2aff523cb78638463b0259 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Wed, 26 Jul 2023 19:41:20 +0800 Subject: [PATCH 15/18] 8888 Signed-off-by: liqiao49 --- services/state/src/dinput_state.cpp | 8 +++----- utils/src/dinput_utils_tool.cpp | 5 ++--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index bf773bf..023ae13 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -168,8 +168,7 @@ void DInputState::CheckKeyboardState(std::string &dhid, std::string &keyboardNod { DHLOGI("CheckKeyboardState enter, dhid %s, keyboardNodePath %s.", GetAnonyString(dhid).c_str(), keyboardNodePath.c_str()); - char canonicalPath[PATH_MAX + 1] = {0x00}; - canonicalPath[PATH_MAX] = '\0'; + char* canonicalPath = new char(PATH_MAX); if (keyboardNodePath.length() == 0 || keyboardNodePath.length() >= PATH_MAX || realpath(keyboardNodePath.c_str(), canonicalPath) == nullptr) { DHLOGE("keyboard Nodepath check fail, error path: %s", keyboardNodePath.c_str()); @@ -209,9 +208,8 @@ void DInputState::CheckMouseKeyState(const int32_t &sessionId, const std::string { DHLOGI("CheckMouseKeyState enter, mouseNodePath %s, mouseNodeDhId %s, sessionId %d.", mouseNodePath.c_str(), GetAnonyString(mouseNodeDhId).c_str(), sessionId); - char canonicalPath[PATH_MAX + 1] = {0x00}; - canonicalPath[PATH_MAX] = '\0'; - if (mouseNodePath.length() == 0 || mouseNodePath.length() >= PATH_MAX || + char* canonicalPath = new char(PATH_MAX); + 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; diff --git a/utils/src/dinput_utils_tool.cpp b/utils/src/dinput_utils_tool.cpp index f4cb110..edbd4e0 100644 --- a/utils/src/dinput_utils_tool.cpp +++ b/utils/src/dinput_utils_tool.cpp @@ -327,9 +327,8 @@ void StringSplitToVector(const std::string& str, const char split, std::vector= PATH_MAX || + char* canonicalDevicePath = new char(PATH_MAX); + 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; -- Gitee From 67cac132dbd113a5651b47586f6c62231ecb4d0f Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Wed, 26 Jul 2023 19:46:49 +0800 Subject: [PATCH 16/18] 9999 Signed-off-by: liqiao49 --- services/state/src/dinput_state.cpp | 4 ++-- utils/src/dinput_utils_tool.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index 023ae13..f17f0a3 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -168,7 +168,7 @@ void DInputState::CheckKeyboardState(std::string &dhid, std::string &keyboardNod { DHLOGI("CheckKeyboardState enter, dhid %s, keyboardNodePath %s.", GetAnonyString(dhid).c_str(), keyboardNodePath.c_str()); - char* canonicalPath = new char(PATH_MAX); + char canonicalPath[PATH_MAX] = {0x00}; if (keyboardNodePath.length() == 0 || keyboardNodePath.length() >= PATH_MAX || realpath(keyboardNodePath.c_str(), canonicalPath) == nullptr) { DHLOGE("keyboard Nodepath check fail, error path: %s", keyboardNodePath.c_str()); @@ -208,7 +208,7 @@ void DInputState::CheckMouseKeyState(const int32_t &sessionId, const std::string { DHLOGI("CheckMouseKeyState enter, mouseNodePath %s, mouseNodeDhId %s, sessionId %d.", mouseNodePath.c_str(), GetAnonyString(mouseNodeDhId).c_str(), sessionId); - char* canonicalPath = new char(PATH_MAX); + char canonicalPath[PATH_MAX] = {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()); diff --git a/utils/src/dinput_utils_tool.cpp b/utils/src/dinput_utils_tool.cpp index edbd4e0..bb6db41 100644 --- a/utils/src/dinput_utils_tool.cpp +++ b/utils/src/dinput_utils_tool.cpp @@ -327,8 +327,8 @@ void StringSplitToVector(const std::string& str, const char split, std::vector PATH_MAX || + char canonicalDevicePath[PATH_MAX] = {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; -- Gitee From ec0682eecd93c5c487adee34e3135531526fb751 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Thu, 27 Jul 2023 23:30:43 +0800 Subject: [PATCH 17/18] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=80=A7=E8=83=BD?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liqiao49 --- services/source/inputinject/BUILD.gn | 1 + .../include/distributed_input_inject.h | 2 + .../include/distributed_input_node_manager.h | 26 +++++- .../src/distributed_input_inject.cpp | 11 +++ .../src/distributed_input_node_manager.cpp | 80 ++++++++++++++++--- .../src/distributed_input_source_manager.cpp | 3 + services/state/src/dinput_state.cpp | 2 +- 7 files changed, 110 insertions(+), 15 deletions(-) diff --git a/services/source/inputinject/BUILD.gn b/services/source/inputinject/BUILD.gn index 07b4208..0c69735 100644 --- a/services/source/inputinject/BUILD.gn +++ b/services/source/inputinject/BUILD.gn @@ -56,6 +56,7 @@ ohos_shared_library("libdinput_inject") { external_deps = [ "c_utils:utils", "dsoftbus:softbus_client", + "eventhandler:libeventhandler", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/services/source/inputinject/include/distributed_input_inject.h b/services/source/inputinject/include/distributed_input_inject.h index d1d293f..68d556e 100644 --- a/services/source/inputinject/include/distributed_input_inject.h +++ b/services/source/inputinject/include/distributed_input_inject.h @@ -59,6 +59,8 @@ public: void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds); + void NotifyNodeMgrScanVirNode(const std::string &dhId); + 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 e38def6..c900322 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -24,6 +24,7 @@ #include #include +#include "event_handler.h" #include "nlohmann/json.hpp" #include "constants_dinput.h" @@ -33,6 +34,8 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { +const uint32_t DINPUT_NODE_MANAGER_SCAN_ALL_NODE = 1; +const std::string INPUT_NODE_DHID = "dhId"; class DistributedInputNodeManager { public: DistributedInputNodeManager(); @@ -57,6 +60,24 @@ public: void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds); + void NotifyNodeMgrScanVirNode(const std::string &dhId); + + class DInputNodeManagerEventHandler : public AppExecFwk::EventHandler { + public: + DInputNodeManagerEventHandler(const std::shared_ptr &runner, + DistributedInputNodeManager *manager); + ~DInputNodeManagerEventHandler() override; + + void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; + private: + void ScanAllNode(const AppExecFwk::InnerEvent::Pointer &event); + + using nodeMgrFunc = void (DInputNodeManagerEventHandler::*)( + const AppExecFwk::InnerEvent::Pointer &event); + std::map eventFuncMap_; + DistributedInputNodeManager *nodeManagerObj_; + }; + private: void AddDeviceLocked(const std::string& dhId, std::unique_ptr device); int32_t CreateHandle(const InputDevice& inputDevice, const std::string& devId, const std::string& dhId); @@ -64,8 +85,8 @@ private: void VerifyInputDevice(const nlohmann::json& inputDeviceJson, InputDevice& pBuf); void InjectEvent(); - void ScanSinkInputDevices(const std::string& dirName); - void OpenInputDevice(const std::string& devicePath); + void ScanSinkInputDevices(const std::string& dhId); + void OpenInputDevice(const std::string& devicePath, const std::string& dhId); bool IsVirtualDev(int fd); bool GetDevDhIdByFd(int fd, std::string& dhId, std::string& physicalPath); void SetPathForDevMap(std::string& dhId, const std::string& devicePath); @@ -83,6 +104,7 @@ private: std::unique_ptr inputHub_; int32_t virtualTouchScreenFd_; std::once_flag callOnceFlag_; + std::shared_ptr callBackHandler_; }; } // namespace DistributedInput } // namespace DistributedHardware diff --git a/services/source/inputinject/src/distributed_input_inject.cpp b/services/source/inputinject/src/distributed_input_inject.cpp index 1de63d9..010d6d6 100644 --- a/services/source/inputinject/src/distributed_input_inject.cpp +++ b/services/source/inputinject/src/distributed_input_inject.cpp @@ -259,12 +259,23 @@ int32_t DistributedInputInject::GetVirtualTouchScreenFd() void DistributedInputInject::GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds) { + std::lock_guard lock(inputNodeManagerMutex_); if (inputNodeManager_ == nullptr) { DHLOGE("inputNodeManager is nullptr"); return; } inputNodeManager_->GetVirtualKeyboardPathsByDhIds(dhIds, shareDhidsPaths, shareDhIds); } + +void DistributedInputInject::NotifyNodeMgrScanVirNode(const std::string &dhId) +{ + std::lock_guard lock(inputNodeManagerMutex_); + if (inputNodeManager_ == nullptr) { + DHLOGE("inputNodeManager is nullptr"); + return; + } + inputNodeManager_->NotifyNodeMgrScanVirNode(dhId); +} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index d90535e..47fcdac 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -34,11 +34,14 @@ namespace DistributedInput { DistributedInputNodeManager::DistributedInputNodeManager() : isInjectThreadCreated_(false), isInjectThreadRunning_(false), inputHub_(std::make_unique()), virtualTouchScreenFd_(UN_INIT_FD_VALUE) { + DHLOGI("DistributedInputNodeManager ctor"); + std::shared_ptr runner = AppExecFwk::EventRunner::Create(true); + callBackHandler_ = std::make_shared(runner, this); } DistributedInputNodeManager::~DistributedInputNodeManager() { - DHLOGI("destructor start"); + DHLOGI("DistributedInputNodeManager dtor"); isInjectThreadCreated_.store(false); isInjectThreadRunning_.store(false); if (eventInjectThread_.joinable()) { @@ -65,7 +68,6 @@ int32_t DistributedInputNodeManager::OpenDevicesNode(const std::string& devId, c DHLOGE("Can not create virtual node!"); return ERR_DH_INPUT_SERVER_SOURCE_OPEN_DEVICE_NODE_FAIL; } - ScanSinkInputDevices(DEVICE_PATH); return DH_SUCCESS; } @@ -128,16 +130,67 @@ void DistributedInputNodeManager::VerifyInputDevice(const nlohmann::json& inputD } } -void DistributedInputNodeManager::ScanSinkInputDevices(const std::string& dirName) +void DistributedInputNodeManager::ScanSinkInputDevices(const std::string& dhId) { - DHLOGI("ScanSinkInputDevices enter, dirName %s.", dirName.c_str()); + DHLOGI("ScanSinkInputDevices enter, dhId %s.", dhId.c_str()); std::vector vecInputDevPath; - ScanInputDevicesPath(dirName, vecInputDevPath); + ScanInputDevicesPath(DEVICE_PATH, vecInputDevPath); for (auto &tempPath: vecInputDevPath) { - OpenInputDevice(tempPath); + OpenInputDevice(tempPath, dhId); } } +void DistributedInputNodeManager::DInputNodeManagerEventHandler::ProcessEvent( + const AppExecFwk::InnerEvent::Pointer &event) +{ + DHLOGI("ProcessEvent enter."); + auto iter = eventFuncMap_.find(event->GetInnerEventId()); + if (iter == eventFuncMap_.end()) { + DHLOGE("Event Id %d is undefined.", event->GetInnerEventId()); + return; + } + nodeMgrFunc &func = iter->second; + (this->*func)(event); +} + +DistributedInputNodeManager::DInputNodeManagerEventHandler::DInputNodeManagerEventHandler( + const std::shared_ptr &runner, DistributedInputNodeManager *manager) + : AppExecFwk::EventHandler(runner) +{ + eventFuncMap_[DINPUT_NODE_MANAGER_SCAN_ALL_NODE] = &DInputNodeManagerEventHandler::ScanAllNode; + + nodeManagerObj_ = manager; +} + +DistributedInputNodeManager::DInputNodeManagerEventHandler::~DInputNodeManagerEventHandler() +{ + eventFuncMap_.clear(); + nodeManagerObj_ = nullptr; +} + +void DistributedInputNodeManager::DInputNodeManagerEventHandler::ScanAllNode( + const AppExecFwk::InnerEvent::Pointer &event) +{ + DHLOGI("ScanAllNode enter."); + std::shared_ptr dataMsg = event->GetSharedObject(); + auto it = dataMsg->begin(); + nlohmann::json innerMsg = *(it); + std::string devicedhId = innerMsg[INPUT_NODE_DHID]; + nodeManagerObj_->ScanSinkInputDevices(devicedhId); +} + +void DistributedInputNodeManager::NotifyNodeMgrScanVirNode(const std::string &dhId) +{ + DHLOGI("NotifyNodeMgrScanVirNode enter."); + std::shared_ptr jsonArrayMsg = std::make_shared(); + nlohmann::json tmpJson; + tmpJson[INPUT_NODE_DHID] = dhId; + jsonArrayMsg->push_back(tmpJson); + AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get( + DINPUT_NODE_MANAGER_SCAN_ALL_NODE, jsonArrayMsg, 0); + callBackHandler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); +} + bool DistributedInputNodeManager::IsVirtualDev(int fd) { char buffer[256] = {0}; @@ -179,6 +232,7 @@ bool DistributedInputNodeManager::GetDevDhIdByFd(int fd, std::string& dhId, std: void DistributedInputNodeManager::SetPathForDevMap(std::string& dhId, 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()); @@ -191,30 +245,32 @@ void DistributedInputNodeManager::SetPathForDevMap(std::string& dhId, const std: } } -void DistributedInputNodeManager::OpenInputDevice(const std::string& devicePath) +void DistributedInputNodeManager::OpenInputDevice(const std::string& devicePath, const std::string& dhId) { DHLOGI("Opening input device path: %s", devicePath.c_str()); - std::string dhId; + std::string curDhId; std::string physicalPath; int fd = OpenInputDeviceFdByPath(devicePath); if (fd == -1) { DHLOGE("The fd open failed, devicePath %s.", devicePath.c_str()); return; } - if (IsVirtualDev(fd) == false) { + if (!IsVirtualDev(fd)) { DHLOGE("The dev not virtual, devicePath %s.", devicePath.c_str()); return; } - if (GetDevDhIdByFd(fd, dhId, physicalPath) == false) { - DHLOGE("Get dev dhid failed, devicePath %s.", devicePath.c_str()); + if (!GetDevDhIdByFd(fd, curDhId, physicalPath) || dhId != curDhId) { + DHLOGE("This is not same dev, curDhId %s.", devicePath.c_str()); return; } - SetPathForDevMap(dhId, devicePath); + DHLOGI("curDhId %s.", GetAnonyString(curDhId).c_str()); + SetPathForDevMap(curDhId, devicePath); } void DistributedInputNodeManager::GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds) { + std::lock_guard lock(virtualDeviceMapMutex_); for (auto dhId_ : dhIds) { auto iter = virtualDeviceMap_.begin(); while (iter != virtualDeviceMap_.end()) { diff --git a/services/source/sourcemanager/src/distributed_input_source_manager.cpp b/services/source/sourcemanager/src/distributed_input_source_manager.cpp index 6c9c46b..4070002 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -1132,6 +1132,9 @@ int32_t DistributedInputSourceManager::RegisterDistributedHardware(const std::st } cli->SyncNodeInfoRemoteInput(GetLocalNetworkId(), dhId, GetNodeDesc(parameters)); + + // 6. Notify node mgr to scan vir dev node info + DistributedInputInject::GetInstance().NotifyNodeMgrScanVirNode(dhId); return DH_SUCCESS; } diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index f17f0a3..1ecdbfa 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -209,7 +209,7 @@ void DInputState::CheckMouseKeyState(const int32_t &sessionId, const std::string DHLOGI("CheckMouseKeyState enter, mouseNodePath %s, mouseNodeDhId %s, sessionId %d.", mouseNodePath.c_str(), GetAnonyString(mouseNodeDhId).c_str(), sessionId); char canonicalPath[PATH_MAX] = {0x00}; - if (mouseNodePath.length() == 0 || mouseNodePath.length() > PATH_MAX || + 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; -- Gitee From c99c28a9a0032feb996329df0b00617ebbd420f8 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Fri, 28 Jul 2023 00:00:53 +0800 Subject: [PATCH 18/18] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liqiao49 --- .../inputinject/src/distributed_input_node_manager.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index 47fcdac..041d4de 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -169,7 +169,7 @@ DistributedInputNodeManager::DInputNodeManagerEventHandler::~DInputNodeManagerEv } void DistributedInputNodeManager::DInputNodeManagerEventHandler::ScanAllNode( - const AppExecFwk::InnerEvent::Pointer &event) + const AppExecFwk::InnerEvent::Pointer &event) { DHLOGI("ScanAllNode enter."); std::shared_ptr dataMsg = event->GetSharedObject(); @@ -237,7 +237,7 @@ void DistributedInputNodeManager::SetPathForDevMap(std::string& dhId, const std: while (iter != virtualDeviceMap_.end()) { DHLOGD("Virtual device map dhid %s.", iter->first.c_str()); if (dhId.compare(iter->first) == 0) { - DHLOGI("Found the virtual device, set path :%s", devicePath.c_str()); + DHLOGD("Found the virtual device, set path :%s", devicePath.c_str()); iter->second->SetPath(devicePath); break; } @@ -263,7 +263,7 @@ void DistributedInputNodeManager::OpenInputDevice(const std::string& devicePath, DHLOGE("This is not same dev, curDhId %s.", devicePath.c_str()); return; } - DHLOGI("curDhId %s.", GetAnonyString(curDhId).c_str()); + DHLOGD("curDhId %s.", GetAnonyString(curDhId).c_str()); SetPathForDevMap(curDhId, devicePath); } -- Gitee