1 /*
2 * Copyright (c) 2023 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 "virtual_device.h"
17
18 #include <iostream>
19 #include <map>
20
21 #include <unistd.h>
22
23 #include "devicestatus_define.h"
24 #include "fi_log.h"
25
26 namespace OHOS {
27 namespace Msdp {
28 namespace DeviceStatus {
29 namespace {
30 constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "VirtualDevice" };
31 constexpr size_t DEFAULT_BUF_SIZE { 1024 };
32 constexpr int32_t MINIMUM_INTERVAL_ALLOWED { 1 };
33 constexpr int32_t MAXIMUM_INTERVAL_ALLOWED { 100 };
34 } // namespace
35
VirtualDevice(const std::string & node)36 VirtualDevice::VirtualDevice(const std::string &node)
37 {
38 inputDev_ = std::make_unique<VInputDevice>(node);
39 inputDev_->Open();
40 }
41
FindDeviceNode(const std::string & name,std::string & node)42 bool VirtualDevice::FindDeviceNode(const std::string &name, std::string &node)
43 {
44 CALL_DEBUG_ENTER;
45 std::map<std::string, std::string> nodes;
46 GetInputDeviceNodes(nodes);
47 FI_HILOGD("There are %{public}zu device nodes.", nodes.size());
48
49 std::map<std::string, std::string>::const_iterator cItr = nodes.find(name);
50 if (cItr == nodes.cend()) {
51 FI_HILOGE("No virtual stylus is found.");
52 return false;
53 }
54 FI_HILOGD("Node name : \'%{public}s\'.", cItr->second.c_str());
55 std::ostringstream ss;
56 ss << "/dev/input/" << cItr->second;
57 node = ss.str();
58 return true;
59 }
60
Execute(const std::string & command,std::vector<std::string> & results)61 void VirtualDevice::Execute(const std::string &command, std::vector<std::string> &results)
62 {
63 CALL_DEBUG_ENTER;
64 if (command.empty()) {
65 FI_HILOGE("Variable command is empty");
66 return;
67 }
68 FI_HILOGD("Execute command:%{public}s.", command.c_str());
69 char buffer[DEFAULT_BUF_SIZE] {};
70 FILE *pin = popen(command.c_str(), "r");
71 if (pin == nullptr) {
72 FI_HILOGE("Failed to popen command.");
73 return;
74 }
75 while (!feof(pin)) {
76 if (fgets(buffer, sizeof(buffer), pin) != nullptr) {
77 results.push_back(buffer);
78 }
79 }
80 FI_HILOGD("Close phandle.");
81 pclose(pin);
82 }
83
GetInputDeviceNodes(std::map<std::string,std::string> & nodes)84 void VirtualDevice::GetInputDeviceNodes(std::map<std::string, std::string> &nodes)
85 {
86 CALL_DEBUG_ENTER;
87 std::string command = "cat /proc/bus/input/devices";
88 std::vector<std::string> results;
89 Execute(command, results);
90 if (results.empty()) {
91 FI_HILOGE("Failed to list devices.");
92 return;
93 }
94 const std::string kname { "Name=\"" };
95 const std::string kevent { "event" };
96 std::string name;
97 for (const auto &item : results) {
98 FI_HILOGD("item:%{public}s.", item.c_str());
99 if (item[0] == 'N') {
100 std::string::size_type spos = item.find(kname);
101 if (spos != std::string::npos) {
102 spos += kname.size();
103 std::string::size_type tpos = item.find("\"", spos);
104 if (tpos != std::string::npos) {
105 name = item.substr(spos, tpos - spos);
106 }
107 }
108 } else if (!name.empty() && (item[0] == 'H')) {
109 std::string::size_type spos = item.find(kevent);
110 if (spos != std::string::npos) {
111 std::map<std::string, std::string>::const_iterator cItr = nodes.find(name);
112 if (cItr != nodes.end()) {
113 nodes.erase(cItr);
114 }
115 std::string::size_type tpos = spos + kevent.size();
116 while (std::isalnum(item[tpos])) {
117 ++tpos;
118 }
119 auto [_, ret] = nodes.emplace(name, item.substr(spos, tpos - spos));
120 if (!ret) {
121 FI_HILOGW("name is duplicated");
122 }
123 name.clear();
124 }
125 }
126 }
127 }
128
SendEvent(uint16_t type,uint16_t code,int32_t value)129 int32_t VirtualDevice::SendEvent(uint16_t type, uint16_t code, int32_t value)
130 {
131 CALL_DEBUG_ENTER;
132 CHKPR(inputDev_, RET_ERR);
133 inputDev_->SendEvent(type, code, value);
134
135 if ((type == EV_SYN) && (code == SYN_REPORT)) {
136 std::this_thread::sleep_for(std::chrono::milliseconds(minimumInterval_));
137 }
138 return RET_OK;
139 }
140
GetName() const141 std::string VirtualDevice::GetName() const
142 {
143 if (!name_.empty()) {
144 return name_;
145 }
146 if (inputDev_ != nullptr) {
147 return inputDev_->GetName();
148 }
149 return {};
150 }
151
SetMinimumInterval(int32_t interval)152 void VirtualDevice::SetMinimumInterval(int32_t interval)
153 {
154 minimumInterval_ = std::max(MINIMUM_INTERVAL_ALLOWED, std::min(interval, MAXIMUM_INTERVAL_ALLOWED));
155 }
156 } // namespace DeviceStatus
157 } // namespace Msdp
158 } // namespace OHOS