1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "virtual_device_base.h"
18 
19 #include <android-base/logging.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <inttypes.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #include "log/log.h"
27 
28 using cuttlefish_input_service::VirtualDeviceBase;
29 
30 namespace {
31 
DoIoctl(int fd,int request,const uint32_t value)32 bool DoIoctl(int fd, int request, const uint32_t value) {
33   int rc = ioctl(fd, request, value);
34   if (rc < 0) {
35     SLOGE("ioctl failed (%s)", strerror(errno));
36     return false;
37   }
38   return true;
39 }
40 
41 }  // namespace
42 
VirtualDeviceBase(const char * device_name,uint16_t product_id)43 VirtualDeviceBase::VirtualDeviceBase(const char* device_name,
44                                      uint16_t product_id)
45     : device_name_(device_name),
46       bus_type_(BUS_USB),
47       vendor_id_(0x6006),
48       product_id_(product_id),
49       version_(1) {}
50 
~VirtualDeviceBase()51 VirtualDeviceBase::~VirtualDeviceBase() {
52   if (fd_ >= 0) {
53     close(fd_);
54     fd_ = -1;
55   }
56 }
57 
SetUp()58 bool VirtualDeviceBase::SetUp() {
59   fd_ = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
60   if (fd_ < 0) {
61     SLOGE("Failed to open /dev/uinput (%s)", strerror(errno));
62     return false;
63   }
64 
65   strncpy(dev_.name, device_name_, sizeof(dev_.name));
66   dev_.id.bustype = bus_type_;
67   dev_.id.vendor = vendor_id_;
68   dev_.id.product = product_id_;
69   dev_.id.version = version_;
70 
71   for (uint32_t evt_type : GetEventTypes()) {
72     if (!DoIoctl(fd_, UI_SET_EVBIT, evt_type)) {
73       SLOGE("Error setting event type: %" PRIu32, evt_type);
74       return false;
75     }
76   }
77 
78   for (uint32_t key : GetKeys()) {
79     if (!DoIoctl(fd_, UI_SET_KEYBIT, key)) {
80       SLOGE("Error setting key: %" PRIu32, key);
81       return false;
82     }
83   }
84 
85   for (uint32_t property : GetProperties()) {
86     if (!DoIoctl(fd_, UI_SET_PROPBIT, property)) {
87       SLOGE("Error setting property: %" PRIu32, property);
88       return false;
89     }
90   }
91 
92   for (uint32_t abs : GetAbs()) {
93     if (!DoIoctl(fd_, UI_SET_ABSBIT, abs)) {
94       SLOGE("Error setting abs: %" PRIu32, abs);
95       return false;
96     }
97   }
98 
99   if (write(fd_, &dev_, sizeof(dev_)) < 0) {
100     SLOGE("Unable to set input device info (%s)", strerror(errno));
101     return false;
102   }
103   if (ioctl(fd_, UI_DEV_CREATE) < 0) {
104     SLOGE("Unable to create input device (%s)", strerror(errno));
105     return false;
106   }
107 
108   LOG(INFO) << "set up virtual device";
109 
110   return true;
111 }
112 
EmitEvent(uint16_t type,uint16_t code,uint32_t value)113 bool VirtualDeviceBase::EmitEvent(uint16_t type,
114                                   uint16_t code,
115                                   uint32_t value) {
116   struct input_event event {};
117   event.type = type;
118   event.code = code;
119   event.value = value;
120 
121   if (write(fd_, &event, sizeof(event)) < static_cast<ssize_t>(sizeof(event))) {
122     SLOGE("Event write failed (%s): aborting", strerror(errno));
123     return false;
124   }
125   return true;
126 }
127 
128 // By default devices have no event types, keys, properties or absolutes,
129 // subclasses can override this behavior if necessary.
GetEventTypes() const130 const std::vector<const uint32_t>& VirtualDeviceBase::GetEventTypes() const {
131   static const std::vector<const uint32_t> evt_types{};
132   return evt_types;
133 }
GetKeys() const134 const std::vector<const uint32_t>& VirtualDeviceBase::GetKeys() const {
135   static const std::vector<const uint32_t> keys{};
136   return keys;
137 }
GetProperties() const138 const std::vector<const uint32_t>& VirtualDeviceBase::GetProperties() const {
139   static const std::vector<const uint32_t> properties{};
140   return properties;
141 }
GetAbs() const142 const std::vector<const uint32_t>& VirtualDeviceBase::GetAbs() const {
143   static const std::vector<const uint32_t> abs{};
144   return abs;
145 }
146