• 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 #include <map>
18 #include <fcntl.h>
19 #include <securec.h>
20 #include <unistd.h>
21 #include <hdf_log.h>
22 #include <cerrno>
23 
24 #define HDF_LOG_TAG virtual_device
25 
26 namespace OHOS {
27 namespace ExternalDeviceManager {
28 namespace {
29 using namespace OHOS::HiviewDFX;
30 constexpr uint32_t MAX_NAME_LENGTH = 80;
31 
DoIoctl(int32_t fd,int32_t request,const uint32_t value)32 bool DoIoctl(int32_t fd, int32_t request, const uint32_t value)
33 {
34     int32_t rc = ioctl(fd, request, value);
35     if (rc < 0) {
36         HDF_LOGE("%{public}s Failed to ioctl", __func__);
37         return false;
38     }
39     return true;
40 }
41 } // namespace
42 
VirtualDevice(const Hid_Device & hidDevice,const Hid_EventProperties & hidEventProperties)43 VirtualDevice::VirtualDevice(const Hid_Device &hidDevice, const Hid_EventProperties &hidEventProperties)
44     : deviceName_(hidDevice.deviceName.c_str()),
45     busType_(hidDevice.bustype),
46     vendorId_(hidDevice.vendorId),
47     productId_(hidDevice.productId),
48     version_(hidDevice.version),
49     eventTypes_(std::vector<uint32_t>(hidEventProperties.hidEventTypes.begin(),
50         hidEventProperties.hidEventTypes.end())),
51     keys_(std::vector<uint32_t>(hidEventProperties.hidKeys.begin(), hidEventProperties.hidKeys.end())),
52     properties_(std::vector<uint32_t>(hidDevice.properties.begin(), hidDevice.properties.end())),
53     abs_(std::vector<uint32_t>(hidEventProperties.hidAbs.begin(), hidEventProperties.hidAbs.end())),
54     relBits_(std::vector<uint32_t>(hidEventProperties.hidRelBits.begin(), hidEventProperties.hidRelBits.end())),
55     miscellaneous_(std::vector<uint32_t>(hidEventProperties.hidMiscellaneous.begin(),
56         hidEventProperties.hidMiscellaneous.end()))
57 {
58     const int absLength = 64;
59     if (hidEventProperties.hidAbsMax.size() <= absLength) {
60         std::copy(hidEventProperties.hidAbsMax.begin(), hidEventProperties.hidAbsMax.end(), uinputDev_.absmax);
61     }
62     if (hidEventProperties.hidAbsMin.size() <= absLength) {
63         std::copy(hidEventProperties.hidAbsMin.begin(), hidEventProperties.hidAbsMin.end(), uinputDev_.absmin);
64     }
65     if (hidEventProperties.hidAbsFuzz.size() <= absLength) {
66         std::copy(hidEventProperties.hidAbsFuzz.begin(), hidEventProperties.hidAbsFuzz.end(), uinputDev_.absfuzz);
67     }
68     if (hidEventProperties.hidAbsFlat.size() <= absLength) {
69         std::copy(hidEventProperties.hidAbsFlat.begin(), hidEventProperties.hidAbsFlat.end(), uinputDev_.absflat);
70     }
71 }
72 
~VirtualDevice()73 VirtualDevice::~VirtualDevice()
74 {
75     if (fd_ >= 0) {
76         ioctl(fd_, UI_DEV_DESTROY);
77         close(fd_);
78         fd_ = -1;
79     }
80 }
81 
SetUp()82 bool VirtualDevice::SetUp()
83 {
84     fd_ = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
85     if (fd_ < 0) {
86         HDF_LOGE("%{public}s Failed to open uinput, errno=%{public}d", __func__, errno);
87         return false;
88     }
89 
90     if (!SetAttribute()) {
91         HDF_LOGE("%{public}s Failed to set attribute", __func__);
92         return false;
93     }
94 
95     if (!CreateKey()) {
96         HDF_LOGE("%{public}s Failed to create uinput KeyValue", __func__);
97         return false;
98     }
99 
100     errno_t ret = strncpy_s(uinputDev_.name, MAX_NAME_LENGTH, deviceName_, sizeof(uinputDev_.name));
101     if (ret != EOK) {
102         HDF_LOGE("%{public}s Failed to copy deviceName", __func__);
103         return false;
104     }
105     uinputDev_.id.bustype = busType_;
106     uinputDev_.id.vendor = vendorId_;
107     uinputDev_.id.product = productId_;
108     uinputDev_.id.version = version_;
109     if (write(fd_, &uinputDev_, sizeof(uinputDev_)) < 0) {
110         HDF_LOGE("%{public}s Unable to set input device info", __func__);
111         return false;
112     }
113     if (ioctl(fd_, UI_DEV_CREATE) < 0) {
114         HDF_LOGE("%{public}s Unable to create input device", __func__);
115         return false;
116     }
117     return true;
118 }
119 
SetAttribute()120 bool VirtualDevice::SetAttribute()
121 {
122     for (const auto &item : GetEventTypes()) {
123         if (!DoIoctl(fd_, UI_SET_EVBIT, item)) {
124             HDF_LOGE("%{public}s Error setting event type:%{public}u", __func__, item);
125             return false;
126         }
127     }
128     for (const auto &item : GetKeys()) {
129         if (!DoIoctl(fd_, UI_SET_KEYBIT, item)) {
130             HDF_LOGE("%{public}s Error setting key:%{public}u", __func__, item);
131             return false;
132         }
133     }
134     for (const auto &item : GetProperties()) {
135         if (!DoIoctl(fd_, UI_SET_PROPBIT, item)) {
136             HDF_LOGE("%{public}s Error setting property:%{public}u", __func__, item);
137             return false;
138         }
139     }
140     for (const auto &item : GetAbs()) {
141         if (!DoIoctl(fd_, UI_SET_ABSBIT, item)) {
142             HDF_LOGE("%{public}s Error setting abs:%{public}u", __func__, item);
143             return false;
144         }
145     }
146     for (const auto &item : GetRelBits()) {
147         if (!DoIoctl(fd_, UI_SET_RELBIT, item)) {
148             HDF_LOGE("%{public}s Error setting rel:%{public}u", __func__, item);
149             return false;
150         }
151     }
152 
153     return true;
154 }
155 
EmitEvent(uint16_t type,uint16_t code,uint32_t value) const156 bool VirtualDevice::EmitEvent(uint16_t type, uint16_t code, uint32_t value) const
157 {
158     struct input_event event {};
159     event.type = type;
160     event.code = code;
161     event.value = (int32_t)value;
162     HDF_LOGW("%{public}s type:%{public}d code:%{public}d value:%{public}d",
163         __func__, event.type, event.code, event.value);
164 #ifndef __MUSL__
165     gettimeofday(&event.time, nullptr);
166 #endif
167     if (write(fd_, &event, sizeof(event)) < static_cast<ssize_t>(sizeof(event))) {
168         HDF_LOGE("%{public}s Event write failed, fd:%{public}d, errno:%{public}d", __func__, fd_, errno);
169         return false;
170     }
171     return true;
172 }
173 
GetEventTypes() const174 const std::vector<uint32_t> &VirtualDevice::GetEventTypes() const
175 {
176     return eventTypes_;
177 }
178 
GetKeys() const179 const std::vector<uint32_t> &VirtualDevice::GetKeys() const
180 {
181     return keys_;
182 }
183 
GetProperties() const184 const std::vector<uint32_t> &VirtualDevice::GetProperties() const
185 {
186     return properties_;
187 }
188 
GetAbs() const189 const std::vector<uint32_t> &VirtualDevice::GetAbs() const
190 {
191     return abs_;
192 }
193 
GetRelBits() const194 const std::vector<uint32_t> &VirtualDevice::GetRelBits() const
195 {
196     return relBits_;
197 }
198 
GetLeds() const199 const std::vector<uint32_t> &VirtualDevice::GetLeds() const
200 {
201     return leds_;
202 }
203 
GetRepeats() const204 const std::vector<uint32_t> &VirtualDevice::GetRepeats() const
205 {
206     return repeats_;
207 }
208 
GetMiscellaneous() const209 const std::vector<uint32_t> &VirtualDevice::GetMiscellaneous() const
210 {
211     return miscellaneous_;
212 }
213 
GetSwitches() const214 const std::vector<uint32_t> &VirtualDevice::GetSwitches() const
215 {
216     return switches_;
217 }
218 
CreateKey()219 bool VirtualDevice::CreateKey()
220 {
221     auto fun = [&](int32_t uiSet, const std::vector<uint32_t> &list) -> bool {
222         for (const auto &item : list) {
223             if (ioctl(fd_, uiSet, item) < 0) {
224                 HDF_LOGE("%{public}s not setting event type: %{public}d, deviceName:%{public}s",
225                     __func__, item, deviceName_);
226                 return false;
227             }
228         }
229         return true;
230     };
231     std::map<int32_t, std::vector<uint32_t>> uinputTypes;
232     uinputTypes[UI_SET_EVBIT] = GetEventTypes();
233     uinputTypes[UI_SET_KEYBIT] = GetKeys();
234     uinputTypes[UI_SET_PROPBIT] = GetProperties();
235     uinputTypes[UI_SET_ABSBIT] = GetAbs();
236     uinputTypes[UI_SET_RELBIT] = GetRelBits();
237 
238     uinputTypes[UI_SET_MSCBIT] = GetMiscellaneous();
239     uinputTypes[UI_SET_LEDBIT] = GetLeds();
240     uinputTypes[UI_SET_SWBIT] = GetSwitches();
241     uinputTypes[UI_SET_FFBIT] = GetRepeats();
242 
243     for (const auto &item : uinputTypes) {
244         if (!fun(item.first, item.second)) {
245             return false;
246         }
247     }
248     return true;
249 }
250 } // namespace ExternalDeviceManager
251 } // namespace OHOS
252