• 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 #include <chrono>
18 #include <thread>
19 
20 using namespace OHOS::MMI;
21 
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, MMI_LOG_DOMAIN, "ManageInjectDevice" };
24 } // namespace
25 
TransformJsonData(const Json & configData)26 int32_t ManageInjectDevice::TransformJsonData(const Json& configData)
27 {
28     MMI_LOGD("Enter");
29     if (configData.empty()) {
30         MMI_LOGE("input data from json file is empty");
31         return RET_ERR;
32     }
33     int32_t ret = RET_ERR;
34     std::string deviceName;
35     std::string sendType;
36     std::string deviceNode;
37     GetDeviceObject getDeviceObject;
38     for (const auto &item : configData) {
39         deviceName = item.at("deviceName").get<std::string>();
40         InputEventArray inputEventArray = {};
41         inputEventArray.deviceName = deviceName;
42 
43         uint16_t devIndex = 0;
44         if (item.find("devIndex") != item.end()) {
45             devIndex = item.at("devIndex").get<uint16_t>();
46         }
47         if (getDeviceNodeObject_.GetDeviceNodeName(deviceName, deviceNode, devIndex) == RET_ERR) {
48             return RET_ERR;
49         }
50         inputEventArray.target = deviceNode;
51 
52         devicePtr_ = getDeviceObject.CreateDeviceObject(deviceName);
53         if (devicePtr_ == nullptr) {
54             return RET_ERR;
55         }
56         ret = devicePtr_->TransformJsonDataToInputData(item, inputEventArray);
57         if (ret != RET_ERR) {
58             ret = SendEvent(inputEventArray);
59         }
60     }
61     if (devicePtr_ != nullptr) {
62         delete devicePtr_;
63         devicePtr_ = nullptr;
64     }
65     MMI_LOGD("Leave");
66 
67     return ret;
68 }
69 
SendEvent(const InputEventArray & inputEventArray)70 int32_t ManageInjectDevice::SendEvent(const InputEventArray& inputEventArray)
71 {
72     return SendEventToDeviveNode(inputEventArray);
73 }
74 
SendEventToDeviveNode(const InputEventArray & inputEventArray)75 int32_t ManageInjectDevice::SendEventToDeviveNode(const InputEventArray& inputEventArray)
76 {
77     MMI_LOGD("Enter");
78     std::string deviceNode = inputEventArray.target;
79     if (deviceNode.empty()) {
80         MMI_LOGE("device node:%{public}s is not exit", deviceNode.c_str());
81         return RET_ERR;
82     }
83     char realPath[PATH_MAX] = {};
84     if (realpath(deviceNode.c_str(), realPath) == nullptr) {
85         MMI_LOGE("path is error, path:%{public}s", deviceNode.c_str());
86         return RET_ERR;
87     }
88     int32_t fd = open(realPath, O_RDWR);
89     if (fd < 0) {
90         MMI_LOGE("open device node:%{public}s faild", deviceNode.c_str());
91         return RET_ERR;
92     }
93     for (const auto &item : inputEventArray.events) {
94         write(fd, &item.event, sizeof(item.event));
95         int32_t blockTime = (item.blockTime == 0) ? INJECT_SLEEP_TIMES : item.blockTime;
96         std::this_thread::sleep_for(std::chrono::milliseconds(blockTime));
97     }
98     if (fd >= 0) {
99         close(fd);
100     }
101     MMI_LOGD("Leave");
102     return RET_OK;
103 }