• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 were 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(std::vector<std::string> & results)61 void VirtualDevice::Execute(std::vector<std::string> &results)
62 {
63     CALL_DEBUG_ENTER;
64     char buffer[DEFAULT_BUF_SIZE] {};
65     FILE *pin = popen("cat /proc/bus/input/devices", "r");
66     if (pin == nullptr) {
67         FI_HILOGE("Failed to popen command");
68         return;
69     }
70     while (!feof(pin)) {
71         if (fgets(buffer, sizeof(buffer), pin) != nullptr) {
72             results.push_back(buffer);
73         }
74     }
75     FI_HILOGD("Close phandle");
76     pclose(pin);
77 }
78 
GetInputDeviceNodes(std::map<std::string,std::string> & nodes)79 void VirtualDevice::GetInputDeviceNodes(std::map<std::string, std::string> &nodes)
80 {
81     CALL_DEBUG_ENTER;
82     std::vector<std::string> results;
83     Execute(results);
84     if (results.empty()) {
85         FI_HILOGE("Failed to list devices");
86         return;
87     }
88     const std::string kname { "Name=\"" };
89     const std::string kevent { "event" };
90     std::string name;
91     for (const auto &res : results) {
92         FI_HILOGD("res:%{public}s", res.c_str());
93         if (res[0] == 'N') {
94             std::string::size_type spos = res.find(kname);
95             if (spos != std::string::npos) {
96                 spos += kname.size();
97                 std::string::size_type tpos = res.find("\"", spos);
98                 if (tpos != std::string::npos) {
99                     name = res.substr(spos, tpos - spos);
100                 }
101             }
102         } else if (!name.empty() && (res[0] == 'H')) {
103             std::string::size_type spos = res.find(kevent);
104             if (spos != std::string::npos) {
105                 std::map<std::string, std::string>::const_iterator cItr = nodes.find(name);
106                 if (cItr != nodes.end()) {
107                     nodes.erase(cItr);
108                 }
109                 std::string::size_type tpos = spos + kevent.size();
110                 while (std::isalnum(res[tpos])) {
111                     ++tpos;
112                 }
113                 auto [_, ret] = nodes.emplace(name, res.substr(spos, tpos - spos));
114                 if (!ret) {
115                     FI_HILOGW("name is duplicated");
116                 }
117                 name.clear();
118             }
119         }
120     }
121 }
122 
SendEvent(uint16_t type,uint16_t code,int32_t value)123 int32_t VirtualDevice::SendEvent(uint16_t type, uint16_t code, int32_t value)
124 {
125     CALL_DEBUG_ENTER;
126     CHKPR(inputDev_, RET_ERR);
127     inputDev_->SendEvent(type, code, value);
128 
129     if ((type == EV_SYN) && (code == SYN_REPORT)) {
130         std::this_thread::sleep_for(std::chrono::milliseconds(minimumInterval_));
131     }
132     return RET_OK;
133 }
134 
GetName() const135 std::string VirtualDevice::GetName() const
136 {
137     if (!name_.empty()) {
138         return name_;
139     }
140     if (inputDev_ != nullptr) {
141         return inputDev_->GetName();
142     }
143     return {};
144 }
145 
SetMinimumInterval(int32_t interval)146 void VirtualDevice::SetMinimumInterval(int32_t interval)
147 {
148     minimumInterval_ = std::max(MINIMUM_INTERVAL_ALLOWED, std::min(interval, MAXIMUM_INTERVAL_ALLOWED));
149 }
150 } // namespace DeviceStatus
151 } // namespace Msdp
152 } // namespace OHOS