set events that has relationship in a batch

Signed-off-by: hwzhangchuang <zhangchuang.zhang@huawei.com>
This commit is contained in:
hwzhangchuang
2023-11-29 16:51:02 +08:00
parent 7860f92084
commit 3b1a8f2aa8
16 changed files with 138 additions and 27 deletions
-1
View File
@@ -48,7 +48,6 @@ namespace DistributedInput {
const uint32_t SCREEN_MSG_MAX = 40 * 1024 * 1024;
const uint32_t AUTH_SESSION_SIDE_SERVER = 0;
const uint32_t IPC_VECTOR_MAX_SIZE = 32;
const uint32_t SIM_TOUCH_TRACKING_ID = 0x0001;
/*
* Device Type definitions
@@ -88,6 +88,7 @@ namespace DistributedInput {
const uint32_t TRANS_SINK_MSG_ON_RELAY_STOPDHID = 25;
const uint32_t TRANS_SINK_MSG_ON_RELAY_STARTTYPE = 26;
const uint32_t TRANS_SINK_MSG_ON_RELAY_STOPTYPE = 27;
const uint32_t TRANS_SINK_MSG_KEY_STATE_BATCH = 28;
// src or sink
const uint32_t TRANS_MSG_SRC_SINK_SPLIT = 30;
@@ -33,6 +33,7 @@ public:
virtual void OnResponseStopRemoteInputDhid(const std::string deviceId, const std::string &dhids, bool result) = 0;
virtual void OnResponseKeyState(const std::string deviceId, const std::string &dhid, const uint32_t type,
const uint32_t code, const uint32_t value) = 0;
virtual void OnResponseKeyStateBatch(const std::string deviceId, const std::string &object) = 0;
virtual void OnReceivedEventRemoteInput(const std::string deviceId, const std::string &object) = 0;
virtual void OnResponseRelayPrepareRemoteInput(int32_t sessionId, const std::string &deviceId, bool result,
@@ -60,6 +60,7 @@ public:
int32_t RespLatency(const int32_t sessionId, std::string &smsg);
void SendKeyStateNodeMsg(const int32_t sessionId, const std::string &dhId, uint32_t type, const uint32_t btnCode,
int32_t value);
void SendKeyStateNodeMsgBatch(const int32_t sessionId, const std::vector<struct RawEvent> &events);
class DInputSinkEventHandler : public AppExecFwk::EventHandler {
public:
@@ -93,6 +94,7 @@ private:
void NotifyRelayStopTypeRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
void RecordEventLog(const std::string &dhId, int32_t type, int32_t code, int32_t value);
void RecordEventLog(const std::vector<struct RawEvent> &events);
private:
std::string mySessionName_;
std::shared_ptr<DistributedInputSinkTransport::DInputSinkEventHandler> eventHandler_;
@@ -225,6 +225,46 @@ void DistributedInputSinkTransport::SendKeyStateNodeMsg(const int32_t sessionId,
RecordEventLog(dhId, type, btnCode, value);
}
void DistributedInputSinkTransport::SendKeyStateNodeMsgBatch(const int32_t sessionId,
const std::vector<struct RawEvent> &events)
{
if (sessionId <= 0) {
DHLOGE("SendKeyStateNodeMsgBatch error, sessionId <= 0.");
return;
}
DHLOGI("SendKeyStateNodeMsgBatch sessionId: %d, event size: %d ", sessionId, events.size());
int64_t currentTimeNs = GetCurrentTime() * 1000LL;
std::shared_ptr<nlohmann::json> eventsJsonArr = std::make_shared<nlohmann::json>();
for (const auto &ev : events) {
nlohmann::json tmpJson;
tmpJson[INPUT_KEY_WHEN] = currentTimeNs;
tmpJson[INPUT_KEY_TYPE] = ev.type;
tmpJson[INPUT_KEY_CODE] = ev.code;
tmpJson[INPUT_KEY_VALUE] = ev.value;
tmpJson[INPUT_KEY_DESCRIPTOR] = ev.descriptor;
tmpJson[INPUT_KEY_PATH] = ev.path;
eventsJsonArr->push_back(tmpJson);
}
nlohmann::json jsonStr;
jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_KEY_STATE_BATCH;
jsonStr[DINPUT_SOFTBUS_KEY_INPUT_DATA] = eventsJsonArr->dump();
std::string msg = jsonStr.dump();
int32_t ret = SendMessage(sessionId, msg);
if (ret != DH_SUCCESS) {
DHLOGE("SendKeyStateNodeMsgBatch error, SendMessage fail.");
}
RecordEventLog(events);
}
void DistributedInputSinkTransport::RecordEventLog(const std::vector<struct RawEvent> &events)
{
for (auto &ev : events) {
RecordEventLog(ev.descriptor, ev.type, ev.code, ev.value);
}
}
void DistributedInputSinkTransport::RecordEventLog(const std::string &dhId, int32_t type, int32_t code, int32_t value)
{
std::string eventType;
@@ -35,6 +35,7 @@ public:
const std::string &parameters);
int32_t UnregisterDistributedHardware(const std::string &devId, const std::string &dhId);
int32_t RegisterDistributedEvent(RawEvent *buffer, size_t bufferSize);
int32_t RegisterDistributedEvent(const std::vector<RawEvent> &events);
int32_t StructTransJson(const InputDevice &pBuf, std::string &strDescriptor);
void StartInjectThread();
void StopInjectThread();
@@ -47,6 +47,7 @@ public:
int32_t GetDevice(const std::string &dhId, VirtualDevice *&device);
void ReportEvent(const RawEvent rawEvent);
void ReportEvent(const std::vector<RawEvent> &events);
int32_t CloseDeviceLocked(const std::string &dhId);
void StartInjectThread();
void StopInjectThread();
@@ -147,13 +147,25 @@ int32_t DistributedInputInject::RegisterDistributedEvent(RawEvent *buffer, size_
DHLOGE("the DistributedInputNodeManager is null");
return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL;
}
DHLOGI("RegisterDistributedEvent start %zu\n", bufferSize);
for (size_t i = 0; i < bufferSize; i++) {
inputNodeManager_->ReportEvent(buffer[i]);
}
return DH_SUCCESS;
}
int32_t DistributedInputInject::RegisterDistributedEvent(const std::vector<RawEvent> &events)
{
std::lock_guard<std::mutex> lock(inputNodeManagerMutex_);
if (inputNodeManager_ == nullptr) {
DHLOGE("the DistributedInputNodeManager is null");
return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL;
}
inputNodeManager_->ReportEvent(events);
return DH_SUCCESS;
}
void DistributedInputInject::StartInjectThread()
{
std::lock_guard<std::mutex> lock(inputNodeManagerMutex_);
@@ -427,6 +427,15 @@ void DistributedInputNodeManager::ReportEvent(const RawEvent rawEvent)
conditionVariable_.notify_all();
}
void DistributedInputNodeManager::ReportEvent(const std::vector<RawEvent> &events)
{
std::lock_guard<std::mutex> lockGuard(injectThreadMutex_);
for (auto ev : events) {
injectQueue_.push(std::make_shared<RawEvent>(ev));
}
conditionVariable_.notify_all();
}
void DistributedInputNodeManager::InjectEvent()
{
int32_t ret = pthread_setname_np(pthread_self(), EVENT_INJECT_THREAD_NAME);
@@ -52,6 +52,7 @@ public:
void OnResponseStopRemoteInputDhid(const std::string deviceId, const std::string &dhids, bool result) override;
void OnResponseKeyState(const std::string deviceId, const std::string &dhid, const uint32_t type,
const uint32_t code, const uint32_t value) override;
void OnResponseKeyStateBatch(const std::string deviceId, const std::string &event) override;
void OnReceivedEventRemoteInput(const std::string deviceId, const std::string &event) override;
void OnResponseRelayPrepareRemoteInput(int32_t sessionId, const std::string &deviceId, bool result,
const std::string &object) override;
@@ -325,24 +325,31 @@ void DInputSourceListener::OnResponseKeyState(const std::string deviceId,
sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
}
void DInputSourceListener::OnReceivedEventRemoteInput(
const std::string deviceId, const std::string &event)
void DInputSourceListener::OnResponseKeyStateBatch(const std::string deviceId, const std::string &event)
{
DHLOGI("OnResponseKeyStateBatch events, deviceId: %s.", GetAnonyString(deviceId).c_str());
OnReceivedEventRemoteInput(deviceId, event);
}
void DInputSourceListener::OnReceivedEventRemoteInput(const std::string deviceId, const std::string &event)
{
nlohmann::json inputData = nlohmann::json::parse(event, nullptr, false);
if (inputData.is_discarded()) {
DHLOGE("inputData parse failed!");
return;
}
size_t jsonSize = inputData.size();
DHLOGI("OnReceivedEventRemoteInput called, deviceId: %s, json size:%d.",
GetAnonyString(deviceId).c_str(), jsonSize);
if (!inputData.is_array()) {
DHLOGE("inputData not vector!");
return;
}
RawEvent mEventBuffer[jsonSize];
size_t jsonSize = inputData.size();
DHLOGI("OnReceivedEventRemoteInput called, deviceId: %s, json size:%d.",
GetAnonyString(deviceId).c_str(), jsonSize);
std::vector<RawEvent> mEventBuffer(jsonSize);
int idx = 0;
for (auto it = inputData.begin(); it != inputData.end(); ++it) {
nlohmann::json oneData = (*it);
@@ -362,7 +369,8 @@ void DInputSourceListener::OnReceivedEventRemoteInput(
oneData[INPUT_KEY_VALUE], oneData[INPUT_KEY_PATH]);
++idx;
}
DistributedInputInject::GetInstance().RegisterDistributedEvent(mEventBuffer, jsonSize);
DistributedInputInject::GetInstance().RegisterDistributedEvent(mEventBuffer);
}
void DInputSourceListener::OnReceiveRelayPrepareResult(int32_t status,
@@ -99,6 +99,7 @@ private:
void NotifyResponseStartRemoteInputDhid(int32_t sessionId, const nlohmann::json &recMsg);
void NotifyResponseStopRemoteInputDhid(int32_t sessionId, const nlohmann::json &recMsg);
void NotifyResponseKeyState(int32_t sessionId, const nlohmann::json &recMsg);
void NotifyResponseKeyStateBatch(int32_t sessionId, const nlohmann::json &recMsg);
void NotifyReceivedEventRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
void ReceiveSrcTSrcRelayPrepare(int32_t sessionId, const nlohmann::json &recMsg);
void ReceiveSrcTSrcRelayUnprepare(int32_t sessionId, const nlohmann::json &recMsg);
@@ -79,6 +79,7 @@ void DistributedInputSourceTransport::RegRespFunMap()
memberFuncMap_[TRANS_SINK_MSG_DHID_ONSTART] = &DistributedInputSourceTransport::NotifyResponseStartRemoteInputDhid;
memberFuncMap_[TRANS_SINK_MSG_DHID_ONSTOP] = &DistributedInputSourceTransport::NotifyResponseStopRemoteInputDhid;
memberFuncMap_[TRANS_SINK_MSG_KEY_STATE] = &DistributedInputSourceTransport::NotifyResponseKeyState;
memberFuncMap_[TRANS_SINK_MSG_KEY_STATE_BATCH] = &DistributedInputSourceTransport::NotifyResponseKeyStateBatch;
memberFuncMap_[TRANS_SOURCE_TO_SOURCE_MSG_PREPARE] = &DistributedInputSourceTransport::ReceiveSrcTSrcRelayPrepare;
memberFuncMap_[TRANS_SINK_MSG_ON_RELAY_PREPARE] =
&DistributedInputSourceTransport::NotifyResponseRelayPrepareRemoteInput;
@@ -1047,6 +1048,22 @@ void DistributedInputSourceTransport::NotifyResponseKeyState(int32_t sessionId,
recMsg[DINPUT_SOFTBUS_KEY_KEYSTATE_VALUE]);
}
void DistributedInputSourceTransport::NotifyResponseKeyStateBatch(int32_t sessionId, const nlohmann::json &recMsg)
{
DHLOGI("OnBytesReceived cmdType is TRANS_SINK_MSG_KEY_STATE_BATCH.");
if (!IsString(recMsg, DINPUT_SOFTBUS_KEY_INPUT_DATA)) {
DHLOGE("The DINPUT_SOFTBUS_KEY_INPUT_DATA is invalid");
return;
}
std::string deviceId = DistributedInputTransportBase::GetInstance().GetDevIdBySessionId(sessionId);
if (deviceId.empty()) {
DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_KEY_STATE_BATCH, deviceId is error.");
return;
}
std::string inputDataStr = recMsg[DINPUT_SOFTBUS_KEY_INPUT_DATA];
callback_->OnResponseKeyStateBatch(deviceId, inputDataStr);
}
void DistributedInputSourceTransport::NotifyReceivedEventRemoteInput(int32_t sessionId, const nlohmann::json &recMsg)
{
DHLOGI("OnBytesReceived cmdType is TRANS_SINK_MSG_BODY_DATA.");
+24 -18
View File
@@ -130,35 +130,41 @@ void DInputState::SimulateEventInjectToSrc(const int32_t sessionId, const std::v
void DInputState::SimulateBtnTouchEvent(const int32_t sessionId, const std::string &dhId, const struct RawEvent &event)
{
DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId,
EV_ABS, ABS_MT_TRACKING_ID, SIM_TOUCH_TRACKING_ID);
int32_t simTrackingId = GetRandomInt32();
std::vector<struct RawEvent> simEvents;
RawEvent touchTrackingIdEv = { event.when, EV_ABS, ABS_MT_TRACKING_ID, simTrackingId, dhId, event.path };
simEvents.push_back(touchTrackingIdEv);
std::pair<int32_t, int32_t> absPos = GetAndClearABSPosition(event.descriptor);
if (absPos.first != -1) {
DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId,
EV_ABS, ABS_MT_POSITION_X, absPos.first);
RawEvent absMTXEv = { event.when, EV_ABS, ABS_MT_POSITION_X, absPos.first, dhId, event.path };
simEvents.push_back(absMTXEv);
}
if (absPos.second != -1) {
DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId,
EV_ABS, ABS_MT_POSITION_Y, absPos.second);
RawEvent absMTYEv = { event.when, EV_ABS, ABS_MT_POSITION_Y, absPos.second, dhId, event.path };
simEvents.push_back(absMTYEv);
}
DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId,
EV_KEY, event.code, KEY_DOWN_STATE);
DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId,
EV_KEY, BTN_TOOL_FINGER, KEY_DOWN_STATE);
RawEvent keyDownEv = { event.when, EV_KEY, event.code, KEY_DOWN_STATE, dhId, event.path };
simEvents.push_back(keyDownEv);
RawEvent fingerEv = { event.when, EV_KEY, BTN_TOOL_FINGER, KEY_DOWN_STATE, dhId, event.path };
simEvents.push_back(fingerEv);
if (absPos.first != -1) {
DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId,
EV_ABS, ABS_X, absPos.first);
RawEvent absXEv = { event.when, EV_ABS, ABS_X, absPos.first, dhId, event.path };
simEvents.push_back(absXEv);
}
if (absPos.second != -1) {
DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId,
EV_ABS, ABS_Y, absPos.second);
RawEvent absYEv = { event.when, EV_ABS, ABS_Y, absPos.second, dhId, event.path };
simEvents.push_back(absYEv);
}
DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId,
EV_MSC, MSC_TIMESTAMP, 0x0);
DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId,
EV_SYN, SYN_REPORT, 0x0);
RawEvent mscEv = { event.when, EV_MSC, MSC_TIMESTAMP, 0x0, dhId, event.path };
simEvents.push_back(mscEv);
RawEvent sycReportEv = { event.when, EV_SYN, SYN_REPORT, 0x0, dhId, event.path };
simEvents.push_back(sycReportEv);
DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsgBatch(sessionId, simEvents);
}
void DInputState::SimulateNormalEvent(const int32_t sessionId, const std::string &dhId, const struct RawEvent &event)
+1
View File
@@ -65,6 +65,7 @@ void ScanInputDevicesPath(const std::string &dirName, std::vector<std::string> &
void ResetVirtualDevicePressedKeys(const std::vector<std::string> &nodePaths);
std::string GetString(const std::vector<std::string> &vec);
int32_t GetRandomInt32();
} // namespace DistributedInput
} // namespace DistributedHardware
} // namespace OHOS
+11
View File
@@ -17,8 +17,11 @@
#include <cstdarg>
#include <cstdio>
#include <random>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -439,6 +442,14 @@ std::string GetString(const std::vector<std::string> &vec)
retStr += "]";
return retStr;
}
int32_t GetRandomInt32()
{
std::default_random_engine engine(time(nullptr));
std::uniform_int_distribution<int> distribution(0, INT32_MAX);
return distribution(engine);
}
} // namespace DistributedInput
} // namespace DistributedHardware
} // namespace OHOS