1 /*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "manage_inject_device.h"
17
18 #include <chrono>
19 #include <thread>
20
21 #undef MMI_LOG_TAG
22 #define MMI_LOG_TAG "ManageInjectDevice"
23
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 constexpr int64_t INJECT_SLEEP_TIMES { 10 };
28 } // namespace
29
TransformJsonData(const DeviceItems & configData)30 int32_t ManageInjectDevice::TransformJsonData(const DeviceItems &configData)
31 {
32 CALL_DEBUG_ENTER;
33 if (configData.empty()) {
34 MMI_HILOGE("Input data from json file is empty");
35 return RET_ERR;
36 }
37 for (const auto &item : configData) {
38 std::string deviceName = item.deviceName;
39 uint16_t devIndex = item.deviceIndex;
40 std::string deviceNode;
41 if (getDeviceNodeObject_.GetDeviceNodeName(deviceName, devIndex, deviceNode) == RET_ERR) {
42 MMI_HILOGE("Failed to get device:%{public}s node", deviceName.c_str());
43 return RET_ERR;
44 }
45 InputEventArray inputEventArray = {};
46 inputEventArray.deviceName = deviceName;
47 inputEventArray.target = deviceNode;
48 auto devicePtr = GetDeviceObject::CreateDeviceObject(deviceName);
49 CHKPR(devicePtr, RET_ERR);
50 int32_t ret = devicePtr->TransformJsonDataToInputData(item, inputEventArray);
51 if (devicePtr != nullptr) {
52 delete devicePtr;
53 devicePtr = nullptr;
54 }
55 if (ret == RET_ERR) {
56 MMI_HILOGE("Failed to read json file");
57 return ret;
58 }
59 ret = SendEvent(inputEventArray);
60 if (ret == RET_ERR) {
61 MMI_HILOGE("Failed to send event");
62 return ret;
63 }
64 }
65 return RET_OK;
66 }
67
SendEvent(const InputEventArray & inputEventArray)68 int32_t ManageInjectDevice::SendEvent(const InputEventArray &inputEventArray)
69 {
70 return SendEventToDeviceNode(inputEventArray);
71 }
72
SendEventToDeviceNode(const InputEventArray & inputEventArray)73 int32_t ManageInjectDevice::SendEventToDeviceNode(const InputEventArray &inputEventArray)
74 {
75 CALL_DEBUG_ENTER;
76 std::string deviceNode = inputEventArray.target;
77 if (deviceNode.empty()) {
78 MMI_HILOGE("Device node:%{public}s is not exit", deviceNode.c_str());
79 return RET_ERR;
80 }
81 char realPath[PATH_MAX] = {};
82 if (realpath(deviceNode.c_str(), realPath) == nullptr) {
83 MMI_HILOGE("Path is error, path:%{private}s", deviceNode.c_str());
84 return RET_ERR;
85 }
86 int32_t fd = open(realPath, O_RDWR);
87 if (fd < 0) {
88 MMI_HILOGE("Open device node:%{public}s failed", deviceNode.c_str());
89 return RET_ERR;
90 }
91 for (const auto &item : inputEventArray.events) {
92 if (write(fd, &item.event, sizeof(item.event)) < 0) {
93 MMI_HILOGE("Write event failed");
94 close(fd);
95 fd = -1;
96 return RET_ERR;
97 };
98 int64_t blockTime = (item.blockTime == 0) ? INJECT_SLEEP_TIMES : item.blockTime;
99 std::this_thread::sleep_for(std::chrono::milliseconds(blockTime));
100 }
101 if (fd >= 0) {
102 close(fd);
103 fd = -1;
104 }
105 return RET_OK;
106 }
107 } // namespace MMI
108 } // namespace OHOS