Signed-off-by: liqiao49 <liqiao49@huawei.com>
This commit is contained in:
liqiao49
2023-07-25 22:42:49 +08:00
parent 9ded4ae448
commit c3e42936d0
23 changed files with 328 additions and 460 deletions
-2
View File
@@ -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;
+12 -71
View File
@@ -18,7 +18,6 @@
#include <algorithm>
#include <cinttypes>
#include <cstring>
#include <dirent.h>
#include <fcntl.h>
#include <filesystem>
#include <regex>
@@ -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<InputDevice> 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<std::string> 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<std::mutex> 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<std::string> dhIds, std::stri
}
}
void InputHub::GetShareKeyboardPathByDhId(std::vector<std::string> dhIds, std::vector<std::string> &shareDhidsPath,
void InputHub::GetShareKeyboardPathsByDhIds(std::vector<std::string> dhIds, std::vector<std::string> &shareDhidsPaths,
std::vector<std::string> &shareDhIds)
{
DHLOGI("GetShareKeyboardPathByDhId: devices_.size:%d,", devices_.size());
DHLOGI("GetShareKeyboardPathsByDhIds: devices_.size:%d,", devices_.size());
std::unique_lock<std::mutex> deviceLock(devicesMutex_);
for (auto dhId_ : dhIds) {
for (const auto &[id, device] : devices_) {
@@ -1156,7 +1097,7 @@ void InputHub::GetShareKeyboardPathByDhId(std::vector<std::string> 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);
}
}
}
+2 -3
View File
@@ -53,10 +53,10 @@ public:
void GetDevicesInfoByType(const uint32_t inputTypes, std::map<int32_t, std::string> &datas);
void GetDevicesInfoByDhId(std::vector<std::string> dhidsVec, std::map<int32_t, std::string> &datas);
void GetShareMousePathByDhId(std::vector<std::string> dhIds, std::string &path, std::string &dhId);
void GetShareKeyboardPathByDhId(std::vector<std::string> dhIds, std::vector<std::string> &shareDhidsPath,
void GetShareKeyboardPathsByDhIds(std::vector<std::string> dhIds, std::vector<std::string> &shareDhidsPaths,
std::vector<std::string> &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);
@@ -43,7 +43,7 @@ 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,
void GetShareKeyboardPathsByDhIds(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();
@@ -175,7 +175,7 @@ void DistributedInputCollector::ReportDhIdSharingState(const AffectDhIds &dhIds)
{
std::lock_guard<std::mutex> 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<std::string> 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<std::string> dhIds, std::vector<std::string> &shareDhidsPaths, std::vector<std::string> &shareDhIds)
void DistributedInputCollector::GetShareKeyboardPathsByDhIds(std::vector<std::string> dhIds,
std::vector<std::string> &shareDhidsPaths, std::vector<std::string> &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);
@@ -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<std::string> &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 {
@@ -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<std::string> 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<std::string> 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<std::string> 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<std::string> stopIndeedDhIds;
std::vector<std::string> 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<std::string> 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<std::string> 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<std::string> stopIndeedDhIds;
std::vector<std::string> 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<std::string> 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<std::string> &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<std::string> 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_) {
@@ -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<std::string> 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();
@@ -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<std::string> &dhIds, std::vector<std::string> &shareDhidsPaths,
void GetVirtualKeyboardPathsByDhIds(const std::vector<std::string> &dhIds, std::vector<std::string> &shareDhidsPaths,
std::vector<std::string> &shareDhIds);
private:
@@ -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<std::string> dhidsVec, std::map<int32_t, std::string> &datas);
void ProcessInjectEvent(const std::shared_ptr<RawEvent> &rawEvent);
void GetVirtualKeyboardPathByDhId(const std::vector<std::string> &dhIds, std::vector<std::string> &shareDhidsPaths,
void GetVirtualKeyboardPathsByDhIds(const std::vector<std::string> &dhIds, std::vector<std::string> &shareDhidsPaths,
std::vector<std::string> &shareDhIds);
private:
void AddDeviceLocked(const std::string& dhId, std::unique_ptr<VirtualDevice> 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);
@@ -256,14 +256,14 @@ int32_t DistributedInputInject::GetVirtualTouchScreenFd()
return inputNodeManager_->GetVirtualTouchScreenFd();
}
void DistributedInputInject::GetVirtualKeyboardPathByDhId(const std::vector<std::string> &dhIds,
void DistributedInputInject::GetVirtualKeyboardPathsByDhIds(const std::vector<std::string> &dhIds,
std::vector<std::string> &shareDhidsPaths, std::vector<std::string> &shareDhIds)
{
if (inputNodeManager_ == nullptr) {
DHLOGE("inputNodeManager is nullptr");
return;
}
inputNodeManager_->GetVirtualKeyboardPathByDhId(dhIds, shareDhidsPaths, shareDhIds);
inputNodeManager_->GetVirtualKeyboardPathsByDhIds(dhIds, shareDhidsPaths, shareDhIds);
}
} // namespace DistributedInput
} // namespace DistributedHardware
@@ -18,11 +18,7 @@
#include <cinttypes>
#include <cstring>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#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<InputHub>()), 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<std::string> 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<std::string> &dhIds,
void DistributedInputNodeManager::GetVirtualKeyboardPathsByDhIds(const std::vector<std::string> &dhIds,
std::vector<std::string> &shareDhidsPaths, std::vector<std::string> &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<std::mutex> lock(virtualDeviceMapMutex_);
auto iter = virtualDeviceMap_.find(dhId);
@@ -455,7 +391,7 @@ void DistributedInputNodeManager::ProcessInjectEvent(const std::shared_ptr<RawEv
DHLOGI("InjectEvent dhId: %s, eventType: %d, eventCode: %d, eventValue: %d, when: " PRId64"",
GetAnonyString(dhId).c_str(), event.type, event.code, event.value, rawEvent->when);
VirtualDevice* device = nullptr;
if (getDevice(dhId, device) < 0) {
if (GetDevice(dhId, device) < 0) {
DHLOGE("could not find the device");
return;
}
@@ -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);
}
@@ -491,7 +491,6 @@ private:
int32_t RelayStopRemoteInputByDhid(const std::string &srcId, const std::string &sinkId,
const std::vector<std::string> &dhIds, sptr<IStartStopDInputsCallback> callback);
bool IsStringDataSame(const std::vector<std::string> &oldDhIds, std::vector<std::string> newDhIds);
void StringSplitToVector(const std::string &str, const char split, std::vector<std::string> &vecStr);
void DeleteNodeInfoAndNotify(const std::string& offlineDevId);
void SendExistVirNodeInfos(sptr<InputNodeListener> listener);
std::set<BeRegNodeInfo> GetSyncNodeInfo(const std::string& devId);
@@ -284,9 +284,9 @@ void DistributedInputSourceManager::DInputSourceListener::OnResponseStartRemoteI
}
std::vector<std::string> 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<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
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<std::string> &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_;
@@ -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<std::string> vecStr;
sourceManager_->StringSplitToVector(str, ',', vecStr);
EXPECT_EQ(true, str.empty());
}
HWTEST_F(DistributedInputSourceManagerTest, RemoveInputDeviceId_01, testing::ext::TestSize.Level1)
{
std::string deviceId = "umkyu1b165e1be98151891erbe8r91ev";
@@ -138,8 +138,6 @@ private:
std::string JointDhIds(const std::vector<std::string> &dhids);
void RegRespFunMap();
void StringSplitToVector(const std::string &str, const char split, std::vector<std::string> &vecStr);
private:
std::mutex operationMutex_;
std::set<int32_t> sessionIdSet_;
@@ -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<std::string> &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<std::string> 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;
+3
View File
@@ -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",
+17 -9
View File
@@ -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<std::string> &dhids);
int32_t DeleteDhids(const std::vector<std::string> &dhids);
int32_t SwitchState(const std::vector<std::string> &dhids, DhidState state);
int32_t SwitchState(const std::vector<std::string> &dhids, DhidState state, const int32_t &sessionId);
DhidState GetStateByDhid(std::string &dhid);
private:
~DInputState();
void CreateKeyUpInjectThread(const std::vector<std::string> &dhids);
void CheckKeyState(std::string &dhid, std::string &keyboardNodePath);
void UpInject(int fd, std::vector<uint32_t> &keyboardPressedKeys, std::string &dhid);
void KeyUpInject(std::vector<std::string> shareDhidsPaths, std::vector<std::string> shareDhIds);
bool IsExistDhid(const std::string &dhid);
void RecordEventLog(const input_event& event);
void CreateSpecialEventInjectThread(const int32_t &sessionId, const std::vector<std::string> &dhids);
void CheckKeyboardState(std::string &dhid, std::string &keyboardNodePath,
std::vector<uint32_t> &keyboardPressedKeys, int &fd);
void SpecEveInject(const int32_t &sessionId, std::vector<std::string> 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_;
+125 -81
View File
@@ -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<std::string> &dhids)
std::unique_lock<std::mutex> 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<std::string> &dhids)
std::unique_lock<std::mutex> 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<std::string> &dhids)
return DH_SUCCESS;
}
int32_t DInputState::SwitchState(const std::vector<std::string> &dhids, DhidState state)
int32_t DInputState::SwitchState(const std::vector<std::string> &dhids, DhidState state, const int32_t &sessionId)
{
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)) {
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<std::string> &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<std::string> &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<std::string> &dhids)
void DInputState::CreateSpecialEventInjectThread(const int32_t &sessionId, 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(&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<std::string> shareDhidsPaths, std::vector<std::string> 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();
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);
}
void DInputState::WriteEventToDev(int &fd, const input_event &event)
{
if (write(fd, &event, sizeof(event)) < static_cast<ssize_t>(sizeof(event))) {
DHLOGE("could not inject event, removed? (fd: %d)", fd);
return;
}
RecordEventLog(event);
}
void DInputState::SpecEveInject(const int32_t &sessionId, std::vector<std::string> 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<std::string> keyboardNodePaths;
std::vector<std::string> 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) {
CheckKeyState(shareDhIds[i], shareDhidsPaths[i]);
std::vector<uint32_t> 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);
}
}
int BitIsSet(const unsigned long *array, int bit)
void DInputState::CheckKeyboardState(std::string &dhid, std::string &keyboardNodePath,
std::vector<uint32_t> &keyboardPressedKeys, int &fd)
{
return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS)));
}
void DInputState::CheckKeyState(std::string &dhid, std::string &keyboardNodePath)
{
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<uint32_t> 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<uint32_t> &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<ssize_t>(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<ssize_t>(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;
}
}
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";
break;
int fd = open(canonicalPath, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
DHLOGE("open mouse Node Path error:", errno);
return;
}
DHLOGD("5.E2E-Test Source write event into input driver, EventType: %s, Code: %d, Value: %d",
eventType.c_str(), event.code, event.value);
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;
}
CloseFd(fd);
}
} // namespace DistributedInput
} // namespace DistributedHardware
+6 -1
View File
@@ -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<std::string>& vecStr);
int OpenInputDeviceFdByPath(std::string devicePath);
std::string ConvertErrNo();
void ScanInputDevicesPath(std::string dirName, std::vector<std::string>& vecInputDevPath);
} // namespace DistributedInput
} // namespace DistributedHardware
} // namespace OHOS
#endif // OHOS_DISTRIBUTED_INPUT_UTILS_TOOL_H
+100 -1
View File
@@ -17,8 +17,12 @@
#include <cstdarg>
#include <cstdio>
#include <dirent.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <openssl/sha.h>
#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<char*>(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<std::string>& 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<std::string>& 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