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 <cerrno>
23 #include "input_uhdf_log.h"
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 fd_ = -1;
79 }
80 if (file_ != nullptr) {
81 fclose(file_);
82 file_ = nullptr;
83 }
84 }
85
SetUp()86 bool VirtualDevice::SetUp()
87 {
88 file_ = fopen("/dev/uinput", "wb");
89 if (file_ == nullptr) {
90 HDF_LOGE("%{public}s Failed to open uinput, errno=%{public}d", __func__, errno);
91 return false;
92 }
93 fd_ = fileno(file_);
94
95 if (!SetAttribute()) {
96 HDF_LOGE("%{public}s Failed to set attribute", __func__);
97 return false;
98 }
99
100 if (!CreateKey()) {
101 HDF_LOGE("%{public}s Failed to create uinput KeyValue", __func__);
102 return false;
103 }
104
105 if (strlen(deviceName_) == 0 || strlen(deviceName_) > MAX_NAME_LENGTH - 1) {
106 HDF_LOGE("%{public}s Length of deviceName_ is out of range", __func__);
107 return false;
108 }
109 errno_t ret = strncpy_s(uinputDev_.name, MAX_NAME_LENGTH, deviceName_, MAX_NAME_LENGTH - 1);
110 if (ret != EOK) {
111 HDF_LOGE("%{public}s Failed to copy deviceName", __func__);
112 return false;
113 }
114 uinputDev_.id.bustype = busType_;
115 uinputDev_.id.vendor = vendorId_;
116 uinputDev_.id.product = productId_;
117 uinputDev_.id.version = version_;
118 if (write(fd_, &uinputDev_, sizeof(uinputDev_)) < 0) {
119 HDF_LOGE("%{public}s Unable to set input device info", __func__);
120 return false;
121 }
122 if (ioctl(fd_, UI_DEV_CREATE) < 0) {
123 HDF_LOGE("%{public}s Unable to create input device", __func__);
124 return false;
125 }
126 return true;
127 }
128
SetAttribute()129 bool VirtualDevice::SetAttribute()
130 {
131 for (const auto &item : GetEventTypes()) {
132 if (!DoIoctl(fd_, UI_SET_EVBIT, item)) {
133 HDF_LOGE("%{public}s Error setting event type:%{public}u", __func__, item);
134 return false;
135 }
136 }
137 for (const auto &item : GetKeys()) {
138 if (!DoIoctl(fd_, UI_SET_KEYBIT, item)) {
139 HDF_LOGE("%{public}s Error setting key:%{public}u", __func__, item);
140 return false;
141 }
142 }
143 for (const auto &item : GetProperties()) {
144 if (!DoIoctl(fd_, UI_SET_PROPBIT, item)) {
145 HDF_LOGE("%{public}s Error setting property:%{public}u", __func__, item);
146 return false;
147 }
148 }
149 for (const auto &item : GetAbs()) {
150 if (!DoIoctl(fd_, UI_SET_ABSBIT, item)) {
151 HDF_LOGE("%{public}s Error setting abs:%{public}u", __func__, item);
152 return false;
153 }
154 }
155 for (const auto &item : GetRelBits()) {
156 if (!DoIoctl(fd_, UI_SET_RELBIT, item)) {
157 HDF_LOGE("%{public}s Error setting rel:%{public}u", __func__, item);
158 return false;
159 }
160 }
161
162 return true;
163 }
164
EmitEvent(uint16_t type,uint16_t code,uint32_t value) const165 bool VirtualDevice::EmitEvent(uint16_t type, uint16_t code, uint32_t value) const
166 {
167 struct input_event event {};
168 event.type = type;
169 event.code = code;
170 event.value = (int32_t)value;
171
172 #ifndef __MUSL__
173 gettimeofday(&event.time, nullptr);
174 #endif
175 if (write(fd_, &event, sizeof(event)) < static_cast<ssize_t>(sizeof(event))) {
176 HDF_LOGE("%{public}s Event write failed, fd:%{public}d, errno:%{public}d", __func__, fd_, errno);
177 return false;
178 }
179 return true;
180 }
181
GetEventTypes() const182 const std::vector<uint32_t> &VirtualDevice::GetEventTypes() const
183 {
184 return eventTypes_;
185 }
186
GetKeys() const187 const std::vector<uint32_t> &VirtualDevice::GetKeys() const
188 {
189 return keys_;
190 }
191
GetProperties() const192 const std::vector<uint32_t> &VirtualDevice::GetProperties() const
193 {
194 return properties_;
195 }
196
GetAbs() const197 const std::vector<uint32_t> &VirtualDevice::GetAbs() const
198 {
199 return abs_;
200 }
201
GetRelBits() const202 const std::vector<uint32_t> &VirtualDevice::GetRelBits() const
203 {
204 return relBits_;
205 }
206
GetLeds() const207 const std::vector<uint32_t> &VirtualDevice::GetLeds() const
208 {
209 return leds_;
210 }
211
GetRepeats() const212 const std::vector<uint32_t> &VirtualDevice::GetRepeats() const
213 {
214 return repeats_;
215 }
216
GetMiscellaneous() const217 const std::vector<uint32_t> &VirtualDevice::GetMiscellaneous() const
218 {
219 return miscellaneous_;
220 }
221
GetSwitches() const222 const std::vector<uint32_t> &VirtualDevice::GetSwitches() const
223 {
224 return switches_;
225 }
226
CreateKey()227 bool VirtualDevice::CreateKey()
228 {
229 auto fun = [&](int32_t uiSet, const std::vector<uint32_t> &list) -> bool {
230 for (const auto &item : list) {
231 if (ioctl(fd_, uiSet, item) < 0) {
232 HDF_LOGE("%{public}s not setting event type: %{public}d, deviceName:%{public}s",
233 __func__, item, deviceName_);
234 return false;
235 }
236 }
237 return true;
238 };
239 std::map<int32_t, std::vector<uint32_t>> uinputTypes;
240 uinputTypes[UI_SET_EVBIT] = GetEventTypes();
241 uinputTypes[UI_SET_KEYBIT] = GetKeys();
242 uinputTypes[UI_SET_PROPBIT] = GetProperties();
243 uinputTypes[UI_SET_ABSBIT] = GetAbs();
244 uinputTypes[UI_SET_RELBIT] = GetRelBits();
245
246 uinputTypes[UI_SET_MSCBIT] = GetMiscellaneous();
247 uinputTypes[UI_SET_LEDBIT] = GetLeds();
248 uinputTypes[UI_SET_SWBIT] = GetSwitches();
249 uinputTypes[UI_SET_FFBIT] = GetRepeats();
250
251 for (const auto &item : uinputTypes) {
252 if (!fun(item.first, item.second)) {
253 return false;
254 }
255 }
256 return true;
257 }
258 } // namespace ExternalDeviceManager
259 } // namespace OHOS
260