• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 namespace OHOS {
22 namespace MMI {
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "ManageInjectDevice" };
25 constexpr int64_t INJECT_SLEEP_TIMES = 10;
26 } // namespace
27 
TransformJsonData(const DeviceItems & configData)28 int32_t ManageInjectDevice::TransformJsonData(const DeviceItems &configData)
29 {
30     CALL_DEBUG_ENTER;
31     if (configData.empty()) {
32         MMI_HILOGE("Input data from json file is empty");
33         return RET_ERR;
34     }
35     for (const auto &item : configData) {
36         std::string deviceName = item.deviceName;
37         uint16_t devIndex = item.deviceIndex;
38         std::string deviceNode;
39         if (getDeviceNodeObject_.GetDeviceNodeName(deviceName, devIndex, deviceNode) == RET_ERR) {
40             MMI_HILOGE("Failed to get device:%{public}s node", deviceName.c_str());
41             return RET_ERR;
42         }
43         InputEventArray inputEventArray = {};
44         inputEventArray.deviceName = deviceName;
45         inputEventArray.target = deviceNode;
46         auto devicePtr = GetDeviceObject::CreateDeviceObject(deviceName);
47         CHKPR(devicePtr, RET_ERR);
48         int32_t ret = devicePtr->TransformJsonDataToInputData(item, inputEventArray);
49         if (devicePtr != nullptr) {
50             delete devicePtr;
51             devicePtr = nullptr;
52         }
53         if (ret == RET_ERR) {
54             MMI_HILOGE("Failed to read json file");
55             return ret;
56         }
57         ret = SendEvent(inputEventArray);
58         if (ret == RET_ERR) {
59             MMI_HILOGE("Failed to send event");
60             return ret;
61         }
62     }
63     return RET_OK;
64 }
65 
SendEvent(const InputEventArray & inputEventArray)66 int32_t ManageInjectDevice::SendEvent(const InputEventArray &inputEventArray)
67 {
68     return SendEventToDeviceNode(inputEventArray);
69 }
70 
SendEventToDeviceNode(const InputEventArray & inputEventArray)71 int32_t ManageInjectDevice::SendEventToDeviceNode(const InputEventArray &inputEventArray)
72 {
73     CALL_DEBUG_ENTER;
74     std::string deviceNode = inputEventArray.target;
75     if (deviceNode.empty()) {
76         MMI_HILOGE("Device node:%{public}s is not exit", deviceNode.c_str());
77         return RET_ERR;
78     }
79     char realPath[PATH_MAX] = {};
80     if (realpath(deviceNode.c_str(), realPath) == nullptr) {
81         MMI_HILOGE("Path is error, path:%{public}s", deviceNode.c_str());
82         return RET_ERR;
83     }
84     int32_t fd = open(realPath, O_RDWR);
85     if (fd < 0) {
86         MMI_HILOGE("Open device node:%{public}s failed", deviceNode.c_str());
87         return RET_ERR;
88     }
89     for (const auto &item : inputEventArray.events) {
90         write(fd, &item.event, sizeof(item.event));
91         int64_t blockTime = (item.blockTime == 0) ? INJECT_SLEEP_TIMES : item.blockTime;
92         std::this_thread::sleep_for(std::chrono::milliseconds(blockTime));
93     }
94     if (fd >= 0) {
95         close(fd);
96         fd = -1;
97     }
98     return RET_OK;
99 }
100 } // namespace MMI
101 } // namespace OHOS