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