From 0fa4a57a9f3a90f126cc130026ded64a30dd5fa2 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Mon, 24 Jul 2023 16:34:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B3=A8=E5=85=A5=E8=BF=9B?= =?UTF-8?q?=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 | 30 +++++----- services/state/src/dinput_state.cpp | 55 ++++++++++--------- 9 files changed, 70 insertions(+), 70 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 - +#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