修复注入进程拉起失败

Signed-off-by: liqiao49 <liqiao49@huawei.com>
This commit is contained in:
liqiao49
2023-07-24 16:34:30 +08:00
parent be332357e4
commit 0fa4a57a9f
9 changed files with 70 additions and 70 deletions
@@ -43,8 +43,8 @@ public:
AffectDhIds SetSharingTypes(bool enabled, const uint32_t &inputType);
AffectDhIds SetSharingDhIds(bool enabled, std::vector<std::string> dhIds);
void GetMouseNodePath(std::vector<std::string> dhIds, std::string &mouseNodePath, std::string &dhid);
void GetKeyboardNodePath(
std::vector<std::string> dhIds, std::vector<std::string> &shareDhidsPaths, std::vector<std::string> &shareDhIds);
void GetKeyboardNodePath(std::vector<std::string> dhIds, std::vector<std::string> &shareDhidsPaths,
std::vector<std::string> &shareDhIds);
// false for sharing device exist, true for all devices stop sharing
bool IsAllDevicesStoped();
int32_t RegisterSharingDhIdListener(sptr<ISharingDhIdListener> sharingDhIdListener);
@@ -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;
}
@@ -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<std::string, std::unique_ptr<VirtualDevice>> virtualDeviceMap_;
std::mutex virtualDeviceMapMutex_;
@@ -257,10 +257,10 @@ int32_t DistributedInputInject::GetVirtualTouchScreenFd()
}
void DistributedInputInject::GetVirtualKeyboardPathByDhId(const std::vector<std::string> &dhIds,
std::vector<std::string> &shareDhidsPaths, std::vector<std::string> &shareDhIds)
std::vector<std::string> &shareDhidsPaths, std::vector<std::string> &shareDhIds)
{
if (inputNodeManager_ == nullptr) {
DHLOGI("inputNodeManager_ is nullptr!");
DHLOGE("inputNodeManager is nullptr");
return;
}
inputNodeManager_->GetVirtualKeyboardPathByDhId(dhIds, shareDhidsPaths, shareDhIds);
@@ -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<InputHub>()), 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_);
}
@@ -285,8 +285,8 @@ void DistributedInputSourceManager::DInputSourceListener::OnResponseStartRemoteI
std::vector<std::string> 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<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
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;
}
@@ -316,8 +316,8 @@ int32_t DistributedInputSourceTransport::StartRemoteInputDhids(int32_t srcTsrcSe
std::vector<std::string> 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;
+14 -16
View File
@@ -16,32 +16,30 @@
#ifndef DISTRIBUTED_INPUT_STATE_BASE_H
#define DISTRIBUTED_INPUT_STATE_BASE_H
#include <map>
#include <vector>
#include <string>
#include <pthread.h>
#include <thread>
#include <linux/input.h>
#include <dirent.h>
#include <fcntl.h>
#include <linux/input.h>
#include <map>
#include <mutex>
#include <pthread.h>
#include <string>
#include <thread>
#include <unistd.h>
#include <vector>
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<std::string> &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<std::string, DhidState> dhidStateMap_;
};
} // namespace DistributedInput
} // namespace DistributedHardware
} // namespace OHOS
+29 -26
View File
@@ -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<std::mutex> mapLock(operationMutex_);
dhidStateMap_.clear();
return DH_SUCCESS;
}
int32_t StateMachine::AddDhids(const std::vector<std::string> &dhids)
int32_t DInputState::AddDhids(const std::vector<std::string> &dhids)
{
DHLOGI("AddDhid dhids size = %zu", dhids.size());
std::unique_lock<std::mutex> 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<std::string> &dhids)
return DH_SUCCESS;
}
int32_t StateMachine::DeleteDhids(const std::vector<std::string> &dhids)
int32_t DInputState::DeleteDhids(const std::vector<std::string> &dhids)
{
DHLOGI("DeleteDhid dhids size = %zu", dhids.size());
std::unique_lock<std::mutex> 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<std::string> &dhids)
return DH_SUCCESS;
}
int32_t StateMachine::SwitchState(const std::vector<std::string> &dhids, DhidState state)
int32_t DInputState::SwitchState(const std::vector<std::string> &dhids, DhidState state)
{
std::unique_lock<std::mutex> 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<std::string> &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<std::string> &dhids)
void DInputState::CreateKeyUpInjectThread(const std::vector<std::string> &dhids)
{
DHLOGI("CreateKeyUpInjectThread enter, dhids.size = %d.", dhids.size());
std::vector<std::string> keyboardNodePaths;
std::vector<std::string> 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<std::string> &dhids
keyUpInjectThread.detach();
}
void StateMachine::KeyUpInject(std::vector<std::string> &shareDhidsPaths, std::vector<std::string> &shareDhIds)
void DInputState::KeyUpInject(std::vector<std::string> shareDhidsPaths, std::vector<std::string> 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<uint32_t> &keyboardPressedKeys, std::string &dhid)
void DInputState::UpInject(int fd, std::vector<uint32_t> &keyboardPressedKeys, std::string &dhid)
{
for (auto &code: keyboardPressedKeys) {
struct input_event event = {
@@ -208,27 +215,23 @@ void StateMachine::UpInject(int fd, std::vector<uint32_t> &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